route.c 18 KB

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