From d399ca8d71f010fd881347f1aeb0287c3932bddc Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Mon, 24 Jun 2019 20:28:00 +0800 Subject: [PATCH] make dhcp server port optional --- pkg/baremetal/pxe/pxe.go | 6 +- pkg/hostman/hostinfo/hostdhcp/dhcpserver.go | 22 ++++-- pkg/util/dhcp/server.go | 84 ++++++++++----------- 3 files changed, 57 insertions(+), 55 deletions(-) diff --git a/pkg/baremetal/pxe/pxe.go b/pkg/baremetal/pxe/pxe.go index dbd0e0a4b8..8b0ea0f5ad 100644 --- a/pkg/baremetal/pxe/pxe.go +++ b/pkg/baremetal/pxe/pxe.go @@ -128,11 +128,7 @@ func (s *Server) Serve() error { return err } - if s.DHCPPort != 67 { - return fmt.Errorf("DHCP listen port %d is not support", s.DHCPPort) - } - - dhcpSrv, _, err := dhcp.NewDHCPServer2(s.ListenIface, dhcp.PORT_67) + dhcpSrv, _, err := dhcp.NewDHCPServer2(s.ListenIface, uint16(s.DHCPPort)) if err != nil { return err } diff --git a/pkg/hostman/hostinfo/hostdhcp/dhcpserver.go b/pkg/hostman/hostinfo/hostdhcp/dhcpserver.go index 91a228a0aa..9012b9995a 100644 --- a/pkg/hostman/hostinfo/hostdhcp/dhcpserver.go +++ b/pkg/hostman/hostinfo/hostdhcp/dhcpserver.go @@ -31,6 +31,8 @@ import ( "yunion.io/x/onecloud/pkg/util/netutils2" ) +const DHCP_RELAY_SERVER_PORT = 68 + type SGuestDHCPServer struct { server *dhcp.DHCPServer relay *SDHCPRelay @@ -45,21 +47,25 @@ func NewGuestDHCPServer(iface string, relay []string) (*SGuestDHCPServer, error) guestdhcp = new(SGuestDHCPServer) ) - if options.HostOptions.DhcpServerPort != 67 { - return nil, fmt.Errorf("DHCP server listen port %d is not support", options.HostOptions.DhcpServerPort) - } - - // port 67 for dhcp server, 68 for dhcp relay server - guestdhcp.server, guestdhcp.conn, err = dhcp.NewDHCPServer2(iface, dhcp.PORT_67_AND_68) - if err != nil { - return nil, err + if len(relay) > 0 && len(relay) != 2 { + return nil, fmt.Errorf("Wrong dhcp relay address") } if len(relay) == 2 { + // port 67 for dhcp server, 68 for dhcp relay server + guestdhcp.server, guestdhcp.conn, err = dhcp.NewDHCPServerWithRelay(iface, uint16(options.HostOptions.DhcpServerPort), DHCP_RELAY_SERVER_PORT) + if err != nil { + return nil, err + } guestdhcp.relay, err = NewDHCPRelay(guestdhcp.conn, relay, iface) if err != nil { return nil, err } + } else { + guestdhcp.server, guestdhcp.conn, err = dhcp.NewDHCPServer2(iface, uint16(options.HostOptions.DhcpServerPort)) + if err != nil { + return nil, err + } } guestdhcp.iface = iface diff --git a/pkg/util/dhcp/server.go b/pkg/util/dhcp/server.go index d234e108d5..7907df2b93 100644 --- a/pkg/util/dhcp/server.go +++ b/pkg/util/dhcp/server.go @@ -23,44 +23,6 @@ import ( "yunion.io/x/log" ) -const ( - PORT_67 = 67 - PORT_67_AND_68 = 67 + 68 -) - -var UDP_BPF_PORT_MAP = map[uint16][]bpf.RawInstruction{ - // ip and udp and dst port 67 - PORT_67: { - {0x28, 0, 0, 0x0000000c}, - {0x15, 0, 8, 0x00000800}, - {0x30, 0, 0, 0x00000017}, - {0x15, 0, 6, 0x00000011}, - {0x28, 0, 0, 0x00000014}, - {0x45, 4, 0, 0x00001fff}, - {0xb1, 0, 0, 0x0000000e}, - {0x48, 0, 0, 0x00000010}, - {0x15, 0, 1, 0x00000043}, - {0x6, 0, 0, 0x00040000}, - {0x6, 0, 0, 0x00000000}, - }, - // ip and udp and dst port 67 and port 68 - PORT_67_AND_68: { - {0x28, 0, 0, 0x0000000c}, - {0x15, 0, 10, 0x00000800}, - {0x30, 0, 0, 0x00000017}, - {0x15, 0, 8, 0x00000011}, - {0x28, 0, 0, 0x00000014}, - {0x45, 6, 0, 0x00001fff}, - {0xb1, 0, 0, 0x0000000e}, - {0x48, 0, 0, 0x00000010}, - {0x15, 0, 3, 0x00000043}, - {0x48, 0, 0, 0x0000000e}, - {0x15, 0, 1, 0x00000044}, - {0x6, 0, 0, 0x00040000}, - {0x6, 0, 0, 0x00000000}, - }, -} - type DHCPServer struct { Address string Port int @@ -74,10 +36,48 @@ func NewDHCPServer(address string, port int) *DHCPServer { } } -func NewDHCPServer2(iface string, portDesc uint16) (*DHCPServer, *Conn, error) { - bpf, ok := UDP_BPF_PORT_MAP[portDesc] - if !ok { - return nil, nil, fmt.Errorf("BPF not found %d", portDesc) +func NewDHCPServer2(iface string, port uint16) (*DHCPServer, *Conn, error) { + bpf := []bpf.RawInstruction{ // ip and udp and dst port 67 + {0x28, 0, 0, 0x0000000c}, + {0x15, 0, 8, 0x00000800}, + {0x30, 0, 0, 0x00000017}, + {0x15, 0, 6, 0x00000011}, + {0x28, 0, 0, 0x00000014}, + {0x45, 4, 0, 0x00001fff}, + {0xb1, 0, 0, 0x0000000e}, + {0x48, 0, 0, 0x00000010}, + {0x15, 0, 1, uint32(port)}, + {0x6, 0, 0, 0x00040000}, + {0x6, 0, 0, 0x00000000}, + } + conn, err := NewSocketConn(iface, bpf) + if err != nil { + return nil, nil, err + } + return &DHCPServer{ + conn: conn, + }, conn, nil +} + +func NewDHCPServerWithRelay(iface string, dhcpServerPort, dhcpRelayPort uint16) (*DHCPServer, *Conn, error) { + // ip and udp and port 67 and port 68 + bpf := []bpf.RawInstruction{ + {0x28, 0, 0, 0x0000000c}, + {0x15, 0, 13, 0x00000800}, + {0x30, 0, 0, 0x00000017}, + {0x15, 0, 11, 0x00000011}, + {0x28, 0, 0, 0x00000014}, + {0x45, 9, 0, 0x00001fff}, + {0xb1, 0, 0, 0x0000000e}, + {0x48, 0, 0, 0x0000000e}, + {0x15, 0, 2, uint32(dhcpServerPort)}, + {0x48, 0, 0, 0x00000010}, + {0x15, 3, 4, uint32(dhcpRelayPort)}, + {0x15, 0, 3, uint32(dhcpRelayPort)}, + {0x48, 0, 0, 0x00000010}, + {0x15, 0, 1, uint32(dhcpServerPort)}, + {0x6, 0, 0, 0x00040000}, + {0x6, 0, 0, 0x00000000}, } conn, err := NewSocketConn(iface, bpf) if err != nil {