-
Notifications
You must be signed in to change notification settings - Fork 1
/
dhcpv4-reply.go
149 lines (121 loc) · 3.6 KB
/
dhcpv4-reply.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
package main
import (
"fmt"
"net"
"strconv"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
"golang.org/x/sys/unix"
"code.local/dhcp-relay/gpckt/dhcp"
"code.local/dhcp-relay/sockets"
"code.local/dhcp-relay/specs"
)
const (
UnicastReply uint8 = 0
BroadcastReply uint8 = 1
)
func HandleDHCPv4GenericReply(
cfg *HandleOptions,
dhcpMessageType string,
layerDHCPv4 *layers.DHCPv4,
replyType uint8,
) error {
srcIP := layerDHCPv4.RelayAgentIP.To4()
if srcIP == nil || srcIP.IsLoopback() || srcIP.Equal(net.IPv4zero) || srcIP.Equal(net.IPv4bcast) {
return fmt.Errorf("invalid Relay Agent address value")
}
opt82 := dhcp.GetRelayAgentInformationOption(layerDHCPv4)
if !dhcp.IsOption(opt82) {
return fmt.Errorf("no Relay Agent Information")
}
subOpts := dhcp.DecodeRelayAgentInformationOption(opt82)
if len(subOpts) == 0 {
return fmt.Errorf("no Relay Agent Information")
}
for _, el := range subOpts {
cl.Debugf("Option 82 -> Sub-option: Type=%d, Len=%d, Data=[% x], ASCII=%s",
el.Type, el.Length, el.Data, strconv.QuoteToASCII(string(el.Data)))
}
ifIndex := dhcp.ExtractAgentCircuitIDSubOptionData(subOpts...)
if ifIndex == 0 {
return fmt.Errorf("invalid Agent Circuit ID sub-option value")
}
ifi, err := net.InterfaceByIndex(ifIndex)
if err != nil {
return fmt.Errorf("invalid interface data in Agent Circuit ID for IfIndex=%d: %w", ifIndex, err)
}
layerEthernet := &layers.Ethernet{
SrcMAC: ifi.HardwareAddr,
DstMAC: layerDHCPv4.ClientHWAddr,
EthernetType: layers.EthernetTypeIPv4,
}
layerIPv4 := &layers.IPv4{
Version: specs.IPv4Version,
Id: GenerateRandomIPv4ID(),
Flags: layers.IPv4DontFragment,
TTL: cfg.ReplyTTL,
Protocol: layers.IPProtocolUDP,
}
layerUDP := &layers.UDP{
SrcPort: specs.DHCPv4ServerPort,
DstPort: specs.DHCPv4ClientPort,
}
err = layerUDP.SetNetworkLayerForChecksum(layerIPv4)
if err != nil {
return fmt.Errorf("layer crafting error: %w", err)
}
if layerDHCPv4.RelayHops > 0 {
layerDHCPv4.RelayHops--
}
if layerDHCPv4.RelayHops == 0 {
dhcp.DeleteRelayAgentInformationOption(layerDHCPv4)
layerDHCPv4.RelayAgentIP = nil
}
if replyType == UnicastReply {
layerIPv4.SrcIP = srcIP.To4()
layerIPv4.DstIP = layerDHCPv4.YourClientIP.To4()
dhcp.SetUnicast(layerDHCPv4)
} else if replyType == BroadcastReply {
layerIPv4.SrcIP = net.IPv4zero
layerIPv4.DstIP = net.IPv4bcast
dhcp.SetBroadcast(layerDHCPv4)
}
buffer := gopacket.NewSerializeBuffer()
err = gopacket.SerializeLayers(
buffer, gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
},
layerEthernet, layerIPv4, layerUDP, layerDHCPv4,
)
if err != nil {
return fmt.Errorf("layer encoding error: %w", err)
}
rs := new(sockets.Raw)
err = rs.Create(sockets.Htons(unix.ETH_P_ALL))
if err != nil {
return fmt.Errorf("socket create error: %w", err)
}
defer rs.Close()
err = rs.AttachBPF([]unix.SockFilter{
{Code: unix.BPF_RET | unix.BPF_K, K: 0}, // filter ALL
})
if err != nil {
return fmt.Errorf("socket attach BPF error: %w", err)
}
n, err := rs.Send(ifi.Index, ifi.HardwareAddr, sockets.Htons(unix.ETH_P_ALL), buffer.Bytes())
if err != nil {
return fmt.Errorf("socket write error: %w", err)
}
cl.Debugf("Sent %d bytes of data to socket\n", n)
cl.Infof("%s 0x%x: DHCP-%s [%d], IfIndex=%d, Src=%s, Dst=%s\n",
logDataOutPrefix, layerDHCPv4.Xid, dhcpMessageType, layerDHCPv4.Len(), ifIndex,
net.JoinHostPort(
layerIPv4.SrcIP.String(), strconv.Itoa(specs.DHCPv4ServerPort),
),
net.JoinHostPort(
layerIPv4.DstIP.String(), strconv.Itoa(specs.DHCPv4ClientPort),
),
)
return nil
}