6in4.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. if (isv4(remote4))
  105. usage();
  106. } else {
  107. remote6[0] = 0xFE; /* link local */
  108. remote6[1] = 0x80;
  109. memcpy(remote6 + IPv4off, remote4 + IPv4off, IPv4addrlen);
  110. }
  111. USED(argv);
  112. if (argc != 0)
  113. usage();
  114. maskip(local6, localmask, localnet);
  115. if (debug)
  116. fprint(2, "localnet %I remote6 %I\n", localnet, remote6);
  117. }
  118. static void
  119. setup(int *v6net, int *tunp)
  120. {
  121. int n, cfd;
  122. char *p, *cl, *ir;
  123. char buf[128], path[64];
  124. /*
  125. * gain access to IPv6-in-IPv4 packets
  126. */
  127. p = seprint(buf, buf + sizeof buf, "%s/ipmux!proto=%2.2x",
  128. net, IP_IPV6PROTO);
  129. if (1)
  130. seprint(p, buf + sizeof buf, ";dst=%V", myip + IPv4off);
  131. if (!anysender)
  132. seprint(p, buf + sizeof buf, ";src=%V", remote4 + IPv4off);
  133. *tunp = dial(buf, 0, 0, 0);
  134. if (*tunp < 0)
  135. sysfatal("can't access ipv6-in-ipv4 with dial str %s: %r", buf);
  136. if (debug)
  137. fprint(2, "dialed %s for v6-in-v4 access\n", buf);
  138. /*
  139. * open local IPv6 interface (as a packet interface)
  140. */
  141. cl = smprint("%s/ipifc/clone", net);
  142. cfd = open(cl, ORDWR); /* allocate a conversation */
  143. n = 0;
  144. if (cfd < 0 || (n = read(cfd, buf, sizeof buf - 1)) <= 0)
  145. sysfatal("can't make packet interface %s: %r", cl);
  146. if (debug)
  147. fprint(2, "cloned %s as local v6 interface\n", cl);
  148. free(cl);
  149. buf[n] = 0;
  150. snprint(path, sizeof path, "%s/ipifc/%s/data", net, buf);
  151. *v6net = open(path, ORDWR);
  152. if (*v6net < 0 || fprint(cfd, "bind pkt") < 0)
  153. sysfatal("can't bind packet interface: %r");
  154. /* 1280 is MTU, apparently from rfc2460 */
  155. if (fprint(cfd, "add %I /128 %I 1280", local6, remote6) <= 0)
  156. sysfatal("can't set local ipv6 address: %r");
  157. close(cfd);
  158. if (debug)
  159. fprint(2, "opened & bound %s as local v6 interface\n", path);
  160. if (gateway) {
  161. /* route global addresses through the tunnel to remote6 */
  162. ir = smprint("%s/iproute", net);
  163. cfd = open(ir, OWRITE);
  164. if (cfd >= 0 && debug)
  165. fprint(2, "injected 2000::/3 %I into %s\n", remote6, ir);
  166. free(ir);
  167. if (cfd < 0 || fprint(cfd, "add 2000:: /3 %I", remote6) <= 0)
  168. sysfatal("can't set default global route: %r");
  169. }
  170. }
  171. static void
  172. runtunnel(int v6net, int tunnel)
  173. {
  174. /* run the tunnel copying in the background */
  175. switch (rfork(RFPROC|RFNOWAIT|RFMEM|RFNOTEG)) {
  176. case -1:
  177. sysfatal("rfork");
  178. default:
  179. exits(nil);
  180. case 0:
  181. break;
  182. }
  183. switch (rfork(RFPROC|RFNOWAIT|RFMEM)) {
  184. case -1:
  185. sysfatal("rfork");
  186. default:
  187. tunnel2ip(tunnel, v6net);
  188. break;
  189. case 0:
  190. ip2tunnel(v6net, tunnel);
  191. break;
  192. }
  193. exits("tunnel gone");
  194. }
  195. void
  196. main(int argc, char **argv)
  197. {
  198. int tunnel, v6net;
  199. fmtinstall('I', eipfmt);
  200. fmtinstall('V', eipfmt);
  201. fmtinstall('M', eipfmt);
  202. ARGBEGIN {
  203. case 'a':
  204. anysender++;
  205. break;
  206. case 'd':
  207. debug++;
  208. break;
  209. case 'g':
  210. gateway++;
  211. break;
  212. case 'x':
  213. net = EARGF(usage());
  214. break;
  215. default:
  216. usage();
  217. } ARGEND
  218. if (myipaddr(myip, net) < 0)
  219. sysfatal("can't find my ipv4 address on %s", net);
  220. if (!isv4(myip))
  221. sysfatal("my ip, %I, is not a v4 address", myip);
  222. procargs(argc, argv);
  223. setup(&v6net, &tunnel);
  224. runtunnel(v6net, tunnel);
  225. }
  226. /*
  227. * encapsulate v6 packets from the packet interface in v4 ones
  228. * and send them into the tunnel.
  229. */
  230. static void
  231. ip2tunnel(int in, int out)
  232. {
  233. int n, m;
  234. char buf[64*1024];
  235. Iphdr *op;
  236. Ip6hdr *ip;
  237. /* populate v4 header */
  238. op = (Iphdr*)buf;
  239. op->vihl = 0x45; /* v4, hdr is 5 longs? */
  240. memcpy(op->src, myip + IPv4off, sizeof op->src);
  241. op->proto = IP_IPV6PROTO;
  242. op->ttl = 100;
  243. /* get a V6 packet destined for the tunnel */
  244. while ((n = read(in, buf + STFHDR, sizeof buf - STFHDR)) > 0) {
  245. /* if not IPV6, drop it */
  246. ip = (Ip6hdr*)(buf + STFHDR);
  247. if ((ip->vcf[0]&0xF0) != 0x60)
  248. continue;
  249. /* check length: drop if too short, trim if too long */
  250. m = nhgets(ip->ploadlen) + sizeof(Ip6hdr);
  251. if (m > n)
  252. continue;
  253. if (m < n)
  254. n = m;
  255. /* drop if v6 source or destination address is naughty */
  256. if (badipv6(ip->src) ||
  257. (!equivip6(ip->dst, remote6) && badipv6(ip->dst))) {
  258. syslog(0, "6in4", "egress filtered %I -> %I",
  259. ip->src, ip->dst);
  260. continue;
  261. }
  262. /* send 6to4 packets directly to ipv4 target */
  263. if ((ip->dst[0]<<8 | ip->dst[1]) == V6to4pfx)
  264. memcpy(op->dst, ip->dst+2, sizeof op->dst);
  265. else
  266. memcpy(op->dst, remote4+IPv4off, sizeof op->dst);
  267. n += STFHDR;
  268. /* pass packet to the other end of the tunnel */
  269. if (write(out, op, n) != n) {
  270. syslog(0, "6in4", "error writing to tunnel (%r), giving up");
  271. break;
  272. }
  273. }
  274. }
  275. /*
  276. * decapsulate v6 packets from v4 ones from the tunnel
  277. * and forward them to the packet interface
  278. */
  279. static void
  280. tunnel2ip(int in, int out)
  281. {
  282. int n, m;
  283. char buf[64*1024];
  284. uchar a[IPaddrlen];
  285. Ip6hdr *op;
  286. Iphdr *ip;
  287. for (;;) {
  288. /* get a packet from the tunnel */
  289. n = read(in, buf, sizeof buf);
  290. ip = (Iphdr*)(buf + IPaddrlen);
  291. n -= IPaddrlen;
  292. if (n <= 0) {
  293. syslog(0, "6in4", "error reading from tunnel (%r), giving up");
  294. break;
  295. }
  296. /* if not IPv4 nor IPv4 protocol IPv6, drop it */
  297. if ((ip->vihl&0xF0) != 0x40 || ip->proto != IP_IPV6PROTO)
  298. continue;
  299. /* check length: drop if too short, trim if too long */
  300. m = nhgets(ip->length);
  301. if (m > n)
  302. continue;
  303. if (m < n)
  304. n = m;
  305. op = (Ip6hdr*)(buf + IPaddrlen + STFHDR);
  306. n -= STFHDR;
  307. /*
  308. * don't relay: just accept packets for local host/subnet
  309. * (this blocks link-local and multicast addresses as well)
  310. */
  311. maskip(op->dst, localmask, a);
  312. if (!equivip6(a, localnet)) {
  313. syslog(0, "6in4", "ingress filtered %I -> %I",
  314. op->src, op->dst);
  315. continue;
  316. }
  317. /* pass V6 packet to the interface */
  318. if (write(out, op, n) != n) {
  319. syslog(0, "6in4", "error writing to packet interface (%r), giving up");
  320. break;
  321. }
  322. }
  323. }
  324. static int
  325. badipv4(uchar *a)
  326. {
  327. switch (a[0]) {
  328. case 0: /* unassigned */
  329. case 10: /* private */
  330. case 127: /* loopback */
  331. return 1;
  332. case 172:
  333. return a[1] >= 16; /* 172.16.0.0/12 private */
  334. case 192:
  335. return a[1] == 168; /* 192.168.0.0/16 private */
  336. case 169:
  337. return a[1] == 254; /* 169.254.0.0/16 DHCP link-local */
  338. }
  339. /* 224.0.0.0/4 multicast, 240.0.0.0/4 reserved, broadcast */
  340. return a[0] >= 240;
  341. }
  342. /*
  343. * 0x0000/16 prefix = v4 compatible, v4 mapped, loopback, unspecified...
  344. * site-local is now deprecated, rfc3879
  345. */
  346. static int
  347. badipv6(uchar *a)
  348. {
  349. int h = a[0]<<8 | a[1];
  350. return h == 0 || ISIPV6MCAST(a) || ISIPV6LINKLOCAL(a) ||
  351. h == V6to4pfx && badipv4(a+2);
  352. }