3
0

iptunnel.c 15 KB

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