6in4.c 9.5 KB

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