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
|
package dhcpv6
import (
"bytes"
"fmt"
"net"
"github.com/insomniacslk/dhcp/iana"
"github.com/u-root/uio/uio"
)
// DUID is the interface that all DUIDs adhere to.
type DUID interface {
fmt.Stringer
ToBytes() []byte
FromBytes(p []byte) error
DUIDType() DUIDType
Equal(d DUID) bool
}
// DUIDLLT is a DUID based on link-layer address plus time (RFC 8415 Section 11.2).
type DUIDLLT struct {
HWType iana.HWType
Time uint32
LinkLayerAddr net.HardwareAddr
}
// String pretty-prints DUIDLLT information.
func (d DUIDLLT) String() string {
return fmt.Sprintf("DUID-LLT{HWType=%s HWAddr=%s Time=%d}", d.HWType, d.LinkLayerAddr, d.Time)
}
// DUIDType returns the DUID_LLT type.
func (d DUIDLLT) DUIDType() DUIDType {
return DUID_LLT
}
// ToBytes serializes the option out to bytes.
func (d DUIDLLT) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
buf.Write16(uint16(d.DUIDType()))
buf.Write16(uint16(d.HWType))
buf.Write32(d.Time)
buf.WriteBytes(d.LinkLayerAddr)
return buf.Data()
}
// FromBytes reads the option.
func (d *DUIDLLT) FromBytes(p []byte) error {
buf := uio.NewBigEndianBuffer(p)
d.HWType = iana.HWType(buf.Read16())
d.Time = buf.Read32()
d.LinkLayerAddr = buf.ReadAll()
return buf.FinError()
}
// Equal returns true if e is a DUID-LLT with the same values as d.
func (d *DUIDLLT) Equal(e DUID) bool {
ellt, ok := e.(*DUIDLLT)
if !ok {
return false
}
if d == nil {
return d == ellt
}
return d.HWType == ellt.HWType && d.Time == ellt.Time && bytes.Equal(d.LinkLayerAddr, ellt.LinkLayerAddr)
}
// DUIDLL is a DUID based on link-layer (RFC 8415 Section 11.4).
type DUIDLL struct {
HWType iana.HWType
LinkLayerAddr net.HardwareAddr
}
// String pretty-prints DUIDLL information.
func (d DUIDLL) String() string {
return fmt.Sprintf("DUID-LL{HWType=%s HWAddr=%s}", d.HWType, d.LinkLayerAddr)
}
// DUIDType returns the DUID_LL type.
func (d DUIDLL) DUIDType() DUIDType {
return DUID_LL
}
// ToBytes serializes the option out to bytes.
func (d DUIDLL) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
buf.Write16(uint16(d.DUIDType()))
buf.Write16(uint16(d.HWType))
buf.WriteBytes(d.LinkLayerAddr)
return buf.Data()
}
// FromBytes reads the option.
func (d *DUIDLL) FromBytes(p []byte) error {
buf := uio.NewBigEndianBuffer(p)
d.HWType = iana.HWType(buf.Read16())
d.LinkLayerAddr = buf.ReadAll()
return buf.FinError()
}
// Equal returns true if e is a DUID-LL with the same values as d.
func (d *DUIDLL) Equal(e DUID) bool {
ell, ok := e.(*DUIDLL)
if !ok {
return false
}
if d == nil {
return d == ell
}
return d.HWType == ell.HWType && bytes.Equal(d.LinkLayerAddr, ell.LinkLayerAddr)
}
// DUIDEN is a DUID based on enterprise number (RFC 8415 Section 11.3).
type DUIDEN struct {
EnterpriseNumber uint32
EnterpriseIdentifier []byte
}
// String pretty-prints DUIDEN information.
func (d DUIDEN) String() string {
return fmt.Sprintf("DUID-EN{EnterpriseNumber=%d EnterpriseIdentifier=%s}", d.EnterpriseNumber, d.EnterpriseIdentifier)
}
// DUIDType returns the DUID_EN type.
func (d DUIDEN) DUIDType() DUIDType {
return DUID_EN
}
// ToBytes serializes the option out to bytes.
func (d DUIDEN) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
buf.Write16(uint16(d.DUIDType()))
buf.Write32(d.EnterpriseNumber)
buf.WriteBytes(d.EnterpriseIdentifier)
return buf.Data()
}
// FromBytes reads the option.
func (d *DUIDEN) FromBytes(p []byte) error {
buf := uio.NewBigEndianBuffer(p)
d.EnterpriseNumber = buf.Read32()
d.EnterpriseIdentifier = buf.ReadAll()
return buf.FinError()
}
// Equal returns true if e is a DUID-EN with the same values as d.
func (d *DUIDEN) Equal(e DUID) bool {
en, ok := e.(*DUIDEN)
if !ok {
return false
}
if d == nil {
return d == en
}
return d.EnterpriseNumber == en.EnterpriseNumber && bytes.Equal(d.EnterpriseIdentifier, en.EnterpriseIdentifier)
}
// DUIDUUID is a DUID based on UUID (RFC 8415 Section 11.5).
type DUIDUUID struct {
// Defined by RFC 6355.
UUID [16]byte
}
// String pretty-prints DUIDUUID information.
func (d DUIDUUID) String() string {
return fmt.Sprintf("DUID-UUID{%#x}", d.UUID[:])
}
// DUIDType returns the DUID_UUID type.
func (d DUIDUUID) DUIDType() DUIDType {
return DUID_UUID
}
// ToBytes serializes the option out to bytes.
func (d DUIDUUID) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
buf.Write16(uint16(d.DUIDType()))
buf.WriteData(d.UUID[:])
return buf.Data()
}
// FromBytes reads the option.
func (d *DUIDUUID) FromBytes(p []byte) error {
if len(p) != 16 {
return fmt.Errorf("buffer is length %d, DUID-UUID must be exactly 16 bytes", len(p))
}
copy(d.UUID[:], p)
return nil
}
// Equal returns true if e is a DUID-UUID with the same values as d.
func (d *DUIDUUID) Equal(e DUID) bool {
euuid, ok := e.(*DUIDUUID)
if !ok {
return false
}
if d == nil {
return d == euuid
}
return d.UUID == euuid.UUID
}
// DUIDOpaque is a DUID of unknown type.
type DUIDOpaque struct {
Type DUIDType
Data []byte
}
// String pretty-prints opaque DUID information.
func (d DUIDOpaque) String() string {
return fmt.Sprintf("DUID-Opaque{Type=%d Data=%#x}", d.Type, d.Data)
}
// DUIDType returns the opaque DUID type.
func (d DUIDOpaque) DUIDType() DUIDType {
return d.Type
}
// ToBytes serializes the option out to bytes.
func (d DUIDOpaque) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
buf.Write16(uint16(d.Type))
buf.WriteData(d.Data)
return buf.Data()
}
// FromBytes reads the option.
func (d *DUIDOpaque) FromBytes(p []byte) error {
d.Data = append([]byte(nil), p...)
return nil
}
// Equal returns true if e is an opaque DUID with the same values as d.
func (d *DUIDOpaque) Equal(e DUID) bool {
eopaque, ok := e.(*DUIDOpaque)
if !ok {
return false
}
if d == nil {
return d == eopaque
}
return d.Type == eopaque.Type && bytes.Equal(d.Data, eopaque.Data)
}
// DUIDType is the DUID type as defined in RFC 3315.
type DUIDType uint16
// DUID types
const (
DUID_LLT DUIDType = 1
DUID_EN DUIDType = 2
DUID_LL DUIDType = 3
DUID_UUID DUIDType = 4
)
// duidTypeToString maps a DUIDType to a name.
var duidTypeToString = map[DUIDType]string{
DUID_LL: "DUID-LL",
DUID_LLT: "DUID-LLT",
DUID_EN: "DUID-EN",
DUID_UUID: "DUID-UUID",
}
func (d DUIDType) String() string {
if dtype, ok := duidTypeToString[d]; ok {
return dtype
}
return "unknown"
}
// DUIDFromBytes parses a DUID from a byte slice.
func DUIDFromBytes(data []byte) (DUID, error) {
buf := uio.NewBigEndianBuffer(data)
if !buf.Has(2) {
return nil, fmt.Errorf("%w: have %d bytes, want 2 bytes", uio.ErrBufferTooShort, buf.Len())
}
typ := DUIDType(buf.Read16())
var d DUID
switch typ {
case DUID_LLT:
d = &DUIDLLT{}
case DUID_LL:
d = &DUIDLL{}
case DUID_EN:
d = &DUIDEN{}
case DUID_UUID:
d = &DUIDUUID{}
default:
d = &DUIDOpaque{Type: typ}
}
return d, d.FromBytes(buf.Data())
}
|