Skip to content

Commit

Permalink
netkit: Allow setting MAC address in L2 mode
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Rife <[email protected]>
  • Loading branch information
jrife committed Feb 23, 2025
1 parent 0af3215 commit 579b483
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 5 additions & 2 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2671,8 +2671,8 @@ func (h *Handle) LinkSetGroup(link Link, group int) error {
}

func addNetkitAttrs(nk *Netkit, linkInfo *nl.RtAttr, flag int) error {
if nk.peerLinkAttrs.HardwareAddr != nil || nk.HardwareAddr != nil {
return fmt.Errorf("netkit doesn't support setting Ethernet")
if nk.Mode != NETKIT_MODE_L2 && (nk.LinkAttrs.HardwareAddr != nil || nk.peerLinkAttrs.HardwareAddr != nil) {
return fmt.Errorf("netkit only allows setting Ethernet in L2 mode")
}

data := linkInfo.AddRtAttr(nl.IFLA_INFO_DATA, nil)
Expand Down Expand Up @@ -2724,6 +2724,9 @@ func addNetkitAttrs(nk *Netkit, linkInfo *nl.RtAttr, flag int) error {
peer.AddRtAttr(unix.IFLA_NET_NS_FD, nl.Uint32Attr(uint32(ns)))
}
}
if nk.peerLinkAttrs.HardwareAddr != nil {
peer.AddRtAttr(unix.IFLA_ADDRESS, []byte(nk.peerLinkAttrs.HardwareAddr))
}
return nil
}

Expand Down
16 changes: 14 additions & 2 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func testLinkAddDel(t *testing.T, link Link) {
if resultPrimary.SupportsScrub() && resultPrimary.PeerScrub != inputPrimary.PeerScrub {
t.Fatalf("Peer Scrub is %d, should be %d", int(resultPrimary.PeerScrub), int(inputPrimary.PeerScrub))
}
if inputPrimary.Mode == NETKIT_MODE_L2 && inputPrimary.HardwareAddr != nil {
if inputPrimary.HardwareAddr.String() != resultPrimary.HardwareAddr.String() {
t.Fatalf("Hardware address is %s, should be %s", resultPrimary.HardwareAddr.String(), inputPrimary.HardwareAddr.String())
}
}

if inputPrimary.peerLinkAttrs.Name != "" {
var resultPeer *Netkit
Expand Down Expand Up @@ -117,6 +122,11 @@ func testLinkAddDel(t *testing.T, link Link) {
if resultPrimary.Scrub != resultPeer.PeerScrub {
t.Fatalf("PeerScrub from peer is %d, should be %d", int(resultPeer.PeerScrub), int(resultPrimary.Scrub))
}
if inputPrimary.Mode == NETKIT_MODE_L2 && inputPrimary.peerLinkAttrs.HardwareAddr != nil {
if inputPrimary.peerLinkAttrs.HardwareAddr.String() != resultPeer.HardwareAddr.String() {
t.Fatalf("Peer hardware address is %s, should be %s", resultPeer.HardwareAddr.String(), inputPrimary.peerLinkAttrs.HardwareAddr.String())
}
}
}
}

Expand Down Expand Up @@ -1061,7 +1071,8 @@ func TestLinkAddDelNetkit(t *testing.T) {

netkit := &Netkit{
LinkAttrs: LinkAttrs{
Name: "foo",
Name: "foo",
HardwareAddr: net.HardwareAddr{0x00, 0x11, 0x22, 0x33, 0x44, 0x55},
},
Mode: NETKIT_MODE_L2,
Policy: NETKIT_POLICY_FORWARD,
Expand All @@ -1070,7 +1081,8 @@ func TestLinkAddDelNetkit(t *testing.T) {
PeerScrub: NETKIT_SCRUB_NONE,
}
peerAttr := &LinkAttrs{
Name: "bar",
Name: "bar",
HardwareAddr: net.HardwareAddr{0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB},
}
netkit.SetPeerAttrs(peerAttr)
testLinkAddDel(t, netkit)
Expand Down

0 comments on commit 579b483

Please sign in to comment.