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
|
// +build darwin
package bsdp
// Implements Apple's netboot protocol BSDP (Boot Service Discovery Protocol).
// Canonical implementation is defined here:
// http://opensource.apple.com/source/bootp/bootp-198.1/Documentation/BSDP.doc
import (
"errors"
"fmt"
"net"
"syscall"
"github.com/insomniacslk/dhcp/dhcpv4"
)
// MaxDHCPMessageSize is the size set in DHCP option 57 (DHCP Maximum Message Size).
// BSDP includes its own sub-option (12) to indicate to NetBoot servers that the
// client can support larger message sizes, and modern NetBoot servers will
// prefer this BSDP-specific option over the DHCP standard option.
const MaxDHCPMessageSize = 1500
// makeVendorClassIdentifier calls the sysctl syscall on macOS to get the
// platform model.
func makeVendorClassIdentifier() (string, error) {
// Fetch hardware model for class ID.
hwModel, err := syscall.Sysctl("hw.model")
if err != nil {
return "", err
}
return fmt.Sprintf("AAPLBSDPC/i386/%s", hwModel), nil
}
// ParseBootImageListFromAck parses the list of boot images presented in the
// ACK[LIST] packet and returns them as a list of BootImages.
func ParseBootImageListFromAck(ack dhcpv4.DHCPv4) ([]BootImage, error) {
var images []BootImage
for _, opt := range ack.Options() {
if opt.Code() == dhcpv4.OptionVendorSpecificInformation {
vendorOpt, err := ParseOptVendorSpecificInformation(opt.ToBytes())
if err != nil {
return nil, err
}
bootImageOpts := vendorOpt.GetOptions(OptionBootImageList)
for _, opt := range bootImageOpts {
images = append(images, opt.(*OptBootImageList).Images...)
}
}
}
return images, nil
}
func needsReplyPort(replyPort uint16) bool {
return replyPort != 0 && replyPort != dhcpv4.ClientPort
}
// NewInformListForInterface creates a new INFORM packet for interface ifname
// with configuration options specified by config.
func NewInformListForInterface(iface string, replyPort uint16) (*dhcpv4.DHCPv4, error) {
d, err := dhcpv4.NewInformForInterface(iface /* needsBroadcast = */, false)
if err != nil {
return nil, err
}
// Validate replyPort first
if needsReplyPort(replyPort) && replyPort >= 1024 {
return nil, errors.New("replyPort must be a privileged port")
}
// These are vendor-specific options used to pass along BSDP information.
vendorOpts := []dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
}
if needsReplyPort(replyPort) {
vendorOpts = append(vendorOpts, &OptReplyPort{replyPort})
}
d.AddOption(&OptVendorSpecificInformation{vendorOpts})
d.AddOption(&dhcpv4.OptParameterRequestList{
[]dhcpv4.OptionCode{
dhcpv4.OptionVendorSpecificInformation,
dhcpv4.OptionClassIdentifier,
},
})
d.AddOption(&dhcpv4.OptMaximumDHCPMessageSize{Size: MaxDHCPMessageSize})
vendorClassID, err := makeVendorClassIdentifier()
if err != nil {
return nil, err
}
d.AddOption(&dhcpv4.OptClassIdentifier{vendorClassID})
d.AddOption(&dhcpv4.OptionGeneric{OptionCode: dhcpv4.OptionEnd})
return d, nil
}
// InformSelectForAck constructs an INFORM[SELECT] packet given an ACK to the
// previously-sent INFORM[LIST] with Config config.
func InformSelectForAck(ack dhcpv4.DHCPv4, replyPort uint16, selectedImage BootImage) (*dhcpv4.DHCPv4, error) {
d, err := dhcpv4.New()
if err != nil {
return nil, err
}
if needsReplyPort(replyPort) && replyPort >= 1024 {
return nil, errors.New("replyPort must be a privileged port")
}
d.SetOpcode(dhcpv4.OpcodeBootRequest)
d.SetHwType(ack.HwType())
d.SetHwAddrLen(ack.HwAddrLen())
clientHwAddr := ack.ClientHwAddr()
d.SetClientHwAddr(clientHwAddr[:])
d.SetTransactionID(ack.TransactionID())
if ack.IsBroadcast() {
d.SetBroadcast()
} else {
d.SetUnicast()
}
// Data for OptionSelectedBootImageID
vendorOpts := []dhcpv4.Option{
&OptMessageType{MessageTypeSelect},
&OptVersion{Version1_1},
&OptSelectedBootImageID{selectedImage.ID},
}
// Find server IP address
var serverIP net.IP
// TODO replace this loop with `ack.GetOneOption(OptionBootImageList)`
for _, opt := range ack.Options() {
if opt.Code() == dhcpv4.OptionServerIdentifier {
serverIP = opt.(*dhcpv4.OptServerIdentifier).ServerID
}
}
if serverIP.To4() == nil {
return nil, fmt.Errorf("could not parse server identifier from ACK")
}
vendorOpts = append(vendorOpts, &OptServerIdentifier{serverIP})
// Validate replyPort if requested.
if needsReplyPort(replyPort) {
vendorOpts = append(vendorOpts, &OptReplyPort{replyPort})
}
vendorClassID, err := makeVendorClassIdentifier()
if err != nil {
return nil, err
}
d.AddOption(&dhcpv4.OptClassIdentifier{vendorClassID})
d.AddOption(&dhcpv4.OptParameterRequestList{
[]dhcpv4.OptionCode{
dhcpv4.OptionSubnetMask,
dhcpv4.OptionRouter,
dhcpv4.OptionBootfileName,
dhcpv4.OptionVendorSpecificInformation,
dhcpv4.OptionClassIdentifier,
},
})
d.AddOption(&dhcpv4.OptMessageType{dhcpv4.MessageTypeInform})
d.AddOption(&OptVendorSpecificInformation{vendorOpts})
d.AddOption(&dhcpv4.OptionGeneric{OptionCode: dhcpv4.OptionEnd})
return d, nil
}
|