From abd993f608680a26937d7e00fb4fb8db1d4d1d0b Mon Sep 17 00:00:00 2001 From: Ghanan Gowripalan Date: Wed, 26 Jan 2022 13:21:16 -0800 Subject: [PATCH] Don't pass link addresses in rx path ...as they are not used in all cases expect in the packet endpoint which can get the link address directly from the link header. PiperOrigin-RevId: 424427195 --- pkg/tcpip/link/channel/channel.go | 7 +-- pkg/tcpip/link/ethernet/ethernet.go | 5 +- pkg/tcpip/link/ethernet/ethernet_test.go | 7 +-- pkg/tcpip/link/fdbased/endpoint.go | 2 +- pkg/tcpip/link/fdbased/endpoint_test.go | 17 +----- pkg/tcpip/link/fdbased/mmap.go | 12 +--- pkg/tcpip/link/fdbased/packet_dispatchers.go | 24 ++------ pkg/tcpip/link/loopback/loopback.go | 2 +- pkg/tcpip/link/muxed/injectable.go | 2 +- pkg/tcpip/link/nested/nested.go | 4 +- pkg/tcpip/link/nested/nested_test.go | 12 +--- pkg/tcpip/link/pipe/pipe.go | 11 +--- pkg/tcpip/link/sharedmem/sharedmem.go | 8 +-- pkg/tcpip/link/sharedmem/sharedmem_server.go | 8 +-- pkg/tcpip/link/sharedmem/sharedmem_test.go | 8 +-- pkg/tcpip/link/sniffer/sniffer.go | 4 +- pkg/tcpip/link/tun/device.go | 11 +--- pkg/tcpip/link/waitable/waitable.go | 4 +- pkg/tcpip/link/waitable/waitable_test.go | 12 ++-- pkg/tcpip/network/ipv6/icmp_test.go | 2 +- pkg/tcpip/network/ipv6/ndp_test.go | 61 ++++++++------------ pkg/tcpip/stack/forwarding_test.go | 2 +- pkg/tcpip/stack/nic.go | 11 +--- pkg/tcpip/stack/nic_test.go | 4 +- pkg/tcpip/stack/registration.go | 4 +- pkg/tcpip/tests/utils/utils.go | 6 +- pkg/tcpip/transport/packet/endpoint.go | 2 +- 27 files changed, 77 insertions(+), 175 deletions(-) diff --git a/pkg/tcpip/link/channel/channel.go b/pkg/tcpip/link/channel/channel.go index 36ef274f5..de7ba5ca3 100644 --- a/pkg/tcpip/link/channel/channel.go +++ b/pkg/tcpip/link/channel/channel.go @@ -188,12 +188,7 @@ func (e *Endpoint) NumQueued() int { // InjectInbound injects an inbound packet. func (e *Endpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { - e.InjectLinkAddr(protocol, "", pkt) -} - -// InjectLinkAddr injects an inbound packet with a remote link address. -func (e *Endpoint) InjectLinkAddr(protocol tcpip.NetworkProtocolNumber, remote tcpip.LinkAddress, pkt *stack.PacketBuffer) { - e.dispatcher.DeliverNetworkPacket(remote, "" /* local */, protocol, pkt) + e.dispatcher.DeliverNetworkPacket(protocol, pkt) } // Attach saves the stack network-layer dispatcher for use later when packets diff --git a/pkg/tcpip/link/ethernet/ethernet.go b/pkg/tcpip/link/ethernet/ethernet.go index bf7814245..7c4529ea1 100644 --- a/pkg/tcpip/link/ethernet/ethernet.go +++ b/pkg/tcpip/link/ethernet/ethernet.go @@ -59,7 +59,7 @@ func (e *Endpoint) MTU() uint32 { } // DeliverNetworkPacket implements stack.NetworkDispatcher. -func (e *Endpoint) DeliverNetworkPacket(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { +func (e *Endpoint) DeliverNetworkPacket(_ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { hdr, ok := pkt.LinkHeader().Consume(header.EthernetMinimumSize) if !ok { return @@ -67,8 +67,7 @@ func (e *Endpoint) DeliverNetworkPacket(_, _ tcpip.LinkAddress, _ tcpip.NetworkP // Note, there is no need to check the destination link address here since // the ethernet hardware filters frames based on their destination addresses. - eth := header.Ethernet(hdr) - e.Endpoint.DeliverNetworkPacket(eth.SourceAddress() /* remote */, eth.DestinationAddress() /* local */, eth.Type() /* protocol */, pkt) + e.Endpoint.DeliverNetworkPacket(header.Ethernet(hdr).Type() /* protocol */, pkt) } // Capabilities implements stack.LinkEndpoint. diff --git a/pkg/tcpip/link/ethernet/ethernet_test.go b/pkg/tcpip/link/ethernet/ethernet_test.go index 982e0a072..a2f124fbc 100644 --- a/pkg/tcpip/link/ethernet/ethernet_test.go +++ b/pkg/tcpip/link/ethernet/ethernet_test.go @@ -35,13 +35,10 @@ type testNetworkDispatcher struct { networkPackets int } -func (t *testNetworkDispatcher) DeliverNetworkPacket(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber, _ *stack.PacketBuffer) { +func (t *testNetworkDispatcher) DeliverNetworkPacket(tcpip.NetworkProtocolNumber, *stack.PacketBuffer) { t.networkPackets++ } -func (*testNetworkDispatcher) DeliverOutboundPacket(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber, _ *stack.PacketBuffer) { -} - func TestDeliverNetworkPacket(t *testing.T) { const ( linkAddr = tcpip.LinkAddress("\x02\x02\x03\x04\x05\x06") @@ -68,7 +65,7 @@ func TestDeliverNetworkPacket(t *testing.T) { }) p := stack.NewPacketBuffer(stack.PacketBufferOptions{Data: eth.ToVectorisedView()}) defer p.DecRef() - e.DeliverNetworkPacket("", "", 0, p) + e.DeliverNetworkPacket(0, p) if networkDispatcher.networkPackets != 1 { t.Fatalf("got networkDispatcher.networkPackets = %d, want = 1", networkDispatcher.networkPackets) } diff --git a/pkg/tcpip/link/fdbased/endpoint.go b/pkg/tcpip/link/fdbased/endpoint.go index 490cd9dc5..129397259 100644 --- a/pkg/tcpip/link/fdbased/endpoint.go +++ b/pkg/tcpip/link/fdbased/endpoint.go @@ -764,7 +764,7 @@ func (e *InjectableEndpoint) Attach(dispatcher stack.NetworkDispatcher) { // InjectInbound injects an inbound packet. func (e *InjectableEndpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { - e.dispatcher.DeliverNetworkPacket("" /* remote */, "" /* local */, protocol, pkt) + e.dispatcher.DeliverNetworkPacket(protocol, pkt) } // NewInjectable creates a new fd-based InjectableEndpoint. diff --git a/pkg/tcpip/link/fdbased/endpoint_test.go b/pkg/tcpip/link/fdbased/endpoint_test.go index 33b1a217f..4586ec2db 100644 --- a/pkg/tcpip/link/fdbased/endpoint_test.go +++ b/pkg/tcpip/link/fdbased/endpoint_test.go @@ -47,7 +47,6 @@ const ( ) type packetInfo struct { - Raddr tcpip.LinkAddress Proto tcpip.NetworkProtocolNumber Contents *stack.PacketBuffer } @@ -134,12 +133,8 @@ func (c *context) cleanup() { } } -func (c *context) DeliverNetworkPacket(remote tcpip.LinkAddress, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { - c.ch <- packetInfo{remote, protocol, pkt} -} - -func (c *context) DeliverOutboundPacket(remote tcpip.LinkAddress, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { - panic("unimplemented") +func (c *context) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { + c.ch <- packetInfo{protocol, pkt} } func TestNoEthernetProperties(t *testing.T) { @@ -410,13 +405,11 @@ func TestDeliverPacket(t *testing.T) { select { case pi := <-c.ch: want := packetInfo{ - Raddr: raddr, Proto: proto, Contents: wantPkt, } if !eth { want.Proto = header.IPv4ProtocolNumber - want.Raddr = "" } checkPacketInfoEqual(t, pi, want) case <-time.After(10 * time.Second): @@ -569,14 +562,10 @@ type fakeNetworkDispatcher struct { pkts []*stack.PacketBuffer } -func (d *fakeNetworkDispatcher) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { +func (d *fakeNetworkDispatcher) DeliverNetworkPacket(_ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { d.pkts = append(d.pkts, pkt) } -func (d *fakeNetworkDispatcher) DeliverOutboundPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { - panic("unimplemented") -} - func TestDispatchPacketFormat(t *testing.T) { for _, test := range []struct { name string diff --git a/pkg/tcpip/link/fdbased/mmap.go b/pkg/tcpip/link/fdbased/mmap.go index 47047578d..694373887 100644 --- a/pkg/tcpip/link/fdbased/mmap.go +++ b/pkg/tcpip/link/fdbased/mmap.go @@ -169,15 +169,9 @@ func (d *packetMMapDispatcher) dispatch() (bool, tcpip.Error) { if err != nil || stopped { return false, err } - var ( - p tcpip.NetworkProtocolNumber - remote, local tcpip.LinkAddress - ) + var p tcpip.NetworkProtocolNumber if d.e.hdrSize > 0 { - eth := header.Ethernet(pkt) - p = eth.Type() - remote = eth.SourceAddress() - local = eth.DestinationAddress() + p = header.Ethernet(pkt).Type() } else { // We don't get any indication of what the packet is, so try to guess // if it's an IPv4 or IPv6 packet. @@ -200,6 +194,6 @@ func (d *packetMMapDispatcher) dispatch() (bool, tcpip.Error) { panic(fmt.Sprintf("LinkHeader().Consume(%d) must succeed", d.e.hdrSize)) } } - d.e.dispatcher.DeliverNetworkPacket(remote, local, p, pbuf) + d.e.dispatcher.DeliverNetworkPacket(p, pbuf) return true, nil } diff --git a/pkg/tcpip/link/fdbased/packet_dispatchers.go b/pkg/tcpip/link/fdbased/packet_dispatchers.go index f3a135441..a77ccc3c5 100644 --- a/pkg/tcpip/link/fdbased/packet_dispatchers.go +++ b/pkg/tcpip/link/fdbased/packet_dispatchers.go @@ -183,19 +183,13 @@ func (d *readVDispatcher) dispatch() (bool, tcpip.Error) { }) defer pkt.DecRef() - var ( - p tcpip.NetworkProtocolNumber - remote, local tcpip.LinkAddress - ) + var p tcpip.NetworkProtocolNumber if d.e.hdrSize > 0 { hdr, ok := pkt.LinkHeader().Consume(d.e.hdrSize) if !ok { return false, nil } - eth := header.Ethernet(hdr) - p = eth.Type() - remote = eth.SourceAddress() - local = eth.DestinationAddress() + p = header.Ethernet(hdr).Type() } else { // We don't get any indication of what the packet is, so try to guess // if it's an IPv4 or IPv6 packet. @@ -214,7 +208,7 @@ func (d *readVDispatcher) dispatch() (bool, tcpip.Error) { } } - d.e.dispatcher.DeliverNetworkPacket(remote, local, p, pkt) + d.e.dispatcher.DeliverNetworkPacket(p, pkt) return true, nil } @@ -298,19 +292,13 @@ func (d *recvMMsgDispatcher) dispatch() (bool, tcpip.Error) { // Mark that this iovec has been processed. d.msgHdrs[k].Msg.Iovlen = 0 - var ( - p tcpip.NetworkProtocolNumber - remote, local tcpip.LinkAddress - ) + var p tcpip.NetworkProtocolNumber if d.e.hdrSize > 0 { hdr, ok := pkt.LinkHeader().Consume(d.e.hdrSize) if !ok { return false, nil } - eth := header.Ethernet(hdr) - p = eth.Type() - remote = eth.SourceAddress() - local = eth.DestinationAddress() + p = header.Ethernet(hdr).Type() } else { // We don't get any indication of what the packet is, so try to guess // if it's an IPv4 or IPv6 packet. @@ -331,7 +319,7 @@ func (d *recvMMsgDispatcher) dispatch() (bool, tcpip.Error) { } } - d.e.dispatcher.DeliverNetworkPacket(remote, local, p, pkt) + d.e.dispatcher.DeliverNetworkPacket(p, pkt) } return true, nil diff --git a/pkg/tcpip/link/loopback/loopback.go b/pkg/tcpip/link/loopback/loopback.go index 7b4635271..f8687008c 100644 --- a/pkg/tcpip/link/loopback/loopback.go +++ b/pkg/tcpip/link/loopback/loopback.go @@ -106,7 +106,7 @@ func (e *endpoint) WriteRawPacket(pkt *stack.PacketBuffer) tcpip.Error { Data: data, }) defer newPkt.DecRef() - e.dispatcher.DeliverNetworkPacket("" /* remote */, "" /* local */, pkt.NetworkProtocolNumber, newPkt) + e.dispatcher.DeliverNetworkPacket(pkt.NetworkProtocolNumber, newPkt) return nil } diff --git a/pkg/tcpip/link/muxed/injectable.go b/pkg/tcpip/link/muxed/injectable.go index 863fb097c..0ba6e20b6 100644 --- a/pkg/tcpip/link/muxed/injectable.go +++ b/pkg/tcpip/link/muxed/injectable.go @@ -81,7 +81,7 @@ func (m *InjectableEndpoint) IsAttached() bool { // InjectInbound implements stack.InjectableLinkEndpoint. func (m *InjectableEndpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { - m.dispatcher.DeliverNetworkPacket("" /* remote */, "" /* local */, protocol, pkt) + m.dispatcher.DeliverNetworkPacket(protocol, pkt) } // WritePackets writes outbound packets to the appropriate diff --git a/pkg/tcpip/link/nested/nested.go b/pkg/tcpip/link/nested/nested.go index b5975ec51..23cfc3787 100644 --- a/pkg/tcpip/link/nested/nested.go +++ b/pkg/tcpip/link/nested/nested.go @@ -51,12 +51,12 @@ func (e *Endpoint) Init(child stack.LinkEndpoint, embedder stack.NetworkDispatch } // DeliverNetworkPacket implements stack.NetworkDispatcher. -func (e *Endpoint) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { +func (e *Endpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { e.mu.RLock() d := e.dispatcher e.mu.RUnlock() if d != nil { - d.DeliverNetworkPacket(remote, local, protocol, pkt) + d.DeliverNetworkPacket(protocol, pkt) } } diff --git a/pkg/tcpip/link/nested/nested_test.go b/pkg/tcpip/link/nested/nested_test.go index 0825d79ad..927b53099 100644 --- a/pkg/tcpip/link/nested/nested_test.go +++ b/pkg/tcpip/link/nested/nested_test.go @@ -54,17 +54,11 @@ type counterDispatcher struct { var _ stack.NetworkDispatcher = (*counterDispatcher)(nil) -func (d *counterDispatcher) DeliverNetworkPacket(tcpip.LinkAddress, tcpip.LinkAddress, tcpip.NetworkProtocolNumber, *stack.PacketBuffer) { +func (d *counterDispatcher) DeliverNetworkPacket(tcpip.NetworkProtocolNumber, *stack.PacketBuffer) { d.count++ } -func (d *counterDispatcher) DeliverOutboundPacket(tcpip.LinkAddress, tcpip.LinkAddress, tcpip.NetworkProtocolNumber, *stack.PacketBuffer) { - panic("unimplemented") -} - func TestNestedLinkEndpoint(t *testing.T) { - const emptyAddress = tcpip.LinkAddress("") - var ( childEP childEndpoint nestedEP parentEndpoint @@ -92,7 +86,7 @@ func TestNestedLinkEndpoint(t *testing.T) { { p := stack.NewPacketBuffer(stack.PacketBufferOptions{}) - nestedEP.DeliverNetworkPacket(emptyAddress, emptyAddress, header.IPv4ProtocolNumber, p) + nestedEP.DeliverNetworkPacket(header.IPv4ProtocolNumber, p) p.DecRef() if disp.count != 1 { t.Errorf("After first packet with dispatcher attached, got disp.count = %d, want = 1", disp.count) @@ -110,7 +104,7 @@ func TestNestedLinkEndpoint(t *testing.T) { { disp.count = 0 p := stack.NewPacketBuffer(stack.PacketBufferOptions{}) - nestedEP.DeliverNetworkPacket(emptyAddress, emptyAddress, header.IPv4ProtocolNumber, p) + nestedEP.DeliverNetworkPacket(header.IPv4ProtocolNumber, p) p.DecRef() if disp.count != 0 { t.Errorf("After second packet with dispatcher detached, got disp.count = %d, want = 0", disp.count) diff --git a/pkg/tcpip/link/pipe/pipe.go b/pkg/tcpip/link/pipe/pipe.go index 33a0b2eab..dacd3e5db 100644 --- a/pkg/tcpip/link/pipe/pipe.go +++ b/pkg/tcpip/link/pipe/pipe.go @@ -53,20 +53,11 @@ func (e *Endpoint) deliverPackets(pkts stack.PacketBufferList) { return } - // Note that the local address from the perspective of this endpoint is the - // remote address from the perspective of the other end of the pipe - // (e.linked). Similarly, the remote address from the perspective of this - // endpoint is the local address on the other end. - // - // Deliver the packet in a new goroutine to escape this goroutine's stack and - // avoid a deadlock when a packet triggers a response which leads the stack to - // try and take a lock it already holds. for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() { newPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ Data: buffer.NewVectorisedView(pkt.Size(), pkt.Views()), }) - r := pkt.EgressRoute - e.linked.dispatcher.DeliverNetworkPacket(r.LocalLinkAddress /* remote */, r.RemoteLinkAddress /* local */, pkt.NetworkProtocolNumber, newPkt) + e.linked.dispatcher.DeliverNetworkPacket(pkt.NetworkProtocolNumber, newPkt) newPkt.DecRef() } } diff --git a/pkg/tcpip/link/sharedmem/sharedmem.go b/pkg/tcpip/link/sharedmem/sharedmem.go index 1b4da2521..43eb629b5 100644 --- a/pkg/tcpip/link/sharedmem/sharedmem.go +++ b/pkg/tcpip/link/sharedmem/sharedmem.go @@ -432,7 +432,6 @@ func (e *endpoint) dispatchLoop(d stack.NetworkDispatcher) { } } - var src, dst tcpip.LinkAddress var proto tcpip.NetworkProtocolNumber if e.addr != "" { hdr, ok := pkt.LinkHeader().Consume(header.EthernetMinimumSize) @@ -440,10 +439,7 @@ func (e *endpoint) dispatchLoop(d stack.NetworkDispatcher) { pkt.DecRef() continue } - eth := header.Ethernet(hdr) - src = eth.SourceAddress() - dst = eth.DestinationAddress() - proto = eth.Type() + proto = header.Ethernet(hdr).Type() } else { // We don't get any indication of what the packet is, so try to guess // if it's an IPv4 or IPv6 packet. @@ -465,7 +461,7 @@ func (e *endpoint) dispatchLoop(d stack.NetworkDispatcher) { } // Send packet up the stack. - d.DeliverNetworkPacket(src, dst, proto, pkt) + d.DeliverNetworkPacket(proto, pkt) pkt.DecRef() } diff --git a/pkg/tcpip/link/sharedmem/sharedmem_server.go b/pkg/tcpip/link/sharedmem/sharedmem_server.go index aff6aa40d..6df530fa9 100644 --- a/pkg/tcpip/link/sharedmem/sharedmem_server.go +++ b/pkg/tcpip/link/sharedmem/sharedmem_server.go @@ -326,7 +326,6 @@ func (e *serverEndpoint) dispatchLoop(d stack.NetworkDispatcher) { continue } } - var src, dst tcpip.LinkAddress var proto tcpip.NetworkProtocolNumber if e.addr != "" { hdr, ok := pkt.LinkHeader().Consume(header.EthernetMinimumSize) @@ -334,10 +333,7 @@ func (e *serverEndpoint) dispatchLoop(d stack.NetworkDispatcher) { pkt.DecRef() continue } - eth := header.Ethernet(hdr) - src = eth.SourceAddress() - dst = eth.DestinationAddress() - proto = eth.Type() + proto = header.Ethernet(hdr).Type() } else { // We don't get any indication of what the packet is, so try to guess // if it's an IPv4 or IPv6 packet. @@ -358,7 +354,7 @@ func (e *serverEndpoint) dispatchLoop(d stack.NetworkDispatcher) { } } // Send packet up the stack. - d.DeliverNetworkPacket(src, dst, proto, pkt) + d.DeliverNetworkPacket(proto, pkt) pkt.DecRef() } diff --git a/pkg/tcpip/link/sharedmem/sharedmem_test.go b/pkg/tcpip/link/sharedmem/sharedmem_test.go index e12953df8..f5f4521e6 100644 --- a/pkg/tcpip/link/sharedmem/sharedmem_test.go +++ b/pkg/tcpip/link/sharedmem/sharedmem_test.go @@ -80,7 +80,6 @@ func (q *queueBuffers) cleanup() { } type packetInfo struct { - addr tcpip.LinkAddress proto tcpip.NetworkProtocolNumber data buffer.View linkHeader buffer.View @@ -145,10 +144,9 @@ func newTestContext(t *testing.T, mtu, bufferSize uint32, addr tcpip.LinkAddress return c } -func (c *testContext) DeliverNetworkPacket(remoteLinkAddr, localLinkAddr tcpip.LinkAddress, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { +func (c *testContext) DeliverNetworkPacket(proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { c.mu.Lock() c.packets = append(c.packets, packetInfo{ - addr: remoteLinkAddr, proto: proto, data: pkt.Data().AsRange().ToOwnedView(), }) @@ -157,10 +155,6 @@ func (c *testContext) DeliverNetworkPacket(remoteLinkAddr, localLinkAddr tcpip.L c.packetCh <- struct{}{} } -func (c *testContext) DeliverOutboundPacket(remoteLinkAddr, localLinkAddr tcpip.LinkAddress, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { - panic("unimplemented") -} - func (c *testContext) cleanup() { c.ep.Close() closeFDs(c.txCfg) diff --git a/pkg/tcpip/link/sniffer/sniffer.go b/pkg/tcpip/link/sniffer/sniffer.go index 4f4828d03..d172821b9 100644 --- a/pkg/tcpip/link/sniffer/sniffer.go +++ b/pkg/tcpip/link/sniffer/sniffer.go @@ -135,9 +135,9 @@ func NewWithWriter(lower stack.LinkEndpoint, writer io.Writer, snapLen uint32) ( // DeliverNetworkPacket implements the stack.NetworkDispatcher interface. It is // called by the link-layer endpoint being wrapped when a packet arrives, and // logs the packet before forwarding to the actual dispatcher. -func (e *endpoint) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { +func (e *endpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { e.dumpPacket(directionRecv, protocol, pkt) - e.Endpoint.DeliverNetworkPacket(remote, local, protocol, pkt) + e.Endpoint.DeliverNetworkPacket(protocol, pkt) } func (e *endpoint) dumpPacket(dir direction, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { diff --git a/pkg/tcpip/link/tun/device.go b/pkg/tcpip/link/tun/device.go index 31328f3f7..3181a4070 100644 --- a/pkg/tcpip/link/tun/device.go +++ b/pkg/tcpip/link/tun/device.go @@ -219,22 +219,13 @@ func (d *Device) Write(data []byte) (int64, error) { } } - // Try to determine remote link address, default zero. - var remote tcpip.LinkAddress - switch { - case ethHdr != nil: - remote = ethHdr.SourceAddress() - default: - remote = tcpip.LinkAddress(zeroMAC[:]) - } - pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: len(ethHdr), Data: buffer.View(data).ToVectorisedView(), }) defer pkt.DecRef() copy(pkt.LinkHeader().Push(len(ethHdr)), ethHdr) - endpoint.InjectLinkAddr(protocol, remote, pkt) + endpoint.InjectInbound(protocol, pkt) return dataLen, nil } diff --git a/pkg/tcpip/link/waitable/waitable.go b/pkg/tcpip/link/waitable/waitable.go index 449ba94b6..e0f4ef8ed 100644 --- a/pkg/tcpip/link/waitable/waitable.go +++ b/pkg/tcpip/link/waitable/waitable.go @@ -50,12 +50,12 @@ func New(lower stack.LinkEndpoint) *Endpoint { // It is called by the link-layer endpoint being wrapped when a packet arrives, // and only forwards to the actual dispatcher if Wait or WaitDispatch haven't // been called. -func (e *Endpoint) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { +func (e *Endpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { if !e.dispatchGate.Enter() { return } - e.dispatcher.DeliverNetworkPacket(remote, local, protocol, pkt) + e.dispatcher.DeliverNetworkPacket(protocol, pkt) e.dispatchGate.Leave() } diff --git a/pkg/tcpip/link/waitable/waitable_test.go b/pkg/tcpip/link/waitable/waitable_test.go index ad808ccd8..9db10e007 100644 --- a/pkg/tcpip/link/waitable/waitable_test.go +++ b/pkg/tcpip/link/waitable/waitable_test.go @@ -40,14 +40,10 @@ type countedEndpoint struct { dispatcher stack.NetworkDispatcher } -func (e *countedEndpoint) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { +func (e *countedEndpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { e.dispatchCount++ } -func (e *countedEndpoint) DeliverOutboundPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { - panic("unimplemented") -} - func (e *countedEndpoint) Attach(dispatcher stack.NetworkDispatcher) { e.attachCount++ e.dispatcher = dispatcher @@ -161,7 +157,7 @@ func TestWaitDispatch(t *testing.T) { // Dispatch and check that it goes through. { p := stack.NewPacketBuffer(stack.PacketBufferOptions{}) - ep.dispatcher.DeliverNetworkPacket("", "", 0, p) + ep.dispatcher.DeliverNetworkPacket(0, p) if want := 1; ep.dispatchCount != want { t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want) } @@ -172,7 +168,7 @@ func TestWaitDispatch(t *testing.T) { { wep.WaitWrite() p := stack.NewPacketBuffer(stack.PacketBufferOptions{}) - ep.dispatcher.DeliverNetworkPacket("", "", 0, p) + ep.dispatcher.DeliverNetworkPacket(0, p) if want := 2; ep.dispatchCount != want { t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want) } @@ -183,7 +179,7 @@ func TestWaitDispatch(t *testing.T) { { wep.WaitDispatch() p := stack.NewPacketBuffer(stack.PacketBufferOptions{}) - ep.dispatcher.DeliverNetworkPacket("", "", 0, p) + ep.dispatcher.DeliverNetworkPacket(0, p) if want := 2; ep.dispatchCount != want { t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want) } diff --git a/pkg/tcpip/network/ipv6/icmp_test.go b/pkg/tcpip/network/ipv6/icmp_test.go index 36a1d7051..8a21874c3 100644 --- a/pkg/tcpip/network/ipv6/icmp_test.go +++ b/pkg/tcpip/network/ipv6/icmp_test.go @@ -486,7 +486,7 @@ func routeICMPv6Packet(t *testing.T, clock *faketime.ManualClock, args routeArgs pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ Data: buffer.NewVectorisedView(pi.Size(), pi.Views()), }) - args.dst.InjectLinkAddr(pi.NetworkProtocolNumber, args.dst.LinkAddress(), pkt) + args.dst.InjectInbound(pi.NetworkProtocolNumber, pkt) } if pi.NetworkProtocolNumber != ProtocolNumber { diff --git a/pkg/tcpip/network/ipv6/ndp_test.go b/pkg/tcpip/network/ipv6/ndp_test.go index 8aaec06f3..78c23f900 100644 --- a/pkg/tcpip/network/ipv6/ndp_test.go +++ b/pkg/tcpip/network/ipv6/ndp_test.go @@ -243,7 +243,6 @@ func TestNeighborSolicitationResponse(t *testing.T) { tests := []struct { name string nsOpts header.NDPOptionsSerializer - nsSrcLinkAddr tcpip.LinkAddress nsSrc tcpip.Address nsDst tcpip.Address nsInvalid bool @@ -256,7 +255,6 @@ func TestNeighborSolicitationResponse(t *testing.T) { { name: "Unspecified source to solicited-node multicast destination", nsOpts: nil, - nsSrcLinkAddr: remoteLinkAddr0, nsSrc: header.IPv6Any, nsDst: nicAddrSNMC, nsInvalid: false, @@ -270,35 +268,31 @@ func TestNeighborSolicitationResponse(t *testing.T) { nsOpts: header.NDPOptionsSerializer{ header.NDPSourceLinkLayerAddressOption(remoteLinkAddr0[:]), }, - nsSrcLinkAddr: remoteLinkAddr0, - nsSrc: header.IPv6Any, - nsDst: nicAddrSNMC, - nsInvalid: true, + nsSrc: header.IPv6Any, + nsDst: nicAddrSNMC, + nsInvalid: true, }, { - name: "Unspecified source to unicast destination", - nsOpts: nil, - nsSrcLinkAddr: remoteLinkAddr0, - nsSrc: header.IPv6Any, - nsDst: nicAddr, - nsInvalid: true, + name: "Unspecified source to unicast destination", + nsOpts: nil, + nsSrc: header.IPv6Any, + nsDst: nicAddr, + nsInvalid: true, }, { name: "Unspecified source with source ll option to unicast destination", nsOpts: header.NDPOptionsSerializer{ header.NDPSourceLinkLayerAddressOption(remoteLinkAddr0[:]), }, - nsSrcLinkAddr: remoteLinkAddr0, - nsSrc: header.IPv6Any, - nsDst: nicAddr, - nsInvalid: true, + nsSrc: header.IPv6Any, + nsDst: nicAddr, + nsInvalid: true, }, { name: "Specified source with 1 source ll to multicast destination", nsOpts: header.NDPOptionsSerializer{ header.NDPSourceLinkLayerAddressOption(remoteLinkAddr0[:]), }, - nsSrcLinkAddr: remoteLinkAddr0, nsSrc: remoteAddr, nsDst: nicAddrSNMC, nsInvalid: false, @@ -312,7 +306,6 @@ func TestNeighborSolicitationResponse(t *testing.T) { nsOpts: header.NDPOptionsSerializer{ header.NDPSourceLinkLayerAddressOption(remoteLinkAddr1[:]), }, - nsSrcLinkAddr: remoteLinkAddr0, nsSrc: remoteAddr, nsDst: nicAddrSNMC, nsInvalid: false, @@ -322,12 +315,11 @@ func TestNeighborSolicitationResponse(t *testing.T) { naDst: remoteAddr, }, { - name: "Specified source to multicast destination", - nsOpts: nil, - nsSrcLinkAddr: remoteLinkAddr0, - nsSrc: remoteAddr, - nsDst: nicAddrSNMC, - nsInvalid: true, + name: "Specified source to multicast destination", + nsOpts: nil, + nsSrc: remoteAddr, + nsDst: nicAddrSNMC, + nsInvalid: true, }, { name: "Specified source with 2 source ll to multicast destination", @@ -335,16 +327,14 @@ func TestNeighborSolicitationResponse(t *testing.T) { header.NDPSourceLinkLayerAddressOption(remoteLinkAddr0[:]), header.NDPSourceLinkLayerAddressOption(remoteLinkAddr1[:]), }, - nsSrcLinkAddr: remoteLinkAddr0, - nsSrc: remoteAddr, - nsDst: nicAddrSNMC, - nsInvalid: true, + nsSrc: remoteAddr, + nsDst: nicAddrSNMC, + nsInvalid: true, }, { name: "Specified source to unicast destination", nsOpts: nil, - nsSrcLinkAddr: remoteLinkAddr0, nsSrc: remoteAddr, nsDst: nicAddr, nsInvalid: false, @@ -362,7 +352,6 @@ func TestNeighborSolicitationResponse(t *testing.T) { nsOpts: header.NDPOptionsSerializer{ header.NDPSourceLinkLayerAddressOption(remoteLinkAddr0[:]), }, - nsSrcLinkAddr: remoteLinkAddr0, nsSrc: remoteAddr, nsDst: nicAddr, nsInvalid: false, @@ -376,7 +365,6 @@ func TestNeighborSolicitationResponse(t *testing.T) { nsOpts: header.NDPOptionsSerializer{ header.NDPSourceLinkLayerAddressOption(remoteLinkAddr1[:]), }, - nsSrcLinkAddr: remoteLinkAddr0, nsSrc: remoteAddr, nsDst: nicAddr, nsInvalid: false, @@ -391,10 +379,9 @@ func TestNeighborSolicitationResponse(t *testing.T) { header.NDPSourceLinkLayerAddressOption(remoteLinkAddr0[:]), header.NDPSourceLinkLayerAddressOption(remoteLinkAddr1[:]), }, - nsSrcLinkAddr: remoteLinkAddr0, - nsSrc: remoteAddr, - nsDst: nicAddr, - nsInvalid: true, + nsSrc: remoteAddr, + nsDst: nicAddr, + nsInvalid: true, }, } @@ -455,7 +442,7 @@ func TestNeighborSolicitationResponse(t *testing.T) { t.Fatalf("got invalid = %d, want = 0", got) } - e.InjectLinkAddr(ProtocolNumber, test.nsSrcLinkAddr, stack.NewPacketBuffer(stack.PacketBufferOptions{ + e.InjectInbound(ProtocolNumber, stack.NewPacketBuffer(stack.PacketBufferOptions{ Data: hdr.View().ToVectorisedView(), })) @@ -528,7 +515,7 @@ func TestNeighborSolicitationResponse(t *testing.T) { SrcAddr: test.nsSrc, DstAddr: nicAddr, }) - e.InjectLinkAddr(ProtocolNumber, "", stack.NewPacketBuffer(stack.PacketBufferOptions{ + e.InjectInbound(ProtocolNumber, stack.NewPacketBuffer(stack.PacketBufferOptions{ Data: hdr.View().ToVectorisedView(), })) } diff --git a/pkg/tcpip/stack/forwarding_test.go b/pkg/tcpip/stack/forwarding_test.go index 16254796b..5673bbf10 100644 --- a/pkg/tcpip/stack/forwarding_test.go +++ b/pkg/tcpip/stack/forwarding_test.go @@ -265,7 +265,7 @@ func (e *fwdTestLinkEndpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber // InjectLinkAddr injects an inbound packet with a remote link address. func (e *fwdTestLinkEndpoint) InjectLinkAddr(protocol tcpip.NetworkProtocolNumber, remote tcpip.LinkAddress, pkt *PacketBuffer) { - e.dispatcher.DeliverNetworkPacket(remote, "" /* local */, protocol, pkt) + e.dispatcher.DeliverNetworkPacket(protocol, pkt) } // Attach saves the stack network-layer dispatcher for use later when packets diff --git a/pkg/tcpip/stack/nic.go b/pkg/tcpip/stack/nic.go index aae961e8d..de0eab024 100644 --- a/pkg/tcpip/stack/nic.go +++ b/pkg/tcpip/stack/nic.go @@ -702,7 +702,7 @@ func (n *nic) isInGroup(addr tcpip.Address) bool { // Note that the ownership of the slice backing vv is retained by the caller. // This rule applies only to the slice itself, not to the items of the slice; // the ownership of the items is not retained by the caller. -func (n *nic) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *PacketBuffer) { +func (n *nic) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *PacketBuffer) { enabled := n.Enabled() // If the NIC is not yet enabled, don't receive any packets. if !enabled { @@ -720,11 +720,6 @@ func (n *nic) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcp return } - // If no local link layer address is provided, assume it was sent - // directly to this NIC. - if local == "" { - local = n.NetworkLinkEndpoint.LinkAddress() - } pkt.RXTransportChecksumValidated = n.NetworkLinkEndpoint.Capabilities()&CapabilityRXChecksumOffload != 0 // Deliver to interested packet endpoints without holding NIC lock. @@ -757,7 +752,7 @@ func (n *nic) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcp clone := packetEPPkt.Clone() defer clone.DecRef() - ep.HandlePacket(n.id, local, protocol, clone) + ep.HandlePacket(n.id, protocol, clone) } n.packetEPsMu.Lock() @@ -820,7 +815,7 @@ func (n *nic) deliverOutboundPacket(remote tcpip.LinkAddress, pkt *PacketBuffer) } clone := packetEPPkt.Clone() defer clone.DecRef() - ep.HandlePacket(n.id, local, pkt.NetworkProtocolNumber, clone) + ep.HandlePacket(n.id, pkt.NetworkProtocolNumber, clone) }) } diff --git a/pkg/tcpip/stack/nic_test.go b/pkg/tcpip/stack/nic_test.go index 4ac5ff228..cdd6dede5 100644 --- a/pkg/tcpip/stack/nic_test.go +++ b/pkg/tcpip/stack/nic_test.go @@ -182,7 +182,7 @@ func TestDisabledRxStatsWhenNICDisabled(t *testing.T) { t.FailNow() } - nic.DeliverNetworkPacket("", "", 0, NewPacketBuffer(PacketBufferOptions{ + nic.DeliverNetworkPacket(0, NewPacketBuffer(PacketBufferOptions{ Data: buffer.View([]byte{1, 2, 3, 4}).ToVectorisedView(), })) @@ -207,7 +207,7 @@ func TestPacketWithUnknownNetworkProtocolNumber(t *testing.T) { } // IPv4 isn't recognized since we haven't initialized the NIC with an IPv4 // endpoint. - nic.DeliverNetworkPacket("", "", header.IPv4ProtocolNumber, NewPacketBuffer(PacketBufferOptions{ + nic.DeliverNetworkPacket(header.IPv4ProtocolNumber, NewPacketBuffer(PacketBufferOptions{ Data: buffer.View([]byte{1, 2, 3, 4}).ToVectorisedView(), })) var count uint64 diff --git a/pkg/tcpip/stack/registration.go b/pkg/tcpip/stack/registration.go index 34e0cc55c..c24609a9c 100644 --- a/pkg/tcpip/stack/registration.go +++ b/pkg/tcpip/stack/registration.go @@ -156,7 +156,7 @@ type PacketEndpoint interface { // should construct its own ethernet header for applications. // // HandlePacket may modify pkt. - HandlePacket(nicID tcpip.NICID, addr tcpip.LinkAddress, netProto tcpip.NetworkProtocolNumber, pkt *PacketBuffer) + HandlePacket(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, pkt *PacketBuffer) } // UnknownDestinationPacketDisposition enumerates the possible return values from @@ -735,7 +735,7 @@ type NetworkDispatcher interface { // packets sent via loopback), and won't have the field set. // // DeliverNetworkPacket may modify pkt. - DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *PacketBuffer) + DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *PacketBuffer) } // LinkEndpointCapabilities is the type associated with the capabilities diff --git a/pkg/tcpip/tests/utils/utils.go b/pkg/tcpip/tests/utils/utils.go index ea76b536e..3a86e838e 100644 --- a/pkg/tcpip/tests/utils/utils.go +++ b/pkg/tcpip/tests/utils/utils.go @@ -207,9 +207,9 @@ var _ stack.NetworkDispatcher = (*EndpointWithDestinationCheck)(nil) var _ stack.LinkEndpoint = (*EndpointWithDestinationCheck)(nil) // DeliverNetworkPacket implements stack.NetworkDispatcher. -func (e *EndpointWithDestinationCheck) DeliverNetworkPacket(src, dst tcpip.LinkAddress, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { - if dst == e.Endpoint.LinkAddress() || dst == header.EthernetBroadcastAddress || header.IsMulticastEthernetAddress(dst) { - e.Endpoint.DeliverNetworkPacket(src, dst, proto, pkt) +func (e *EndpointWithDestinationCheck) DeliverNetworkPacket(proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { + if dst := header.Ethernet(pkt.LinkHeader().View()).DestinationAddress(); dst == e.Endpoint.LinkAddress() || dst == header.EthernetBroadcastAddress || header.IsMulticastEthernetAddress(dst) { + e.Endpoint.DeliverNetworkPacket(proto, pkt) } } diff --git a/pkg/tcpip/transport/packet/endpoint.go b/pkg/tcpip/transport/packet/endpoint.go index 80eef39e9..d3250df7d 100644 --- a/pkg/tcpip/transport/packet/endpoint.go +++ b/pkg/tcpip/transport/packet/endpoint.go @@ -409,7 +409,7 @@ func (ep *endpoint) GetSockOptInt(opt tcpip.SockOptInt) (int, tcpip.Error) { } // HandlePacket implements stack.PacketEndpoint.HandlePacket. -func (ep *endpoint) HandlePacket(nicID tcpip.NICID, _ tcpip.LinkAddress, netProto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { +func (ep *endpoint) HandlePacket(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { ep.rcvMu.Lock() // Drop the packet if our buffer is currently full.