forked from mdlayher/ndp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
425 lines (339 loc) · 9.44 KB
/
message.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package ndp
import (
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv6"
)
const (
// Length of an ICMPv6 header.
icmpLen = 4
// Minimum byte length values for each type of valid Message.
naLen = 20
nsLen = 20
raLen = 12
rsLen = 4
)
// A Message is a Neighbor Discovery Protocol message.
type Message interface {
// Type specifies the ICMPv6 type for a Message.
Type() ipv6.ICMPType
// Called via MarshalMessage and ParseMessage.
marshal() ([]byte, error)
unmarshal(b []byte) error
}
func marshalMessage(m Message, psh []byte) ([]byte, error) {
mb, err := m.marshal()
if err != nil {
return nil, err
}
im := icmp.Message{
Type: m.Type(),
// Always zero.
Code: 0,
// Calculated by caller or OS.
Checksum: 0,
Body: &icmp.RawBody{
Data: mb,
},
}
return im.Marshal(psh)
}
// MarshalMessage marshals a Message into its binary form and prepends an
// ICMPv6 message with the correct type.
//
// It is assumed that the operating system or caller will calculate and place
// the ICMPv6 checksum in the result.
func MarshalMessage(m Message) ([]byte, error) {
// Pseudo-header always nil so checksum is calculated by caller or OS.
return marshalMessage(m, nil)
}
// MarshalMessageChecksum marshals a Message into its binary form and prepends
// an ICMPv6 message with the correct type.
//
// The source and destination IP addresses are used to compute an IPv6 pseudo
// header for checksum calculation.
func MarshalMessageChecksum(m Message, source, destination net.IP) ([]byte, error) {
return marshalMessage(m, icmp.IPv6PseudoHeader(source, destination))
}
// errParseMessage is a sentinel which indicates an error from ParseMessage.
var errParseMessage = errors.New("failed to parse message")
// ParseMessage parses a Message from its binary form after determining its
// type from a leading ICMPv6 message.
func ParseMessage(b []byte) (Message, error) {
if len(b) < icmpLen {
return nil, fmt.Errorf("ndp: ICMPv6 message too short: %w", errParseMessage)
}
// TODO(mdlayher): verify checksum?
var m Message
t := ipv6.ICMPType(b[0])
switch t {
case ipv6.ICMPTypeNeighborAdvertisement:
m = new(NeighborAdvertisement)
case ipv6.ICMPTypeNeighborSolicitation:
m = new(NeighborSolicitation)
case ipv6.ICMPTypeRouterAdvertisement:
m = new(RouterAdvertisement)
case ipv6.ICMPTypeRouterSolicitation:
m = new(RouterSolicitation)
default:
return nil, fmt.Errorf("ndp: unrecognized ICMPv6 type %d: %w", t, errParseMessage)
}
if err := m.unmarshal(b[icmpLen:]); err != nil {
return nil, fmt.Errorf("ndp: failed to unmarshal %s: %w", t, errParseMessage)
}
return m, nil
}
var _ Message = &NeighborAdvertisement{}
// A NeighborAdvertisement is a Neighbor Advertisement message as
// described in RFC 4861, Section 4.4.
type NeighborAdvertisement struct {
Router bool
Solicited bool
Override bool
TargetAddress net.IP
Options []Option
}
// Type implements Message.
func (na *NeighborAdvertisement) Type() ipv6.ICMPType { return ipv6.ICMPTypeNeighborAdvertisement }
func (na *NeighborAdvertisement) marshal() ([]byte, error) {
if err := checkIPv6(na.TargetAddress); err != nil {
return nil, err
}
b := make([]byte, naLen)
if na.Router {
b[0] |= (1 << 7)
}
if na.Solicited {
b[0] |= (1 << 6)
}
if na.Override {
b[0] |= (1 << 5)
}
copy(b[4:], na.TargetAddress)
ob, err := marshalOptions(na.Options)
if err != nil {
return nil, err
}
b = append(b, ob...)
return b, nil
}
func (na *NeighborAdvertisement) unmarshal(b []byte) error {
if len(b) < naLen {
return io.ErrUnexpectedEOF
}
// Skip flags and reserved area.
addr := b[4:naLen]
if err := checkIPv6(addr); err != nil {
return err
}
options, err := parseOptions(b[naLen:])
if err != nil {
return err
}
*na = NeighborAdvertisement{
Router: (b[0] & 0x80) != 0,
Solicited: (b[0] & 0x40) != 0,
Override: (b[0] & 0x20) != 0,
TargetAddress: make(net.IP, net.IPv6len),
Options: options,
}
copy(na.TargetAddress, addr)
return nil
}
var _ Message = &NeighborSolicitation{}
// A NeighborSolicitation is a Neighbor Solicitation message as
// described in RFC 4861, Section 4.3.
type NeighborSolicitation struct {
TargetAddress net.IP
Options []Option
}
// Type implements Message.
func (ns *NeighborSolicitation) Type() ipv6.ICMPType { return ipv6.ICMPTypeNeighborSolicitation }
func (ns *NeighborSolicitation) marshal() ([]byte, error) {
if err := checkIPv6(ns.TargetAddress); err != nil {
return nil, err
}
b := make([]byte, nsLen)
copy(b[4:], ns.TargetAddress)
ob, err := marshalOptions(ns.Options)
if err != nil {
return nil, err
}
b = append(b, ob...)
return b, nil
}
func (ns *NeighborSolicitation) unmarshal(b []byte) error {
if len(b) < nsLen {
return io.ErrUnexpectedEOF
}
// Skip reserved area.
addr := b[4:nsLen]
if err := checkIPv6(addr); err != nil {
return err
}
options, err := parseOptions(b[nsLen:])
if err != nil {
return err
}
*ns = NeighborSolicitation{
TargetAddress: make(net.IP, net.IPv6len),
Options: options,
}
copy(ns.TargetAddress, addr)
return nil
}
var _ Message = &RouterAdvertisement{}
// A RouterAdvertisement is a Router Advertisement message as
// described in RFC 4861, Section 4.1.
type RouterAdvertisement struct {
CurrentHopLimit uint8
ManagedConfiguration bool
OtherConfiguration bool
MobileIPv6HomeAgent bool
RouterSelectionPreference Preference
NeighborDiscoveryProxy bool
RouterLifetime time.Duration
ReachableTime time.Duration
RetransmitTimer time.Duration
Options []Option
}
// A Preference is a NDP router selection or route preference value as
// described in RFC 4191, Section 2.1.
type Preference int
// Possible Preference values.
const (
Medium Preference = 0
High Preference = 1
prfReserved Preference = 2
Low Preference = 3
)
// Type implements Message.
func (ra *RouterAdvertisement) Type() ipv6.ICMPType { return ipv6.ICMPTypeRouterAdvertisement }
func (ra *RouterAdvertisement) marshal() ([]byte, error) {
if err := checkPreference(ra.RouterSelectionPreference); err != nil {
return nil, err
}
b := make([]byte, raLen)
b[0] = ra.CurrentHopLimit
if ra.ManagedConfiguration {
b[1] |= (1 << 7)
}
if ra.OtherConfiguration {
b[1] |= (1 << 6)
}
if ra.MobileIPv6HomeAgent {
b[1] |= (1 << 5)
}
if prf := uint8(ra.RouterSelectionPreference); prf != 0 {
b[1] |= (prf << 3)
}
if ra.NeighborDiscoveryProxy {
b[1] |= (1 << 2)
}
lifetime := ra.RouterLifetime.Seconds()
binary.BigEndian.PutUint16(b[2:4], uint16(lifetime))
reach := ra.ReachableTime / time.Millisecond
binary.BigEndian.PutUint32(b[4:8], uint32(reach))
retrans := ra.RetransmitTimer / time.Millisecond
binary.BigEndian.PutUint32(b[8:12], uint32(retrans))
ob, err := marshalOptions(ra.Options)
if err != nil {
return nil, err
}
b = append(b, ob...)
return b, nil
}
func (ra *RouterAdvertisement) unmarshal(b []byte) error {
if len(b) < raLen {
return io.ErrUnexpectedEOF
}
// Skip message body for options.
options, err := parseOptions(b[raLen:])
if err != nil {
return err
}
var (
mFlag = (b[1] & 0x80) != 0
oFlag = (b[1] & 0x40) != 0
hFlag = (b[1] & 0x20) != 0
prf = Preference((b[1] & 0x18) >> 3)
pFlag = (b[1] & 0x04) != 0
lifetime = time.Duration(binary.BigEndian.Uint16(b[2:4])) * time.Second
reach = time.Duration(binary.BigEndian.Uint32(b[4:8])) * time.Millisecond
retrans = time.Duration(binary.BigEndian.Uint32(b[8:12])) * time.Millisecond
)
// Per RFC 4191, Section 2.2:
// "If the Reserved (10) value is received, the receiver MUST treat the
// value as if it were (00)."
if prf == prfReserved {
prf = Medium
}
*ra = RouterAdvertisement{
CurrentHopLimit: b[0],
ManagedConfiguration: mFlag,
OtherConfiguration: oFlag,
MobileIPv6HomeAgent: hFlag,
RouterSelectionPreference: prf,
NeighborDiscoveryProxy: pFlag,
RouterLifetime: lifetime,
ReachableTime: reach,
RetransmitTimer: retrans,
Options: options,
}
return nil
}
var _ Message = &RouterSolicitation{}
// A RouterSolicitation is a Router Solicitation message as
// described in RFC 4861, Section 4.1.
type RouterSolicitation struct {
Options []Option
}
// Type implements Message.
func (rs *RouterSolicitation) Type() ipv6.ICMPType { return ipv6.ICMPTypeRouterSolicitation }
func (rs *RouterSolicitation) marshal() ([]byte, error) {
// b contains reserved area.
b := make([]byte, rsLen)
ob, err := marshalOptions(rs.Options)
if err != nil {
return nil, err
}
b = append(b, ob...)
return b, nil
}
func (rs *RouterSolicitation) unmarshal(b []byte) error {
if len(b) < rsLen {
return io.ErrUnexpectedEOF
}
// Skip reserved area.
options, err := parseOptions(b[rsLen:])
if err != nil {
return err
}
*rs = RouterSolicitation{
Options: options,
}
return nil
}
// checkIPv6 verifies that ip is an IPv6 address.
func checkIPv6(ip net.IP) error {
if ip.To16() == nil || ip.To4() != nil {
return fmt.Errorf("ndp: invalid IPv6 address: %q", ip.String())
}
return nil
}
// checkPreference checks the validity of a Preference value.
func checkPreference(prf Preference) error {
switch prf {
case Low, Medium, High:
return nil
case prfReserved:
return errors.New("ndp: cannot use reserved router selection preference value")
default:
return fmt.Errorf("ndp: unknown router selection preference value: %d", prf)
}
}