Support IFLA_ADDRESS which changes a device's hardware address.

PiperOrigin-RevId: 645611089
This commit is contained in:
Jing Chen
2024-06-21 23:56:13 -07:00
committed by gVisor bot
parent 646a033213
commit cd3efc6519
2 changed files with 32 additions and 7 deletions
+19 -7
View File
@@ -136,6 +136,7 @@ func (s *Stack) SetInterface(ctx context.Context, msg *nlmsg.Message) *syserr.Er
}
case linux.IFLA_MASTER:
case linux.IFLA_LINKINFO:
case linux.IFLA_ADDRESS:
default:
ctx.Warningf("unexpected attribute: %x", attr)
return syserr.ErrNotSupported
@@ -169,13 +170,24 @@ func (s *Stack) SetInterface(ctx context.Context, msg *nlmsg.Message) *syserr.Er
}
func (s *Stack) setLink(id tcpip.NICID, linkAttrs map[uint16]nlmsg.BytesView) *syserr.Error {
if v, ok := linkAttrs[linux.IFLA_MASTER]; ok {
master, ok := v.Uint32()
if !ok {
return syserr.ErrInvalidArgument
}
if master != 0 {
if err := s.Stack.SetNICCoordinator(id, tcpip.NICID(master)); err != nil {
for t, v := range linkAttrs {
switch t {
case linux.IFLA_MASTER:
master, ok := v.Uint32()
if !ok {
return syserr.ErrInvalidArgument
}
if master != 0 {
if err := s.Stack.SetNICCoordinator(id, tcpip.NICID(master)); err != nil {
return syserr.TranslateNetstackError(err)
}
}
case linux.IFLA_ADDRESS:
addr, err := tcpip.ParseMACAddress(v.String())
if err != nil {
return syserr.ErrInvalidArgument
}
if err := s.Stack.SetNICAddress(id, addr); err != nil {
return syserr.TranslateNetstackError(err)
}
}
+13
View File
@@ -1025,6 +1025,19 @@ func (s *Stack) SetNICCoordinator(id tcpip.NICID, mid tcpip.NICID) tcpip.Error {
return nil
}
// SetNICAddress sets the hardware address which is identified by the nic ID.
func (s *Stack) SetNICAddress(id tcpip.NICID, addr tcpip.LinkAddress) tcpip.Error {
s.mu.Lock()
defer s.mu.Unlock()
nic, ok := s.nics[id]
if !ok {
return &tcpip.ErrUnknownNICID{}
}
nic.NetworkLinkEndpoint.SetLinkAddress(addr)
return nil
}
// NICInfo captures the name and addresses assigned to a NIC.
type NICInfo struct {
Name string