3
0

iproute.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. *
  7. * Changes:
  8. *
  9. * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
  10. * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
  11. */
  12. #include "ip_common.h" /* #include "libbb.h" is inside */
  13. #include "rt_names.h"
  14. #include "utils.h"
  15. #ifndef RTAX_RTTVAR
  16. #define RTAX_RTTVAR RTAX_HOPS
  17. #endif
  18. struct filter_t {
  19. int tb;
  20. smallint flushed;
  21. char *flushb;
  22. int flushp;
  23. int flushe;
  24. struct rtnl_handle *rth;
  25. //int protocol, protocolmask; - write-only fields?!
  26. //int scope, scopemask; - unused
  27. //int type; - read-only
  28. //int typemask; - unused
  29. //int tos, tosmask; - unused
  30. int iif;
  31. int oif;
  32. //int realm, realmmask; - unused
  33. //inet_prefix rprefsrc; - read-only
  34. inet_prefix rvia;
  35. inet_prefix rdst;
  36. inet_prefix mdst;
  37. inet_prefix rsrc;
  38. inet_prefix msrc;
  39. } FIX_ALIASING;
  40. typedef struct filter_t filter_t;
  41. #define G_filter (*(filter_t*)&bb_common_bufsiz1)
  42. static int flush_update(void)
  43. {
  44. if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
  45. bb_perror_msg("can't send flush request");
  46. return -1;
  47. }
  48. G_filter.flushp = 0;
  49. return 0;
  50. }
  51. static unsigned get_hz(void)
  52. {
  53. static unsigned hz_internal;
  54. FILE *fp;
  55. if (hz_internal)
  56. return hz_internal;
  57. fp = fopen_for_read("/proc/net/psched");
  58. if (fp) {
  59. unsigned nom, denom;
  60. if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
  61. if (nom == 1000000)
  62. hz_internal = denom;
  63. fclose(fp);
  64. }
  65. if (!hz_internal)
  66. hz_internal = sysconf(_SC_CLK_TCK);
  67. return hz_internal;
  68. }
  69. static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
  70. struct nlmsghdr *n, void *arg UNUSED_PARAM)
  71. {
  72. struct rtmsg *r = NLMSG_DATA(n);
  73. int len = n->nlmsg_len;
  74. struct rtattr *tb[RTA_MAX+1];
  75. char abuf[256];
  76. inet_prefix dst;
  77. inet_prefix src;
  78. int host_len = -1;
  79. SPRINT_BUF(b1);
  80. if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
  81. fprintf(stderr, "Not a route: %08x %08x %08x\n",
  82. n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
  83. return 0;
  84. }
  85. if (G_filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
  86. return 0;
  87. len -= NLMSG_LENGTH(sizeof(*r));
  88. if (len < 0)
  89. bb_error_msg_and_die("wrong nlmsg len %d", len);
  90. if (r->rtm_family == AF_INET6)
  91. host_len = 128;
  92. else if (r->rtm_family == AF_INET)
  93. host_len = 32;
  94. if (r->rtm_family == AF_INET6) {
  95. if (G_filter.tb) {
  96. if (G_filter.tb < 0) {
  97. if (!(r->rtm_flags & RTM_F_CLONED)) {
  98. return 0;
  99. }
  100. } else {
  101. if (r->rtm_flags & RTM_F_CLONED) {
  102. return 0;
  103. }
  104. if (G_filter.tb == RT_TABLE_LOCAL) {
  105. if (r->rtm_type != RTN_LOCAL) {
  106. return 0;
  107. }
  108. } else if (G_filter.tb == RT_TABLE_MAIN) {
  109. if (r->rtm_type == RTN_LOCAL) {
  110. return 0;
  111. }
  112. } else {
  113. return 0;
  114. }
  115. }
  116. }
  117. } else {
  118. if (G_filter.tb > 0 && G_filter.tb != r->rtm_table) {
  119. return 0;
  120. }
  121. }
  122. if (G_filter.rdst.family
  123. && (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
  124. ) {
  125. return 0;
  126. }
  127. if (G_filter.mdst.family
  128. && (r->rtm_family != G_filter.mdst.family
  129. || (G_filter.mdst.bitlen >= 0 && G_filter.mdst.bitlen < r->rtm_dst_len)
  130. )
  131. ) {
  132. return 0;
  133. }
  134. if (G_filter.rsrc.family
  135. && (r->rtm_family != G_filter.rsrc.family || G_filter.rsrc.bitlen > r->rtm_src_len)
  136. ) {
  137. return 0;
  138. }
  139. if (G_filter.msrc.family
  140. && (r->rtm_family != G_filter.msrc.family
  141. || (G_filter.msrc.bitlen >= 0 && G_filter.msrc.bitlen < r->rtm_src_len)
  142. )
  143. ) {
  144. return 0;
  145. }
  146. memset(tb, 0, sizeof(tb));
  147. memset(&src, 0, sizeof(src));
  148. memset(&dst, 0, sizeof(dst));
  149. parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
  150. if (tb[RTA_SRC]) {
  151. src.bitlen = r->rtm_src_len;
  152. src.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
  153. memcpy(src.data, RTA_DATA(tb[RTA_SRC]), src.bytelen);
  154. }
  155. if (tb[RTA_DST]) {
  156. dst.bitlen = r->rtm_dst_len;
  157. dst.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
  158. memcpy(dst.data, RTA_DATA(tb[RTA_DST]), dst.bytelen);
  159. }
  160. if (G_filter.rdst.family
  161. && inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen)
  162. ) {
  163. return 0;
  164. }
  165. if (G_filter.mdst.family
  166. && G_filter.mdst.bitlen >= 0
  167. && inet_addr_match(&dst, &G_filter.mdst, r->rtm_dst_len)
  168. ) {
  169. return 0;
  170. }
  171. if (G_filter.rsrc.family
  172. && inet_addr_match(&src, &G_filter.rsrc, G_filter.rsrc.bitlen)
  173. ) {
  174. return 0;
  175. }
  176. if (G_filter.msrc.family && G_filter.msrc.bitlen >= 0
  177. && inet_addr_match(&src, &G_filter.msrc, r->rtm_src_len)
  178. ) {
  179. return 0;
  180. }
  181. if (G_filter.oif != 0) {
  182. if (!tb[RTA_OIF])
  183. return 0;
  184. if (G_filter.oif != *(int*)RTA_DATA(tb[RTA_OIF]))
  185. return 0;
  186. }
  187. if (G_filter.flushb) {
  188. struct nlmsghdr *fn;
  189. /* We are creating route flush commands */
  190. if (r->rtm_family == AF_INET6
  191. && r->rtm_dst_len == 0
  192. && r->rtm_type == RTN_UNREACHABLE
  193. && tb[RTA_PRIORITY]
  194. && *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1
  195. ) {
  196. return 0;
  197. }
  198. if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
  199. if (flush_update())
  200. bb_error_msg_and_die("flush");
  201. }
  202. fn = (void*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
  203. memcpy(fn, n, n->nlmsg_len);
  204. fn->nlmsg_type = RTM_DELROUTE;
  205. fn->nlmsg_flags = NLM_F_REQUEST;
  206. fn->nlmsg_seq = ++G_filter.rth->seq;
  207. G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
  208. G_filter.flushed = 1;
  209. return 0;
  210. }
  211. /* We are printing routes */
  212. if (n->nlmsg_type == RTM_DELROUTE) {
  213. printf("Deleted ");
  214. }
  215. if (r->rtm_type != RTN_UNICAST /* && !G_filter.type - always 0 */) {
  216. printf("%s ", rtnl_rtntype_n2a(r->rtm_type, b1));
  217. }
  218. if (tb[RTA_DST]) {
  219. if (r->rtm_dst_len != host_len) {
  220. printf("%s/%u ", rt_addr_n2a(r->rtm_family,
  221. RTA_DATA(tb[RTA_DST]),
  222. abuf, sizeof(abuf)),
  223. r->rtm_dst_len
  224. );
  225. } else {
  226. printf("%s ", format_host(r->rtm_family,
  227. RTA_PAYLOAD(tb[RTA_DST]),
  228. RTA_DATA(tb[RTA_DST]),
  229. abuf, sizeof(abuf))
  230. );
  231. }
  232. } else if (r->rtm_dst_len) {
  233. printf("0/%d ", r->rtm_dst_len);
  234. } else {
  235. printf("default ");
  236. }
  237. if (tb[RTA_SRC]) {
  238. if (r->rtm_src_len != host_len) {
  239. printf("from %s/%u ", rt_addr_n2a(r->rtm_family,
  240. RTA_DATA(tb[RTA_SRC]),
  241. abuf, sizeof(abuf)),
  242. r->rtm_src_len
  243. );
  244. } else {
  245. printf("from %s ", format_host(r->rtm_family,
  246. RTA_PAYLOAD(tb[RTA_SRC]),
  247. RTA_DATA(tb[RTA_SRC]),
  248. abuf, sizeof(abuf))
  249. );
  250. }
  251. } else if (r->rtm_src_len) {
  252. printf("from 0/%u ", r->rtm_src_len);
  253. }
  254. if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) {
  255. printf("via %s ", format_host(r->rtm_family,
  256. RTA_PAYLOAD(tb[RTA_GATEWAY]),
  257. RTA_DATA(tb[RTA_GATEWAY]),
  258. abuf, sizeof(abuf)));
  259. }
  260. if (tb[RTA_OIF]) {
  261. printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
  262. }
  263. /* Todo: parse & show "proto kernel", "scope link" here */
  264. if (tb[RTA_PREFSRC] && /*G_filter.rprefsrc.bitlen - always 0*/ 0 != host_len) {
  265. /* Do not use format_host(). It is our local addr
  266. and symbolic name will not be useful.
  267. */
  268. printf(" src %s ", rt_addr_n2a(r->rtm_family,
  269. RTA_DATA(tb[RTA_PREFSRC]),
  270. abuf, sizeof(abuf)));
  271. }
  272. if (tb[RTA_PRIORITY]) {
  273. printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
  274. }
  275. if (r->rtm_family == AF_INET6) {
  276. struct rta_cacheinfo *ci = NULL;
  277. if (tb[RTA_CACHEINFO]) {
  278. ci = RTA_DATA(tb[RTA_CACHEINFO]);
  279. }
  280. if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
  281. if (r->rtm_flags & RTM_F_CLONED) {
  282. printf("%c cache ", _SL_);
  283. }
  284. if (ci->rta_expires) {
  285. printf(" expires %dsec", ci->rta_expires / get_hz());
  286. }
  287. if (ci->rta_error != 0) {
  288. printf(" error %d", ci->rta_error);
  289. }
  290. } else if (ci) {
  291. if (ci->rta_error != 0)
  292. printf(" error %d", ci->rta_error);
  293. }
  294. }
  295. if (tb[RTA_IIF] && G_filter.iif == 0) {
  296. printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
  297. }
  298. bb_putchar('\n');
  299. return 0;
  300. }
  301. /* Return value becomes exitcode. It's okay to not return at all */
  302. static int iproute_modify(int cmd, unsigned flags, char **argv)
  303. {
  304. static const char keywords[] ALIGN1 =
  305. "src\0""via\0""mtu\0""lock\0""protocol\0"IF_FEATURE_IP_RULE("table\0")
  306. "dev\0""oif\0""to\0""metric\0";
  307. enum {
  308. ARG_src,
  309. ARG_via,
  310. ARG_mtu, PARM_lock,
  311. ARG_protocol,
  312. IF_FEATURE_IP_RULE(ARG_table,)
  313. ARG_dev,
  314. ARG_oif,
  315. ARG_to,
  316. ARG_metric,
  317. };
  318. enum {
  319. gw_ok = 1 << 0,
  320. dst_ok = 1 << 1,
  321. proto_ok = 1 << 2,
  322. type_ok = 1 << 3
  323. };
  324. struct rtnl_handle rth;
  325. struct {
  326. struct nlmsghdr n;
  327. struct rtmsg r;
  328. char buf[1024];
  329. } req;
  330. char mxbuf[256];
  331. struct rtattr * mxrta = (void*)mxbuf;
  332. unsigned mxlock = 0;
  333. char *d = NULL;
  334. smalluint ok = 0;
  335. int arg;
  336. memset(&req, 0, sizeof(req));
  337. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  338. req.n.nlmsg_flags = NLM_F_REQUEST | flags;
  339. req.n.nlmsg_type = cmd;
  340. req.r.rtm_family = preferred_family;
  341. if (RT_TABLE_MAIN) /* if it is zero, memset already did it */
  342. req.r.rtm_table = RT_TABLE_MAIN;
  343. if (RT_SCOPE_NOWHERE)
  344. req.r.rtm_scope = RT_SCOPE_NOWHERE;
  345. if (cmd != RTM_DELROUTE) {
  346. req.r.rtm_protocol = RTPROT_BOOT;
  347. req.r.rtm_scope = RT_SCOPE_UNIVERSE;
  348. req.r.rtm_type = RTN_UNICAST;
  349. }
  350. mxrta->rta_type = RTA_METRICS;
  351. mxrta->rta_len = RTA_LENGTH(0);
  352. while (*argv) {
  353. arg = index_in_substrings(keywords, *argv);
  354. if (arg == ARG_src) {
  355. inet_prefix addr;
  356. NEXT_ARG();
  357. get_addr(&addr, *argv, req.r.rtm_family);
  358. if (req.r.rtm_family == AF_UNSPEC)
  359. req.r.rtm_family = addr.family;
  360. addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
  361. } else if (arg == ARG_via) {
  362. inet_prefix addr;
  363. ok |= gw_ok;
  364. NEXT_ARG();
  365. get_addr(&addr, *argv, req.r.rtm_family);
  366. if (req.r.rtm_family == AF_UNSPEC) {
  367. req.r.rtm_family = addr.family;
  368. }
  369. addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
  370. } else if (arg == ARG_mtu) {
  371. unsigned mtu;
  372. NEXT_ARG();
  373. if (index_in_strings(keywords, *argv) == PARM_lock) {
  374. mxlock |= (1 << RTAX_MTU);
  375. NEXT_ARG();
  376. }
  377. mtu = get_unsigned(*argv, "mtu");
  378. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
  379. } else if (arg == ARG_protocol) {
  380. uint32_t prot;
  381. NEXT_ARG();
  382. if (rtnl_rtprot_a2n(&prot, *argv))
  383. invarg(*argv, "protocol");
  384. req.r.rtm_protocol = prot;
  385. ok |= proto_ok;
  386. #if ENABLE_FEATURE_IP_RULE
  387. } else if (arg == ARG_table) {
  388. uint32_t tid;
  389. NEXT_ARG();
  390. if (rtnl_rttable_a2n(&tid, *argv))
  391. invarg(*argv, "table");
  392. req.r.rtm_table = tid;
  393. #endif
  394. } else if (arg == ARG_dev || arg == ARG_oif) {
  395. NEXT_ARG();
  396. d = *argv;
  397. } else if (arg == ARG_metric) {
  398. uint32_t metric;
  399. NEXT_ARG();
  400. metric = get_u32(*argv, "metric");
  401. addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
  402. } else {
  403. int type;
  404. inet_prefix dst;
  405. if (arg == ARG_to) {
  406. NEXT_ARG();
  407. }
  408. if ((**argv < '0' || **argv > '9')
  409. && rtnl_rtntype_a2n(&type, *argv) == 0
  410. ) {
  411. NEXT_ARG();
  412. req.r.rtm_type = type;
  413. ok |= type_ok;
  414. }
  415. if (ok & dst_ok) {
  416. duparg2("to", *argv);
  417. }
  418. get_prefix(&dst, *argv, req.r.rtm_family);
  419. if (req.r.rtm_family == AF_UNSPEC) {
  420. req.r.rtm_family = dst.family;
  421. }
  422. req.r.rtm_dst_len = dst.bitlen;
  423. ok |= dst_ok;
  424. if (dst.bytelen) {
  425. addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
  426. }
  427. }
  428. argv++;
  429. }
  430. xrtnl_open(&rth);
  431. if (d) {
  432. int idx;
  433. ll_init_map(&rth);
  434. if (d) {
  435. idx = xll_name_to_index(d);
  436. addattr32(&req.n, sizeof(req), RTA_OIF, idx);
  437. }
  438. }
  439. if (mxrta->rta_len > RTA_LENGTH(0)) {
  440. if (mxlock) {
  441. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
  442. }
  443. addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
  444. }
  445. if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
  446. req.r.rtm_scope = RT_SCOPE_HOST;
  447. else
  448. if (req.r.rtm_type == RTN_BROADCAST
  449. || req.r.rtm_type == RTN_MULTICAST
  450. || req.r.rtm_type == RTN_ANYCAST
  451. ) {
  452. req.r.rtm_scope = RT_SCOPE_LINK;
  453. }
  454. else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
  455. if (cmd == RTM_DELROUTE)
  456. req.r.rtm_scope = RT_SCOPE_NOWHERE;
  457. else if (!(ok & gw_ok))
  458. req.r.rtm_scope = RT_SCOPE_LINK;
  459. }
  460. if (req.r.rtm_family == AF_UNSPEC) {
  461. req.r.rtm_family = AF_INET;
  462. }
  463. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
  464. return 2;
  465. }
  466. return 0;
  467. }
  468. static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
  469. {
  470. struct {
  471. struct nlmsghdr nlh;
  472. struct rtmsg rtm;
  473. } req;
  474. struct sockaddr_nl nladdr;
  475. memset(&nladdr, 0, sizeof(nladdr));
  476. memset(&req, 0, sizeof(req));
  477. nladdr.nl_family = AF_NETLINK;
  478. req.nlh.nlmsg_len = sizeof(req);
  479. if (RTM_GETROUTE)
  480. req.nlh.nlmsg_type = RTM_GETROUTE;
  481. if (NLM_F_ROOT | NLM_F_REQUEST)
  482. req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
  483. /*req.nlh.nlmsg_pid = 0; - memset did it already */
  484. req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
  485. req.rtm.rtm_family = family;
  486. if (RTM_F_CLONED)
  487. req.rtm.rtm_flags = RTM_F_CLONED;
  488. return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
  489. }
  490. static void iproute_flush_cache(void)
  491. {
  492. static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
  493. int flush_fd = open_or_warn(fn, O_WRONLY);
  494. if (flush_fd < 0) {
  495. return;
  496. }
  497. if (write(flush_fd, "-1", 2) < 2) {
  498. bb_perror_msg("can't flush routing cache");
  499. return;
  500. }
  501. close(flush_fd);
  502. }
  503. static void iproute_reset_filter(void)
  504. {
  505. memset(&G_filter, 0, sizeof(G_filter));
  506. G_filter.mdst.bitlen = -1;
  507. G_filter.msrc.bitlen = -1;
  508. }
  509. /* Return value becomes exitcode. It's okay to not return at all */
  510. static int iproute_list_or_flush(char **argv, int flush)
  511. {
  512. int do_ipv6 = preferred_family;
  513. struct rtnl_handle rth;
  514. char *id = NULL;
  515. char *od = NULL;
  516. static const char keywords[] ALIGN1 =
  517. /* "ip route list/flush" parameters: */
  518. "protocol\0" "dev\0" "oif\0" "iif\0"
  519. "via\0" "table\0" "cache\0"
  520. "from\0" "to\0"
  521. /* and possible further keywords */
  522. "all\0"
  523. "root\0"
  524. "match\0"
  525. "exact\0"
  526. "main\0"
  527. ;
  528. enum {
  529. KW_proto, KW_dev, KW_oif, KW_iif,
  530. KW_via, KW_table, KW_cache,
  531. KW_from, KW_to,
  532. /* */
  533. KW_all,
  534. KW_root,
  535. KW_match,
  536. KW_exact,
  537. KW_main,
  538. };
  539. int arg, parm;
  540. iproute_reset_filter();
  541. G_filter.tb = RT_TABLE_MAIN;
  542. if (flush && !*argv)
  543. bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
  544. while (*argv) {
  545. arg = index_in_substrings(keywords, *argv);
  546. if (arg == KW_proto) {
  547. uint32_t prot = 0;
  548. NEXT_ARG();
  549. //G_filter.protocolmask = -1;
  550. if (rtnl_rtprot_a2n(&prot, *argv)) {
  551. if (index_in_strings(keywords, *argv) != KW_all)
  552. invarg(*argv, "protocol");
  553. prot = 0;
  554. //G_filter.protocolmask = 0;
  555. }
  556. //G_filter.protocol = prot;
  557. } else if (arg == KW_dev || arg == KW_oif) {
  558. NEXT_ARG();
  559. od = *argv;
  560. } else if (arg == KW_iif) {
  561. NEXT_ARG();
  562. id = *argv;
  563. } else if (arg == KW_via) {
  564. NEXT_ARG();
  565. get_prefix(&G_filter.rvia, *argv, do_ipv6);
  566. } else if (arg == KW_table) { /* table all/cache/main */
  567. NEXT_ARG();
  568. parm = index_in_substrings(keywords, *argv);
  569. if (parm == KW_cache)
  570. G_filter.tb = -1;
  571. else if (parm == KW_all)
  572. G_filter.tb = 0;
  573. else if (parm != KW_main) {
  574. #if ENABLE_FEATURE_IP_RULE
  575. uint32_t tid;
  576. if (rtnl_rttable_a2n(&tid, *argv))
  577. invarg(*argv, "table");
  578. G_filter.tb = tid;
  579. #else
  580. invarg(*argv, "table");
  581. #endif
  582. }
  583. } else if (arg == KW_cache) {
  584. /* The command 'ip route flush cache' is used by OpenSWAN.
  585. * Assuming it's a synonym for 'ip route flush table cache' */
  586. G_filter.tb = -1;
  587. } else if (arg == KW_from) {
  588. NEXT_ARG();
  589. parm = index_in_substrings(keywords, *argv);
  590. if (parm == KW_root) {
  591. NEXT_ARG();
  592. get_prefix(&G_filter.rsrc, *argv, do_ipv6);
  593. } else if (parm == KW_match) {
  594. NEXT_ARG();
  595. get_prefix(&G_filter.msrc, *argv, do_ipv6);
  596. } else {
  597. if (parm == KW_exact)
  598. NEXT_ARG();
  599. get_prefix(&G_filter.msrc, *argv, do_ipv6);
  600. G_filter.rsrc = G_filter.msrc;
  601. }
  602. } else { /* "to" is the default parameter */
  603. if (arg == KW_to) {
  604. NEXT_ARG();
  605. arg = index_in_substrings(keywords, *argv);
  606. }
  607. /* parm = arg; - would be more plausible, but we reuse 'arg' here */
  608. if (arg == KW_root) {
  609. NEXT_ARG();
  610. get_prefix(&G_filter.rdst, *argv, do_ipv6);
  611. } else if (arg == KW_match) {
  612. NEXT_ARG();
  613. get_prefix(&G_filter.mdst, *argv, do_ipv6);
  614. } else { /* "to exact" is the default */
  615. if (arg == KW_exact)
  616. NEXT_ARG();
  617. get_prefix(&G_filter.mdst, *argv, do_ipv6);
  618. G_filter.rdst = G_filter.mdst;
  619. }
  620. }
  621. argv++;
  622. }
  623. if (do_ipv6 == AF_UNSPEC && G_filter.tb) {
  624. do_ipv6 = AF_INET;
  625. }
  626. xrtnl_open(&rth);
  627. ll_init_map(&rth);
  628. if (id || od) {
  629. int idx;
  630. if (id) {
  631. idx = xll_name_to_index(id);
  632. G_filter.iif = idx;
  633. }
  634. if (od) {
  635. idx = xll_name_to_index(od);
  636. G_filter.oif = idx;
  637. }
  638. }
  639. if (flush) {
  640. char flushb[4096-512];
  641. if (G_filter.tb == -1) { /* "flush table cache" */
  642. if (do_ipv6 != AF_INET6)
  643. iproute_flush_cache();
  644. if (do_ipv6 == AF_INET)
  645. return 0;
  646. }
  647. G_filter.flushb = flushb;
  648. G_filter.flushp = 0;
  649. G_filter.flushe = sizeof(flushb);
  650. G_filter.rth = &rth;
  651. for (;;) {
  652. xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
  653. G_filter.flushed = 0;
  654. xrtnl_dump_filter(&rth, print_route, NULL);
  655. if (G_filter.flushed == 0)
  656. return 0;
  657. if (flush_update())
  658. return 1;
  659. }
  660. }
  661. if (G_filter.tb != -1) {
  662. xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
  663. } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
  664. bb_perror_msg_and_die("can't send dump request");
  665. }
  666. xrtnl_dump_filter(&rth, print_route, NULL);
  667. return 0;
  668. }
  669. /* Return value becomes exitcode. It's okay to not return at all */
  670. static int iproute_get(char **argv)
  671. {
  672. struct rtnl_handle rth;
  673. struct {
  674. struct nlmsghdr n;
  675. struct rtmsg r;
  676. char buf[1024];
  677. } req;
  678. char *idev = NULL;
  679. char *odev = NULL;
  680. bool connected = 0;
  681. bool from_ok = 0;
  682. static const char options[] ALIGN1 =
  683. "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
  684. memset(&req, 0, sizeof(req));
  685. iproute_reset_filter();
  686. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  687. if (NLM_F_REQUEST)
  688. req.n.nlmsg_flags = NLM_F_REQUEST;
  689. if (RTM_GETROUTE)
  690. req.n.nlmsg_type = RTM_GETROUTE;
  691. req.r.rtm_family = preferred_family;
  692. /*req.r.rtm_table = 0; - memset did this already */
  693. /*req.r.rtm_protocol = 0;*/
  694. /*req.r.rtm_scope = 0;*/
  695. /*req.r.rtm_type = 0;*/
  696. /*req.r.rtm_src_len = 0;*/
  697. /*req.r.rtm_dst_len = 0;*/
  698. /*req.r.rtm_tos = 0;*/
  699. while (*argv) {
  700. switch (index_in_strings(options, *argv)) {
  701. case 0: /* from */
  702. {
  703. inet_prefix addr;
  704. NEXT_ARG();
  705. from_ok = 1;
  706. get_prefix(&addr, *argv, req.r.rtm_family);
  707. if (req.r.rtm_family == AF_UNSPEC) {
  708. req.r.rtm_family = addr.family;
  709. }
  710. if (addr.bytelen) {
  711. addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
  712. }
  713. req.r.rtm_src_len = addr.bitlen;
  714. break;
  715. }
  716. case 1: /* iif */
  717. NEXT_ARG();
  718. idev = *argv;
  719. break;
  720. case 2: /* oif */
  721. case 3: /* dev */
  722. NEXT_ARG();
  723. odev = *argv;
  724. break;
  725. case 4: /* notify */
  726. req.r.rtm_flags |= RTM_F_NOTIFY;
  727. break;
  728. case 5: /* connected */
  729. connected = 1;
  730. break;
  731. case 6: /* to */
  732. NEXT_ARG();
  733. default:
  734. {
  735. inet_prefix addr;
  736. get_prefix(&addr, *argv, req.r.rtm_family);
  737. if (req.r.rtm_family == AF_UNSPEC) {
  738. req.r.rtm_family = addr.family;
  739. }
  740. if (addr.bytelen) {
  741. addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
  742. }
  743. req.r.rtm_dst_len = addr.bitlen;
  744. }
  745. }
  746. argv++;
  747. }
  748. if (req.r.rtm_dst_len == 0) {
  749. bb_error_msg_and_die("need at least destination address");
  750. }
  751. xrtnl_open(&rth);
  752. ll_init_map(&rth);
  753. if (idev || odev) {
  754. int idx;
  755. if (idev) {
  756. idx = xll_name_to_index(idev);
  757. addattr32(&req.n, sizeof(req), RTA_IIF, idx);
  758. }
  759. if (odev) {
  760. idx = xll_name_to_index(odev);
  761. addattr32(&req.n, sizeof(req), RTA_OIF, idx);
  762. }
  763. }
  764. if (req.r.rtm_family == AF_UNSPEC) {
  765. req.r.rtm_family = AF_INET;
  766. }
  767. if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
  768. return 2;
  769. }
  770. if (connected && !from_ok) {
  771. struct rtmsg *r = NLMSG_DATA(&req.n);
  772. int len = req.n.nlmsg_len;
  773. struct rtattr * tb[RTA_MAX+1];
  774. print_route(NULL, &req.n, NULL);
  775. if (req.n.nlmsg_type != RTM_NEWROUTE) {
  776. bb_error_msg_and_die("not a route?");
  777. }
  778. len -= NLMSG_LENGTH(sizeof(*r));
  779. if (len < 0) {
  780. bb_error_msg_and_die("wrong len %d", len);
  781. }
  782. memset(tb, 0, sizeof(tb));
  783. parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
  784. if (tb[RTA_PREFSRC]) {
  785. tb[RTA_PREFSRC]->rta_type = RTA_SRC;
  786. r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
  787. } else if (!tb[RTA_SRC]) {
  788. bb_error_msg_and_die("can't connect the route");
  789. }
  790. if (!odev && tb[RTA_OIF]) {
  791. tb[RTA_OIF]->rta_type = 0;
  792. }
  793. if (tb[RTA_GATEWAY]) {
  794. tb[RTA_GATEWAY]->rta_type = 0;
  795. }
  796. if (!idev && tb[RTA_IIF]) {
  797. tb[RTA_IIF]->rta_type = 0;
  798. }
  799. req.n.nlmsg_flags = NLM_F_REQUEST;
  800. req.n.nlmsg_type = RTM_GETROUTE;
  801. if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
  802. return 2;
  803. }
  804. }
  805. print_route(NULL, &req.n, NULL);
  806. return 0;
  807. }
  808. /* Return value becomes exitcode. It's okay to not return at all */
  809. int FAST_FUNC do_iproute(char **argv)
  810. {
  811. static const char ip_route_commands[] ALIGN1 =
  812. /*0-3*/ "add\0""append\0""change\0""chg\0"
  813. /*4-7*/ "delete\0""get\0""list\0""show\0"
  814. /*8..*/ "prepend\0""replace\0""test\0""flush\0";
  815. int command_num;
  816. unsigned flags = 0;
  817. int cmd = RTM_NEWROUTE;
  818. if (!*argv)
  819. return iproute_list_or_flush(argv, 0);
  820. /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
  821. /* It probably means that it is using "first match" rule */
  822. command_num = index_in_substrings(ip_route_commands, *argv);
  823. switch (command_num) {
  824. case 0: /* add */
  825. flags = NLM_F_CREATE|NLM_F_EXCL;
  826. break;
  827. case 1: /* append */
  828. flags = NLM_F_CREATE|NLM_F_APPEND;
  829. break;
  830. case 2: /* change */
  831. case 3: /* chg */
  832. flags = NLM_F_REPLACE;
  833. break;
  834. case 4: /* delete */
  835. cmd = RTM_DELROUTE;
  836. break;
  837. case 5: /* get */
  838. return iproute_get(argv+1);
  839. case 6: /* list */
  840. case 7: /* show */
  841. return iproute_list_or_flush(argv+1, 0);
  842. case 8: /* prepend */
  843. flags = NLM_F_CREATE;
  844. break;
  845. case 9: /* replace */
  846. flags = NLM_F_CREATE|NLM_F_REPLACE;
  847. break;
  848. case 10: /* test */
  849. flags = NLM_F_EXCL;
  850. break;
  851. case 11: /* flush */
  852. return iproute_list_or_flush(argv+1, 1);
  853. default:
  854. bb_error_msg_and_die("unknown command %s", *argv);
  855. }
  856. return iproute_modify(cmd, flags, argv+1);
  857. }