Debian Codegraph @

tcpip/link/ethernet/ethernet.go

raw symbols SCIP
1 // Copyright 2020 The gVisor Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package ethernet provides an implementation of an ethernet link endpoint that
16 // wraps an inner link endpoint.
17 package ethernet
18
19 import (
20 "inet.af/netstack/tcpip"
21 "inet.af/netstack/tcpip/header"
22 "inet.af/netstack/tcpip/link/nested"
23 "inet.af/netstack/tcpip/stack"
24 )
25
26 var _ stack.NetworkDispatcher = (*Endpoint)(nil)
27 var _ stack.LinkEndpoint = (*Endpoint)(nil)
28
29 // New returns an ethernet link endpoint that wraps an inner link endpoint.
30 func New(ep stack.LinkEndpoint) *Endpoint {
31 var e Endpoint
32 e.Endpoint.Init(ep, &e)
33 return &e
34 }
35
36 // Endpoint is an ethernet endpoint.
37 //
38 // It adds an ethernet header to packets before sending them out through its
39 // inner link endpoint and consumes an ethernet header before sending the
40 // packet to the stack.
41 type Endpoint struct {
42 nested.Endpoint
43 }
44
45 // LinkAddress implements stack.LinkEndpoint.
46 func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
47 if l := e.Endpoint.LinkAddress(); len(l) != 0 {
48 return l
49 }
50 return header.UnspecifiedEthernetAddress
51 }
52
53 // DeliverNetworkPacket implements stack.NetworkDispatcher.
54 func (e *Endpoint) DeliverNetworkPacket(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
55 hdr, ok := pkt.LinkHeader().Consume(header.EthernetMinimumSize)
56 if !ok {
57 return
58 }
59
60 // Note, there is no need to check the destination link address here since
61 // the ethernet hardware filters frames based on their destination addresses.
62 eth := header.Ethernet(hdr)
63 e.Endpoint.DeliverNetworkPacket(eth.SourceAddress() /* remote */, eth.DestinationAddress() /* local */, eth.Type() /* protocol */, pkt)
64 }
65
66 // Capabilities implements stack.LinkEndpoint.
67 func (e *Endpoint) Capabilities() stack.LinkEndpointCapabilities {
68 c := e.Endpoint.Capabilities()
69 if c&stack.CapabilityLoopback == 0 {
70 c |= stack.CapabilityResolutionRequired
71 }
72 return c
73 }
74
75 // WritePacket implements stack.LinkEndpoint.
76 func (e *Endpoint) WritePacket(r stack.RouteInfo, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
77 e.AddHeader(e.LinkAddress(), r.RemoteLinkAddress, proto, pkt)
78 return e.Endpoint.WritePacket(r, proto, pkt)
79 }
80
81 // WritePackets implements stack.LinkEndpoint.
82 func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, proto tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
83 linkAddr := e.LinkAddress()
84
85 for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() {
86 e.AddHeader(linkAddr, r.RemoteLinkAddress, proto, pkt)
87 }
88
89 return e.Endpoint.WritePackets(r, pkts, proto)
90 }
91
92 // MaxHeaderLength implements stack.LinkEndpoint.
93 func (e *Endpoint) MaxHeaderLength() uint16 {
94 return header.EthernetMinimumSize + e.Endpoint.MaxHeaderLength()
95 }
96
97 // ARPHardwareType implements stack.LinkEndpoint.
98 func (e *Endpoint) ARPHardwareType() header.ARPHardwareType {
99 if a := e.Endpoint.ARPHardwareType(); a != header.ARPHardwareNone {
100 return a
101 }
102 return header.ARPHardwareEther
103 }
104
105 // AddHeader implements stack.LinkEndpoint.
106 func (*Endpoint) AddHeader(local, remote tcpip.LinkAddress, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
107 eth := header.Ethernet(pkt.LinkHeader().Push(header.EthernetMinimumSize))
108 fields := header.EthernetFields{
109 SrcAddr: local,
110 DstAddr: remote,
111 Type: proto,
112 }
113 eth.Encode(&fields)
114 }
115
116 // WriteRawPacket implements stack.LinkEndpoint.
117 func (e *Endpoint) WriteRawPacket(pkt *stack.PacketBuffer) tcpip.Error {
118 return e.Endpoint.WriteRawPacket(pkt)
119 }