Merge pull request #13209 from swordqiu/feature/qj-support-dhcp-option-82

feature: support dhcp relay information option 82
This commit is contained in:
Zexi Li
2022-01-17 10:08:10 +08:00
committed by GitHub
3 changed files with 26 additions and 0 deletions
+6
View File
@@ -88,6 +88,12 @@ func (h *DHCPHandler) ServeDHCP(pkt dhcp.Packet, _ *net.UDPAddr, _ *net.Interfac
if conf == nil {
return nil, nil, fmt.Errorf("Empty packet config")
}
// handle Option 82 relay information
// https://datatracker.ietf.org/doc/html/rfc3046
if relayInfo := pkt.GetOptionValue(dhcp.OptionRelayAgentInformation); relayInfo != nil {
// return relay information transparently
conf.RelayInfo = relayInfo
}
pkg, err := dhcp.MakeReplyPacket(pkt, conf)
if err != nil {
return nil, nil, errors.Wrap(err, "dhcp.MakeReplyPacket")
@@ -151,6 +151,10 @@ func (s *SGuestDHCPServer) getGuestConfig(guestDesc, guestNic jsonutils.JSONObje
}
}
if nicdesc.Mtu > 0 {
conf.MTU = uint16(nicdesc.Mtu)
}
conf.OsName, _ = guestDesc.GetString("os_name")
conf.LeaseTime = time.Duration(options.HostOptions.DhcpLeaseTime) * time.Second
conf.RenewalTime = time.Duration(options.HostOptions.DhcpRenewalTime) * time.Second
+16
View File
@@ -47,6 +47,10 @@ type ResponseConfig struct {
DNSServers []net.IP // OptDNSServers
Routes [][]string // TODO: 249 for windows, 121 for linux
NTPServers []net.IP // OptNTPServers 42
MTU uint16 // OptMTU 26
// Relay Info https://datatracker.ietf.org/doc/html/rfc3046
RelayInfo []byte
// TFTP config
BootServer string
@@ -62,6 +66,12 @@ func (conf ResponseConfig) GetHostname() string {
return hostname
}
func GetOptUint16(val uint16) []byte {
opts := []byte{0, 0}
binary.BigEndian.PutUint16(opts, val)
return opts
}
func GetOptIP(ip net.IP) []byte {
return []byte(ip.To4())
}
@@ -154,6 +164,12 @@ func makeDHCPReplyPacket(req Packet, conf *ResponseConfig, msgType MessageType)
if len(conf.NTPServers) > 0 {
opts = append(opts, Option{OptionNetworkTimeProtocolServers, GetOptIPs(conf.NTPServers)})
}
if conf.MTU > 0 {
opts = append(opts, Option{OptionInterfaceMTU, GetOptUint16(conf.MTU)})
}
if conf.RelayInfo != nil {
opts = append(opts, Option{OptionRelayAgentInformation, conf.RelayInfo})
}
resp := ReplyPacket(req, msgType, conf.ServerIP, conf.ClientIP, conf.LeaseTime, opts)
if conf.BootServer != "" {
//resp.Options[OptOverload] = []byte{3}