iptunnel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * iptunnel.c "ip tunnel"
  4. *
  5. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  6. *
  7. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  8. *
  9. *
  10. * Changes:
  11. *
  12. * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
  13. * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
  14. * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag
  15. */
  16. #include <netinet/ip.h>
  17. #include <net/if.h>
  18. #include <net/if_arp.h>
  19. #include <asm/types.h>
  20. #ifndef __constant_htons
  21. #define __constant_htons htons
  22. #endif
  23. #include <linux/if_tunnel.h>
  24. #include "ip_common.h" /* #include "libbb.h" is inside */
  25. #include "rt_names.h"
  26. #include "utils.h"
  27. /* Dies on error */
  28. static int do_ioctl_get_ifindex(char *dev)
  29. {
  30. struct ifreq ifr;
  31. int fd;
  32. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  33. fd = xsocket(AF_INET, SOCK_DGRAM, 0);
  34. xioctl(fd, SIOCGIFINDEX, &ifr);
  35. close(fd);
  36. return ifr.ifr_ifindex;
  37. }
  38. static int do_ioctl_get_iftype(char *dev)
  39. {
  40. struct ifreq ifr;
  41. int fd;
  42. int err;
  43. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  44. fd = xsocket(AF_INET, SOCK_DGRAM, 0);
  45. err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
  46. close(fd);
  47. return err ? -1 : ifr.ifr_addr.sa_family;
  48. }
  49. static char *do_ioctl_get_ifname(int idx)
  50. {
  51. struct ifreq ifr;
  52. int fd;
  53. int err;
  54. ifr.ifr_ifindex = idx;
  55. fd = xsocket(AF_INET, SOCK_DGRAM, 0);
  56. err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr);
  57. close(fd);
  58. return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
  59. }
  60. static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
  61. {
  62. struct ifreq ifr;
  63. int fd;
  64. int err;
  65. strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
  66. ifr.ifr_ifru.ifru_data = (void*)p;
  67. fd = xsocket(AF_INET, SOCK_DGRAM, 0);
  68. err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
  69. close(fd);
  70. return err;
  71. }
  72. /* Dies on error, otherwise returns 0 */
  73. static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
  74. {
  75. struct ifreq ifr;
  76. int fd;
  77. if (cmd == SIOCCHGTUNNEL && p->name[0]) {
  78. strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
  79. } else {
  80. strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
  81. }
  82. ifr.ifr_ifru.ifru_data = (void*)p;
  83. fd = xsocket(AF_INET, SOCK_DGRAM, 0);
  84. #if ENABLE_IOCTL_HEX2STR_ERROR
  85. /* #define magic will turn ioctl# into string */
  86. if (cmd == SIOCCHGTUNNEL)
  87. xioctl(fd, SIOCCHGTUNNEL, &ifr);
  88. else
  89. xioctl(fd, SIOCADDTUNNEL, &ifr);
  90. #else
  91. xioctl(fd, cmd, &ifr);
  92. #endif
  93. close(fd);
  94. return 0;
  95. }
  96. /* Dies on error, otherwise returns 0 */
  97. static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
  98. {
  99. struct ifreq ifr;
  100. int fd;
  101. if (p->name[0]) {
  102. strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
  103. } else {
  104. strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
  105. }
  106. ifr.ifr_ifru.ifru_data = (void*)p;
  107. fd = xsocket(AF_INET, SOCK_DGRAM, 0);
  108. xioctl(fd, SIOCDELTUNNEL, &ifr);
  109. close(fd);
  110. return 0;
  111. }
  112. /* Dies on error */
  113. static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
  114. {
  115. static const char keywords[] ALIGN1 =
  116. "mode\0""ipip\0""ip/ip\0""gre\0""gre/ip\0""sit\0""ipv6/ip\0"
  117. "key\0""ikey\0""okey\0""seq\0""iseq\0""oseq\0"
  118. "csum\0""icsum\0""ocsum\0""nopmtudisc\0""pmtudisc\0"
  119. "remote\0""any\0""local\0""dev\0"
  120. "ttl\0""inherit\0""tos\0""dsfield\0"
  121. "name\0";
  122. enum {
  123. ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
  124. ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
  125. ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc,
  126. ARG_remote, ARG_any, ARG_local, ARG_dev,
  127. ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
  128. ARG_name
  129. };
  130. int count = 0;
  131. char medium[IFNAMSIZ];
  132. int key;
  133. memset(p, 0, sizeof(*p));
  134. memset(&medium, 0, sizeof(medium));
  135. p->iph.version = 4;
  136. p->iph.ihl = 5;
  137. #ifndef IP_DF
  138. #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
  139. #endif
  140. p->iph.frag_off = htons(IP_DF);
  141. while (*argv) {
  142. key = index_in_strings(keywords, *argv);
  143. if (key == ARG_mode) {
  144. NEXT_ARG();
  145. key = index_in_strings(keywords, *argv);
  146. if (key == ARG_ipip ||
  147. key == ARG_ip_ip) {
  148. if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
  149. bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
  150. }
  151. p->iph.protocol = IPPROTO_IPIP;
  152. } else if (key == ARG_gre ||
  153. key == ARG_gre_ip) {
  154. if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
  155. bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
  156. }
  157. p->iph.protocol = IPPROTO_GRE;
  158. } else if (key == ARG_sit ||
  159. key == ARG_ip6_ip) {
  160. if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
  161. bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
  162. }
  163. p->iph.protocol = IPPROTO_IPV6;
  164. } else {
  165. bb_error_msg_and_die("cannot guess tunnel mode");
  166. }
  167. } else if (key == ARG_key) {
  168. unsigned uval;
  169. NEXT_ARG();
  170. p->i_flags |= GRE_KEY;
  171. p->o_flags |= GRE_KEY;
  172. if (strchr(*argv, '.'))
  173. p->i_key = p->o_key = get_addr32(*argv);
  174. else {
  175. if (get_unsigned(&uval, *argv, 0) < 0) {
  176. bb_error_msg_and_die("invalid value of \"key\"");
  177. }
  178. p->i_key = p->o_key = htonl(uval);
  179. }
  180. } else if (key == ARG_ikey) {
  181. unsigned uval;
  182. NEXT_ARG();
  183. p->i_flags |= GRE_KEY;
  184. if (strchr(*argv, '.'))
  185. p->o_key = get_addr32(*argv);
  186. else {
  187. if (get_unsigned(&uval, *argv, 0) < 0) {
  188. bb_error_msg_and_die("invalid value of \"ikey\"");
  189. }
  190. p->i_key = htonl(uval);
  191. }
  192. } else if (key == ARG_okey) {
  193. unsigned uval;
  194. NEXT_ARG();
  195. p->o_flags |= GRE_KEY;
  196. if (strchr(*argv, '.'))
  197. p->o_key = get_addr32(*argv);
  198. else {
  199. if (get_unsigned(&uval, *argv, 0) < 0) {
  200. bb_error_msg_and_die("invalid value of \"okey\"");
  201. }
  202. p->o_key = htonl(uval);
  203. }
  204. } else if (key == ARG_seq) {
  205. p->i_flags |= GRE_SEQ;
  206. p->o_flags |= GRE_SEQ;
  207. } else if (key == ARG_iseq) {
  208. p->i_flags |= GRE_SEQ;
  209. } else if (key == ARG_oseq) {
  210. p->o_flags |= GRE_SEQ;
  211. } else if (key == ARG_csum) {
  212. p->i_flags |= GRE_CSUM;
  213. p->o_flags |= GRE_CSUM;
  214. } else if (key == ARG_icsum) {
  215. p->i_flags |= GRE_CSUM;
  216. } else if (key == ARG_ocsum) {
  217. p->o_flags |= GRE_CSUM;
  218. } else if (key == ARG_nopmtudisc) {
  219. p->iph.frag_off = 0;
  220. } else if (key == ARG_pmtudisc) {
  221. p->iph.frag_off = htons(IP_DF);
  222. } else if (key == ARG_remote) {
  223. NEXT_ARG();
  224. key = index_in_strings(keywords, *argv);
  225. if (key != ARG_any)
  226. p->iph.daddr = get_addr32(*argv);
  227. } else if (key == ARG_local) {
  228. NEXT_ARG();
  229. key = index_in_strings(keywords, *argv);
  230. if (key != ARG_any)
  231. p->iph.saddr = get_addr32(*argv);
  232. } else if (key == ARG_dev) {
  233. NEXT_ARG();
  234. strncpy(medium, *argv, IFNAMSIZ-1);
  235. } else if (key == ARG_ttl) {
  236. unsigned uval;
  237. NEXT_ARG();
  238. key = index_in_strings(keywords, *argv);
  239. if (key != ARG_inherit) {
  240. if (get_unsigned(&uval, *argv, 0))
  241. invarg(*argv, "TTL");
  242. if (uval > 255)
  243. invarg(*argv, "TTL must be <=255");
  244. p->iph.ttl = uval;
  245. }
  246. } else if (key == ARG_tos ||
  247. key == ARG_dsfield) {
  248. uint32_t uval;
  249. NEXT_ARG();
  250. key = index_in_strings(keywords, *argv);
  251. if (key != ARG_inherit) {
  252. if (rtnl_dsfield_a2n(&uval, *argv))
  253. invarg(*argv, "TOS");
  254. p->iph.tos = uval;
  255. } else
  256. p->iph.tos = 1;
  257. } else {
  258. if (key == ARG_name) {
  259. NEXT_ARG();
  260. }
  261. if (p->name[0])
  262. duparg2("name", *argv);
  263. strncpy(p->name, *argv, IFNAMSIZ);
  264. if (cmd == SIOCCHGTUNNEL && count == 0) {
  265. struct ip_tunnel_parm old_p;
  266. memset(&old_p, 0, sizeof(old_p));
  267. if (do_get_ioctl(*argv, &old_p))
  268. exit(EXIT_FAILURE);
  269. *p = old_p;
  270. }
  271. }
  272. count++;
  273. argv++;
  274. }
  275. if (p->iph.protocol == 0) {
  276. if (memcmp(p->name, "gre", 3) == 0)
  277. p->iph.protocol = IPPROTO_GRE;
  278. else if (memcmp(p->name, "ipip", 4) == 0)
  279. p->iph.protocol = IPPROTO_IPIP;
  280. else if (memcmp(p->name, "sit", 3) == 0)
  281. p->iph.protocol = IPPROTO_IPV6;
  282. }
  283. if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
  284. if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
  285. bb_error_msg_and_die("keys are not allowed with ipip and sit");
  286. }
  287. }
  288. if (medium[0]) {
  289. p->link = do_ioctl_get_ifindex(medium);
  290. }
  291. if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
  292. p->i_key = p->iph.daddr;
  293. p->i_flags |= GRE_KEY;
  294. }
  295. if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
  296. p->o_key = p->iph.daddr;
  297. p->o_flags |= GRE_KEY;
  298. }
  299. if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
  300. bb_error_msg_and_die("broadcast tunnel requires a source address");
  301. }
  302. }
  303. /* Return value becomes exitcode. It's okay to not return at all */
  304. static int do_add(int cmd, char **argv)
  305. {
  306. struct ip_tunnel_parm p;
  307. parse_args(argv, cmd, &p);
  308. if (p.iph.ttl && p.iph.frag_off == 0) {
  309. bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
  310. }
  311. switch (p.iph.protocol) {
  312. case IPPROTO_IPIP:
  313. return do_add_ioctl(cmd, "tunl0", &p);
  314. case IPPROTO_GRE:
  315. return do_add_ioctl(cmd, "gre0", &p);
  316. case IPPROTO_IPV6:
  317. return do_add_ioctl(cmd, "sit0", &p);
  318. default:
  319. bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)");
  320. }
  321. }
  322. /* Return value becomes exitcode. It's okay to not return at all */
  323. static int do_del(char **argv)
  324. {
  325. struct ip_tunnel_parm p;
  326. parse_args(argv, SIOCDELTUNNEL, &p);
  327. switch (p.iph.protocol) {
  328. case IPPROTO_IPIP:
  329. return do_del_ioctl("tunl0", &p);
  330. case IPPROTO_GRE:
  331. return do_del_ioctl("gre0", &p);
  332. case IPPROTO_IPV6:
  333. return do_del_ioctl("sit0", &p);
  334. default:
  335. return do_del_ioctl(p.name, &p);
  336. }
  337. }
  338. static void print_tunnel(struct ip_tunnel_parm *p)
  339. {
  340. char s1[256];
  341. char s2[256];
  342. char s3[64];
  343. char s4[64];
  344. format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
  345. format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
  346. inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
  347. inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
  348. printf("%s: %s/ip remote %s local %s ",
  349. p->name,
  350. p->iph.protocol == IPPROTO_IPIP ? "ip" :
  351. (p->iph.protocol == IPPROTO_GRE ? "gre" :
  352. (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
  353. p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
  354. if (p->link) {
  355. char *n = do_ioctl_get_ifname(p->link);
  356. if (n) {
  357. printf(" dev %s ", n);
  358. free(n);
  359. }
  360. }
  361. if (p->iph.ttl)
  362. printf(" ttl %d ", p->iph.ttl);
  363. else
  364. printf(" ttl inherit ");
  365. if (p->iph.tos) {
  366. SPRINT_BUF(b1);
  367. printf(" tos");
  368. if (p->iph.tos & 1)
  369. printf(" inherit");
  370. if (p->iph.tos & ~1)
  371. printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
  372. rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
  373. }
  374. if (!(p->iph.frag_off & htons(IP_DF)))
  375. printf(" nopmtudisc");
  376. if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
  377. printf(" key %s", s3);
  378. else if ((p->i_flags | p->o_flags) & GRE_KEY) {
  379. if (p->i_flags & GRE_KEY)
  380. printf(" ikey %s ", s3);
  381. if (p->o_flags & GRE_KEY)
  382. printf(" okey %s ", s4);
  383. }
  384. if (p->i_flags & GRE_SEQ)
  385. printf("%c Drop packets out of sequence.\n", _SL_);
  386. if (p->i_flags & GRE_CSUM)
  387. printf("%c Checksum in received packet is required.", _SL_);
  388. if (p->o_flags & GRE_SEQ)
  389. printf("%c Sequence packets on output.", _SL_);
  390. if (p->o_flags & GRE_CSUM)
  391. printf("%c Checksum output packets.", _SL_);
  392. }
  393. static void do_tunnels_list(struct ip_tunnel_parm *p)
  394. {
  395. char name[IFNAMSIZ];
  396. unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
  397. rx_fifo, rx_frame,
  398. tx_bytes, tx_packets, tx_errs, tx_drops,
  399. tx_fifo, tx_colls, tx_carrier, rx_multi;
  400. int type;
  401. struct ip_tunnel_parm p1;
  402. char buf[512];
  403. FILE *fp = fopen_or_warn("/proc/net/dev", "r");
  404. if (fp == NULL) {
  405. return;
  406. }
  407. fgets(buf, sizeof(buf), fp);
  408. fgets(buf, sizeof(buf), fp);
  409. while (fgets(buf, sizeof(buf), fp) != NULL) {
  410. char *ptr;
  411. /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
  412. ptr = strchr(buf, ':');
  413. if (ptr == NULL ||
  414. (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
  415. bb_error_msg("wrong format of /proc/net/dev");
  416. return;
  417. }
  418. if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
  419. &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
  420. &rx_fifo, &rx_frame, &rx_multi,
  421. &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
  422. &tx_fifo, &tx_colls, &tx_carrier) != 14)
  423. continue;
  424. if (p->name[0] && strcmp(p->name, name))
  425. continue;
  426. type = do_ioctl_get_iftype(name);
  427. if (type == -1) {
  428. bb_error_msg("cannot get type of [%s]", name);
  429. continue;
  430. }
  431. if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
  432. continue;
  433. memset(&p1, 0, sizeof(p1));
  434. if (do_get_ioctl(name, &p1))
  435. continue;
  436. if ((p->link && p1.link != p->link) ||
  437. (p->name[0] && strcmp(p1.name, p->name)) ||
  438. (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
  439. (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
  440. (p->i_key && p1.i_key != p->i_key))
  441. continue;
  442. print_tunnel(&p1);
  443. bb_putchar('\n');
  444. }
  445. }
  446. /* Return value becomes exitcode. It's okay to not return at all */
  447. static int do_show(char **argv)
  448. {
  449. int err;
  450. struct ip_tunnel_parm p;
  451. parse_args(argv, SIOCGETTUNNEL, &p);
  452. switch (p.iph.protocol) {
  453. case IPPROTO_IPIP:
  454. err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
  455. break;
  456. case IPPROTO_GRE:
  457. err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
  458. break;
  459. case IPPROTO_IPV6:
  460. err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
  461. break;
  462. default:
  463. do_tunnels_list(&p);
  464. return 0;
  465. }
  466. if (err)
  467. return -1;
  468. print_tunnel(&p);
  469. bb_putchar('\n');
  470. return 0;
  471. }
  472. /* Return value becomes exitcode. It's okay to not return at all */
  473. int do_iptunnel(char **argv)
  474. {
  475. static const char keywords[] ALIGN1 =
  476. "add\0""change\0""delete\0""show\0""list\0""lst\0";
  477. enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
  478. int key;
  479. if (*argv) {
  480. key = index_in_substrings(keywords, *argv);
  481. if (key < 0)
  482. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  483. argv++;
  484. if (key == ARG_add)
  485. return do_add(SIOCADDTUNNEL, argv);
  486. if (key == ARG_change)
  487. return do_add(SIOCCHGTUNNEL, argv);
  488. if (key == ARG_del)
  489. return do_del(argv);
  490. }
  491. return do_show(argv);
  492. }