Skip to content

Commit 0e7078e

Browse files
jrifeaboch
authored andcommitted
netkit: Allow setting MAC address in L2 mode
Signed-off-by: Jordan Rife <[email protected]>
1 parent 0af3215 commit 0e7078e

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

link_linux.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,8 +2671,8 @@ func (h *Handle) LinkSetGroup(link Link, group int) error {
26712671
}
26722672

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

26782678
data := linkInfo.AddRtAttr(nl.IFLA_INFO_DATA, nil)
@@ -2724,6 +2724,9 @@ func addNetkitAttrs(nk *Netkit, linkInfo *nl.RtAttr, flag int) error {
27242724
peer.AddRtAttr(unix.IFLA_NET_NS_FD, nl.Uint32Attr(uint32(ns)))
27252725
}
27262726
}
2727+
if nk.peerLinkAttrs.HardwareAddr != nil {
2728+
peer.AddRtAttr(unix.IFLA_ADDRESS, []byte(nk.peerLinkAttrs.HardwareAddr))
2729+
}
27272730
return nil
27282731
}
27292732

link_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ func testLinkAddDel(t *testing.T, link Link) {
8686
if resultPrimary.SupportsScrub() && resultPrimary.PeerScrub != inputPrimary.PeerScrub {
8787
t.Fatalf("Peer Scrub is %d, should be %d", int(resultPrimary.PeerScrub), int(inputPrimary.PeerScrub))
8888
}
89+
if inputPrimary.Mode == NETKIT_MODE_L2 && inputPrimary.HardwareAddr != nil {
90+
if inputPrimary.HardwareAddr.String() != resultPrimary.HardwareAddr.String() {
91+
t.Fatalf("Hardware address is %s, should be %s", resultPrimary.HardwareAddr.String(), inputPrimary.HardwareAddr.String())
92+
}
93+
}
8994

9095
if inputPrimary.peerLinkAttrs.Name != "" {
9196
var resultPeer *Netkit
@@ -117,6 +122,11 @@ func testLinkAddDel(t *testing.T, link Link) {
117122
if resultPrimary.Scrub != resultPeer.PeerScrub {
118123
t.Fatalf("PeerScrub from peer is %d, should be %d", int(resultPeer.PeerScrub), int(resultPrimary.Scrub))
119124
}
125+
if inputPrimary.Mode == NETKIT_MODE_L2 && inputPrimary.peerLinkAttrs.HardwareAddr != nil {
126+
if inputPrimary.peerLinkAttrs.HardwareAddr.String() != resultPeer.HardwareAddr.String() {
127+
t.Fatalf("Peer hardware address is %s, should be %s", resultPeer.HardwareAddr.String(), inputPrimary.peerLinkAttrs.HardwareAddr.String())
128+
}
129+
}
120130
}
121131
}
122132

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

10621072
netkit := &Netkit{
10631073
LinkAttrs: LinkAttrs{
1064-
Name: "foo",
1074+
Name: "foo",
1075+
HardwareAddr: net.HardwareAddr{0x00, 0x11, 0x22, 0x33, 0x44, 0x55},
10651076
},
10661077
Mode: NETKIT_MODE_L2,
10671078
Policy: NETKIT_POLICY_FORWARD,
@@ -1070,7 +1081,8 @@ func TestLinkAddDelNetkit(t *testing.T) {
10701081
PeerScrub: NETKIT_SCRUB_NONE,
10711082
}
10721083
peerAttr := &LinkAttrs{
1073-
Name: "bar",
1084+
Name: "bar",
1085+
HardwareAddr: net.HardwareAddr{0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB},
10741086
}
10751087
netkit.SetPeerAttrs(peerAttr)
10761088
testLinkAddDel(t, netkit)

0 commit comments

Comments
 (0)