/
dhcp.go
105 lines (92 loc) · 2.69 KB
/
dhcp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
package main
import (
"net"
"strings"
"github.com/google/gopacket/layers"
)
func (s *State) HandleDHCP() {
e := &EventDHCP{
HardwareAddr: s.dhcp.ClientHWAddr,
ClientIP: s.dhcp.ClientIP,
}
for _, option := range s.dhcp.Options {
switch option.Type {
case layers.DHCPOptMessageType:
if len(option.Data) != 1 {
continue
}
e.MsgType = layers.DHCPMsgType(option.Data[0])
switch e.MsgType {
case layers.DHCPMsgTypeOffer, layers.DHCPMsgTypeAck:
if e.ClientIP.IsUnspecified() {
e.ClientIP = s.dhcp.YourClientIP
}
// Double check that the ClientIP is now specified. Seen many
// cases where neither ClientIP nor YourClientIP is set... not
// sure what that means.
if !e.ClientIP.IsUnspecified() {
// Set default subnet based on default mask. This is the
// same thing that Linux dhcp clients do when there is no
// Subnet Option.
mask := e.ClientIP.DefaultMask()
e.Subnet = net.IPNet{
IP: e.ClientIP.Mask(mask),
Mask: mask,
}
}
}
case layers.DHCPOptSubnetMask:
mask := net.IPMask(option.Data)
e.Subnet = net.IPNet{
IP: e.ClientIP.Mask(mask),
Mask: mask,
}
case layers.DHCPOptTimeOffset:
case layers.DHCPOptRouter:
data := option.Data
for len(data) > 0 {
e.Routers = append(e.Routers, net.IPNet{
IP: net.IP(data[:4]),
})
data = data[4:]
}
case layers.DHCPOptTimeServer:
case layers.DHCPOptNameServer:
case layers.DHCPOptDNS:
data := option.Data
for len(data) > 0 {
e.Nameservers = append(e.Nameservers, net.IP(data[:4]))
data = data[4:]
}
case layers.DHCPOptLogServer:
case layers.DHCPOptCookieServer:
case layers.DHCPOptLPRServer:
case layers.DHCPOptImpressServer:
case layers.DHCPOptResLocServer:
case layers.DHCPOptHostname:
// Hostnames are case insensitive (RFC 4343)
e.Hostname = strings.ToLower(string(option.Data))
case layers.DHCPOptBootfileSize:
case layers.DHCPOptMeritDumpFile:
case layers.DHCPOptDomainName:
e.Domain = string(option.Data)
case layers.DHCPOptSwapServer:
case layers.DHCPOptRootPath:
case layers.DHCPOptExtensionsPath:
case layers.DHCPOptRequestIP:
e.RequestedIPAddr = net.IP(option.Data)
}
}
// Patch up the routers with the subnet mask
for i := range e.Routers {
e.Routers[i].Mask = e.Subnet.Mask
}
// Patch up the hostname to make a FQDN
if e.Domain != "" && e.Hostname != "" && !strings.Contains(e.Hostname, ".") {
e.Hostname = e.Hostname + "." + e.Domain
}
s.events <- e
}