Skip to content

Commit

Permalink
Support IFLA_ADDRESS which changes a device's hardware address.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 645611089
  • Loading branch information
milantracy authored and gvisor-bot committed Jun 22, 2024
1 parent 646a033 commit cd3efc6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pkg/sentry/socket/netstack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/tcpip/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd3efc6

Please sign in to comment.