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
18 #include <sys/socket.h>
26 #include "ip_common.h"
29 #define RTAX_RTTVAR RTAX_HOPS
40 struct rtnl_handle *rth;
41 int protocol, protocolmask;
56 static int flush_update(void)
58 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
59 perror("Failed to send flush request\n");
66 static int print_route(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
67 struct nlmsghdr *n, void *arg)
69 FILE *fp = (FILE*)arg;
70 struct rtmsg *r = NLMSG_DATA(n);
71 int len = n->nlmsg_len;
72 struct rtattr * tb[RTA_MAX+1];
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);
85 if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
87 len -= NLMSG_LENGTH(sizeof(*r));
89 bb_error_msg("wrong nlmsg len %d", len);
93 if (r->rtm_family == AF_INET6)
95 else if (r->rtm_family == AF_INET)
98 if (r->rtm_family == AF_INET6) {
101 if (!(r->rtm_flags&RTM_F_CLONED)) {
105 if (r->rtm_flags&RTM_F_CLONED) {
108 if (filter.tb == RT_TABLE_LOCAL) {
109 if (r->rtm_type != RTN_LOCAL) {
112 } else if (filter.tb == RT_TABLE_MAIN) {
113 if (r->rtm_type == RTN_LOCAL) {
122 if (filter.tb > 0 && filter.tb != r->rtm_table) {
126 if (filter.rdst.family &&
127 (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len)) {
130 if (filter.mdst.family &&
131 (r->rtm_family != filter.mdst.family ||
132 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) {
135 if (filter.rsrc.family &&
136 (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len)) {
139 if (filter.msrc.family &&
140 (r->rtm_family != filter.msrc.family ||
141 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) {
145 memset(tb, 0, sizeof(tb));
146 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
148 if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
150 if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
151 inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
154 if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
156 if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
157 inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
161 r->rtm_family == AF_INET6 &&
162 r->rtm_dst_len == 0 &&
163 r->rtm_type == RTN_UNREACHABLE &&
165 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
170 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
174 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
175 memcpy(fn, n, n->nlmsg_len);
176 fn->nlmsg_type = RTM_DELROUTE;
177 fn->nlmsg_flags = NLM_F_REQUEST;
178 fn->nlmsg_seq = ++filter.rth->seq;
179 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
184 if (n->nlmsg_type == RTM_DELROUTE) {
185 fprintf(fp, "Deleted ");
187 if (r->rtm_type != RTN_UNICAST && !filter.type) {
188 fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
192 if (r->rtm_dst_len != host_len) {
193 fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
194 RTA_PAYLOAD(tb[RTA_DST]),
195 RTA_DATA(tb[RTA_DST]),
200 fprintf(fp, "%s ", format_host(r->rtm_family,
201 RTA_PAYLOAD(tb[RTA_DST]),
202 RTA_DATA(tb[RTA_DST]),
206 } else if (r->rtm_dst_len) {
207 fprintf(fp, "0/%d ", r->rtm_dst_len);
209 fprintf(fp, "default ");
212 if (r->rtm_src_len != host_len) {
213 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
214 RTA_PAYLOAD(tb[RTA_SRC]),
215 RTA_DATA(tb[RTA_SRC]),
220 fprintf(fp, "from %s ", format_host(r->rtm_family,
221 RTA_PAYLOAD(tb[RTA_SRC]),
222 RTA_DATA(tb[RTA_SRC]),
226 } else if (r->rtm_src_len) {
227 fprintf(fp, "from 0/%u ", r->rtm_src_len);
229 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
230 fprintf(fp, "via %s ",
231 format_host(r->rtm_family,
232 RTA_PAYLOAD(tb[RTA_GATEWAY]),
233 RTA_DATA(tb[RTA_GATEWAY]),
234 abuf, sizeof(abuf)));
236 if (tb[RTA_OIF] && filter.oifmask != -1) {
237 fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
240 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
241 /* Do not use format_host(). It is our local addr
242 and symbolic name will not be useful.
244 fprintf(fp, " src %s ",
245 rt_addr_n2a(r->rtm_family,
246 RTA_PAYLOAD(tb[RTA_PREFSRC]),
247 RTA_DATA(tb[RTA_PREFSRC]),
248 abuf, sizeof(abuf)));
250 if (tb[RTA_PRIORITY]) {
251 fprintf(fp, " metric %d ", *(__u32*)RTA_DATA(tb[RTA_PRIORITY]));
253 if (r->rtm_family == AF_INET6) {
254 struct rta_cacheinfo *ci = NULL;
255 if (tb[RTA_CACHEINFO]) {
256 ci = RTA_DATA(tb[RTA_CACHEINFO]);
258 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
263 if (r->rtm_flags & RTM_F_CLONED) {
264 fprintf(fp, "%s cache ", _SL_);
266 if (ci->rta_expires) {
267 fprintf(fp, " expires %dsec", ci->rta_expires/hz);
269 if (ci->rta_error != 0) {
270 fprintf(fp, " error %d", ci->rta_error);
273 if (ci->rta_error != 0)
274 fprintf(fp, " error %d", ci->rta_error);
277 if (tb[RTA_IIF] && filter.iifmask != -1) {
278 fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
285 static int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
287 struct rtnl_handle rth;
294 struct rtattr * mxrta = (void*)mxbuf;
302 memset(&req, 0, sizeof(req));
304 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
305 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
306 req.n.nlmsg_type = cmd;
307 req.r.rtm_family = preferred_family;
308 req.r.rtm_table = RT_TABLE_MAIN;
309 req.r.rtm_scope = RT_SCOPE_NOWHERE;
311 if (cmd != RTM_DELROUTE) {
312 req.r.rtm_protocol = RTPROT_BOOT;
313 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
314 req.r.rtm_type = RTN_UNICAST;
317 mxrta->rta_type = RTA_METRICS;
318 mxrta->rta_len = RTA_LENGTH(0);
321 if (strcmp(*argv, "src") == 0) {
324 get_addr(&addr, *argv, req.r.rtm_family);
325 if (req.r.rtm_family == AF_UNSPEC) {
326 req.r.rtm_family = addr.family;
328 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
329 } else if (strcmp(*argv, "via") == 0) {
333 get_addr(&addr, *argv, req.r.rtm_family);
334 if (req.r.rtm_family == AF_UNSPEC) {
335 req.r.rtm_family = addr.family;
337 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
338 } else if (strcmp(*argv, "mtu") == 0) {
341 if (strcmp(*argv, "lock") == 0) {
342 mxlock |= (1<<RTAX_MTU);
345 if (get_unsigned(&mtu, *argv, 0)) {
346 invarg(*argv, "mtu");
348 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
349 } else if (matches(*argv, "protocol") == 0) {
352 if (rtnl_rtprot_a2n(&prot, *argv))
353 invarg(*argv, "protocol");
354 req.r.rtm_protocol = prot;
356 } else if (strcmp(*argv, "dev") == 0 ||
357 strcmp(*argv, "oif") == 0) {
364 if (strcmp(*argv, "to") == 0) {
367 if ((**argv < '0' || **argv > '9') &&
368 rtnl_rtntype_a2n(&type, *argv) == 0) {
370 req.r.rtm_type = type;
375 duparg2("to", *argv);
377 get_prefix(&dst, *argv, req.r.rtm_family);
378 if (req.r.rtm_family == AF_UNSPEC) {
379 req.r.rtm_family = dst.family;
381 req.r.rtm_dst_len = dst.bitlen;
384 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
390 if (rtnl_open(&rth, 0) < 0) {
400 if ((idx = ll_name_to_index(d)) == 0) {
401 bb_error_msg("Cannot find device \"%s\"", d);
404 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
408 if (mxrta->rta_len > RTA_LENGTH(0)) {
410 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
412 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
415 if (req.r.rtm_family == AF_UNSPEC) {
416 req.r.rtm_family = AF_INET;
419 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
426 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
432 struct sockaddr_nl nladdr;
434 memset(&nladdr, 0, sizeof(nladdr));
435 memset(&req, 0, sizeof(req));
436 nladdr.nl_family = AF_NETLINK;
438 req.nlh.nlmsg_len = sizeof(req);
439 req.nlh.nlmsg_type = RTM_GETROUTE;
440 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
441 req.nlh.nlmsg_pid = 0;
442 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
443 req.rtm.rtm_family = family;
444 req.rtm.rtm_flags |= RTM_F_CLONED;
446 return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
449 static int iproute_flush_cache(void)
451 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
454 int flush_fd = open (ROUTE_FLUSH_PATH, O_WRONLY);
458 fprintf (stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH);
462 len = strlen (buffer);
464 if ((write (flush_fd, (void *)buffer, len)) < len) {
465 fprintf (stderr, "Cannot flush routing cache\n");
472 static void iproute_reset_filter(void)
474 memset(&filter, 0, sizeof(filter));
475 filter.mdst.bitlen = -1;
476 filter.msrc.bitlen = -1;
479 static int iproute_list_or_flush(int argc, char **argv, int flush)
481 int do_ipv6 = preferred_family;
482 struct rtnl_handle rth;
486 iproute_reset_filter();
487 filter.tb = RT_TABLE_MAIN;
489 if (flush && argc <= 0) {
490 bb_error_msg(bb_msg_requires_arg, "\"ip route flush\"");
495 if (matches(*argv, "protocol") == 0) {
498 filter.protocolmask = -1;
499 if (rtnl_rtprot_a2n(&prot, *argv)) {
500 if (strcmp(*argv, "all") != 0) {
501 invarg(*argv, "protocol");
504 filter.protocolmask = 0;
506 filter.protocol = prot;
507 } else if (strcmp(*argv, "dev") == 0 ||
508 strcmp(*argv, "oif") == 0) {
511 } else if (strcmp(*argv, "iif") == 0) {
514 } else if (matches(*argv, "from") == 0) {
516 if (matches(*argv, "root") == 0) {
518 get_prefix(&filter.rsrc, *argv, do_ipv6);
519 } else if (matches(*argv, "match") == 0) {
521 get_prefix(&filter.msrc, *argv, do_ipv6);
523 if (matches(*argv, "exact") == 0) {
526 get_prefix(&filter.msrc, *argv, do_ipv6);
527 filter.rsrc = filter.msrc;
530 if (matches(*argv, "to") == 0) {
533 if (matches(*argv, "root") == 0) {
535 get_prefix(&filter.rdst, *argv, do_ipv6);
536 } else if (matches(*argv, "match") == 0) {
538 get_prefix(&filter.mdst, *argv, do_ipv6);
539 } else if (matches(*argv, "table") == 0) {
541 if (matches(*argv, "cache") == 0) {
543 } else if (matches(*argv, "main") != 0) {
544 invarg(*argv, "table");
546 } else if (matches(*argv, "cache") == 0) {
549 if (matches(*argv, "exact") == 0) {
552 get_prefix(&filter.mdst, *argv, do_ipv6);
553 filter.rdst = filter.mdst;
559 if (do_ipv6 == AF_UNSPEC && filter.tb) {
563 if (rtnl_open(&rth, 0) < 0) {
573 if ((idx = ll_name_to_index(id)) == 0) {
574 bb_error_msg("Cannot find device \"%s\"", id);
581 if ((idx = ll_name_to_index(od)) == 0) {
582 bb_error_msg("Cannot find device \"%s\"", od);
590 char flushb[4096-512];
592 if (filter.tb == -1) {
593 if (do_ipv6 != AF_INET6)
594 iproute_flush_cache();
595 if (do_ipv6 == AF_INET)
599 filter.flushb = flushb;
601 filter.flushe = sizeof(flushb);
605 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
606 perror("Cannot send dump request");
610 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
611 bb_error_msg("Flush terminated");
614 if (filter.flushed == 0) {
618 if (flush_update() < 0)
623 if (filter.tb != -1) {
624 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
625 bb_perror_msg_and_die("Cannot send dump request");
628 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
629 bb_perror_msg_and_die("Cannot send dump request");
633 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
634 bb_error_msg_and_die("Dump terminated");
641 static int iproute_get(int argc, char **argv)
643 struct rtnl_handle rth;
653 static const char * const options[] =
654 { "from", "iif", "oif", "dev", "notify", "connected", "to", 0 };
656 memset(&req, 0, sizeof(req));
658 iproute_reset_filter();
660 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
661 req.n.nlmsg_flags = NLM_F_REQUEST;
662 req.n.nlmsg_type = RTM_GETROUTE;
663 req.r.rtm_family = preferred_family;
665 req.r.rtm_protocol = 0;
668 req.r.rtm_src_len = 0;
669 req.r.rtm_dst_len = 0;
673 switch (compare_string_array(options, *argv)) {
679 get_prefix(&addr, *argv, req.r.rtm_family);
680 if (req.r.rtm_family == AF_UNSPEC) {
681 req.r.rtm_family = addr.family;
684 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
686 req.r.rtm_src_len = addr.bitlen;
699 req.r.rtm_flags |= RTM_F_NOTIFY;
701 case 5: /* connected */
709 get_prefix(&addr, *argv, req.r.rtm_family);
710 if (req.r.rtm_family == AF_UNSPEC) {
711 req.r.rtm_family = addr.family;
714 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
716 req.r.rtm_dst_len = addr.bitlen;
722 if (req.r.rtm_dst_len == 0) {
723 bb_error_msg_and_die("need at least destination address");
726 if (rtnl_open(&rth, 0) < 0)
735 if ((idx = ll_name_to_index(idev)) == 0) {
736 bb_error_msg("Cannot find device \"%s\"", idev);
739 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
742 if ((idx = ll_name_to_index(odev)) == 0) {
743 bb_error_msg("Cannot find device \"%s\"", odev);
746 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
750 if (req.r.rtm_family == AF_UNSPEC) {
751 req.r.rtm_family = AF_INET;
754 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
758 if (connected && !from_ok) {
759 struct rtmsg *r = NLMSG_DATA(&req.n);
760 int len = req.n.nlmsg_len;
761 struct rtattr * tb[RTA_MAX+1];
763 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
764 bb_error_msg_and_die("An error :-)");
767 if (req.n.nlmsg_type != RTM_NEWROUTE) {
768 bb_error_msg("Not a route?");
771 len -= NLMSG_LENGTH(sizeof(*r));
773 bb_error_msg("Wrong len %d", len);
777 memset(tb, 0, sizeof(tb));
778 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
780 if (tb[RTA_PREFSRC]) {
781 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
782 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
783 } else if (!tb[RTA_SRC]) {
784 bb_error_msg("Failed to connect the route");
787 if (!odev && tb[RTA_OIF]) {
788 tb[RTA_OIF]->rta_type = 0;
790 if (tb[RTA_GATEWAY]) {
791 tb[RTA_GATEWAY]->rta_type = 0;
793 if (!idev && tb[RTA_IIF]) {
794 tb[RTA_IIF]->rta_type = 0;
796 req.n.nlmsg_flags = NLM_F_REQUEST;
797 req.n.nlmsg_type = RTM_GETROUTE;
799 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
804 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
805 bb_error_msg_and_die("An error :-)");
811 int do_iproute(int argc, char **argv)
813 static const char * const ip_route_commands[] =
814 { "add", "append", "change", "chg", "delete", "del", "get",
815 "list", "show", "prepend", "replace", "test", "flush", 0 };
817 unsigned int flags = 0;
818 int cmd = RTM_NEWROUTE;
821 command_num = compare_string_array(ip_route_commands, *argv);
823 switch (command_num) {
825 flags = NLM_F_CREATE|NLM_F_EXCL;
828 flags = NLM_F_CREATE|NLM_F_APPEND;
832 flags = NLM_F_REPLACE;
839 return iproute_get(argc-1, argv+1);
842 return iproute_list_or_flush(argc-1, argv+1, 0);
843 case 9: /* prepend */
844 flags = NLM_F_CREATE;
845 case 10: /* replace */
846 flags = NLM_F_CREATE|NLM_F_REPLACE;
850 return iproute_list_or_flush(argc-1, argv+1, 1);
852 bb_error_msg_and_die("Unknown command %s", *argv);
855 return iproute_modify(cmd, flags, argc-1, argv+1);