Don't hold link address in tcpip.Address

All tcpip.Endpoints use a tcpip.FullAddress to hold addresses associated
with an outgoing or incoming packet, but the struct only had a field for
a tcpip.Address. This field was overloaded to also hold a link address
(which is normally held in a tcpip.LinkAddress). This worked relatively
fine until the change to make addresses opaque types.

commit 64268c8483 required the use of a
workaround to continue passing around link addresses through the
tcpip.Address field. This workaround requires assumptions about the
size of the hardware address and unncessary jumping-through-hoops.

This change just introduces a new field to tcpip.FullAddress with type
tcpip.LinkAddress that can hold link addresses and this new field will
be used by packet endpoints.

The change referenced above is cl/532284732.

PiperOrigin-RevId: 536561363
This commit is contained in:
Ghanan Gowripalan
2023-05-30 18:14:32 -07:00
committed by gVisor bot
parent d23e3cca23
commit a055645a79
7 changed files with 67 additions and 51 deletions
+4 -11
View File
@@ -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:
+52 -18
View File
@@ -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(),
+1 -1
View File
@@ -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)
}
}
+1 -1
View File
@@ -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)
}
+3 -3
View File
@@ -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)
}
+4 -1
View File
@@ -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.
+2 -16
View File
@@ -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.