3
0

route.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /* route
  2. *
  3. * Similar to the standard Unix route, but with only the necessary
  4. * parts for AF_INET and AF_INET6
  5. *
  6. * Bjorn Wesen, Axis Communications AB
  7. *
  8. * Author of the original route:
  9. * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  10. * (derived from FvK's 'route.c 1.70 01/04/94')
  11. *
  12. * This program is free software; you can redistribute it
  13. * and/or modify it under the terms of the GNU General
  14. * Public License as published by the Free Software
  15. * Foundation; either version 2 of the License, or (at
  16. * your option) any later version.
  17. *
  18. * $Id: route.c,v 1.26 2004/03/19 23:27:08 mjn3 Exp $
  19. *
  20. * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru>
  21. * adjustments by Larry Doolittle <LRDoolittle@lbl.gov>
  22. *
  23. * IPV6 support added by Bart Visscher <magick@linux-fan.com>
  24. */
  25. /* 2004/03/09 Manuel Novoa III <mjn3@codepoet.org>
  26. *
  27. * Rewritten to fix several bugs, add additional error checking, and
  28. * remove ridiculous amounts of bloat.
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <assert.h>
  35. #include <unistd.h>
  36. #include <fcntl.h>
  37. #include <getopt.h>
  38. #include <sys/types.h>
  39. #include <sys/ioctl.h>
  40. #include <net/route.h>
  41. #include <net/if.h>
  42. #include "busybox.h"
  43. #include "inet_common.h"
  44. #ifndef RTF_UP
  45. /* Keep this in sync with /usr/src/linux/include/linux/route.h */
  46. #define RTF_UP 0x0001 /* route usable */
  47. #define RTF_GATEWAY 0x0002 /* destination is a gateway */
  48. #define RTF_HOST 0x0004 /* host entry (net otherwise) */
  49. #define RTF_REINSTATE 0x0008 /* reinstate route after tmout */
  50. #define RTF_DYNAMIC 0x0010 /* created dyn. (by redirect) */
  51. #define RTF_MODIFIED 0x0020 /* modified dyn. (by redirect) */
  52. #define RTF_MTU 0x0040 /* specific MTU for this route */
  53. #ifndef RTF_MSS
  54. #define RTF_MSS RTF_MTU /* Compatibility :-( */
  55. #endif
  56. #define RTF_WINDOW 0x0080 /* per route window clamping */
  57. #define RTF_IRTT 0x0100 /* Initial round trip time */
  58. #define RTF_REJECT 0x0200 /* Reject route */
  59. #endif
  60. #if defined (SIOCADDRTOLD) || defined (RTF_IRTT) /* route */
  61. #define HAVE_NEW_ADDRT 1
  62. #endif
  63. #if HAVE_NEW_ADDRT
  64. #define mask_in_addr(x) (((struct sockaddr_in *)&((x).rt_genmask))->sin_addr.s_addr)
  65. #define full_mask(x) (x)
  66. #else
  67. #define mask_in_addr(x) ((x).rt_genmask)
  68. #define full_mask(x) (((struct sockaddr_in *)&(x))->sin_addr.s_addr)
  69. #endif
  70. /* The RTACTION entries must agree with tbl_verb[] below! */
  71. #define RTACTION_ADD 1
  72. #define RTACTION_DEL 2
  73. /* For the various tbl_*[] arrays, the 1st byte is the offset to
  74. * the next entry and the 2nd byte is return value. */
  75. #define NET_FLAG 1
  76. #define HOST_FLAG 2
  77. /* We remap '-' to '#' to avoid problems with getopt. */
  78. static const char tbl_hash_net_host[] =
  79. "\007\001#net\0"
  80. /* "\010\002#host\0" */
  81. "\007\002#host" /* Since last, we can save a byte. */
  82. ;
  83. #define KW_TAKES_ARG 020
  84. #define KW_SETS_FLAG 040
  85. #define KW_IPVx_METRIC 020
  86. #define KW_IPVx_NETMASK 021
  87. #define KW_IPVx_GATEWAY 022
  88. #define KW_IPVx_MSS 023
  89. #define KW_IPVx_WINDOW 024
  90. #define KW_IPVx_IRTT 025
  91. #define KW_IPVx_DEVICE 026
  92. #define KW_IPVx_FLAG_ONLY 040
  93. #define KW_IPVx_REJECT 040
  94. #define KW_IPVx_MOD 041
  95. #define KW_IPVx_DYN 042
  96. #define KW_IPVx_REINSTATE 043
  97. static const char tbl_ipvx[] =
  98. /* 020 is the "takes an arg" bit */
  99. #if HAVE_NEW_ADDRT
  100. "\011\020metric\0"
  101. #endif
  102. "\012\021netmask\0"
  103. "\005\022gw\0"
  104. "\012\022gateway\0"
  105. "\006\023mss\0"
  106. "\011\024window\0"
  107. #ifdef RTF_IRTT
  108. "\007\025irtt\0"
  109. #endif
  110. "\006\026dev\0"
  111. "\011\026device\0"
  112. /* 040 is the "sets a flag" bit - MUST match flags_ipvx[] values below. */
  113. #ifdef RTF_REJECT
  114. "\011\040reject\0"
  115. #endif
  116. "\006\041mod\0"
  117. "\006\042dyn\0"
  118. /* "\014\043reinstate\0" */
  119. "\013\043reinstate" /* Since last, we can save a byte. */
  120. ;
  121. static const int flags_ipvx[] = { /* MUST match tbl_ipvx[] values above. */
  122. #ifdef RTF_REJECT
  123. RTF_REJECT,
  124. #endif
  125. RTF_MODIFIED,
  126. RTF_DYNAMIC,
  127. RTF_REINSTATE
  128. };
  129. static int kw_lookup(const char *kwtbl, char ***pargs)
  130. {
  131. if (**pargs) {
  132. do {
  133. if (strcmp(kwtbl+2, **pargs) == 0) { /* Found a match. */
  134. *pargs += 1;
  135. if (kwtbl[1] & KW_TAKES_ARG) {
  136. if (!**pargs) { /* No more args! */
  137. bb_show_usage();
  138. }
  139. *pargs += 1; /* Calling routine will use args[-1]. */
  140. }
  141. return kwtbl[1];
  142. }
  143. kwtbl += *kwtbl;
  144. } while (*kwtbl);
  145. }
  146. return 0;
  147. }
  148. /* Add or delete a route, depending on action. */
  149. static void INET_setroute(int action, char **args)
  150. {
  151. struct rtentry rt;
  152. const char *netmask;
  153. int skfd, isnet, xflag;
  154. assert((action == RTACTION_ADD) || (action == RTACTION_DEL));
  155. /* Grab the -net or -host options. Remember they were transformed. */
  156. xflag = kw_lookup(tbl_hash_net_host, &args);
  157. /* If we did grab -net or -host, make sure we still have an arg left. */
  158. if (*args == NULL) {
  159. bb_show_usage();
  160. }
  161. /* Clean out the RTREQ structure. */
  162. memset((char *) &rt, 0, sizeof(struct rtentry));
  163. {
  164. const char *target = *args++;
  165. /* Prefer hostname lookup is -host flag (xflag==1) was given. */
  166. isnet = INET_resolve(target, (struct sockaddr_in *) &rt.rt_dst,
  167. (xflag & HOST_FLAG));
  168. if (isnet < 0) {
  169. bb_error_msg_and_die("resolving %s", target);
  170. }
  171. }
  172. if (xflag) { /* Reinit isnet if -net or -host was specified. */
  173. isnet = (xflag & NET_FLAG);
  174. }
  175. /* Fill in the other fields. */
  176. rt.rt_flags = ((isnet) ? RTF_UP : (RTF_UP | RTF_HOST));
  177. netmask = bb_INET_default;
  178. while (*args) {
  179. int k = kw_lookup(tbl_ipvx, &args);
  180. const char *args_m1 = args[-1];
  181. if (k & KW_IPVx_FLAG_ONLY) {
  182. rt.rt_flags |= flags_ipvx[k & 3];
  183. continue;
  184. }
  185. #if HAVE_NEW_ADDRT
  186. if (k == KW_IPVx_METRIC) {
  187. rt.rt_metric = bb_xgetularg10(args_m1) + 1;
  188. continue;
  189. }
  190. #endif
  191. if (k == KW_IPVx_NETMASK) {
  192. struct sockaddr mask;
  193. if (mask_in_addr(rt)) {
  194. bb_show_usage();
  195. }
  196. netmask = args_m1;
  197. isnet = INET_resolve(netmask, (struct sockaddr_in *) &mask, 0);
  198. if (isnet < 0) {
  199. bb_error_msg_and_die("resolving %s", netmask);
  200. }
  201. rt.rt_genmask = full_mask(mask);
  202. continue;
  203. }
  204. if (k == KW_IPVx_GATEWAY) {
  205. if (rt.rt_flags & RTF_GATEWAY) {
  206. bb_show_usage();
  207. }
  208. isnet = INET_resolve(args_m1,
  209. (struct sockaddr_in *) &rt.rt_gateway, 1);
  210. rt.rt_flags |= RTF_GATEWAY;
  211. if (isnet) {
  212. if (isnet < 0) {
  213. bb_error_msg_and_die("resolving %s", args_m1);
  214. }
  215. bb_error_msg_and_die("gateway %s is a NETWORK", args_m1);
  216. }
  217. continue;
  218. }
  219. if (k == KW_IPVx_MSS) { /* Check valid MSS bounds. */
  220. rt.rt_flags |= RTF_MSS;
  221. rt.rt_mss = bb_xgetularg10_bnd(args_m1, 64, 32768);
  222. continue;
  223. }
  224. if (k == KW_IPVx_WINDOW) { /* Check valid window bounds. */
  225. rt.rt_flags |= RTF_WINDOW;
  226. rt.rt_window = bb_xgetularg10_bnd(args_m1, 128, INT_MAX);
  227. continue;
  228. }
  229. #ifdef RTF_IRTT
  230. if (k == KW_IPVx_IRTT) {
  231. rt.rt_flags |= RTF_IRTT;
  232. rt.rt_irtt = bb_xgetularg10(args_m1);
  233. rt.rt_irtt *= (sysconf(_SC_CLK_TCK) / 100); /* FIXME */
  234. #if 0 /* FIXME: do we need to check anything of this? */
  235. if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) {
  236. bb_error_msg_and_die("bad irtt");
  237. }
  238. #endif
  239. continue;
  240. }
  241. #endif
  242. /* Device is special in that it can be the last arg specified
  243. * and doesn't requre the dev/device keyword in that case. */
  244. if (!rt.rt_dev && ((k == KW_IPVx_DEVICE) || (!k && !*++args))) {
  245. /* Don't use args_m1 here since args may have changed! */
  246. rt.rt_dev = args[-1];
  247. continue;
  248. }
  249. /* Nothing matched. */
  250. bb_show_usage();
  251. }
  252. #ifdef RTF_REJECT
  253. if ((rt.rt_flags & RTF_REJECT) && !rt.rt_dev) {
  254. rt.rt_dev = "lo";
  255. }
  256. #endif
  257. /* sanity checks.. */
  258. if (mask_in_addr(rt)) {
  259. unsigned long mask = mask_in_addr(rt);
  260. mask = ~ntohl(mask);
  261. if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) {
  262. bb_error_msg_and_die("netmask %.8x and host route conflict",
  263. (unsigned int) mask);
  264. }
  265. if (mask & (mask + 1)) {
  266. bb_error_msg_and_die("bogus netmask %s", netmask);
  267. }
  268. mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr;
  269. if (mask & ~mask_in_addr(rt)) {
  270. bb_error_msg_and_die("netmask and route address conflict");
  271. }
  272. }
  273. /* Fill out netmask if still unset */
  274. if ((action == RTACTION_ADD) && (rt.rt_flags & RTF_HOST)) {
  275. mask_in_addr(rt) = 0xffffffff;
  276. }
  277. /* Create a socket to the INET kernel. */
  278. if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  279. bb_perror_msg_and_die("socket");
  280. }
  281. if (ioctl(skfd, ((action==RTACTION_ADD) ? SIOCADDRT : SIOCDELRT), &rt)<0) {
  282. bb_perror_msg_and_die("SIOC[ADD|DEL]RT");
  283. }
  284. /* Don't bother closing, as we're exiting after we return anyway. */
  285. /* close(skfd); */
  286. }
  287. #ifdef CONFIG_FEATURE_IPV6
  288. static void INET6_setroute(int action, char **args)
  289. {
  290. struct sockaddr_in6 sa6;
  291. struct in6_rtmsg rt;
  292. int prefix_len, skfd;
  293. const char *devname;
  294. assert((action == RTACTION_ADD) || (action == RTACTION_DEL));
  295. {
  296. /* We know args isn't NULL from the check in route_main. */
  297. const char *target = *args++;
  298. if (strcmp(target, "default") == 0) {
  299. prefix_len = 0;
  300. memset(&sa6, 0, sizeof(sa6));
  301. } else {
  302. char *cp;
  303. if ((cp = strchr(target, '/'))) { /* Yes... const to non is ok. */
  304. *cp = 0;
  305. prefix_len = bb_xgetularg10_bnd(cp+1, 0, 128);
  306. } else {
  307. prefix_len = 128;
  308. }
  309. if (INET6_resolve(target, (struct sockaddr_in6 *) &sa6) < 0) {
  310. bb_error_msg_and_die("resolving %s", target);
  311. }
  312. }
  313. }
  314. /* Clean out the RTREQ structure. */
  315. memset((char *) &rt, 0, sizeof(struct in6_rtmsg));
  316. memcpy(&rt.rtmsg_dst, sa6.sin6_addr.s6_addr, sizeof(struct in6_addr));
  317. /* Fill in the other fields. */
  318. rt.rtmsg_dst_len = prefix_len;
  319. rt.rtmsg_flags = ((prefix_len == 128) ? (RTF_UP|RTF_HOST) : RTF_UP);
  320. rt.rtmsg_metric = 1;
  321. devname = NULL;
  322. while (*args) {
  323. int k = kw_lookup(tbl_ipvx, &args);
  324. const char *args_m1 = args[-1];
  325. if ((k == KW_IPVx_MOD) || (k == KW_IPVx_DYN)) {
  326. rt.rtmsg_flags |= flags_ipvx[k & 3];
  327. continue;
  328. }
  329. if (k == KW_IPVx_METRIC) {
  330. rt.rtmsg_metric = bb_xgetularg10(args_m1);
  331. continue;
  332. }
  333. if (k == KW_IPVx_GATEWAY) {
  334. if (rt.rtmsg_flags & RTF_GATEWAY) {
  335. bb_show_usage();
  336. }
  337. if (INET6_resolve(args_m1, (struct sockaddr_in6 *) &sa6) < 0) {
  338. bb_error_msg_and_die("resolving %s", args_m1);
  339. }
  340. memcpy(&rt.rtmsg_gateway, sa6.sin6_addr.s6_addr,
  341. sizeof(struct in6_addr));
  342. rt.rtmsg_flags |= RTF_GATEWAY;
  343. continue;
  344. }
  345. /* Device is special in that it can be the last arg specified
  346. * and doesn't requre the dev/device keyword in that case. */
  347. if (!devname && ((k == KW_IPVx_DEVICE) || (!k && !*++args))) {
  348. /* Don't use args_m1 here since args may have changed! */
  349. devname = args[-1];
  350. continue;
  351. }
  352. /* Nothing matched. */
  353. bb_show_usage();
  354. }
  355. /* Create a socket to the INET6 kernel. */
  356. if ((skfd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
  357. bb_perror_msg_and_die("socket");
  358. }
  359. rt.rtmsg_ifindex = 0;
  360. if (devname) {
  361. struct ifreq ifr;
  362. memset(&ifr, 0, sizeof(ifr));
  363. strcpy(ifr.ifr_name, devname);
  364. if (ioctl(skfd, SIOGIFINDEX, &ifr) < 0) {
  365. bb_perror_msg_and_die("SIOGIFINDEX");
  366. }
  367. rt.rtmsg_ifindex = ifr.ifr_ifindex;
  368. }
  369. /* Tell the kernel to accept this route. */
  370. if (ioctl(skfd, ((action==RTACTION_ADD) ? SIOCADDRT : SIOCDELRT), &rt)<0) {
  371. bb_perror_msg_and_die("SIOC[ADD|DEL]RT");
  372. }
  373. /* Don't bother closing, as we're exiting after we return anyway. */
  374. /* close(skfd); */
  375. }
  376. #endif
  377. static const unsigned int flagvals[] = { /* Must agree with flagchars[]. */
  378. RTF_GATEWAY,
  379. RTF_HOST,
  380. RTF_REINSTATE,
  381. RTF_DYNAMIC,
  382. RTF_MODIFIED,
  383. #ifdef CONFIG_FEATURE_IPV6
  384. RTF_DEFAULT,
  385. RTF_ADDRCONF,
  386. RTF_CACHE
  387. #endif
  388. };
  389. #define IPV4_MASK (RTF_GATEWAY|RTF_HOST|RTF_REINSTATE|RTF_DYNAMIC|RTF_MODIFIED)
  390. #define IPV6_MASK (RTF_GATEWAY|RTF_HOST|RTF_DEFAULT|RTF_ADDRCONF|RTF_CACHE)
  391. static const char flagchars[] = /* Must agree with flagvals[]. */
  392. "GHRDM"
  393. #ifdef CONFIG_FEATURE_IPV6
  394. "DAC"
  395. #endif
  396. ;
  397. static
  398. #ifndef CONFIG_FEATURE_IPV6
  399. __inline
  400. #endif
  401. void set_flags(char *flagstr, int flags)
  402. {
  403. int i;
  404. *flagstr++ = 'U';
  405. for (i=0 ; (*flagstr = flagchars[i]) != 0 ; i++) {
  406. if (flags & flagvals[i]) {
  407. ++flagstr;
  408. }
  409. }
  410. }
  411. /* also used in netstat */
  412. void displayroutes(int noresolve, int netstatfmt)
  413. {
  414. char devname[64], flags[16], sdest[16], sgw[16];
  415. unsigned long int d, g, m;
  416. int flgs, ref, use, metric, mtu, win, ir;
  417. struct sockaddr_in s_addr;
  418. struct in_addr mask;
  419. FILE *fp = bb_xfopen("/proc/net/route", "r");
  420. bb_printf("Kernel IP routing table\n"
  421. "Destination Gateway Genmask"
  422. " Flags %s Iface\n",
  423. netstatfmt ? " MSS Window irtt" : "Metric Ref Use");
  424. if (fscanf(fp, "%*[^\n]\n") < 0) { /* Skip the first line. */
  425. goto ERROR; /* Empty or missing line, or read error. */
  426. }
  427. while (1) {
  428. int r;
  429. r = fscanf(fp, "%63s%lx%lx%X%d%d%d%lx%d%d%d\n",
  430. devname, &d, &g, &flgs, &ref, &use, &metric, &m,
  431. &mtu, &win, &ir);
  432. if (r != 11) {
  433. if ((r < 0) && feof(fp)) { /* EOF with no (nonspace) chars read. */
  434. break;
  435. }
  436. ERROR:
  437. bb_error_msg_and_die("fscanf");
  438. }
  439. if (!(flgs & RTF_UP)) { /* Skip interfaces that are down. */
  440. continue;
  441. }
  442. set_flags(flags, (flgs & IPV4_MASK));
  443. #ifdef RTF_REJECT
  444. if (flgs & RTF_REJECT) {
  445. flags[0] = '!';
  446. }
  447. #endif
  448. memset(&s_addr, 0, sizeof(struct sockaddr_in));
  449. s_addr.sin_family = AF_INET;
  450. s_addr.sin_addr.s_addr = d;
  451. INET_rresolve(sdest, sizeof(sdest), &s_addr,
  452. (noresolve | 0x8000), m); /* Default instead of *. */
  453. s_addr.sin_addr.s_addr = g;
  454. INET_rresolve(sgw, sizeof(sgw), &s_addr,
  455. (noresolve | 0x4000), m); /* Host instead of net. */
  456. mask.s_addr = m;
  457. bb_printf("%-16s%-16s%-16s%-6s", sdest, sgw, inet_ntoa(mask), flags);
  458. if (netstatfmt) {
  459. bb_printf("%5d %-5d %6d %s\n", mtu, win, ir, devname);
  460. } else {
  461. bb_printf("%-6d %-2d %7d %s\n", metric, ref, use, devname);
  462. }
  463. }
  464. }
  465. #ifdef CONFIG_FEATURE_IPV6
  466. static void INET6_displayroutes(int noresolve)
  467. {
  468. char addr6[128], naddr6[128];
  469. /* In addr6x, we store both 40-byte ':'-delimited ipv6 addresses.
  470. * We read the non-delimited strings into the tail of the buffer
  471. * using fscanf and then modify the buffer by shifting forward
  472. * while inserting ':'s and the nul terminator for the first string.
  473. * Hence the strings are at addr6x and addr6x+40. This generates
  474. * _much_ less code than the previous (upstream) approach. */
  475. char addr6x[80];
  476. char iface[16], flags[16];
  477. int iflags, metric, refcnt, use, prefix_len, slen;
  478. struct sockaddr_in6 snaddr6;
  479. FILE *fp = bb_xfopen("/proc/net/ipv6_route", "r");
  480. bb_printf("Kernel IPv6 routing table\n%-44s%-40s"
  481. "Flags Metric Ref Use Iface\n",
  482. "Destination", "Next Hop");
  483. while (1) {
  484. int r;
  485. r = fscanf(fp, "%32s%x%*s%x%32s%x%x%x%x%s\n",
  486. addr6x+14, &prefix_len, &slen, addr6x+40+7,
  487. &metric, &use, &refcnt, &iflags, iface);
  488. if (r != 9) {
  489. if ((r < 0) && feof(fp)) { /* EOF with no (nonspace) chars read. */
  490. break;
  491. }
  492. ERROR:
  493. bb_error_msg_and_die("fscanf");
  494. }
  495. /* Do the addr6x shift-and-insert changes to ':'-delimit addresses.
  496. * For now, always do this to validate the proc route format, even
  497. * if the interface is down. */
  498. {
  499. int i = 0;
  500. char *p = addr6x+14;
  501. do {
  502. if (!*p) {
  503. if (i==40) { /* nul terminator for 1st address? */
  504. addr6x[39] = 0; /* Fixup... need 0 instead of ':'. */
  505. ++p; /* Skip and continue. */
  506. continue;
  507. }
  508. goto ERROR;
  509. }
  510. addr6x[i++] = *p++;
  511. if (!((i+1)%5)) {
  512. addr6x[i++] = ':';
  513. }
  514. } while (i < 40+28+7);
  515. }
  516. if (!(iflags & RTF_UP)) { /* Skip interfaces that are down. */
  517. continue;
  518. }
  519. set_flags(flags, (iflags & IPV6_MASK));
  520. r = 0;
  521. do {
  522. inet_pton(AF_INET6, addr6x + r,
  523. (struct sockaddr *) &snaddr6.sin6_addr);
  524. snaddr6.sin6_family = AF_INET6;
  525. INET6_rresolve(naddr6, sizeof(naddr6),
  526. (struct sockaddr_in6 *) &snaddr6,
  527. #if 0
  528. (noresolve | 0x8000) /* Default instead of *. */
  529. #else
  530. 0x0fff /* Apparently, upstream never resolves. */
  531. #endif
  532. );
  533. if (!r) { /* 1st pass */
  534. snprintf(addr6, sizeof(addr6), "%s/%d", naddr6, prefix_len);
  535. r += 40;
  536. } else { /* 2nd pass */
  537. /* Print the info. */
  538. bb_printf("%-43s %-39s %-5s %-6d %-2d %7d %-8s\n",
  539. addr6, naddr6, flags, metric, refcnt, use, iface);
  540. break;
  541. }
  542. } while (1);
  543. }
  544. }
  545. #endif
  546. #define ROUTE_OPT_A 0x01
  547. #define ROUTE_OPT_n 0x02
  548. #define ROUTE_OPT_e 0x04
  549. #define ROUTE_OPT_INET6 0x08 /* Not an actual option. See below. */
  550. /* 1st byte is offset to next entry offset. 2nd byte is return value. */
  551. static const char tbl_verb[] = /* 2nd byte matches RTACTION_* code */
  552. "\006\001add\0"
  553. "\006\002del\0"
  554. /* "\011\002delete\0" */
  555. "\010\002delete" /* Since last, we can save a byte. */
  556. ;
  557. int route_main(int argc, char **argv)
  558. {
  559. unsigned long opt;
  560. int what;
  561. char *family;
  562. /* First, remap '-net' and '-host' to avoid getopt problems. */
  563. {
  564. char **p = argv;
  565. while (*++p) {
  566. if ((strcmp(*p, "-net") == 0) || (strcmp(*p, "-host") == 0)) {
  567. p[0][0] = '#';
  568. }
  569. }
  570. }
  571. opt = bb_getopt_ulflags(argc, argv, "A:ne", &family);
  572. if ((opt & ROUTE_OPT_A) && strcmp(family, "inet")) {
  573. #ifdef CONFIG_FEATURE_IPV6
  574. if (strcmp(family, "inet6") == 0) {
  575. opt |= ROUTE_OPT_INET6; /* Set flag for ipv6. */
  576. } else
  577. #endif
  578. bb_show_usage();
  579. }
  580. argv += optind;
  581. /* No more args means display the routing table. */
  582. if (!*argv) {
  583. int noresolve = (opt & ROUTE_OPT_n) ? 0x0fff : 0;
  584. #ifdef CONFIG_FEATURE_IPV6
  585. if (opt & ROUTE_OPT_INET6)
  586. INET6_displayroutes(noresolve);
  587. else
  588. #endif
  589. displayroutes(noresolve, opt & ROUTE_OPT_e);
  590. bb_xferror_stdout();
  591. bb_fflush_stdout_and_exit(EXIT_SUCCESS);
  592. }
  593. /* Check verb. At the moment, must be add, del, or delete. */
  594. what = kw_lookup(tbl_verb, &argv);
  595. if (!what || !*argv) { /* Unknown verb or no more args. */
  596. bb_show_usage();
  597. }
  598. #ifdef CONFIG_FEATURE_IPV6
  599. if (opt & ROUTE_OPT_INET6)
  600. INET6_setroute(what, argv);
  601. else
  602. #endif
  603. INET_setroute(what, argv);
  604. return EXIT_SUCCESS;
  605. }