6in4.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. * based on libthread's threadsetname, but drags in less library code.
  226. * actually just sets the arguments displayed.
  227. */
  228. void
  229. procsetname(char *fmt, ...)
  230. {
  231. int fd;
  232. char *cmdname;
  233. char buf[128];
  234. va_list arg;
  235. va_start(arg, fmt);
  236. cmdname = vsmprint(fmt, arg);
  237. va_end(arg);
  238. if (cmdname == nil)
  239. return;
  240. snprint(buf, sizeof buf, "#p/%d/args", getpid());
  241. if((fd = open(buf, OWRITE)) >= 0){
  242. write(fd, cmdname, strlen(cmdname)+1);
  243. close(fd);
  244. }
  245. free(cmdname);
  246. }
  247. /*
  248. * encapsulate v6 packets from the packet interface in v4 ones
  249. * and send them into the tunnel.
  250. */
  251. static void
  252. ip2tunnel(int in, int out)
  253. {
  254. int n, m;
  255. char buf[64*1024];
  256. Iphdr *op;
  257. Ip6hdr *ip;
  258. if (anysender)
  259. procsetname("v6 %I -> tunnel", local6);
  260. else
  261. procsetname("v6 %I -> tunnel %I %I", local6, remote4, remote6);
  262. /* populate v4 header */
  263. op = (Iphdr*)buf;
  264. op->vihl = IP_VER4 | 5; /* hdr is 5 longs? */
  265. memcpy(op->src, myip + IPv4off, sizeof op->src);
  266. op->proto = IP_IPV6PROTO;
  267. op->ttl = 100;
  268. /* get a V6 packet destined for the tunnel */
  269. while ((n = read(in, buf + STFHDR, sizeof buf - STFHDR)) > 0) {
  270. /* if not IPV6, drop it */
  271. ip = (Ip6hdr*)(buf + STFHDR);
  272. if ((ip->vcf[0] & 0xF0) != IP_VER6)
  273. continue;
  274. /* check length: drop if too short, trim if too long */
  275. m = nhgets(ip->ploadlen) + sizeof(Ip6hdr);
  276. if (m > n)
  277. continue;
  278. if (m < n)
  279. n = m;
  280. /* drop if v6 source or destination address is naughty */
  281. if (badipv6(ip->src) ||
  282. (!equivip6(ip->dst, remote6) && badipv6(ip->dst))) {
  283. syslog(0, "6in4", "egress filtered %I -> %I",
  284. ip->src, ip->dst);
  285. continue;
  286. }
  287. if (debug > 1)
  288. fprint(2, "v6 to tunnel %I -> %I\n", ip->src, ip->dst);
  289. /* send 6to4 packets directly to ipv4 target */
  290. if ((ip->dst[0]<<8 | ip->dst[1]) == V6to4pfx)
  291. memcpy(op->dst, ip->dst+2, sizeof op->dst);
  292. else
  293. memcpy(op->dst, remote4+IPv4off, sizeof op->dst);
  294. n += STFHDR;
  295. /* pass packet to the other end of the tunnel */
  296. if (write(out, op, n) != n) {
  297. syslog(0, "6in4", "error writing to tunnel (%r), giving up");
  298. break;
  299. }
  300. }
  301. }
  302. /*
  303. * decapsulate v6 packets from v4 ones from the tunnel
  304. * and forward them to the packet interface
  305. */
  306. static void
  307. tunnel2ip(int in, int out)
  308. {
  309. int n, m;
  310. char buf[64*1024];
  311. uchar a[IPaddrlen];
  312. Ip6hdr *op;
  313. Iphdr *ip;
  314. if (anysender)
  315. procsetname("tunnel -> v6 %I", local6);
  316. else
  317. procsetname("tunnel %I %I -> v6 %I", remote4, remote6, local6);
  318. for (;;) {
  319. /* get a packet from the tunnel */
  320. n = read(in, buf, sizeof buf);
  321. ip = (Iphdr*)(buf + IPaddrlen);
  322. n -= IPaddrlen;
  323. if (n <= 0) {
  324. syslog(0, "6in4", "error reading from tunnel (%r), giving up");
  325. break;
  326. }
  327. /* if not IPv4 nor IPv4 protocol IPv6, drop it */
  328. if ((ip->vihl & 0xF0) != IP_VER4 || ip->proto != IP_IPV6PROTO)
  329. continue;
  330. /* check length: drop if too short, trim if too long */
  331. m = nhgets(ip->length);
  332. if (m > n)
  333. continue;
  334. if (m < n)
  335. n = m;
  336. op = (Ip6hdr*)(buf + IPaddrlen + STFHDR);
  337. n -= STFHDR;
  338. /*
  339. * don't relay: just accept packets for local host/subnet
  340. * (this blocks link-local and multicast addresses as well)
  341. */
  342. maskip(op->dst, localmask, a);
  343. if (!equivip6(a, localnet)) {
  344. syslog(0, "6in4", "ingress filtered %I -> %I",
  345. op->src, op->dst);
  346. continue;
  347. }
  348. if (debug > 1)
  349. fprint(2, "tunnel to v6 %I -> %I\n", op->src, op->dst);
  350. /* pass V6 packet to the interface */
  351. if (write(out, op, n) != n) {
  352. syslog(0, "6in4", "error writing to packet interface (%r), giving up");
  353. break;
  354. }
  355. }
  356. }
  357. static int
  358. badipv4(uchar *a)
  359. {
  360. switch (a[0]) {
  361. case 0: /* unassigned */
  362. case 10: /* private */
  363. case 127: /* loopback */
  364. return 1;
  365. case 172:
  366. return a[1] >= 16; /* 172.16.0.0/12 private */
  367. case 192:
  368. return a[1] == 168; /* 192.168.0.0/16 private */
  369. case 169:
  370. return a[1] == 254; /* 169.254.0.0/16 DHCP link-local */
  371. }
  372. /* 224.0.0.0/4 multicast, 240.0.0.0/4 reserved, broadcast */
  373. return a[0] >= 240;
  374. }
  375. /*
  376. * 0x0000/16 prefix = v4 compatible, v4 mapped, loopback, unspecified...
  377. * site-local is now deprecated, rfc3879
  378. */
  379. static int
  380. badipv6(uchar *a)
  381. {
  382. int h = a[0]<<8 | a[1];
  383. return h == 0 || ISIPV6MCAST(a) || ISIPV6LINKLOCAL(a) ||
  384. h == V6to4pfx && badipv4(a+2);
  385. }