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];
94 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
95 fprintf(stderr, "Not a route: %08x %08x %08x\n",
96 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
99 if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
101 len -= NLMSG_LENGTH(sizeof(*r));
103 bb_error_msg_and_die("wrong nlmsg len %d", len);
105 if (r->rtm_family == AF_INET6)
107 else if (r->rtm_family == AF_INET)
110 if (r->rtm_family == AF_INET6) {
113 if (!(r->rtm_flags & RTM_F_CLONED)) {
117 if (r->rtm_flags & RTM_F_CLONED) {
120 if (filter.tb == RT_TABLE_LOCAL) {
121 if (r->rtm_type != RTN_LOCAL) {
124 } else if (filter.tb == RT_TABLE_MAIN) {
125 if (r->rtm_type == RTN_LOCAL) {
134 if (filter.tb > 0 && filter.tb != r->rtm_table) {
138 if (filter.rdst.family &&
139 (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len)) {
142 if (filter.mdst.family &&
143 (r->rtm_family != filter.mdst.family ||
144 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) {
147 if (filter.rsrc.family &&
148 (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len)) {
151 if (filter.msrc.family &&
152 (r->rtm_family != filter.msrc.family ||
153 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) {
157 memset(tb, 0, sizeof(tb));
158 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
160 if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
162 if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
163 inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
166 if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
168 if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
169 inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
173 r->rtm_family == AF_INET6 &&
174 r->rtm_dst_len == 0 &&
175 r->rtm_type == RTN_UNREACHABLE &&
177 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
182 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
184 bb_error_msg_and_die("flush");
186 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
187 memcpy(fn, n, n->nlmsg_len);
188 fn->nlmsg_type = RTM_DELROUTE;
189 fn->nlmsg_flags = NLM_F_REQUEST;
190 fn->nlmsg_seq = ++filter.rth->seq;
191 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
196 if (n->nlmsg_type == RTM_DELROUTE) {
197 fprintf(fp, "Deleted ");
199 if (r->rtm_type != RTN_UNICAST && !filter.type) {
200 fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
204 if (r->rtm_dst_len != host_len) {
205 fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
206 RTA_PAYLOAD(tb[RTA_DST]),
207 RTA_DATA(tb[RTA_DST]),
212 fprintf(fp, "%s ", format_host(r->rtm_family,
213 RTA_PAYLOAD(tb[RTA_DST]),
214 RTA_DATA(tb[RTA_DST]),
218 } else if (r->rtm_dst_len) {
219 fprintf(fp, "0/%d ", r->rtm_dst_len);
221 fprintf(fp, "default ");
224 if (r->rtm_src_len != host_len) {
225 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
226 RTA_PAYLOAD(tb[RTA_SRC]),
227 RTA_DATA(tb[RTA_SRC]),
232 fprintf(fp, "from %s ", format_host(r->rtm_family,
233 RTA_PAYLOAD(tb[RTA_SRC]),
234 RTA_DATA(tb[RTA_SRC]),
238 } else if (r->rtm_src_len) {
239 fprintf(fp, "from 0/%u ", r->rtm_src_len);
241 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
242 fprintf(fp, "via %s ",
243 format_host(r->rtm_family,
244 RTA_PAYLOAD(tb[RTA_GATEWAY]),
245 RTA_DATA(tb[RTA_GATEWAY]),
246 abuf, sizeof(abuf)));
248 if (tb[RTA_OIF] && filter.oifmask != -1) {
249 fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
252 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
253 /* Do not use format_host(). It is our local addr
254 and symbolic name will not be useful.
256 fprintf(fp, " src %s ",
257 rt_addr_n2a(r->rtm_family,
258 RTA_PAYLOAD(tb[RTA_PREFSRC]),
259 RTA_DATA(tb[RTA_PREFSRC]),
260 abuf, sizeof(abuf)));
262 if (tb[RTA_PRIORITY]) {
263 fprintf(fp, " metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
265 if (r->rtm_family == AF_INET6) {
266 struct rta_cacheinfo *ci = NULL;
267 if (tb[RTA_CACHEINFO]) {
268 ci = RTA_DATA(tb[RTA_CACHEINFO]);
270 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
271 if (r->rtm_flags & RTM_F_CLONED) {
272 fprintf(fp, "%c cache ", _SL_);
274 if (ci->rta_expires) {
275 fprintf(fp, " expires %dsec", ci->rta_expires / get_hz());
277 if (ci->rta_error != 0) {
278 fprintf(fp, " error %d", ci->rta_error);
281 if (ci->rta_error != 0)
282 fprintf(fp, " error %d", ci->rta_error);
285 if (tb[RTA_IIF] && filter.iifmask != -1) {
286 fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
293 /* Return value becomes exitcode. It's okay to not return at all */
294 static int iproute_modify(int cmd, unsigned flags, char **argv)
296 static const char keywords[] ALIGN1 =
297 "src\0""via\0""mtu\0""lock\0""protocol\0"USE_FEATURE_IP_RULE("table\0")
298 "dev\0""oif\0""to\0";
304 USE_FEATURE_IP_RULE(ARG_table,)
315 struct rtnl_handle rth;
322 struct rtattr * mxrta = (void*)mxbuf;
328 memset(&req, 0, sizeof(req));
330 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
331 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
332 req.n.nlmsg_type = cmd;
333 req.r.rtm_family = preferred_family;
334 req.r.rtm_table = RT_TABLE_MAIN;
335 req.r.rtm_scope = RT_SCOPE_NOWHERE;
337 if (cmd != RTM_DELROUTE) {
338 req.r.rtm_protocol = RTPROT_BOOT;
339 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
340 req.r.rtm_type = RTN_UNICAST;
343 mxrta->rta_type = RTA_METRICS;
344 mxrta->rta_len = RTA_LENGTH(0);
347 arg = index_in_substrings(keywords, *argv);
348 if (arg == ARG_src) {
351 get_addr(&addr, *argv, req.r.rtm_family);
352 if (req.r.rtm_family == AF_UNSPEC)
353 req.r.rtm_family = addr.family;
354 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
355 } else if (arg == ARG_via) {
359 get_addr(&addr, *argv, req.r.rtm_family);
360 if (req.r.rtm_family == AF_UNSPEC) {
361 req.r.rtm_family = addr.family;
363 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
364 } else if (arg == ARG_mtu) {
367 if (index_in_strings(keywords, *argv) == PARM_lock) {
368 mxlock |= (1 << RTAX_MTU);
371 if (get_unsigned(&mtu, *argv, 0))
372 invarg(*argv, "mtu");
373 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
374 } else if (arg == ARG_protocol) {
377 if (rtnl_rtprot_a2n(&prot, *argv))
378 invarg(*argv, "protocol");
379 req.r.rtm_protocol = prot;
381 #if ENABLE_FEATURE_IP_RULE
382 } else if (arg == ARG_table) {
385 if (rtnl_rttable_a2n(&tid, *argv))
386 invarg(*argv, "table");
387 req.r.rtm_table = tid;
389 } else if (arg == ARG_dev || arg == ARG_oif) {
399 if ((**argv < '0' || **argv > '9')
400 && rtnl_rtntype_a2n(&type, *argv) == 0) {
402 req.r.rtm_type = type;
407 duparg2("to", *argv);
409 get_prefix(&dst, *argv, req.r.rtm_family);
410 if (req.r.rtm_family == AF_UNSPEC) {
411 req.r.rtm_family = dst.family;
413 req.r.rtm_dst_len = dst.bitlen;
416 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
430 idx = xll_name_to_index(d);
431 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
435 if (mxrta->rta_len > RTA_LENGTH(0)) {
437 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
439 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
442 if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
443 req.r.rtm_scope = RT_SCOPE_HOST;
444 else if (req.r.rtm_type == RTN_BROADCAST ||
445 req.r.rtm_type == RTN_MULTICAST ||
446 req.r.rtm_type == RTN_ANYCAST)
447 req.r.rtm_scope = RT_SCOPE_LINK;
448 else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
449 if (cmd == RTM_DELROUTE)
450 req.r.rtm_scope = RT_SCOPE_NOWHERE;
451 else if (!(ok & gw_ok))
452 req.r.rtm_scope = RT_SCOPE_LINK;
455 if (req.r.rtm_family == AF_UNSPEC) {
456 req.r.rtm_family = AF_INET;
459 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
466 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
472 struct sockaddr_nl nladdr;
474 memset(&nladdr, 0, sizeof(nladdr));
475 memset(&req, 0, sizeof(req));
476 nladdr.nl_family = AF_NETLINK;
478 req.nlh.nlmsg_len = sizeof(req);
479 req.nlh.nlmsg_type = RTM_GETROUTE;
480 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
481 req.nlh.nlmsg_pid = 0;
482 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
483 req.rtm.rtm_family = family;
484 req.rtm.rtm_flags |= RTM_F_CLONED;
486 return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
489 static void iproute_flush_cache(void)
491 static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
492 int flush_fd = open_or_warn(fn, O_WRONLY);
498 if (write(flush_fd, "-1", 2) < 2) {
499 bb_perror_msg("cannot flush routing cache");
505 static void iproute_reset_filter(void)
507 memset(&filter, 0, sizeof(filter));
508 filter.mdst.bitlen = -1;
509 filter.msrc.bitlen = -1;
512 /* Return value becomes exitcode. It's okay to not return at all */
513 static int iproute_list_or_flush(char **argv, int flush)
515 int do_ipv6 = preferred_family;
516 struct rtnl_handle rth;
519 static const char keywords[] ALIGN1 =
520 /* "ip route list/flush" parameters: */
521 "protocol\0" "dev\0" "oif\0" "iif\0"
522 "via\0" "table\0" "cache\0"
524 /* and possible further keywords */
532 KW_proto, KW_dev, KW_oif, KW_iif,
533 KW_via, KW_table, KW_cache,
544 iproute_reset_filter();
545 filter.tb = RT_TABLE_MAIN;
548 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
551 arg = index_in_substrings(keywords, *argv);
552 if (arg == KW_proto) {
555 filter.protocolmask = -1;
556 if (rtnl_rtprot_a2n(&prot, *argv)) {
557 if (index_in_strings(keywords, *argv) != KW_all)
558 invarg(*argv, "protocol");
560 filter.protocolmask = 0;
562 filter.protocol = prot;
563 } else if (arg == KW_dev || arg == KW_oif) {
566 } else if (arg == KW_iif) {
569 } else if (arg == KW_via) {
571 get_prefix(&filter.rvia, *argv, do_ipv6);
572 } else if (arg == KW_table) { /* table all/cache/main */
574 parm = index_in_substrings(keywords, *argv);
575 if (parm == KW_cache)
577 else if (parm == KW_all)
579 else if (parm != KW_main) {
580 #if ENABLE_FEATURE_IP_RULE
582 if (rtnl_rttable_a2n(&tid, *argv))
583 invarg(*argv, "table");
586 invarg(*argv, "table");
589 } else if (arg == KW_cache) {
590 /* The command 'ip route flush cache' is used by OpenSWAN.
591 * Assuming it's a synonym for 'ip route flush table cache' */
593 } else if (arg == KW_from) {
595 parm = index_in_substrings(keywords, *argv);
596 if (parm == KW_root) {
598 get_prefix(&filter.rsrc, *argv, do_ipv6);
599 } else if (parm == KW_match) {
601 get_prefix(&filter.msrc, *argv, do_ipv6);
603 if (parm == KW_exact)
605 get_prefix(&filter.msrc, *argv, do_ipv6);
606 filter.rsrc = filter.msrc;
608 } else { /* "to" is the default parameter */
611 arg = index_in_substrings(keywords, *argv);
613 /* parm = arg; - would be more plausible, but we reuse 'arg' here */
614 if (arg == KW_root) {
616 get_prefix(&filter.rdst, *argv, do_ipv6);
617 } else if (arg == KW_match) {
619 get_prefix(&filter.mdst, *argv, do_ipv6);
620 } else { /* "to exact" is the default */
623 get_prefix(&filter.mdst, *argv, do_ipv6);
624 filter.rdst = filter.mdst;
630 if (do_ipv6 == AF_UNSPEC && filter.tb) {
641 idx = xll_name_to_index(id);
646 idx = xll_name_to_index(od);
653 char flushb[4096-512];
655 if (filter.tb == -1) { /* "flush table cache" */
656 if (do_ipv6 != AF_INET6)
657 iproute_flush_cache();
658 if (do_ipv6 == AF_INET)
662 filter.flushb = flushb;
664 filter.flushe = sizeof(flushb);
668 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
670 xrtnl_dump_filter(&rth, print_route, stdout);
671 if (filter.flushed == 0)
678 if (filter.tb != -1) {
679 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
680 } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
681 bb_perror_msg_and_die("cannot send dump request");
683 xrtnl_dump_filter(&rth, print_route, stdout);
689 /* Return value becomes exitcode. It's okay to not return at all */
690 static int iproute_get(char **argv)
692 struct rtnl_handle rth;
702 static const char options[] ALIGN1 =
703 "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
705 memset(&req, 0, sizeof(req));
707 iproute_reset_filter();
709 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
710 req.n.nlmsg_flags = NLM_F_REQUEST;
711 req.n.nlmsg_type = RTM_GETROUTE;
712 req.r.rtm_family = preferred_family;
714 req.r.rtm_protocol = 0;
717 req.r.rtm_src_len = 0;
718 req.r.rtm_dst_len = 0;
722 switch (index_in_strings(options, *argv)) {
728 get_prefix(&addr, *argv, req.r.rtm_family);
729 if (req.r.rtm_family == AF_UNSPEC) {
730 req.r.rtm_family = addr.family;
733 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
735 req.r.rtm_src_len = addr.bitlen;
748 req.r.rtm_flags |= RTM_F_NOTIFY;
750 case 5: /* connected */
758 get_prefix(&addr, *argv, req.r.rtm_family);
759 if (req.r.rtm_family == AF_UNSPEC) {
760 req.r.rtm_family = addr.family;
763 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
765 req.r.rtm_dst_len = addr.bitlen;
771 if (req.r.rtm_dst_len == 0) {
772 bb_error_msg_and_die("need at least destination address");
783 idx = xll_name_to_index(idev);
784 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
787 idx = xll_name_to_index(odev);
788 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
792 if (req.r.rtm_family == AF_UNSPEC) {
793 req.r.rtm_family = AF_INET;
796 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
800 if (connected && !from_ok) {
801 struct rtmsg *r = NLMSG_DATA(&req.n);
802 int len = req.n.nlmsg_len;
803 struct rtattr * tb[RTA_MAX+1];
805 print_route(NULL, &req.n, (void*)stdout);
807 if (req.n.nlmsg_type != RTM_NEWROUTE) {
808 bb_error_msg_and_die("not a route?");
810 len -= NLMSG_LENGTH(sizeof(*r));
812 bb_error_msg_and_die("wrong len %d", len);
815 memset(tb, 0, sizeof(tb));
816 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
818 if (tb[RTA_PREFSRC]) {
819 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
820 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
821 } else if (!tb[RTA_SRC]) {
822 bb_error_msg_and_die("failed to connect the route");
824 if (!odev && tb[RTA_OIF]) {
825 tb[RTA_OIF]->rta_type = 0;
827 if (tb[RTA_GATEWAY]) {
828 tb[RTA_GATEWAY]->rta_type = 0;
830 if (!idev && tb[RTA_IIF]) {
831 tb[RTA_IIF]->rta_type = 0;
833 req.n.nlmsg_flags = NLM_F_REQUEST;
834 req.n.nlmsg_type = RTM_GETROUTE;
836 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
840 print_route(NULL, &req.n, (void*)stdout);
844 /* Return value becomes exitcode. It's okay to not return at all */
845 int do_iproute(char **argv)
847 static const char ip_route_commands[] ALIGN1 =
848 /*0-3*/ "add\0""append\0""change\0""chg\0"
849 /*4-7*/ "delete\0""get\0""list\0""show\0"
850 /*8..*/ "prepend\0""replace\0""test\0""flush\0";
853 int cmd = RTM_NEWROUTE;
856 return iproute_list_or_flush(argv, 0);
858 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
859 /* It probably means that it is using "first match" rule */
860 command_num = index_in_substrings(ip_route_commands, *argv);
862 switch (command_num) {
864 flags = NLM_F_CREATE|NLM_F_EXCL;
867 flags = NLM_F_CREATE|NLM_F_APPEND;
871 flags = NLM_F_REPLACE;
877 return iproute_get(argv+1);
880 return iproute_list_or_flush(argv+1, 0);
881 case 8: /* prepend */
882 flags = NLM_F_CREATE;
884 case 9: /* replace */
885 flags = NLM_F_CREATE|NLM_F_REPLACE;
891 return iproute_list_or_flush(argv+1, 1);
893 bb_error_msg_and_die("unknown command %s", *argv);
896 return iproute_modify(cmd, flags, argv+1);