6in4.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * 6in4 - tunnel client for automatic 6to4 or configured v6-in-v4 tunnels
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <ip.h>
  7. enum {
  8. IP_IPV6PROTO = 41,
  9. };
  10. int anysender;
  11. int gateway;
  12. uchar local6[IPaddrlen];
  13. uchar remote6[IPaddrlen];
  14. uchar remote4[IPaddrlen];
  15. uchar localmask[IPaddrlen];
  16. uchar localnet[IPaddrlen];
  17. uchar myip[IPaddrlen];
  18. /* magic anycast address from rfc3068 */
  19. uchar anycast6to4[IPv4addrlen] = { 192, 88, 99, 1 };
  20. static char *net = "/net";
  21. static int badipv4(uchar*);
  22. static int badipv6(uchar*);
  23. static void ip2tunnel(int, int);
  24. static void tunnel2ip(int, int);
  25. static void
  26. usage(void)
  27. {
  28. fprint(2, "Usage: %s [-ag] [-x mtpt] local6[/mask] [remote4 [remote6]]\n",
  29. argv0);
  30. exits("Usage");
  31. }
  32. void
  33. main(int argc, char **argv)
  34. {
  35. int n, tunnel, ifc, cfd;
  36. char *p, *cl, *ir;
  37. char buf[128], path[64];
  38. fmtinstall('I', eipfmt);
  39. fmtinstall('V', eipfmt);
  40. fmtinstall('M', eipfmt);
  41. ARGBEGIN {
  42. case 'a':
  43. anysender++;
  44. break;
  45. case 'g':
  46. gateway++;
  47. break;
  48. case 'x':
  49. net = EARGF(usage());
  50. break;
  51. default:
  52. usage();
  53. } ARGEND
  54. if (argc < 1)
  55. usage();
  56. /* local v6 address (mask defaults to /128) */
  57. memcpy(localmask, IPallbits, sizeof localmask);
  58. if ((p = strchr(argv[0], '/')) != nil) {
  59. parseipmask(localmask, p);
  60. *p = 0;
  61. }
  62. parseip(local6, argv[0]);
  63. if (isv4(local6))
  64. usage();
  65. argv++;
  66. argc--;
  67. if (argc >= 1 && argv[0][0] == '/') {
  68. parseipmask(localmask, argv[0]);
  69. argv++;
  70. argc--;
  71. }
  72. /* remote v4 address (defaults to anycast 6to4) */
  73. if (argc >= 1) {
  74. parseip(remote4, argv[0]);
  75. if (!isv4(remote4))
  76. usage();
  77. argv++;
  78. argc--;
  79. } else {
  80. v4tov6(remote4, anycast6to4);
  81. anysender++;
  82. }
  83. /* remote v6 address (defaults to link-local w/ v4 as interface part) */
  84. if (argc >= 1) {
  85. parseip(remote6, argv[0]);
  86. if (isv4(remote4))
  87. usage();
  88. argv++;
  89. argc--;
  90. } else {
  91. remote6[0] = 0xFE; /* link local */
  92. remote6[1] = 0x80;
  93. memcpy(remote6 + IPv4off, remote4 + IPv4off, IPv4addrlen);
  94. }
  95. USED(argv);
  96. if (argc != 0)
  97. usage();
  98. maskip(local6, localmask, localnet);
  99. if (myipaddr(myip, net) < 0)
  100. sysfatal("can't find my ipv4 address on %s", net);
  101. /*
  102. * open IPv6-in-IPv4 tunnel
  103. */
  104. p = seprint(buf, buf + sizeof buf, "ipmux!proto=%2.2x;dst=%V",
  105. IP_IPV6PROTO, myip + IPv4off);
  106. if (!anysender)
  107. seprint(p, buf + sizeof buf, ";src=%V", remote4 + IPv4off);
  108. tunnel = dial(buf, 0, 0, 0);
  109. if (tunnel < 0)
  110. sysfatal("can't make 6in4 tunnel with dial str %s: %r", buf);
  111. /*
  112. * open local IPv6 interface (as a packet interface)
  113. */
  114. cl = smprint("%s/ipifc/clone", net);
  115. cfd = open(cl, ORDWR); /* allocate a conversation */
  116. free(cl);
  117. n = 0;
  118. if (cfd < 0 || (n = read(cfd, buf, sizeof buf - 1)) <= 0)
  119. sysfatal("can't make packet interface: %r");
  120. buf[n] = 0;
  121. snprint(path, sizeof path, "%s/ipifc/%s/data", net, buf);
  122. ifc = open(path, ORDWR);
  123. if (ifc < 0 || fprint(cfd, "bind pkt") < 0)
  124. sysfatal("can't bind packet interface: %r");
  125. /* 1280 is MTU, apparently from rfc2460 */
  126. if (fprint(cfd, "add %I /128 %I 1280", local6, remote6) <= 0)
  127. sysfatal("can't set local ipv6 address: %r");
  128. close(cfd);
  129. if (gateway) {
  130. /* route global addresses through the tunnel to remote6 */
  131. ir = smprint("%s/iproute", net);
  132. cfd = open(ir, OWRITE);
  133. free(ir);
  134. if (cfd < 0 || fprint(cfd, "add 2000:: /3 %I", remote6) <= 0)
  135. sysfatal("can't set default global route: %r");
  136. }
  137. switch (rfork(RFPROC|RFNOWAIT|RFMEM)) {
  138. case -1:
  139. sysfatal("rfork");
  140. case 0:
  141. ip2tunnel(ifc, tunnel);
  142. break;
  143. default:
  144. tunnel2ip(tunnel, ifc);
  145. break;
  146. }
  147. exits("tunnel gone");
  148. }
  149. typedef struct Iphdr Iphdr;
  150. typedef struct Ip6hdr Ip6hdr;
  151. struct Iphdr
  152. {
  153. uchar vihl; /* Version and header length */
  154. uchar tos; /* Type of service */
  155. uchar length[2]; /* packet length */
  156. uchar id[2]; /* Identification */
  157. uchar frag[2]; /* Fragment information */
  158. uchar ttl; /* Time to live */
  159. uchar proto; /* Protocol */
  160. uchar cksum[2]; /* Header checksum */
  161. uchar src[4]; /* Ip source (uchar ordering unimportant) */
  162. uchar dst[4]; /* Ip destination (uchar ordering unimportant) */
  163. };
  164. struct Ip6hdr {
  165. uchar vcf[4]; /* version:4, traffic class:8, flow label:20 */
  166. uchar ploadlen[2]; /* payload length: packet length - 40 */
  167. uchar proto; /* next header type */
  168. uchar ttl; /* hop limit */
  169. uchar src[IPaddrlen];
  170. uchar dst[IPaddrlen];
  171. };
  172. #define STFHDR (sizeof(Iphdr))
  173. static void
  174. ip2tunnel(int in, int out)
  175. {
  176. int n, m;
  177. char buf[64*1024];
  178. Iphdr *op;
  179. Ip6hdr *ip;
  180. op = (Iphdr*)buf;
  181. op->vihl = 0x45; /* v4, hdr is 5 longs? */
  182. memcpy(op->src, myip + IPv4off, sizeof op->src);
  183. op->proto = IP_IPV6PROTO;
  184. op->ttl = 100;
  185. /* get a V6 packet destined for the tunnel */
  186. while ((n = read(in, buf + STFHDR, sizeof buf - STFHDR)) > 0) {
  187. /* if not IPV6, drop it */
  188. ip = (Ip6hdr*)(buf + STFHDR);
  189. if ((ip->vcf[0]&0xF0) != 0x60)
  190. continue;
  191. /* check length: drop if too short, trim if too long */
  192. m = nhgets(ip->ploadlen) + sizeof(Ip6hdr);
  193. if (m > n)
  194. continue;
  195. if (m < n)
  196. n = m;
  197. /* drop if v6 source or destination address is naughty */
  198. if (badipv6(ip->src) ||
  199. (!equivip6(ip->dst, remote6) && badipv6(ip->dst))) {
  200. syslog(0, "6in4", "egress filtered %I -> %I",
  201. ip->src, ip->dst);
  202. continue;
  203. }
  204. /* send 6to4 packets (2002::) directly to ipv4 target */
  205. if (ip->dst[0] == 0x20 && ip->dst[1] == 0x02)
  206. memcpy(op->dst, ip->dst+2, sizeof op->dst);
  207. else
  208. memcpy(op->dst, remote4+IPv4off, sizeof op->dst);
  209. n += STFHDR;
  210. /* pass packet to the other end of the tunnel */
  211. if (write(out, op, n) != n) {
  212. syslog(0, "6in4", "error writing to tunnel (%r), giving up");
  213. break;
  214. }
  215. }
  216. }
  217. static void
  218. tunnel2ip(int in, int out)
  219. {
  220. int n, m;
  221. char buf[64*1024];
  222. uchar a[IPaddrlen];
  223. Ip6hdr *op;
  224. Iphdr *ip;
  225. for (;;) {
  226. /* get a packet from the tunnel */
  227. n = read(in, buf, sizeof buf);
  228. ip = (Iphdr*)(buf + IPaddrlen);
  229. n -= IPaddrlen;
  230. if (n <= 0) {
  231. syslog(0, "6in4", "error reading from tunnel (%r), giving up");
  232. break;
  233. }
  234. /* if not IPv4 nor IP protocol IPv6, drop it */
  235. if ((ip->vihl&0xF0) != 0x40 || ip->proto != IP_IPV6PROTO)
  236. continue;
  237. /* check length: drop if too short, trim if too long */
  238. m = nhgets(ip->length);
  239. if (m > n)
  240. continue;
  241. if (m < n)
  242. n = m;
  243. op = (Ip6hdr*)(buf + IPaddrlen + STFHDR);
  244. n -= STFHDR;
  245. /* don't relay: just accept packets for local host/subnet */
  246. /* (this blocks link-local and multicast addresses as well) */
  247. maskip(op->dst, localmask, a);
  248. if (!equivip6(a, localnet)) {
  249. syslog(0, "6in4", "ingress filtered %I -> %I",
  250. op->src, op->dst);
  251. continue;
  252. }
  253. /* pass V6 packet to the interface */
  254. write(out, op, n);
  255. }
  256. }
  257. static int
  258. badipv4(uchar *a)
  259. {
  260. switch (a[0]) {
  261. case 0: /* unassigned */
  262. case 10: /* private */
  263. case 127: /* loopback */
  264. return 1;
  265. case 172:
  266. return a[1] >= 16; /* 172.16.0.0/12 private */
  267. case 192:
  268. return a[1] == 168; /* 192.168.0.0/16 private */
  269. case 169:
  270. return a[1] == 254; /* 169.254.0.0/16 DHCP link-local */
  271. }
  272. /* 224.0.0.0/4 multicast, 240.0.0.0/4 reserved, broadcast */
  273. return a[0] >= 240;
  274. }
  275. static int
  276. badipv6(uchar *a)
  277. {
  278. int h = a[0]<<8 | a[1];
  279. if (h == 0 || /* compatible, mapped, loopback, unspecified ... */
  280. h >= 0xFE80) /* multicast, link-local or site-local */
  281. return 1;
  282. if (h == 0x2002 && /* 6to4 address */
  283. badipv4(a+2))
  284. return 1;
  285. return 0;
  286. }