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(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
82 struct nlmsghdr *n, void *arg)
84 FILE *fp = (FILE*)arg;
85 struct rtmsg *r = NLMSG_DATA(n);
86 int len = n->nlmsg_len;
87 struct rtattr * tb[RTA_MAX+1];
95 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
96 fprintf(stderr, "Not a route: %08x %08x %08x\n",
97 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
100 if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
102 len -= NLMSG_LENGTH(sizeof(*r));
104 bb_error_msg_and_die("wrong nlmsg len %d", len);
106 if (r->rtm_family == AF_INET6)
108 else if (r->rtm_family == AF_INET)
111 if (r->rtm_family == AF_INET6) {
114 if (!(r->rtm_flags & RTM_F_CLONED)) {
118 if (r->rtm_flags & RTM_F_CLONED) {
121 if (filter.tb == RT_TABLE_LOCAL) {
122 if (r->rtm_type != RTN_LOCAL) {
125 } else if (filter.tb == RT_TABLE_MAIN) {
126 if (r->rtm_type == RTN_LOCAL) {
135 if (filter.tb > 0 && filter.tb != r->rtm_table) {
139 if (filter.rdst.family &&
140 (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len)) {
143 if (filter.mdst.family &&
144 (r->rtm_family != filter.mdst.family ||
145 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) {
148 if (filter.rsrc.family &&
149 (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len)) {
152 if (filter.msrc.family &&
153 (r->rtm_family != filter.msrc.family ||
154 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) {
158 memset(tb, 0, sizeof(tb));
159 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
161 if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
163 if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
164 inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
167 if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
169 if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
170 inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
174 r->rtm_family == AF_INET6 &&
175 r->rtm_dst_len == 0 &&
176 r->rtm_type == RTN_UNREACHABLE &&
178 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
183 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
185 bb_error_msg_and_die("flush");
187 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
188 memcpy(fn, n, n->nlmsg_len);
189 fn->nlmsg_type = RTM_DELROUTE;
190 fn->nlmsg_flags = NLM_F_REQUEST;
191 fn->nlmsg_seq = ++filter.rth->seq;
192 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
197 if (n->nlmsg_type == RTM_DELROUTE) {
198 fprintf(fp, "Deleted ");
200 if (r->rtm_type != RTN_UNICAST && !filter.type) {
201 fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
205 if (r->rtm_dst_len != host_len) {
206 fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
207 RTA_PAYLOAD(tb[RTA_DST]),
208 RTA_DATA(tb[RTA_DST]),
213 fprintf(fp, "%s ", format_host(r->rtm_family,
214 RTA_PAYLOAD(tb[RTA_DST]),
215 RTA_DATA(tb[RTA_DST]),
219 } else if (r->rtm_dst_len) {
220 fprintf(fp, "0/%d ", r->rtm_dst_len);
222 fprintf(fp, "default ");
225 if (r->rtm_src_len != host_len) {
226 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
227 RTA_PAYLOAD(tb[RTA_SRC]),
228 RTA_DATA(tb[RTA_SRC]),
233 fprintf(fp, "from %s ", format_host(r->rtm_family,
234 RTA_PAYLOAD(tb[RTA_SRC]),
235 RTA_DATA(tb[RTA_SRC]),
239 } else if (r->rtm_src_len) {
240 fprintf(fp, "from 0/%u ", r->rtm_src_len);
242 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
243 fprintf(fp, "via %s ",
244 format_host(r->rtm_family,
245 RTA_PAYLOAD(tb[RTA_GATEWAY]),
246 RTA_DATA(tb[RTA_GATEWAY]),
247 abuf, sizeof(abuf)));
249 if (tb[RTA_OIF] && filter.oifmask != -1) {
250 fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
253 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
254 /* Do not use format_host(). It is our local addr
255 and symbolic name will not be useful.
257 fprintf(fp, " src %s ",
258 rt_addr_n2a(r->rtm_family,
259 RTA_PAYLOAD(tb[RTA_PREFSRC]),
260 RTA_DATA(tb[RTA_PREFSRC]),
261 abuf, sizeof(abuf)));
263 if (tb[RTA_PRIORITY]) {
264 fprintf(fp, " metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
266 if (r->rtm_family == AF_INET6) {
267 struct rta_cacheinfo *ci = NULL;
268 if (tb[RTA_CACHEINFO]) {
269 ci = RTA_DATA(tb[RTA_CACHEINFO]);
271 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
272 if (r->rtm_flags & RTM_F_CLONED) {
273 fprintf(fp, "%c cache ", _SL_);
275 if (ci->rta_expires) {
276 fprintf(fp, " expires %dsec", ci->rta_expires / get_hz());
278 if (ci->rta_error != 0) {
279 fprintf(fp, " error %d", ci->rta_error);
282 if (ci->rta_error != 0)
283 fprintf(fp, " error %d", ci->rta_error);
286 if (tb[RTA_IIF] && filter.iifmask != -1) {
287 fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
294 /* Return value becomes exitcode. It's okay to not return at all */
295 static int iproute_modify(int cmd, unsigned flags, char **argv)
297 static const char keywords[] ALIGN1 =
298 "src\0""via\0""mtu\0""lock\0""protocol\0"USE_FEATURE_IP_RULE("table\0")
299 "dev\0""oif\0""to\0";
305 USE_FEATURE_IP_RULE(ARG_table,)
316 struct rtnl_handle rth;
323 struct rtattr * mxrta = (void*)mxbuf;
329 memset(&req, 0, sizeof(req));
331 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
332 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
333 req.n.nlmsg_type = cmd;
334 req.r.rtm_family = preferred_family;
335 req.r.rtm_table = RT_TABLE_MAIN;
336 req.r.rtm_scope = RT_SCOPE_NOWHERE;
338 if (cmd != RTM_DELROUTE) {
339 req.r.rtm_protocol = RTPROT_BOOT;
340 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
341 req.r.rtm_type = RTN_UNICAST;
344 mxrta->rta_type = RTA_METRICS;
345 mxrta->rta_len = RTA_LENGTH(0);
348 arg = index_in_substrings(keywords, *argv);
349 if (arg == ARG_src) {
352 get_addr(&addr, *argv, req.r.rtm_family);
353 if (req.r.rtm_family == AF_UNSPEC)
354 req.r.rtm_family = addr.family;
355 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
356 } else if (arg == ARG_via) {
360 get_addr(&addr, *argv, req.r.rtm_family);
361 if (req.r.rtm_family == AF_UNSPEC) {
362 req.r.rtm_family = addr.family;
364 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
365 } else if (arg == ARG_mtu) {
368 if (index_in_strings(keywords, *argv) == PARM_lock) {
369 mxlock |= (1 << RTAX_MTU);
372 if (get_unsigned(&mtu, *argv, 0))
373 invarg(*argv, "mtu");
374 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
375 } else if (arg == ARG_protocol) {
378 if (rtnl_rtprot_a2n(&prot, *argv))
379 invarg(*argv, "protocol");
380 req.r.rtm_protocol = prot;
382 #if ENABLE_FEATURE_IP_RULE
383 } else if (arg == ARG_table) {
386 if (rtnl_rttable_a2n(&tid, *argv))
387 invarg(*argv, "table");
388 req.r.rtm_table = tid;
390 } else if (arg == ARG_dev || arg == ARG_oif) {
400 if ((**argv < '0' || **argv > '9')
401 && rtnl_rtntype_a2n(&type, *argv) == 0) {
403 req.r.rtm_type = type;
408 duparg2("to", *argv);
410 get_prefix(&dst, *argv, req.r.rtm_family);
411 if (req.r.rtm_family == AF_UNSPEC) {
412 req.r.rtm_family = dst.family;
414 req.r.rtm_dst_len = dst.bitlen;
417 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
431 idx = xll_name_to_index(d);
432 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
436 if (mxrta->rta_len > RTA_LENGTH(0)) {
438 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
440 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
443 if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
444 req.r.rtm_scope = RT_SCOPE_HOST;
445 else if (req.r.rtm_type == RTN_BROADCAST ||
446 req.r.rtm_type == RTN_MULTICAST ||
447 req.r.rtm_type == RTN_ANYCAST)
448 req.r.rtm_scope = RT_SCOPE_LINK;
449 else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
450 if (cmd == RTM_DELROUTE)
451 req.r.rtm_scope = RT_SCOPE_NOWHERE;
452 else if (!(ok & gw_ok))
453 req.r.rtm_scope = RT_SCOPE_LINK;
456 if (req.r.rtm_family == AF_UNSPEC) {
457 req.r.rtm_family = AF_INET;
460 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
467 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
473 struct sockaddr_nl nladdr;
475 memset(&nladdr, 0, sizeof(nladdr));
476 memset(&req, 0, sizeof(req));
477 nladdr.nl_family = AF_NETLINK;
479 req.nlh.nlmsg_len = sizeof(req);
480 req.nlh.nlmsg_type = RTM_GETROUTE;
481 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
482 req.nlh.nlmsg_pid = 0;
483 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
484 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 invarg(*argv, "table");
582 } else if (arg == KW_cache) {
583 /* The command 'ip route flush cache' is used by OpenSWAN.
584 * Assuming it's a synonym for 'ip route flush table cache' */
586 } else if (arg == KW_from) {
588 parm = index_in_substrings(keywords, *argv);
589 if (parm == KW_root) {
591 get_prefix(&filter.rsrc, *argv, do_ipv6);
592 } else if (parm == KW_match) {
594 get_prefix(&filter.msrc, *argv, do_ipv6);
596 if (parm == KW_exact)
598 get_prefix(&filter.msrc, *argv, do_ipv6);
599 filter.rsrc = filter.msrc;
601 } else { /* "to" is the default parameter */
604 arg = index_in_substrings(keywords, *argv);
606 /* parm = arg; - would be more plausible, but we reuse 'arg' here */
607 if (arg == KW_root) {
609 get_prefix(&filter.rdst, *argv, do_ipv6);
610 } else if (arg == KW_match) {
612 get_prefix(&filter.mdst, *argv, do_ipv6);
613 } else { /* "to exact" is the default */
616 get_prefix(&filter.mdst, *argv, do_ipv6);
617 filter.rdst = filter.mdst;
623 if (do_ipv6 == AF_UNSPEC && filter.tb) {
634 idx = xll_name_to_index(id);
639 idx = xll_name_to_index(od);
646 char flushb[4096-512];
648 if (filter.tb == -1) { /* "flush table cache" */
649 if (do_ipv6 != AF_INET6)
650 iproute_flush_cache();
651 if (do_ipv6 == AF_INET)
655 filter.flushb = flushb;
657 filter.flushe = sizeof(flushb);
661 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
663 xrtnl_dump_filter(&rth, print_route, stdout);
664 if (filter.flushed == 0)
671 if (filter.tb != -1) {
672 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
673 } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
674 bb_perror_msg_and_die("cannot send dump request");
676 xrtnl_dump_filter(&rth, print_route, stdout);
682 /* Return value becomes exitcode. It's okay to not return at all */
683 static int iproute_get(char **argv)
685 struct rtnl_handle rth;
695 static const char options[] ALIGN1 =
696 "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
698 memset(&req, 0, sizeof(req));
700 iproute_reset_filter();
702 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
703 req.n.nlmsg_flags = NLM_F_REQUEST;
704 req.n.nlmsg_type = RTM_GETROUTE;
705 req.r.rtm_family = preferred_family;
707 req.r.rtm_protocol = 0;
710 req.r.rtm_src_len = 0;
711 req.r.rtm_dst_len = 0;
715 switch (index_in_strings(options, *argv)) {
721 get_prefix(&addr, *argv, req.r.rtm_family);
722 if (req.r.rtm_family == AF_UNSPEC) {
723 req.r.rtm_family = addr.family;
726 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
728 req.r.rtm_src_len = addr.bitlen;
741 req.r.rtm_flags |= RTM_F_NOTIFY;
743 case 5: /* connected */
751 get_prefix(&addr, *argv, req.r.rtm_family);
752 if (req.r.rtm_family == AF_UNSPEC) {
753 req.r.rtm_family = addr.family;
756 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
758 req.r.rtm_dst_len = addr.bitlen;
764 if (req.r.rtm_dst_len == 0) {
765 bb_error_msg_and_die("need at least destination address");
776 idx = xll_name_to_index(idev);
777 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
780 idx = xll_name_to_index(odev);
781 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
785 if (req.r.rtm_family == AF_UNSPEC) {
786 req.r.rtm_family = AF_INET;
789 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
793 if (connected && !from_ok) {
794 struct rtmsg *r = NLMSG_DATA(&req.n);
795 int len = req.n.nlmsg_len;
796 struct rtattr * tb[RTA_MAX+1];
798 print_route(NULL, &req.n, (void*)stdout);
800 if (req.n.nlmsg_type != RTM_NEWROUTE) {
801 bb_error_msg_and_die("not a route?");
803 len -= NLMSG_LENGTH(sizeof(*r));
805 bb_error_msg_and_die("wrong len %d", len);
808 memset(tb, 0, sizeof(tb));
809 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
811 if (tb[RTA_PREFSRC]) {
812 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
813 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
814 } else if (!tb[RTA_SRC]) {
815 bb_error_msg_and_die("failed to connect the route");
817 if (!odev && tb[RTA_OIF]) {
818 tb[RTA_OIF]->rta_type = 0;
820 if (tb[RTA_GATEWAY]) {
821 tb[RTA_GATEWAY]->rta_type = 0;
823 if (!idev && tb[RTA_IIF]) {
824 tb[RTA_IIF]->rta_type = 0;
826 req.n.nlmsg_flags = NLM_F_REQUEST;
827 req.n.nlmsg_type = RTM_GETROUTE;
829 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
833 print_route(NULL, &req.n, (void*)stdout);
837 /* Return value becomes exitcode. It's okay to not return at all */
838 int do_iproute(char **argv)
840 static const char ip_route_commands[] ALIGN1 =
841 /*0-3*/ "add\0""append\0""change\0""chg\0"
842 /*4-7*/ "delete\0""get\0""list\0""show\0"
843 /*8..*/ "prepend\0""replace\0""test\0""flush\0";
846 int cmd = RTM_NEWROUTE;
849 return iproute_list_or_flush(argv, 0);
851 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
852 /* It probably means that it is using "first match" rule */
853 command_num = index_in_substrings(ip_route_commands, *argv);
855 switch (command_num) {
857 flags = NLM_F_CREATE|NLM_F_EXCL;
860 flags = NLM_F_CREATE|NLM_F_APPEND;
864 flags = NLM_F_REPLACE;
870 return iproute_get(argv+1);
873 return iproute_list_or_flush(argv+1, 0);
874 case 8: /* prepend */
875 flags = NLM_F_CREATE;
877 case 9: /* replace */
878 flags = NLM_F_CREATE|NLM_F_REPLACE;
884 return iproute_list_or_flush(argv+1, 1);
886 bb_error_msg_and_die("unknown command %s", *argv);
889 return iproute_modify(cmd, flags, argv+1);