| 1 | package virtualnetwork |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "math" |
| 7 | "net" |
| 8 | "net/http" |
| 9 | "os" |
| 10 | |
| 11 | "github.com/containers/gvisor-tap-vsock/pkg/notification" |
| 12 | "github.com/containers/gvisor-tap-vsock/pkg/tap" |
| 13 | "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 14 | "gvisor.dev/gvisor/pkg/tcpip" |
| 15 | "gvisor.dev/gvisor/pkg/tcpip/header" |
| 16 | "gvisor.dev/gvisor/pkg/tcpip/link/sniffer" |
| 17 | "gvisor.dev/gvisor/pkg/tcpip/network/arp" |
| 18 | "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" |
| 19 | "gvisor.dev/gvisor/pkg/tcpip/stack" |
| 20 | "gvisor.dev/gvisor/pkg/tcpip/transport/icmp" |
| 21 | "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" |
| 22 | "gvisor.dev/gvisor/pkg/tcpip/transport/udp" |
| 23 | ) |
| 24 | |
| 25 | type VirtualNetwork struct { |
| 26 | configuration *types.Configuration |
| 27 | stack *stack.Stack |
| 28 | networkSwitch *tap.Switch |
| 29 | servicesMux http.Handler |
| 30 | ipPool *tap.IPPool |
| 31 | } |
| 32 | |
| 33 | func (n *VirtualNetwork) SetNotificationSender(notificationSender *notification.NotificationSender) { |
| 34 | n.networkSwitch.SetNotificationSender(notificationSender) |
| 35 | } |
| 36 | |
| 37 | func New(configuration *types.Configuration) (*VirtualNetwork, error) { |
| 38 | _, subnet, err := net.ParseCIDR(configuration.Subnet) |
| 39 | if err != nil { |
| 40 | return nil, fmt.Errorf("cannot parse subnet cidr: %w", err) |
| 41 | } |
| 42 | |
| 43 | var endpoint stack.LinkEndpoint |
| 44 | |
| 45 | ipPool := tap.NewIPPool(subnet) |
| 46 | ipPool.Reserve(net.ParseIP(configuration.GatewayIP), configuration.GatewayMacAddress) |
| 47 | for ip, mac := range configuration.DHCPStaticLeases { |
| 48 | ipPool.Reserve(net.ParseIP(ip), mac) |
| 49 | } |
| 50 | |
| 51 | mtu := configuration.MTU |
| 52 | if mtu < 0 || mtu > math.MaxInt32 { |
| 53 | return nil, errors.New("mtu is out of range") |
| 54 | } |
| 55 | tapEndpoint, err := tap.NewLinkEndpoint(configuration.Debug, uint32(mtu), configuration.GatewayMacAddress, configuration.GatewayIP, configuration.GatewayVirtualIPs) |
| 56 | if err != nil { |
| 57 | return nil, fmt.Errorf("cannot create tap endpoint: %w", err) |
| 58 | } |
| 59 | networkSwitch := tap.NewSwitch(configuration.Debug) |
| 60 | tapEndpoint.Connect(networkSwitch) |
| 61 | networkSwitch.Connect(tapEndpoint) |
| 62 | |
| 63 | if configuration.CaptureFile != "" { |
| 64 | _ = os.Remove(configuration.CaptureFile) |
| 65 | fd, err := os.Create(configuration.CaptureFile) |
| 66 | if err != nil { |
| 67 | return nil, fmt.Errorf("cannot create capture file: %w", err) |
| 68 | } |
| 69 | endpoint, err = sniffer.NewWithWriter(tapEndpoint, fd, math.MaxUint32) |
| 70 | if err != nil { |
| 71 | return nil, fmt.Errorf("cannot create sniffer: %w", err) |
| 72 | } |
| 73 | } else { |
| 74 | endpoint = tapEndpoint |
| 75 | } |
| 76 | |
| 77 | stack, err := createStack(configuration, endpoint) |
| 78 | if err != nil { |
| 79 | return nil, fmt.Errorf("cannot create network stack: %w", err) |
| 80 | } |
| 81 | |
| 82 | if configuration.DroppedIPv6 != nil && stack.NetworkProtocolInstance(header.IPv6ProtocolNumber) == nil { |
| 83 | networkSwitch.SetDroppedIPv6(configuration.DroppedIPv6) |
| 84 | } |
| 85 | mux, err := addServices(configuration, stack, ipPool) |
| 86 | if err != nil { |
| 87 | return nil, fmt.Errorf("cannot add network services: %w", err) |
| 88 | } |
| 89 | |
| 90 | return &VirtualNetwork{ |
| 91 | configuration: configuration, |
| 92 | stack: stack, |
| 93 | networkSwitch: networkSwitch, |
| 94 | servicesMux: mux, |
| 95 | ipPool: ipPool, |
| 96 | }, nil |
| 97 | } |
| 98 | |
| 99 | func (n *VirtualNetwork) BytesSent() uint64 { |
| 100 | if n.networkSwitch == nil { |
| 101 | return 0 |
| 102 | } |
| 103 | return n.networkSwitch.Sent |
| 104 | } |
| 105 | |
| 106 | func (n *VirtualNetwork) BytesReceived() uint64 { |
| 107 | if n.networkSwitch == nil { |
| 108 | return 0 |
| 109 | } |
| 110 | return n.networkSwitch.Received |
| 111 | } |
| 112 | |
| 113 | func createStack(configuration *types.Configuration, endpoint stack.LinkEndpoint) (*stack.Stack, error) { |
| 114 | s := stack.New(stack.Options{ |
| 115 | NetworkProtocols: []stack.NetworkProtocolFactory{ |
| 116 | ipv4.NewProtocol, |
| 117 | arp.NewProtocol, |
| 118 | }, |
| 119 | TransportProtocols: []stack.TransportProtocolFactory{ |
| 120 | tcp.NewProtocol, |
| 121 | udp.NewProtocol, |
| 122 | icmp.NewProtocol4, |
| 123 | }, |
| 124 | }) |
| 125 | |
| 126 | if err := s.CreateNIC(1, endpoint); err != nil { |
| 127 | return nil, errors.New(err.String()) |
| 128 | } |
| 129 | |
| 130 | if err := s.AddProtocolAddress(1, tcpip.ProtocolAddress{ |
| 131 | Protocol: ipv4.ProtocolNumber, |
| 132 | AddressWithPrefix: tcpip.AddrFrom4Slice(net.ParseIP(configuration.GatewayIP).To4()).WithPrefix(), |
| 133 | }, stack.AddressProperties{}); err != nil { |
| 134 | return nil, errors.New(err.String()) |
| 135 | } |
| 136 | |
| 137 | s.SetSpoofing(1, true) |
| 138 | s.SetPromiscuousMode(1, true) |
| 139 | |
| 140 | _, parsedSubnet, err := net.ParseCIDR(configuration.Subnet) |
| 141 | if err != nil { |
| 142 | return nil, fmt.Errorf("cannot parse cidr: %w", err) |
| 143 | } |
| 144 | |
| 145 | subnet, err := tcpip.NewSubnet(tcpip.AddrFromSlice(parsedSubnet.IP), tcpip.MaskFromBytes(parsedSubnet.Mask)) |
| 146 | if err != nil { |
| 147 | return nil, fmt.Errorf("cannot parse subnet: %w", err) |
| 148 | } |
| 149 | s.SetRouteTable([]tcpip.Route{ |
| 150 | { |
| 151 | Destination: subnet, |
| 152 | Gateway: tcpip.Address{}, |
| 153 | NIC: 1, |
| 154 | }, |
| 155 | }) |
| 156 | |
| 157 | return s, nil |
| 158 | } |