-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvswitch.c
More file actions
711 lines (631 loc) · 16.5 KB
/
vswitch.c
File metadata and controls
711 lines (631 loc) · 16.5 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*
This file (was) part of GNUnet.
Copyright (C) 2018 Christian Grothoff
GNUnet is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file vswitch.c
* @brief Ethernet switch
* @author Christian Grothoff
*/
#include "glab.h"
#include "print.c"
#include "stdbool.h"
/**
* Maximum number of VLANs supported per interface.
* (and also by the 802.1Q standard tag).
*/
#define MAX_VLANS 4092
/**
* Value used to indicate "no VLAN" (or no more VLANs).
*/
#define NO_VLAN (-1)
/**
* Which VLAN should we assume for untagged frames on
* interfaces without any specified tag?
*/
#define DEFAULT_VLAN 0
/**
* gcc 4.x-ism to pack structures (to be used before structs);
* Using this still causes structs to be unaligned on the stack on Sparc
* (See #670578 from Debian).
*/
_Pragma("pack(push)") _Pragma("pack(1)")
struct EthernetHeader
{
struct MacAddress dst;
struct MacAddress src;
uint16_t tag;
uint16_t vlanId;
};
struct SwitchCache {
struct Interface *interface;
struct MacAddress macAddress;
};
static struct SwitchCache switchCache[500];
static int switchTableIndex = 0;
static void printMac(struct MacAddress *mac) {
fprintf(stderr,
"%02X:%02X:%02X:%02X:%02X:%02X:",
mac->mac[0],
mac->mac[1],
mac->mac[2],
mac->mac[3],
mac->mac[4],
mac->mac[5]);
}
/**
* Compare two MacAddresses
* @param macAddress1
* @param macAddress2
* @return
*/
static int maccmp (const struct MacAddress *macAddress1, const struct MacAddress *macAddress2){
return memcmp(macAddress1, macAddress2, sizeof(struct MacAddress));
}
/**
* IEEE 802.1Q header.
*/
struct Q
{
uint16_t tpid; /* must be #ETH_802_1Q_TAG */
uint16_t tci;
};
_Pragma("pack(pop)")
/**
* Per-interface context.
*/
struct Interface
{
/**
* MAC of interface.
*/
struct MacAddress mac;
/**
* Number of this interface.
*/
uint16_t ifc_num;
/**
* Name of the network interface, i.e. "eth0".
*/
char *ifc_name;
/**
* Which tagged VLANs does this interface participate in?
* Array terminated by #NO_VLAN entry.
*/
int16_t tagged_vlans[MAX_VLANS + 1];
/**
* Which untagged VLAN does this interface participate in?
* #NO_VLAN for none.
*/
int16_t untagged_vlan;
};
/**
* Forward @a frame to interface @a dst.
*
* @param dst target interface to send the frame out on
* @param frame the frame to forward
* @param frame_size number of bytes in @a frame
*/
static void
forward_to (struct Interface *dst,
const void *frame,
size_t frame_size)
{
char iob[frame_size + sizeof (struct GLAB_MessageHeader)];
struct GLAB_MessageHeader hdr;
hdr.size = htons (sizeof(iob));
hdr.type = htons (dst->ifc_num);
memcpy (iob,
&hdr,
sizeof(hdr));
memcpy (&iob[sizeof (hdr)],
frame,
frame_size);
write_all (STDOUT_FILENO,
iob,
sizeof(iob));
}
/**
* Number of available contexts.
*/
static unsigned int num_ifc;
/**
* All the contexts.
*/
static struct Interface *gifc;
static void
print_frame(uint16_t interface,
const void *frame,
size_t frame_size) {
const uint8_t *frame1 = frame;
unsigned int index;
fprintf(stderr, "\n");
for (index = 0; index < frame_size; index++) {
fprintf(stderr, "%02X:",
frame1[index]);
};
fprintf(stderr, "\n");
fprintf(stderr, "frame_size: %lu", frame_size);
fprintf(stderr, "source: ");
for (index = 0; index < 6; index++) {
fprintf(stderr, "%02X:", frame1[index]);
};
fprintf(stderr, " destination: ");
for (index = 6; index < 12; index++) {
fprintf(stderr, "%02X:", frame1[index]);
};
fprintf(stderr, " payload: ");
for (index = 12; index < 20; index++) {
fprintf(stderr, "%02X:", frame1[index]);
};
fprintf(stderr, " fsc: ");
for (index = frame_size - 4; index < frame_size; index++) {
fprintf(stderr, "%02X:", frame1[index]);
};
fprintf(stderr, "\n");
}
/**
* Parse and process frame received on @a ifc.
*
* @param ifc interface we got the frame on
* @param frame raw frame data
* @param frame_size number of bytes in @a frame
*/
static void
parse_frame (struct Interface *ifc,
const void *frame,
size_t frame_size)
{
const uint8_t *framec = frame;
struct EthernetHeader ethernetHeader;
if (frame_size < sizeof (ethernetHeader))
{
fprintf (stderr,
"Malformed frame\n");
return;
}
memcpy (ðernetHeader,
frame,
sizeof (ethernetHeader));
/* DO work here! */
ethernetHeader.vlanId = htons(ethernetHeader.vlanId);
for (int i = 0; ifc->tagged_vlans[i] != NO_VLAN; i++) {
fprintf(stderr, "tagged_vlans: %04X\n", ifc->tagged_vlans[i]);
}
fprintf(stderr, "ethernet header tag: %04X\n", ethernetHeader.tag);
fprintf(stderr, "ethernet header vlanId: %04X\n", ethernetHeader.vlanId);
for (int i = 0; i < num_ifc; i++) {
fprintf(stderr, "ifc_name: %s\n", gifc[i].ifc_name);
fprintf(stderr, "ifc_num: %d\n", gifc[i].ifc_num);
fprintf(stderr, "untagged_vlan: %04X\n", gifc[i].untagged_vlan);
for (int j = 0; gifc[i].tagged_vlans[j] != NO_VLAN; j++) {
fprintf(stderr, "tagged_vlans: %04X\n", gifc[i].tagged_vlans[j]);
}
}
if (ethernetHeader.tag == 0x0081) {
for (int i = 0; ifc->tagged_vlans[i] != ethernetHeader.vlanId; i++) {
if (ifc -> tagged_vlans[i] == NO_VLAN) {
return;
}
}
if (ifc -> untagged_vlan != NO_VLAN) {
return;
}
}
else {
if (ifc -> untagged_vlan == NO_VLAN) {
return;
}
}
bool foundEntry = false;
for (int i = 0; i < switchTableIndex; i++){
if (0 == maccmp(&switchCache[i].macAddress, ðernetHeader.src)) {
foundEntry = true;
switchCache[i].interface = ifc;
}
}
if (!foundEntry && switchTableIndex < 500)
{
switchCache[switchTableIndex].interface = ifc;
switchCache[switchTableIndex].macAddress = ethernetHeader.src;
switchTableIndex++;
}
fprintf(stderr, "ethernetHeader dst:\n");
printMac(ðernetHeader.dst);
fprintf(stderr, "\n");
for (int i = 0; i < switchTableIndex; i++) {
if (0 == maccmp(ðernetHeader.dst, &switchCache[i].macAddress)) {
if (ethernetHeader.tag == htons(0x8100)) {
if (ethernetHeader.vlanId == switchCache[i].interface -> untagged_vlan) {
const uint8_t *frame1 = frame;
uint8_t frame2[frame_size - 4];
memcpy(&frame2, &frame1, 12);
memcpy(&frame2[12], &frame1[16], frame_size - 12);
forward_to(switchCache[i].interface, frame2, sizeof(frame2));
return;
}
else {
for (int j = 0; switchCache[i].interface->tagged_vlans[j] != ethernetHeader.vlanId; j++) {
if (switchCache[i].interface -> tagged_vlans[j] == NO_VLAN) {
return;
}
}
forward_to(switchCache[i].interface, frame, frame_size);
return;
}
}
else {
if (ifc -> untagged_vlan == switchCache[i].interface -> untagged_vlan) {
forward_to(switchCache[i].interface, frame, frame_size);
return;
}
else {
for (int j = 0; switchCache[i].interface->tagged_vlans[j] != ifc->untagged_vlan; j++) {
if (switchCache[i].interface -> tagged_vlans[j] == NO_VLAN) {
return;
}
}
const uint8_t *frame1 = frame;
uint8_t frame2[frame_size + 4];
struct Q q = {htons(0x8100), ifc->untagged_vlan};
memcpy(&frame2, frame1, 12);
memcpy(&frame2[12], &q, sizeof(struct Q));
memcpy(&frame2[16], &frame1[12], frame_size - 16);
forward_to(switchCache[i].interface, frame1, sizeof(frame1));
return;
}
}
}
}
for (int i = 0; i < num_ifc; i++) {
if (0 != maccmp(&ifc->mac, &gifc[i].mac)) {
if (ethernetHeader.tag == htons(0x8100)) {
if (ethernetHeader.vlanId == gifc[i].untagged_vlan) {
const uint8_t *frame1 = frame;
uint8_t frame2[frame_size - 4];
print_frame(gifc[i].ifc_num, frame, frame_size);
memcpy(&frame2, frame, 12);
memcpy(&frame2[12], &frame[16], frame_size - 12);
struct EthernetHeader ethernetHeader1;
forward_to(&gifc[i], frame2, sizeof(frame2));
}
else {
bool doSendFrame = true;
for (int j = 0; gifc[i].tagged_vlans[j] != ethernetHeader.vlanId; j++) {
if (gifc[i].tagged_vlans[j] == NO_VLAN) {
doSendFrame = false;
break;
}
}
if (doSendFrame) {
const uint8_t *frame1 = frame;
uint8_t frame2[frame_size + 4];
struct Q q = {htons(0x8100), htons(ifc->untagged_vlan)};
memcpy(&frame2[16], &frame1[12], frame_size - 16);
forward_to(&gifc[i], frame, frame_size);
}
}
}
else {
if (ifc-> untagged_vlan == gifc[i].untagged_vlan) {
printMac(&gifc[i].mac);
forward_to(&gifc[i], frame, frame_size);
}
else {
bool doSendFrame = true;
for (int j = 0; gifc[i].tagged_vlans[j] != ifc->untagged_vlan; j++) {
if (gifc[i].tagged_vlans[j] == NO_VLAN) {
doSendFrame = false;
break;
}
}
if (doSendFrame) {
const uint8_t *frame1 = frame;
uint8_t frame2[frame_size + 4];
fprintf(stderr, "dest address: ");
printMac(&gifc[i].mac);
fprintf(stderr, ", dest: %s\n", gifc[i].ifc_name);
struct Q q = {0x0081, 0x0100};
memcpy(&frame2, frame1, 12);
memcpy(&frame2[12], &q, sizeof(struct Q));
memcpy(&frame2[16], &frame1[12], frame_size - 12);
struct EthernetHeader ethernetHeader1;
memcpy(ðernetHeader1, frame1, sizeof(ethernetHeader1));
fprintf(stderr, "ethernetHeader.src:\n");
printMac(ðernetHeader1.src);
fprintf(stderr, "ethernetHeader.dst:\n");
printMac(ðernetHeader1.dst);
fprintf(stderr, "ethernetHeader.vlanId: %04X\n", ethernetHeader1.vlanId);
fprintf(stderr, "ethernetHeader.vlanId: %04X\n", ethernetHeader1.tag);
forward_to(&gifc[i], frame2, sizeof(frame2));
}
}
}
}
}
}
/**
* Process frame received from @a interface.
*
* @param interface number of the interface on which we received @a frame
* @param frame the frame
* @param frame_size number of bytes in @a frame
*/
static void
handle_frame (uint16_t interface,
const void *frame,
size_t frame_size)
{
if (interface > num_ifc)
abort ();
parse_frame (&gifc[interface - 1],
frame,
frame_size);
}
/**
* Handle control message @a cmd.
*
* @param cmd text the user entered
* @param cmd_len length of @a cmd
*/
static void
handle_control (char *cmd,
size_t cmd_len)
{
cmd[cmd_len - 1] = '\0';
fprintf (stderr,
"Received command `%s' (ignored)\n",
cmd);
}
/**
* Handle MAC information @a mac
*
* @param ifc_num number of the interface with @a mac
* @param mac the MAC address at @a ifc_num
*/
static void
handle_mac (uint16_t ifc_num,
const struct MacAddress *mac)
{
if (ifc_num > num_ifc)
abort ();
gifc[ifc_num - 1].mac = *mac;
}
/**
* Parse tagged interface specification found between @a start
* and @a end.
*
* @param start beginning of tagged specification, with ':'
* @param end end of tagged specification, should point to ']'
* @param off interface offset for error reporting
* @param ifc[out] what to initialize
* @return 0 on success
*/
static int
parse_tagged (const char *start,
const char *end,
int off,
struct Interface *ifc)
{
char *spec;
unsigned int pos;
if (':' != *start)
{
fprintf (stderr,
"Tagged definition for interface #%d lacks ':'\n",
off);
return 1;
}
start++;
spec = strndup (start,
end - start);
if (NULL == spec)
{
perror ("strndup");
return 1;
}
pos = 0;
for (const char *tok = strtok (spec,
",");
NULL != tok;
tok = strtok (NULL,
","))
{
unsigned int tag;
if (pos == MAX_VLANS)
{
fprintf (stderr,
"Too many VLANs specified for interface #%d\n",
off);
free (spec);
return 1;
}
if (1 != sscanf (tok,
"%u",
&tag))
{
fprintf (stderr,
"Expected number in tagged definition for interface #%d\n",
off);
free (spec);
return 1;
}
if (tag > MAX_VLANS)
{
fprintf (stderr,
"%u is too large for a 802.1Q VLAN ID (on interface #%d)\n",
tag,
off);
free (spec);
return 1;
}
ifc->tagged_vlans[pos++] = (int16_t) tag;
}
ifc->tagged_vlans[pos] = NO_VLAN;
free (spec);
return 0;
}
/**
* Parse untagged interface specification found between @a start
* and @a end.
*
* @param start beginning of tagged specification, with ':'
* @param end end of tagged specification, should point to ']'
* @param off interface offset for error reporting
* @param ifc[out] what to initialize
* @return 0 on success
*/
static int
parse_untagged (const char *start,
const char *end,
int off,
struct Interface *ifc)
{
char *spec;
unsigned int tag;
if (':' != *start)
{
fprintf (stderr,
"Untagged definition for interface #%d lacks ':'\n",
off);
return 1;
}
start++;
spec = strndup (start,
end - start);
if (NULL == spec)
{
perror ("strndup");
return 1;
}
if (1 != sscanf (spec,
"%u",
&tag))
{
fprintf (stderr,
"Expected number in untagged definition for interface #%d\n",
off);
free (spec);
return 1;
}
if (tag > MAX_VLANS)
{
fprintf (stderr,
"%u is too large for a 802.1Q VLAN ID (on interface #%d)\n",
tag,
off);
free (spec);
return 1;
}
ifc->untagged_vlan = (int16_t) tag;
free (spec);
return 0;
}
/**
* Parse command-line argument with interface specification.
*
* @param arg command-line argument
* @param off offset of @a arg for error reporting
* @param ifc interface to initialize (ifc_name, tagged_vlans and untagged_vlan).
* @return 0 on success
*/
static int
parse_vlan_args (const char *arg,
int off,
struct Interface *ifc)
{
const char *openbracket;
const char *closebracket;
ifc->tagged_vlans[0] = NO_VLAN;
ifc->untagged_vlan = NO_VLAN;
openbracket = strchr (arg,
(unsigned char) '[');
if (NULL == openbracket)
{
ifc->ifc_name = strdup (arg);
if (NULL == ifc->ifc_name)
{
perror ("strdup");
return 1;
}
ifc->untagged_vlan = DEFAULT_VLAN;
return 0;
}
ifc->ifc_name = strndup (arg,
openbracket - arg);
if (NULL == ifc->ifc_name)
{
perror ("strndup");
return 1;
}
openbracket++;
closebracket = strchr (openbracket,
(unsigned char) ']');
if (NULL == closebracket)
{
fprintf (stderr,
"Interface definition #%d includes '[' but lacks ']'\n",
off);
return 1;
}
switch (*openbracket)
{
case 'T':
return parse_tagged (openbracket + 1,
closebracket,
off,
ifc);
break;
case 'U':
return parse_untagged (openbracket + 1,
closebracket,
off,
ifc);
break;
default:
fprintf (stderr,
"Unsupported tagged/untagged specification `%c' in interface definition #%d\n",
*openbracket,
off);
return 1;
}
}
#include "loop.c"
/**
* Launches the vswitch.
*
* @param argc number of arguments in @a argv
* @param argv binary name, followed by list of interfaces to switch between
* @return not really
*/
int
main (int argc,
char **argv)
{
struct Interface ifc[argc-1];
(void) print;
memset (ifc,
0,
sizeof (ifc));
num_ifc = argc - 1;
gifc = ifc;
for (unsigned int i=1;i<argc;i++)
{
ifc[i-1].ifc_num = i;
if (0 != parse_vlan_args (argv[i], i, &ifc[i-1])){
return 1;
}
}
loop ();
return 0;
}