route.c 18 KB

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