| 1 | package virtualnetwork |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | |
| 9 | "github.com/containers/gvisor-tap-vsock/pkg/services/dhcp" |
| 10 | "github.com/containers/gvisor-tap-vsock/pkg/services/dns" |
| 11 | "github.com/containers/gvisor-tap-vsock/pkg/services/forwarder" |
| 12 | "github.com/containers/gvisor-tap-vsock/pkg/tap" |
| 13 | "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 14 | log "github.com/sirupsen/logrus" |
| 15 | "gvisor.dev/gvisor/pkg/tcpip" |
| 16 | "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" |
| 17 | "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" |
| 18 | "gvisor.dev/gvisor/pkg/tcpip/stack" |
| 19 | "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" |
| 20 | "gvisor.dev/gvisor/pkg/tcpip/transport/udp" |
| 21 | ) |
| 22 | |
| 23 | func addServices(configuration *types.Configuration, s *stack.Stack, ipPool *tap.IPPool) (http.Handler, error) { |
| 24 | var natLock sync.Mutex |
| 25 | translation := parseNATTable(configuration) |
| 26 | |
| 27 | tcpForwarder := forwarder.TCP(s, translation, &natLock, configuration.Ec2MetadataAccess, configuration.Outbound, configuration.DialContext) |
| 28 | s.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpForwarder.HandlePacket) |
| 29 | udpForwarder := forwarder.UDP(s, translation, &natLock, configuration.Ec2MetadataAccess, configuration.Outbound) |
| 30 | s.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket) |
| 31 | |
| 32 | dnsMux, err := dnsServer(configuration, s) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | |
| 37 | dhcpMux, err := dhcpServer(configuration, s, ipPool) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | |
| 42 | forwarderMux, err := forwardHostVM(configuration, s) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | mux := http.NewServeMux() |
| 47 | mux.Handle("/forwarder/", http.StripPrefix("/forwarder", forwarderMux)) |
| 48 | mux.Handle("/dhcp/", http.StripPrefix("/dhcp", dhcpMux)) |
| 49 | mux.Handle("/dns/", http.StripPrefix("/dns", dnsMux)) |
| 50 | return mux, nil |
| 51 | } |
| 52 | |
| 53 | func parseNATTable(configuration *types.Configuration) map[tcpip.Address]tcpip.Address { |
| 54 | translation := make(map[tcpip.Address]tcpip.Address) |
| 55 | for source, destination := range configuration.NAT { |
| 56 | translation[tcpip.AddrFrom4Slice(net.ParseIP(source).To4())] = tcpip.AddrFrom4Slice(net.ParseIP(destination).To4()) |
| 57 | } |
| 58 | return translation |
| 59 | } |
| 60 | |
| 61 | func dnsServer(configuration *types.Configuration, s *stack.Stack) (http.Handler, error) { |
| 62 | udpConn, err := gonet.DialUDP(s, &tcpip.FullAddress{ |
| 63 | NIC: 1, |
| 64 | Addr: tcpip.AddrFrom4Slice(net.ParseIP(configuration.GatewayIP).To4()), |
| 65 | Port: uint16(53), |
| 66 | }, nil, ipv4.ProtocolNumber) |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | |
| 71 | tcpLn, err := gonet.ListenTCP(s, tcpip.FullAddress{ |
| 72 | NIC: 1, |
| 73 | Addr: tcpip.AddrFrom4Slice(net.ParseIP(configuration.GatewayIP).To4()), |
| 74 | Port: uint16(53), |
| 75 | }, ipv4.ProtocolNumber) |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | |
| 80 | server, err := dns.New(udpConn, tcpLn, configuration.DNS) |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | |
| 85 | server.SetPolicy(configuration.DNSNames, configuration.DNSAnswered) |
| 86 | server.SetExchange(configuration.DNSExchange) |
| 87 | |
| 88 | go func() { |
| 89 | if err := server.Serve(); err != nil { |
| 90 | log.Error(err) |
| 91 | } |
| 92 | }() |
| 93 | go func() { |
| 94 | if err := server.ServeTCP(); err != nil { |
| 95 | log.Error(err) |
| 96 | } |
| 97 | }() |
| 98 | return server.Mux(), nil |
| 99 | } |
| 100 | |
| 101 | func dhcpServer(configuration *types.Configuration, s *stack.Stack, ipPool *tap.IPPool) (http.Handler, error) { |
| 102 | server, err := dhcp.New(configuration, s, ipPool) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | go func() { |
| 107 | log.Error(server.Serve()) |
| 108 | }() |
| 109 | return server.Mux(), nil |
| 110 | } |
| 111 | |
| 112 | func forwardHostVM(configuration *types.Configuration, s *stack.Stack) (http.Handler, error) { |
| 113 | fw := forwarder.NewPortsForwarder(s) |
| 114 | for local, remote := range configuration.Forwards { |
| 115 | if strings.HasPrefix(local, "udp:") { |
| 116 | if err := fw.Expose(types.UDP, strings.TrimPrefix(local, "udp:"), remote); err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | } else { |
| 120 | if err := fw.Expose(types.TCP, local, remote); err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | return fw.Mux(), nil |
| 126 | } |