/
ethernet.dylan
287 lines (245 loc) · 9.44 KB
/
ethernet.dylan
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
module: ethernet
author: Andreas Bogk and Hannes Mehnert
copyright: 2005-2011 Andreas Bogk and Hannes Mehnert. All rights reserved.
license: see license.txt in this distribution
define n-byte-vector(<ipv4-address>, 4) end;
define method read-frame (frame-type == <ipv4-address>, string :: <string>)
=> (res)
make(<ipv4-address>,
data: map-as(<stretchy-vector-subsequence>, string-to-integer, split(string, '.')));
end;
define method as (class == <string>, frame :: <ipv4-address>) => (string :: <string>);
reduce1(method(a, b) concatenate(a, ".", b) end,
map-as(<stretchy-vector>,
integer-to-string,
frame.data))
end;
define n-byte-vector(<mac-address>, 6) end;
define method read-frame(type == <mac-address>,
string :: <string>)
=> (res)
let res = as-lowercase(string);
if (any?(method(x) x = ':' end, res))
//input: 00:de:ad:be:ef:00
let fields = split(res, ':');
unless(fields.size = 6)
signal(make(<parse-error>))
end;
make(<mac-address>,
data: map-as(<stretchy-vector-subsequence>, rcurry(string-to-integer, base: 16), fields));
else
//input: 00deadbeef00
unless (res.size = 12)
signal(make(<parse-error>));
end;
let data = make(<byte-vector>, size: 6);
for (i from 0 below data.size)
data[i] := string-to-integer(res, start: i * 2, end: (i + 1) * 2, base: 16);
end;
make(<mac-address>, data: data);
end;
end;
define method as (class == <string>, frame :: <mac-address>) => (string :: <string>);
reduce1(method(a, b) concatenate(a, ":", b) end,
map-as(<stretchy-vector>,
rcurry(integer-to-string, base: 16, size: 2),
frame.data))
end;
define binary-data <ethernet-frame> (<header-frame>)
summary "ETH %= -> %=", source-address, destination-address;
field destination-address :: <mac-address>;
field source-address :: <mac-address>;
layering field type-code :: <2byte-big-endian-unsigned-integer>;
variably-typed field payload,
type-function: if (frame.type-code > 1500)
frame.payload-type
else
<llc-frame>
end;
end;
define binary-data <llc-frame> (<header-frame>)
field dsap :: <7bit-unsigned-integer>;
field address-type-designation :: <1bit-unsigned-integer>;
field ssap :: <7bit-unsigned-integer>;
field command-response-identifer :: <1bit-unsigned-integer>;
field control :: <unsigned-byte>;
variably-typed field payload,
type-function: case
frame.dsap = 85 & frame.ssap = 85 => <snap-frame>;
frame.dsap = 33 & frame.ssap = 33 => <stp-frame>;
otherwise => <raw-frame>;
end;
end;
define binary-data <snap-frame> (<header-frame>)
field organization-code :: <3byte-big-endian-unsigned-integer> = 0;
layering field type-code :: <2byte-big-endian-unsigned-integer>;
variably-typed field payload;
end;
define binary-data <vlan-tag> (<header-frame>)
over <ethernet-frame> #x8100;
summary "VLAN: %=", vlan-id;
field priority :: <3bit-unsigned-integer> = 0;
field canonical-format-indicator :: <1bit-unsigned-integer> = 0;
field vlan-id :: <12bit-unsigned-integer>;
layering field type-code :: <2byte-big-endian-unsigned-integer>;
variably-typed field payload;
end;
define method source-address (frame :: <vlan-tag>) => (res :: <mac-address>)
frame.parent.source-address;
end;
define binary-data <stp-identifier> (<container-frame>)
summary "%=/%=", bridge-priority, bridge-address;
field bridge-priority :: <2byte-big-endian-unsigned-integer>;
field bridge-address :: <mac-address>;
end;
define abstract binary-data <stp-frame> (<variably-typed-container-frame>)
field protocol-identifier :: <2byte-big-endian-unsigned-integer>;
field protocol-version :: <unsigned-byte>;
layering field bpdu-type :: <unsigned-byte>;
end;
define binary-data <stp-configuration-frame> (<stp-frame>)
summary "STP configuration";
over <stp-frame> 0;
field flags :: <unsigned-byte>;
field root-identifier :: <stp-identifier>;
field root-path-cost :: <big-endian-unsigned-integer-4byte>;
field bridge-identifier :: <stp-identifier>;
field port-identifier :: <2byte-big-endian-unsigned-integer>;
field message-age :: <2byte-big-endian-unsigned-integer>;
field max-age :: <2byte-big-endian-unsigned-integer>;
field hello-time :: <2byte-big-endian-unsigned-integer>;
field forward-delay :: <2byte-big-endian-unsigned-integer>;
end;
define binary-data <stp-topology-change-frame> (<stp-frame>)
over <stp-frame> #x80;
summary "STP topology change notification";
end;
define abstract binary-data <cdp-record> (<variably-typed-container-frame>)
length frame.cdp-length * 8;
layering field cdp-type :: <2byte-big-endian-unsigned-integer>;
field cdp-length :: <2byte-big-endian-unsigned-integer>,
fixup: byte-offset(frame-size(frame));
end;
define abstract binary-data <cdp-unknown-record> (<cdp-record>)
field cdp-value :: <raw-frame>;
end;
define abstract binary-data <cdp-string-record> (<cdp-record>)
field cdp-value :: <externally-delimited-string>;
end;
define binary-data <cdp-device-id> (<cdp-string-record>)
over <cdp-record> #x1;
summary "ID: %=", cdp-value;
end;
define binary-data <cdp-address-frame> (<container-frame>)
field cdp-protocol-type :: <unsigned-byte>;
field cdp-protocol-length :: <unsigned-byte>;
field cdp-protocol :: <raw-frame>, length: frame.cdp-protocol-length * 8;
field cdp-address-length :: <2byte-big-endian-unsigned-integer>;
field cdp-address :: <raw-frame>, length: frame.cdp-address-length * 8;
end;
define binary-data <cdp-addresses-frame> (<cdp-record>)
over <cdp-record> #x2;
// FIXME: field address-count :: <big-endian-unsigned-integer-4byte>;
field address-count-first :: <unsigned-byte>;
field address-count :: <3byte-big-endian-unsigned-integer>;
repeated field cdp-addresses :: <cdp-address-frame>,
count: frame.address-count;
end;
define binary-data <cdp-port-id> (<cdp-string-record>)
over <cdp-record> #x3;
summary "Port: %=", cdp-value;
end;
define binary-data <cdp-capabilities> (<cdp-record>)
over <cdp-record> #x4;
field padding1 :: <3byte-big-endian-unsigned-integer>;
field padding2 :: <1bit-unsigned-integer>;
field cdp-layer1 :: <1bit-unsigned-integer>;
field cdp-no-igmp :: <1bit-unsigned-integer>;
field cdp-layer3-host :: <1bit-unsigned-integer>;
field cdp-layer2-switching :: <1bit-unsigned-integer>;
field cdp-layer2-source-route :: <1bit-unsigned-integer>;
field cdp-layer2-transparent :: <1bit-unsigned-integer>;
field cdp-layer3 :: <1bit-unsigned-integer>;
end;
define binary-data <cdp-version> (<cdp-string-record>)
over <cdp-record> #x5;
summary "Version: %=", cdp-value;
end;
define binary-data <cdp-platform> (<cdp-string-record>)
over <cdp-record> #x6;
summary "Platform: %=", cdp-value;
end;
define binary-data <cdp-ip-prefix> (<container-frame>)
field ip-address :: <raw-frame>, length: 32;
field netmask :: <unsigned-byte>;
end;
define binary-data <cdp-ip-prefixes> (<cdp-record>)
over <cdp-record> #x7;
field ip-prefix-count :: <unsigned-byte>;
repeated field ip-prefixes :: <cdp-ip-prefix>,
count: frame.ip-prefix-count;
end;
define binary-data <cdp-vtp-management-domain> (<cdp-string-record>)
over <cdp-record> #x9;
summary "VTP Management domain: %=", cdp-value;
end;
define binary-data <cdp-vtp-native-vlan-id> (<cdp-record>)
over <cdp-record> #xa;
summary "VTP native VLAN: %=", cdp-native-vlan-id;
field cdp-native-vlan-id :: <2byte-big-endian-unsigned-integer>;
end;
define binary-data <cdp-duplex-frame> (<cdp-record>)
over <cdp-record> #xb;
summary "Duplex: %s", method(x) if (x.cdp-duplex = 0) "half" else "full" end end;
field cdp-duplex :: <unsigned-byte>;
end;
define binary-data <cdp-hello> (<cdp-unknown-record>)
over <cdp-record> #x8;
summary "Hello (undocumented)";
end;
define binary-data <cdp-ata-186-voip-vlan-request> (<cdp-unknown-record>)
over <cdp-record> #xe;
summary "ATA 186 VoIP VLAN Request";
end;
define binary-data <cdp-ata-186-voip-vlan-assignment> (<cdp-unknown-record>)
over <cdp-record> #xf;
summary "ATA 186 VoIP VLAN Assignment";
end;
define binary-data <cdp-power> (<cdp-unknown-record>)
over <cdp-record> #x10;
summary "Power: %=", cdp-value;
end;
define binary-data <cdp-mtu-frame> (<cdp-record>)
over <cdp-record> #x11;
summary "MTU: %=", cdp-mtu;
field cdp-mtu :: <big-endian-unsigned-integer-4byte>;
end;
define binary-data <cdp-avvid-trust-bitmap> (<cdp-unknown-record>)
over <cdp-record> #x12;
summary "AVVID Trust Bitmap";
end;
define binary-data <cdp-avvid-untrusted-port-CoS> (<cdp-unknown-record>)
over <cdp-record> #x13;
summary "AVVID Untrusted Port CoS";
end;
define binary-data <cdp-system-name> (<cdp-string-record>)
over <cdp-record> #x14;
summary "System name: %=", cdp-value;
end;
define binary-data <cdp-system-object-id> (<cdp-unknown-record>)
over <cdp-record> #x16;
summary "System OID: %=", cdp-value;
end;
define binary-data <cdp-physical-location> (<cdp-string-record>)
over <cdp-record> #x17;
summary "Physical location: %=", cdp-value;
end;
define binary-data <cdp-frame> (<container-frame>)
over <ethernet-frame> #x2000;
over <cisco-hdlc-frame> #x2000;
summary "Cisco Discovery Protocol";
field version :: <unsigned-byte>;
field time-to-live :: <unsigned-byte>;
field checksum :: <2byte-big-endian-unsigned-integer>;
repeated field cdp-values :: <cdp-record>, reached-end?: #f;
end;