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 (filter.cloned == !(r->rtm_flags&RTM_F_CLONED))
166 if (r->rtm_family == AF_INET6 && !ip6_multiple_tables) {
168 if (filter.tb == RT_TABLE_LOCAL) {
169 if (r->rtm_type != RTN_LOCAL)
171 } else if (filter.tb == RT_TABLE_MAIN) {
172 if (r->rtm_type == RTN_LOCAL)
179 if (filter.tb > 0 && filter.tb != table)
182 if ((filter.protocol^r->rtm_protocol)&filter.protocolmask)
184 if ((filter.scope^r->rtm_scope)&filter.scopemask)
186 if ((filter.type^r->rtm_type)&filter.typemask)
188 if ((filter.tos^r->rtm_tos)&filter.tosmask)
190 if (filter.rdst.family &&
191 (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len))
193 if (filter.mdst.family &&
194 (r->rtm_family != filter.mdst.family ||
195 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len)))
197 if (filter.rsrc.family &&
198 (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len))
200 if (filter.msrc.family &&
201 (r->rtm_family != filter.msrc.family ||
202 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len)))
204 if (filter.rvia.family && r->rtm_family != filter.rvia.family)
206 if (filter.rprefsrc.family && r->rtm_family != filter.rprefsrc.family)
209 memset(&dst, 0, sizeof(dst));
210 dst.family = r->rtm_family;
212 memcpy(&dst.data, RTA_DATA(tb[RTA_DST]), (r->rtm_dst_len+7)/8);
213 if (filter.rsrc.family || filter.msrc.family) {
214 memset(&src, 0, sizeof(src));
215 src.family = r->rtm_family;
217 memcpy(&src.data, RTA_DATA(tb[RTA_SRC]), (r->rtm_src_len+7)/8);
219 if (filter.rvia.bitlen>0) {
220 memset(&via, 0, sizeof(via));
221 via.family = r->rtm_family;
223 memcpy(&via.data, RTA_DATA(tb[RTA_GATEWAY]), host_len/8);
225 if (filter.rprefsrc.bitlen>0) {
226 memset(&prefsrc, 0, sizeof(prefsrc));
227 prefsrc.family = r->rtm_family;
229 memcpy(&prefsrc.data, RTA_DATA(tb[RTA_PREFSRC]), host_len/8);
232 if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
234 if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
235 inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
238 if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
240 if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
241 inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
244 if (filter.rvia.family && inet_addr_match(&via, &filter.rvia, filter.rvia.bitlen))
246 if (filter.rprefsrc.family && inet_addr_match(&prefsrc, &filter.rprefsrc, filter.rprefsrc.bitlen))
248 if (filter.realmmask) {
251 realms = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
252 if ((realms^filter.realm)&filter.realmmask)
255 if (filter.iifmask) {
258 iif = *(int*)RTA_DATA(tb[RTA_IIF]);
259 if ((iif^filter.iif)&filter.iifmask)
262 if (filter.oifmask) {
265 oif = *(int*)RTA_DATA(tb[RTA_OIF]);
266 if ((oif^filter.oif)&filter.oifmask)
270 r->rtm_family == AF_INET6 &&
271 r->rtm_dst_len == 0 &&
272 r->rtm_type == RTN_UNREACHABLE &&
274 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
279 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
283 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
284 memcpy(fn, n, n->nlmsg_len);
285 fn->nlmsg_type = RTM_DELROUTE;
286 fn->nlmsg_flags = NLM_F_REQUEST;
287 fn->nlmsg_seq = ++rth.seq;
288 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
294 if (n->nlmsg_type == RTM_DELROUTE)
295 fprintf(fp, "Deleted ");
296 if (r->rtm_type != RTN_UNICAST && !filter.type)
297 fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
300 if (r->rtm_dst_len != host_len) {
301 fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
302 RTA_PAYLOAD(tb[RTA_DST]),
303 RTA_DATA(tb[RTA_DST]),
308 fprintf(fp, "%s ", format_host(r->rtm_family,
309 RTA_PAYLOAD(tb[RTA_DST]),
310 RTA_DATA(tb[RTA_DST]),
314 } else if (r->rtm_dst_len) {
315 fprintf(fp, "0/%d ", r->rtm_dst_len);
317 fprintf(fp, "default ");
320 if (r->rtm_src_len != host_len) {
321 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
322 RTA_PAYLOAD(tb[RTA_SRC]),
323 RTA_DATA(tb[RTA_SRC]),
328 fprintf(fp, "from %s ", format_host(r->rtm_family,
329 RTA_PAYLOAD(tb[RTA_SRC]),
330 RTA_DATA(tb[RTA_SRC]),
334 } else if (r->rtm_src_len) {
335 fprintf(fp, "from 0/%u ", r->rtm_src_len);
337 if (r->rtm_tos && filter.tosmask != -1) {
339 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
342 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
343 fprintf(fp, "via %s ",
344 format_host(r->rtm_family,
345 RTA_PAYLOAD(tb[RTA_GATEWAY]),
346 RTA_DATA(tb[RTA_GATEWAY]),
347 abuf, sizeof(abuf)));
349 if (tb[RTA_OIF] && filter.oifmask != -1)
350 fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
352 if (!(r->rtm_flags&RTM_F_CLONED)) {
353 if (table != RT_TABLE_MAIN && !filter.tb)
354 fprintf(fp, " table %s ", rtnl_rttable_n2a(table, b1, sizeof(b1)));
355 if (r->rtm_protocol != RTPROT_BOOT && filter.protocolmask != -1)
356 fprintf(fp, " proto %s ", rtnl_rtprot_n2a(r->rtm_protocol, b1, sizeof(b1)));
357 if (r->rtm_scope != RT_SCOPE_UNIVERSE && filter.scopemask != -1)
358 fprintf(fp, " scope %s ", rtnl_rtscope_n2a(r->rtm_scope, b1, sizeof(b1)));
360 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
361 /* Do not use format_host(). It is our local addr
362 and symbolic name will not be useful.
364 fprintf(fp, " src %s ",
365 rt_addr_n2a(r->rtm_family,
366 RTA_PAYLOAD(tb[RTA_PREFSRC]),
367 RTA_DATA(tb[RTA_PREFSRC]),
368 abuf, sizeof(abuf)));
370 if (tb[RTA_PRIORITY])
371 fprintf(fp, " metric %d ", *(__u32*)RTA_DATA(tb[RTA_PRIORITY]));
372 if (r->rtm_flags & RTNH_F_DEAD)
373 fprintf(fp, "dead ");
374 if (r->rtm_flags & RTNH_F_ONLINK)
375 fprintf(fp, "onlink ");
376 if (r->rtm_flags & RTNH_F_PERVASIVE)
377 fprintf(fp, "pervasive ");
378 if (r->rtm_flags & RTM_F_NOTIFY)
379 fprintf(fp, "notify ");
381 if (tb[RTA_FLOW] && filter.realmmask != ~0U) {
382 __u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
385 fprintf(fp, "realm%s ", from ? "s" : "");
388 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
391 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
393 if ((r->rtm_flags&RTM_F_CLONED) && r->rtm_family == AF_INET) {
394 __u32 flags = r->rtm_flags&~0xFFFF;
397 fprintf(fp, "%s cache ", _SL_);
399 #define PRTFL(fl,flname) if (flags&RTCF_##fl) { \
400 flags &= ~RTCF_##fl; \
401 fprintf(fp, "%s" flname "%s", first ? "<" : "", flags ? "," : "> "); \
403 PRTFL(LOCAL, "local");
404 PRTFL(REJECT, "reject");
405 PRTFL(MULTICAST, "mc");
406 PRTFL(BROADCAST, "brd");
407 PRTFL(DNAT, "dst-nat");
408 PRTFL(SNAT, "src-nat");
410 PRTFL(DIRECTDST, "dst-direct");
411 PRTFL(DIRECTSRC, "src-direct");
412 PRTFL(REDIRECTED, "redirected");
413 PRTFL(DOREDIRECT, "redirect");
414 PRTFL(FAST, "fastroute");
415 PRTFL(NOTIFY, "notify");
416 PRTFL(TPROXY, "proxy");
419 fprintf(fp, "%s%x> ", first ? "<" : "", flags);
420 if (tb[RTA_CACHEINFO]) {
421 struct rta_cacheinfo *ci = RTA_DATA(tb[RTA_CACHEINFO]);
424 if (ci->rta_expires != 0)
425 fprintf(fp, " expires %dsec", ci->rta_expires/hz);
426 if (ci->rta_error != 0)
427 fprintf(fp, " error %d", ci->rta_error);
430 fprintf(fp, " users %d", ci->rta_clntref);
431 if (ci->rta_used != 0)
432 fprintf(fp, " used %d", ci->rta_used);
433 if (ci->rta_lastuse != 0)
434 fprintf(fp, " age %dsec", ci->rta_lastuse/hz);
436 #ifdef RTNETLINK_HAVE_PEERINFO
438 fprintf(fp, " ipid 0x%04x", ci->rta_id);
439 if (ci->rta_ts || ci->rta_tsage)
440 fprintf(fp, " ts 0x%x tsage %dsec", ci->rta_ts, ci->rta_tsage);
443 } else if (r->rtm_family == AF_INET6) {
444 struct rta_cacheinfo *ci = NULL;
445 if (tb[RTA_CACHEINFO])
446 ci = RTA_DATA(tb[RTA_CACHEINFO]);
447 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
450 if (r->rtm_flags & RTM_F_CLONED)
451 fprintf(fp, "%s cache ", _SL_);
453 fprintf(fp, " expires %dsec", ci->rta_expires/hz);
454 if (ci->rta_error != 0)
455 fprintf(fp, " error %d", ci->rta_error);
458 fprintf(fp, " users %d", ci->rta_clntref);
459 if (ci->rta_used != 0)
460 fprintf(fp, " used %d", ci->rta_used);
461 if (ci->rta_lastuse != 0)
462 fprintf(fp, " age %dsec", ci->rta_lastuse/hz);
465 if (ci->rta_error != 0)
466 fprintf(fp, " error %d", ci->rta_error);
469 if (tb[RTA_METRICS]) {
472 struct rtattr *mxrta[RTAX_MAX+1];
474 parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
475 RTA_PAYLOAD(tb[RTA_METRICS]));
476 if (mxrta[RTAX_LOCK])
477 mxlock = *(unsigned*)RTA_DATA(mxrta[RTAX_LOCK]);
479 for (i=2; i<= RTAX_MAX; i++) {
482 if (mxrta[i] == NULL)
487 if (i < sizeof(mx_names)/sizeof(char*) && mx_names[i])
488 fprintf(fp, " %s", mx_names[i]);
490 fprintf(fp, " metric %d", i);
492 fprintf(fp, " lock");
494 val = *(unsigned*)RTA_DATA(mxrta[i]);
501 fprintf(fp, " %u", val);
510 else if (i == RTAX_RTTVAR)
514 fprintf(fp, " %llums",
515 (unsigned long long) val / hz);
517 fprintf(fp, " %.2fms",
522 if (tb[RTA_IIF] && filter.iifmask != -1) {
523 fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
525 if (tb[RTA_MULTIPATH]) {
526 struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]);
529 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
532 if (len < sizeof(*nh))
534 if (nh->rtnh_len > len)
536 if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) {
538 fprintf(fp, " Oifs:");
542 fprintf(fp, "%s\tnexthop", _SL_);
543 if (nh->rtnh_len > sizeof(*nh)) {
544 parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh), nh->rtnh_len - sizeof(*nh));
545 if (tb[RTA_GATEWAY]) {
546 fprintf(fp, " via %s ",
547 format_host(r->rtm_family,
548 RTA_PAYLOAD(tb[RTA_GATEWAY]),
549 RTA_DATA(tb[RTA_GATEWAY]),
550 abuf, sizeof(abuf)));
553 __u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
556 fprintf(fp, " realm%s ", from ? "s" : "");
559 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
562 rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
565 if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) {
566 fprintf(fp, " %s", ll_index_to_name(nh->rtnh_ifindex));
567 if (nh->rtnh_hops != 1)
568 fprintf(fp, "(ttl>%d)", nh->rtnh_hops);
570 fprintf(fp, " dev %s", ll_index_to_name(nh->rtnh_ifindex));
571 fprintf(fp, " weight %d", nh->rtnh_hops+1);
573 if (nh->rtnh_flags & RTNH_F_DEAD)
574 fprintf(fp, " dead");
575 if (nh->rtnh_flags & RTNH_F_ONLINK)
576 fprintf(fp, " onlink");
577 if (nh->rtnh_flags & RTNH_F_PERVASIVE)
578 fprintf(fp, " pervasive");
579 len -= NLMSG_ALIGN(nh->rtnh_len);
589 int parse_one_nh(struct rtattr *rta, struct rtnexthop *rtnh, int *argcp, char ***argvp)
592 char **argv = *argvp;
594 while (++argv, --argc > 0) {
595 if (strcmp(*argv, "via") == 0) {
597 rta_addattr32(rta, 4096, RTA_GATEWAY, get_addr32(*argv));
598 rtnh->rtnh_len += sizeof(struct rtattr) + 4;
599 } else if (strcmp(*argv, "dev") == 0) {
601 if ((rtnh->rtnh_ifindex = ll_name_to_index(*argv)) == 0) {
602 fprintf(stderr, "Cannot find device \"%s\"\n", *argv);
605 } else if (strcmp(*argv, "weight") == 0) {
608 if (get_unsigned(&w, *argv, 0) || w == 0 || w > 256)
609 invarg("\"weight\" is invalid\n", *argv);
610 rtnh->rtnh_hops = w - 1;
611 } else if (strcmp(*argv, "onlink") == 0) {
612 rtnh->rtnh_flags |= RTNH_F_ONLINK;
613 } else if (matches(*argv, "realms") == 0) {
616 if (get_rt_realms(&realm, *argv))
617 invarg("\"realm\" value is invalid\n", *argv);
618 rta_addattr32(rta, 4096, RTA_FLOW, realm);
619 rtnh->rtnh_len += sizeof(struct rtattr) + 4;
628 int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r, int argc, char **argv)
631 struct rtattr *rta = (void*)buf;
632 struct rtnexthop *rtnh;
634 rta->rta_type = RTA_MULTIPATH;
635 rta->rta_len = RTA_LENGTH(0);
636 rtnh = RTA_DATA(rta);
639 if (strcmp(*argv, "nexthop") != 0) {
640 fprintf(stderr, "Error: \"nexthop\" or end of line is expected instead of \"%s\"\n", *argv);
644 fprintf(stderr, "Error: unexpected end of line after \"nexthop\"\n");
647 memset(rtnh, 0, sizeof(*rtnh));
648 rtnh->rtnh_len = sizeof(*rtnh);
649 rta->rta_len += rtnh->rtnh_len;
650 parse_one_nh(rta, rtnh, &argc, &argv);
651 rtnh = RTNH_NEXT(rtnh);
654 if (rta->rta_len > RTA_LENGTH(0))
655 addattr_l(n, 1024, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta));
660 int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
668 struct rtattr * mxrta = (void*)mxbuf;
680 memset(&req, 0, sizeof(req));
682 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
683 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
684 req.n.nlmsg_type = cmd;
685 req.r.rtm_family = preferred_family;
686 req.r.rtm_table = RT_TABLE_MAIN;
687 req.r.rtm_scope = RT_SCOPE_NOWHERE;
689 if (cmd != RTM_DELROUTE) {
690 req.r.rtm_protocol = RTPROT_BOOT;
691 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
692 req.r.rtm_type = RTN_UNICAST;
695 mxrta->rta_type = RTA_METRICS;
696 mxrta->rta_len = RTA_LENGTH(0);
699 if (strcmp(*argv, "src") == 0) {
702 get_addr(&addr, *argv, req.r.rtm_family);
703 if (req.r.rtm_family == AF_UNSPEC)
704 req.r.rtm_family = addr.family;
705 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
706 } else if (strcmp(*argv, "via") == 0) {
710 get_addr(&addr, *argv, req.r.rtm_family);
711 if (req.r.rtm_family == AF_UNSPEC)
712 req.r.rtm_family = addr.family;
713 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
714 } else if (strcmp(*argv, "from") == 0) {
717 get_prefix(&addr, *argv, req.r.rtm_family);
718 if (req.r.rtm_family == AF_UNSPEC)
719 req.r.rtm_family = addr.family;
721 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
722 req.r.rtm_src_len = addr.bitlen;
723 } else if (strcmp(*argv, "tos") == 0 ||
724 matches(*argv, "dsfield") == 0) {
727 if (rtnl_dsfield_a2n(&tos, *argv))
728 invarg("\"tos\" value is invalid\n", *argv);
730 } else if (matches(*argv, "metric") == 0 ||
731 matches(*argv, "priority") == 0 ||
732 matches(*argv, "preference") == 0) {
735 if (get_u32(&metric, *argv, 0))
736 invarg("\"metric\" value is invalid\n", *argv);
737 addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
738 } else if (strcmp(*argv, "scope") == 0) {
741 if (rtnl_rtscope_a2n(&scope, *argv))
742 invarg("invalid \"scope\" value\n", *argv);
743 req.r.rtm_scope = scope;
745 } else if (strcmp(*argv, "mtu") == 0) {
748 if (strcmp(*argv, "lock") == 0) {
749 mxlock |= (1<<RTAX_MTU);
752 if (get_unsigned(&mtu, *argv, 0))
753 invarg("\"mtu\" value is invalid\n", *argv);
754 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
756 } else if (strcmp(*argv, "hoplimit") == 0) {
759 if (strcmp(*argv, "lock") == 0) {
760 mxlock |= (1<<RTAX_HOPLIMIT);
763 if (get_unsigned(&hoplimit, *argv, 0))
764 invarg("\"hoplimit\" value is invalid\n", *argv);
765 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit);
768 } else if (strcmp(*argv, "advmss") == 0) {
771 if (strcmp(*argv, "lock") == 0) {
772 mxlock |= (1<<RTAX_ADVMSS);
775 if (get_unsigned(&mss, *argv, 0))
776 invarg("\"mss\" value is invalid\n", *argv);
777 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss);
779 #ifdef RTAX_REORDERING
780 } else if (matches(*argv, "reordering") == 0) {
783 if (strcmp(*argv, "lock") == 0) {
784 mxlock |= (1<<RTAX_REORDERING);
787 if (get_unsigned(&reord, *argv, 0))
788 invarg("\"reordering\" value is invalid\n", *argv);
789 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord);
791 } else if (strcmp(*argv, "rtt") == 0) {
794 if (strcmp(*argv, "lock") == 0) {
795 mxlock |= (1<<RTAX_RTT);
798 if (get_jiffies(&rtt, *argv, 0, &raw))
799 invarg("\"rtt\" value is invalid\n", *argv);
800 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT,
801 (raw) ? rtt : rtt * 8);
802 } else if (strcmp(*argv, "rto_min") == 0) {
805 mxlock |= (1<<RTAX_RTO_MIN);
806 if (get_jiffies(&rto_min, *argv, 0, &raw))
807 invarg("\"rto_min\" value is invalid\n",
809 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN,
811 } else if (matches(*argv, "window") == 0) {
814 if (strcmp(*argv, "lock") == 0) {
815 mxlock |= (1<<RTAX_WINDOW);
818 if (get_unsigned(&win, *argv, 0))
819 invarg("\"window\" value is invalid\n", *argv);
820 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win);
821 } else if (matches(*argv, "cwnd") == 0) {
824 if (strcmp(*argv, "lock") == 0) {
825 mxlock |= (1<<RTAX_CWND);
828 if (get_unsigned(&win, *argv, 0))
829 invarg("\"cwnd\" value is invalid\n", *argv);
830 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win);
831 } else if (matches(*argv, "initcwnd") == 0) {
834 if (strcmp(*argv, "lock") == 0) {
835 mxlock |= (1<<RTAX_INITCWND);
838 if (get_unsigned(&win, *argv, 0))
839 invarg("\"initcwnd\" value is invalid\n", *argv);
840 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITCWND, win);
841 } else if (matches(*argv, "initrwnd") == 0) {
844 if (strcmp(*argv, "lock") == 0) {
845 mxlock |= (1<<RTAX_INITRWND);
848 if (get_unsigned(&win, *argv, 0))
849 invarg("\"initrwnd\" value is invalid\n", *argv);
850 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITRWND, win);
851 } else if (matches(*argv, "rttvar") == 0) {
854 if (strcmp(*argv, "lock") == 0) {
855 mxlock |= (1<<RTAX_RTTVAR);
858 if (get_jiffies(&win, *argv, 0, &raw))
859 invarg("\"rttvar\" value is invalid\n", *argv);
860 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR,
861 (raw) ? win : win * 4);
862 } else if (matches(*argv, "ssthresh") == 0) {
865 if (strcmp(*argv, "lock") == 0) {
866 mxlock |= (1<<RTAX_SSTHRESH);
869 if (get_unsigned(&win, *argv, 0))
870 invarg("\"ssthresh\" value is invalid\n", *argv);
871 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win);
872 } else if (matches(*argv, "realms") == 0) {
875 if (get_rt_realms(&realm, *argv))
876 invarg("\"realm\" value is invalid\n", *argv);
877 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
878 } else if (strcmp(*argv, "onlink") == 0) {
879 req.r.rtm_flags |= RTNH_F_ONLINK;
880 } else if (strcmp(*argv, "nexthop") == 0) {
883 } else if (matches(*argv, "protocol") == 0) {
886 if (rtnl_rtprot_a2n(&prot, *argv))
887 invarg("\"protocol\" value is invalid\n", *argv);
888 req.r.rtm_protocol = prot;
890 } else if (matches(*argv, "table") == 0) {
893 if (rtnl_rttable_a2n(&tid, *argv))
894 invarg("\"table\" value is invalid\n", *argv);
896 req.r.rtm_table = tid;
898 req.r.rtm_table = RT_TABLE_UNSPEC;
899 addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
902 } else if (strcmp(*argv, "dev") == 0 ||
903 strcmp(*argv, "oif") == 0) {
910 if (strcmp(*argv, "to") == 0) {
913 if ((**argv < '0' || **argv > '9') &&
914 rtnl_rtntype_a2n(&type, *argv) == 0) {
916 req.r.rtm_type = type;
920 if (matches(*argv, "help") == 0)
923 duparg2("to", *argv);
924 get_prefix(&dst, *argv, req.r.rtm_family);
925 if (req.r.rtm_family == AF_UNSPEC)
926 req.r.rtm_family = dst.family;
927 req.r.rtm_dst_len = dst.bitlen;
930 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
941 if ((idx = ll_name_to_index(d)) == 0) {
942 fprintf(stderr, "Cannot find device \"%s\"\n", d);
945 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
949 if (mxrta->rta_len > RTA_LENGTH(0)) {
951 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
952 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
956 parse_nexthops(&req.n, &req.r, argc, argv);
959 if (req.r.rtm_type == RTN_LOCAL ||
960 req.r.rtm_type == RTN_BROADCAST ||
961 req.r.rtm_type == RTN_NAT ||
962 req.r.rtm_type == RTN_ANYCAST)
963 req.r.rtm_table = RT_TABLE_LOCAL;
966 if (req.r.rtm_type == RTN_LOCAL ||
967 req.r.rtm_type == RTN_NAT)
968 req.r.rtm_scope = RT_SCOPE_HOST;
969 else if (req.r.rtm_type == RTN_BROADCAST ||
970 req.r.rtm_type == RTN_MULTICAST ||
971 req.r.rtm_type == RTN_ANYCAST)
972 req.r.rtm_scope = RT_SCOPE_LINK;
973 else if (req.r.rtm_type == RTN_UNICAST ||
974 req.r.rtm_type == RTN_UNSPEC) {
975 if (cmd == RTM_DELROUTE)
976 req.r.rtm_scope = RT_SCOPE_NOWHERE;
977 else if (!gw_ok && !nhs_ok)
978 req.r.rtm_scope = RT_SCOPE_LINK;
982 if (req.r.rtm_family == AF_UNSPEC)
983 req.r.rtm_family = AF_INET;
985 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
991 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
997 struct sockaddr_nl nladdr;
999 memset(&nladdr, 0, sizeof(nladdr));
1000 memset(&req, 0, sizeof(req));
1001 nladdr.nl_family = AF_NETLINK;
1003 req.nlh.nlmsg_len = sizeof(req);
1004 req.nlh.nlmsg_type = RTM_GETROUTE;
1005 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
1006 req.nlh.nlmsg_pid = 0;
1007 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
1008 req.rtm.rtm_family = family;
1009 req.rtm.rtm_flags |= RTM_F_CLONED;
1011 return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
1014 static int iproute_flush_cache(void)
1016 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
1019 int flush_fd = open (ROUTE_FLUSH_PATH, O_WRONLY);
1020 char *buffer = "-1";
1023 fprintf (stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH);
1027 len = strlen (buffer);
1029 if ((write (flush_fd, (void *)buffer, len)) < len) {
1030 fprintf (stderr, "Cannot flush routing cache\n");
1038 static int iproute_list_or_flush(int argc, char **argv, int flush)
1040 int do_ipv6 = preferred_family;
1044 iproute_reset_filter();
1045 filter.tb = RT_TABLE_MAIN;
1047 if (flush && argc <= 0) {
1048 fprintf(stderr, "\"ip route flush\" requires arguments.\n");
1053 if (matches(*argv, "table") == 0) {
1056 if (rtnl_rttable_a2n(&tid, *argv)) {
1057 if (strcmp(*argv, "all") == 0) {
1059 } else if (strcmp(*argv, "cache") == 0) {
1061 } else if (strcmp(*argv, "help") == 0) {
1064 invarg("table id value is invalid\n", *argv);
1068 } else if (matches(*argv, "cached") == 0 ||
1069 matches(*argv, "cloned") == 0) {
1071 } else if (strcmp(*argv, "tos") == 0 ||
1072 matches(*argv, "dsfield") == 0) {
1075 if (rtnl_dsfield_a2n(&tos, *argv))
1076 invarg("TOS value is invalid\n", *argv);
1078 filter.tosmask = -1;
1079 } else if (matches(*argv, "protocol") == 0) {
1082 filter.protocolmask = -1;
1083 if (rtnl_rtprot_a2n(&prot, *argv)) {
1084 if (strcmp(*argv, "all") != 0)
1085 invarg("invalid \"protocol\"\n", *argv);
1087 filter.protocolmask = 0;
1089 filter.protocol = prot;
1090 } else if (matches(*argv, "scope") == 0) {
1093 filter.scopemask = -1;
1094 if (rtnl_rtscope_a2n(&scope, *argv)) {
1095 if (strcmp(*argv, "all") != 0)
1096 invarg("invalid \"scope\"\n", *argv);
1097 scope = RT_SCOPE_NOWHERE;
1098 filter.scopemask = 0;
1100 filter.scope = scope;
1101 } else if (matches(*argv, "type") == 0) {
1104 filter.typemask = -1;
1105 if (rtnl_rtntype_a2n(&type, *argv))
1106 invarg("node type value is invalid\n", *argv);
1108 } else if (strcmp(*argv, "dev") == 0 ||
1109 strcmp(*argv, "oif") == 0) {
1112 } else if (strcmp(*argv, "iif") == 0) {
1115 } else if (strcmp(*argv, "via") == 0) {
1117 get_prefix(&filter.rvia, *argv, do_ipv6);
1118 } else if (strcmp(*argv, "src") == 0) {
1120 get_prefix(&filter.rprefsrc, *argv, do_ipv6);
1121 } else if (matches(*argv, "realms") == 0) {
1124 if (get_rt_realms(&realm, *argv))
1125 invarg("invalid realms\n", *argv);
1126 filter.realm = realm;
1127 filter.realmmask = ~0U;
1128 if ((filter.realm&0xFFFF) == 0 &&
1129 (*argv)[strlen(*argv) - 1] == '/')
1130 filter.realmmask &= ~0xFFFF;
1131 if ((filter.realm&0xFFFF0000U) == 0 &&
1132 (strchr(*argv, '/') == NULL ||
1134 filter.realmmask &= ~0xFFFF0000U;
1135 } else if (matches(*argv, "from") == 0) {
1137 if (matches(*argv, "root") == 0) {
1139 get_prefix(&filter.rsrc, *argv, do_ipv6);
1140 } else if (matches(*argv, "match") == 0) {
1142 get_prefix(&filter.msrc, *argv, do_ipv6);
1144 if (matches(*argv, "exact") == 0) {
1147 get_prefix(&filter.msrc, *argv, do_ipv6);
1148 filter.rsrc = filter.msrc;
1151 if (matches(*argv, "to") == 0) {
1154 if (matches(*argv, "root") == 0) {
1156 get_prefix(&filter.rdst, *argv, do_ipv6);
1157 } else if (matches(*argv, "match") == 0) {
1159 get_prefix(&filter.mdst, *argv, do_ipv6);
1161 if (matches(*argv, "exact") == 0) {
1164 get_prefix(&filter.mdst, *argv, do_ipv6);
1165 filter.rdst = filter.mdst;
1171 if (do_ipv6 == AF_UNSPEC && filter.tb)
1180 if ((idx = ll_name_to_index(id)) == 0) {
1181 fprintf(stderr, "Cannot find device \"%s\"\n", id);
1185 filter.iifmask = -1;
1188 if ((idx = ll_name_to_index(od)) == 0) {
1189 fprintf(stderr, "Cannot find device \"%s\"\n", od);
1193 filter.oifmask = -1;
1199 char flushb[4096-512];
1200 time_t start = time(0);
1202 if (filter.cloned) {
1203 if (do_ipv6 != AF_INET6) {
1204 iproute_flush_cache();
1206 printf("*** IPv4 routing cache is flushed.\n");
1208 if (do_ipv6 == AF_INET)
1212 filter.flushb = flushb;
1214 filter.flushe = sizeof(flushb);
1217 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
1218 perror("Cannot send dump request");
1222 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
1223 fprintf(stderr, "Flush terminated\n");
1226 if (filter.flushed == 0) {
1228 if (round == 0 && (!filter.cloned || do_ipv6 == AF_INET6))
1229 printf("Nothing to flush.\n");
1231 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
1237 if (flush_update() < 0)
1240 if (time(0) - start > 30) {
1241 printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
1242 time(0) - start, filter.flushed);
1247 printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed);
1253 if (!filter.cloned) {
1254 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
1255 perror("Cannot send dump request");
1259 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
1260 perror("Cannot send dump request");
1265 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
1266 fprintf(stderr, "Dump terminated\n");
1274 int iproute_get(int argc, char **argv)
1286 memset(&req, 0, sizeof(req));
1288 iproute_reset_filter();
1291 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1292 req.n.nlmsg_flags = NLM_F_REQUEST;
1293 req.n.nlmsg_type = RTM_GETROUTE;
1294 req.r.rtm_family = preferred_family;
1295 req.r.rtm_table = 0;
1296 req.r.rtm_protocol = 0;
1297 req.r.rtm_scope = 0;
1299 req.r.rtm_src_len = 0;
1300 req.r.rtm_dst_len = 0;
1304 if (strcmp(*argv, "tos") == 0 ||
1305 matches(*argv, "dsfield") == 0) {
1308 if (rtnl_dsfield_a2n(&tos, *argv))
1309 invarg("TOS value is invalid\n", *argv);
1310 req.r.rtm_tos = tos;
1311 } else if (matches(*argv, "from") == 0) {
1314 if (matches(*argv, "help") == 0)
1317 get_prefix(&addr, *argv, req.r.rtm_family);
1318 if (req.r.rtm_family == AF_UNSPEC)
1319 req.r.rtm_family = addr.family;
1321 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
1322 req.r.rtm_src_len = addr.bitlen;
1323 } else if (matches(*argv, "iif") == 0) {
1326 } else if (matches(*argv, "oif") == 0 ||
1327 strcmp(*argv, "dev") == 0) {
1330 } else if (matches(*argv, "notify") == 0) {
1331 req.r.rtm_flags |= RTM_F_NOTIFY;
1332 } else if (matches(*argv, "connected") == 0) {
1336 if (strcmp(*argv, "to") == 0) {
1339 if (matches(*argv, "help") == 0)
1341 get_prefix(&addr, *argv, req.r.rtm_family);
1342 if (req.r.rtm_family == AF_UNSPEC)
1343 req.r.rtm_family = addr.family;
1345 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
1346 req.r.rtm_dst_len = addr.bitlen;
1351 if (req.r.rtm_dst_len == 0) {
1352 fprintf(stderr, "need at least destination address\n");
1362 if ((idx = ll_name_to_index(idev)) == 0) {
1363 fprintf(stderr, "Cannot find device \"%s\"\n", idev);
1366 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
1369 if ((idx = ll_name_to_index(odev)) == 0) {
1370 fprintf(stderr, "Cannot find device \"%s\"\n", odev);
1373 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
1377 if (req.r.rtm_family == AF_UNSPEC)
1378 req.r.rtm_family = AF_INET;
1380 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
1383 if (connected && !from_ok) {
1384 struct rtmsg *r = NLMSG_DATA(&req.n);
1385 int len = req.n.nlmsg_len;
1386 struct rtattr * tb[RTA_MAX+1];
1388 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
1389 fprintf(stderr, "An error :-)\n");
1393 if (req.n.nlmsg_type != RTM_NEWROUTE) {
1394 fprintf(stderr, "Not a route?\n");
1397 len -= NLMSG_LENGTH(sizeof(*r));
1399 fprintf(stderr, "Wrong len %d\n", len);
1403 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
1405 if (tb[RTA_PREFSRC]) {
1406 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
1407 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
1408 } else if (!tb[RTA_SRC]) {
1409 fprintf(stderr, "Failed to connect the route\n");
1412 if (!odev && tb[RTA_OIF])
1413 tb[RTA_OIF]->rta_type = 0;
1414 if (tb[RTA_GATEWAY])
1415 tb[RTA_GATEWAY]->rta_type = 0;
1416 if (!idev && tb[RTA_IIF])
1417 tb[RTA_IIF]->rta_type = 0;
1418 req.n.nlmsg_flags = NLM_F_REQUEST;
1419 req.n.nlmsg_type = RTM_GETROUTE;
1421 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
1425 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
1426 fprintf(stderr, "An error :-)\n");
1433 void iproute_reset_filter()
1435 memset(&filter, 0, sizeof(filter));
1436 filter.mdst.bitlen = -1;
1437 filter.msrc.bitlen = -1;
1440 int do_iproute(int argc, char **argv)
1443 return iproute_list_or_flush(0, NULL, 0);
1445 if (matches(*argv, "add") == 0)
1446 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL,
1448 if (matches(*argv, "change") == 0 || strcmp(*argv, "chg") == 0)
1449 return iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE,
1451 if (matches(*argv, "replace") == 0)
1452 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
1454 if (matches(*argv, "prepend") == 0)
1455 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE,
1457 if (matches(*argv, "append") == 0)
1458 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND,
1460 if (matches(*argv, "test") == 0)
1461 return iproute_modify(RTM_NEWROUTE, NLM_F_EXCL,
1463 if (matches(*argv, "delete") == 0)
1464 return iproute_modify(RTM_DELROUTE, 0,
1466 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1467 || matches(*argv, "lst") == 0)
1468 return iproute_list_or_flush(argc-1, argv+1, 0);
1469 if (matches(*argv, "get") == 0)
1470 return iproute_get(argc-1, argv+1);
1471 if (matches(*argv, "flush") == 0)
1472 return iproute_list_or_flush(argc-1, argv+1, 1);
1473 if (matches(*argv, "help") == 0)
1475 fprintf(stderr, "Command \"%s\" is unknown, try \"ip route help\".\n", *argv);