-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.c
More file actions
962 lines (850 loc) · 19.3 KB
/
router.c
File metadata and controls
962 lines (850 loc) · 19.3 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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
/*
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 router.c
* @brief IPv4 router
* @author Christian Grothoff
*/
#include "glab.h"
#include "print.c"
#include "crc.c"
/* see http://www.iana.org/assignments/ethernet-numbers */
#ifndef ETH_P_IPV4
/**
* Number for IPv4
*/
#define ETH_P_IPV4 0x0800
#endif
#ifndef ETH_P_ARP
/**
* Number for ARP
*/
#define ETH_P_ARP 0x0806
#endif
/**
* 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;
/**
* See ETH_P-values.
*/
uint16_t tag;
};
/**
* ARP header for Ethernet-IPv4.
*/
struct ArpHeaderEthernetIPv4
{
/**
* Must be #ARP_HTYPE_ETHERNET.
*/
uint16_t htype;
/**
* Protocol type, must be #ARP_PTYPE_IPV4
*/
uint16_t ptype;
/**
* HLEN. Must be #MAC_ADDR_SIZE.
*/
uint8_t hlen;
/**
* PLEN. Must be sizeof (struct in_addr) (aka 4).
*/
uint8_t plen;
/**
* Type of the operation.
*/
uint16_t oper;
/**
* HW address of sender. We only support Ethernet.
*/
struct MacAddress sender_ha;
/**
* Layer3-address of sender. We only support IPv4.
*/
struct in_addr sender_pa;
/**
* HW address of target. We only support Ethernet.
*/
struct MacAddress target_ha;
/**
* Layer3-address of target. We only support IPv4.
*/
struct in_addr target_pa;
};
/* some systems use one underscore only, and mingw uses no underscore... */
#ifndef __BYTE_ORDER
#ifdef _BYTE_ORDER
#define __BYTE_ORDER _BYTE_ORDER
#else
#ifdef BYTE_ORDER
#define __BYTE_ORDER BYTE_ORDER
#endif
#endif
#endif
#ifndef __BIG_ENDIAN
#ifdef _BIG_ENDIAN
#define __BIG_ENDIAN _BIG_ENDIAN
#else
#ifdef BIG_ENDIAN
#define __BIG_ENDIAN BIG_ENDIAN
#endif
#endif
#endif
#ifndef __LITTLE_ENDIAN
#ifdef _LITTLE_ENDIAN
#define __LITTLE_ENDIAN _LITTLE_ENDIAN
#else
#ifdef LITTLE_ENDIAN
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#endif
#endif
#endif
#define IP_FLAGS_RESERVED 1
#define IP_FLAGS_DO_NOT_FRAGMENT 2
#define IP_FLAGS_MORE_FRAGMENTS 4
#define IP_FLAGS 7
#define IP_FRAGMENT_MULTIPLE 8
/**
* Standard IPv4 header.
*/
struct IPv4Header
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int header_length:4;
unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int version:4;
unsigned int header_length:4;
#else
#error byteorder undefined
#endif
uint8_t diff_serv;
/**
* Length of the packet, including this header.
*/
uint16_t total_length;
/**
* Unique random ID for matching up fragments.
*/
uint16_t identification;
/**
* Fragmentation flags and fragmentation offset.
*/
uint16_t fragmentation_info;
/**
* How many more hops can this packet be forwarded?
*/
uint8_t ttl;
/**
* L4-protocol, for example, IPPROTO_UDP or IPPROTO_TCP.
*/
uint8_t protocol;
/**
* Checksum.
*/
uint16_t checksum;
/**
* Origin of the packet.
*/
struct in_addr source_address;
/**
* Destination of the packet.
*/
struct in_addr destination_address;
};
#define ICMPTYPE_DESTINATION_UNREACHABLE 3
#define ICMPTYPE_TIME_EXCEEDED 11
#define ICMPCODE_NETWORK_UNREACHABLE 0
#define ICMPCODE_HOST_UNREACHABLE 1
#define ICMPCODE_FRAGMENTATION_REQUIRED 4
/**
* ICMP header.
*/
struct IcmpHeader
{
uint8_t type;
uint8_t code;
uint16_t crc;
union
{
/**
* Payload for #ICMPTYPE_DESTINATION_UNREACHABLE (RFC 1191)
*/
struct ih_pmtu
{
uint16_t empty;
uint16_t next_hop_mtu;
} destination_unreachable;
/**
* Unused bytes for #ICMPTYPE_TIME_EXCEEDED.
*/
uint32_t time_exceeded_unused;
} quench;
/* followed by original IP header + first 8 bytes of original IP datagram
(at least for the two ICMP message types we care about here) */
};
_Pragma("pack(pop)")
/**
* Per-interface context.
*/
struct Interface
{
/**
* MAC of interface.
*/
struct MacAddress mac;
/**
* IPv4 address of interface (we only support one IP per interface!)
*/
struct in_addr ip;
/**
* IPv4 netmask of interface.
*/
struct in_addr netmask;
/**
* Name of the interface.
*/
char *name;
/**
* Number of this interface.
*/
uint16_t ifc_num;
/**
* MTU to enforce for this interface.
*/
uint16_t mtu;
};
/**
* Number of available contexts.
*/
static unsigned int num_ifc;
/**
* All the contexts.
*/
static struct Interface *gifc;
/**
* 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;
if (frame_size > dst->mtu)
abort ();
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));
}
/**
* Create Ethernet frame and forward it via @a ifc to @a target_ha.
*
* @param ifc interface to send frame out on
* @param target destination MAC
* @param tag Ethernet tag to use
* @param frame_payload payload to use in frame
* @param frame_payload_size number of bytes in @a frame_payload
*/
static void
forward_frame_payload_to (struct Interface *ifc,
const struct MacAddress *target_ha,
uint16_t tag,
const void *frame_payload,
size_t frame_payload_size)
{
char frame[sizeof (struct EthernetHeader) + frame_payload_size];
struct EthernetHeader eh;
if (frame_payload_size + sizeof (struct EthernetHeader) > ifc->mtu)
abort ();
eh.dst = *target_ha;
eh.src = ifc->mac;
eh.tag = ntohs (tag);
memcpy (frame,
&eh,
sizeof (eh));
memcpy (&frame[sizeof (eh)],
frame_payload,
frame_payload_size);
forward_to (ifc,
frame,
sizeof (frame));
}
/**
* Route the @a ip packet with its @a payload.
*
* @param origin interface we received the packet from
* @param ip IP header
* @param payload IP packet payload
* @param payload_size number of bytes in @a payload
*/
static void
route (struct Interface *origin,
const struct IPv4Header *ip,
const void *payload,
size_t payload_size)
{
/* TODO: do work here */
}
/**
* Process ARP (request or response!)
*
* @param ifc interface we received the ARP request from
* @param eh ethernet header
* @param ah ARP header
*/
static void
handle_arp (struct Interface *ifc,
const struct EthernetHeader *eh,
const struct ArpHeaderEthernetIPv4 *ah)
{
/* TODO: do work here */
}
/**
* 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)
{
struct EthernetHeader eh;
const char *cframe = frame;
if (frame_size < sizeof (eh))
{
fprintf (stderr,
"Malformed frame\n");
return;
}
memcpy (&eh,
frame,
sizeof (eh));
switch (ntohs (eh.tag))
{
case ETH_P_IPV4:
{
struct IPv4Header ip;
if (frame_size < sizeof (struct EthernetHeader) + sizeof (struct IPv4Header))
{
fprintf (stderr,
"Malformed frame\n");
return;
}
memcpy (&ip,
&cframe[sizeof (struct EthernetHeader)],
sizeof (struct IPv4Header));
/* TODO: possibly do work here (ARP learning) */
route (ifc,
&ip,
&cframe[sizeof (struct EthernetHeader) + sizeof (struct IPv4Header)],
frame_size - sizeof (struct EthernetHeader) - sizeof (struct IPv4Header));
break;
}
case ETH_P_ARP:
{
struct ArpHeaderEthernetIPv4 ah;
if (frame_size < sizeof (struct EthernetHeader) + sizeof (struct ArpHeaderEthernetIPv4))
{
#if DEBUG
fprintf (stderr,
"Unsupported ARP frame\n");
#endif
return;
}
memcpy (&ah,
&cframe[sizeof (struct EthernetHeader)],
sizeof (struct ArpHeaderEthernetIPv4));
handle_arp (ifc,
&eh,
&ah);
break;
}
default:
#if DEBUG
fprintf (stderr,
"Unsupported Ethernet tag %04X\n",
ntohs (eh.tag));
#endif
return;
}
}
/**
* 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);
}
/**
* Find network interface by @a name.
*
* @param name name to look up by
* @return NULL if @a name was not found
*/
static struct Interface *
find_interface (const char *name)
{
for (unsigned int i=0;i<num_ifc;i++)
if (0 == strcasecmp (name,
gifc[i].name))
return &gifc[i];
return NULL;
}
/**
* The user entered an "arp" command. The remaining
* arguments can be obtained via 'strtok()'.
*/
static void
process_cmd_arp ()
{
const char *tok = strtok (NULL, " ");
struct in_addr v4;
struct MacAddress mac;
struct Interface *ifc;
if (NULL == tok)
{
// print_arp_cache ();
return;
}
if (1 !=
inet_pton (AF_INET,
tok,
&v4))
{
fprintf (stderr,
"`%s' is not a valid IPv4 address\n",
tok);
return;
}
tok = strtok (NULL, " ");
if (NULL == tok)
{
fprintf (stderr,
"No network interface provided\n");
return;
}
ifc = find_interface (tok);
if (NULL == ifc)
{
fprintf (stderr,
"Interface `%s' unknown\n",
tok);
return;
}
/* TODO: do MAC lookup */
}
/**
* Parse network specification in @a net, initializing @a network and @a netmask.
* Format of @a net is "IP/NETMASK".
*
* @param network[out] network specification to initialize
* @param netmask[out] netmask specification to initialize
* @param arg interface specification to parse
* @return 0 on success
*/
static int
parse_network (struct in_addr *network,
struct in_addr *netmask,
const char *net)
{
const char *tok;
char *ip;
unsigned int mask;
tok = strchr (net, '/');
if (NULL == tok)
{
fprintf (stderr,
"Error in network specification: lacks '/'\n");
return 1;
}
ip = strndup (net,
tok - net);
if (1 !=
inet_pton (AF_INET,
ip,
network))
{
fprintf (stderr,
"IP address `%s' malformed\n",
ip);
free (ip);
return 1;
}
free (ip);
tok++;
if (1 !=
sscanf (tok,
"%u",
&mask))
{
fprintf (stderr,
"Netmask `%s' malformed\n",
tok);
return 1;
}
if (mask > 32)
{
fprintf (stderr,
"Netmask invalid (too large)\n");
return 1;
}
netmask->s_addr = htonl (~ (uint32_t) ((1LLU << (32 - mask)) - 1LLU));
return 0;
}
/**
* Parse route from arguments in strtok() buffer.
*
* @param target_network[out] set to target network
* @param target_netmask[out] set to target netmask
* @param next_hop[out] set to next hop
* @param ifc[out] set to target interface
*/
static int
parse_route (struct in_addr *target_network,
struct in_addr *target_netmask,
struct in_addr *next_hop,
struct Interface **ifc)
{
char *tok;
tok = strtok (NULL, " ");
if ( (NULL == tok) ||
(0 != parse_network (target_network,
target_netmask,
tok)) )
{
fprintf (stderr,
"Expected network specification, not `%s'\n",
tok);
return 1;
}
tok = strtok (NULL, " ");
if ( (NULL == tok) ||
(0 != strcasecmp ("via",
tok)))
{
fprintf (stderr,
"Expected `via', not `%s'\n",
tok);
return 1;
}
tok = strtok (NULL, " ");
if ( (NULL == tok) ||
(1 != inet_pton (AF_INET,
tok,
next_hop)) )
{
fprintf (stderr,
"Expected next hop, not `%s'\n",
tok);
return 1;
}
tok = strtok (NULL, " ");
if ( (NULL == tok) ||
(0 != strcasecmp ("dev",
tok)))
{
fprintf (stderr,
"Expected `dev', not `%s'\n",
tok);
return 1;
}
tok = strtok (NULL, " ");
*ifc = find_interface (tok);
if (NULL == *ifc)
{
fprintf (stderr,
"Interface `%s' unknown\n",
tok);
return 1;
}
return 0;
}
/**
* Add a route.
*/
static void
process_cmd_route_add ()
{
struct in_addr target_network;
struct in_addr target_netmask;
struct in_addr next_hop;
struct Interface *ifc;
if (0 != parse_route (&target_network,
&target_netmask,
&next_hop,
&ifc))
return;
/* TODO: Add routing table entry */
}
/**
* Delete a route.
*/
static void
process_cmd_route_del ()
{
struct in_addr target_network;
struct in_addr target_netmask;
struct in_addr next_hop;
struct Interface *ifc;
if (0 != parse_route (&target_network,
&target_netmask,
&next_hop,
&ifc))
return;
/* TODO: Delete routing table entry */
}
/**
* Print out the routing table.
*/
static void
process_cmd_route_list ()
{
/* TODO: show routing table with 'print' */
}
/**
* The user entered a "route" command. The remaining
* arguments can be obtained via 'strtok()'.
*/
static void
process_cmd_route ()
{
char *subcommand = strtok (NULL, " ");
if (NULL == subcommand)
subcommand = "list";
if (0 == strcasecmp ("add",
subcommand))
process_cmd_route_add ();
else if (0 == strcasecmp ("del",
subcommand))
process_cmd_route_del ();
else if (0 == strcasecmp ("list",
subcommand))
process_cmd_route_list ();
else
fprintf (stderr,
"Subcommand `%s' not understood\n",
subcommand);
}
/**
* Parse network specification in @a net, initializing @a ifc.
* Format of @a net is "IPV4:IP/NETMASK".
*
* @param ifc[out] interface specification to initialize
* @param arg interface specification to parse
* @return 0 on success
*/
static int
parse_network_arg (struct Interface *ifc,
const char *net)
{
if (0 !=
strncasecmp (net,
"IPV4:",
strlen ("IPV4:")))
{
fprintf (stderr,
"Interface specification `%s' does not start with `IPV4:'\n",
net);
return 1;
}
net += strlen ("IPV4:");
return parse_network (&ifc->ip,
&ifc->netmask,
net);
}
/**
* Parse interface specification @a arg and update @a ifc. Format is
* "IFCNAME[IPV4:IP/NETMASK]=MTU". The "=MTU" is optional.
*
* @param ifc[out] interface specification to initialize
* @param arg interface specification to parse
* @return 0 on success
*/
static int
parse_cmd_arg (struct Interface *ifc,
const char *arg)
{
const char *tok;
char *nspec;
ifc->mtu = 1500; /* default in case unspecified */
tok = strchr (arg, '[');
if (NULL == tok)
{
fprintf (stderr,
"Error in interface specification: lacks '['");
return 1;
}
ifc->name = strndup (arg,
tok - arg);
arg = tok + 1;
tok = strchr (arg, ']');
if (NULL == tok)
{
fprintf (stderr,
"Error in interface specification: lacks ']'");
return 1;
}
nspec = strndup (arg,
tok - arg);
if (0 !=
parse_network_arg (ifc,
nspec))
{
free (nspec);
return 1;
}
free (nspec);
arg = tok + 1;
if ('=' == arg[0])
{
unsigned int mtu;
if (1 != (sscanf (&arg[1],
"%u",
&mtu)))
{
fprintf (stderr,
"Error in interface specification: MTU not a number\n");
return 1;
}
if (mtu < 400)
{
fprintf (stderr,
"Error in interface specification: MTU too small\n");
return 1;
}
if (mtu > UINT16_MAX)
{
fprintf (stderr,
"Error in interface specification: MTU too large\n");
return 1;
}
ifc->mtu = mtu;
#if DEBUG
fprintf (stderr,
"Interface %s has MTU %u\n",
ifc->name,
(unsigned int) ifc->mtu);
#endif
}
return 0;
}
/**
* 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)
{
const char *tok;
cmd[cmd_len - 1] = '\0';
tok = strtok (cmd,
" ");
if (NULL == tok)
return;
if (0 == strcasecmp (tok,
"arp"))
process_cmd_arp ();
else if (0 == strcasecmp (tok,
"route"))
process_cmd_route ();
else
fprintf (stderr,
"Unsupported command `%s'\n",
tok);
}
/**
* 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;
}
#include "loop.c"
/**
* Launches the router.
*
* @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];
memset (ifc,
0,
sizeof (ifc));
num_ifc = argc - 1;
gifc = ifc;
for (unsigned int i=1;i<argc;i++)
{
struct Interface *p = &ifc[i-1];
ifc[i-1].ifc_num = i;
if (0 !=
parse_cmd_arg (p,
argv[i]))
abort ();
}
loop ();
for (unsigned int i=1;i<argc;i++)
free (ifc[i-1].name);
return 0;
}