Skip to content

Commit b7a442d

Browse files
committed
bugfix: parse ipv4 src/dst error
1 parent b1ce50c commit b7a442d

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

filter_test.go

+75-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
"testing"
1010
"time"
1111

12-
"github.com/vishvananda/netlink/nl"
1312
"golang.org/x/sys/unix"
13+
14+
"github.com/vishvananda/netlink/nl"
1415
)
1516

1617
func TestFilterAddDel(t *testing.T) {
@@ -2097,6 +2098,79 @@ func TestFilterIPv6FlowerPedit(t *testing.T) {
20972098
}
20982099
}
20992100

2101+
func TestFilterU32PeditAddDel(t *testing.T) {
2102+
tearDown := setUpNetlinkTest(t)
2103+
defer tearDown()
2104+
2105+
qdisc, link := setupLinkForTestWithQdisc(t, "foo")
2106+
2107+
classId := MakeHandle(1, 1)
2108+
filter := &U32{
2109+
FilterAttrs: FilterAttrs{
2110+
LinkIndex: link.Attrs().Index,
2111+
Parent: qdisc.Attrs().Handle,
2112+
Priority: 1,
2113+
Protocol: unix.ETH_P_ALL,
2114+
},
2115+
ClassId: classId,
2116+
}
2117+
2118+
dstIP := net.ParseIP("172.17.0.2")
2119+
peditAction := NewPeditAction()
2120+
peditAction.Proto = uint8(nl.IPPROTO_TCP)
2121+
peditAction.DstIP = dstIP
2122+
filter.Actions = append(filter.Actions, peditAction)
2123+
2124+
if err := FilterAdd(filter); err != nil {
2125+
t.Fatal(err)
2126+
}
2127+
2128+
filters, err := FilterList(link, qdisc.Attrs().Handle)
2129+
if err != nil {
2130+
t.Fatal(err)
2131+
}
2132+
if len(filters) != 1 {
2133+
t.Fatal("Failed to add filter")
2134+
}
2135+
u32, ok := filters[0].(*U32)
2136+
if !ok {
2137+
t.Fatal("Filter is the wrong type")
2138+
}
2139+
2140+
if len(u32.Actions) != 1 {
2141+
t.Fatalf("Too few Actions in filter")
2142+
}
2143+
2144+
pedit, ok := u32.Actions[0].(*PeditAction)
2145+
if !ok {
2146+
t.Fatal("action is the wrong type")
2147+
}
2148+
if !pedit.DstIP.Equal(dstIP) {
2149+
t.Fatal("dstIP is wrong")
2150+
}
2151+
2152+
if err := FilterDel(filter); err != nil {
2153+
t.Fatal(err)
2154+
}
2155+
filters, err = FilterList(link, qdisc.Attrs().Handle)
2156+
if err != nil {
2157+
t.Fatal(err)
2158+
}
2159+
if len(filters) != 0 {
2160+
t.Fatal("Failed to remove filter")
2161+
}
2162+
2163+
if err := QdiscDel(qdisc); err != nil {
2164+
t.Fatal(err)
2165+
}
2166+
qdiscs, err := SafeQdiscList(link)
2167+
if err != nil {
2168+
t.Fatal(err)
2169+
}
2170+
if len(qdiscs) != 0 {
2171+
t.Fatal("Failed to remove qdisc")
2172+
}
2173+
}
21002174
func TestFilterU32PoliceAddDel(t *testing.T) {
21012175
tearDown := setUpNetlinkTest(t)
21022176
defer tearDown()

nl/tc_linux.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ func (p *TcPedit) SetIPv6Dst(ip6 net.IP) {
15331533
}
15341534

15351535
func (p *TcPedit) SetIPv4Src(ip net.IP) {
1536-
u32 := NativeEndian().Uint32(ip[:4])
1536+
u32 := NativeEndian().Uint32(ip[12:16])
15371537

15381538
tKey := TcPeditKey{}
15391539
tKeyEx := TcPeditKeyEx{}
@@ -1549,7 +1549,7 @@ func (p *TcPedit) SetIPv4Src(ip net.IP) {
15491549
}
15501550

15511551
func (p *TcPedit) SetIPv4Dst(ip net.IP) {
1552-
u32 := NativeEndian().Uint32(ip[:4])
1552+
u32 := NativeEndian().Uint32(ip[12:16])
15531553

15541554
tKey := TcPeditKey{}
15551555
tKeyEx := TcPeditKeyEx{}

0 commit comments

Comments
 (0)