-
Notifications
You must be signed in to change notification settings - Fork 1
/
dhcpv4.go
92 lines (75 loc) · 2.5 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
package main
import (
"net"
"strconv"
"github.com/gopacket/gopacket/layers"
"golang.org/x/sys/unix"
"code.local/dhcp-relay/gpckt/dhcp"
)
type HandleOptions struct {
PacketConn net.PacketConn
DHCPServerAddress string
ReplyTTL uint8
}
func HandleDHCPv4(
cfg *HandleOptions,
sall *unix.SockaddrLinklayer,
layerEthernet *layers.Ethernet,
layerIPv4 *layers.IPv4,
layerUDP *layers.UDP,
layerDHCPv4 *layers.DHCPv4,
) {
dhcpMessageType := dhcp.GetMessageType(layerDHCPv4)
if dhcpMessageType == "" {
cl.Debugf("Discarding DHCPv4-%s relayed message: invalid type\n",
layerDHCPv4.Operation)
return
}
layerDHCPv4.Options = dhcp.DeleteSplitOptions(layerDHCPv4.Options...)
funcDataInLog := func() {
cl.Infof("%s 0x%x: DHCP-%s [%d], IfIndex=%d, Src=%s(%s), Dst=%s(%s)\n",
logDataInPrefix, layerDHCPv4.Xid, dhcpMessageType, layerDHCPv4.Len(), sall.Ifindex,
net.JoinHostPort(layerIPv4.SrcIP.String(), strconv.Itoa(int(layerUDP.SrcPort))), layerEthernet.SrcMAC,
net.JoinHostPort(layerIPv4.DstIP.String(), strconv.Itoa(int(layerUDP.DstPort))), layerEthernet.DstMAC,
)
}
switch layerDHCPv4.Operation {
case layers.DHCPOpRequest:
funcDataInLog()
if layerDHCPv4.RelayHops > 0 {
cl.Debugf("Forwarding DHCPv4-%s relayed message: Xid=0x%x\n",
dhcpMessageType, layerDHCPv4)
if err := ForwardDHCPv4RelayedRequest(cfg, dhcpMessageType, layerDHCPv4); err != nil {
cl.Errorf("Error handling DHCPv4-%s relayed message: %v\n",
dhcpMessageType, err)
}
return
}
if err := HandleDHCPv4GenericRequest(cfg, sall.Ifindex, dhcpMessageType, layerDHCPv4); err != nil {
cl.Errorf("Error handling DHCPv4-%s relayed message: %v\n",
dhcpMessageType, err)
}
case layers.DHCPOpReply:
if layerDHCPv4.RelayHops != 1 {
cl.Debugf("Discarding DHCPv4-%s relayed message: unexpected hops count\n",
dhcpMessageType)
return
}
funcDataInLog()
bootFileName := dhcp.GetBootFileName(layerDHCPv4)
if bootFileName != "" {
cl.Debugf("Boot File Name: %s\n", bootFileName)
}
if dhcp.IsUnicast(layerDHCPv4) {
if err := HandleDHCPv4GenericReply(cfg, dhcpMessageType, layerDHCPv4, UnicastReply); err != nil {
cl.Errorf("Error handling DHCPv4-%s unicast relayed message: %v\n",
dhcpMessageType, err)
}
} else if dhcp.IsBroadcast(layerDHCPv4) {
if err := HandleDHCPv4GenericReply(cfg, dhcpMessageType, layerDHCPv4, BroadcastReply); err != nil {
cl.Errorf("Error handling DHCPv4-%s broadcast relayed message: %v\n",
dhcpMessageType, err)
}
}
}
}