forked from antrea-io/libOpenflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethernet.go
171 lines (150 loc) · 3.15 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
package protocol
import (
"encoding/binary"
"errors"
"net"
"antrea.io/libOpenflow/util"
)
// see http://en.wikipedia.org/wiki/EtherType
const (
IPv4_MSG = 0x0800
ARP_MSG = 0x0806
LLDP_MSG = 0x88cc
WOL_MSG = 0x0842
RARP_MSG = 0x8035
VLAN_MSG = 0x8100
IPv6_MSG = 0x86DD
STP_MSG = 0x4242
STP_BPDU_MSG = 0xAAAA
)
type Ethernet struct {
Delimiter uint8
HWDst net.HardwareAddr
HWSrc net.HardwareAddr
VLANID VLAN
Ethertype uint16
Data util.Message
}
func NewEthernet() *Ethernet {
eth := new(Ethernet)
eth.HWDst = net.HardwareAddr(make([]byte, 6))
eth.HWSrc = net.HardwareAddr(make([]byte, 6))
eth.VLANID = *NewVLAN()
eth.Ethertype = 0x800
eth.Data = nil
return eth
}
func (e *Ethernet) Len() (n uint16) {
n = 0
n += 12
if e.VLANID.VID != 0 {
n += 4
}
n += 2
if e.Data != nil {
n += e.Data.Len()
}
return
}
func (e *Ethernet) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(e.Len()))
var bytes []byte
n := 0
copy(data[n:], e.HWDst)
n += len(e.HWDst)
copy(data[n:], e.HWSrc)
n += len(e.HWSrc)
if e.VLANID.VID != 0 {
if bytes, err = e.VLANID.MarshalBinary(); err != nil {
return
}
copy(data[n:], bytes)
n += len(bytes)
}
binary.BigEndian.PutUint16(data[n:n+2], e.Ethertype)
n += 2
if e.Data != nil {
if bytes, err = e.Data.MarshalBinary(); err != nil {
return
}
copy(data[n:n+len(bytes)], bytes)
}
return
}
func (e *Ethernet) UnmarshalBinary(data []byte) error {
if len(data) < 14 {
return errors.New("The []byte is too short to unmarshal a full Ethernet message.")
}
n := 0
e.HWDst = net.HardwareAddr(make([]byte, 6))
copy(e.HWDst, data[n:n+6])
n += 6
e.HWSrc = net.HardwareAddr(make([]byte, 6))
copy(e.HWSrc, data[n:n+6])
n += 6
e.Ethertype = binary.BigEndian.Uint16(data[n:])
if e.Ethertype == VLAN_MSG {
e.VLANID = *new(VLAN)
err := e.VLANID.UnmarshalBinary(data[n:])
if err != nil {
return err
}
n += int(e.VLANID.Len())
e.Ethertype = binary.BigEndian.Uint16(data[n:])
} else {
e.VLANID = *new(VLAN)
e.VLANID.VID = 0
}
n += 2
switch e.Ethertype {
case IPv4_MSG:
e.Data = new(IPv4)
case IPv6_MSG:
e.Data = new(IPv6)
case ARP_MSG:
e.Data = new(ARP)
default:
e.Data = new(util.Buffer)
}
return e.Data.UnmarshalBinary(data[n:])
}
const (
PCP_MASK = 0xe000
DEI_MASK = 0x1000
VID_MASK = 0x0fff
)
type VLAN struct {
TPID uint16
PCP uint8
DEI uint8
VID uint16
}
func NewVLAN() *VLAN {
v := new(VLAN)
v.TPID = 0x8100
v.VID = 0
return v
}
func (v *VLAN) Len() (n uint16) {
return 4
}
func (v *VLAN) MarshalBinary() (data []byte, err error) {
data = make([]byte, v.Len())
binary.BigEndian.PutUint16(data[:2], v.TPID)
var tci uint16
tci = (tci | uint16(v.PCP)<<13) + (tci | uint16(v.DEI)<<12) + (tci | v.VID)
binary.BigEndian.PutUint16(data[2:], tci)
return
}
func (v *VLAN) UnmarshalBinary(data []byte) error {
if len(data) < 4 {
return errors.New("The []byte is too short to unmarshal a full VLAN header.")
}
v.TPID = binary.BigEndian.Uint16(data[:2])
var tci uint16
tci = binary.BigEndian.Uint16(data[2:])
v.PCP = uint8(PCP_MASK & tci >> 13)
v.DEI = uint8(DEI_MASK & tci >> 12)
v.VID = VID_MASK & tci
return nil
}