| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 30 |
func New(ep stack.LinkEndpoint) *Endpoint { |
| 31 |
var e Endpoint |
| 32 |
e.Endpoint.Init(ep, &e) |
| 33 |
return &e |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
type Endpoint struct { |
| 42 |
nested.Endpoint |
| 43 |
} |
| 44 |
|
| 45 |
|
| 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 |
|
| 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 |
|
| 61 |
|
| 62 |
eth := header.Ethernet(hdr) |
| 63 |
e.Endpoint.DeliverNetworkPacket(eth.SourceAddress() , eth.DestinationAddress() , eth.Type() , pkt) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 93 |
func (e *Endpoint) MaxHeaderLength() uint16 { |
| 94 |
return header.EthernetMinimumSize + e.Endpoint.MaxHeaderLength() |
| 95 |
} |
| 96 |
|
| 97 |
|
| 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 |
|
| 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 |
|
| 117 |
func (e *Endpoint) WriteRawPacket(pkt *stack.PacketBuffer) tcpip.Error { |
| 118 |
return e.Endpoint.WriteRawPacket(pkt) |
| 119 |
} |