Home | History | Annotate | Download | only in ip
      1 /*
      2  * iproute.c		"ip route".
      3  *
      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.
      8  *
      9  * Authors:	Alexey Kuznetsov, <kuznet (at) ms2.inr.ac.ru>
     10  *
     11  */
     12 
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <unistd.h>
     16 #include <syslog.h>
     17 #include <fcntl.h>
     18 #include <string.h>
     19 #include <time.h>
     20 #include <sys/time.h>
     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>
     26 
     27 #include "rt_names.h"
     28 #include "utils.h"
     29 #include "ip_common.h"
     30 
     31 #ifndef RTAX_RTTVAR
     32 #define RTAX_RTTVAR RTAX_HOPS
     33 #endif
     34 
     35 
     36 static const char *mx_names[RTAX_MAX+1] = {
     37 	[RTAX_MTU]	= "mtu",
     38 	[RTAX_WINDOW]	= "window",
     39 	[RTAX_RTT]	= "rtt",
     40 	[RTAX_RTTVAR]	= "rttvar",
     41 	[RTAX_SSTHRESH] = "ssthresh",
     42 	[RTAX_CWND]	= "cwnd",
     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",
     50 };
     51 static void usage(void) __attribute__((noreturn));
     52 
     53 static void usage(void)
     54 {
     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");
     81 	exit(-1);
     82 }
     83 
     84 
     85 static struct
     86 {
     87 	int tb;
     88 	int cloned;
     89 	int flushed;
     90 	char *flushb;
     91 	int flushp;
     92 	int flushe;
     93 	int protocol, protocolmask;
     94 	int scope, scopemask;
     95 	int type, typemask;
     96 	int tos, tosmask;
     97 	int iif, iifmask;
     98 	int oif, oifmask;
     99 	int realm, realmmask;
    100 	inet_prefix rprefsrc;
    101 	inet_prefix rvia;
    102 	inet_prefix rdst;
    103 	inet_prefix mdst;
    104 	inet_prefix rsrc;
    105 	inet_prefix msrc;
    106 } filter;
    107 
    108 static int flush_update(void)
    109 {
    110 	if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
    111 		perror("Failed to send flush request");
    112 		return -1;
    113 	}
    114 	filter.flushp = 0;
    115 	return 0;
    116 }
    117 
    118 int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
    119 {
    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];
    124 	char abuf[256];
    125 	inet_prefix dst;
    126 	inet_prefix src;
    127 	inet_prefix prefsrc;
    128 	inet_prefix via;
    129 	int host_len = -1;
    130 	static int ip6_multiple_tables;
    131 	__u32 table;
    132 	SPRINT_BUF(b1);
    133 	static int hz;
    134 
    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);
    138 		return 0;
    139 	}
    140 	if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
    141 		return 0;
    142 	len -= NLMSG_LENGTH(sizeof(*r));
    143 	if (len < 0) {
    144 		fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
    145 		return -1;
    146 	}
    147 
    148 	if (r->rtm_family == AF_INET6)
    149 		host_len = 128;
    150 	else if (r->rtm_family == AF_INET)
    151 		host_len = 32;
    152 	else if (r->rtm_family == AF_DECnet)
    153 		host_len = 16;
    154 	else if (r->rtm_family == AF_IPX)
    155 		host_len = 80;
    156 
    157 	parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
    158 	table = rtm_get_table(r, tb);
    159 
    160 	if (r->rtm_family == AF_INET6 && table != RT_TABLE_MAIN)
    161 		ip6_multiple_tables = 1;
    162 
    163 	if (filter.cloned == !(r->rtm_flags&RTM_F_CLONED))
    164 		return 0;
    165 
    166 	if (r->rtm_family == AF_INET6 && !ip6_multiple_tables) {
    167 		if (filter.tb) {
    168 			if (filter.tb == RT_TABLE_LOCAL) {
    169 				if (r->rtm_type != RTN_LOCAL)
    170 					return 0;
    171 			} else if (filter.tb == RT_TABLE_MAIN) {
    172 				if (r->rtm_type == RTN_LOCAL)
    173 					return 0;
    174 			} else {
    175 				return 0;
    176 			}
    177 		}
    178 	} else {
    179 		if (filter.tb > 0 && filter.tb != table)
    180 			return 0;
    181 	}
    182 	if ((filter.protocol^r->rtm_protocol)&filter.protocolmask)
    183 		return 0;
    184 	if ((filter.scope^r->rtm_scope)&filter.scopemask)
    185 		return 0;
    186 	if ((filter.type^r->rtm_type)&filter.typemask)
    187 		return 0;
    188 	if ((filter.tos^r->rtm_tos)&filter.tosmask)
    189 		return 0;
    190 	if (filter.rdst.family &&
    191 	    (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len))
    192 		return 0;
    193 	if (filter.mdst.family &&
    194 	    (r->rtm_family != filter.mdst.family ||
    195 	     (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len)))
    196 		return 0;
    197 	if (filter.rsrc.family &&
    198 	    (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len))
    199 		return 0;
    200 	if (filter.msrc.family &&
    201 	    (r->rtm_family != filter.msrc.family ||
    202 	     (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len)))
    203 		return 0;
    204 	if (filter.rvia.family && r->rtm_family != filter.rvia.family)
    205 		return 0;
    206 	if (filter.rprefsrc.family && r->rtm_family != filter.rprefsrc.family)
    207 		return 0;
    208 
    209 	memset(&dst, 0, sizeof(dst));
    210 	dst.family = r->rtm_family;
    211 	if (tb[RTA_DST])
    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;
    216 		if (tb[RTA_SRC])
    217 			memcpy(&src.data, RTA_DATA(tb[RTA_SRC]), (r->rtm_src_len+7)/8);
    218 	}
    219 	if (filter.rvia.bitlen>0) {
    220 		memset(&via, 0, sizeof(via));
    221 		via.family = r->rtm_family;
    222 		if (tb[RTA_GATEWAY])
    223 			memcpy(&via.data, RTA_DATA(tb[RTA_GATEWAY]), host_len/8);
    224 	}
    225 	if (filter.rprefsrc.bitlen>0) {
    226 		memset(&prefsrc, 0, sizeof(prefsrc));
    227 		prefsrc.family = r->rtm_family;
    228 		if (tb[RTA_PREFSRC])
    229 			memcpy(&prefsrc.data, RTA_DATA(tb[RTA_PREFSRC]), host_len/8);
    230 	}
    231 
    232 	if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
    233 		return 0;
    234 	if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
    235 	    inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
    236 		return 0;
    237 
    238 	if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
    239 		return 0;
    240 	if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
    241 	    inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
    242 		return 0;
    243 
    244 	if (filter.rvia.family && inet_addr_match(&via, &filter.rvia, filter.rvia.bitlen))
    245 		return 0;
    246 	if (filter.rprefsrc.family && inet_addr_match(&prefsrc, &filter.rprefsrc, filter.rprefsrc.bitlen))
    247 		return 0;
    248 	if (filter.realmmask) {
    249 		__u32 realms = 0;
    250 		if (tb[RTA_FLOW])
    251 			realms = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
    252 		if ((realms^filter.realm)&filter.realmmask)
    253 			return 0;
    254 	}
    255 	if (filter.iifmask) {
    256 		int iif = 0;
    257 		if (tb[RTA_IIF])
    258 			iif = *(int*)RTA_DATA(tb[RTA_IIF]);
    259 		if ((iif^filter.iif)&filter.iifmask)
    260 			return 0;
    261 	}
    262 	if (filter.oifmask) {
    263 		int oif = 0;
    264 		if (tb[RTA_OIF])
    265 			oif = *(int*)RTA_DATA(tb[RTA_OIF]);
    266 		if ((oif^filter.oif)&filter.oifmask)
    267 			return 0;
    268 	}
    269 	if (filter.flushb &&
    270 	    r->rtm_family == AF_INET6 &&
    271 	    r->rtm_dst_len == 0 &&
    272 	    r->rtm_type == RTN_UNREACHABLE &&
    273 	    tb[RTA_PRIORITY] &&
    274 	    *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
    275 		return 0;
    276 
    277 	if (filter.flushb) {
    278 		struct nlmsghdr *fn;
    279 		if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
    280 			if (flush_update())
    281 				return -1;
    282 		}
    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;
    289 		filter.flushed++;
    290 		if (show_stats < 2)
    291 			return 0;
    292 	}
    293 
    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)));
    298 
    299 	if (tb[RTA_DST]) {
    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]),
    304 							 abuf, sizeof(abuf)),
    305 				r->rtm_dst_len
    306 				);
    307 		} else {
    308 			fprintf(fp, "%s ", format_host(r->rtm_family,
    309 						       RTA_PAYLOAD(tb[RTA_DST]),
    310 						       RTA_DATA(tb[RTA_DST]),
    311 						       abuf, sizeof(abuf))
    312 				);
    313 		}
    314 	} else if (r->rtm_dst_len) {
    315 		fprintf(fp, "0/%d ", r->rtm_dst_len);
    316 	} else {
    317 		fprintf(fp, "default ");
    318 	}
    319 	if (tb[RTA_SRC]) {
    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]),
    324 							 abuf, sizeof(abuf)),
    325 				r->rtm_src_len
    326 				);
    327 		} else {
    328 			fprintf(fp, "from %s ", format_host(r->rtm_family,
    329 						       RTA_PAYLOAD(tb[RTA_SRC]),
    330 						       RTA_DATA(tb[RTA_SRC]),
    331 						       abuf, sizeof(abuf))
    332 				);
    333 		}
    334 	} else if (r->rtm_src_len) {
    335 		fprintf(fp, "from 0/%u ", r->rtm_src_len);
    336 	}
    337 	if (r->rtm_tos && filter.tosmask != -1) {
    338 		SPRINT_BUF(b1);
    339 		fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
    340 	}
    341 
    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)));
    348 	}
    349 	if (tb[RTA_OIF] && filter.oifmask != -1)
    350 		fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
    351 
    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)));
    359 	}
    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.
    363 		 */
    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)));
    369 	}
    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 ");
    380 
    381 	if (tb[RTA_FLOW] && filter.realmmask != ~0U) {
    382 		__u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
    383 		__u32 from = to>>16;
    384 		to &= 0xFFFF;
    385 		fprintf(fp, "realm%s ", from ? "s" : "");
    386 		if (from) {
    387 			fprintf(fp, "%s/",
    388 				rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
    389 		}
    390 		fprintf(fp, "%s ",
    391 			rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
    392 	}
    393 	if ((r->rtm_flags&RTM_F_CLONED) && r->rtm_family == AF_INET) {
    394 		__u32 flags = r->rtm_flags&~0xFFFF;
    395 		int first = 1;
    396 
    397 		fprintf(fp, "%s    cache ", _SL_);
    398 
    399 #define PRTFL(fl,flname) if (flags&RTCF_##fl) { \
    400   flags &= ~RTCF_##fl; \
    401   fprintf(fp, "%s" flname "%s", first ? "<" : "", flags ? "," : "> "); \
    402   first = 0; }
    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");
    409 		PRTFL(MASQ, "masq");
    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");
    417 
    418 		if (flags)
    419 			fprintf(fp, "%s%x> ", first ? "<" : "", flags);
    420 		if (tb[RTA_CACHEINFO]) {
    421 			struct rta_cacheinfo *ci = RTA_DATA(tb[RTA_CACHEINFO]);
    422 			if (!hz)
    423 				hz = get_user_hz();
    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);
    428 			if (show_stats) {
    429 				if (ci->rta_clntref)
    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);
    435 			}
    436 #ifdef RTNETLINK_HAVE_PEERINFO
    437 			if (ci->rta_id)
    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);
    441 #endif
    442 		}
    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)) {
    448 			if (!hz)
    449 				hz = get_user_hz();
    450 			if (r->rtm_flags & RTM_F_CLONED)
    451 				fprintf(fp, "%s    cache ", _SL_);
    452 			if (ci->rta_expires)
    453 				fprintf(fp, " expires %dsec", ci->rta_expires/hz);
    454 			if (ci->rta_error != 0)
    455 				fprintf(fp, " error %d", ci->rta_error);
    456 			if (show_stats) {
    457 				if (ci->rta_clntref)
    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);
    463 			}
    464 		} else if (ci) {
    465 			if (ci->rta_error != 0)
    466 				fprintf(fp, " error %d", ci->rta_error);
    467 		}
    468 	}
    469 	if (tb[RTA_METRICS]) {
    470 		int i;
    471 		unsigned mxlock = 0;
    472 		struct rtattr *mxrta[RTAX_MAX+1];
    473 
    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]);
    478 
    479 		for (i=2; i<= RTAX_MAX; i++) {
    480 			unsigned val;
    481 
    482 			if (mxrta[i] == NULL)
    483 				continue;
    484 			if (!hz)
    485 				hz = get_user_hz();
    486 
    487 			if (i < sizeof(mx_names)/sizeof(char*) && mx_names[i])
    488 				fprintf(fp, " %s", mx_names[i]);
    489 			else
    490 				fprintf(fp, " metric %d", i);
    491 			if (mxlock & (1<<i))
    492 				fprintf(fp, " lock");
    493 
    494 			val = *(unsigned*)RTA_DATA(mxrta[i]);
    495 			switch (i) {
    496 			case RTAX_HOPLIMIT:
    497 				if ((int)val == -1)
    498 					val = 0;
    499 				/* fall through */
    500 			default:
    501 				fprintf(fp, " %u", val);
    502 				break;
    503 
    504 			case RTAX_RTT:
    505 			case RTAX_RTTVAR:
    506 			case RTAX_RTO_MIN:
    507 				val *= 1000;
    508 				if (i == RTAX_RTT)
    509 					val /= 8;
    510 				else if (i == RTAX_RTTVAR)
    511 					val /= 4;
    512 
    513 				if (val >= hz)
    514 					fprintf(fp, " %llums",
    515 						(unsigned long long) val / hz);
    516 				else
    517 					fprintf(fp, " %.2fms",
    518 						(double)val / hz);
    519 			}
    520 		}
    521 	}
    522 	if (tb[RTA_IIF] && filter.iifmask != -1) {
    523 		fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
    524 	}
    525 	if (tb[RTA_MULTIPATH]) {
    526 		struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]);
    527 		int first = 0;
    528 
    529 		len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
    530 
    531 		for (;;) {
    532 			if (len < sizeof(*nh))
    533 				break;
    534 			if (nh->rtnh_len > len)
    535 				break;
    536 			if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) {
    537 				if (first)
    538 					fprintf(fp, " Oifs:");
    539 				else
    540 					fprintf(fp, " ");
    541 			} else
    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)));
    551 				}
    552 				if (tb[RTA_FLOW]) {
    553 					__u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
    554 					__u32 from = to>>16;
    555 					to &= 0xFFFF;
    556 					fprintf(fp, " realm%s ", from ? "s" : "");
    557 					if (from) {
    558 						fprintf(fp, "%s/",
    559 							rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
    560 					}
    561 					fprintf(fp, "%s",
    562 						rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
    563 				}
    564 			}
    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);
    569 			} else {
    570 				fprintf(fp, " dev %s", ll_index_to_name(nh->rtnh_ifindex));
    571 				fprintf(fp, " weight %d", nh->rtnh_hops+1);
    572 			}
    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);
    580 			nh = RTNH_NEXT(nh);
    581 		}
    582 	}
    583 	fprintf(fp, "\n");
    584 	fflush(fp);
    585 	return 0;
    586 }
    587 
    588 
    589 int parse_one_nh(struct rtattr *rta, struct rtnexthop *rtnh, int *argcp, char ***argvp)
    590 {
    591 	int argc = *argcp;
    592 	char **argv = *argvp;
    593 
    594 	while (++argv, --argc > 0) {
    595 		if (strcmp(*argv, "via") == 0) {
    596 			NEXT_ARG();
    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) {
    600 			NEXT_ARG();
    601 			if ((rtnh->rtnh_ifindex = ll_name_to_index(*argv)) == 0) {
    602 				fprintf(stderr, "Cannot find device \"%s\"\n", *argv);
    603 				exit(1);
    604 			}
    605 		} else if (strcmp(*argv, "weight") == 0) {
    606 			unsigned w;
    607 			NEXT_ARG();
    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) {
    614 			__u32 realm;
    615 			NEXT_ARG();
    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;
    620 		} else
    621 			break;
    622 	}
    623 	*argcp = argc;
    624 	*argvp = argv;
    625 	return 0;
    626 }
    627 
    628 int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r, int argc, char **argv)
    629 {
    630 	char buf[1024];
    631 	struct rtattr *rta = (void*)buf;
    632 	struct rtnexthop *rtnh;
    633 
    634 	rta->rta_type = RTA_MULTIPATH;
    635 	rta->rta_len = RTA_LENGTH(0);
    636 	rtnh = RTA_DATA(rta);
    637 
    638 	while (argc > 0) {
    639 		if (strcmp(*argv, "nexthop") != 0) {
    640 			fprintf(stderr, "Error: \"nexthop\" or end of line is expected instead of \"%s\"\n", *argv);
    641 			exit(-1);
    642 		}
    643 		if (argc <= 1) {
    644 			fprintf(stderr, "Error: unexpected end of line after \"nexthop\"\n");
    645 			exit(-1);
    646 		}
    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);
    652 	}
    653 
    654 	if (rta->rta_len > RTA_LENGTH(0))
    655 		addattr_l(n, 1024, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta));
    656 	return 0;
    657 }
    658 
    659 
    660 int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
    661 {
    662 	struct {
    663 		struct nlmsghdr 	n;
    664 		struct rtmsg 		r;
    665 		char   			buf[1024];
    666 	} req;
    667 	char  mxbuf[256];
    668 	struct rtattr * mxrta = (void*)mxbuf;
    669 	unsigned mxlock = 0;
    670 	char  *d = NULL;
    671 	int gw_ok = 0;
    672 	int dst_ok = 0;
    673 	int nhs_ok = 0;
    674 	int scope_ok = 0;
    675 	int table_ok = 0;
    676 	int proto_ok = 0;
    677 	int type_ok = 0;
    678 	int raw = 0;
    679 
    680 	memset(&req, 0, sizeof(req));
    681 
    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;
    688 
    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;
    693 	}
    694 
    695 	mxrta->rta_type = RTA_METRICS;
    696 	mxrta->rta_len = RTA_LENGTH(0);
    697 
    698 	while (argc > 0) {
    699 		if (strcmp(*argv, "src") == 0) {
    700 			inet_prefix addr;
    701 			NEXT_ARG();
    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) {
    707 			inet_prefix addr;
    708 			gw_ok = 1;
    709 			NEXT_ARG();
    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) {
    715 			inet_prefix addr;
    716 			NEXT_ARG();
    717 			get_prefix(&addr, *argv, req.r.rtm_family);
    718 			if (req.r.rtm_family == AF_UNSPEC)
    719 				req.r.rtm_family = addr.family;
    720 			if (addr.bytelen)
    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) {
    725 			__u32 tos;
    726 			NEXT_ARG();
    727 			if (rtnl_dsfield_a2n(&tos, *argv))
    728 				invarg("\"tos\" value is invalid\n", *argv);
    729 			req.r.rtm_tos = tos;
    730 		} else if (matches(*argv, "metric") == 0 ||
    731 			   matches(*argv, "priority") == 0 ||
    732 			   matches(*argv, "preference") == 0) {
    733 			__u32 metric;
    734 			NEXT_ARG();
    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) {
    739 			__u32 scope = 0;
    740 			NEXT_ARG();
    741 			if (rtnl_rtscope_a2n(&scope, *argv))
    742 				invarg("invalid \"scope\" value\n", *argv);
    743 			req.r.rtm_scope = scope;
    744 			scope_ok = 1;
    745 		} else if (strcmp(*argv, "mtu") == 0) {
    746 			unsigned mtu;
    747 			NEXT_ARG();
    748 			if (strcmp(*argv, "lock") == 0) {
    749 				mxlock |= (1<<RTAX_MTU);
    750 				NEXT_ARG();
    751 			}
    752 			if (get_unsigned(&mtu, *argv, 0))
    753 				invarg("\"mtu\" value is invalid\n", *argv);
    754 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
    755 #ifdef RTAX_HOPLIMIT
    756 		} else if (strcmp(*argv, "hoplimit") == 0) {
    757 			unsigned hoplimit;
    758 			NEXT_ARG();
    759 			if (strcmp(*argv, "lock") == 0) {
    760 				mxlock |= (1<<RTAX_HOPLIMIT);
    761 				NEXT_ARG();
    762 			}
    763 			if (get_unsigned(&hoplimit, *argv, 0))
    764 				invarg("\"hoplimit\" value is invalid\n", *argv);
    765 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit);
    766 #endif
    767 #ifdef RTAX_ADVMSS
    768 		} else if (strcmp(*argv, "advmss") == 0) {
    769 			unsigned mss;
    770 			NEXT_ARG();
    771 			if (strcmp(*argv, "lock") == 0) {
    772 				mxlock |= (1<<RTAX_ADVMSS);
    773 				NEXT_ARG();
    774 			}
    775 			if (get_unsigned(&mss, *argv, 0))
    776 				invarg("\"mss\" value is invalid\n", *argv);
    777 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss);
    778 #endif
    779 #ifdef RTAX_REORDERING
    780 		} else if (matches(*argv, "reordering") == 0) {
    781 			unsigned reord;
    782 			NEXT_ARG();
    783 			if (strcmp(*argv, "lock") == 0) {
    784 				mxlock |= (1<<RTAX_REORDERING);
    785 				NEXT_ARG();
    786 			}
    787 			if (get_unsigned(&reord, *argv, 0))
    788 				invarg("\"reordering\" value is invalid\n", *argv);
    789 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord);
    790 #endif
    791 		} else if (strcmp(*argv, "rtt") == 0) {
    792 			unsigned rtt;
    793 			NEXT_ARG();
    794 			if (strcmp(*argv, "lock") == 0) {
    795 				mxlock |= (1<<RTAX_RTT);
    796 				NEXT_ARG();
    797 			}
    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) {
    803 			unsigned rto_min;
    804 			NEXT_ARG();
    805 			mxlock |= (1<<RTAX_RTO_MIN);
    806 			if (get_jiffies(&rto_min, *argv, 0, &raw))
    807 				invarg("\"rto_min\" value is invalid\n",
    808 				       *argv);
    809 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN,
    810 				      rto_min);
    811 		} else if (matches(*argv, "window") == 0) {
    812 			unsigned win;
    813 			NEXT_ARG();
    814 			if (strcmp(*argv, "lock") == 0) {
    815 				mxlock |= (1<<RTAX_WINDOW);
    816 				NEXT_ARG();
    817 			}
    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) {
    822 			unsigned win;
    823 			NEXT_ARG();
    824 			if (strcmp(*argv, "lock") == 0) {
    825 				mxlock |= (1<<RTAX_CWND);
    826 				NEXT_ARG();
    827 			}
    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) {
    832 			unsigned win;
    833 			NEXT_ARG();
    834 			if (strcmp(*argv, "lock") == 0) {
    835 				mxlock |= (1<<RTAX_INITCWND);
    836 				NEXT_ARG();
    837 			}
    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) {
    842 			unsigned win;
    843 			NEXT_ARG();
    844 			if (strcmp(*argv, "lock") == 0) {
    845 				mxlock |= (1<<RTAX_INITRWND);
    846 				NEXT_ARG();
    847 			}
    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) {
    852 			unsigned win;
    853 			NEXT_ARG();
    854 			if (strcmp(*argv, "lock") == 0) {
    855 				mxlock |= (1<<RTAX_RTTVAR);
    856 				NEXT_ARG();
    857 			}
    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) {
    863 			unsigned win;
    864 			NEXT_ARG();
    865 			if (strcmp(*argv, "lock") == 0) {
    866 				mxlock |= (1<<RTAX_SSTHRESH);
    867 				NEXT_ARG();
    868 			}
    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) {
    873 			__u32 realm;
    874 			NEXT_ARG();
    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) {
    881 			nhs_ok = 1;
    882 			break;
    883 		} else if (matches(*argv, "protocol") == 0) {
    884 			__u32 prot;
    885 			NEXT_ARG();
    886 			if (rtnl_rtprot_a2n(&prot, *argv))
    887 				invarg("\"protocol\" value is invalid\n", *argv);
    888 			req.r.rtm_protocol = prot;
    889 			proto_ok =1;
    890 		} else if (matches(*argv, "table") == 0) {
    891 			__u32 tid;
    892 			NEXT_ARG();
    893 			if (rtnl_rttable_a2n(&tid, *argv))
    894 				invarg("\"table\" value is invalid\n", *argv);
    895 			if (tid < 256)
    896 				req.r.rtm_table = tid;
    897 			else {
    898 				req.r.rtm_table = RT_TABLE_UNSPEC;
    899 				addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
    900 			}
    901 			table_ok = 1;
    902 		} else if (strcmp(*argv, "dev") == 0 ||
    903 			   strcmp(*argv, "oif") == 0) {
    904 			NEXT_ARG();
    905 			d = *argv;
    906 		} else {
    907 			int type;
    908 			inet_prefix dst;
    909 
    910 			if (strcmp(*argv, "to") == 0) {
    911 				NEXT_ARG();
    912 			}
    913 			if ((**argv < '0' || **argv > '9') &&
    914 			    rtnl_rtntype_a2n(&type, *argv) == 0) {
    915 				NEXT_ARG();
    916 				req.r.rtm_type = type;
    917 				type_ok = 1;
    918 			}
    919 
    920 			if (matches(*argv, "help") == 0)
    921 				usage();
    922 			if (dst_ok)
    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;
    928 			dst_ok = 1;
    929 			if (dst.bytelen)
    930 				addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
    931 		}
    932 		argc--; argv++;
    933 	}
    934 
    935 	if (d || nhs_ok)  {
    936 		int idx;
    937 
    938 		ll_init_map(&rth);
    939 
    940 		if (d) {
    941 			if ((idx = ll_name_to_index(d)) == 0) {
    942 				fprintf(stderr, "Cannot find device \"%s\"\n", d);
    943 				return -1;
    944 			}
    945 			addattr32(&req.n, sizeof(req), RTA_OIF, idx);
    946 		}
    947 	}
    948 
    949 	if (mxrta->rta_len > RTA_LENGTH(0)) {
    950 		if (mxlock)
    951 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
    952 		addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
    953 	}
    954 
    955 	if (nhs_ok)
    956 		parse_nexthops(&req.n, &req.r, argc, argv);
    957 
    958 	if (!table_ok) {
    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;
    964 	}
    965 	if (!scope_ok) {
    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;
    979 		}
    980 	}
    981 
    982 	if (req.r.rtm_family == AF_UNSPEC)
    983 		req.r.rtm_family = AF_INET;
    984 
    985 	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
    986 		exit(2);
    987 
    988 	return 0;
    989 }
    990 
    991 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
    992 {
    993 	struct {
    994 		struct nlmsghdr nlh;
    995 		struct rtmsg rtm;
    996 	} req;
    997 	struct sockaddr_nl nladdr;
    998 
    999 	memset(&nladdr, 0, sizeof(nladdr));
   1000 	memset(&req, 0, sizeof(req));
   1001 	nladdr.nl_family = AF_NETLINK;
   1002 
   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;
   1010 
   1011 	return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
   1012 }
   1013 
   1014 static int iproute_flush_cache(void)
   1015 {
   1016 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
   1017 
   1018 	int len;
   1019 	int flush_fd = open (ROUTE_FLUSH_PATH, O_WRONLY);
   1020 	char *buffer = "-1";
   1021 
   1022 	if (flush_fd < 0) {
   1023 		fprintf (stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH);
   1024 		return -1;
   1025 	}
   1026 
   1027 	len = strlen (buffer);
   1028 
   1029 	if ((write (flush_fd, (void *)buffer, len)) < len) {
   1030 		fprintf (stderr, "Cannot flush routing cache\n");
   1031 		close(flush_fd);
   1032 		return -1;
   1033 	}
   1034 	close(flush_fd);
   1035 	return 0;
   1036 }
   1037 
   1038 
   1039 static int iproute_list_or_flush(int argc, char **argv, int flush)
   1040 {
   1041 	int do_ipv6 = preferred_family;
   1042 	char *id = NULL;
   1043 	char *od = NULL;
   1044 
   1045 	iproute_reset_filter();
   1046 	filter.tb = RT_TABLE_MAIN;
   1047 
   1048 	if (flush && argc <= 0) {
   1049 		fprintf(stderr, "\"ip route flush\" requires arguments.\n");
   1050 		return -1;
   1051 	}
   1052 
   1053 	while (argc > 0) {
   1054 		if (matches(*argv, "table") == 0) {
   1055 			__u32 tid;
   1056 			NEXT_ARG();
   1057 			if (rtnl_rttable_a2n(&tid, *argv)) {
   1058 				if (strcmp(*argv, "all") == 0) {
   1059 					filter.tb = 0;
   1060 				} else if (strcmp(*argv, "cache") == 0) {
   1061 					filter.cloned = 1;
   1062 				} else if (strcmp(*argv, "help") == 0) {
   1063 					usage();
   1064 				} else {
   1065 					invarg("table id value is invalid\n", *argv);
   1066 				}
   1067 			} else
   1068 				filter.tb = tid;
   1069 		} else if (matches(*argv, "cached") == 0 ||
   1070 			   matches(*argv, "cloned") == 0) {
   1071 			filter.cloned = 1;
   1072 		} else if (strcmp(*argv, "tos") == 0 ||
   1073 			   matches(*argv, "dsfield") == 0) {
   1074 			__u32 tos;
   1075 			NEXT_ARG();
   1076 			if (rtnl_dsfield_a2n(&tos, *argv))
   1077 				invarg("TOS value is invalid\n", *argv);
   1078 			filter.tos = tos;
   1079 			filter.tosmask = -1;
   1080 		} else if (matches(*argv, "protocol") == 0) {
   1081 			__u32 prot = 0;
   1082 			NEXT_ARG();
   1083 			filter.protocolmask = -1;
   1084 			if (rtnl_rtprot_a2n(&prot, *argv)) {
   1085 				if (strcmp(*argv, "all") != 0)
   1086 					invarg("invalid \"protocol\"\n", *argv);
   1087 				prot = 0;
   1088 				filter.protocolmask = 0;
   1089 			}
   1090 			filter.protocol = prot;
   1091 		} else if (matches(*argv, "scope") == 0) {
   1092 			__u32 scope = 0;
   1093 			NEXT_ARG();
   1094 			filter.scopemask = -1;
   1095 			if (rtnl_rtscope_a2n(&scope, *argv)) {
   1096 				if (strcmp(*argv, "all") != 0)
   1097 					invarg("invalid \"scope\"\n", *argv);
   1098 				scope = RT_SCOPE_NOWHERE;
   1099 				filter.scopemask = 0;
   1100 			}
   1101 			filter.scope = scope;
   1102 		} else if (matches(*argv, "type") == 0) {
   1103 			int type;
   1104 			NEXT_ARG();
   1105 			filter.typemask = -1;
   1106 			if (rtnl_rtntype_a2n(&type, *argv))
   1107 				invarg("node type value is invalid\n", *argv);
   1108 			filter.type = type;
   1109 		} else if (strcmp(*argv, "dev") == 0 ||
   1110 			   strcmp(*argv, "oif") == 0) {
   1111 			NEXT_ARG();
   1112 			od = *argv;
   1113 		} else if (strcmp(*argv, "iif") == 0) {
   1114 			NEXT_ARG();
   1115 			id = *argv;
   1116 		} else if (strcmp(*argv, "via") == 0) {
   1117 			NEXT_ARG();
   1118 			get_prefix(&filter.rvia, *argv, do_ipv6);
   1119 		} else if (strcmp(*argv, "src") == 0) {
   1120 			NEXT_ARG();
   1121 			get_prefix(&filter.rprefsrc, *argv, do_ipv6);
   1122 		} else if (matches(*argv, "realms") == 0) {
   1123 			__u32 realm;
   1124 			NEXT_ARG();
   1125 			if (get_rt_realms(&realm, *argv))
   1126 				invarg("invalid realms\n", *argv);
   1127 			filter.realm = realm;
   1128 			filter.realmmask = ~0U;
   1129 			if ((filter.realm&0xFFFF) == 0 &&
   1130 			    (*argv)[strlen(*argv) - 1] == '/')
   1131 				filter.realmmask &= ~0xFFFF;
   1132 			if ((filter.realm&0xFFFF0000U) == 0 &&
   1133 			    (strchr(*argv, '/') == NULL ||
   1134 			     (*argv)[0] == '/'))
   1135 				filter.realmmask &= ~0xFFFF0000U;
   1136 		} else if (matches(*argv, "from") == 0) {
   1137 			NEXT_ARG();
   1138 			if (matches(*argv, "root") == 0) {
   1139 				NEXT_ARG();
   1140 				get_prefix(&filter.rsrc, *argv, do_ipv6);
   1141 			} else if (matches(*argv, "match") == 0) {
   1142 				NEXT_ARG();
   1143 				get_prefix(&filter.msrc, *argv, do_ipv6);
   1144 			} else {
   1145 				if (matches(*argv, "exact") == 0) {
   1146 					NEXT_ARG();
   1147 				}
   1148 				get_prefix(&filter.msrc, *argv, do_ipv6);
   1149 				filter.rsrc = filter.msrc;
   1150 			}
   1151 		} else {
   1152 			if (matches(*argv, "to") == 0) {
   1153 				NEXT_ARG();
   1154 			}
   1155 			if (matches(*argv, "root") == 0) {
   1156 				NEXT_ARG();
   1157 				get_prefix(&filter.rdst, *argv, do_ipv6);
   1158 			} else if (matches(*argv, "match") == 0) {
   1159 				NEXT_ARG();
   1160 				get_prefix(&filter.mdst, *argv, do_ipv6);
   1161 			} else {
   1162 				if (matches(*argv, "exact") == 0) {
   1163 					NEXT_ARG();
   1164 				}
   1165 				get_prefix(&filter.mdst, *argv, do_ipv6);
   1166 				filter.rdst = filter.mdst;
   1167 			}
   1168 		}
   1169 		argc--; argv++;
   1170 	}
   1171 
   1172 	if (do_ipv6 == AF_UNSPEC && filter.tb)
   1173 		do_ipv6 = AF_INET;
   1174 
   1175 	ll_init_map(&rth);
   1176 
   1177 	if (id || od)  {
   1178 		int idx;
   1179 
   1180 		if (id) {
   1181 			if ((idx = ll_name_to_index(id)) == 0) {
   1182 				fprintf(stderr, "Cannot find device \"%s\"\n", id);
   1183 				return -1;
   1184 			}
   1185 			filter.iif = idx;
   1186 			filter.iifmask = -1;
   1187 		}
   1188 		if (od) {
   1189 			if ((idx = ll_name_to_index(od)) == 0) {
   1190 				fprintf(stderr, "Cannot find device \"%s\"\n", od);
   1191 				return -1;
   1192 			}
   1193 			filter.oif = idx;
   1194 			filter.oifmask = -1;
   1195 		}
   1196 	}
   1197 
   1198 	if (flush) {
   1199 		int round = 0;
   1200 		char flushb[4096-512];
   1201 		time_t start = time(0);
   1202 
   1203 		if (filter.cloned) {
   1204 			if (do_ipv6 != AF_INET6) {
   1205 				iproute_flush_cache();
   1206 				if (show_stats)
   1207 					printf("*** IPv4 routing cache is flushed.\n");
   1208 			}
   1209 			if (do_ipv6 == AF_INET)
   1210 				return 0;
   1211 		}
   1212 
   1213 		filter.flushb = flushb;
   1214 		filter.flushp = 0;
   1215 		filter.flushe = sizeof(flushb);
   1216 
   1217 		for (;;) {
   1218 			if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
   1219 				perror("Cannot send dump request");
   1220 				exit(1);
   1221 			}
   1222 			filter.flushed = 0;
   1223 			if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
   1224 				fprintf(stderr, "Flush terminated\n");
   1225 				exit(1);
   1226 			}
   1227 			if (filter.flushed == 0) {
   1228 				if (show_stats) {
   1229 					if (round == 0 && (!filter.cloned || do_ipv6 == AF_INET6))
   1230 						printf("Nothing to flush.\n");
   1231 					else
   1232 						printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
   1233 				}
   1234 				fflush(stdout);
   1235 				return 0;
   1236 			}
   1237 			round++;
   1238 			if (flush_update() < 0)
   1239 				exit(1);
   1240 
   1241 			if (time(0) - start > 30) {
   1242 				printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
   1243 				       time(0) - start, filter.flushed);
   1244 				exit(1);
   1245 			}
   1246 
   1247 			if (show_stats) {
   1248 				printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed);
   1249 				fflush(stdout);
   1250 			}
   1251 		}
   1252 	}
   1253 
   1254 	if (!filter.cloned) {
   1255 		if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
   1256 			perror("Cannot send dump request");
   1257 			exit(1);
   1258 		}
   1259 	} else {
   1260 		if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
   1261 			perror("Cannot send dump request");
   1262 			exit(1);
   1263 		}
   1264 	}
   1265 
   1266 	if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
   1267 		fprintf(stderr, "Dump terminated\n");
   1268 		exit(1);
   1269 	}
   1270 
   1271 	exit(0);
   1272 }
   1273 
   1274 
   1275 int iproute_get(int argc, char **argv)
   1276 {
   1277 	struct {
   1278 		struct nlmsghdr 	n;
   1279 		struct rtmsg 		r;
   1280 		char   			buf[1024];
   1281 	} req;
   1282 	char  *idev = NULL;
   1283 	char  *odev = NULL;
   1284 	int connected = 0;
   1285 	int from_ok = 0;
   1286 
   1287 	memset(&req, 0, sizeof(req));
   1288 
   1289 	iproute_reset_filter();
   1290 
   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;
   1298 	req.r.rtm_type = 0;
   1299 	req.r.rtm_src_len = 0;
   1300 	req.r.rtm_dst_len = 0;
   1301 	req.r.rtm_tos = 0;
   1302 
   1303 	while (argc > 0) {
   1304 		if (strcmp(*argv, "tos") == 0 ||
   1305 		    matches(*argv, "dsfield") == 0) {
   1306 			__u32 tos;
   1307 			NEXT_ARG();
   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) {
   1312 			inet_prefix addr;
   1313 			NEXT_ARG();
   1314 			if (matches(*argv, "help") == 0)
   1315 				usage();
   1316 			from_ok = 1;
   1317 			get_prefix(&addr, *argv, req.r.rtm_family);
   1318 			if (req.r.rtm_family == AF_UNSPEC)
   1319 				req.r.rtm_family = addr.family;
   1320 			if (addr.bytelen)
   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) {
   1324 			NEXT_ARG();
   1325 			idev = *argv;
   1326 		} else if (matches(*argv, "oif") == 0 ||
   1327 			   strcmp(*argv, "dev") == 0) {
   1328 			NEXT_ARG();
   1329 			odev = *argv;
   1330 		} else if (matches(*argv, "notify") == 0) {
   1331 			req.r.rtm_flags |= RTM_F_NOTIFY;
   1332 		} else if (matches(*argv, "connected") == 0) {
   1333 			connected = 1;
   1334 		} else {
   1335 			inet_prefix addr;
   1336 			if (strcmp(*argv, "to") == 0) {
   1337 				NEXT_ARG();
   1338 			}
   1339 			if (matches(*argv, "help") == 0)
   1340 				usage();
   1341 			get_prefix(&addr, *argv, req.r.rtm_family);
   1342 			if (req.r.rtm_family == AF_UNSPEC)
   1343 				req.r.rtm_family = addr.family;
   1344 			if (addr.bytelen)
   1345 				addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
   1346 			req.r.rtm_dst_len = addr.bitlen;
   1347 		}
   1348 		argc--; argv++;
   1349 	}
   1350 
   1351 	if (req.r.rtm_dst_len == 0) {
   1352 		fprintf(stderr, "need at least destination address\n");
   1353 		exit(1);
   1354 	}
   1355 
   1356 	ll_init_map(&rth);
   1357 
   1358 	if (idev || odev)  {
   1359 		int idx;
   1360 
   1361 		if (idev) {
   1362 			if ((idx = ll_name_to_index(idev)) == 0) {
   1363 				fprintf(stderr, "Cannot find device \"%s\"\n", idev);
   1364 				return -1;
   1365 			}
   1366 			addattr32(&req.n, sizeof(req), RTA_IIF, idx);
   1367 		}
   1368 		if (odev) {
   1369 			if ((idx = ll_name_to_index(odev)) == 0) {
   1370 				fprintf(stderr, "Cannot find device \"%s\"\n", odev);
   1371 				return -1;
   1372 			}
   1373 			addattr32(&req.n, sizeof(req), RTA_OIF, idx);
   1374 		}
   1375 	}
   1376 
   1377 	if (req.r.rtm_family == AF_UNSPEC)
   1378 		req.r.rtm_family = AF_INET;
   1379 
   1380 	if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
   1381 		exit(2);
   1382 
   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];
   1387 
   1388 		if (print_route(NULL, &req.n, (void*)stdout) < 0) {
   1389 			fprintf(stderr, "An error :-)\n");
   1390 			exit(1);
   1391 		}
   1392 
   1393 		if (req.n.nlmsg_type != RTM_NEWROUTE) {
   1394 			fprintf(stderr, "Not a route?\n");
   1395 			return -1;
   1396 		}
   1397 		len -= NLMSG_LENGTH(sizeof(*r));
   1398 		if (len < 0) {
   1399 			fprintf(stderr, "Wrong len %d\n", len);
   1400 			return -1;
   1401 		}
   1402 
   1403 		parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
   1404 
   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");
   1410 			return -1;
   1411 		}
   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;
   1420 
   1421 		if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
   1422 			exit(2);
   1423 	}
   1424 
   1425 	if (print_route(NULL, &req.n, (void*)stdout) < 0) {
   1426 		fprintf(stderr, "An error :-)\n");
   1427 		exit(1);
   1428 	}
   1429 
   1430 	exit(0);
   1431 }
   1432 
   1433 void iproute_reset_filter()
   1434 {
   1435 	memset(&filter, 0, sizeof(filter));
   1436 	filter.mdst.bitlen = -1;
   1437 	filter.msrc.bitlen = -1;
   1438 }
   1439 
   1440 int do_iproute(int argc, char **argv)
   1441 {
   1442 	if (argc < 1)
   1443 		return iproute_list_or_flush(0, NULL, 0);
   1444 
   1445 	if (matches(*argv, "add") == 0)
   1446 		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL,
   1447 				      argc-1, argv+1);
   1448 	if (matches(*argv, "change") == 0 || strcmp(*argv, "chg") == 0)
   1449 		return iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE,
   1450 				      argc-1, argv+1);
   1451 	if (matches(*argv, "replace") == 0)
   1452 		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
   1453 				      argc-1, argv+1);
   1454 	if (matches(*argv, "prepend") == 0)
   1455 		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE,
   1456 				      argc-1, argv+1);
   1457 	if (matches(*argv, "append") == 0)
   1458 		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND,
   1459 				      argc-1, argv+1);
   1460 	if (matches(*argv, "test") == 0)
   1461 		return iproute_modify(RTM_NEWROUTE, NLM_F_EXCL,
   1462 				      argc-1, argv+1);
   1463 	if (matches(*argv, "delete") == 0)
   1464 		return iproute_modify(RTM_DELROUTE, 0,
   1465 				      argc-1, argv+1);
   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)
   1474 		usage();
   1475 	fprintf(stderr, "Command \"%s\" is unknown, try \"ip route help\".\n", *argv);
   1476 	exit(-1);
   1477 }
   1478 
   1479