forked from galthaus/ocb-dhcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhcp.go
200 lines (169 loc) · 4.89 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Example of minimal DHCP server:
package main
import (
"log"
"net"
"strings"
dhcp "github.com/krolaw/dhcp4"
)
func RunDhcpHandler(dhcpInfo *DataTracker, intf net.Interface, myIp string) {
log.Println("Starting on interface: ", intf.Name, " with server ip: ", myIp)
serverIP, _, _ := net.ParseCIDR(myIp)
serverIP = serverIP.To4()
handler := &DHCPHandler{
ip: serverIP,
intf: intf,
info: dhcpInfo,
}
log.Fatal(dhcp.ListenAndServeIf(intf.Name, handler))
}
func StartDhcpHandlers(dhcpInfo *DataTracker, serverIp string) error {
intfs, err := net.Interfaces()
if err != nil {
return err
}
for _, intf := range intfs {
if (intf.Flags & net.FlagLoopback) == net.FlagLoopback {
continue
}
if (intf.Flags & net.FlagUp) != net.FlagUp {
continue
}
if strings.HasPrefix(intf.Name, "veth") {
continue
}
var sip string
addrs, err := intf.Addrs()
if err != nil {
return err
}
for _, addr := range addrs {
thisIP, _, _ := net.ParseCIDR(addr.String())
// Only care about addresses that are not link-local.
if !thisIP.IsGlobalUnicast() {
continue
}
// Only deal with IPv4 for now.
if thisIP.To4() == nil {
continue
}
if serverIp != "" && serverIp == addr.String() {
sip = addr.String()
break
}
}
if sip == "" {
continue
}
// Only run the first one that matches
go RunDhcpHandler(dhcpInfo, intf, sip)
break
}
return nil
}
type DHCPHandler struct {
intf net.Interface // Interface processing on.
ip net.IP // Server IP to use
info *DataTracker // Subnet data
}
func (h *DHCPHandler) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options) (d dhcp.Packet) {
// First find the subnet to use. giaddr field to lookup subnet if not all zeros.
// If all zeros, use the interfaces Addrs to find a subnet, first wins.
var subnet *Subnet
subnet = nil
giaddr := p.GIAddr()
if !giaddr.Equal(net.IPv4zero) {
subnet = h.info.FindSubnet(giaddr)
} else {
log.Println("Received Broadcast/Local message on ", h.intf.Name)
addrs, err := h.intf.Addrs()
if err != nil {
log.Println("Can't find addresses for ", h.intf.Name, ": ", err)
}
for _, a := range addrs {
aip, _, _ := net.ParseCIDR(a.String())
// Only operate on v4 addresses
if aip.To4() == nil {
continue
}
subnet = h.info.FindSubnet(aip)
if subnet != nil {
break
}
}
if ignore_anonymus {
// Search all subnets for a binding. First wins
log.Println("Looking up bound subnet for ", p.CHAddr().String())
subnet = h.info.FindBoundIP(p.CHAddr())
}
if subnet == nil {
// We didn't find a subnet for the interface. Look for the assigned server IP
subnet = h.info.FindSubnet(h.ip)
}
}
if subnet == nil {
log.Println("Can not find subnet for packet, ignoring")
return
}
nic := p.CHAddr().String()
switch msgType {
case dhcp.Discover:
lease, binding := subnet.find_or_get_info(h.info, nic, p.CIAddr())
if lease == nil {
log.Println("Out of IPs for ", subnet.Name, ", ignoring")
return nil
}
// Ignore unknown MAC address
if ignore_anonymus && binding == nil {
log.Println("Ignoring request from unknown MAC address")
return dhcp.ReplyPacket(p, dhcp.NAK, h.ip, nil, 0, nil)
}
options, lease_time := subnet.build_options(lease, binding)
reply := dhcp.ReplyPacket(p, dhcp.Offer,
h.ip,
lease.Ip,
lease_time,
subnet.Options.SelectOrderOrAll(options[dhcp.OptionParameterRequestList]))
log.Println("Discover: Handing out: ", reply.YIAddr(), " to ", reply.CHAddr())
return reply
case dhcp.Request:
server, ok := options[dhcp.OptionServerIdentifier]
if ok && !net.IP(server).Equal(h.ip) {
return nil // Message not for this dhcp server
}
reqIP := net.IP(options[dhcp.OptionRequestedIPAddress])
if reqIP == nil {
reqIP = net.IP(p.CIAddr())
}
if len(reqIP) != 4 || reqIP.Equal(net.IPv4zero) {
return dhcp.ReplyPacket(p, dhcp.NAK, h.ip, nil, 0, nil)
}
lease, binding := subnet.find_info(h.info, nic)
// Ignore unknown MAC address
if ignore_anonymus && binding == nil {
log.Println("Ignoring request from unknown MAC address")
return dhcp.ReplyPacket(p, dhcp.NAK, h.ip, nil, 0, nil)
}
if lease == nil || !lease.Ip.Equal(reqIP) {
return dhcp.ReplyPacket(p, dhcp.NAK, h.ip, nil, 0, nil)
}
options, lease_time := subnet.build_options(lease, binding)
subnet.update_lease_time(h.info, lease, lease_time)
reply := dhcp.ReplyPacket(p, dhcp.ACK,
h.ip,
lease.Ip,
lease_time,
subnet.Options.SelectOrderOrAll(options[dhcp.OptionParameterRequestList]))
if binding != nil && binding.NextServer != nil {
reply.SetSIAddr(net.ParseIP(*binding.NextServer))
} else if subnet.NextServer != nil {
reply.SetSIAddr(*subnet.NextServer)
}
log.Println("Request: Handing out: ", reply.YIAddr(), " to ", reply.CHAddr())
return reply
case dhcp.Release, dhcp.Decline:
nic := p.CHAddr().String()
subnet.free_lease(h.info, nic)
}
return nil
}