iptunnel.c 13 KB

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