| 1 | package tap |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | |
| 6 | "github.com/google/gopacket" |
| 7 | "github.com/google/gopacket/layers" |
| 8 | log "github.com/sirupsen/logrus" |
| 9 | "gvisor.dev/gvisor/pkg/tcpip" |
| 10 | "gvisor.dev/gvisor/pkg/tcpip/header" |
| 11 | "gvisor.dev/gvisor/pkg/tcpip/stack" |
| 12 | ) |
| 13 | |
| 14 | type LinkEndpoint struct { |
| 15 | debug bool |
| 16 | mtu uint32 |
| 17 | mac tcpip.LinkAddress |
| 18 | ip string |
| 19 | virtualIPs map[string]struct{} |
| 20 | |
| 21 | dispatcher stack.NetworkDispatcher |
| 22 | networkSwitch NetworkSwitch |
| 23 | } |
| 24 | |
| 25 | func NewLinkEndpoint(debug bool, mtu uint32, macAddress string, ip string, virtualIPs []string) (*LinkEndpoint, error) { |
| 26 | linkAddr, err := net.ParseMAC(macAddress) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | set := make(map[string]struct{}) |
| 31 | for _, virtualIP := range virtualIPs { |
| 32 | set[virtualIP] = struct{}{} |
| 33 | } |
| 34 | return &LinkEndpoint{ |
| 35 | debug: debug, |
| 36 | mtu: mtu, |
| 37 | mac: tcpip.LinkAddress(linkAddr), |
| 38 | ip: ip, |
| 39 | virtualIPs: set, |
| 40 | }, nil |
| 41 | } |
| 42 | |
| 43 | func (e *LinkEndpoint) ARPHardwareType() header.ARPHardwareType { |
| 44 | return header.ARPHardwareEther |
| 45 | } |
| 46 | |
| 47 | func (e *LinkEndpoint) Connect(networkSwitch NetworkSwitch) { |
| 48 | e.networkSwitch = networkSwitch |
| 49 | } |
| 50 | |
| 51 | func (e *LinkEndpoint) Attach(dispatcher stack.NetworkDispatcher) { |
| 52 | e.dispatcher = dispatcher |
| 53 | } |
| 54 | |
| 55 | func (e *LinkEndpoint) IsAttached() bool { |
| 56 | return e.dispatcher != nil |
| 57 | } |
| 58 | |
| 59 | func (e *LinkEndpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { |
| 60 | e.dispatcher.DeliverNetworkPacket(protocol, pkt) |
| 61 | } |
| 62 | |
| 63 | func (e *LinkEndpoint) AddHeader(_ *stack.PacketBuffer) { |
| 64 | } |
| 65 | |
| 66 | func (e *LinkEndpoint) ParseHeader(*stack.PacketBuffer) bool { return true } |
| 67 | |
| 68 | func (e *LinkEndpoint) Capabilities() stack.LinkEndpointCapabilities { |
| 69 | return stack.CapabilityResolutionRequired | stack.CapabilityRXChecksumOffload |
| 70 | } |
| 71 | |
| 72 | func (e *LinkEndpoint) LinkAddress() tcpip.LinkAddress { |
| 73 | return e.mac |
| 74 | } |
| 75 | |
| 76 | func (e *LinkEndpoint) SetLinkAddress(addr tcpip.LinkAddress) { |
| 77 | e.mac = addr |
| 78 | } |
| 79 | |
| 80 | func (e *LinkEndpoint) MaxHeaderLength() uint16 { |
| 81 | return uint16(header.EthernetMinimumSize) |
| 82 | } |
| 83 | |
| 84 | func (e *LinkEndpoint) MTU() uint32 { |
| 85 | return e.mtu |
| 86 | } |
| 87 | |
| 88 | func (e *LinkEndpoint) SetMTU(mtu uint32) { |
| 89 | e.mtu = mtu |
| 90 | } |
| 91 | |
| 92 | func (e *LinkEndpoint) Wait() {} |
| 93 | func (e *LinkEndpoint) Close() {} |
| 94 | func (e *LinkEndpoint) SetOnCloseAction(_ func()) {} |
| 95 | |
| 96 | func (e *LinkEndpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) { |
| 97 | n := 0 |
| 98 | for _, p := range pkts.AsSlice() { |
| 99 | if err := e.writePacket(p.EgressRoute, p.NetworkProtocolNumber, p); err != nil { |
| 100 | return n, err |
| 101 | } |
| 102 | n++ |
| 103 | } |
| 104 | return n, nil |
| 105 | } |
| 106 | |
| 107 | func (e *LinkEndpoint) writePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error { |
| 108 | |
| 109 | srcAddr := e.LinkAddress() |
| 110 | if r.LocalLinkAddress != "" { |
| 111 | srcAddr = r.LocalLinkAddress |
| 112 | } |
| 113 | eth := header.Ethernet(pkt.LinkHeader().Push(header.EthernetMinimumSize)) |
| 114 | eth.Encode(&header.EthernetFields{ |
| 115 | Type: protocol, |
| 116 | SrcAddr: srcAddr, |
| 117 | DstAddr: r.RemoteLinkAddress, |
| 118 | }) |
| 119 | |
| 120 | h := header.ARP(pkt.NetworkHeader().Slice()) |
| 121 | if h.IsValid() && |
| 122 | h.Op() == header.ARPReply { |
| 123 | ip := tcpip.AddrFromSlice(h.ProtocolAddressSender()).String() |
| 124 | _, ok := e.virtualIPs[ip] |
| 125 | if ip != e.IP() && !ok { |
| 126 | log.Debugf("dropping spoofing packets from the gateway about IP %s", ip) |
| 127 | return nil |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if e.debug { |
| 132 | packet := gopacket.NewPacket(pkt.ToView().AsSlice(), layers.LayerTypeEthernet, gopacket.Default) |
| 133 | log.Info(packet.String()) |
| 134 | } |
| 135 | |
| 136 | e.networkSwitch.DeliverNetworkPacket(protocol, pkt) |
| 137 | return nil |
| 138 | } |
| 139 | |
| 140 | func (e *LinkEndpoint) WriteRawPacket(_ *stack.PacketBuffer) tcpip.Error { |
| 141 | return &tcpip.ErrNotSupported{} |
| 142 | } |
| 143 | |
| 144 | func (e *LinkEndpoint) IP() string { |
| 145 | return e.ip |
| 146 | } |