route.c 17 KB

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