diff --git a/pkg/sentry/socket/socket.go b/pkg/sentry/socket/socket.go index 5e6d248f5..5e9dc097c 100644 --- a/pkg/sentry/socket/socket.go +++ b/pkg/sentry/socket/socket.go @@ -498,7 +498,7 @@ func ConvertAddress(family int, addr tcpip.FullAddress) (linux.SockAddr, uint32) out.Family = linux.AF_PACKET out.InterfaceIndex = int32(addr.NIC) out.HardwareAddrLen = header.EthernetAddressSize - copy(out.HardwareAddr[:], addr.Addr.AsSlice()) + copy(out.HardwareAddr[:], addr.LinkAddr) return &out, uint32(sockAddrLinkSize) default: @@ -570,16 +570,9 @@ func AddressAndFamily(addr []byte) (tcpip.FullAddress, uint16, *syserr.Error) { } return tcpip.FullAddress{ - NIC: tcpip.NICID(a.InterfaceIndex), - // This is a hack. FullAddress is designed to carry IP - // addresses, but it's overloaded here to carry a link - // address. We stick the 6 byte link address to 10 - // zeroed bytes. - Addr: tcpip.AddrFrom16Slice(append( - a.HardwareAddr[:header.EthernetAddressSize], - []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}..., - )), - Port: Ntohs(a.Protocol), + NIC: tcpip.NICID(a.InterfaceIndex), + LinkAddr: tcpip.LinkAddress(a.HardwareAddr[:a.HardwareAddrLen]), + Port: Ntohs(a.Protocol), }, family, nil case linux.AF_UNSPEC: diff --git a/pkg/tcpip/adapters/gonet/gonet_test.go b/pkg/tcpip/adapters/gonet/gonet_test.go index 1994523d2..43bf263c7 100644 --- a/pkg/tcpip/adapters/gonet/gonet_test.go +++ b/pkg/tcpip/adapters/gonet/gonet_test.go @@ -136,8 +136,11 @@ func TestCloseReader(t *testing.T) { s.Wait() }() - addr := tcpip.FullAddress{NICID, tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), 11211} - + addr := tcpip.FullAddress{ + NIC: NICID, + Addr: tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), + Port: 11211, + } protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: addr.Addr.WithPrefix(), @@ -196,7 +199,11 @@ func TestCloseReaderWithForwarder(t *testing.T) { s.Wait() }() - addr := tcpip.FullAddress{NICID, tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), 11211} + addr := tcpip.FullAddress{ + NIC: NICID, + Addr: tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), + Port: 11211, + } protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: addr.Addr.WithPrefix(), @@ -256,7 +263,11 @@ func TestCloseRead(t *testing.T) { s.Wait() }() - addr := tcpip.FullAddress{NICID, tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), 11211} + addr := tcpip.FullAddress{ + NIC: NICID, + Addr: tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), + Port: 11211, + } protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: addr.Addr.WithPrefix(), @@ -314,7 +325,11 @@ func TestCloseWrite(t *testing.T) { s.Wait() }() - addr := tcpip.FullAddress{NICID, tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), 11211} + addr := tcpip.FullAddress{ + NIC: NICID, + Addr: tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), + Port: 11211, + } protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: addr.Addr.WithPrefix(), @@ -378,7 +393,11 @@ func TestCloseStack(t *testing.T) { t.Fatalf("newLoopbackStack() = %v", err) } - addr := tcpip.FullAddress{NICID, tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), 11211} + addr := tcpip.FullAddress{ + NIC: NICID, + Addr: tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), + Port: 11211, + } protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: addr.Addr.WithPrefix(), @@ -441,7 +460,7 @@ func TestUDPForwarder(t *testing.T) { }() ip1 := tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()) - addr1 := tcpip.FullAddress{NICID, ip1, 11211} + addr1 := tcpip.FullAddress{NIC: NICID, Addr: ip1, Port: 11211} protocolAddr1 := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: ip1.WithPrefix(), @@ -450,7 +469,7 @@ func TestUDPForwarder(t *testing.T) { t.Fatalf("AddProtocolAddress(%d, %+v, {}): %s", NICID, protocolAddr1, err) } ip2 := tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 2).To4()) - addr2 := tcpip.FullAddress{NICID, ip2, 11311} + addr2 := tcpip.FullAddress{NIC: NICID, Addr: ip2, Port: 11311} protocolAddr2 := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: ip2.WithPrefix(), @@ -513,7 +532,11 @@ func TestDeadlineChange(t *testing.T) { s.Wait() }() - addr := tcpip.FullAddress{NICID, tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), 11211} + addr := tcpip.FullAddress{ + NIC: NICID, + Addr: tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), + Port: 11211, + } protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, @@ -575,7 +598,7 @@ func TestPacketConnTransfer(t *testing.T) { }() ip1 := tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()) - addr1 := tcpip.FullAddress{NICID, ip1, 11211} + addr1 := tcpip.FullAddress{NIC: NICID, Addr: ip1, Port: 11211} protocolAddr1 := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: ip1.WithPrefix(), @@ -584,7 +607,7 @@ func TestPacketConnTransfer(t *testing.T) { t.Fatalf("AddProtocolAddress(%d, %+v, {}): %s", NICID, protocolAddr1, err) } ip2 := tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 2).To4()) - addr2 := tcpip.FullAddress{NICID, ip2, 11311} + addr2 := tcpip.FullAddress{NIC: NICID, Addr: ip2, Port: 11311} protocolAddr2 := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: ip2.WithPrefix(), @@ -643,7 +666,7 @@ func TestConnectedPacketConnTransfer(t *testing.T) { }() ip := tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()) - addr := tcpip.FullAddress{NICID, ip, 11211} + addr := tcpip.FullAddress{NIC: NICID, Addr: ip, Port: 11211} protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: ip.WithPrefix(), @@ -693,7 +716,7 @@ func makePipe() (c1, c2 net.Conn, stop func(), err error) { } ip := tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()) - addr := tcpip.FullAddress{NICID, ip, 11211} + addr := tcpip.FullAddress{NIC: NICID, Addr: ip, Port: 11211} protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: ip.WithPrefix(), @@ -793,7 +816,7 @@ func TestTCPDialError(t *testing.T) { }() ip := tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()) - addr := tcpip.FullAddress{NICID, ip, 11211} + addr := tcpip.FullAddress{NIC: NICID, Addr: ip, Port: 11211} switch _, err := DialTCP(s, addr, ipv4.ProtocolNumber); err := err.(type) { case *net.OpError: @@ -815,7 +838,11 @@ func TestDialContextTCPCanceled(t *testing.T) { s.Wait() }() - addr := tcpip.FullAddress{NICID, tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), 11211} + addr := tcpip.FullAddress{ + NIC: NICID, + Addr: tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), + Port: 11211, + } protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: addr.Addr.WithPrefix(), @@ -843,7 +870,11 @@ func TestDialContextTCPTimeout(t *testing.T) { s.Wait() }() - addr := tcpip.FullAddress{NICID, tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), 11211} + addr := tcpip.FullAddress{ + NIC: NICID, + Addr: tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), + Port: 11211, + } protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: addr.Addr.WithPrefix(), @@ -903,8 +934,11 @@ func TestInterruptListender(t *testing.T) { s.Wait() }() - addr := tcpip.FullAddress{NICID, tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), 11211} - + addr := tcpip.FullAddress{ + NIC: NICID, + Addr: tcpip.AddrFromSlice(net.IPv4(169, 254, 10, 1).To4()), + Port: 11211, + } protocolAddr := tcpip.ProtocolAddress{ Protocol: ipv4.ProtocolNumber, AddressWithPrefix: addr.Addr.WithPrefix(), diff --git a/pkg/tcpip/sample/tun_tcp_connect/main.go b/pkg/tcpip/sample/tun_tcp_connect/main.go index 2616fa863..6f600e64e 100644 --- a/pkg/tcpip/sample/tun_tcp_connect/main.go +++ b/pkg/tcpip/sample/tun_tcp_connect/main.go @@ -171,7 +171,7 @@ func main() { // Bind if a port is specified. if localPort != 0 { - if err := ep.Bind(tcpip.FullAddress{0, tcpip.Address{}, localPort}); err != nil { + if err := ep.Bind(tcpip.FullAddress{Port: localPort}); err != nil { log.Fatal("Bind failed: ", err) } } diff --git a/pkg/tcpip/sample/tun_tcp_echo/main.go b/pkg/tcpip/sample/tun_tcp_echo/main.go index 9be5e60e1..29f760f2a 100644 --- a/pkg/tcpip/sample/tun_tcp_echo/main.go +++ b/pkg/tcpip/sample/tun_tcp_echo/main.go @@ -205,7 +205,7 @@ func main() { defer ep.Close() - if err := ep.Bind(tcpip.FullAddress{0, tcpip.Address{}, uint16(localPort)}); err != nil { + if err := ep.Bind(tcpip.FullAddress{Port: uint16(localPort)}); err != nil { log.Fatal("Bind failed: ", err) } diff --git a/pkg/tcpip/stack/transport_test.go b/pkg/tcpip/stack/transport_test.go index 624e1d1d8..51a569d00 100644 --- a/pkg/tcpip/stack/transport_test.go +++ b/pkg/tcpip/stack/transport_test.go @@ -391,7 +391,7 @@ func TestTransportReceive(t *testing.T) { t.Fatalf("NewEndpoint failed: %v", err) } - if err := ep.Connect(tcpip.FullAddress{0, tcpip.AddrFromSlice([]byte("\x02\x00\x00\x00")), 0}); err != nil { + if err := ep.Connect(tcpip.FullAddress{Addr: tcpip.AddrFromSlice([]byte("\x02\x00\x00\x00"))}); err != nil { t.Fatalf("Connect failed: %v", err) } @@ -469,7 +469,7 @@ func TestTransportControlReceive(t *testing.T) { t.Fatalf("NewEndpoint failed: %v", err) } - if err := ep.Connect(tcpip.FullAddress{0, tcpip.AddrFromSlice([]byte("\x02\x00\x00\x00")), 0}); err != nil { + if err := ep.Connect(tcpip.FullAddress{Addr: tcpip.AddrFromSlice([]byte("\x02\x00\x00\x00"))}); err != nil { t.Fatalf("Connect failed: %v", err) } @@ -553,7 +553,7 @@ func TestTransportSend(t *testing.T) { t.Fatalf("NewEndpoint failed: %v", err) } - if err := ep.Connect(tcpip.FullAddress{0, tcpip.AddrFromSlice([]byte("\x02\x00\x00\x00")), 0}); err != nil { + if err := ep.Connect(tcpip.FullAddress{Addr: tcpip.AddrFromSlice([]byte("\x02\x00\x00\x00"))}); err != nil { t.Fatalf("Connect failed: %v", err) } diff --git a/pkg/tcpip/tcpip.go b/pkg/tcpip/tcpip.go index 1af4685af..92ac54db4 100644 --- a/pkg/tcpip/tcpip.go +++ b/pkg/tcpip/tcpip.go @@ -509,13 +509,16 @@ type FullAddress struct { // This may not be used by all endpoint types. NIC NICID - // Addr is the network or link layer address. + // Addr is the network address. Addr Address // Port is the transport port. // // This may not be used by all endpoint types. Port uint16 + + // LinkAddr is the link layer address. + LinkAddr LinkAddress } // Payloader is an interface that provides data. diff --git a/pkg/tcpip/transport/packet/endpoint.go b/pkg/tcpip/transport/packet/endpoint.go index 48f760da2..668bd5957 100644 --- a/pkg/tcpip/transport/packet/endpoint.go +++ b/pkg/tcpip/transport/packet/endpoint.go @@ -25,7 +25,6 @@ package packet import ( - "fmt" "io" "time" @@ -220,11 +219,7 @@ func (ep *endpoint) Write(p tcpip.Payloader, opts tcpip.WriteOptions) (int64, tc var remote tcpip.LinkAddress if to := opts.To; to != nil { - // This is a hack. FullAddress is designed to carry IP - // addresses, but it's overloaded here to carry a link address. - // Assume the address is a 6 byte link address prepended to 10 - // zeroed bytes. - remote = tcpip.LinkAddress(to.Addr.AsSlice()[:header.EthernetAddressSize]) + remote = to.LinkAddr if n := to.NIC; n != 0 { nicID = n @@ -456,16 +451,7 @@ func (ep *endpoint) HandlePacket(nicID tcpip.NICID, netProto tcpip.NetworkProtoc if len(pkt.LinkHeader().Slice()) != 0 { hdr := header.Ethernet(pkt.LinkHeader().Slice()) - // This is a hack. FullAddress is designed to carry IP - // addresses, but it's overloaded here to carry a link address. - // Assume the address is a 6 byte link address prepended to 10 - // zeroed bytes. - if len(hdr.SourceAddress()) != 6 { - panic(fmt.Sprintf("invalid ethernet address size: %d", len(hdr.SourceAddress()))) - } - rcvdPkt.senderAddr.Addr = tcpip.AddrFrom16Slice( - append([]byte(hdr.SourceAddress()), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...), - ) + rcvdPkt.senderAddr.LinkAddr = hdr.SourceAddress() } // Raw packet endpoints include link-headers in received packets.