-
Notifications
You must be signed in to change notification settings - Fork 18
/
handler.go
273 lines (244 loc) · 7.36 KB
/
handler.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
// Package dhcpd contains snippets from MIT-licensed coredhcp project at
// https://github.com/coredhcp/coredhcp
package dhcpd
import (
"errors"
"net"
"runtime"
"strings"
"github.com/DSpeichert/netbootd/dhcpd/arp"
mfest "github.com/DSpeichert/netbootd/manifest"
"github.com/insomniacslk/dhcp/dhcpv4"
"golang.org/x/net/ipv4"
)
func (server *Server) HandleMsg4(buf []byte, oob *ipv4.ControlMessage, peer net.Addr) {
var (
resp *dhcpv4.DHCPv4
err error
bootFileSize int
manifest *mfest.Manifest
)
req, err := dhcpv4.FromBytes(buf)
if err != nil {
server.logger.Error().Err(err).Msg("Error parsing DHCPv4 request")
return
}
server.logger.Trace().
Str("peer", peer.String()).
Interface("request", req.Summary()).
Msg("Received DHCP packet")
if req.OpCode != dhcpv4.OpcodeBootRequest {
server.logger.Error().
Int("opcode", int(req.OpCode)).
Msg("unsupported opcode")
return
}
resp, err = dhcpv4.NewReplyFromRequest(req)
if err != nil {
server.logger.Error().
Err(err).
Msg("failed to build reply")
return
}
switch mt := req.MessageType(); mt {
case dhcpv4.MessageTypeDiscover:
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeOffer))
case dhcpv4.MessageTypeRequest:
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeAck))
case dhcpv4.MessageTypeRelease:
return
default:
server.logger.Error().
Str("type", mt.String()).
Msg("unknown message type")
return
}
// find local IP
ifIndex := server.Interface.Index
if ifIndex == 0 && oob != nil {
ifIndex = oob.IfIndex
}
localIp, err := getIpv4ForInterface(ifIndex)
if err != nil {
server.logger.Error().
Err(err).
Int("ifIndex", ifIndex).
Msg("failed to find local interface")
resp = nil
goto response
}
manifest = server.store.FindByMAC(req.ClientHWAddr)
if manifest == nil {
server.logger.Info().
Str("MAC", req.ClientHWAddr.String()).
Msg("ignore packet from unknown MAC")
resp = nil
goto response
}
// server ID
if req.ServerIPAddr != nil &&
!req.ServerIPAddr.Equal(net.IPv4zero) &&
!req.ServerIPAddr.Equal(localIp) {
server.logger.Trace().
Msg("requested server ID does not match this server's ID")
resp = nil
goto response
} else {
resp.ServerIPAddr = make(net.IP, net.IPv4len)
copy(resp.ServerIPAddr[:], localIp)
resp.UpdateOption(dhcpv4.OptServerIdentifier(localIp))
}
resp.YourIPAddr = manifest.IPv4.IP
resp.Options.Update(dhcpv4.OptSubnetMask(manifest.IPv4.Net.Mask))
// lease time
if req.OpCode == dhcpv4.OpcodeBootRequest && manifest.LeaseDuration != 0 {
resp.Options.Update(dhcpv4.OptIPAddressLeaseTime(manifest.LeaseDuration))
}
// hostname
if req.IsOptionRequested(dhcpv4.OptionHostName) {
resp.Options.Update(dhcpv4.OptHostName(manifest.Hostname))
}
// dns
if req.IsOptionRequested(dhcpv4.OptionDomainNameServer) {
resp.Options.Update(dhcpv4.OptDNS(manifest.DNS...))
}
// router
if req.IsOptionRequested(dhcpv4.OptionRouter) {
resp.Options.Update(dhcpv4.OptRouter(manifest.Router...))
}
// NTP
if req.IsOptionRequested(dhcpv4.OptionNTPServers) {
resp.Options.Update(dhcpv4.OptNTPServers(manifest.NTP...))
}
// NBP
if req.IsOptionRequested(dhcpv4.OptionTFTPServerName) && !manifest.Suspended {
resp.Options.Update(dhcpv4.OptTFTPServerName(localIp.String()))
}
if req.IsOptionRequested(dhcpv4.OptionBootfileName) && !manifest.Suspended {
// serve iPXE script if user-class is iPXE, or whatever the user chooses if iPXE is disabled
if stringSlicesEqual(req.UserClass(), []string{"iPXE"}) || !manifest.Ipxe {
resp.Options.Update(dhcpv4.OptBootFileName(manifest.BootFilename))
} else if len(req.ClientArch()) > 0 && req.ClientArch()[0] > 0 {
// likely UEFI (not BIOS)
if strings.Contains(req.ClassIdentifier(), "PXEClient:Arch:00011") {
resp.Options.Update(dhcpv4.OptBootFileName("ipxe_arm64.efi"))
} else {
resp.Options.Update(dhcpv4.OptBootFileName("ipxe.efi"))
}
//bootFileSize = 1
} else {
resp.Options.Update(dhcpv4.OptBootFileName("undionly.kpxe"))
//bootFileSize = 1
}
}
if req.IsOptionRequested(dhcpv4.OptionBootFileSize) && bootFileSize > 0 {
resp.Options.Update(dhcpv4.Option{
Code: dhcpv4.OptionBootFileSize,
Value: Uint8(bootFileSize),
})
}
// iPXE specific
if stringSlicesEqual(req.UserClass(), []string{"iPXE"}) {
resp.Options.Update(dhcpv4.Option{
Code: dhcpv4.GenericOptionCode(176), // ipxe.no-pxedhcp
Value: dhcpv4.Uint16(1), // should be uint8 according to iPXE docs
})
}
response:
// continue main handler
if resp != nil {
var peer *net.UDPAddr
if !req.GatewayIPAddr.IsUnspecified() {
// TODO: make RFC8357 compliant
peer = &net.UDPAddr{IP: req.GatewayIPAddr, Port: dhcpv4.ServerPort}
} else if resp.MessageType() == dhcpv4.MessageTypeNak {
peer = &net.UDPAddr{IP: net.IPv4bcast, Port: dhcpv4.ClientPort}
} else if !req.ClientIPAddr.IsUnspecified() {
peer = &net.UDPAddr{IP: req.ClientIPAddr, Port: dhcpv4.ClientPort}
} else if req.IsBroadcast() {
peer = &net.UDPAddr{IP: net.IPv4bcast, Port: dhcpv4.ClientPort}
} else {
// we must inject ARP to unicast to IP/MAC that's not on the network yet
device := server.Interface.Name
if device == "" && oob != nil && oob.IfIndex != 0 {
if netif, err := net.InterfaceByIndex(oob.IfIndex); err == nil {
device = netif.Name
}
}
rawConn, err := server.UdpConn.SyscallConn()
if device != "" && err == nil {
rawConn.Control(func(fd uintptr) {
err = arp.InjectArpFd(fd, resp.YourIPAddr, req.ClientHWAddr, arp.ATF_COM, device)
})
if err != nil && runtime.GOOS == "linux" {
server.logger.Error().
Err(err).
Msg("ioctl failed")
}
}
if device != "" && err == nil {
peer = &net.UDPAddr{IP: resp.YourIPAddr, Port: dhcpv4.ClientPort}
} else {
// fall back to broadcast
peer = &net.UDPAddr{IP: net.IPv4bcast, Port: dhcpv4.ClientPort}
}
}
var woob *ipv4.ControlMessage
if peer.IP.Equal(net.IPv4bcast) || peer.IP.IsLinkLocalUnicast() {
// Direct broadcasts and link-local to the interface the request was
// received on. Other packets should use the normal routing table in
// case of asymmetric routing.
switch {
case server.Interface.Index != 0:
woob = &ipv4.ControlMessage{IfIndex: server.Interface.Index}
case oob != nil && oob.IfIndex != 0:
woob = &ipv4.ControlMessage{IfIndex: oob.IfIndex}
default:
server.logger.Error().
Str("peer", peer.String()).
Msg("did not receive interface information")
}
}
server.logger.Debug().
Interface("response", resp).
Msg("sending DHCP packet")
if _, err := server.WriteTo(resp.ToBytes(), woob, peer); err != nil {
server.logger.Error().
Err(err).
Str("peer", peer.String()).
Msg("conn.Write failed")
}
} else {
server.logger.Trace().
Msg("dropping request because response is nil")
}
}
func stringSlicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func getIpv4ForInterface(i int) (net.IP, error) {
netif, err := net.InterfaceByIndex(i)
if err != nil {
return nil, err
}
addresses, err := netif.Addrs()
if err != nil {
return nil, err
}
for _, address := range addresses {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.To4(), nil
}
}
}
return nil, errors.New("no IP found")
}