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>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
24 #include <arpa/inet.h>
25 #include <linux/in_route.h>
29 #include "ip_common.h"
32 #define RTAX_RTTVAR RTAX_HOPS
36 static const char *mx_names[RTAX_MAX+1] = {
38 [RTAX_WINDOW] = "window",
40 [RTAX_RTTVAR] = "rttvar",
41 [RTAX_SSTHRESH] = "ssthresh",
43 [RTAX_ADVMSS] = "advmss",
44 [RTAX_REORDERING]="reordering",
45 [RTAX_HOPLIMIT] = "hoplimit",
46 [RTAX_INITCWND] = "initcwnd",
47 [RTAX_FEATURES] = "features",
48 [RTAX_RTO_MIN] = "rto_min",
49 [RTAX_INITRWND] = "initrwnd",
51 static void usage(void) __attribute__((noreturn));
53 static void usage(void)
55 fprintf(stderr, "Usage: ip route { list | flush } SELECTOR\n");
56 fprintf(stderr, " ip route get ADDRESS [ from ADDRESS iif STRING ]\n");
57 fprintf(stderr, " [ oif STRING ] [ tos TOS ]\n");
58 fprintf(stderr, " ip route { add | del | change | append | replace | monitor } ROUTE\n");
59 fprintf(stderr, "SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ]\n");
60 fprintf(stderr, " [ table TABLE_ID ] [ proto RTPROTO ]\n");
61 fprintf(stderr, " [ type TYPE ] [ scope SCOPE ]\n");
62 fprintf(stderr, "ROUTE := NODE_SPEC [ INFO_SPEC ]\n");
63 fprintf(stderr, "NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ]\n");
64 fprintf(stderr, " [ table TABLE_ID ] [ proto RTPROTO ]\n");
65 fprintf(stderr, " [ scope SCOPE ] [ metric METRIC ]\n");
66 fprintf(stderr, "INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ]...\n");
67 fprintf(stderr, "NH := [ via ADDRESS ] [ dev STRING ] [ weight NUMBER ] NHFLAGS\n");
68 fprintf(stderr, "OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ]\n");
69 fprintf(stderr, " [ rtt TIME ] [ rttvar TIME ] [reordering NUMBER ]\n");
70 fprintf(stderr, " [ window NUMBER] [ cwnd NUMBER ] [ initcwnd NUMBER ]\n");
71 fprintf(stderr, " [ ssthresh NUMBER ] [ realms REALM ] [ src ADDRESS ]\n");
72 fprintf(stderr, " [ rto_min TIME ] [ hoplimit NUMBER ] [ initrwnd NUMBER ]\n");
73 fprintf(stderr, "TYPE := [ unicast | local | broadcast | multicast | throw |\n");
74 fprintf(stderr, " unreachable | prohibit | blackhole | nat ]\n");
75 fprintf(stderr, "TABLE_ID := [ local | main | default | all | NUMBER ]\n");
76 fprintf(stderr, "SCOPE := [ host | link | global | NUMBER ]\n");
77 fprintf(stderr, "MP_ALGO := { rr | drr | random | wrandom }\n");
78 fprintf(stderr, "NHFLAGS := [ onlink | pervasive ]\n");
79 fprintf(stderr, "RTPROTO := [ kernel | boot | static | NUMBER ]\n");
80 fprintf(stderr, "TIME := NUMBER[s|ms|us|ns|j]\n");
93 int protocol, protocolmask;
100 inet_prefix rprefsrc;
108 static int flush_update(void)
110 if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
111 perror("Failed to send flush request");
118 int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
120 FILE *fp = (FILE*)arg;
121 struct rtmsg *r = NLMSG_DATA(n);
122 int len = n->nlmsg_len;
123 struct rtattr * tb[RTA_MAX+1];
130 static int ip6_multiple_tables;
135 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
136 fprintf(stderr, "Not a route: %08x %08x %08x\n",
137 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
140 if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
142 len -= NLMSG_LENGTH(sizeof(*r));
144 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
148 if (r->rtm_family == AF_INET6)
150 else if (r->rtm_family == AF_INET)
152 else if (r->rtm_family == AF_DECnet)
154 else if (r->rtm_family == AF_IPX)
157 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
158 table = rtm_get_table(r, tb);
160 if (r->rtm_family == AF_INET6 && table != RT_TABLE_MAIN)
161 ip6_multiple_tables = 1;
163 if (r->rtm_family == AF_INET6 && !ip6_multiple_tables) {
165 if (!(r->rtm_flags&RTM_F_CLONED))
169 if (!filter.cloned && r->rtm_flags&RTM_F_CLONED)
171 if (filter.tb == RT_TABLE_LOCAL) {
172 if (r->rtm_type != RTN_LOCAL)
174 } else if (filter.tb == RT_TABLE_MAIN) {
175 if (r->rtm_type == RTN_LOCAL)
183 if (!(r->rtm_flags&RTM_F_CLONED))
186 if (filter.tb > 0 && filter.tb != table)
189 if ((filter.protocol^r->rtm_protocol)&filter.protocolmask)
191 if ((filter.scope^r->rtm_scope)&filter.scopemask)
193 if ((filter.type^r->rtm_type)&filter.typemask)
195 if ((filter.tos^r->rtm_tos)&filter.tosmask)
197 if (filter.rdst.family &&
198 (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len))
200 if (filter.mdst.family &&
201 (r->rtm_family != filter.mdst.family ||
202 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len)))
204 if (filter.rsrc.family &&
205 (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len))
207 if (filter.msrc.family &&
208 (r->rtm_family != filter.msrc.family ||
209 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len)))
211 if (filter.rvia.family && r->rtm_family != filter.rvia.family)
213 if (filter.rprefsrc.family && r->rtm_family != filter.rprefsrc.family)
216 memset(&dst, 0, sizeof(dst));
217 dst.family = r->rtm_family;
219 memcpy(&dst.data, RTA_DATA(tb[RTA_DST]), (r->rtm_dst_len+7)/8);
220 if (filter.rsrc.family || filter.msrc.family) {
221 memset(&src, 0, sizeof(src));
222 src.family = r->rtm_family;
224 memcpy(&src.data, RTA_DATA(tb[RTA_SRC]), (r->rtm_src_len+7)/8);
226 if (filter.rvia.bitlen>0) {
227 memset(&via, 0, sizeof(via));
228 via.family = r->rtm_family;
230 memcpy(&via.data, RTA_DATA(tb[RTA_GATEWAY]), host_len/8);
232 if (filter.rprefsrc.bitlen>0) {
233 memset(&prefsrc, 0, sizeof(prefsrc));
234 prefsrc.family = r->rtm_family;
236 memcpy(&prefsrc.data, RTA_DATA(tb[RTA_PREFSRC]), host_len/8);
239 if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
241 if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
242 inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
245 if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
247 if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
248 inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
251 if (filter.rvia.family && inet_addr_match(&via, &filter.rvia, filter.rvia.bitlen))
253 if (filter.rprefsrc.family && inet_addr_match(&prefsrc, &filter.rprefsrc, filter.rprefsrc.bitlen))
255 if (filter.realmmask) {
258 realms = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
259 if ((realms^filter.realm)&filter.realmmask)
262 if (filter.iifmask) {
265 iif = *(int*)RTA_DATA(tb[RTA_IIF]);
266 if ((iif^filter.iif)&filter.iifmask)
269 if (filter.oifmask) {
272 oif = *(int*)RTA_DATA(tb[RTA_OIF]);
273 if ((oif^filter.oif)&filter.oifmask)
277 r->rtm_family == AF_INET6 &&
278 r->rtm_dst_len == 0 &&
279 r->rtm_type == RTN_UNREACHABLE &&
281 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
286 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
290 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
291 memcpy(fn, n, n->nlmsg_len);
292 fn->nlmsg_type = RTM_DELROUTE;
293 fn->nlmsg_flags = NLM_F_REQUEST;
294 fn->nlmsg_seq = ++rth.seq;
295 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
301 if (n->nlmsg_type == RTM_DELROUTE)
302 fprintf(fp, "Deleted ");
303 if (r->rtm_type != RTN_UNICAST && !filter.type)
304 fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
307 if (r->rtm_dst_len != host_len) {
308 fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
309 RTA_PAYLOAD(tb[RTA_DST]),
310 RTA_DATA(tb[RTA_DST]),
315 fprintf(fp, "%s ", format_host(r->rtm_family,
316 RTA_PAYLOAD(tb[RTA_DST]),
317 RTA_DATA(tb[RTA_DST]),
321 } else if (r->rtm_dst_len) {
322 fprintf(fp, "0/%d ", r->rtm_dst_len);
324 fprintf(fp, "default ");
327 if (r->rtm_src_len != host_len) {
328 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
329 RTA_PAYLOAD(tb[RTA_SRC]),
330 RTA_DATA(tb[RTA_SRC]),
335 fprintf(fp, "from %s ", format_host(r->rtm_family,
336 RTA_PAYLOAD(tb[RTA_SRC]),
337 RTA_DATA(tb[RTA_SRC]),
341 } else if (r->rtm_src_len) {
342 fprintf(fp, "from 0/%u ", r->rtm_src_len);
344 if (r->rtm_tos && filter.tosmask != -1) {
346 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
349 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
350 fprintf(fp, "via %s ",
351 format_host(r->rtm_family,
352 RTA_PAYLOAD(tb[RTA_GATEWAY]),
353 RTA_DATA(tb[RTA_GATEWAY]),
354 abuf, sizeof(abuf)));
356 if (tb[RTA_OIF] && filter.oifmask != -1)
357 fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
359 if (!(r->rtm_flags&RTM_F_CLONED)) {
360 if (table != RT_TABLE_MAIN && !filter.tb)
361 fprintf(fp, " table %s ", rtnl_rttable_n2a(table, b1, sizeof(b1)));
362 if (r->rtm_protocol != RTPROT_BOOT && filter.protocolmask != -1)
363 fprintf(fp, " proto %s ", rtnl_rtprot_n2a(r->rtm_protocol, b1, sizeof(b1)));
364 if (r->rtm_scope != RT_SCOPE_UNIVERSE && filter.scopemask != -1)
365 fprintf(fp, " scope %s ", rtnl_rtscope_n2a(r->rtm_scope, b1, sizeof(b1)));
367 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
368 /* Do not use format_host(). It is our local addr
369 and symbolic name will not be useful.
371 fprintf(fp, " src %s ",
372 rt_addr_n2a(r->rtm_family,
373 RTA_PAYLOAD(tb[RTA_PREFSRC]),
374 RTA_DATA(tb[RTA_PREFSRC]),
375 abuf, sizeof(abuf)));
377 if (tb[RTA_PRIORITY])
378 fprintf(fp, " metric %d ", *(__u32*)RTA_DATA(tb[RTA_PRIORITY]));
379 if (r->rtm_flags & RTNH_F_DEAD)
380 fprintf(fp, "dead ");
381 if (r->rtm_flags & RTNH_F_ONLINK)
382 fprintf(fp, "onlink ");
383 if (r->rtm_flags & RTNH_F_PERVASIVE)
384 fprintf(fp, "pervasive ");
385 if (r->rtm_flags & RTM_F_NOTIFY)
386 fprintf(fp, "notify ");
388 if (tb[RTA_FLOW] && filter.realmmask != ~0U) {
389 __u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
392 fprintf(fp, "realm%s ", from ? "s" : "");
395 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
398 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
400 if ((r->rtm_flags&RTM_F_CLONED) && r->rtm_family == AF_INET) {
401 __u32 flags = r->rtm_flags&~0xFFFF;
404 fprintf(fp, "%s cache ", _SL_);
406 #define PRTFL(fl,flname) if (flags&RTCF_##fl) { \
407 flags &= ~RTCF_##fl; \
408 fprintf(fp, "%s" flname "%s", first ? "<" : "", flags ? "," : "> "); \
410 PRTFL(LOCAL, "local");
411 PRTFL(REJECT, "reject");
412 PRTFL(MULTICAST, "mc");
413 PRTFL(BROADCAST, "brd");
414 PRTFL(DNAT, "dst-nat");
415 PRTFL(SNAT, "src-nat");
417 PRTFL(DIRECTDST, "dst-direct");
418 PRTFL(DIRECTSRC, "src-direct");
419 PRTFL(REDIRECTED, "redirected");
420 PRTFL(DOREDIRECT, "redirect");
421 PRTFL(FAST, "fastroute");
422 PRTFL(NOTIFY, "notify");
423 PRTFL(TPROXY, "proxy");
426 fprintf(fp, "%s%x> ", first ? "<" : "", flags);
427 if (tb[RTA_CACHEINFO]) {
428 struct rta_cacheinfo *ci = RTA_DATA(tb[RTA_CACHEINFO]);
431 if (ci->rta_expires != 0)
432 fprintf(fp, " expires %dsec", ci->rta_expires/hz);
433 if (ci->rta_error != 0)
434 fprintf(fp, " error %d", ci->rta_error);
437 fprintf(fp, " users %d", ci->rta_clntref);
438 if (ci->rta_used != 0)
439 fprintf(fp, " used %d", ci->rta_used);
440 if (ci->rta_lastuse != 0)
441 fprintf(fp, " age %dsec", ci->rta_lastuse/hz);
443 #ifdef RTNETLINK_HAVE_PEERINFO
445 fprintf(fp, " ipid 0x%04x", ci->rta_id);
446 if (ci->rta_ts || ci->rta_tsage)
447 fprintf(fp, " ts 0x%x tsage %dsec", ci->rta_ts, ci->rta_tsage);
450 } else if (r->rtm_family == AF_INET6) {
451 struct rta_cacheinfo *ci = NULL;
452 if (tb[RTA_CACHEINFO])
453 ci = RTA_DATA(tb[RTA_CACHEINFO]);
454 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
457 if (r->rtm_flags & RTM_F_CLONED)
458 fprintf(fp, "%s cache ", _SL_);
460 fprintf(fp, " expires %dsec", ci->rta_expires/hz);
461 if (ci->rta_error != 0)
462 fprintf(fp, " error %d", ci->rta_error);
465 fprintf(fp, " users %d", ci->rta_clntref);
466 if (ci->rta_used != 0)
467 fprintf(fp, " used %d", ci->rta_used);
468 if (ci->rta_lastuse != 0)
469 fprintf(fp, " age %dsec", ci->rta_lastuse/hz);
472 if (ci->rta_error != 0)
473 fprintf(fp, " error %d", ci->rta_error);
476 if (tb[RTA_METRICS]) {
479 struct rtattr *mxrta[RTAX_MAX+1];
481 parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
482 RTA_PAYLOAD(tb[RTA_METRICS]));
483 if (mxrta[RTAX_LOCK])
484 mxlock = *(unsigned*)RTA_DATA(mxrta[RTAX_LOCK]);
486 for (i=2; i<= RTAX_MAX; i++) {
489 if (mxrta[i] == NULL)
494 if (i < sizeof(mx_names)/sizeof(char*) && mx_names[i])
495 fprintf(fp, " %s", mx_names[i]);
497 fprintf(fp, " metric %d", i);
499 fprintf(fp, " lock");
501 val = *(unsigned*)RTA_DATA(mxrta[i]);
508 fprintf(fp, " %u", val);
517 else if (i == RTAX_RTTVAR)
521 fprintf(fp, " %llums",
522 (unsigned long long) val / hz);
524 fprintf(fp, " %.2fms",
529 if (tb[RTA_IIF] && filter.iifmask != -1) {
530 fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
532 if (tb[RTA_MULTIPATH]) {
533 struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]);
536 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
539 if (len < sizeof(*nh))
541 if (nh->rtnh_len > len)
543 if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) {
545 fprintf(fp, " Oifs:");
549 fprintf(fp, "%s\tnexthop", _SL_);
550 if (nh->rtnh_len > sizeof(*nh)) {
551 parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh), nh->rtnh_len - sizeof(*nh));
552 if (tb[RTA_GATEWAY]) {
553 fprintf(fp, " via %s ",
554 format_host(r->rtm_family,
555 RTA_PAYLOAD(tb[RTA_GATEWAY]),
556 RTA_DATA(tb[RTA_GATEWAY]),
557 abuf, sizeof(abuf)));
560 __u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
563 fprintf(fp, " realm%s ", from ? "s" : "");
566 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
569 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
572 if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) {
573 fprintf(fp, " %s", ll_index_to_name(nh->rtnh_ifindex));
574 if (nh->rtnh_hops != 1)
575 fprintf(fp, "(ttl>%d)", nh->rtnh_hops);
577 fprintf(fp, " dev %s", ll_index_to_name(nh->rtnh_ifindex));
578 fprintf(fp, " weight %d", nh->rtnh_hops+1);
580 if (nh->rtnh_flags & RTNH_F_DEAD)
581 fprintf(fp, " dead");
582 if (nh->rtnh_flags & RTNH_F_ONLINK)
583 fprintf(fp, " onlink");
584 if (nh->rtnh_flags & RTNH_F_PERVASIVE)
585 fprintf(fp, " pervasive");
586 len -= NLMSG_ALIGN(nh->rtnh_len);
596 int parse_one_nh(struct rtattr *rta, struct rtnexthop *rtnh, int *argcp, char ***argvp)
599 char **argv = *argvp;
601 while (++argv, --argc > 0) {
602 if (strcmp(*argv, "via") == 0) {
604 rta_addattr32(rta, 4096, RTA_GATEWAY, get_addr32(*argv));
605 rtnh->rtnh_len += sizeof(struct rtattr) + 4;
606 } else if (strcmp(*argv, "dev") == 0) {
608 if ((rtnh->rtnh_ifindex = ll_name_to_index(*argv)) == 0) {
609 fprintf(stderr, "Cannot find device \"%s\"\n", *argv);
612 } else if (strcmp(*argv, "weight") == 0) {
615 if (get_unsigned(&w, *argv, 0) || w == 0 || w > 256)
616 invarg("\"weight\" is invalid\n", *argv);
617 rtnh->rtnh_hops = w - 1;
618 } else if (strcmp(*argv, "onlink") == 0) {
619 rtnh->rtnh_flags |= RTNH_F_ONLINK;
620 } else if (matches(*argv, "realms") == 0) {
623 if (get_rt_realms(&realm, *argv))
624 invarg("\"realm\" value is invalid\n", *argv);
625 rta_addattr32(rta, 4096, RTA_FLOW, realm);
626 rtnh->rtnh_len += sizeof(struct rtattr) + 4;
635 int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r, int argc, char **argv)
638 struct rtattr *rta = (void*)buf;
639 struct rtnexthop *rtnh;
641 rta->rta_type = RTA_MULTIPATH;
642 rta->rta_len = RTA_LENGTH(0);
643 rtnh = RTA_DATA(rta);
646 if (strcmp(*argv, "nexthop") != 0) {
647 fprintf(stderr, "Error: \"nexthop\" or end of line is expected instead of \"%s\"\n", *argv);
651 fprintf(stderr, "Error: unexpected end of line after \"nexthop\"\n");
654 memset(rtnh, 0, sizeof(*rtnh));
655 rtnh->rtnh_len = sizeof(*rtnh);
656 rta->rta_len += rtnh->rtnh_len;
657 parse_one_nh(rta, rtnh, &argc, &argv);
658 rtnh = RTNH_NEXT(rtnh);
661 if (rta->rta_len > RTA_LENGTH(0))
662 addattr_l(n, 1024, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta));
667 int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
675 struct rtattr * mxrta = (void*)mxbuf;
687 memset(&req, 0, sizeof(req));
689 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
690 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
691 req.n.nlmsg_type = cmd;
692 req.r.rtm_family = preferred_family;
693 req.r.rtm_table = RT_TABLE_MAIN;
694 req.r.rtm_scope = RT_SCOPE_NOWHERE;
696 if (cmd != RTM_DELROUTE) {
697 req.r.rtm_protocol = RTPROT_BOOT;
698 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
699 req.r.rtm_type = RTN_UNICAST;
702 mxrta->rta_type = RTA_METRICS;
703 mxrta->rta_len = RTA_LENGTH(0);
706 if (strcmp(*argv, "src") == 0) {
709 get_addr(&addr, *argv, req.r.rtm_family);
710 if (req.r.rtm_family == AF_UNSPEC)
711 req.r.rtm_family = addr.family;
712 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
713 } else if (strcmp(*argv, "via") == 0) {
717 get_addr(&addr, *argv, req.r.rtm_family);
718 if (req.r.rtm_family == AF_UNSPEC)
719 req.r.rtm_family = addr.family;
720 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
721 } else if (strcmp(*argv, "from") == 0) {
724 get_prefix(&addr, *argv, req.r.rtm_family);
725 if (req.r.rtm_family == AF_UNSPEC)
726 req.r.rtm_family = addr.family;
728 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
729 req.r.rtm_src_len = addr.bitlen;
730 } else if (strcmp(*argv, "tos") == 0 ||
731 matches(*argv, "dsfield") == 0) {
734 if (rtnl_dsfield_a2n(&tos, *argv))
735 invarg("\"tos\" value is invalid\n", *argv);
737 } else if (matches(*argv, "metric") == 0 ||
738 matches(*argv, "priority") == 0 ||
739 matches(*argv, "preference") == 0) {
742 if (get_u32(&metric, *argv, 0))
743 invarg("\"metric\" value is invalid\n", *argv);
744 addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
745 } else if (strcmp(*argv, "scope") == 0) {
748 if (rtnl_rtscope_a2n(&scope, *argv))
749 invarg("invalid \"scope\" value\n", *argv);
750 req.r.rtm_scope = scope;
752 } else if (strcmp(*argv, "mtu") == 0) {
755 if (strcmp(*argv, "lock") == 0) {
756 mxlock |= (1<<RTAX_MTU);
759 if (get_unsigned(&mtu, *argv, 0))
760 invarg("\"mtu\" value is invalid\n", *argv);
761 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
763 } else if (strcmp(*argv, "hoplimit") == 0) {
766 if (strcmp(*argv, "lock") == 0) {
767 mxlock |= (1<<RTAX_HOPLIMIT);
770 if (get_unsigned(&hoplimit, *argv, 0))
771 invarg("\"hoplimit\" value is invalid\n", *argv);
772 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit);
775 } else if (strcmp(*argv, "advmss") == 0) {
778 if (strcmp(*argv, "lock") == 0) {
779 mxlock |= (1<<RTAX_ADVMSS);
782 if (get_unsigned(&mss, *argv, 0))
783 invarg("\"mss\" value is invalid\n", *argv);
784 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss);
786 #ifdef RTAX_REORDERING
787 } else if (matches(*argv, "reordering") == 0) {
790 if (strcmp(*argv, "lock") == 0) {
791 mxlock |= (1<<RTAX_REORDERING);
794 if (get_unsigned(&reord, *argv, 0))
795 invarg("\"reordering\" value is invalid\n", *argv);
796 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord);
798 } else if (strcmp(*argv, "rtt") == 0) {
801 if (strcmp(*argv, "lock") == 0) {
802 mxlock |= (1<<RTAX_RTT);
805 if (get_jiffies(&rtt, *argv, 0, &raw))
806 invarg("\"rtt\" value is invalid\n", *argv);
807 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT,
808 (raw) ? rtt : rtt * 8);
809 } else if (strcmp(*argv, "rto_min") == 0) {
812 mxlock |= (1<<RTAX_RTO_MIN);
813 if (get_jiffies(&rto_min, *argv, 0, &raw))
814 invarg("\"rto_min\" value is invalid\n",
816 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN,
818 } else if (matches(*argv, "window") == 0) {
821 if (strcmp(*argv, "lock") == 0) {
822 mxlock |= (1<<RTAX_WINDOW);
825 if (get_unsigned(&win, *argv, 0))
826 invarg("\"window\" value is invalid\n", *argv);
827 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win);
828 } else if (matches(*argv, "cwnd") == 0) {
831 if (strcmp(*argv, "lock") == 0) {
832 mxlock |= (1<<RTAX_CWND);
835 if (get_unsigned(&win, *argv, 0))
836 invarg("\"cwnd\" value is invalid\n", *argv);
837 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win);
838 } else if (matches(*argv, "initcwnd") == 0) {
841 if (strcmp(*argv, "lock") == 0) {
842 mxlock |= (1<<RTAX_INITCWND);
845 if (get_unsigned(&win, *argv, 0))
846 invarg("\"initcwnd\" value is invalid\n", *argv);
847 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITCWND, win);
848 } else if (matches(*argv, "initrwnd") == 0) {
851 if (strcmp(*argv, "lock") == 0) {
852 mxlock |= (1<<RTAX_INITRWND);
855 if (get_unsigned(&win, *argv, 0))
856 invarg("\"initrwnd\" value is invalid\n", *argv);
857 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITRWND, win);
858 } else if (matches(*argv, "rttvar") == 0) {
861 if (strcmp(*argv, "lock") == 0) {
862 mxlock |= (1<<RTAX_RTTVAR);
865 if (get_jiffies(&win, *argv, 0, &raw))
866 invarg("\"rttvar\" value is invalid\n", *argv);
867 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR,
868 (raw) ? win : win * 4);
869 } else if (matches(*argv, "ssthresh") == 0) {
872 if (strcmp(*argv, "lock") == 0) {
873 mxlock |= (1<<RTAX_SSTHRESH);
876 if (get_unsigned(&win, *argv, 0))
877 invarg("\"ssthresh\" value is invalid\n", *argv);
878 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win);
879 } else if (matches(*argv, "realms") == 0) {
882 if (get_rt_realms(&realm, *argv))
883 invarg("\"realm\" value is invalid\n", *argv);
884 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
885 } else if (strcmp(*argv, "onlink") == 0) {
886 req.r.rtm_flags |= RTNH_F_ONLINK;
887 } else if (strcmp(*argv, "nexthop") == 0) {
890 } else if (matches(*argv, "protocol") == 0) {
893 if (rtnl_rtprot_a2n(&prot, *argv))
894 invarg("\"protocol\" value is invalid\n", *argv);
895 req.r.rtm_protocol = prot;
897 } else if (matches(*argv, "table") == 0) {
900 if (rtnl_rttable_a2n(&tid, *argv))
901 invarg("\"table\" value is invalid\n", *argv);
903 req.r.rtm_table = tid;
905 req.r.rtm_table = RT_TABLE_UNSPEC;
906 addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
909 } else if (strcmp(*argv, "dev") == 0 ||
910 strcmp(*argv, "oif") == 0) {
917 if (strcmp(*argv, "to") == 0) {
920 if ((**argv < '0' || **argv > '9') &&
921 rtnl_rtntype_a2n(&type, *argv) == 0) {
923 req.r.rtm_type = type;
927 if (matches(*argv, "help") == 0)
930 duparg2("to", *argv);
931 get_prefix(&dst, *argv, req.r.rtm_family);
932 if (req.r.rtm_family == AF_UNSPEC)
933 req.r.rtm_family = dst.family;
934 req.r.rtm_dst_len = dst.bitlen;
937 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
948 if ((idx = ll_name_to_index(d)) == 0) {
949 fprintf(stderr, "Cannot find device \"%s\"\n", d);
952 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
956 if (mxrta->rta_len > RTA_LENGTH(0)) {
958 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
959 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
963 parse_nexthops(&req.n, &req.r, argc, argv);
966 if (req.r.rtm_type == RTN_LOCAL ||
967 req.r.rtm_type == RTN_BROADCAST ||
968 req.r.rtm_type == RTN_NAT ||
969 req.r.rtm_type == RTN_ANYCAST)
970 req.r.rtm_table = RT_TABLE_LOCAL;
973 if (req.r.rtm_type == RTN_LOCAL ||
974 req.r.rtm_type == RTN_NAT)
975 req.r.rtm_scope = RT_SCOPE_HOST;
976 else if (req.r.rtm_type == RTN_BROADCAST ||
977 req.r.rtm_type == RTN_MULTICAST ||
978 req.r.rtm_type == RTN_ANYCAST)
979 req.r.rtm_scope = RT_SCOPE_LINK;
980 else if (req.r.rtm_type == RTN_UNICAST ||
981 req.r.rtm_type == RTN_UNSPEC) {
982 if (cmd == RTM_DELROUTE)
983 req.r.rtm_scope = RT_SCOPE_NOWHERE;
984 else if (!gw_ok && !nhs_ok)
985 req.r.rtm_scope = RT_SCOPE_LINK;
989 if (req.r.rtm_family == AF_UNSPEC)
990 req.r.rtm_family = AF_INET;
992 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
998 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
1001 struct nlmsghdr nlh;
1004 struct sockaddr_nl nladdr;
1006 memset(&nladdr, 0, sizeof(nladdr));
1007 memset(&req, 0, sizeof(req));
1008 nladdr.nl_family = AF_NETLINK;
1010 req.nlh.nlmsg_len = sizeof(req);
1011 req.nlh.nlmsg_type = RTM_GETROUTE;
1012 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
1013 req.nlh.nlmsg_pid = 0;
1014 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
1015 req.rtm.rtm_family = family;
1016 req.rtm.rtm_flags |= RTM_F_CLONED;
1018 return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
1021 static int iproute_flush_cache(void)
1023 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
1026 int flush_fd = open (ROUTE_FLUSH_PATH, O_WRONLY);
1027 char *buffer = "-1";
1030 fprintf (stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH);
1034 len = strlen (buffer);
1036 if ((write (flush_fd, (void *)buffer, len)) < len) {
1037 fprintf (stderr, "Cannot flush routing cache\n");
1045 static int iproute_list_or_flush(int argc, char **argv, int flush)
1047 int do_ipv6 = preferred_family;
1051 iproute_reset_filter();
1052 filter.tb = RT_TABLE_MAIN;
1054 if (flush && argc <= 0) {
1055 fprintf(stderr, "\"ip route flush\" requires arguments.\n");
1060 if (matches(*argv, "table") == 0) {
1063 if (rtnl_rttable_a2n(&tid, *argv)) {
1064 if (strcmp(*argv, "all") == 0) {
1066 } else if (strcmp(*argv, "cache") == 0) {
1068 } else if (strcmp(*argv, "help") == 0) {
1071 invarg("table id value is invalid\n", *argv);
1075 } else if (matches(*argv, "cached") == 0 ||
1076 matches(*argv, "cloned") == 0) {
1078 } else if (strcmp(*argv, "tos") == 0 ||
1079 matches(*argv, "dsfield") == 0) {
1082 if (rtnl_dsfield_a2n(&tos, *argv))
1083 invarg("TOS value is invalid\n", *argv);
1085 filter.tosmask = -1;
1086 } else if (matches(*argv, "protocol") == 0) {
1089 filter.protocolmask = -1;
1090 if (rtnl_rtprot_a2n(&prot, *argv)) {
1091 if (strcmp(*argv, "all") != 0)
1092 invarg("invalid \"protocol\"\n", *argv);
1094 filter.protocolmask = 0;
1096 filter.protocol = prot;
1097 } else if (matches(*argv, "scope") == 0) {
1100 filter.scopemask = -1;
1101 if (rtnl_rtscope_a2n(&scope, *argv)) {
1102 if (strcmp(*argv, "all") != 0)
1103 invarg("invalid \"scope\"\n", *argv);
1104 scope = RT_SCOPE_NOWHERE;
1105 filter.scopemask = 0;
1107 filter.scope = scope;
1108 } else if (matches(*argv, "type") == 0) {
1111 filter.typemask = -1;
1112 if (rtnl_rtntype_a2n(&type, *argv))
1113 invarg("node type value is invalid\n", *argv);
1115 } else if (strcmp(*argv, "dev") == 0 ||
1116 strcmp(*argv, "oif") == 0) {
1119 } else if (strcmp(*argv, "iif") == 0) {
1122 } else if (strcmp(*argv, "via") == 0) {
1124 get_prefix(&filter.rvia, *argv, do_ipv6);
1125 } else if (strcmp(*argv, "src") == 0) {
1127 get_prefix(&filter.rprefsrc, *argv, do_ipv6);
1128 } else if (matches(*argv, "realms") == 0) {
1131 if (get_rt_realms(&realm, *argv))
1132 invarg("invalid realms\n", *argv);
1133 filter.realm = realm;
1134 filter.realmmask = ~0U;
1135 if ((filter.realm&0xFFFF) == 0 &&
1136 (*argv)[strlen(*argv) - 1] == '/')
1137 filter.realmmask &= ~0xFFFF;
1138 if ((filter.realm&0xFFFF0000U) == 0 &&
1139 (strchr(*argv, '/') == NULL ||
1141 filter.realmmask &= ~0xFFFF0000U;
1142 } else if (matches(*argv, "from") == 0) {
1144 if (matches(*argv, "root") == 0) {
1146 get_prefix(&filter.rsrc, *argv, do_ipv6);
1147 } else if (matches(*argv, "match") == 0) {
1149 get_prefix(&filter.msrc, *argv, do_ipv6);
1151 if (matches(*argv, "exact") == 0) {
1154 get_prefix(&filter.msrc, *argv, do_ipv6);
1155 filter.rsrc = filter.msrc;
1158 if (matches(*argv, "to") == 0) {
1161 if (matches(*argv, "root") == 0) {
1163 get_prefix(&filter.rdst, *argv, do_ipv6);
1164 } else if (matches(*argv, "match") == 0) {
1166 get_prefix(&filter.mdst, *argv, do_ipv6);
1168 if (matches(*argv, "exact") == 0) {
1171 get_prefix(&filter.mdst, *argv, do_ipv6);
1172 filter.rdst = filter.mdst;
1178 if (do_ipv6 == AF_UNSPEC && filter.tb)
1187 if ((idx = ll_name_to_index(id)) == 0) {
1188 fprintf(stderr, "Cannot find device \"%s\"\n", id);
1192 filter.iifmask = -1;
1195 if ((idx = ll_name_to_index(od)) == 0) {
1196 fprintf(stderr, "Cannot find device \"%s\"\n", od);
1200 filter.oifmask = -1;
1206 char flushb[4096-512];
1207 time_t start = time(0);
1209 if (filter.cloned) {
1210 if (do_ipv6 != AF_INET6) {
1211 iproute_flush_cache();
1213 printf("*** IPv4 routing cache is flushed.\n");
1215 if (do_ipv6 == AF_INET)
1219 filter.flushb = flushb;
1221 filter.flushe = sizeof(flushb);
1224 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
1225 perror("Cannot send dump request");
1229 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
1230 fprintf(stderr, "Flush terminated\n");
1233 if (filter.flushed == 0) {
1235 if (round == 0 && (!filter.cloned || do_ipv6 == AF_INET6))
1236 printf("Nothing to flush.\n");
1238 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
1244 if (flush_update() < 0)
1247 if (time(0) - start > 30) {
1248 printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
1249 time(0) - start, filter.flushed);
1254 printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed);
1260 if (!filter.cloned) {
1261 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
1262 perror("Cannot send dump request");
1266 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
1267 perror("Cannot send dump request");
1272 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
1273 fprintf(stderr, "Dump terminated\n");
1281 int iproute_get(int argc, char **argv)
1293 memset(&req, 0, sizeof(req));
1295 iproute_reset_filter();
1297 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1298 req.n.nlmsg_flags = NLM_F_REQUEST;
1299 req.n.nlmsg_type = RTM_GETROUTE;
1300 req.r.rtm_family = preferred_family;
1301 req.r.rtm_table = 0;
1302 req.r.rtm_protocol = 0;
1303 req.r.rtm_scope = 0;
1305 req.r.rtm_src_len = 0;
1306 req.r.rtm_dst_len = 0;
1310 if (strcmp(*argv, "tos") == 0 ||
1311 matches(*argv, "dsfield") == 0) {
1314 if (rtnl_dsfield_a2n(&tos, *argv))
1315 invarg("TOS value is invalid\n", *argv);
1316 req.r.rtm_tos = tos;
1317 } else if (matches(*argv, "from") == 0) {
1320 if (matches(*argv, "help") == 0)
1323 get_prefix(&addr, *argv, req.r.rtm_family);
1324 if (req.r.rtm_family == AF_UNSPEC)
1325 req.r.rtm_family = addr.family;
1327 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
1328 req.r.rtm_src_len = addr.bitlen;
1329 } else if (matches(*argv, "iif") == 0) {
1332 } else if (matches(*argv, "oif") == 0 ||
1333 strcmp(*argv, "dev") == 0) {
1336 } else if (matches(*argv, "notify") == 0) {
1337 req.r.rtm_flags |= RTM_F_NOTIFY;
1338 } else if (matches(*argv, "connected") == 0) {
1342 if (strcmp(*argv, "to") == 0) {
1345 if (matches(*argv, "help") == 0)
1347 get_prefix(&addr, *argv, req.r.rtm_family);
1348 if (req.r.rtm_family == AF_UNSPEC)
1349 req.r.rtm_family = addr.family;
1351 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
1352 req.r.rtm_dst_len = addr.bitlen;
1357 if (req.r.rtm_dst_len == 0) {
1358 fprintf(stderr, "need at least destination address\n");
1368 if ((idx = ll_name_to_index(idev)) == 0) {
1369 fprintf(stderr, "Cannot find device \"%s\"\n", idev);
1372 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
1375 if ((idx = ll_name_to_index(odev)) == 0) {
1376 fprintf(stderr, "Cannot find device \"%s\"\n", odev);
1379 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
1383 if (req.r.rtm_family == AF_UNSPEC)
1384 req.r.rtm_family = AF_INET;
1386 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
1389 if (connected && !from_ok) {
1390 struct rtmsg *r = NLMSG_DATA(&req.n);
1391 int len = req.n.nlmsg_len;
1392 struct rtattr * tb[RTA_MAX+1];
1394 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
1395 fprintf(stderr, "An error :-)\n");
1399 if (req.n.nlmsg_type != RTM_NEWROUTE) {
1400 fprintf(stderr, "Not a route?\n");
1403 len -= NLMSG_LENGTH(sizeof(*r));
1405 fprintf(stderr, "Wrong len %d\n", len);
1409 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
1411 if (tb[RTA_PREFSRC]) {
1412 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
1413 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
1414 } else if (!tb[RTA_SRC]) {
1415 fprintf(stderr, "Failed to connect the route\n");
1418 if (!odev && tb[RTA_OIF])
1419 tb[RTA_OIF]->rta_type = 0;
1420 if (tb[RTA_GATEWAY])
1421 tb[RTA_GATEWAY]->rta_type = 0;
1422 if (!idev && tb[RTA_IIF])
1423 tb[RTA_IIF]->rta_type = 0;
1424 req.n.nlmsg_flags = NLM_F_REQUEST;
1425 req.n.nlmsg_type = RTM_GETROUTE;
1427 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
1431 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
1432 fprintf(stderr, "An error :-)\n");
1439 void iproute_reset_filter()
1441 memset(&filter, 0, sizeof(filter));
1442 filter.mdst.bitlen = -1;
1443 filter.msrc.bitlen = -1;
1446 int do_iproute(int argc, char **argv)
1449 return iproute_list_or_flush(0, NULL, 0);
1451 if (matches(*argv, "add") == 0)
1452 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL,
1454 if (matches(*argv, "change") == 0 || strcmp(*argv, "chg") == 0)
1455 return iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE,
1457 if (matches(*argv, "replace") == 0)
1458 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
1460 if (matches(*argv, "prepend") == 0)
1461 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE,
1463 if (matches(*argv, "append") == 0)
1464 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND,
1466 if (matches(*argv, "test") == 0)
1467 return iproute_modify(RTM_NEWROUTE, NLM_F_EXCL,
1469 if (matches(*argv, "delete") == 0)
1470 return iproute_modify(RTM_DELROUTE, 0,
1472 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1473 || matches(*argv, "lst") == 0)
1474 return iproute_list_or_flush(argc-1, argv+1, 0);
1475 if (matches(*argv, "get") == 0)
1476 return iproute_get(argc-1, argv+1);
1477 if (matches(*argv, "flush") == 0)
1478 return iproute_list_or_flush(argc-1, argv+1, 1);
1479 if (matches(*argv, "help") == 0)
1481 fprintf(stderr, "Command \"%s\" is unknown, try \"ip route help\".\n", *argv);