Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flower: add support for mac address matching #872

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions filter_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (filter *U32) Type() string {

type Flower struct {
FilterAttrs
DestEth net.HardwareAddr
DestEthMask net.HardwareAddr
SrcEth net.HardwareAddr
SrcEthMask net.HardwareAddr
DestIP net.IP
DestIPMask net.IPMask
SrcIP net.IP
Expand Down Expand Up @@ -81,6 +85,11 @@ func (filter *Flower) Type() string {
return "flower"
}

func (filter *Flower) encodeMac(parent *nl.RtAttr, mac net.HardwareAddr, mask net.HardwareAddr, macType, maskType int) {
parent.AddRtAttr(macType, mac)
parent.AddRtAttr(maskType, mask)
}

func (filter *Flower) encodeIP(parent *nl.RtAttr, ip net.IP, mask net.IPMask, v4Type, v6Type int, v4MaskType, v6MaskType int) {
ipType := v4Type
maskType := v4MaskType
Expand Down Expand Up @@ -108,6 +117,14 @@ func (filter *Flower) encode(parent *nl.RtAttr) error {
if filter.EthType != 0 {
parent.AddRtAttr(nl.TCA_FLOWER_KEY_ETH_TYPE, htons(filter.EthType))
}
if filter.DestEth != nil {
filter.encodeMac(parent, filter.DestEth, filter.DestEthMask,
nl.TCA_FLOWER_KEY_ETH_DST, nl.TCA_FLOWER_KEY_ETH_DST_MASK)
}
if filter.SrcEth != nil {
filter.encodeMac(parent, filter.SrcEth, filter.SrcEthMask,
nl.TCA_FLOWER_KEY_ETH_SRC, nl.TCA_FLOWER_KEY_ETH_SRC_MASK)
}
if filter.SrcIP != nil {
filter.encodeIP(parent, filter.SrcIP, filter.SrcIPMask,
nl.TCA_FLOWER_KEY_IPV4_SRC, nl.TCA_FLOWER_KEY_IPV6_SRC,
Expand Down Expand Up @@ -178,6 +195,14 @@ func (filter *Flower) encode(parent *nl.RtAttr) error {
func (filter *Flower) decode(data []syscall.NetlinkRouteAttr) error {
for _, datum := range data {
switch datum.Attr.Type {
case nl.TCA_FLOWER_KEY_ETH_DST:
filter.DestEth = datum.Value
case nl.TCA_FLOWER_KEY_ETH_DST_MASK:
filter.DestEthMask = datum.Value
case nl.TCA_FLOWER_KEY_ETH_SRC:
filter.SrcEth = datum.Value
case nl.TCA_FLOWER_KEY_ETH_SRC_MASK:
filter.SrcEthMask = datum.Value
case nl.TCA_FLOWER_KEY_ETH_TYPE:
filter.EthType = ntohs(datum.Value)
case nl.TCA_FLOWER_KEY_IPV4_SRC, nl.TCA_FLOWER_KEY_IPV6_SRC:
Expand Down
17 changes: 16 additions & 1 deletion filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,6 @@ func TestFilterMatchAllAddDel(t *testing.T) {
if len(filters) != 0 {
t.Fatal("Failed to remove filter")
}

}

func TestFilterU32TunnelKeyAddDel(t *testing.T) {
Expand Down Expand Up @@ -1780,6 +1779,10 @@ func TestFilterFlowerAddDel(t *testing.T) {
Priority: 1,
Protocol: unix.ETH_P_ALL,
},
DestEth: net.HardwareAddr([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}),
DestEthMask: net.HardwareAddr([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}),
SrcEth: net.HardwareAddr([]byte{0xba, 0xad, 0xca, 0xca, 0x00, 0x00}),
SrcEthMask: net.HardwareAddr([]byte{0xff, 0xff, 0xff, 0xff, 0x00, 0x00}),
DestIP: net.ParseIP("1.0.0.1"),
DestIPMask: testMask,
SrcIP: net.ParseIP("2.0.0.1"),
Expand Down Expand Up @@ -1827,6 +1830,18 @@ func TestFilterFlowerAddDel(t *testing.T) {
t.Fatal("Filter is the wrong type")
}

if !reflect.DeepEqual(filter.DestEth, flower.DestEth) {
t.Fatalf("Flower DestEth doesn't match")
}
if !reflect.DeepEqual(filter.DestEthMask, flower.DestEthMask) {
t.Fatalf("Flower DestEthMask doesn't match")
}
if !reflect.DeepEqual(filter.SrcEth, flower.SrcEth) {
t.Fatalf("Flower SrcEth doesn't match")
}
if !reflect.DeepEqual(filter.SrcEthMask, flower.SrcEthMask) {
t.Fatalf("Flower SrcEthMask doesn't match")
}
if filter.EthType != flower.EthType {
t.Fatalf("Flower EthType doesn't match")
}
Expand Down