forked from danderson/pixiecore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdhcp.go
More file actions
226 lines (197 loc) · 4.99 KB
/
dhcp.go
File metadata and controls
226 lines (197 loc) · 4.99 KB
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
package main
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
"golang.org/x/net/ipv4"
)
var dhcpMagic = []byte{99, 130, 83, 99}
type DHCPPacket struct {
TID []byte
MAC net.HardwareAddr
GUID []byte
ServerIP net.IP
}
func ServeProxyDHCP(port int, booter Booter) error {
conn, err := net.ListenPacket("udp4", fmt.Sprintf(":%d", port))
if err != nil {
return err
}
defer conn.Close()
l := ipv4.NewPacketConn(conn)
if err = l.SetControlMessage(ipv4.FlagInterface, true); err != nil {
return err
}
Log("ProxyDHCP", "Listening on port %d", port)
buf := make([]byte, 1024)
for {
n, msg, addr, err := l.ReadFrom(buf)
if err != nil {
Log("ProxyDHCP", "Error reading from socket: %s", err)
continue
}
udpAddr := addr.(*net.UDPAddr)
udpAddr.IP = net.IPv4bcast
req, err := ParseDHCP(buf[:n])
if err != nil {
Debug("ProxyDHCP", "ParseDHCP: %s", err)
continue
}
if err = booter.ShouldBoot(req.MAC); err != nil {
Debug("ProxyDHCP", "Not offering to boot %s: %s", req.MAC, err)
continue
}
req.ServerIP, err = interfaceIP(msg.IfIndex)
if err != nil {
Log("ProxyDHCP", "Couldn't find an IP address to use to reply to %s: %s", req.MAC, err)
continue
}
Log("ProxyDHCP", "Offering to boot %s (via %s)", req.MAC, req.ServerIP)
if _, err := l.WriteTo(OfferDHCP(req), &ipv4.ControlMessage{
IfIndex: msg.IfIndex,
}, udpAddr); err != nil {
Log("ProxyDHCP", "Responding to %s: %s", req.MAC, err)
continue
}
}
}
func OfferDHCP(p *DHCPPacket) []byte {
var b bytes.Buffer
// Fixed length BOOTP response
var bootp [236]byte
bootp[0] = 2 // BOOTP reply
bootp[1] = 1 // PHY = ethernet
bootp[2] = 6 // Hardware address length
bootp[10] = 0x80 // Please speak broadcast
copy(bootp[4:], p.TID)
copy(bootp[28:], p.MAC)
b.Write(bootp[:])
// DHCP magic
b.Write(dhcpMagic)
// Type = DHCPOFFER
b.Write([]byte{53, 1, 2})
// Server ID
b.Write([]byte{54, 4})
b.Write(p.ServerIP)
// Vendor class
b.Write([]byte{60, 9})
b.WriteString("PXEClient")
// Client UUID
b.Write([]byte{97, 17, 0})
b.Write(p.GUID)
// PXE vendor options
var pxe bytes.Buffer
// Discovery Control - disable broadcast and multicast boot server discovery
pxe.Write([]byte{6, 1, 3})
// PXE boot server
pxe.Write([]byte{8, 7, 0x80, 0x00, 1})
pxe.Write(p.ServerIP)
// PXE boot menu - one entry, pointing to the above PXE boot server
pxe.Write([]byte{9, 12, 0x80, 0x00, 9})
pxe.WriteString("Pixiecore")
// PXE menu prompt+timeout
pxe.Write([]byte{10, 10, 0})
pxe.WriteString("Pixiecore")
// End vendor options
pxe.WriteByte(255)
b.Write([]byte{43, byte(pxe.Len())})
pxe.WriteTo(&b)
// End DHCP options
b.WriteByte(255)
return b.Bytes()
}
func ParseDHCP(b []byte) (req *DHCPPacket, err error) {
if len(b) < 240 {
return nil, errors.New("packet too short")
}
ret := &DHCPPacket{
TID: b[4:8],
MAC: net.HardwareAddr(b[28:34]),
}
// BOOTP operation type
if b[0] != 1 {
return nil, fmt.Errorf("packet from %s is not a BOOTP request", ret.MAC)
}
if b[1] != 1 && b[2] != 6 {
return nil, fmt.Errorf("packet from %s is not for an Ethernet PHY", ret.MAC)
}
if !bytes.Equal(b[236:240], dhcpMagic) {
return nil, fmt.Errorf("packet from %s is not a DHCP request", ret.MAC)
}
typ, val, opts := dhcpOption(b[240:])
for typ != 255 {
switch typ {
case 53:
if len(val) != 1 {
return nil, fmt.Errorf("packet from %s has malformed option 53", ret.MAC)
}
if val[0] != 1 {
return nil, fmt.Errorf("packet from %s is not a DHCPDISCOVER", ret.MAC)
}
case 93:
if len(val) != 2 {
return nil, fmt.Errorf("packet from %s has malformed option 93", ret.MAC)
}
if binary.BigEndian.Uint16(val) != 0 {
return nil, fmt.Errorf("%s is not an x86 PXE client", ret.MAC)
}
case 97:
if len(val) != 17 || val[0] != 0 {
return nil, fmt.Errorf("packet from %s has malformed option 97", ret.MAC)
}
ret.GUID = val[1:]
}
typ, val, opts = dhcpOption(opts)
}
if ret.GUID == nil {
return nil, fmt.Errorf("%s is not a PXE client", ret.MAC)
}
// Valid PXE request!
return ret, nil
}
func dhcpOption(b []byte) (typ byte, val []byte, next []byte) {
if len(b) < 2 || b[0] == 255 {
return 255, nil, nil
}
typ, l := b[0], int(b[1])
if len(b) < l+2 {
return 255, nil, nil
}
return typ, b[2 : 2+l], b[2+l:]
}
func interfaceIP(ifIdx int) (net.IP, error) {
iface, err := net.InterfaceByIndex(ifIdx)
if err != nil {
return nil, err
}
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
// Try to find an IPv4 address to use, in the following order:
// global unicast (includes rfc1918), link-local unicast,
// loopback.
fs := [](func(net.IP) bool){
net.IP.IsGlobalUnicast,
net.IP.IsLinkLocalUnicast,
net.IP.IsLoopback,
}
for _, f := range fs {
for _, a := range addrs {
ipaddr, ok := a.(*net.IPNet)
if !ok {
continue
}
ip := ipaddr.IP.To4()
if ip == nil {
continue
}
if f(ip) {
return ip, nil
}
}
}
return nil, fmt.Errorf("interface %s has no usable unicast addresses", iface.Name)
}