Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Assign a random MAC to the bridge #1

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 22 additions & 1 deletion netlinktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"bytes"
"crypto/rand"
"net"

log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"net"
)

func main() {
Expand Down Expand Up @@ -58,10 +60,29 @@ func createTAPAdapter(tapName string) (*netlink.Tuntap, error) {
func createBridge(bridgeName string) (*netlink.Bridge, error) {
la := netlink.NewLinkAttrs()
la.Name = bridgeName
// Assign a mac to the bridge - if we don't do this it will take the lowest address
mac, err := randomMAC()
if err != nil {
return nil, err
}
la.HardwareAddr = mac
bridge := &netlink.Bridge{LinkAttrs: la}
return bridge, addLink(bridge)
}

func randomMAC() (net.HardwareAddr, error) {
mac := make([]byte, 6)
if _, err := rand.Read(mac); err != nil {
return nil, err
}

// In the first byte of the MAC, the 'multicast' bit should be
// clear and 'locally administered' bit should be set.
mac[0] = (mac[0] & 0xFE) | 0x02

return net.HardwareAddr(mac), nil
}

func addLink(link netlink.Link) (err error) {
if err = netlink.LinkAdd(link); err == nil {
err = netlink.LinkSetUp(link)
Expand Down