-
Notifications
You must be signed in to change notification settings - Fork 38
/
ethernet.go
297 lines (252 loc) · 8.64 KB
/
ethernet.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
// Package ethernet implements marshaling and unmarshaling of IEEE 802.3
// Ethernet II frames and IEEE 802.1Q VLAN tags.
package ethernet
import (
"encoding/binary"
"errors"
"fmt"
"hash/crc32"
"io"
"net"
)
//go:generate stringer -output=string.go -type=EtherType
const (
// minPayload is the minimum payload size for an Ethernet frame, assuming
// that no 802.1Q VLAN tags are present.
minPayload = 46
)
// Broadcast is a special hardware address which indicates a Frame should
// be sent to every device on a given LAN segment.
var Broadcast = net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
// ErrInvalidFCS is returned when Frame.UnmarshalFCS detects an incorrect
// Ethernet frame check sequence in a byte slice for a Frame.
var ErrInvalidFCS = errors.New("invalid frame check sequence")
// An EtherType is a value used to identify an upper layer protocol
// encapsulated in a Frame.
//
// A list of IANA-assigned EtherType values may be found here:
// http://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml.
type EtherType uint16
// Common EtherType values frequently used in a Frame.
const (
EtherTypeIPv4 EtherType = 0x0800
EtherTypeARP EtherType = 0x0806
EtherTypeIPv6 EtherType = 0x86DD
// EtherTypeVLAN and EtherTypeServiceVLAN are used as 802.1Q Tag Protocol
// Identifiers (TPIDs).
EtherTypeVLAN EtherType = 0x8100
EtherTypeServiceVLAN EtherType = 0x88a8
)
// A Frame is an IEEE 802.3 Ethernet II frame. A Frame contains information
// such as source and destination hardware addresses, zero or more optional
// 802.1Q VLAN tags, an EtherType, and payload data.
type Frame struct {
// Destination specifies the destination hardware address for this Frame.
//
// If this address is set to Broadcast, the Frame will be sent to every
// device on a given LAN segment.
Destination net.HardwareAddr
// Source specifies the source hardware address for this Frame.
//
// Typically, this is the hardware address of the network interface used to
// send this Frame.
Source net.HardwareAddr
// ServiceVLAN specifies an optional 802.1Q service VLAN tag, for use with
// 802.1ad double tagging, or "Q-in-Q". If ServiceVLAN is not nil, VLAN must
// not be nil as well.
//
// Most users should leave this field set to nil and use VLAN instead.
ServiceVLAN *VLAN
// VLAN specifies an optional 802.1Q customer VLAN tag, which may or may
// not be present in a Frame. It is important to note that the operating
// system may automatically strip VLAN tags before they can be parsed.
VLAN *VLAN
// EtherType is a value used to identify an upper layer protocol
// encapsulated in this Frame.
EtherType EtherType
// Payload is a variable length data payload encapsulated by this Frame.
Payload []byte
}
// MarshalBinary allocates a byte slice and marshals a Frame into binary form.
func (f *Frame) MarshalBinary() ([]byte, error) {
b := make([]byte, f.length())
_, err := f.read(b)
return b, err
}
// MarshalFCS allocates a byte slice, marshals a Frame into binary form, and
// finally calculates and places a 4-byte IEEE CRC32 frame check sequence at
// the end of the slice.
//
// Most users should use MarshalBinary instead. MarshalFCS is provided as a
// convenience for rare occasions when the operating system cannot
// automatically generate a frame check sequence for an Ethernet frame.
func (f *Frame) MarshalFCS() ([]byte, error) {
// Frame length with 4 extra bytes for frame check sequence
b := make([]byte, f.length()+4)
if _, err := f.read(b); err != nil {
return nil, err
}
// Compute IEEE CRC32 checksum of frame bytes and place it directly
// in the last four bytes of the slice
binary.BigEndian.PutUint32(b[len(b)-4:], crc32.ChecksumIEEE(b[0:len(b)-4]))
return b, nil
}
// read reads data from a Frame into b. read is used to marshal a Frame
// into binary form, but does not allocate on its own.
func (f *Frame) read(b []byte) (int, error) {
// S-VLAN must also have accompanying C-VLAN.
if f.ServiceVLAN != nil && f.VLAN == nil {
return 0, ErrInvalidVLAN
}
copy(b[0:6], f.Destination)
copy(b[6:12], f.Source)
// Marshal each non-nil VLAN tag into bytes, inserting the appropriate
// EtherType/TPID before each, so devices know that one or more VLANs
// are present.
vlans := []struct {
vlan *VLAN
tpid EtherType
}{
{vlan: f.ServiceVLAN, tpid: EtherTypeServiceVLAN},
{vlan: f.VLAN, tpid: EtherTypeVLAN},
}
n := 12
for _, vt := range vlans {
if vt.vlan == nil {
continue
}
// Add VLAN EtherType and VLAN bytes.
binary.BigEndian.PutUint16(b[n:n+2], uint16(vt.tpid))
if _, err := vt.vlan.read(b[n+2 : n+4]); err != nil {
return 0, err
}
n += 4
}
// Marshal actual EtherType after any VLANs, copy payload into
// output bytes.
binary.BigEndian.PutUint16(b[n:n+2], uint16(f.EtherType))
copy(b[n+2:], f.Payload)
return len(b), nil
}
// UnmarshalBinary unmarshals a byte slice into a Frame.
func (f *Frame) UnmarshalBinary(b []byte) error {
// Verify that both hardware addresses and a single EtherType are present
if len(b) < 14 {
return io.ErrUnexpectedEOF
}
// Track offset in packet for reading data
n := 14
// Continue looping and parsing VLAN tags until no more VLAN EtherType
// values are detected
et := EtherType(binary.BigEndian.Uint16(b[n-2 : n]))
switch et {
case EtherTypeServiceVLAN, EtherTypeVLAN:
// VLAN type is hinted for further parsing. An index is returned which
// indicates how many bytes were consumed by VLAN tags.
nn, err := f.unmarshalVLANs(et, b[n:])
if err != nil {
return err
}
n += nn
default:
// No VLANs detected.
f.EtherType = et
}
// Allocate single byte slice to store destination and source hardware
// addresses, and payload
bb := make([]byte, 6+6+len(b[n:]))
copy(bb[0:6], b[0:6])
f.Destination = bb[0:6]
copy(bb[6:12], b[6:12])
f.Source = bb[6:12]
// There used to be a minimum payload length restriction here, but as
// long as two hardware addresses and an EtherType are present, it
// doesn't really matter what is contained in the payload. We will
// follow the "robustness principle".
copy(bb[12:], b[n:])
f.Payload = bb[12:]
return nil
}
// UnmarshalFCS computes the IEEE CRC32 frame check sequence of a Frame,
// verifies it against the checksum present in the byte slice, and finally,
// unmarshals a byte slice into a Frame.
//
// Most users should use UnmarshalBinary instead. UnmarshalFCS is provided as
// a convenience for rare occasions when the operating system cannot
// automatically verify a frame check sequence for an Ethernet frame.
func (f *Frame) UnmarshalFCS(b []byte) error {
// Must contain enough data for FCS, to avoid panics
if len(b) < 4 {
return io.ErrUnexpectedEOF
}
// Verify checksum in slice versus newly computed checksum
want := binary.BigEndian.Uint32(b[len(b)-4:])
got := crc32.ChecksumIEEE(b[0 : len(b)-4])
if want != got {
return ErrInvalidFCS
}
return f.UnmarshalBinary(b[0 : len(b)-4])
}
// length calculates the number of bytes required to store a Frame.
func (f *Frame) length() int {
// If payload is less than the required minimum length, we zero-pad up to
// the required minimum length
pl := len(f.Payload)
if pl < minPayload {
pl = minPayload
}
// Add additional length if VLAN tags are needed.
var vlanLen int
switch {
case f.ServiceVLAN != nil && f.VLAN != nil:
vlanLen = 8
case f.VLAN != nil:
vlanLen = 4
}
// 6 bytes: destination hardware address
// 6 bytes: source hardware address
// N bytes: VLAN tags (if present)
// 2 bytes: EtherType
// N bytes: payload length (may be padded)
return 6 + 6 + vlanLen + 2 + pl
}
// unmarshalVLANs unmarshals S/C-VLAN tags. It is assumed that tpid
// is a valid S/C-VLAN TPID.
func (f *Frame) unmarshalVLANs(tpid EtherType, b []byte) (int, error) {
// 4 or more bytes must remain for valid S/C-VLAN tag and EtherType.
if len(b) < 4 {
return 0, io.ErrUnexpectedEOF
}
// Track how many bytes are consumed by VLAN tags.
var n int
switch tpid {
case EtherTypeServiceVLAN:
vlan := new(VLAN)
if err := vlan.UnmarshalBinary(b[n : n+2]); err != nil {
return 0, err
}
f.ServiceVLAN = vlan
// Assume that a C-VLAN immediately trails an S-VLAN.
if EtherType(binary.BigEndian.Uint16(b[n+2:n+4])) != EtherTypeVLAN {
return 0, ErrInvalidVLAN
}
// 4 or more bytes must remain for valid C-VLAN tag and EtherType.
n += 4
if len(b[n:]) < 4 {
return 0, io.ErrUnexpectedEOF
}
// Continue to parse the C-VLAN.
fallthrough
case EtherTypeVLAN:
vlan := new(VLAN)
if err := vlan.UnmarshalBinary(b[n : n+2]); err != nil {
return 0, err
}
f.VLAN = vlan
f.EtherType = EtherType(binary.BigEndian.Uint16(b[n+2 : n+4]))
n += 4
default:
panic(fmt.Sprintf("unknown VLAN TPID: %04x", tpid))
}
return n, nil
}