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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
package dhcpv6
import (
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"log"
"net"
"time"
"github.com/insomniacslk/dhcp/iana"
)
const MessageHeaderSize = 4
type DHCPv6Message struct {
messageType MessageType
transactionID uint32 // only 24 bits are used though
options []Option
}
func BytesToTransactionID(data []byte) (*uint32, error) {
// return a uint32 from a sequence of bytes, representing a transaction ID.
// Transaction IDs are three-bytes long. If the provided data is shorter than
// 3 bytes, it return an error. If longer, will use the first three bytes
// only.
if len(data) < 3 {
return nil, fmt.Errorf("Invalid transaction ID: less than 3 bytes")
}
buf := make([]byte, 4)
copy(buf[1:4], data[:3])
tid := binary.BigEndian.Uint32(buf)
return &tid, nil
}
func GenerateTransactionID() (*uint32, error) {
var tid *uint32
for {
tidBytes := make([]byte, 4)
n, err := rand.Read(tidBytes)
if n != 4 {
return nil, fmt.Errorf("Invalid random sequence: shorter than 4 bytes")
}
tid, err = BytesToTransactionID(tidBytes)
if err != nil {
return nil, err
}
if tid == nil {
return nil, fmt.Errorf("Error: got a nil Transaction ID")
}
// retry until != 0
// TODO add retry limit
if *tid != 0 {
break
}
}
return tid, nil
}
// GetTime returns a time integer suitable for DUID-LLT, i.e. the current time counted
// in seconds since January 1st, 2000, midnight UTC, modulo 2^32
func GetTime() uint32 {
now := time.Since(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC))
return uint32((now.Nanoseconds() / 1000000000) % 0xffffffff)
}
// NewSolicitForInterface creates a new SOLICIT message with DUID-LLT, using the
// given network interface's hardware address and current time
func NewSolicitForInterface(ifname string, modifiers ...Modifier) (DHCPv6, error) {
d, err := NewMessage()
if err != nil {
return nil, err
}
d.(*DHCPv6Message).SetMessage(SOLICIT)
iface, err := net.InterfaceByName(ifname)
if err != nil {
return nil, err
}
cid := OptClientId{
Cid: Duid{
Type: DUID_LLT,
HwType: iana.HwTypeEthernet,
Time: GetTime(),
LinkLayerAddr: iface.HardwareAddr,
},
}
d.AddOption(&cid)
oro := OptRequestedOption{}
oro.SetRequestedOptions([]OptionCode{
DNS_RECURSIVE_NAME_SERVER,
DOMAIN_SEARCH_LIST,
})
d.AddOption(&oro)
d.AddOption(&OptElapsedTime{})
// FIXME use real values for IA_NA
iaNa := OptIANA{}
iaNa.SetIAID([4]byte{0xfa, 0xce, 0xb0, 0x0c})
iaNa.SetT1(0xe10)
iaNa.SetT2(0x1518)
d.AddOption(&iaNa)
// apply modifiers
for _, mod := range modifiers {
d = mod(d)
}
return d, nil
}
// NewAdvertiseFromSolicit creates a new ADVERTISE packet based on an SOLICIT packet.
func NewAdvertiseFromSolicit(solicit DHCPv6, modifiers ...Modifier) (DHCPv6, error) {
if solicit == nil {
return nil, errors.New("SOLICIT cannot be nil")
}
if solicit.Type() != SOLICIT {
return nil, errors.New("The passed SOLICIT must have SOLICIT type set")
}
sol, ok := solicit.(*DHCPv6Message)
if !ok {
return nil, errors.New("The passed SOLICIT must be of DHCPv6Message type")
}
// build ADVERTISE from SOLICIT
adv := DHCPv6Message{}
adv.SetMessage(ADVERTISE)
adv.SetTransactionID(sol.TransactionID())
// add Client ID
cid := sol.GetOneOption(OPTION_CLIENTID)
if cid == nil {
return nil, errors.New("Client ID cannot be nil in SOLICIT when building ADVERTISE")
}
adv.AddOption(cid)
// apply modifiers
d := DHCPv6(&adv)
for _, mod := range modifiers {
d = mod(d)
}
return d, nil
}
// NewRequestFromAdvertise creates a new REQUEST packet based on an ADVERTISE
// packet options.
func NewRequestFromAdvertise(advertise DHCPv6, modifiers ...Modifier) (DHCPv6, error) {
if advertise == nil {
return nil, fmt.Errorf("ADVERTISE cannot be nil")
}
if advertise.Type() != ADVERTISE {
return nil, fmt.Errorf("The passed ADVERTISE must have ADVERTISE type set")
}
adv, ok := advertise.(*DHCPv6Message)
if !ok {
return nil, fmt.Errorf("The passed ADVERTISE must be of DHCPv6Message type")
}
// build REQUEST from ADVERTISE
req := DHCPv6Message{}
req.SetMessage(REQUEST)
req.SetTransactionID(adv.TransactionID())
// add Client ID
cid := adv.GetOneOption(OPTION_CLIENTID)
if cid == nil {
return nil, fmt.Errorf("Client ID cannot be nil in ADVERTISE when building REQUEST")
}
req.AddOption(cid)
// add Server ID
sid := adv.GetOneOption(OPTION_SERVERID)
if sid == nil {
return nil, fmt.Errorf("Server ID cannot be nil in ADVERTISE when building REQUEST")
}
req.AddOption(sid)
// add Elapsed Time
req.AddOption(&OptElapsedTime{})
// add IA_NA
iaNa := adv.GetOneOption(OPTION_IA_NA)
if iaNa == nil {
return nil, fmt.Errorf("IA_NA cannot be nil in ADVERTISE when building REQUEST")
}
req.AddOption(iaNa)
// add OptRequestedOption
oro := OptRequestedOption{}
oro.SetRequestedOptions([]OptionCode{
DNS_RECURSIVE_NAME_SERVER,
DOMAIN_SEARCH_LIST,
})
req.AddOption(&oro)
// add OPTION_VENDOR_CLASS, only if present in the original request
// TODO implement OptionVendorClass
vClass := adv.GetOneOption(OPTION_VENDOR_CLASS)
if vClass != nil {
req.AddOption(vClass)
}
// apply modifiers
d := DHCPv6(&req)
for _, mod := range modifiers {
d = mod(d)
}
return d, nil
}
// NewReplyFromRequest creates a new REPLY packet based on a REQUEST packet.
func NewReplyFromRequest(request DHCPv6, modifiers ...Modifier) (DHCPv6, error) {
if request == nil {
return nil, errors.New("REQUEST cannot be nil")
}
if request.Type() != REQUEST {
return nil, errors.New("The passed REQUEST must have REQUEST type set")
}
req, ok := request.(*DHCPv6Message)
if !ok {
return nil, errors.New("The passed REQUEST must be of DHCPv6Message type")
}
// build REPLY from REQUEST
rep := DHCPv6Message{}
rep.SetMessage(REPLY)
rep.SetTransactionID(req.TransactionID())
// add Client ID
cid := req.GetOneOption(OPTION_CLIENTID)
if cid == nil {
return nil, errors.New("Client ID cannot be nil in REQUEST when building REPLY")
}
rep.AddOption(cid)
// add Server ID
sid := req.GetOneOption(OPTION_SERVERID)
if sid == nil {
return nil, errors.New("Server ID cannot be nil in REQUEST when building REPLY")
}
rep.AddOption(sid)
// apply modifiers
d := DHCPv6(&rep)
for _, mod := range modifiers {
d = mod(d)
}
return d, nil
}
func (d *DHCPv6Message) Type() MessageType {
return d.messageType
}
func (d *DHCPv6Message) SetMessage(messageType MessageType) {
msgString := MessageTypeToString(messageType)
if msgString == "" {
log.Printf("Warning: unknown DHCPv6 message type: %v", messageType)
}
if messageType == RELAY_FORW || messageType == RELAY_REPL {
log.Printf("Warning: using a RELAY message type with a non-relay message: %v (%v)",
msgString, messageType)
}
d.messageType = messageType
}
func (d *DHCPv6Message) MessageTypeToString() string {
return MessageTypeToString(d.messageType)
}
func (d *DHCPv6Message) TransactionID() uint32 {
return d.transactionID
}
func (d *DHCPv6Message) SetTransactionID(tid uint32) {
ttid := tid & 0x00ffffff
if ttid != tid {
log.Printf("Warning: truncating transaction ID that is longer than 24 bits: %v", tid)
}
d.transactionID = ttid
}
func (d *DHCPv6Message) SetOptions(options []Option) {
d.options = options
}
func (d *DHCPv6Message) AddOption(option Option) {
d.options = append(d.options, option)
}
func (d *DHCPv6Message) UpdateOption(option Option) {
for idx, opt := range d.options {
if opt.Code() == option.Code() {
d.options[idx] = option
// don't look further
break
}
}
}
func (d *DHCPv6Message) String() string {
return fmt.Sprintf("DHCPv6Message(messageType=%v transactionID=0x%06x, %d options)",
d.MessageTypeToString(), d.TransactionID(), len(d.options),
)
}
func (d *DHCPv6Message) Summary() string {
ret := fmt.Sprintf(
"DHCPv6Message\n"+
" messageType=%v\n"+
" transactionid=0x%06x\n",
d.MessageTypeToString(),
d.TransactionID(),
)
ret += " options=["
if len(d.options) > 0 {
ret += "\n"
}
for _, opt := range d.options {
ret += fmt.Sprintf(" %v\n", opt.String())
}
ret += " ]\n"
return ret
}
// Convert a DHCPv6Message structure into its binary representation, suitable for being
// sent over the network
func (d *DHCPv6Message) ToBytes() []byte {
var ret []byte
ret = append(ret, byte(d.messageType))
tidBytes := make([]byte, 4)
binary.BigEndian.PutUint32(tidBytes, d.transactionID)
ret = append(ret, tidBytes[1:4]...) // discard the first byte
for _, opt := range d.options {
ret = append(ret, opt.ToBytes()...)
}
return ret
}
func (d *DHCPv6Message) Length() int {
mLen := 4
for _, opt := range d.options {
mLen += opt.Length() + 4 // +4 for opt code and opt len
}
return mLen
}
func (d *DHCPv6Message) Options() []Option {
return d.options
}
func (d *DHCPv6Message) GetOption(code OptionCode) []Option {
return getOptions(d.options, code, false)
}
func (d *DHCPv6Message) GetOneOption(code OptionCode) Option {
return getOption(d.options, code)
}
func (d *DHCPv6Message) IsRelay() bool {
return false
}
|