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_for_read("/proc/net/psched");
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""metric\0";
300 USE_FEATURE_IP_RULE(ARG_table,)
312 struct rtnl_handle rth;
319 struct rtattr * mxrta = (void*)mxbuf;
325 memset(&req, 0, sizeof(req));
327 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
328 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
329 req.n.nlmsg_type = cmd;
330 req.r.rtm_family = preferred_family;
331 if (RT_TABLE_MAIN) /* if it is zero, memset already did it */
332 req.r.rtm_table = RT_TABLE_MAIN;
333 if (RT_SCOPE_NOWHERE)
334 req.r.rtm_scope = RT_SCOPE_NOWHERE;
336 if (cmd != RTM_DELROUTE) {
337 req.r.rtm_protocol = RTPROT_BOOT;
338 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
339 req.r.rtm_type = RTN_UNICAST;
342 mxrta->rta_type = RTA_METRICS;
343 mxrta->rta_len = RTA_LENGTH(0);
346 arg = index_in_substrings(keywords, *argv);
347 if (arg == ARG_src) {
350 get_addr(&addr, *argv, req.r.rtm_family);
351 if (req.r.rtm_family == AF_UNSPEC)
352 req.r.rtm_family = addr.family;
353 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
354 } else if (arg == ARG_via) {
358 get_addr(&addr, *argv, req.r.rtm_family);
359 if (req.r.rtm_family == AF_UNSPEC) {
360 req.r.rtm_family = addr.family;
362 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
363 } else if (arg == ARG_mtu) {
366 if (index_in_strings(keywords, *argv) == PARM_lock) {
367 mxlock |= (1 << RTAX_MTU);
370 if (get_unsigned(&mtu, *argv, 0))
371 invarg(*argv, "mtu");
372 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
373 } else if (arg == ARG_protocol) {
376 if (rtnl_rtprot_a2n(&prot, *argv))
377 invarg(*argv, "protocol");
378 req.r.rtm_protocol = prot;
380 #if ENABLE_FEATURE_IP_RULE
381 } else if (arg == ARG_table) {
384 if (rtnl_rttable_a2n(&tid, *argv))
385 invarg(*argv, "table");
386 req.r.rtm_table = tid;
388 } else if (arg == ARG_dev || arg == ARG_oif) {
391 } else if (arg == ARG_metric) {
394 if (get_u32(&metric, *argv, 0))
395 invarg(*argv, "metric");
396 addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
404 if ((**argv < '0' || **argv > '9')
405 && rtnl_rtntype_a2n(&type, *argv) == 0) {
407 req.r.rtm_type = type;
412 duparg2("to", *argv);
414 get_prefix(&dst, *argv, req.r.rtm_family);
415 if (req.r.rtm_family == AF_UNSPEC) {
416 req.r.rtm_family = dst.family;
418 req.r.rtm_dst_len = dst.bitlen;
421 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
435 idx = xll_name_to_index(d);
436 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
440 if (mxrta->rta_len > RTA_LENGTH(0)) {
442 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
444 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
447 if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
448 req.r.rtm_scope = RT_SCOPE_HOST;
449 else if (req.r.rtm_type == RTN_BROADCAST ||
450 req.r.rtm_type == RTN_MULTICAST ||
451 req.r.rtm_type == RTN_ANYCAST)
452 req.r.rtm_scope = RT_SCOPE_LINK;
453 else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
454 if (cmd == RTM_DELROUTE)
455 req.r.rtm_scope = RT_SCOPE_NOWHERE;
456 else if (!(ok & gw_ok))
457 req.r.rtm_scope = RT_SCOPE_LINK;
460 if (req.r.rtm_family == AF_UNSPEC) {
461 req.r.rtm_family = AF_INET;
464 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
471 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
477 struct sockaddr_nl nladdr;
479 memset(&nladdr, 0, sizeof(nladdr));
480 memset(&req, 0, sizeof(req));
481 nladdr.nl_family = AF_NETLINK;
483 req.nlh.nlmsg_len = sizeof(req);
485 req.nlh.nlmsg_type = RTM_GETROUTE;
486 if (NLM_F_ROOT | NLM_F_REQUEST)
487 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
488 /*req.nlh.nlmsg_pid = 0; - memset did it already */
489 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
490 req.rtm.rtm_family = family;
492 req.rtm.rtm_flags = RTM_F_CLONED;
494 return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
497 static void iproute_flush_cache(void)
499 static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
500 int flush_fd = open_or_warn(fn, O_WRONLY);
506 if (write(flush_fd, "-1", 2) < 2) {
507 bb_perror_msg("cannot flush routing cache");
513 static void iproute_reset_filter(void)
515 memset(&filter, 0, sizeof(filter));
516 filter.mdst.bitlen = -1;
517 filter.msrc.bitlen = -1;
520 /* Return value becomes exitcode. It's okay to not return at all */
521 static int iproute_list_or_flush(char **argv, int flush)
523 int do_ipv6 = preferred_family;
524 struct rtnl_handle rth;
527 static const char keywords[] ALIGN1 =
528 /* "ip route list/flush" parameters: */
529 "protocol\0" "dev\0" "oif\0" "iif\0"
530 "via\0" "table\0" "cache\0"
532 /* and possible further keywords */
540 KW_proto, KW_dev, KW_oif, KW_iif,
541 KW_via, KW_table, KW_cache,
552 iproute_reset_filter();
553 filter.tb = RT_TABLE_MAIN;
556 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
559 arg = index_in_substrings(keywords, *argv);
560 if (arg == KW_proto) {
563 filter.protocolmask = -1;
564 if (rtnl_rtprot_a2n(&prot, *argv)) {
565 if (index_in_strings(keywords, *argv) != KW_all)
566 invarg(*argv, "protocol");
568 filter.protocolmask = 0;
570 filter.protocol = prot;
571 } else if (arg == KW_dev || arg == KW_oif) {
574 } else if (arg == KW_iif) {
577 } else if (arg == KW_via) {
579 get_prefix(&filter.rvia, *argv, do_ipv6);
580 } else if (arg == KW_table) { /* table all/cache/main */
582 parm = index_in_substrings(keywords, *argv);
583 if (parm == KW_cache)
585 else if (parm == KW_all)
587 else if (parm != KW_main) {
588 #if ENABLE_FEATURE_IP_RULE
590 if (rtnl_rttable_a2n(&tid, *argv))
591 invarg(*argv, "table");
594 invarg(*argv, "table");
597 } else if (arg == KW_cache) {
598 /* The command 'ip route flush cache' is used by OpenSWAN.
599 * Assuming it's a synonym for 'ip route flush table cache' */
601 } else if (arg == KW_from) {
603 parm = index_in_substrings(keywords, *argv);
604 if (parm == KW_root) {
606 get_prefix(&filter.rsrc, *argv, do_ipv6);
607 } else if (parm == KW_match) {
609 get_prefix(&filter.msrc, *argv, do_ipv6);
611 if (parm == KW_exact)
613 get_prefix(&filter.msrc, *argv, do_ipv6);
614 filter.rsrc = filter.msrc;
616 } else { /* "to" is the default parameter */
619 arg = index_in_substrings(keywords, *argv);
621 /* parm = arg; - would be more plausible, but we reuse 'arg' here */
622 if (arg == KW_root) {
624 get_prefix(&filter.rdst, *argv, do_ipv6);
625 } else if (arg == KW_match) {
627 get_prefix(&filter.mdst, *argv, do_ipv6);
628 } else { /* "to exact" is the default */
631 get_prefix(&filter.mdst, *argv, do_ipv6);
632 filter.rdst = filter.mdst;
638 if (do_ipv6 == AF_UNSPEC && filter.tb) {
649 idx = xll_name_to_index(id);
654 idx = xll_name_to_index(od);
661 char flushb[4096-512];
663 if (filter.tb == -1) { /* "flush table cache" */
664 if (do_ipv6 != AF_INET6)
665 iproute_flush_cache();
666 if (do_ipv6 == AF_INET)
670 filter.flushb = flushb;
672 filter.flushe = sizeof(flushb);
676 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
678 xrtnl_dump_filter(&rth, print_route, NULL);
679 if (filter.flushed == 0)
686 if (filter.tb != -1) {
687 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
688 } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
689 bb_perror_msg_and_die("cannot send dump request");
691 xrtnl_dump_filter(&rth, print_route, NULL);
697 /* Return value becomes exitcode. It's okay to not return at all */
698 static int iproute_get(char **argv)
700 struct rtnl_handle rth;
710 static const char options[] ALIGN1 =
711 "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
713 memset(&req, 0, sizeof(req));
715 iproute_reset_filter();
717 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
719 req.n.nlmsg_flags = NLM_F_REQUEST;
721 req.n.nlmsg_type = RTM_GETROUTE;
722 req.r.rtm_family = preferred_family;
723 /*req.r.rtm_table = 0; - memset did this already */
724 /*req.r.rtm_protocol = 0;*/
725 /*req.r.rtm_scope = 0;*/
726 /*req.r.rtm_type = 0;*/
727 /*req.r.rtm_src_len = 0;*/
728 /*req.r.rtm_dst_len = 0;*/
729 /*req.r.rtm_tos = 0;*/
732 switch (index_in_strings(options, *argv)) {
738 get_prefix(&addr, *argv, req.r.rtm_family);
739 if (req.r.rtm_family == AF_UNSPEC) {
740 req.r.rtm_family = addr.family;
743 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
745 req.r.rtm_src_len = addr.bitlen;
758 req.r.rtm_flags |= RTM_F_NOTIFY;
760 case 5: /* connected */
768 get_prefix(&addr, *argv, req.r.rtm_family);
769 if (req.r.rtm_family == AF_UNSPEC) {
770 req.r.rtm_family = addr.family;
773 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
775 req.r.rtm_dst_len = addr.bitlen;
781 if (req.r.rtm_dst_len == 0) {
782 bb_error_msg_and_die("need at least destination address");
793 idx = xll_name_to_index(idev);
794 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
797 idx = xll_name_to_index(odev);
798 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
802 if (req.r.rtm_family == AF_UNSPEC) {
803 req.r.rtm_family = AF_INET;
806 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
810 if (connected && !from_ok) {
811 struct rtmsg *r = NLMSG_DATA(&req.n);
812 int len = req.n.nlmsg_len;
813 struct rtattr * tb[RTA_MAX+1];
815 print_route(NULL, &req.n, NULL);
817 if (req.n.nlmsg_type != RTM_NEWROUTE) {
818 bb_error_msg_and_die("not a route?");
820 len -= NLMSG_LENGTH(sizeof(*r));
822 bb_error_msg_and_die("wrong len %d", len);
825 memset(tb, 0, sizeof(tb));
826 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
828 if (tb[RTA_PREFSRC]) {
829 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
830 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
831 } else if (!tb[RTA_SRC]) {
832 bb_error_msg_and_die("failed to connect the route");
834 if (!odev && tb[RTA_OIF]) {
835 tb[RTA_OIF]->rta_type = 0;
837 if (tb[RTA_GATEWAY]) {
838 tb[RTA_GATEWAY]->rta_type = 0;
840 if (!idev && tb[RTA_IIF]) {
841 tb[RTA_IIF]->rta_type = 0;
843 req.n.nlmsg_flags = NLM_F_REQUEST;
844 req.n.nlmsg_type = RTM_GETROUTE;
846 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
850 print_route(NULL, &req.n, NULL);
854 /* Return value becomes exitcode. It's okay to not return at all */
855 int do_iproute(char **argv)
857 static const char ip_route_commands[] ALIGN1 =
858 /*0-3*/ "add\0""append\0""change\0""chg\0"
859 /*4-7*/ "delete\0""get\0""list\0""show\0"
860 /*8..*/ "prepend\0""replace\0""test\0""flush\0";
863 int cmd = RTM_NEWROUTE;
866 return iproute_list_or_flush(argv, 0);
868 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
869 /* It probably means that it is using "first match" rule */
870 command_num = index_in_substrings(ip_route_commands, *argv);
872 switch (command_num) {
874 flags = NLM_F_CREATE|NLM_F_EXCL;
877 flags = NLM_F_CREATE|NLM_F_APPEND;
881 flags = NLM_F_REPLACE;
887 return iproute_get(argv+1);
890 return iproute_list_or_flush(argv+1, 0);
891 case 8: /* prepend */
892 flags = NLM_F_CREATE;
894 case 9: /* replace */
895 flags = NLM_F_CREATE|NLM_F_REPLACE;
901 return iproute_list_or_flush(argv+1, 1);
903 bb_error_msg_and_die("unknown command %s", *argv);
906 return iproute_modify(cmd, flags, argv+1);