Skip to content

Commit

Permalink
bugfix: parse ipv4 src/dst error
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie17Li committed Sep 8, 2024
1 parent b1ce50c commit b7a442d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
76 changes: 75 additions & 1 deletion filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"testing"
"time"

"github.com/vishvananda/netlink/nl"
"golang.org/x/sys/unix"

"github.com/vishvananda/netlink/nl"
)

func TestFilterAddDel(t *testing.T) {
Expand Down Expand Up @@ -2097,6 +2098,79 @@ func TestFilterIPv6FlowerPedit(t *testing.T) {
}
}

func TestFilterU32PeditAddDel(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()

qdisc, link := setupLinkForTestWithQdisc(t, "foo")

classId := MakeHandle(1, 1)
filter := &U32{
FilterAttrs: FilterAttrs{
LinkIndex: link.Attrs().Index,
Parent: qdisc.Attrs().Handle,
Priority: 1,
Protocol: unix.ETH_P_ALL,
},
ClassId: classId,
}

dstIP := net.ParseIP("172.17.0.2")
peditAction := NewPeditAction()
peditAction.Proto = uint8(nl.IPPROTO_TCP)
peditAction.DstIP = dstIP
filter.Actions = append(filter.Actions, peditAction)

if err := FilterAdd(filter); err != nil {
t.Fatal(err)
}

filters, err := FilterList(link, qdisc.Attrs().Handle)
if err != nil {
t.Fatal(err)
}
if len(filters) != 1 {
t.Fatal("Failed to add filter")
}
u32, ok := filters[0].(*U32)
if !ok {
t.Fatal("Filter is the wrong type")
}

if len(u32.Actions) != 1 {
t.Fatalf("Too few Actions in filter")
}

pedit, ok := u32.Actions[0].(*PeditAction)
if !ok {
t.Fatal("action is the wrong type")
}
if !pedit.DstIP.Equal(dstIP) {
t.Fatal("dstIP is wrong")
}

if err := FilterDel(filter); err != nil {
t.Fatal(err)
}
filters, err = FilterList(link, qdisc.Attrs().Handle)
if err != nil {
t.Fatal(err)
}
if len(filters) != 0 {
t.Fatal("Failed to remove filter")
}

if err := QdiscDel(qdisc); err != nil {
t.Fatal(err)
}
qdiscs, err := SafeQdiscList(link)
if err != nil {
t.Fatal(err)
}
if len(qdiscs) != 0 {
t.Fatal("Failed to remove qdisc")
}
}
func TestFilterU32PoliceAddDel(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
Expand Down
4 changes: 2 additions & 2 deletions nl/tc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ func (p *TcPedit) SetIPv6Dst(ip6 net.IP) {
}

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

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

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

tKey := TcPeditKey{}
tKeyEx := TcPeditKeyEx{}
Expand Down

0 comments on commit b7a442d

Please sign in to comment.