1 /* vi: set sw=4 ts=4: */
3 * iproute.c "ip route".
5 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
13 * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
16 #include "ip_common.h" /* #include "libbb.h" is inside */
21 #define RTAX_RTTVAR RTAX_HOPS
25 typedef struct filter_t {
31 struct rtnl_handle *rth;
32 int protocol, protocolmask;
47 #define filter (*(filter_t*)&bb_common_bufsiz1)
49 static int flush_update(void)
51 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
52 bb_perror_msg("failed to send flush request");
59 static unsigned get_hz(void)
61 static unsigned hz_internal;
67 fp = fopen("/proc/net/psched", "r");
71 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
77 hz_internal = sysconf(_SC_CLK_TCK);
81 static int print_route(const struct sockaddr_nl *who UNUSED_PARAM,
82 struct nlmsghdr *n, void *arg UNUSED_PARAM)
84 struct rtmsg *r = NLMSG_DATA(n);
85 int len = n->nlmsg_len;
86 struct rtattr * tb[RTA_MAX+1];
93 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
94 fprintf(stderr, "Not a route: %08x %08x %08x\n",
95 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
98 if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
100 len -= NLMSG_LENGTH(sizeof(*r));
102 bb_error_msg_and_die("wrong nlmsg len %d", len);
104 if (r->rtm_family == AF_INET6)
106 else if (r->rtm_family == AF_INET)
109 if (r->rtm_family == AF_INET6) {
112 if (!(r->rtm_flags & RTM_F_CLONED)) {
116 if (r->rtm_flags & RTM_F_CLONED) {
119 if (filter.tb == RT_TABLE_LOCAL) {
120 if (r->rtm_type != RTN_LOCAL) {
123 } else if (filter.tb == RT_TABLE_MAIN) {
124 if (r->rtm_type == RTN_LOCAL) {
133 if (filter.tb > 0 && filter.tb != r->rtm_table) {
137 if (filter.rdst.family &&
138 (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len)) {
141 if (filter.mdst.family &&
142 (r->rtm_family != filter.mdst.family ||
143 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) {
146 if (filter.rsrc.family &&
147 (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len)) {
150 if (filter.msrc.family &&
151 (r->rtm_family != filter.msrc.family ||
152 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) {
156 memset(tb, 0, sizeof(tb));
157 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
159 if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
161 if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
162 inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
165 if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
167 if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
168 inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
172 r->rtm_family == AF_INET6 &&
173 r->rtm_dst_len == 0 &&
174 r->rtm_type == RTN_UNREACHABLE &&
176 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
181 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
183 bb_error_msg_and_die("flush");
185 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
186 memcpy(fn, n, n->nlmsg_len);
187 fn->nlmsg_type = RTM_DELROUTE;
188 fn->nlmsg_flags = NLM_F_REQUEST;
189 fn->nlmsg_seq = ++filter.rth->seq;
190 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
195 if (n->nlmsg_type == RTM_DELROUTE) {
198 if (r->rtm_type != RTN_UNICAST && !filter.type) {
199 printf("%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
203 if (r->rtm_dst_len != host_len) {
204 printf("%s/%u ", rt_addr_n2a(r->rtm_family,
205 RTA_PAYLOAD(tb[RTA_DST]),
206 RTA_DATA(tb[RTA_DST]),
211 printf("%s ", format_host(r->rtm_family,
212 RTA_PAYLOAD(tb[RTA_DST]),
213 RTA_DATA(tb[RTA_DST]),
217 } else if (r->rtm_dst_len) {
218 printf("0/%d ", r->rtm_dst_len);
223 if (r->rtm_src_len != host_len) {
224 printf("from %s/%u ", rt_addr_n2a(r->rtm_family,
225 RTA_PAYLOAD(tb[RTA_SRC]),
226 RTA_DATA(tb[RTA_SRC]),
231 printf("from %s ", format_host(r->rtm_family,
232 RTA_PAYLOAD(tb[RTA_SRC]),
233 RTA_DATA(tb[RTA_SRC]),
237 } else if (r->rtm_src_len) {
238 printf("from 0/%u ", r->rtm_src_len);
240 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
241 printf("via %s ", format_host(r->rtm_family,
242 RTA_PAYLOAD(tb[RTA_GATEWAY]),
243 RTA_DATA(tb[RTA_GATEWAY]),
244 abuf, sizeof(abuf)));
246 if (tb[RTA_OIF] && filter.oifmask != -1) {
247 printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
250 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
251 /* Do not use format_host(). It is our local addr
252 and symbolic name will not be useful.
254 printf(" src %s ", rt_addr_n2a(r->rtm_family,
255 RTA_PAYLOAD(tb[RTA_PREFSRC]),
256 RTA_DATA(tb[RTA_PREFSRC]),
257 abuf, sizeof(abuf)));
259 if (tb[RTA_PRIORITY]) {
260 printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
262 if (r->rtm_family == AF_INET6) {
263 struct rta_cacheinfo *ci = NULL;
264 if (tb[RTA_CACHEINFO]) {
265 ci = RTA_DATA(tb[RTA_CACHEINFO]);
267 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
268 if (r->rtm_flags & RTM_F_CLONED) {
269 printf("%c cache ", _SL_);
271 if (ci->rta_expires) {
272 printf(" expires %dsec", ci->rta_expires / get_hz());
274 if (ci->rta_error != 0) {
275 printf(" error %d", ci->rta_error);
278 if (ci->rta_error != 0)
279 printf(" error %d", ci->rta_error);
282 if (tb[RTA_IIF] && filter.iifmask != -1) {
283 printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
289 /* Return value becomes exitcode. It's okay to not return at all */
290 static int iproute_modify(int cmd, unsigned flags, char **argv)
292 static const char keywords[] ALIGN1 =
293 "src\0""via\0""mtu\0""lock\0""protocol\0"USE_FEATURE_IP_RULE("table\0")
294 "dev\0""oif\0""to\0";
300 USE_FEATURE_IP_RULE(ARG_table,)
311 struct rtnl_handle rth;
318 struct rtattr * mxrta = (void*)mxbuf;
324 memset(&req, 0, sizeof(req));
326 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
327 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
328 req.n.nlmsg_type = cmd;
329 req.r.rtm_family = preferred_family;
330 if (RT_TABLE_MAIN) /* if it is zero, memset already did it */
331 req.r.rtm_table = RT_TABLE_MAIN;
332 if (RT_SCOPE_NOWHERE)
333 req.r.rtm_scope = RT_SCOPE_NOWHERE;
335 if (cmd != RTM_DELROUTE) {
336 req.r.rtm_protocol = RTPROT_BOOT;
337 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
338 req.r.rtm_type = RTN_UNICAST;
341 mxrta->rta_type = RTA_METRICS;
342 mxrta->rta_len = RTA_LENGTH(0);
345 arg = index_in_substrings(keywords, *argv);
346 if (arg == ARG_src) {
349 get_addr(&addr, *argv, req.r.rtm_family);
350 if (req.r.rtm_family == AF_UNSPEC)
351 req.r.rtm_family = addr.family;
352 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
353 } else if (arg == ARG_via) {
357 get_addr(&addr, *argv, req.r.rtm_family);
358 if (req.r.rtm_family == AF_UNSPEC) {
359 req.r.rtm_family = addr.family;
361 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
362 } else if (arg == ARG_mtu) {
365 if (index_in_strings(keywords, *argv) == PARM_lock) {
366 mxlock |= (1 << RTAX_MTU);
369 if (get_unsigned(&mtu, *argv, 0))
370 invarg(*argv, "mtu");
371 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
372 } else if (arg == ARG_protocol) {
375 if (rtnl_rtprot_a2n(&prot, *argv))
376 invarg(*argv, "protocol");
377 req.r.rtm_protocol = prot;
379 #if ENABLE_FEATURE_IP_RULE
380 } else if (arg == ARG_table) {
383 if (rtnl_rttable_a2n(&tid, *argv))
384 invarg(*argv, "table");
385 req.r.rtm_table = tid;
387 } else if (arg == ARG_dev || arg == ARG_oif) {
397 if ((**argv < '0' || **argv > '9')
398 && rtnl_rtntype_a2n(&type, *argv) == 0) {
400 req.r.rtm_type = type;
405 duparg2("to", *argv);
407 get_prefix(&dst, *argv, req.r.rtm_family);
408 if (req.r.rtm_family == AF_UNSPEC) {
409 req.r.rtm_family = dst.family;
411 req.r.rtm_dst_len = dst.bitlen;
414 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
428 idx = xll_name_to_index(d);
429 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
433 if (mxrta->rta_len > RTA_LENGTH(0)) {
435 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
437 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
440 if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
441 req.r.rtm_scope = RT_SCOPE_HOST;
442 else if (req.r.rtm_type == RTN_BROADCAST ||
443 req.r.rtm_type == RTN_MULTICAST ||
444 req.r.rtm_type == RTN_ANYCAST)
445 req.r.rtm_scope = RT_SCOPE_LINK;
446 else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
447 if (cmd == RTM_DELROUTE)
448 req.r.rtm_scope = RT_SCOPE_NOWHERE;
449 else if (!(ok & gw_ok))
450 req.r.rtm_scope = RT_SCOPE_LINK;
453 if (req.r.rtm_family == AF_UNSPEC) {
454 req.r.rtm_family = AF_INET;
457 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
464 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
470 struct sockaddr_nl nladdr;
472 memset(&nladdr, 0, sizeof(nladdr));
473 memset(&req, 0, sizeof(req));
474 nladdr.nl_family = AF_NETLINK;
476 req.nlh.nlmsg_len = sizeof(req);
478 req.nlh.nlmsg_type = RTM_GETROUTE;
479 if (NLM_F_ROOT | NLM_F_REQUEST)
480 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
481 /*req.nlh.nlmsg_pid = 0; - memset did it already */
482 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
483 req.rtm.rtm_family = family;
485 req.rtm.rtm_flags = RTM_F_CLONED;
487 return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
490 static void iproute_flush_cache(void)
492 static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
493 int flush_fd = open_or_warn(fn, O_WRONLY);
499 if (write(flush_fd, "-1", 2) < 2) {
500 bb_perror_msg("cannot flush routing cache");
506 static void iproute_reset_filter(void)
508 memset(&filter, 0, sizeof(filter));
509 filter.mdst.bitlen = -1;
510 filter.msrc.bitlen = -1;
513 /* Return value becomes exitcode. It's okay to not return at all */
514 static int iproute_list_or_flush(char **argv, int flush)
516 int do_ipv6 = preferred_family;
517 struct rtnl_handle rth;
520 static const char keywords[] ALIGN1 =
521 /* "ip route list/flush" parameters: */
522 "protocol\0" "dev\0" "oif\0" "iif\0"
523 "via\0" "table\0" "cache\0"
525 /* and possible further keywords */
533 KW_proto, KW_dev, KW_oif, KW_iif,
534 KW_via, KW_table, KW_cache,
545 iproute_reset_filter();
546 filter.tb = RT_TABLE_MAIN;
549 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
552 arg = index_in_substrings(keywords, *argv);
553 if (arg == KW_proto) {
556 filter.protocolmask = -1;
557 if (rtnl_rtprot_a2n(&prot, *argv)) {
558 if (index_in_strings(keywords, *argv) != KW_all)
559 invarg(*argv, "protocol");
561 filter.protocolmask = 0;
563 filter.protocol = prot;
564 } else if (arg == KW_dev || arg == KW_oif) {
567 } else if (arg == KW_iif) {
570 } else if (arg == KW_via) {
572 get_prefix(&filter.rvia, *argv, do_ipv6);
573 } else if (arg == KW_table) { /* table all/cache/main */
575 parm = index_in_substrings(keywords, *argv);
576 if (parm == KW_cache)
578 else if (parm == KW_all)
580 else if (parm != KW_main) {
581 #if ENABLE_FEATURE_IP_RULE
583 if (rtnl_rttable_a2n(&tid, *argv))
584 invarg(*argv, "table");
587 invarg(*argv, "table");
590 } else if (arg == KW_cache) {
591 /* The command 'ip route flush cache' is used by OpenSWAN.
592 * Assuming it's a synonym for 'ip route flush table cache' */
594 } else if (arg == KW_from) {
596 parm = index_in_substrings(keywords, *argv);
597 if (parm == KW_root) {
599 get_prefix(&filter.rsrc, *argv, do_ipv6);
600 } else if (parm == KW_match) {
602 get_prefix(&filter.msrc, *argv, do_ipv6);
604 if (parm == KW_exact)
606 get_prefix(&filter.msrc, *argv, do_ipv6);
607 filter.rsrc = filter.msrc;
609 } else { /* "to" is the default parameter */
612 arg = index_in_substrings(keywords, *argv);
614 /* parm = arg; - would be more plausible, but we reuse 'arg' here */
615 if (arg == KW_root) {
617 get_prefix(&filter.rdst, *argv, do_ipv6);
618 } else if (arg == KW_match) {
620 get_prefix(&filter.mdst, *argv, do_ipv6);
621 } else { /* "to exact" is the default */
624 get_prefix(&filter.mdst, *argv, do_ipv6);
625 filter.rdst = filter.mdst;
631 if (do_ipv6 == AF_UNSPEC && filter.tb) {
642 idx = xll_name_to_index(id);
647 idx = xll_name_to_index(od);
654 char flushb[4096-512];
656 if (filter.tb == -1) { /* "flush table cache" */
657 if (do_ipv6 != AF_INET6)
658 iproute_flush_cache();
659 if (do_ipv6 == AF_INET)
663 filter.flushb = flushb;
665 filter.flushe = sizeof(flushb);
669 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
671 xrtnl_dump_filter(&rth, print_route, NULL);
672 if (filter.flushed == 0)
679 if (filter.tb != -1) {
680 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
681 } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
682 bb_perror_msg_and_die("cannot send dump request");
684 xrtnl_dump_filter(&rth, print_route, NULL);
690 /* Return value becomes exitcode. It's okay to not return at all */
691 static int iproute_get(char **argv)
693 struct rtnl_handle rth;
703 static const char options[] ALIGN1 =
704 "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
706 memset(&req, 0, sizeof(req));
708 iproute_reset_filter();
710 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
712 req.n.nlmsg_flags = NLM_F_REQUEST;
714 req.n.nlmsg_type = RTM_GETROUTE;
715 req.r.rtm_family = preferred_family;
716 /*req.r.rtm_table = 0; - memset did this already */
717 /*req.r.rtm_protocol = 0;*/
718 /*req.r.rtm_scope = 0;*/
719 /*req.r.rtm_type = 0;*/
720 /*req.r.rtm_src_len = 0;*/
721 /*req.r.rtm_dst_len = 0;*/
722 /*req.r.rtm_tos = 0;*/
725 switch (index_in_strings(options, *argv)) {
731 get_prefix(&addr, *argv, req.r.rtm_family);
732 if (req.r.rtm_family == AF_UNSPEC) {
733 req.r.rtm_family = addr.family;
736 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
738 req.r.rtm_src_len = addr.bitlen;
751 req.r.rtm_flags |= RTM_F_NOTIFY;
753 case 5: /* connected */
761 get_prefix(&addr, *argv, req.r.rtm_family);
762 if (req.r.rtm_family == AF_UNSPEC) {
763 req.r.rtm_family = addr.family;
766 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
768 req.r.rtm_dst_len = addr.bitlen;
774 if (req.r.rtm_dst_len == 0) {
775 bb_error_msg_and_die("need at least destination address");
786 idx = xll_name_to_index(idev);
787 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
790 idx = xll_name_to_index(odev);
791 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
795 if (req.r.rtm_family == AF_UNSPEC) {
796 req.r.rtm_family = AF_INET;
799 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
803 if (connected && !from_ok) {
804 struct rtmsg *r = NLMSG_DATA(&req.n);
805 int len = req.n.nlmsg_len;
806 struct rtattr * tb[RTA_MAX+1];
808 print_route(NULL, &req.n, NULL);
810 if (req.n.nlmsg_type != RTM_NEWROUTE) {
811 bb_error_msg_and_die("not a route?");
813 len -= NLMSG_LENGTH(sizeof(*r));
815 bb_error_msg_and_die("wrong len %d", len);
818 memset(tb, 0, sizeof(tb));
819 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
821 if (tb[RTA_PREFSRC]) {
822 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
823 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
824 } else if (!tb[RTA_SRC]) {
825 bb_error_msg_and_die("failed to connect the route");
827 if (!odev && tb[RTA_OIF]) {
828 tb[RTA_OIF]->rta_type = 0;
830 if (tb[RTA_GATEWAY]) {
831 tb[RTA_GATEWAY]->rta_type = 0;
833 if (!idev && tb[RTA_IIF]) {
834 tb[RTA_IIF]->rta_type = 0;
836 req.n.nlmsg_flags = NLM_F_REQUEST;
837 req.n.nlmsg_type = RTM_GETROUTE;
839 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
843 print_route(NULL, &req.n, NULL);
847 /* Return value becomes exitcode. It's okay to not return at all */
848 int do_iproute(char **argv)
850 static const char ip_route_commands[] ALIGN1 =
851 /*0-3*/ "add\0""append\0""change\0""chg\0"
852 /*4-7*/ "delete\0""get\0""list\0""show\0"
853 /*8..*/ "prepend\0""replace\0""test\0""flush\0";
856 int cmd = RTM_NEWROUTE;
859 return iproute_list_or_flush(argv, 0);
861 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
862 /* It probably means that it is using "first match" rule */
863 command_num = index_in_substrings(ip_route_commands, *argv);
865 switch (command_num) {
867 flags = NLM_F_CREATE|NLM_F_EXCL;
870 flags = NLM_F_CREATE|NLM_F_APPEND;
874 flags = NLM_F_REPLACE;
880 return iproute_get(argv+1);
883 return iproute_list_or_flush(argv+1, 0);
884 case 8: /* prepend */
885 flags = NLM_F_CREATE;
887 case 9: /* replace */
888 flags = NLM_F_CREATE|NLM_F_REPLACE;
894 return iproute_list_or_flush(argv+1, 1);
896 bb_error_msg_and_die("unknown command %s", *argv);
899 return iproute_modify(cmd, flags, argv+1);