6in4.c 8.6 KB

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