-
Notifications
You must be signed in to change notification settings - Fork 10
/
dhcpv4.go
371 lines (284 loc) · 9.61 KB
/
dhcpv4.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package handler
import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/ipchama/dhammer/config"
"github.com/ipchama/dhammer/message"
"github.com/ipchama/dhammer/socketeer"
"github.com/ipchama/dhammer/stats"
"github.com/vishvananda/netlink"
"net"
"time"
)
type LeaseDhcpV4 struct {
Packet gopacket.Packet
LinkAddr *netlink.Addr
Acquired time.Time
HwAddr net.HardwareAddr
}
type HandlerDhcpV4 struct {
options *config.DhcpV4Options
socketeer *socketeer.RawSocketeer
iface *net.Interface
link netlink.Link
acquiredIPs map[string]*LeaseDhcpV4
addLog func(string) bool
addError func(error) bool
sendPayload func([]byte) bool
addStat func(stats.StatValue) bool
inputChannel chan message.Message
doneChannel chan struct{}
}
func init() {
if err := AddHandler("dhcpv4", NewDhcpV4); err != nil {
panic(err)
}
}
func NewDhcpV4(hip HandlerInitParams) Handler {
h := HandlerDhcpV4{
options: hip.options.(*config.DhcpV4Options),
socketeer: hip.socketeer,
iface: hip.socketeer.IfInfo,
acquiredIPs: make(map[string]*LeaseDhcpV4),
addLog: hip.logFunc,
addError: hip.errFunc,
sendPayload: hip.socketeer.AddPayload,
addStat: hip.statFunc,
inputChannel: make(chan message.Message, 10000),
doneChannel: make(chan struct{}),
}
return &h
}
func (h *HandlerDhcpV4) ReceiveMessage(msg message.Message) bool {
select {
case h.inputChannel <- msg:
return true
default:
}
return false
}
func (h *HandlerDhcpV4) Init() error {
var err error = nil
h.link, err = netlink.LinkByName("lo")
return err
}
func (h *HandlerDhcpV4) DeInit() error {
if h.options.Bind {
for _, lease := range h.acquiredIPs {
if err := netlink.AddrDel(h.link, lease.LinkAddr); err != nil {
h.addError(err)
}
}
}
return nil
}
func (h *HandlerDhcpV4) Stop() error {
close(h.inputChannel)
<-h.doneChannel
return nil
}
func (h *HandlerDhcpV4) Run() {
var msg message.Message
var dhcpReply *layers.DHCPv4
socketeerOptions := h.socketeer.Options()
ethernetLayer := &layers.Ethernet{
DstMAC: layers.EthernetBroadcast,
SrcMAC: h.iface.HardwareAddr,
EthernetType: layers.EthernetTypeIPv4,
Length: 0,
}
if !h.options.EthernetBroadcast {
ethernetLayer.DstMAC = socketeerOptions.GatewayMAC
}
ipLayer := &layers.IPv4{
Version: 4, // IPv4
TTL: 64,
Protocol: 17, // UDP
SrcIP: net.IPv4(0, 0, 0, 0),
DstIP: net.IPv4(255, 255, 255, 255),
}
udpLayer := &layers.UDP{
SrcPort: layers.UDPPort(68),
DstPort: layers.UDPPort(h.options.TargetPort),
}
outDhcpLayer := &layers.DHCPv4{
Operation: layers.DHCPOpRequest,
HardwareType: layers.LinkTypeEthernet,
HardwareLen: 6,
Flags: 0x8000, // Broadcast
}
if !h.options.DhcpBroadcast {
outDhcpLayer.Flags = 0x0
}
if h.options.DhcpRelay {
ipLayer.SrcIP = h.options.RelaySourceIP
ipLayer.DstIP = h.options.RelayTargetServerIP
ethernetLayer.SrcMAC = h.iface.HardwareAddr
ethernetLayer.DstMAC = socketeerOptions.GatewayMAC
outDhcpLayer.RelayAgentIP = h.options.RelayGatewayIP
udpLayer.SrcPort = 67
}
goPacketSerializeOpts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
for msg = range h.inputChannel {
if h.options.Arp && msg.Packet.Layer(layers.LayerTypeARP) != nil {
h.addStat(stats.ArpRequestReceivedStat)
h.handleARP(msg)
continue
} else if msg.Packet.Layer(layers.LayerTypeDHCPv4) == nil {
continue
}
dhcpReply = msg.Packet.Layer(layers.LayerTypeDHCPv4).(*layers.DHCPv4)
var replyOptions [256]layers.DHCPOption
for _, option := range dhcpReply.Options { // Assuming that we'll expand on usage of options in the reply later and just doing this now.
replyOptions[option.Type] = option
}
replyMsgType := replyOptions[layers.DHCPOptMessageType].Data[0]
//h.addLog(fmt.Sprintf("[REPLY] %v %v %v %v %v", dhcpReply.Options[0].String(), dhcpReply.YourClientIP.String(), string(dhcpReply.ServerName), dhcpReply.ClientIP.String(), dhcpReply.ClientHWAddr))
if replyMsgType == (byte)(layers.DHCPMsgTypeOffer) {
h.addStat(stats.OfferReceivedStat)
if h.options.Handshake {
buf := gopacket.NewSerializeBuffer()
outDhcpLayer.Xid = dhcpReply.Xid
outDhcpLayer.Options = make(layers.DHCPOptions, 4)
if h.options.DhcpDecline {
outDhcpLayer.Options[0] = layers.NewDHCPOption(layers.DHCPOptMessageType, []byte{byte(layers.DHCPMsgTypeDecline)})
} else {
outDhcpLayer.Options[0] = layers.NewDHCPOption(layers.DHCPOptMessageType, []byte{byte(layers.DHCPMsgTypeRequest)})
}
outDhcpLayer.Options[1] = layers.NewDHCPOption(layers.DHCPOptRequestIP, dhcpReply.YourClientIP)
outDhcpLayer.Options[2] = layers.NewDHCPOption(layers.DHCPOptServerID, replyOptions[layers.DHCPOptServerID].Data)
outDhcpLayer.Options[3] = layers.NewDHCPOption(layers.DHCPOptEnd, []byte{})
outDhcpLayer.ClientHWAddr = dhcpReply.ClientHWAddr
udpLayer.SetNetworkLayerForChecksum(ipLayer)
gopacket.SerializeLayers(buf, goPacketSerializeOpts,
ethernetLayer,
ipLayer,
udpLayer,
outDhcpLayer,
)
if h.sendPayload(buf.Bytes()) {
if h.options.DhcpDecline {
h.addStat(stats.DeclineSentStat)
} else {
h.addStat(stats.RequestSentStat)
}
}
}
} else if replyMsgType == (byte)(layers.DHCPMsgTypeAck) {
h.addStat(stats.AckReceivedStat)
if h.options.Arp || h.options.Bind {
ipStr := dhcpReply.YourClientIP.String()
if _, found := h.acquiredIPs[ipStr]; !found {
h.acquiredIPs[ipStr] = &LeaseDhcpV4{
Packet: msg.Packet,
Acquired: time.Now(),
HwAddr: dhcpReply.ClientHWAddr,
}
if h.options.Bind {
// Need to fix the CIDR here...
if addr, err := netlink.ParseAddr(ipStr + "/32"); err != nil {
h.addError(err)
} else if err = netlink.AddrAdd(h.link, addr); err != nil {
h.addError(err)
} else {
h.acquiredIPs[ipStr].LinkAddr = addr
}
}
}
}
if h.options.DhcpRelease || h.options.DhcpInfo {
buf := gopacket.NewSerializeBuffer()
outDhcpLayer.Xid = dhcpReply.Xid
/* We have to unicast DHCPRELEASE - https://tools.ietf.org/html/rfc2131#section-4.4.4 */
dhcpReplyEtherFrame := msg.Packet.Layer(layers.LayerTypeEthernet).(*layers.Ethernet)
/*
The "next server" value of the DHCP reply might not actually be the server issuing the IP.
Not seeing another sure option for grabbing the DHCP server IP aside from yanking it out of the IP header.
*/
dhcpReplyIpHeader := msg.Packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
releaseEthernetLayer := &layers.Ethernet{
DstMAC: dhcpReplyEtherFrame.SrcMAC,
SrcMAC: h.iface.HardwareAddr,
EthernetType: layers.EthernetTypeIPv4,
Length: 0,
}
releaseIpLayer := &layers.IPv4{
Version: 4, // IPv4
TTL: 64,
Protocol: 17, // UDP
SrcIP: dhcpReply.YourClientIP,
DstIP: dhcpReplyIpHeader.SrcIP,
}
previousClientIP := outDhcpLayer.ClientIP
outDhcpLayer.ClientIP = dhcpReply.YourClientIP
outDhcpLayer.Options = make(layers.DHCPOptions, 2)
previousFlags := outDhcpLayer.Flags
outDhcpLayer.Flags = 0x0
if h.options.DhcpInfo {
outDhcpLayer.Options[0] = layers.NewDHCPOption(layers.DHCPOptMessageType, []byte{byte(layers.DHCPMsgTypeInform)})
} else {
outDhcpLayer.Options[0] = layers.NewDHCPOption(layers.DHCPOptMessageType, []byte{byte(layers.DHCPMsgTypeRelease)})
}
outDhcpLayer.Options[1] = layers.NewDHCPOption(layers.DHCPOptEnd, []byte{})
outDhcpLayer.ClientHWAddr = dhcpReply.ClientHWAddr
udpLayer.SetNetworkLayerForChecksum(ipLayer)
gopacket.SerializeLayers(buf, goPacketSerializeOpts,
releaseEthernetLayer,
releaseIpLayer,
udpLayer,
outDhcpLayer,
)
// Reset ClientIP to what it was. It might have been an IP or it might have been 0.0.0.0, depending what options were used.
outDhcpLayer.ClientIP = previousClientIP
// Similarly for flags.
outDhcpLayer.Flags = previousFlags
if h.sendPayload(buf.Bytes()) {
if h.options.DhcpInfo {
h.addStat(stats.InfoSentStat)
} else {
h.addStat(stats.ReleaseSentStat)
}
}
}
} else if dhcpReply.Options[0].Data[0] == (byte)(layers.DHCPMsgTypeNak) {
h.addStat(stats.NakReceivedStat)
}
}
h.doneChannel <- struct{}{}
}
func (h *HandlerDhcpV4) handleARP(msg message.Message) {
arpRequest := msg.Packet.Layer(layers.LayerTypeARP).(*layers.ARP)
if arpRequest.Operation == layers.ARPRequest {
if lease, found := h.acquiredIPs[net.IP(arpRequest.DstProtAddress).String()]; found {
goPacketSerializeOpts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
ethernetLayer := &layers.Ethernet{
DstMAC: net.HardwareAddr(arpRequest.SourceHwAddress),
SrcMAC: h.iface.HardwareAddr,
EthernetType: layers.EthernetTypeARP,
Length: 0,
}
arpLayer := &layers.ARP{
Operation: layers.ARPReply,
DstHwAddress: arpRequest.SourceHwAddress,
DstProtAddress: arpRequest.SourceProtAddress,
HwAddressSize: arpRequest.HwAddressSize,
AddrType: arpRequest.AddrType,
ProtAddressSize: arpRequest.ProtAddressSize,
Protocol: arpRequest.Protocol,
SourceHwAddress: h.iface.HardwareAddr,
SourceProtAddress: arpRequest.DstProtAddress,
}
if h.options.ArpFakeMAC {
arpLayer.SourceHwAddress = lease.HwAddr
}
buf := gopacket.NewSerializeBuffer()
gopacket.SerializeLayers(buf, goPacketSerializeOpts,
ethernetLayer,
arpLayer,
)
if h.sendPayload(buf.Bytes()) {
h.addStat(stats.ArpReplySentStat)
}
}
}
}