| 1 | package dhcp |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "math" |
| 7 | "net" |
| 8 | "net/http" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/containers/gvisor-tap-vsock/pkg/tap" |
| 12 | "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 13 | "github.com/insomniacslk/dhcp/dhcpv4" |
| 14 | "github.com/insomniacslk/dhcp/dhcpv4/server4" |
| 15 | "github.com/insomniacslk/dhcp/rfc1035label" |
| 16 | log "github.com/sirupsen/logrus" |
| 17 | "gvisor.dev/gvisor/pkg/tcpip" |
| 18 | "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" |
| 19 | "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" |
| 20 | "gvisor.dev/gvisor/pkg/tcpip/stack" |
| 21 | "gvisor.dev/gvisor/pkg/tcpip/transport/udp" |
| 22 | "gvisor.dev/gvisor/pkg/waiter" |
| 23 | ) |
| 24 | |
| 25 | const serverPort = 67 |
| 26 | |
| 27 | func handler(configuration *types.Configuration, ipPool *tap.IPPool) server4.Handler { |
| 28 | return func(conn net.PacketConn, peer net.Addr, m *dhcpv4.DHCPv4) { |
| 29 | reply, err := dhcpv4.NewReplyFromRequest(m) |
| 30 | if err != nil { |
| 31 | log.Errorf("dhcp: cannot build reply from request: %v", err) |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | ip, err := ipPool.GetOrAssign(m.ClientHWAddr.String()) |
| 36 | if err != nil { |
| 37 | log.Errorf("dhcp: cannot assign ip: %v", err) |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | _, parsedSubnet, err := net.ParseCIDR(configuration.Subnet) |
| 42 | if err != nil { |
| 43 | log.Errorf("dhcp: invalid subnet %v", err) |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | reply.YourIPAddr = ip |
| 48 | reply.UpdateOption(dhcpv4.OptServerIdentifier(net.ParseIP(configuration.GatewayIP))) |
| 49 | reply.UpdateOption(dhcpv4.OptIPAddressLeaseTime(time.Hour)) |
| 50 | |
| 51 | reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionSubnetMask, Value: dhcpv4.IP(parsedSubnet.Mask)}) |
| 52 | reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionRouter, Value: dhcpv4.IP(net.ParseIP(configuration.GatewayIP))}) |
| 53 | reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionDomainNameServer, Value: dhcpv4.IPs([]net.IP{net.ParseIP(configuration.GatewayIP)})}) |
| 54 | |
| 55 | mtu := configuration.MTU |
| 56 | if mtu < 0 || mtu > math.MaxUint16 { |
| 57 | log.Errorf("dhcp: invalid MTU %d", mtu) |
| 58 | } else { |
| 59 | reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionInterfaceMTU, Value: dhcpv4.Uint16(mtu)}) |
| 60 | } |
| 61 | reply.UpdateOption(dhcpv4.Option{Code: dhcpv4.OptionDNSDomainSearchList, Value: &rfc1035label.Labels{ |
| 62 | Labels: configuration.DNSSearchDomains, |
| 63 | }}) |
| 64 | |
| 65 | switch mt := m.MessageType(); mt { |
| 66 | case dhcpv4.MessageTypeDiscover: |
| 67 | reply.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeOffer)) |
| 68 | case dhcpv4.MessageTypeRequest: |
| 69 | reply.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeAck)) |
| 70 | case dhcpv4.MessageTypeRelease: |
| 71 | log.Debugf("dhcp: unhandled message type: %v", mt) |
| 72 | return |
| 73 | default: |
| 74 | log.Errorf("dhcp: unhandled message type: %v", mt) |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | if _, err := conn.WriteTo(reply.ToBytes(), peer); err != nil { |
| 79 | log.Errorf("dhcp: cannot reply to client: %v", err) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func dial(s *stack.Stack, nic tcpip.NICID) (*gonet.UDPConn, error) { |
| 85 | var wq waiter.Queue |
| 86 | ep, err := s.NewEndpoint(udp.ProtocolNumber, ipv4.ProtocolNumber, &wq) |
| 87 | if err != nil { |
| 88 | return nil, errors.New(err.String()) |
| 89 | } |
| 90 | |
| 91 | ep.SocketOptions().SetBroadcast(true) |
| 92 | |
| 93 | if err := ep.Bind(tcpip.FullAddress{ |
| 94 | NIC: tcpip.NICID(nic), |
| 95 | Addr: tcpip.Address{}, |
| 96 | Port: uint16(serverPort), |
| 97 | }); err != nil { |
| 98 | ep.Close() |
| 99 | return nil, errors.New(err.String()) |
| 100 | } |
| 101 | |
| 102 | return gonet.NewUDPConn(&wq, ep), nil |
| 103 | } |
| 104 | |
| 105 | type Server struct { |
| 106 | Underlying *server4.Server |
| 107 | IPPool *tap.IPPool |
| 108 | } |
| 109 | |
| 110 | func New(configuration *types.Configuration, stack *stack.Stack, ipPool *tap.IPPool) (*Server, error) { |
| 111 | ln, err := dial(stack, tcpip.NICID(1)) |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | |
| 116 | s, err := server4.NewServer("", nil, handler(configuration, ipPool), server4.WithConn(ln)) |
| 117 | if err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | |
| 121 | return &Server{ |
| 122 | Underlying: s, |
| 123 | IPPool: ipPool, |
| 124 | }, nil |
| 125 | } |
| 126 | |
| 127 | func (s *Server) Serve() error { |
| 128 | return s.Underlying.Serve() |
| 129 | } |
| 130 | |
| 131 | func (s *Server) Mux() http.Handler { |
| 132 | mux := http.NewServeMux() |
| 133 | mux.HandleFunc("/leases", func(w http.ResponseWriter, _ *http.Request) { |
| 134 | _ = json.NewEncoder(w).Encode(s.IPPool.Leases()) |
| 135 | }) |
| 136 | return mux |
| 137 | } |