2 * iproute.c "ip route".
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
14 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
15 * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
18 #include <sys/socket.h>
31 #define RTAX_RTTVAR RTAX_HOPS
42 struct rtnl_handle *rth;
43 int protocol, protocolmask;
58 static int flush_update(void)
60 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
61 perror("Failed to send flush request\n");
68 static int print_route(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
70 FILE *fp = (FILE*)arg;
71 struct rtmsg *r = NLMSG_DATA(n);
72 int len = n->nlmsg_len;
73 struct rtattr * tb[RTA_MAX+1];
79 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
80 fprintf(stderr, "Not a route: %08x %08x %08x\n",
81 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
84 if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
86 len -= NLMSG_LENGTH(sizeof(*r));
88 bb_error_msg("wrong nlmsg len %d", len);
92 if (r->rtm_family == AF_INET6)
94 else if (r->rtm_family == AF_INET)
97 if (r->rtm_family == AF_INET6) {
100 if (!(r->rtm_flags&RTM_F_CLONED)) {
104 if (r->rtm_flags&RTM_F_CLONED) {
107 if (filter.tb == RT_TABLE_LOCAL) {
108 if (r->rtm_type != RTN_LOCAL) {
111 } else if (filter.tb == RT_TABLE_MAIN) {
112 if (r->rtm_type == RTN_LOCAL) {
121 if (filter.tb > 0 && filter.tb != r->rtm_table) {
125 if (filter.rdst.family &&
126 (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len)) {
129 if (filter.mdst.family &&
130 (r->rtm_family != filter.mdst.family ||
131 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) {
134 if (filter.rsrc.family &&
135 (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len)) {
138 if (filter.msrc.family &&
139 (r->rtm_family != filter.msrc.family ||
140 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) {
144 memset(tb, 0, sizeof(tb));
145 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
148 r->rtm_family == AF_INET6 &&
149 r->rtm_dst_len == 0 &&
150 r->rtm_type == RTN_UNREACHABLE &&
152 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
157 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
161 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
162 memcpy(fn, n, n->nlmsg_len);
163 fn->nlmsg_type = RTM_DELROUTE;
164 fn->nlmsg_flags = NLM_F_REQUEST;
165 fn->nlmsg_seq = ++filter.rth->seq;
166 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
171 if (n->nlmsg_type == RTM_DELROUTE) {
172 fprintf(fp, "Deleted ");
174 if (r->rtm_type != RTN_UNICAST && !filter.type) {
175 fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
179 if (r->rtm_dst_len != host_len) {
180 fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
181 RTA_PAYLOAD(tb[RTA_DST]),
182 RTA_DATA(tb[RTA_DST]),
187 fprintf(fp, "%s ", format_host(r->rtm_family,
188 RTA_PAYLOAD(tb[RTA_DST]),
189 RTA_DATA(tb[RTA_DST]),
193 } else if (r->rtm_dst_len) {
194 fprintf(fp, "0/%d ", r->rtm_dst_len);
196 fprintf(fp, "default ");
199 if (r->rtm_src_len != host_len) {
200 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
201 RTA_PAYLOAD(tb[RTA_SRC]),
202 RTA_DATA(tb[RTA_SRC]),
207 fprintf(fp, "from %s ", format_host(r->rtm_family,
208 RTA_PAYLOAD(tb[RTA_SRC]),
209 RTA_DATA(tb[RTA_SRC]),
213 } else if (r->rtm_src_len) {
214 fprintf(fp, "from 0/%u ", r->rtm_src_len);
216 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
217 fprintf(fp, "via %s ",
218 format_host(r->rtm_family,
219 RTA_PAYLOAD(tb[RTA_GATEWAY]),
220 RTA_DATA(tb[RTA_GATEWAY]),
221 abuf, sizeof(abuf)));
223 if (tb[RTA_OIF] && filter.oifmask != -1) {
224 fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
227 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
228 /* Do not use format_host(). It is our local addr
229 and symbolic name will not be useful.
231 fprintf(fp, " src %s ",
232 rt_addr_n2a(r->rtm_family,
233 RTA_PAYLOAD(tb[RTA_PREFSRC]),
234 RTA_DATA(tb[RTA_PREFSRC]),
235 abuf, sizeof(abuf)));
237 if (tb[RTA_PRIORITY]) {
238 fprintf(fp, " metric %d ", *(__u32*)RTA_DATA(tb[RTA_PRIORITY]));
240 if (r->rtm_family == AF_INET6) {
241 struct rta_cacheinfo *ci = NULL;
242 if (tb[RTA_CACHEINFO]) {
243 ci = RTA_DATA(tb[RTA_CACHEINFO]);
245 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
250 if (r->rtm_flags & RTM_F_CLONED) {
251 fprintf(fp, "%s cache ", _SL_);
253 if (ci->rta_expires) {
254 fprintf(fp, " expires %dsec", ci->rta_expires/hz);
256 if (ci->rta_error != 0) {
257 fprintf(fp, " error %d", ci->rta_error);
260 if (ci->rta_error != 0)
261 fprintf(fp, " error %d", ci->rta_error);
264 if (tb[RTA_IIF] && filter.iifmask != -1) {
265 fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
272 static int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
274 struct rtnl_handle rth;
281 struct rtattr * mxrta = (void*)mxbuf;
289 memset(&req, 0, sizeof(req));
291 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
292 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
293 req.n.nlmsg_type = cmd;
294 req.r.rtm_family = preferred_family;
295 req.r.rtm_table = RT_TABLE_MAIN;
296 req.r.rtm_scope = RT_SCOPE_NOWHERE;
298 if (cmd != RTM_DELROUTE) {
299 req.r.rtm_protocol = RTPROT_BOOT;
300 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
301 req.r.rtm_type = RTN_UNICAST;
304 mxrta->rta_type = RTA_METRICS;
305 mxrta->rta_len = RTA_LENGTH(0);
308 if (strcmp(*argv, "src") == 0) {
311 get_addr(&addr, *argv, req.r.rtm_family);
312 if (req.r.rtm_family == AF_UNSPEC) {
313 req.r.rtm_family = addr.family;
315 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
316 } else if (strcmp(*argv, "via") == 0) {
320 get_addr(&addr, *argv, req.r.rtm_family);
321 if (req.r.rtm_family == AF_UNSPEC) {
322 req.r.rtm_family = addr.family;
324 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
325 } else if (strcmp(*argv, "mtu") == 0) {
328 if (strcmp(*argv, "lock") == 0) {
329 mxlock |= (1<<RTAX_MTU);
332 if (get_unsigned(&mtu, *argv, 0)) {
333 invarg("\"mtu\" value is invalid\n", *argv);
335 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
336 } else if (matches(*argv, "protocol") == 0) {
339 if (rtnl_rtprot_a2n(&prot, *argv))
340 invarg("\"protocol\" value is invalid\n", *argv);
341 req.r.rtm_protocol = prot;
343 } else if (strcmp(*argv, "dev") == 0 ||
344 strcmp(*argv, "oif") == 0) {
351 if (strcmp(*argv, "to") == 0) {
354 if ((**argv < '0' || **argv > '9') &&
355 rtnl_rtntype_a2n(&type, *argv) == 0) {
357 req.r.rtm_type = type;
362 duparg2("to", *argv);
364 get_prefix(&dst, *argv, req.r.rtm_family);
365 if (req.r.rtm_family == AF_UNSPEC) {
366 req.r.rtm_family = dst.family;
368 req.r.rtm_dst_len = dst.bitlen;
371 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
377 if (rtnl_open(&rth, 0) < 0) {
387 if ((idx = ll_name_to_index(d)) == 0) {
388 bb_error_msg("Cannot find device \"%s\"", d);
391 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
395 if (mxrta->rta_len > RTA_LENGTH(0)) {
397 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
399 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
402 if (req.r.rtm_family == AF_UNSPEC) {
403 req.r.rtm_family = AF_INET;
406 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
413 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
419 struct sockaddr_nl nladdr;
421 memset(&nladdr, 0, sizeof(nladdr));
422 memset(&req, 0, sizeof(req));
423 nladdr.nl_family = AF_NETLINK;
425 req.nlh.nlmsg_len = sizeof(req);
426 req.nlh.nlmsg_type = RTM_GETROUTE;
427 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
428 req.nlh.nlmsg_pid = 0;
429 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
430 req.rtm.rtm_family = family;
431 req.rtm.rtm_flags |= RTM_F_CLONED;
433 return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
436 static int iproute_flush_cache(void)
438 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
441 int flush_fd = open (ROUTE_FLUSH_PATH, O_WRONLY);
445 fprintf (stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH);
449 len = strlen (buffer);
451 if ((write (flush_fd, (void *)buffer, len)) < len) {
452 fprintf (stderr, "Cannot flush routing cache\n");
459 static void iproute_reset_filter(void)
461 memset(&filter, 0, sizeof(filter));
462 filter.mdst.bitlen = -1;
463 filter.msrc.bitlen = -1;
466 static int iproute_list_or_flush(int argc, char **argv, int flush)
468 int do_ipv6 = preferred_family;
469 struct rtnl_handle rth;
473 iproute_reset_filter();
474 filter.tb = RT_TABLE_MAIN;
476 if (flush && argc <= 0) {
477 fprintf(stderr, "\"ip route flush\" requires arguments.\n");
482 if (matches(*argv, "protocol") == 0) {
485 filter.protocolmask = -1;
486 if (rtnl_rtprot_a2n(&prot, *argv)) {
487 if (strcmp(*argv, "all") != 0) {
488 invarg("invalid \"protocol\"\n", *argv);
491 filter.protocolmask = 0;
493 filter.protocol = prot;
494 } else if (strcmp(*argv, "dev") == 0 ||
495 strcmp(*argv, "oif") == 0) {
498 } else if (strcmp(*argv, "iif") == 0) {
501 } else if (matches(*argv, "from") == 0) {
503 if (matches(*argv, "root") == 0) {
505 get_prefix(&filter.rsrc, *argv, do_ipv6);
506 } else if (matches(*argv, "match") == 0) {
508 get_prefix(&filter.msrc, *argv, do_ipv6);
510 if (matches(*argv, "exact") == 0) {
513 get_prefix(&filter.msrc, *argv, do_ipv6);
514 filter.rsrc = filter.msrc;
517 if (matches(*argv, "to") == 0) {
520 if (matches(*argv, "root") == 0) {
522 get_prefix(&filter.rdst, *argv, do_ipv6);
523 } else if (matches(*argv, "match") == 0) {
525 get_prefix(&filter.mdst, *argv, do_ipv6);
527 if (matches(*argv, "exact") == 0) {
530 get_prefix(&filter.mdst, *argv, do_ipv6);
531 filter.rdst = filter.mdst;
537 if (do_ipv6 == AF_UNSPEC && filter.tb) {
541 if (rtnl_open(&rth, 0) < 0) {
551 if ((idx = ll_name_to_index(id)) == 0) {
552 bb_error_msg("Cannot find device \"%s\"", id);
559 if ((idx = ll_name_to_index(od)) == 0) {
560 bb_error_msg("Cannot find device \"%s\"", od);
569 char flushb[4096-512];
571 if (filter.tb == -1) {
572 if (do_ipv6 != AF_INET6)
573 iproute_flush_cache();
574 if (do_ipv6 == AF_INET)
578 filter.flushb = flushb;
580 filter.flushe = sizeof(flushb);
584 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
585 perror("Cannot send dump request");
589 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
590 bb_error_msg("Flush terminated\n");
593 if (filter.flushed == 0) {
595 if (filter.tb != -1 || do_ipv6 == AF_INET6)
596 fprintf(stderr, "Nothing to flush.\n");
602 if (flush_update() < 0)
607 if (filter.tb != -1) {
608 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
609 bb_perror_msg_and_die("Cannot send dump request");
612 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
613 bb_perror_msg_and_die("Cannot send dump request");
617 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
618 bb_error_msg_and_die("Dump terminated");
625 static int iproute_get(int argc, char **argv)
627 struct rtnl_handle rth;
637 const char *options[] = { "from", "iif", "oif", "dev", "notify", "connected", "to", 0 };
639 memset(&req, 0, sizeof(req));
641 iproute_reset_filter();
643 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
644 req.n.nlmsg_flags = NLM_F_REQUEST;
645 req.n.nlmsg_type = RTM_GETROUTE;
646 req.r.rtm_family = preferred_family;
648 req.r.rtm_protocol = 0;
651 req.r.rtm_src_len = 0;
652 req.r.rtm_dst_len = 0;
656 switch (compare_string_array(options, *argv)) {
662 get_prefix(&addr, *argv, req.r.rtm_family);
663 if (req.r.rtm_family == AF_UNSPEC) {
664 req.r.rtm_family = addr.family;
667 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
669 req.r.rtm_src_len = addr.bitlen;
682 req.r.rtm_flags |= RTM_F_NOTIFY;
684 case 5: /* connected */
692 get_prefix(&addr, *argv, req.r.rtm_family);
693 if (req.r.rtm_family == AF_UNSPEC) {
694 req.r.rtm_family = addr.family;
697 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
699 req.r.rtm_dst_len = addr.bitlen;
705 if (req.r.rtm_dst_len == 0) {
706 bb_error_msg_and_die("need at least destination address");
709 if (rtnl_open(&rth, 0) < 0)
718 if ((idx = ll_name_to_index(idev)) == 0) {
719 bb_error_msg("Cannot find device \"%s\"", idev);
722 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
725 if ((idx = ll_name_to_index(odev)) == 0) {
726 bb_error_msg("Cannot find device \"%s\"", odev);
729 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
733 if (req.r.rtm_family == AF_UNSPEC) {
734 req.r.rtm_family = AF_INET;
737 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
741 if (connected && !from_ok) {
742 struct rtmsg *r = NLMSG_DATA(&req.n);
743 int len = req.n.nlmsg_len;
744 struct rtattr * tb[RTA_MAX+1];
746 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
747 bb_error_msg_and_die("An error :-)");
750 if (req.n.nlmsg_type != RTM_NEWROUTE) {
751 bb_error_msg("Not a route?");
754 len -= NLMSG_LENGTH(sizeof(*r));
756 bb_error_msg("Wrong len %d", len);
760 memset(tb, 0, sizeof(tb));
761 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
763 if (tb[RTA_PREFSRC]) {
764 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
765 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
766 } else if (!tb[RTA_SRC]) {
767 bb_error_msg("Failed to connect the route");
770 if (!odev && tb[RTA_OIF]) {
771 tb[RTA_OIF]->rta_type = 0;
773 if (tb[RTA_GATEWAY]) {
774 tb[RTA_GATEWAY]->rta_type = 0;
776 if (!idev && tb[RTA_IIF]) {
777 tb[RTA_IIF]->rta_type = 0;
779 req.n.nlmsg_flags = NLM_F_REQUEST;
780 req.n.nlmsg_type = RTM_GETROUTE;
782 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
787 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
788 bb_error_msg_and_die("An error :-)");
794 int do_iproute(int argc, char **argv)
796 const char *ip_route_commands[] = { "add", "append", "change", "chg",
797 "delete", "get", "list", "show", "prepend", "replace", "test", "flush", 0 };
798 unsigned short command_num = 6;
799 unsigned int flags = 0;
800 int cmd = RTM_NEWROUTE;
803 command_num = compare_string_array(ip_route_commands, *argv);
805 switch(command_num) {
807 flags = NLM_F_CREATE|NLM_F_EXCL;
810 flags = NLM_F_CREATE|NLM_F_APPEND;
814 flags = NLM_F_REPLACE;
820 return iproute_get(argc-1, argv+1);
823 return iproute_list_or_flush(argc-1, argv+1, 0);
824 case 8: /* prepend */
825 flags = NLM_F_CREATE;
826 case 9: /* replace */
827 flags = NLM_F_CREATE|NLM_F_REPLACE;
831 return iproute_list_or_flush(argc-1, argv+1, 1);
833 bb_error_msg_and_die("Unknown command %s", *argv);
836 return iproute_modify(cmd, flags, argc-1, argv+1);