6in4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * 6in4 - tunnel client for automatic 6to4 or configured v6-in-v4 tunnels.
  11. * see rfc3056.
  12. */
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <ip.h>
  16. /*
  17. * IPv6 and related IP protocols & their numbers:
  18. *
  19. * ipv6 41 IPv6 # Internet Protocol, version 6
  20. * ipv6-route 43 IPv6-Route # Routing Header for IPv6
  21. * ipv6-frag 44 IPv6-Frag # Fragment Header for IPv6
  22. * esp 50 ESP # Encapsulating Security Payload
  23. * ah 51 AH # Authentication Header
  24. * ipv6-icmp 58 IPv6-ICMP icmp6 # ICMP version 6
  25. * ipv6-nonxt 59 IPv6-NoNxt # No Next Header for IPv6
  26. * ipv6-opts 60 IPv6-Opts # Destination Options for IPv6
  27. */
  28. enum {
  29. IP_IPV6PROTO = 41, /* IPv4 protocol number for IPv6 */
  30. IP_ESPPROTO = 50, /* IP v4 and v6 protocol number */
  31. IP_AHPROTO = 51, /* IP v4 and v6 protocol number */
  32. IP_ICMPV6PROTO = 58,
  33. V6to4pfx = 0x2002,
  34. };
  35. typedef struct Iphdr Iphdr;
  36. struct Iphdr
  37. {
  38. uint8_t vihl; /* Version and header length */
  39. uint8_t tos; /* Type of service */
  40. uint8_t length[2]; /* packet length */
  41. uint8_t id[2]; /* Identification */
  42. uint8_t frag[2]; /* Fragment information */
  43. uint8_t ttl; /* Time to live */
  44. uint8_t proto; /* Protocol */
  45. uint8_t cksum[2]; /* Header checksum */
  46. uint8_t src[4]; /* Ip source (uint8_t ordering unimportant) */
  47. uint8_t dst[4]; /* Ip destination (uint8_t ordering unimportant) */
  48. uint8_t payload[];
  49. };
  50. #define STFHDR offsetof(Iphdr, payload[0])
  51. int anysender;
  52. int gateway;
  53. int debug;
  54. uint8_t local6[IPaddrlen];
  55. uint8_t remote6[IPaddrlen];
  56. uint8_t remote4[IPaddrlen];
  57. uint8_t localmask[IPaddrlen];
  58. uint8_t localnet[IPaddrlen];
  59. uint8_t myip[IPaddrlen];
  60. /* magic anycast address from rfc3068 */
  61. uint8_t anycast6to4[IPv4addrlen] = { 192, 88, 99, 1 };
  62. static char *net = "/net";
  63. static int badipv4(uint8_t*);
  64. static int badipv6(uint8_t*);
  65. static void ip2tunnel(int, int);
  66. static void tunnel2ip(int, int);
  67. static void
  68. usage(void)
  69. {
  70. fprint(2, "usage: %s [-ag] [-x mtpt] [local6[/mask]] [remote4 [remote6]]\n",
  71. argv0);
  72. exits("Usage");
  73. }
  74. static char *
  75. defv6addr(void)
  76. {
  77. uint8_t *ipv4 = &myip[IPaddrlen - IPv4addrlen];
  78. return smprint("%x:%2.2x%2.2x:%2.2x%2.2x::1/48", V6to4pfx,
  79. ipv4[0], ipv4[1], ipv4[2], ipv4[3]);
  80. }
  81. /* process non-option arguments */
  82. static void
  83. procargs(int argc, char **argv)
  84. {
  85. char *p, *loc6;
  86. if (argc < 1)
  87. loc6 = defv6addr();
  88. else if (strcmp(argv[0], "-") == 0) {
  89. loc6 = defv6addr();
  90. argv++;
  91. argc--;
  92. } else {
  93. loc6 = *argv++;
  94. argc--;
  95. }
  96. /* local v6 address (mask defaults to /128) */
  97. memcpy(localmask, IPallbits, sizeof localmask);
  98. p = strchr(loc6, '/');
  99. if (p != nil) {
  100. parseipmask(localmask, p);
  101. *p = 0;
  102. }
  103. if (parseip(local6, loc6) == -1)
  104. sysfatal("bad local v6 address %s", loc6);
  105. if (isv4(local6))
  106. usage();
  107. if (argc >= 1 && argv[0][0] == '/') {
  108. parseipmask(localmask, *argv++);
  109. argc--;
  110. }
  111. if (debug)
  112. fprint(2, "local6 %I %M\n", local6, localmask);
  113. /* remote v4 address (defaults to anycast 6to4) */
  114. if (argc >= 1) {
  115. if (parseip(remote4, *argv++) == -1)
  116. sysfatal("bad remote v4 address %s", argv[-1]);
  117. argc--;
  118. if (!isv4(remote4))
  119. usage();
  120. } else {
  121. v4tov6(remote4, anycast6to4);
  122. anysender++;
  123. }
  124. if (debug)
  125. fprint(2, "remote4 %I\n", remote4);
  126. /* remote v6 address (defaults to link-local w/ v4 as interface part) */
  127. if (argc >= 1) {
  128. if (parseip(remote6, *argv++) == -1)
  129. sysfatal("bad remote v6 address %s", argv[-1]);
  130. argc--;
  131. } else {
  132. remote6[0] = 0xFE; /* link local */
  133. remote6[1] = 0x80;
  134. memcpy(remote6 + IPv4off, remote4 + IPv4off, IPv4addrlen);
  135. }
  136. USED(argv);
  137. if (argc != 0)
  138. usage();
  139. maskip(local6, localmask, localnet);
  140. if (debug)
  141. fprint(2, "localnet %I remote6 %I\n", localnet, remote6);
  142. }
  143. static void
  144. setup(int *v6net, int *tunp)
  145. {
  146. int n, cfd;
  147. char *p, *cl, *ir;
  148. char buf[128], path[64];
  149. /*
  150. * gain access to IPv6-in-IPv4 packets via ipmux
  151. */
  152. p = seprint(buf, buf + sizeof buf, "%s/ipmux!proto=%2.2x|%2.2x;dst=%V",
  153. net, IP_IPV6PROTO, IP_ICMPV6PROTO, myip + IPv4off);
  154. if (!anysender)
  155. seprint(p, buf + sizeof buf, ";src=%V", remote4 + IPv4off);
  156. *tunp = dial(buf, 0, 0, 0);
  157. if (*tunp < 0)
  158. sysfatal("can't access ipv6-in-ipv4 with dial str %s: %r", buf);
  159. if (debug)
  160. fprint(2, "dialed %s for v6-in-v4 access\n", buf);
  161. /*
  162. * open local IPv6 interface (as a packet interface)
  163. */
  164. cl = smprint("%s/ipifc/clone", net);
  165. cfd = open(cl, ORDWR); /* allocate a conversation */
  166. n = 0;
  167. if (cfd < 0 || (n = read(cfd, buf, sizeof buf - 1)) <= 0)
  168. sysfatal("can't make packet interface %s: %r", cl);
  169. if (debug)
  170. fprint(2, "cloned %s as local v6 interface\n", cl);
  171. free(cl);
  172. buf[n] = 0;
  173. snprint(path, sizeof path, "%s/ipifc/%s/data", net, buf);
  174. *v6net = open(path, ORDWR);
  175. if (*v6net < 0 || fprint(cfd, "bind pkt") < 0)
  176. sysfatal("can't bind packet interface: %r");
  177. /* 1280 is MTU, apparently from rfc2460 */
  178. if (fprint(cfd, "add %I /128 %I 1280", local6, remote6) <= 0)
  179. sysfatal("can't set local ipv6 address: %r");
  180. close(cfd);
  181. if (debug)
  182. fprint(2, "opened & bound %s as local v6 interface\n", path);
  183. if (gateway) {
  184. /* route global addresses through the tunnel to remote6 */
  185. ir = smprint("%s/iproute", net);
  186. cfd = open(ir, OWRITE);
  187. if (cfd >= 0 && debug)
  188. fprint(2, "injected 2000::/3 %I into %s\n", remote6, ir);
  189. free(ir);
  190. if (cfd < 0 || fprint(cfd, "add 2000:: /3 %I", remote6) <= 0)
  191. sysfatal("can't set default global route: %r");
  192. }
  193. }
  194. static void
  195. runtunnel(int v6net, int tunnel)
  196. {
  197. /* run the tunnel copying in the background */
  198. switch (rfork(RFPROC|RFNOWAIT|RFMEM|RFNOTEG)) {
  199. case -1:
  200. sysfatal("rfork");
  201. default:
  202. exits(nil);
  203. case 0:
  204. break;
  205. }
  206. switch (rfork(RFPROC|RFNOWAIT|RFMEM)) {
  207. case -1:
  208. sysfatal("rfork");
  209. default:
  210. tunnel2ip(tunnel, v6net);
  211. break;
  212. case 0:
  213. ip2tunnel(v6net, tunnel);
  214. break;
  215. }
  216. exits("tunnel gone");
  217. }
  218. void
  219. main(int argc, char **argv)
  220. {
  221. int tunnel, v6net;
  222. fmtinstall('I', eipfmt);
  223. fmtinstall('V', eipfmt);
  224. fmtinstall('M', eipfmt);
  225. ARGBEGIN {
  226. case 'a':
  227. anysender++;
  228. break;
  229. case 'd':
  230. debug++;
  231. break;
  232. case 'g':
  233. gateway++;
  234. break;
  235. case 'x':
  236. net = EARGF(usage());
  237. break;
  238. default:
  239. usage();
  240. } ARGEND
  241. if (myipaddr(myip, net) < 0)
  242. sysfatal("can't find my ipv4 address on %s", net);
  243. if (!isv4(myip))
  244. sysfatal("my ip, %I, is not a v4 address", myip);
  245. procargs(argc, argv);
  246. setup(&v6net, &tunnel);
  247. runtunnel(v6net, tunnel);
  248. exits(0);
  249. }
  250. /*
  251. * based on libthread's threadsetname, but drags in less library code.
  252. * actually just sets the arguments displayed.
  253. */
  254. void
  255. procsetname(char *fmt, ...)
  256. {
  257. int fd;
  258. char *cmdname;
  259. char buf[128];
  260. va_list arg;
  261. va_start(arg, fmt);
  262. cmdname = vsmprint(fmt, arg);
  263. va_end(arg);
  264. if (cmdname == nil)
  265. return;
  266. snprint(buf, sizeof buf, "#p/%d/args", getpid());
  267. if((fd = open(buf, OWRITE)) >= 0){
  268. write(fd, cmdname, strlen(cmdname)+1);
  269. close(fd);
  270. }
  271. free(cmdname);
  272. }
  273. /*
  274. * encapsulate v6 packets from the packet interface in v4 ones
  275. * and send them into the tunnel.
  276. */
  277. static void
  278. ip2tunnel(int in, int out)
  279. {
  280. int n, m;
  281. char buf[64*1024];
  282. Iphdr *op;
  283. Ip6hdr *ip;
  284. if (anysender)
  285. procsetname("v6 %I -> tunnel", local6);
  286. else
  287. procsetname("v6 %I -> tunnel %I %I", local6, remote4, remote6);
  288. /* populate v4 header */
  289. op = (Iphdr*)buf;
  290. op->vihl = IP_VER4 | 5; /* hdr is 5 longs? */
  291. memcpy(op->src, myip + IPv4off, sizeof op->src);
  292. op->proto = IP_IPV6PROTO; /* inner protocol */
  293. op->ttl = 100;
  294. /* get a V6 packet destined for the tunnel */
  295. ip = (Ip6hdr*)(buf + STFHDR);
  296. while ((n = read(in, ip, sizeof buf - STFHDR)) > 0) {
  297. /* if not IPV6, drop it */
  298. if ((ip->vcf[0] & 0xF0) != IP_VER6)
  299. continue;
  300. /* check length: drop if too short, trim if too long */
  301. m = nhgets(ip->ploadlen) + IPV6HDR_LEN;
  302. if (m > n)
  303. continue;
  304. if (m < n)
  305. n = m;
  306. /* drop if v6 source or destination address is naughty */
  307. if (badipv6(ip->src)) {
  308. syslog(0, "6in4", "egress filtered %I -> %I; bad src",
  309. ip->src, ip->dst);
  310. continue;
  311. }
  312. if ((!equivip6(ip->dst, remote6) && badipv6(ip->dst))) {
  313. syslog(0, "6in4", "egress filtered %I -> %I; "
  314. "bad dst not remote", ip->src, ip->dst);
  315. continue;
  316. }
  317. if (debug > 1)
  318. fprint(2, "v6 to tunnel %I -> %I\n", ip->src, ip->dst);
  319. /* send 6to4 packets directly to ipv4 target */
  320. if ((ip->dst[0]<<8 | ip->dst[1]) == V6to4pfx)
  321. memcpy(op->dst, ip->dst+2, sizeof op->dst);
  322. else
  323. memcpy(op->dst, remote4+IPv4off, sizeof op->dst);
  324. n += STFHDR;
  325. /* pass packet to the other end of the tunnel */
  326. if (write(out, op, n) != n) {
  327. syslog(0, "6in4", "error writing to tunnel (%r), giving up");
  328. break;
  329. }
  330. }
  331. }
  332. /*
  333. * decapsulate v6 packets from v4 ones from the tunnel
  334. * and forward them to the packet interface
  335. */
  336. static void
  337. tunnel2ip(int in, int out)
  338. {
  339. int n, m;
  340. char buf[64*1024];
  341. uint8_t a[IPaddrlen];
  342. Ip6hdr *op;
  343. Iphdr *ip;
  344. if (anysender)
  345. procsetname("tunnel -> v6 %I", local6);
  346. else
  347. procsetname("tunnel %I %I -> v6 %I", remote4, remote6, local6);
  348. for (;;) {
  349. /* get a packet from the tunnel */
  350. n = read(in, buf, sizeof buf);
  351. ip = (Iphdr*)(buf + IPaddrlen);
  352. n -= IPaddrlen;
  353. if (n <= 0) {
  354. syslog(0, "6in4", "error reading from tunnel (%r), giving up");
  355. break;
  356. }
  357. /* if not IPv4 nor IPv4 protocol IPv6 nor ICMPv6, drop it */
  358. if ((ip->vihl & 0xF0) != IP_VER4 ||
  359. ip->proto != IP_IPV6PROTO && ip->proto != IP_ICMPV6PROTO) {
  360. syslog(0, "6in4",
  361. "dropping pkt from tunnel with inner proto %d",
  362. ip->proto);
  363. continue;
  364. }
  365. /* check length: drop if too short, trim if too long */
  366. m = nhgets(ip->length);
  367. if (m > n)
  368. continue;
  369. if (m < n)
  370. n = m;
  371. op = (Ip6hdr*)(buf + IPaddrlen + STFHDR);
  372. n -= STFHDR;
  373. /*
  374. * don't relay: just accept packets for local host/subnet
  375. * (this blocks link-local and multicast addresses as well)
  376. */
  377. maskip(op->dst, localmask, a);
  378. if (!equivip6(a, localnet)) {
  379. syslog(0, "6in4", "ingress filtered %I -> %I; "
  380. "dst not on local net", op->src, op->dst);
  381. continue;
  382. }
  383. if (debug > 1)
  384. fprint(2, "tunnel to v6 %I -> %I\n", op->src, op->dst);
  385. /* pass V6 packet to the interface */
  386. if (write(out, op, n) != n) {
  387. syslog(0, "6in4", "error writing to packet interface (%r), giving up");
  388. break;
  389. }
  390. }
  391. }
  392. static int
  393. badipv4(uint8_t *a)
  394. {
  395. switch (a[0]) {
  396. case 0: /* unassigned */
  397. case 10: /* private */
  398. case 127: /* loopback */
  399. return 1;
  400. case 172:
  401. return a[1] >= 16; /* 172.16.0.0/12 private */
  402. case 192:
  403. return a[1] == 168; /* 192.168.0.0/16 private */
  404. case 169:
  405. return a[1] == 254; /* 169.254.0.0/16 DHCP link-local */
  406. }
  407. /* 224.0.0.0/4 multicast, 240.0.0.0/4 reserved, broadcast */
  408. return a[0] >= 240;
  409. }
  410. /*
  411. * 0x0000/16 prefix = v4 compatible, v4 mapped, loopback, unspecified...
  412. * site-local is now deprecated, rfc3879
  413. */
  414. static int
  415. badipv6(uint8_t *a)
  416. {
  417. int h = a[0]<<8 | a[1];
  418. return h == 0 || ISIPV6MCAST(a) || ISIPV6LINKLOCAL(a) ||
  419. h == V6to4pfx && badipv4(a+2);
  420. }