forked from vanrein/6bed4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.c
866 lines (791 loc) · 26.6 KB
/
router.c
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
/* 6bed4/router.c -- traffic forwarding daemon for public TSP service
*
* This is an implementation of the profile that makes TSP service publicly
* usable, that is without authentication. However to avoid abuse of such
* a service, it is not anonymous -- IPv6 addresses contain the IPv4 address
* and port.
*
* This is an implementation of neighbour and router discovery over a
* tunnel that packs IPv6 inside UDP/IPv4. This tunnel mechanism is
* targeted specifically at embedded devices that are to function on
* any network, including IPv4-only, while being designed as IPv6-only
* devices with a fallback to this tunnel.
*
* Interestingly, as a side-effect of this design the router daemon can be
* stateless. Any further requirements that are stateful are most likely
* filtering, and that can be solved in stateful firewall configuration.
*
* The intention of TSP is to enable IPv4-only hosts to connecto to
* IPv6 services; the public TSP profile adds to that the ability to
* do it in a temporary manner.
*
* TODO: Should we translate ICMPv4 --> ICMPv6?
*
* From: Rick van Rein <[email protected]>
*/
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <netinet/icmp6.h>
#include <arpa/inet.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <linux/if_ether.h>
/* The following will initially fail, due to an IANA obligation to avoid
* default builds with non-standard options.
*/
#include "nonstd.h"
#define MTU 1280
/*
* The HAVE_SETUP_TUNNEL variable is used to determine whether absense of
* the -t option leads to an error, or to an attempt to setup the tunnel.
* The setup_tunnel() function used for that is defined per platform, such
* as for LINUX. Remember to maintain the manpage's optionality for -t.
*/
#undef HAVE_SETUP_TUNNEL
/* Global variables */
char *program;
int v4sox = -1;
int v6sox = -1;
char *v4server = NULL;
char *v6server = NULL;
char *v6prefix = NULL;
const uint8_t v6listen_linklocal [16] = { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t v6listen_linklocal_complete [16] = { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t lladdr_6bed4 [6];
struct sockaddr_in v4name;
struct sockaddr_in6 v6name;
struct in6_addr v6listen;
struct in6_addr v6listen_complete;
struct in_addr v4listen;
struct {
struct tun_pi tun;
union {
struct {
struct ip6_hdr v6hdr;
uint8_t data [MTU - sizeof (struct ip6_hdr)];
} idata;
struct {
struct ip6_hdr v6hdr;
struct icmp6_hdr v6icmphdr;
} ndata;
} udata;
} v4data6;
#define v4tunpi6 ( v4data6.tun)
#define v4data ((uint8_t *) &v4data6.udata)
#define v4hdr6 (&v4data6.udata.idata.v6hdr)
#define v4src6 (&v4data6.udata.idata.v6hdr.ip6_src)
#define v4dst6 (&v4data6.udata.idata.v6hdr.ip6_dst)
#define v4v6plen ( v4data6.udata.ndata.v6hdr.ip6_plen)
#define v4v6nexthdr ( v4data6.udata.ndata.v6hdr.ip6_nxt)
#define v4v6hoplimit ( v4data6.udata.ndata.v6hdr.ip6_hops)
#define v4icmp6 (&v4data6.udata.ndata.v6icmphdr)
#define v4v6icmpdata ( v4data6.udata.ndata.v6icmphdr.icmp6_data8)
#define v4v6icmptype ( v4data6.udata.ndata.v6icmphdr.icmp6_type)
#define v4v6icmpcode ( v4data6.udata.ndata.v6icmphdr.icmp6_code)
#define v4v6icmpcksum ( v4data6.udata.ndata.v6icmphdr.icmp6_cksum)
#define v4ngbsoltarget (&v4data6.udata.ndata.v6icmphdr.icmp6_data8 [4])
struct {
struct tun_pi tun;
union {
uint8_t data [MTU];
struct ip6_hdr v6hdr;
struct icmp6_hdr v6icmp;
} udata;
uint8_t zero;
} v6data6;
#define v6tuncmd ( v6data6.tun)
#define v6data ( v6data6.udata.data)
#define v6hdr6 (&v6data6.udata.v6hdr)
#define v6src6 (&v6data6.udata.v6hdr.ip6_src)
#define v6dst6 (&v6data6.udata.v6hdr.ip6_dst)
#define v6hoplimit ( v6data6.udata.v6hdr.ip6_hops)
#define v6nexthdr ( v6data6.udata.v6hdr.ip6_nxt)
#define v6icmptype ( v6data6.udata.v6icmp.icmp6_type)
uint8_t router_linklocal_address [] = {
0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0x00,
};
uint8_t democlient_linklocal_address [] = {
0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0x01,
};
uint8_t allnodes_linklocal_address [] = {
0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0x01,
};
uint8_t allrouters_linklocal_address [] = {
0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0x02,
};
/*
*
* Driver routines
*
*/
#ifdef LINUX
#define HAVE_SETUP_TUNNEL
/* Implement the setup_tunnel() command for Linux.
* Return 1 on success, 0 on failure.
*/
int setup_tunnel (void) {
v6sox = open ("/dev/net/tun", O_RDWR);
if (v6sox == -1) {
fprintf (stderr, "%s: Failed to access tunnel driver on /dev/net/tun: %s\n", program, strerror (errno));
return 0;
}
int ok = 1;
struct ifreq ifreq;
memset (&ifreq, 0, sizeof (ifreq));
strncpy (ifreq.ifr_name, "6bed4_rfc", IFNAMSIZ);
ifreq.ifr_flags = IFF_TUN;
if (ok && (ioctl (v6sox, TUNSETIFF, (void *) &ifreq) == -1)) {
ok = 0;
}
ifreq.ifr_name [IFNAMSIZ] = 0;
char cmd [512+1];
snprintf (cmd, 512, "/sbin/ip -6 addr flush dev %s", ifreq.ifr_name);
if (ok && system (cmd) != 0) {
ok = 0;
}
snprintf (cmd, 512, "/sbin/ip addr add fe80::0 dev %s scope link", ifreq.ifr_name);
if (ok && system (cmd) != 0) {
ok = 0;
}
snprintf (cmd, 512, "/sbin/ip -6 addr add %s dev %s", v6prefix, ifreq.ifr_name);
if (ok && system (cmd) != 0) {
ok = 0;
}
snprintf (cmd, 512, "/sbin/ip link set %s up mtu 1280", ifreq.ifr_name);
if (ok && system (cmd) != 0) {
ok = 0;
}
if (!ok) {
close (v6sox); /* This removes the tunnel interface */
}
return ok;
}
#endif /* LINUX */
/*
*
* Utility functions
*
*/
/* Calculate the ICMPv6 checksum field
*/
uint16_t icmp6_checksum (size_t payloadlen) {
uint16_t plenword = htons (payloadlen);
uint16_t nxthword = htons (IPPROTO_ICMPV6);
uint16_t *area [] = { (uint16_t *) v4src6, (uint16_t *) v4dst6, &plenword, &nxthword, (uint16_t *) v4icmp6, (uint16_t *) v4v6icmpdata };
uint8_t areawords [] = { 8, 8, 1, 1, 1, payloadlen/2 - 2 };
uint32_t csum = 0;
u_int8_t i, j;
for (i=0; i < 6; i++) {
for (j=0; j<areawords [i]; j++) {
csum += ntohs (area [i] [j]);
}
}
csum = (csum & 0xffff) + (csum >> 16);
csum = (csum & 0xffff) + (csum >> 16);
csum = htons (~csum);
return csum;
}
/* Send an ICMPv6 reply. This is constructed at the tunnel end, from
* the incoming message. The parameter indicates how many bytes the
* ICMPv6 package counts after the ICMPv6 header. It must be 4 (mod 8).
*
* Actions: v4/udp src becomes dest, set v4/udp/v6 src, len, cksum, send.
* v6 dest is provided (usually v4src6) but if it starts with
* 0x00,0x00 it will be replaced with allnodes_linklocal_address.
*/
void icmp6_reply (size_t icmp6bodylen, struct in6_addr *dest) {
v4v6hoplimit = 255;
if ((icmp6bodylen & 0x07) != 4) {
return; /* illegal length, drop */
}
v4v6plen = htons (icmp6bodylen + 4);
if ((* (uint16_t *) dest) == htons (0x0000)) {
memcpy (v4dst6, allnodes_linklocal_address, 16);
} else {
memcpy (v4dst6, dest, 16);
}
memcpy (v4src6, router_linklocal_address, 16);
v4v6icmpcksum = icmp6_checksum (ntohs (v4v6plen));
//
// Send the message to the IPv4 originator port
printf ("Sending ICMPv6-IPv6-UDP-IPv4 to %d.%d.%d.%d:%d, result = %d\n",
((uint8_t *) &v4name.sin_addr.s_addr) [0],
((uint8_t *) &v4name.sin_addr.s_addr) [1],
((uint8_t *) &v4name.sin_addr.s_addr) [2],
((uint8_t *) &v4name.sin_addr.s_addr) [3],
ntohs (v4name.sin_port),
sendto (v4sox,
v4data,
sizeof (struct ip6_hdr) + 4 + icmp6bodylen,
MSG_DONTWAIT,
(struct sockaddr *) &v4name, sizeof (v4name)));
}
/* Append the current prefix to an ICMPv6 message. Incoming optidx
* and return values signify original and new offset for ICMPv6 options.
* The endlife parameter must be set to obtain zero lifetimes, thus
* instructing the tunnel client to stop using an invalid prefix.
*/
size_t icmp6_prefix (size_t optidx, uint8_t endlife) {
v4v6icmpdata [optidx++] = 3; // Type
v4v6icmpdata [optidx++] = 4; // Length
v4v6icmpdata [optidx++] = 64; // This is a /64 prefix
v4v6icmpdata [optidx++] = 0xc0; // L=1, A=1, Reserved1=0
memset (v4v6icmpdata + optidx, endlife? 0x00: 0xff, 8);
optidx += 8;
// Valid Lifetime: Zero / Infinite
// Preferred Lifetime: Zero / Infinite
memset (v4v6icmpdata + optidx, 0, 4);
optidx += 4;
// Reserved2=0
memcpy (v4v6icmpdata + optidx + 0, &v6listen, 8);
memset (v4v6icmpdata + optidx + 8, 0, 8);
optidx += 16;
return optidx;
}
/*
* TODO: DEPRECATED
*
* Append a Destination Link-Layer Address Option to an ICMPv6
* message. The address is comprised from the remote's UDP port
* and IPv4 address, as seen by the router. They are supplied
* in that order, in network byte order. The resulting address
* is 6 bytes, but even though that makes it look like a MAC
* address, it really is another beast.
* Note that no effort is made in the router to recognise the
* "illegal port number" 0x3333 -- the client needs a message
* and will recognise it and act on it.
*/
size_t icmp6_dest_linkaddr (size_t optidx) {
uint8_t typelen [2] = { ND_OPT_DESTINATION_LINKADDR, 1 };
memcpy (v4v6icmpdata + optidx + 0, &typelen, 2);
v4v6icmpdata [optidx + 2] = ntohs (v4name.sin_port) % 256;
v4v6icmpdata [optidx + 3] = ntohs (v4name.sin_port) / 256;
memcpy (v4v6icmpdata + optidx + 4, &v4name.sin_addr, 4);
optidx += 8;
return optidx;
}
/*
* Test if the provided IPv6 address matches the prefix used for 6bed4.
*/
static inline bool prefix_6bed4 (struct in6_addr *ip6) {
return memcmp (&v6listen, ip6->s6_addr, 8) == 0;
}
/*
* Validate the originator's IPv6 address. It should match the
* UDP/IPv4 coordinates of the receiving 6bed4 socket. Also,
* the /64 prefix must match that of v6listen.
*/
bool validate_originator (struct sockaddr_in *sin, struct in6_addr *ip6) {
uint16_t port = ntohs (sin->sin_port);
uint32_t addr = ntohl (sin->sin_addr.s_addr);
if (!prefix_6bed4 (ip6)) {
return false;
}
if ((port % 256) != (ip6->s6_addr [8] ^ 0x02)) {
return false;
}
if ((port / 256) != ip6->s6_addr [9]) {
return false;
}
if ((addr >> 24) != ip6->s6_addr [10]) {
return false;
}
if ((addr & 0x00ffffff) != (htonl (ip6->s6_addr32 [3]) & 0x00ffffff)) {
return false;
}
return true;
}
/*
* Major packet processing functions
*/
/*
* Respond to a Router Solicitation received over the 6bed4 Network.
*/
void handle_6bed4_router_solicit (void) {
struct in6_addr observed;
v4v6icmptype = ND_ROUTER_ADVERT;
v4v6icmpdata [0] = 0; // Cur Hop Limit: unspec
v4v6icmpdata [1] = 0x18; // M=0, O=0
// H=0, Prf=11=Low
// Reserved=0
// TODO: wire says 0x44 for router_adv.flags
size_t writepos = 2;
memset (v4v6icmpdata+writepos, 0xff, 2+4+4);
// Router Lifetime: max, 18.2h
// Reachable Time: max
// Retrans Timer: max
writepos += 2+4+4;
writepos = icmp6_prefix (writepos, 0);
//TODO:DEPRECATED// writepos = icmp6_dest_linkaddr (writepos);
memcpy (&observed, v6listen_linklocal, 8);
observed.s6_addr [8] = htons (v4name.sin_port) % 256 ^ 0x02;
observed.s6_addr [9] = htons (v4name.sin_port) / 256;
memcpy (&observed.s6_addr32 [3], &v4name.sin_addr, 4);
observed.s6_addr [10] = observed.s6_addr [12];
observed.s6_addr [11] = 0xff;
observed.s6_addr [12] = 0xfe;
icmp6_reply (writepos, &observed);
}
/* Handle the IPv4 message pointed at by msg as a neighbouring command.
*
* Type Code ICMPv6 meaning Handling
* ---- ---- ----------------------------- ----------------------------
* 133 0 Router Solicitation Send Router Advertisement
* 134 0 Router Advertisement Ignore
* 135 0 Neighbour Solicitation Send Neighbour Advertisement
* 136 0 Neighbour Advertisement Ignore
* 137 0 Redirect Ignore
*/
void handle_4to6_nd (ssize_t v4ngbcmdlen) {
uint16_t srclinklayer;
if (v4ngbcmdlen < sizeof (struct ip6_hdr) + sizeof (struct icmp6_hdr)) {
return;
}
if (v4v6icmpcode != 0) {
return;
}
if (icmp6_checksum (v4ngbcmdlen - sizeof (struct ip6_hdr)) != v4v6icmpcksum) {
return;
}
//
// Approved. Perform neighbourly courtesy.
switch (v4v6icmptype) {
case ND_ROUTER_SOLICIT:
//
// Validate Router Solicitation
srclinklayer = 0;
if (v4ngbcmdlen == sizeof (struct ip6_hdr) + 8 + 8) {
// One option is possible, the source link-layer address
if (v4v6icmpdata [4] != 1 || v4v6icmpdata [5] != 1) {
break; /* bad opton, ignore */
}
// The source link-layer address is end-aligned
srclinklayer = (v4v6icmpdata [10] << 8) | v4v6icmpdata [11];
if (srclinklayer == 0) {
break; /* illegal, ignore */
}
} else if (v4ngbcmdlen == sizeof (struct ip6_hdr) + 8) {
srclinklayer = 0; /* set for latter filling */
} else {
break; /* illegal length, drop */
}
//
// Having accepted the Router Advertisement, respond
handle_6bed4_router_solicit ();
break;
case ND_NEIGHBOR_SOLICIT:
//
// Validate Neigbour Solicitation
if (!validate_originator (&v4name, v4src6)) {
break; /* bad source address, drop */
}
if ((v4ngbcmdlen != sizeof (struct ip6_hdr) + 24) &&
(v4ngbcmdlen != sizeof (struct ip6_hdr) + 24 + 8)) {
break; /* funny length, drop */
}
if ((memcmp (v4ngbsoltarget, v6listen_linklocal, 16) != 0) &&
(memcmp (v4ngbsoltarget, v6listen_linklocal_complete, 16) != 0) &&
(memcmp (v4ngbsoltarget, &v6listen_complete, 16) != 0)) {
break; /* target not known here, drop */
}
//
// Construct Neigbour Advertisement
v4v6icmptype = ND_NEIGHBOR_ADVERT;
v4v6icmpdata [0] = 0xc0;
v4v6icmpdata [1] =
v4v6icmpdata [2] =
v4v6icmpdata [3] = 0x00; // R=1, S=1, O=1, Reserved=0
memcpy (v4v6icmpdata + 4, &v6listen_complete, 16);
// Append option: the target link-layer address
// Note: wire does not include target link-layer address
v4v6icmpdata [20] = 2; // Type: Target Link-Layer Addr
v4v6icmpdata [21] = 1; // Length: 1x 8 bytes
memcpy (v4v6icmpdata + 22, lladdr_6bed4, 6);
icmp6_reply (28, v4src6); // 28 is the ICMPv6 response length
break;
case ND_ROUTER_ADVERT:
case ND_NEIGHBOR_ADVERT:
case ND_REDIRECT:
break; /* drop */
}
}
/*
* Forward a message received over the 6bed4 Network over IPv6.
* Note that existing checksums will work well, as only the
* Hop Limit has been altered, and this is not part of the
* checksum calculations.
*/
void handle_4to6_plain_unicast (ssize_t v4datalen) {
printf ("Writing IPv6, result = %d\n",
write (v6sox, &v4data6, sizeof (struct tun_pi) + v4datalen));
}
/*
* Forward a 6bed4 message to another 6bed4 destination address.
* Note that existing checksums will work well, as only the
* Hop Limit has been altered, and this is not part of the
* checksum calculations.
*/
void relay_6bed4_plain_unicast (ssize_t v4datalen, struct in6_addr *ip6) {
v4name.sin_port = htons (ip6->s6_addr [9] << 8 | ip6->s6_addr [8] ^ 0x02);
uint8_t *addr = (uint8_t *) &v4name.sin_addr.s_addr;
addr [0] = ip6->s6_addr [10];
memcpy (addr + 1, ip6->s6_addr + 13, 3);
printf ("Relaying over 6bed4 Network to %d.%d.%d.%d:%d, result = %d\n",
((uint8_t *) &v4name.sin_addr.s_addr) [0],
((uint8_t *) &v4name.sin_addr.s_addr) [1],
((uint8_t *) &v4name.sin_addr.s_addr) [2],
((uint8_t *) &v4name.sin_addr.s_addr) [3],
ntohs (v4name.sin_port),
sendto (v4sox,
v4data, v4datalen,
MSG_DONTWAIT,
(struct sockaddr *) &v4name, sizeof (v4name)));
}
/* Receive a tunnel package, and route it to either the handler for the
* tunnel protocol, or to the handler that checks and then unpacks the
* contained IPv6.
*/
void handle_4to6 (void) {
uint8_t buf [1501];
ssize_t buflen;
socklen_t adrlen = sizeof (v4name);
//
// Receive IPv4 package, which may be tunneled or a tunnel request
buflen = recvfrom (v4sox,
v4data, MTU,
MSG_DONTWAIT,
(struct sockaddr *) &v4name, &adrlen
);
if (buflen == -1) {
printf ("%s: Error receiving IPv4-side package: %s",
program, strerror (errno));
return;
}
if (buflen < sizeof (struct ip6_hdr)) {
return;
}
if ((v4data [0] & 0xf0) != 0x60) {
// Not an IPv6 packet
return;
}
//
// Handle the tunneled IPv6 package (dependent on its class)
if ((v4v6nexthdr == IPPROTO_ICMPV6) &&
(v4v6icmptype >= 133) && (v4v6icmptype <= 137)) {
//
// Not Plain: Router Adv/Sol, Neighbor Adv/Sol, Redirect
if (v4v6hoplimit != 255) {
return;
}
handle_4to6_nd (buflen);
} else if ((v4dst6->s6_addr [0] != 0xff) && !(v4dst6->s6_addr [8] & 0x01)) {
//
// Plain Unicast
if (validate_originator (&v4name, v4src6)) {
if (v4v6hoplimit-- <= 1) {
return;
}
if (prefix_6bed4 (v4dst6)) {
relay_6bed4_plain_unicast (buflen, v4dst6);
} else {
handle_4to6_plain_unicast (buflen);
}
} else if (prefix_6bed4 (v4src6)) {
// The sender must not have kept NAT/firewall holes
// open and should be instructed about a change in
// its 6bed4 Link-Local Address.
handle_6bed4_router_solicit ();
}
} else {
//
// Plain Multicast
//OPTIONAL// validate_originator, hoplimit, relay_mcast.
return;
}
}
/* Receive an IPv6 package, check its address and pickup IPv4 address and
* port, then package it as a tunnel message and forward it to IPv4:port.
*/
void handle_6to4 (void) {
//
// Receive the IPv6 package and ensure a consistent size
size_t rawlen = read (v6sox, &v6data6, sizeof (v6data6));
if (rawlen == -1) {
return; /* error reading, drop */
}
if (rawlen < sizeof (struct tun_pi) + sizeof (struct ip6_hdr) + 1) {
return; /* too small, drop */
}
if (v6tuncmd.proto != htons (ETH_P_IPV6)) {
return; /* no IPv6, drop */
}
if ((v6nexthdr == IPPROTO_ICMPV6) &&
(v6icmptype >= 133) && (v6icmptype <= 137)) {
return; /* not plain IPv6, drop */
}
if (v6hoplimit-- <= 1) {
// TODO: Send back an ICMPv6 error message
return; /* hop limit exceeded, drop */
}
if ((v6dst6->s6_addr [0] == 0xff) /* TODO:UDP_PORT_NOT_YET_FORCED_TO_EVEN || (v6dst6->s6_addr [8] & 0x01) */ ) {
//OPTIONAL// handle_6to4_plain_multicast ()
return; /* multicast, drop */
}
printf ("Received plain unicast IPv6 data, flags=0x%04x, proto=0x%04x\n", v6tuncmd.flags, v6tuncmd.proto);
//
// Ensure that the incoming IPv6 address is properly formatted
// Note that this avoids access to ::1/128, fe80::/10, fec0::/10
if (memcmp (v6dst6, &v6listen, 8) != 0) {
return;
}
//
// Harvest socket address data from destination IPv6, then send
relay_6bed4_plain_unicast (rawlen - sizeof (struct tun_pi), v6dst6);
}
/* Run the daemon core code, passing information between IPv4 and IPv6 and
* responding to tunnel requests if so requested.
*/
void run_daemon (void) {
fd_set io;
FD_ZERO (&io);
FD_SET (v4sox, &io);
FD_SET (v6sox, &io);
int nfds = (v4sox < v6sox)? (v6sox + 1): (v4sox + 1);
while (1) {
select (nfds, &io, NULL, NULL, NULL);
if (FD_ISSET (v4sox, &io)) {
handle_4to6 ();
} else {
FD_SET (v4sox, &io);
}
if (FD_ISSET (v6sox, &io)) {
handle_6to4 ();
} else {
FD_SET (v6sox, &io);
}
fflush (stdout);
}
}
/* Option descriptive data structures */
char *short_opt = "l:L:t:h";
struct option long_opt [] = {
{ "v4listen", 1, NULL, 'l' },
{ "v6prefix", 1, NULL, 'L' },
{ "tundev", 1, NULL, 't' },
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 } /* Array termination */
};
/* Parse commandline arguments (and start to process them).
* Return 1 on success, 0 on failure.
*/
int process_args (int argc, char *argv []) {
int ok = 1;
int help = (argc == 1);
int done = 0;
while (!done) {
switch (getopt_long (argc, argv, short_opt, long_opt, NULL)) {
case -1:
done = 1;
if (optind != argc) {
fprintf (stderr, "%s: Extra arguments not permitted: %s...\n", program, argv [optind]);
ok = 0;
}
break;
case 'l':
if (v4sox != -1) {
ok = 0;
fprintf (stderr, "%s: Only one -l argument is permitted\n", program);
break;
}
v4server = optarg;
if (inet_pton (AF_INET, optarg, &v4name.sin_addr) <= 0) {
ok = 0;
fprintf (stderr, "%s: Failed to parse IPv4 address %s\n", program, optarg);
break;
}
memcpy (&v4listen, &v4name.sin_addr, 4);
v4sox = socket (AF_INET, SOCK_DGRAM, 0);
if (v4sox == -1) {
ok = 0;
fprintf (stderr, "%s: Failed to allocate UDPv4 socket: %s\n", program, strerror (errno));
break;
}
if (bind (v4sox, (struct sockaddr *) &v4name, sizeof (v4name)) != 0) {
ok = 0;
fprintf (stderr, "%s: Failed to bind to UDPv4 %s:%d: %s\n", program, optarg, ntohs (v4name.sin_port), strerror (errno));
break;
}
break;
case 'L':
if (v6server) {
ok = 0;
fprintf (stderr, "%s: Only one -L argument is permitted\n", program);
break;
}
char *slash64 = strchr (optarg, '/');
if (!slash64 || strcmp (slash64, "/64") != 0) {
ok = 0;
fprintf (stderr, "%s: The -L argument must be an explicit /64 prefix and not %s\n", program, slash64? slash64: "implied");
break;
}
*slash64 = 0;
v6server = strdup (optarg);
*slash64 = '/';
v6prefix = optarg;
if (!v6server || inet_pton (AF_INET6, v6server, &v6listen) <= 0) {
ok = 0;
fprintf (stderr, "%s: Failed to parse IPv6 prefix %s\n", program, optarg);
break;
}
if (v6listen.s6_addr32 [2] || v6listen.s6_addr32 [3]) {
ok = 0;
fprintf (stderr, "%s: IPv6 prefix contains bits beyond its /64 prefix: %s\n", program, optarg);
break;
}
break;
case 't':
if (v6sox != -1) {
ok = 0;
fprintf (stderr, "%s: Multiple -t arguments are not permitted\n", program);
break;
}
v6sox = open (optarg, O_RDWR);
if (v6sox == -1) {
ok = 0;
fprintf (stderr, "%s: Failed to open tunnel device %s: %s\n", program, optarg, strerror (errno));
break;
}
break;
default:
ok = 0;
help = 1;
/* continue into 'h' to produce usage information */
case 'h':
help = 1;
break;
}
if (help || !ok) {
done = 1;
}
}
if (help) {
#ifdef HAVE_SETUP_TUNNEL
fprintf (stderr, "Usage: %s [-t /dev/tunX] -l <v4server> -L <v6prefix>/64\n %s -h\n", program, program);
#else
fprintf (stderr, "Usage: %s -t /dev/tunX -l <v4server> -L <v6prefix>/64\n %s -h\n", program, program);
#endif
return ok;
}
if (!ok) {
return 0;
}
if (v4sox == -1) {
fprintf (stderr, "%s: Use -l to specify an IPv4 address for the tunnel interface\n", program);
return 0;
}
if (!v6server) {
fprintf (stderr, "%s: Use -L to specify a /64 prefix on the IPv6 side\n", program);
return 0;
}
#ifdef HAVE_SETUP_TUNNEL
if (v6sox == -1) {
if (geteuid () != 0) {
fprintf (stderr, "%s: You should be root, or use -t to specify an accessible tunnel device\n", program);
return 0;
}
ok = setup_tunnel ();
}
#else /* ! HAVE_SETUP_TUNNEL */
if (v6sox == -1) {
fprintf (stderr, "%s: You must specify a tunnel device with -t that is accessible to you\n", program);
return 0;
}
#endif /* HAVE_SETUP_TUNNEL */
return ok;
}
/* The main program parses commandline arguments and forks off the daemon
*/
int main (int argc, char *argv []) {
//
// Initialise
program = argv [0];
memset (&v4name, 0, sizeof (v4name));
memset (&v6name, 0, sizeof (v6name));
v4name.sin_family = AF_INET ;
v6name.sin6_family = AF_INET6;
v4name.sin_port = htons (UDP_PORT_6BED4); /* 6BED4 standard port */
v4tunpi6.flags = 0;
v4tunpi6.proto = htons (ETH_P_IPV6);
//
// Parse commandline arguments
if (!process_args (argc, argv)) {
exit (1);
}
//
// Setup a few addresses for later comparison/reproduction
// * lladdr_6bed4 is the 6bed4 Link-Local Address
// * v6listen_complete is the router's full IPv6 address (with if-id)
// * v6listen_linklocal_complete is fe80::/64 plus the router's if-id
// A few others have already been setup at this time
// * v6listen is the router's 6bed4 prefix ending in 64 zero bits
// * v6listen_linklocal is the address fe80::/128
//
lladdr_6bed4 [0] = UDP_PORT_6BED4 % 256;
lladdr_6bed4 [1] = UDP_PORT_6BED4 / 256;
memcpy (lladdr_6bed4 + 2, (uint8_t *) &v4name.sin_addr, 4);
memcpy (&v6listen_complete, &v6listen, 8);
v6listen_complete.s6_addr [8] = lladdr_6bed4 [0] ^ 0x02;
v6listen_complete.s6_addr [9] = lladdr_6bed4 [1];
v6listen_complete.s6_addr [10] = lladdr_6bed4 [2];
v6listen_complete.s6_addr [11] = 0xff;
v6listen_complete.s6_addr [12] = 0xfe;
memcpy (&v6listen_complete.s6_addr [13], lladdr_6bed4 + 3, 3);
memcpy (v6listen_linklocal_complete, v6listen_linklocal, 8);
memcpy (v6listen_linklocal_complete + 8, &v6listen_complete.s6_addr [8], 8);
printf ("LISTEN lladdr_6bed4 = %02x:%02x:%02x:%02x:%02x:%02x\n", lladdr_6bed4 [0], lladdr_6bed4 [1], lladdr_6bed4 [2], lladdr_6bed4 [3], lladdr_6bed4 [4], lladdr_6bed4 [5]);
printf ("LISTEN v6listen = %x:%x:%x:%x:%x:%x:%x:%x\n", htons (v6listen.s6_addr16 [0]), htons (v6listen.s6_addr16 [1]), htons (v6listen.s6_addr16 [2]), htons (v6listen.s6_addr16 [3]), htons (v6listen.s6_addr16 [4]), htons (v6listen.s6_addr16 [5]), htons (v6listen.s6_addr16 [6]), htons (v6listen.s6_addr16 [7]));
printf ("LISTEN v6listen_complete = %x:%x:%x:%x:%x:%x:%x:%x\n", htons (v6listen_complete.s6_addr16 [0]), htons (v6listen_complete.s6_addr16 [1]), htons (v6listen_complete.s6_addr16 [2]), htons (v6listen_complete.s6_addr16 [3]), htons (v6listen_complete.s6_addr16 [4]), htons (v6listen_complete.s6_addr16 [5]), htons (v6listen_complete.s6_addr16 [6]), htons (v6listen_complete.s6_addr16 [7]));
printf ("LISTEN v6listen_linklocal = %x:%x:%x:%x:%x:%x:%x:%x\n", htons (((uint16_t *) v6listen_linklocal) [0]), htons (((uint16_t *) v6listen_linklocal) [1]), htons (((uint16_t *) v6listen_linklocal) [2]), htons (((uint16_t *) v6listen_linklocal) [3]), htons (((uint16_t *) v6listen_linklocal) [4]), htons (((uint16_t *) v6listen_linklocal) [5]), htons (((uint16_t *) v6listen_linklocal) [6]), htons (((uint16_t *) v6listen_linklocal) [7]));
printf ("LISTEN v6listen_linklocal_complete = %x:%x:%x:%x:%x:%x:%x:%x\n", htons (((uint16_t *) v6listen_linklocal_complete) [0]), htons (((uint16_t *) v6listen_linklocal_complete) [1]), htons (((uint16_t *) v6listen_linklocal_complete) [2]), htons (((uint16_t *) v6listen_linklocal_complete) [3]), htons (((uint16_t *) v6listen_linklocal_complete) [4]), htons (((uint16_t *) v6listen_linklocal_complete) [5]), htons (((uint16_t *) v6listen_linklocal_complete) [6]), htons (((uint16_t *) v6listen_linklocal_complete) [7]));
//
// Start the main daemon process
#ifdef SKIP_TESTING_KLUDGE_IN_FOREGROUND
switch (fork ()) {
case -1: /* Error forking */
fprintf (stderr, "%s: Failed to fork: %s\n", program, strerror (errno));
exit (1);
case 0: /* Child process */
close (0);
//TODO: tmp.support for ^printf// close (1);
close (2);
setsid ();
run_daemon ();
break;
default: /* Parent process */
close (v4sox);
close (v6sox);
break;
}
#else
run_daemon ();
#endif
//
// Report successful creation of the daemon
return 0;
}