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 1a940e7
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 @@ -640,6 +641,79 @@ func TestFilterFwActAddDel(t *testing.T) {
}
}

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

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

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

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")
}
fw, ok := filters[0].(*FwFilter)
if !ok {
t.Fatal("Filter is the wrong type")
}

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

pedit, ok := fw.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 TestFilterU32BpfAddDel(t *testing.T) {
t.Skipf("Fd does not match in ci")
tearDown := setUpNetlinkTest(t)
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 1a940e7

Please sign in to comment.