route.c 18 KB

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