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 };
     50 static void usage(void) __attribute__((noreturn));
     51 
     52 static void usage(void)
     53 {
     54 	fprintf(stderr, "Usage: ip route { list | flush } SELECTOR\n");
     55 	fprintf(stderr, "       ip route get ADDRESS [ from ADDRESS iif STRING ]\n");
     56 	fprintf(stderr, "                            [ oif STRING ]  [ tos TOS ]\n");
     57 	fprintf(stderr, "       ip route { add | del | change | append | replace | monitor } ROUTE\n");
     58 	fprintf(stderr, "SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ]\n");
     59 	fprintf(stderr, "            [ table TABLE_ID ] [ proto RTPROTO ]\n");
     60 	fprintf(stderr, "            [ type TYPE ] [ scope SCOPE ]\n");
     61 	fprintf(stderr, "ROUTE := NODE_SPEC [ INFO_SPEC ]\n");
     62 	fprintf(stderr, "NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ]\n");
     63 	fprintf(stderr, "             [ table TABLE_ID ] [ proto RTPROTO ]\n");
     64 	fprintf(stderr, "             [ scope SCOPE ] [ metric METRIC ]\n");
     65 	fprintf(stderr, "INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ]...\n");
     66 	fprintf(stderr, "NH := [ via ADDRESS ] [ dev STRING ] [ weight NUMBER ] NHFLAGS\n");
     67 	fprintf(stderr, "OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ]\n");
     68 	fprintf(stderr, "           [ rtt TIME ] [ rttvar TIME ] [reordering NUMBER ]\n");
     69 	fprintf(stderr, "           [ window NUMBER] [ cwnd NUMBER ] [ initcwnd NUMBER ]\n");
     70 	fprintf(stderr, "           [ ssthresh NUMBER ] [ realms REALM ] [ src ADDRESS ]\n");
     71 	fprintf(stderr, "           [ rto_min TIME ] [ hoplimit NUMBER ] \n");
     72 	fprintf(stderr, "TYPE := [ unicast | local | broadcast | multicast | throw |\n");
     73 	fprintf(stderr, "          unreachable | prohibit | blackhole | nat ]\n");
     74 	fprintf(stderr, "TABLE_ID := [ local | main | default | all | NUMBER ]\n");
     75 	fprintf(stderr, "SCOPE := [ host | link | global | NUMBER ]\n");
     76 	fprintf(stderr, "FLAGS := [ equalize ]\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 (r->rtm_family == AF_INET6 && !ip6_multiple_tables) {
    164 		if (filter.cloned) {
    165 			if (!(r->rtm_flags&RTM_F_CLONED))
    166 				return 0;
    167 		}
    168 		if (filter.tb) {
    169 			if (!filter.cloned && r->rtm_flags&RTM_F_CLONED)
    170 				return 0;
    171 			if (filter.tb == RT_TABLE_LOCAL) {
    172 				if (r->rtm_type != RTN_LOCAL)
    173 					return 0;
    174 			} else if (filter.tb == RT_TABLE_MAIN) {
    175 				if (r->rtm_type == RTN_LOCAL)
    176 					return 0;
    177 			} else {
    178 				return 0;
    179 			}
    180 		}
    181 	} else {
    182 		if (filter.cloned) {
    183 			if (!(r->rtm_flags&RTM_F_CLONED))
    184 				return 0;
    185 		}
    186 		if (filter.tb > 0 && filter.tb != table)
    187 			return 0;
    188 	}
    189 	if ((filter.protocol^r->rtm_protocol)&filter.protocolmask)
    190 		return 0;
    191 	if ((filter.scope^r->rtm_scope)&filter.scopemask)
    192 		return 0;
    193 	if ((filter.type^r->rtm_type)&filter.typemask)
    194 		return 0;
    195 	if ((filter.tos^r->rtm_tos)&filter.tosmask)
    196 		return 0;
    197 	if (filter.rdst.family &&
    198 	    (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len))
    199 		return 0;
    200 	if (filter.mdst.family &&
    201 	    (r->rtm_family != filter.mdst.family ||
    202 	     (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len)))
    203 		return 0;
    204 	if (filter.rsrc.family &&
    205 	    (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len))
    206 		return 0;
    207 	if (filter.msrc.family &&
    208 	    (r->rtm_family != filter.msrc.family ||
    209 	     (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len)))
    210 		return 0;
    211 	if (filter.rvia.family && r->rtm_family != filter.rvia.family)
    212 		return 0;
    213 	if (filter.rprefsrc.family && r->rtm_family != filter.rprefsrc.family)
    214 		return 0;
    215 
    216 	memset(&dst, 0, sizeof(dst));
    217 	dst.family = r->rtm_family;
    218 	if (tb[RTA_DST])
    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;
    223 		if (tb[RTA_SRC])
    224 			memcpy(&src.data, RTA_DATA(tb[RTA_SRC]), (r->rtm_src_len+7)/8);
    225 	}
    226 	if (filter.rvia.bitlen>0) {
    227 		memset(&via, 0, sizeof(via));
    228 		via.family = r->rtm_family;
    229 		if (tb[RTA_GATEWAY])
    230 			memcpy(&via.data, RTA_DATA(tb[RTA_GATEWAY]), host_len/8);
    231 	}
    232 	if (filter.rprefsrc.bitlen>0) {
    233 		memset(&prefsrc, 0, sizeof(prefsrc));
    234 		prefsrc.family = r->rtm_family;
    235 		if (tb[RTA_PREFSRC])
    236 			memcpy(&prefsrc.data, RTA_DATA(tb[RTA_PREFSRC]), host_len/8);
    237 	}
    238 
    239 	if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
    240 		return 0;
    241 	if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
    242 	    inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
    243 		return 0;
    244 
    245 	if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
    246 		return 0;
    247 	if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
    248 	    inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
    249 		return 0;
    250 
    251 	if (filter.rvia.family && inet_addr_match(&via, &filter.rvia, filter.rvia.bitlen))
    252 		return 0;
    253 	if (filter.rprefsrc.family && inet_addr_match(&prefsrc, &filter.rprefsrc, filter.rprefsrc.bitlen))
    254 		return 0;
    255 	if (filter.realmmask) {
    256 		__u32 realms = 0;
    257 		if (tb[RTA_FLOW])
    258 			realms = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
    259 		if ((realms^filter.realm)&filter.realmmask)
    260 			return 0;
    261 	}
    262 	if (filter.iifmask) {
    263 		int iif = 0;
    264 		if (tb[RTA_IIF])
    265 			iif = *(int*)RTA_DATA(tb[RTA_IIF]);
    266 		if ((iif^filter.iif)&filter.iifmask)
    267 			return 0;
    268 	}
    269 	if (filter.oifmask) {
    270 		int oif = 0;
    271 		if (tb[RTA_OIF])
    272 			oif = *(int*)RTA_DATA(tb[RTA_OIF]);
    273 		if ((oif^filter.oif)&filter.oifmask)
    274 			return 0;
    275 	}
    276 	if (filter.flushb &&
    277 	    r->rtm_family == AF_INET6 &&
    278 	    r->rtm_dst_len == 0 &&
    279 	    r->rtm_type == RTN_UNREACHABLE &&
    280 	    tb[RTA_PRIORITY] &&
    281 	    *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
    282 		return 0;
    283 
    284 	if (filter.flushb) {
    285 		struct nlmsghdr *fn;
    286 		if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
    287 			if (flush_update())
    288 				return -1;
    289 		}
    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;
    296 		filter.flushed++;
    297 		if (show_stats < 2)
    298 			return 0;
    299 	}
    300 
    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)));
    305 
    306 	if (tb[RTA_DST]) {
    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]),
    311 							 abuf, sizeof(abuf)),
    312 				r->rtm_dst_len
    313 				);
    314 		} else {
    315 			fprintf(fp, "%s ", format_host(r->rtm_family,
    316 						       RTA_PAYLOAD(tb[RTA_DST]),
    317 						       RTA_DATA(tb[RTA_DST]),
    318 						       abuf, sizeof(abuf))
    319 				);
    320 		}
    321 	} else if (r->rtm_dst_len) {
    322 		fprintf(fp, "0/%d ", r->rtm_dst_len);
    323 	} else {
    324 		fprintf(fp, "default ");
    325 	}
    326 	if (tb[RTA_SRC]) {
    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]),
    331 							 abuf, sizeof(abuf)),
    332 				r->rtm_src_len
    333 				);
    334 		} else {
    335 			fprintf(fp, "from %s ", format_host(r->rtm_family,
    336 						       RTA_PAYLOAD(tb[RTA_SRC]),
    337 						       RTA_DATA(tb[RTA_SRC]),
    338 						       abuf, sizeof(abuf))
    339 				);
    340 		}
    341 	} else if (r->rtm_src_len) {
    342 		fprintf(fp, "from 0/%u ", r->rtm_src_len);
    343 	}
    344 	if (r->rtm_tos && filter.tosmask != -1) {
    345 		SPRINT_BUF(b1);
    346 		fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
    347 	}
    348 
    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)));
    355 	}
    356 	if (tb[RTA_OIF] && filter.oifmask != -1)
    357 		fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
    358 
    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)));
    366 	}
    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.
    370 		 */
    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)));
    376 	}
    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_EQUALIZE)
    386 		fprintf(fp, "equalize ");
    387 	if (r->rtm_flags & RTM_F_NOTIFY)
    388 		fprintf(fp, "notify ");
    389 
    390 	if (tb[RTA_FLOW] && filter.realmmask != ~0U) {
    391 		__u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
    392 		__u32 from = to>>16;
    393 		to &= 0xFFFF;
    394 		fprintf(fp, "realm%s ", from ? "s" : "");
    395 		if (from) {
    396 			fprintf(fp, "%s/",
    397 				rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
    398 		}
    399 		fprintf(fp, "%s ",
    400 			rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
    401 	}
    402 	if ((r->rtm_flags&RTM_F_CLONED) && r->rtm_family == AF_INET) {
    403 		__u32 flags = r->rtm_flags&~0xFFFF;
    404 		int first = 1;
    405 
    406 		fprintf(fp, "%s    cache ", _SL_);
    407 
    408 #define PRTFL(fl,flname) if (flags&RTCF_##fl) { \
    409   flags &= ~RTCF_##fl; \
    410   fprintf(fp, "%s" flname "%s", first ? "<" : "", flags ? "," : "> "); \
    411   first = 0; }
    412 		PRTFL(LOCAL, "local");
    413 		PRTFL(REJECT, "reject");
    414 		PRTFL(MULTICAST, "mc");
    415 		PRTFL(BROADCAST, "brd");
    416 		PRTFL(DNAT, "dst-nat");
    417 		PRTFL(SNAT, "src-nat");
    418 		PRTFL(MASQ, "masq");
    419 		PRTFL(DIRECTDST, "dst-direct");
    420 		PRTFL(DIRECTSRC, "src-direct");
    421 		PRTFL(REDIRECTED, "redirected");
    422 		PRTFL(DOREDIRECT, "redirect");
    423 		PRTFL(FAST, "fastroute");
    424 		PRTFL(NOTIFY, "notify");
    425 		PRTFL(TPROXY, "proxy");
    426 #ifdef RTCF_EQUALIZE
    427 		PRTFL(EQUALIZE, "equalize");
    428 #endif
    429 		if (flags)
    430 			fprintf(fp, "%s%x> ", first ? "<" : "", flags);
    431 		if (tb[RTA_CACHEINFO]) {
    432 			struct rta_cacheinfo *ci = RTA_DATA(tb[RTA_CACHEINFO]);
    433 			if (!hz)
    434 				hz = get_user_hz();
    435 			if (ci->rta_expires != 0)
    436 				fprintf(fp, " expires %dsec", ci->rta_expires/hz);
    437 			if (ci->rta_error != 0)
    438 				fprintf(fp, " error %d", ci->rta_error);
    439 			if (show_stats) {
    440 				if (ci->rta_clntref)
    441 					fprintf(fp, " users %d", ci->rta_clntref);
    442 				if (ci->rta_used != 0)
    443 					fprintf(fp, " used %d", ci->rta_used);
    444 				if (ci->rta_lastuse != 0)
    445 					fprintf(fp, " age %dsec", ci->rta_lastuse/hz);
    446 			}
    447 #ifdef RTNETLINK_HAVE_PEERINFO
    448 			if (ci->rta_id)
    449 				fprintf(fp, " ipid 0x%04x", ci->rta_id);
    450 			if (ci->rta_ts || ci->rta_tsage)
    451 				fprintf(fp, " ts 0x%x tsage %dsec", ci->rta_ts, ci->rta_tsage);
    452 #endif
    453 		}
    454 	} else if (r->rtm_family == AF_INET6) {
    455 		struct rta_cacheinfo *ci = NULL;
    456 		if (tb[RTA_CACHEINFO])
    457 			ci = RTA_DATA(tb[RTA_CACHEINFO]);
    458 		if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
    459 			if (!hz)
    460 				hz = get_user_hz();
    461 			if (r->rtm_flags & RTM_F_CLONED)
    462 				fprintf(fp, "%s    cache ", _SL_);
    463 			if (ci->rta_expires)
    464 				fprintf(fp, " expires %dsec", ci->rta_expires/hz);
    465 			if (ci->rta_error != 0)
    466 				fprintf(fp, " error %d", ci->rta_error);
    467 			if (show_stats) {
    468 				if (ci->rta_clntref)
    469 					fprintf(fp, " users %d", ci->rta_clntref);
    470 				if (ci->rta_used != 0)
    471 					fprintf(fp, " used %d", ci->rta_used);
    472 				if (ci->rta_lastuse != 0)
    473 					fprintf(fp, " age %dsec", ci->rta_lastuse/hz);
    474 			}
    475 		} else if (ci) {
    476 			if (ci->rta_error != 0)
    477 				fprintf(fp, " error %d", ci->rta_error);
    478 		}
    479 	}
    480 	if (tb[RTA_METRICS]) {
    481 		int i;
    482 		unsigned mxlock = 0;
    483 		struct rtattr *mxrta[RTAX_MAX+1];
    484 
    485 		parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
    486 			    RTA_PAYLOAD(tb[RTA_METRICS]));
    487 		if (mxrta[RTAX_LOCK])
    488 			mxlock = *(unsigned*)RTA_DATA(mxrta[RTAX_LOCK]);
    489 
    490 		for (i=2; i<= RTAX_MAX; i++) {
    491 			unsigned val;
    492 
    493 			if (mxrta[i] == NULL)
    494 				continue;
    495 			if (!hz)
    496 				hz = get_hz();
    497 
    498 			if (i < sizeof(mx_names)/sizeof(char*) && mx_names[i])
    499 				fprintf(fp, " %s", mx_names[i]);
    500 			else
    501 				fprintf(fp, " metric %d", i);
    502 			if (mxlock & (1<<i))
    503 				fprintf(fp, " lock");
    504 
    505 			val = *(unsigned*)RTA_DATA(mxrta[i]);
    506 			switch (i) {
    507 			case RTAX_HOPLIMIT:
    508 				if ((long)val == -1)
    509 					val = 0;
    510 				/* fall through */
    511 			default:
    512 				fprintf(fp, " %u", val);
    513 				break;
    514 
    515 			case RTAX_RTT:
    516 			case RTAX_RTTVAR:
    517 			case RTAX_RTO_MIN:
    518 				val *= 1000;
    519 				if (i == RTAX_RTT)
    520 					val /= 8;
    521 				else if (i == RTAX_RTTVAR)
    522 					val /= 4;
    523 
    524 				if (val >= hz)
    525 					fprintf(fp, " %llums",
    526 						(unsigned long long) val / hz);
    527 				else
    528 					fprintf(fp, " %.2fms",
    529 						(double)val / hz);
    530 			}
    531 		}
    532 	}
    533 	if (tb[RTA_IIF] && filter.iifmask != -1) {
    534 		fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
    535 	}
    536 	if (tb[RTA_MULTIPATH]) {
    537 		struct rtnexthop *nh = RTA_DATA(tb[RTA_MULTIPATH]);
    538 		int first = 0;
    539 
    540 		len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
    541 
    542 		for (;;) {
    543 			if (len < sizeof(*nh))
    544 				break;
    545 			if (nh->rtnh_len > len)
    546 				break;
    547 			if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) {
    548 				if (first)
    549 					fprintf(fp, " Oifs:");
    550 				else
    551 					fprintf(fp, " ");
    552 			} else
    553 				fprintf(fp, "%s\tnexthop", _SL_);
    554 			if (nh->rtnh_len > sizeof(*nh)) {
    555 				parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh), nh->rtnh_len - sizeof(*nh));
    556 				if (tb[RTA_GATEWAY]) {
    557 					fprintf(fp, " via %s ",
    558 						format_host(r->rtm_family,
    559 							    RTA_PAYLOAD(tb[RTA_GATEWAY]),
    560 							    RTA_DATA(tb[RTA_GATEWAY]),
    561 							    abuf, sizeof(abuf)));
    562 				}
    563 				if (tb[RTA_FLOW]) {
    564 					__u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
    565 					__u32 from = to>>16;
    566 					to &= 0xFFFF;
    567 					fprintf(fp, " realm%s ", from ? "s" : "");
    568 					if (from) {
    569 						fprintf(fp, "%s/",
    570 							rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
    571 					}
    572 					fprintf(fp, "%s",
    573 						rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
    574 				}
    575 			}
    576 			if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) {
    577 				fprintf(fp, " %s", ll_index_to_name(nh->rtnh_ifindex));
    578 				if (nh->rtnh_hops != 1)
    579 					fprintf(fp, "(ttl>%d)", nh->rtnh_hops);
    580 			} else {
    581 				fprintf(fp, " dev %s", ll_index_to_name(nh->rtnh_ifindex));
    582 				fprintf(fp, " weight %d", nh->rtnh_hops+1);
    583 			}
    584 			if (nh->rtnh_flags & RTNH_F_DEAD)
    585 				fprintf(fp, " dead");
    586 			if (nh->rtnh_flags & RTNH_F_ONLINK)
    587 				fprintf(fp, " onlink");
    588 			if (nh->rtnh_flags & RTNH_F_PERVASIVE)
    589 				fprintf(fp, " pervasive");
    590 			len -= NLMSG_ALIGN(nh->rtnh_len);
    591 			nh = RTNH_NEXT(nh);
    592 		}
    593 	}
    594 	fprintf(fp, "\n");
    595 	fflush(fp);
    596 	return 0;
    597 }
    598 
    599 
    600 int parse_one_nh(struct rtattr *rta, struct rtnexthop *rtnh, int *argcp, char ***argvp)
    601 {
    602 	int argc = *argcp;
    603 	char **argv = *argvp;
    604 
    605 	while (++argv, --argc > 0) {
    606 		if (strcmp(*argv, "via") == 0) {
    607 			NEXT_ARG();
    608 			rta_addattr32(rta, 4096, RTA_GATEWAY, get_addr32(*argv));
    609 			rtnh->rtnh_len += sizeof(struct rtattr) + 4;
    610 		} else if (strcmp(*argv, "dev") == 0) {
    611 			NEXT_ARG();
    612 			if ((rtnh->rtnh_ifindex = ll_name_to_index(*argv)) == 0) {
    613 				fprintf(stderr, "Cannot find device \"%s\"\n", *argv);
    614 				exit(1);
    615 			}
    616 		} else if (strcmp(*argv, "weight") == 0) {
    617 			unsigned w;
    618 			NEXT_ARG();
    619 			if (get_unsigned(&w, *argv, 0) || w == 0 || w > 256)
    620 				invarg("\"weight\" is invalid\n", *argv);
    621 			rtnh->rtnh_hops = w - 1;
    622 		} else if (strcmp(*argv, "onlink") == 0) {
    623 			rtnh->rtnh_flags |= RTNH_F_ONLINK;
    624 		} else if (matches(*argv, "realms") == 0) {
    625 			__u32 realm;
    626 			NEXT_ARG();
    627 			if (get_rt_realms(&realm, *argv))
    628 				invarg("\"realm\" value is invalid\n", *argv);
    629 			rta_addattr32(rta, 4096, RTA_FLOW, realm);
    630 			rtnh->rtnh_len += sizeof(struct rtattr) + 4;
    631 		} else
    632 			break;
    633 	}
    634 	*argcp = argc;
    635 	*argvp = argv;
    636 	return 0;
    637 }
    638 
    639 int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r, int argc, char **argv)
    640 {
    641 	char buf[1024];
    642 	struct rtattr *rta = (void*)buf;
    643 	struct rtnexthop *rtnh;
    644 
    645 	rta->rta_type = RTA_MULTIPATH;
    646 	rta->rta_len = RTA_LENGTH(0);
    647 	rtnh = RTA_DATA(rta);
    648 
    649 	while (argc > 0) {
    650 		if (strcmp(*argv, "nexthop") != 0) {
    651 			fprintf(stderr, "Error: \"nexthop\" or end of line is expected instead of \"%s\"\n", *argv);
    652 			exit(-1);
    653 		}
    654 		if (argc <= 1) {
    655 			fprintf(stderr, "Error: unexpected end of line after \"nexthop\"\n");
    656 			exit(-1);
    657 		}
    658 		memset(rtnh, 0, sizeof(*rtnh));
    659 		rtnh->rtnh_len = sizeof(*rtnh);
    660 		rta->rta_len += rtnh->rtnh_len;
    661 		parse_one_nh(rta, rtnh, &argc, &argv);
    662 		rtnh = RTNH_NEXT(rtnh);
    663 	}
    664 
    665 	if (rta->rta_len > RTA_LENGTH(0))
    666 		addattr_l(n, 1024, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta));
    667 	return 0;
    668 }
    669 
    670 
    671 int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
    672 {
    673 	struct {
    674 		struct nlmsghdr 	n;
    675 		struct rtmsg 		r;
    676 		char   			buf[1024];
    677 	} req;
    678 	char  mxbuf[256];
    679 	struct rtattr * mxrta = (void*)mxbuf;
    680 	unsigned mxlock = 0;
    681 	char  *d = NULL;
    682 	int gw_ok = 0;
    683 	int dst_ok = 0;
    684 	int nhs_ok = 0;
    685 	int scope_ok = 0;
    686 	int table_ok = 0;
    687 	int proto_ok = 0;
    688 	int type_ok = 0;
    689 	int raw = 0;
    690 
    691 	memset(&req, 0, sizeof(req));
    692 
    693 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
    694 	req.n.nlmsg_flags = NLM_F_REQUEST|flags;
    695 	req.n.nlmsg_type = cmd;
    696 	req.r.rtm_family = preferred_family;
    697 	req.r.rtm_table = RT_TABLE_MAIN;
    698 	req.r.rtm_scope = RT_SCOPE_NOWHERE;
    699 
    700 	if (cmd != RTM_DELROUTE) {
    701 		req.r.rtm_protocol = RTPROT_BOOT;
    702 		req.r.rtm_scope = RT_SCOPE_UNIVERSE;
    703 		req.r.rtm_type = RTN_UNICAST;
    704 	}
    705 
    706 	mxrta->rta_type = RTA_METRICS;
    707 	mxrta->rta_len = RTA_LENGTH(0);
    708 
    709 	while (argc > 0) {
    710 		if (strcmp(*argv, "src") == 0) {
    711 			inet_prefix addr;
    712 			NEXT_ARG();
    713 			get_addr(&addr, *argv, req.r.rtm_family);
    714 			if (req.r.rtm_family == AF_UNSPEC)
    715 				req.r.rtm_family = addr.family;
    716 			addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
    717 		} else if (strcmp(*argv, "via") == 0) {
    718 			inet_prefix addr;
    719 			gw_ok = 1;
    720 			NEXT_ARG();
    721 			get_addr(&addr, *argv, req.r.rtm_family);
    722 			if (req.r.rtm_family == AF_UNSPEC)
    723 				req.r.rtm_family = addr.family;
    724 			addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
    725 		} else if (strcmp(*argv, "from") == 0) {
    726 			inet_prefix addr;
    727 			NEXT_ARG();
    728 			get_prefix(&addr, *argv, req.r.rtm_family);
    729 			if (req.r.rtm_family == AF_UNSPEC)
    730 				req.r.rtm_family = addr.family;
    731 			if (addr.bytelen)
    732 				addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
    733 			req.r.rtm_src_len = addr.bitlen;
    734 		} else if (strcmp(*argv, "tos") == 0 ||
    735 			   matches(*argv, "dsfield") == 0) {
    736 			__u32 tos;
    737 			NEXT_ARG();
    738 			if (rtnl_dsfield_a2n(&tos, *argv))
    739 				invarg("\"tos\" value is invalid\n", *argv);
    740 			req.r.rtm_tos = tos;
    741 		} else if (matches(*argv, "metric") == 0 ||
    742 			   matches(*argv, "priority") == 0 ||
    743 			   matches(*argv, "preference") == 0) {
    744 			__u32 metric;
    745 			NEXT_ARG();
    746 			if (get_u32(&metric, *argv, 0))
    747 				invarg("\"metric\" value is invalid\n", *argv);
    748 			addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
    749 		} else if (strcmp(*argv, "scope") == 0) {
    750 			__u32 scope = 0;
    751 			NEXT_ARG();
    752 			if (rtnl_rtscope_a2n(&scope, *argv))
    753 				invarg("invalid \"scope\" value\n", *argv);
    754 			req.r.rtm_scope = scope;
    755 			scope_ok = 1;
    756 		} else if (strcmp(*argv, "mtu") == 0) {
    757 			unsigned mtu;
    758 			NEXT_ARG();
    759 			if (strcmp(*argv, "lock") == 0) {
    760 				mxlock |= (1<<RTAX_MTU);
    761 				NEXT_ARG();
    762 			}
    763 			if (get_unsigned(&mtu, *argv, 0))
    764 				invarg("\"mtu\" value is invalid\n", *argv);
    765 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
    766 #ifdef RTAX_HOPLIMIT
    767 		} else if (strcmp(*argv, "hoplimit") == 0) {
    768 			unsigned hoplimit;
    769 			NEXT_ARG();
    770 			if (strcmp(*argv, "lock") == 0) {
    771 				mxlock |= (1<<RTAX_HOPLIMIT);
    772 				NEXT_ARG();
    773 			}
    774 			if (get_unsigned(&hoplimit, *argv, 0))
    775 				invarg("\"hoplimit\" value is invalid\n", *argv);
    776 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit);
    777 #endif
    778 #ifdef RTAX_ADVMSS
    779 		} else if (strcmp(*argv, "advmss") == 0) {
    780 			unsigned mss;
    781 			NEXT_ARG();
    782 			if (strcmp(*argv, "lock") == 0) {
    783 				mxlock |= (1<<RTAX_ADVMSS);
    784 				NEXT_ARG();
    785 			}
    786 			if (get_unsigned(&mss, *argv, 0))
    787 				invarg("\"mss\" value is invalid\n", *argv);
    788 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss);
    789 #endif
    790 #ifdef RTAX_REORDERING
    791 		} else if (matches(*argv, "reordering") == 0) {
    792 			unsigned reord;
    793 			NEXT_ARG();
    794 			if (strcmp(*argv, "lock") == 0) {
    795 				mxlock |= (1<<RTAX_REORDERING);
    796 				NEXT_ARG();
    797 			}
    798 			if (get_unsigned(&reord, *argv, 0))
    799 				invarg("\"reordering\" value is invalid\n", *argv);
    800 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord);
    801 #endif
    802 		} else if (strcmp(*argv, "rtt") == 0) {
    803 			unsigned rtt;
    804 			NEXT_ARG();
    805 			if (strcmp(*argv, "lock") == 0) {
    806 				mxlock |= (1<<RTAX_RTT);
    807 				NEXT_ARG();
    808 			}
    809 			if (get_jiffies(&rtt, *argv, 0, &raw))
    810 				invarg("\"rtt\" value is invalid\n", *argv);
    811 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT,
    812 				(raw) ? rtt : rtt * 8);
    813 		} else if (strcmp(*argv, "rto_min") == 0) {
    814 			unsigned rto_min;
    815 			NEXT_ARG();
    816 			mxlock |= (1<<RTAX_RTO_MIN);
    817 			if (get_jiffies(&rto_min, *argv, 0, &raw))
    818 				invarg("\"rto_min\" value is invalid\n",
    819 				       *argv);
    820 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN,
    821 				      rto_min);
    822 		} else if (matches(*argv, "window") == 0) {
    823 			unsigned win;
    824 			NEXT_ARG();
    825 			if (strcmp(*argv, "lock") == 0) {
    826 				mxlock |= (1<<RTAX_WINDOW);
    827 				NEXT_ARG();
    828 			}
    829 			if (get_unsigned(&win, *argv, 0))
    830 				invarg("\"window\" value is invalid\n", *argv);
    831 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win);
    832 		} else if (matches(*argv, "cwnd") == 0) {
    833 			unsigned win;
    834 			NEXT_ARG();
    835 			if (strcmp(*argv, "lock") == 0) {
    836 				mxlock |= (1<<RTAX_CWND);
    837 				NEXT_ARG();
    838 			}
    839 			if (get_unsigned(&win, *argv, 0))
    840 				invarg("\"cwnd\" value is invalid\n", *argv);
    841 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win);
    842 		} else if (matches(*argv, "initcwnd") == 0) {
    843 			unsigned win;
    844 			NEXT_ARG();
    845 			if (strcmp(*argv, "lock") == 0) {
    846 				mxlock |= (1<<RTAX_INITCWND);
    847 				NEXT_ARG();
    848 			}
    849 			if (get_unsigned(&win, *argv, 0))
    850 				invarg("\"initcwnd\" value is invalid\n", *argv);
    851 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITCWND, win);
    852 		} else if (matches(*argv, "rttvar") == 0) {
    853 			unsigned win;
    854 			NEXT_ARG();
    855 			if (strcmp(*argv, "lock") == 0) {
    856 				mxlock |= (1<<RTAX_RTTVAR);
    857 				NEXT_ARG();
    858 			}
    859 			if (get_jiffies(&win, *argv, 0, &raw))
    860 				invarg("\"rttvar\" value is invalid\n", *argv);
    861 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR,
    862 				(raw) ? win : win * 4);
    863 		} else if (matches(*argv, "ssthresh") == 0) {
    864 			unsigned win;
    865 			NEXT_ARG();
    866 			if (strcmp(*argv, "lock") == 0) {
    867 				mxlock |= (1<<RTAX_SSTHRESH);
    868 				NEXT_ARG();
    869 			}
    870 			if (get_unsigned(&win, *argv, 0))
    871 				invarg("\"ssthresh\" value is invalid\n", *argv);
    872 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win);
    873 		} else if (matches(*argv, "realms") == 0) {
    874 			__u32 realm;
    875 			NEXT_ARG();
    876 			if (get_rt_realms(&realm, *argv))
    877 				invarg("\"realm\" value is invalid\n", *argv);
    878 			addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
    879 		} else if (strcmp(*argv, "onlink") == 0) {
    880 			req.r.rtm_flags |= RTNH_F_ONLINK;
    881 		} else if (matches(*argv, "equalize") == 0 ||
    882 			   strcmp(*argv, "eql") == 0) {
    883 			req.r.rtm_flags |= RTM_F_EQUALIZE;
    884 		} else if (strcmp(*argv, "nexthop") == 0) {
    885 			nhs_ok = 1;
    886 			break;
    887 		} else if (matches(*argv, "protocol") == 0) {
    888 			__u32 prot;
    889 			NEXT_ARG();
    890 			if (rtnl_rtprot_a2n(&prot, *argv))
    891 				invarg("\"protocol\" value is invalid\n", *argv);
    892 			req.r.rtm_protocol = prot;
    893 			proto_ok =1;
    894 		} else if (matches(*argv, "table") == 0) {
    895 			__u32 tid;
    896 			NEXT_ARG();
    897 			if (rtnl_rttable_a2n(&tid, *argv))
    898 				invarg("\"table\" value is invalid\n", *argv);
    899 			if (tid < 256)
    900 				req.r.rtm_table = tid;
    901 			else {
    902 				req.r.rtm_table = RT_TABLE_UNSPEC;
    903 				addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
    904 			}
    905 			table_ok = 1;
    906 		} else if (strcmp(*argv, "dev") == 0 ||
    907 			   strcmp(*argv, "oif") == 0) {
    908 			NEXT_ARG();
    909 			d = *argv;
    910 		} else {
    911 			int type;
    912 			inet_prefix dst;
    913 
    914 			if (strcmp(*argv, "to") == 0) {
    915 				NEXT_ARG();
    916 			}
    917 			if ((**argv < '0' || **argv > '9') &&
    918 			    rtnl_rtntype_a2n(&type, *argv) == 0) {
    919 				NEXT_ARG();
    920 				req.r.rtm_type = type;
    921 				type_ok = 1;
    922 			}
    923 
    924 			if (matches(*argv, "help") == 0)
    925 				usage();
    926 			if (dst_ok)
    927 				duparg2("to", *argv);
    928 			get_prefix(&dst, *argv, req.r.rtm_family);
    929 			if (req.r.rtm_family == AF_UNSPEC)
    930 				req.r.rtm_family = dst.family;
    931 			req.r.rtm_dst_len = dst.bitlen;
    932 			dst_ok = 1;
    933 			if (dst.bytelen)
    934 				addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
    935 		}
    936 		argc--; argv++;
    937 	}
    938 
    939 	if (d || nhs_ok)  {
    940 		int idx;
    941 
    942 		ll_init_map(&rth);
    943 
    944 		if (d) {
    945 			if ((idx = ll_name_to_index(d)) == 0) {
    946 				fprintf(stderr, "Cannot find device \"%s\"\n", d);
    947 				return -1;
    948 			}
    949 			addattr32(&req.n, sizeof(req), RTA_OIF, idx);
    950 		}
    951 	}
    952 
    953 	if (mxrta->rta_len > RTA_LENGTH(0)) {
    954 		if (mxlock)
    955 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
    956 		addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
    957 	}
    958 
    959 	if (nhs_ok)
    960 		parse_nexthops(&req.n, &req.r, argc, argv);
    961 
    962 	if (!table_ok) {
    963 		if (req.r.rtm_type == RTN_LOCAL ||
    964 		    req.r.rtm_type == RTN_BROADCAST ||
    965 		    req.r.rtm_type == RTN_NAT ||
    966 		    req.r.rtm_type == RTN_ANYCAST)
    967 			req.r.rtm_table = RT_TABLE_LOCAL;
    968 	}
    969 	if (!scope_ok) {
    970 		if (req.r.rtm_type == RTN_LOCAL ||
    971 		    req.r.rtm_type == RTN_NAT)
    972 			req.r.rtm_scope = RT_SCOPE_HOST;
    973 		else if (req.r.rtm_type == RTN_BROADCAST ||
    974 			 req.r.rtm_type == RTN_MULTICAST ||
    975 			 req.r.rtm_type == RTN_ANYCAST)
    976 			req.r.rtm_scope = RT_SCOPE_LINK;
    977 		else if (req.r.rtm_type == RTN_UNICAST ||
    978 			 req.r.rtm_type == RTN_UNSPEC) {
    979 			if (cmd == RTM_DELROUTE)
    980 				req.r.rtm_scope = RT_SCOPE_NOWHERE;
    981 			else if (!gw_ok && !nhs_ok)
    982 				req.r.rtm_scope = RT_SCOPE_LINK;
    983 		}
    984 	}
    985 
    986 	if (req.r.rtm_family == AF_UNSPEC)
    987 		req.r.rtm_family = AF_INET;
    988 
    989 	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
    990 		exit(2);
    991 
    992 	return 0;
    993 }
    994 
    995 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
    996 {
    997 	struct {
    998 		struct nlmsghdr nlh;
    999 		struct rtmsg rtm;
   1000 	} req;
   1001 	struct sockaddr_nl nladdr;
   1002 
   1003 	memset(&nladdr, 0, sizeof(nladdr));
   1004 	memset(&req, 0, sizeof(req));
   1005 	nladdr.nl_family = AF_NETLINK;
   1006 
   1007 	req.nlh.nlmsg_len = sizeof(req);
   1008 	req.nlh.nlmsg_type = RTM_GETROUTE;
   1009 	req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
   1010 	req.nlh.nlmsg_pid = 0;
   1011 	req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
   1012 	req.rtm.rtm_family = family;
   1013 	req.rtm.rtm_flags |= RTM_F_CLONED;
   1014 
   1015 	return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
   1016 }
   1017 
   1018 static int iproute_flush_cache(void)
   1019 {
   1020 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
   1021 
   1022 	int len;
   1023 	int flush_fd = open (ROUTE_FLUSH_PATH, O_WRONLY);
   1024 	char *buffer = "-1";
   1025 
   1026 	if (flush_fd < 0) {
   1027 		fprintf (stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH);
   1028 		return -1;
   1029 	}
   1030 
   1031 	len = strlen (buffer);
   1032 
   1033 	if ((write (flush_fd, (void *)buffer, len)) < len) {
   1034 		fprintf (stderr, "Cannot flush routing cache\n");
   1035 		return -1;
   1036 	}
   1037 	close(flush_fd);
   1038 	return 0;
   1039 }
   1040 
   1041 
   1042 static int iproute_list_or_flush(int argc, char **argv, int flush)
   1043 {
   1044 	int do_ipv6 = preferred_family;
   1045 	char *id = NULL;
   1046 	char *od = NULL;
   1047 
   1048 	iproute_reset_filter();
   1049 	filter.tb = RT_TABLE_MAIN;
   1050 
   1051 	if (flush && argc <= 0) {
   1052 		fprintf(stderr, "\"ip route flush\" requires arguments.\n");
   1053 		return -1;
   1054 	}
   1055 
   1056 	while (argc > 0) {
   1057 		if (matches(*argv, "table") == 0) {
   1058 			__u32 tid;
   1059 			NEXT_ARG();
   1060 			if (rtnl_rttable_a2n(&tid, *argv)) {
   1061 				if (strcmp(*argv, "all") == 0) {
   1062 					filter.tb = 0;
   1063 				} else if (strcmp(*argv, "cache") == 0) {
   1064 					filter.cloned = 1;
   1065 				} else if (strcmp(*argv, "help") == 0) {
   1066 					usage();
   1067 				} else {
   1068 					invarg("table id value is invalid\n", *argv);
   1069 				}
   1070 			} else
   1071 				filter.tb = tid;
   1072 		} else if (matches(*argv, "cached") == 0 ||
   1073 			   matches(*argv, "cloned") == 0) {
   1074 			filter.cloned = 1;
   1075 		} else if (strcmp(*argv, "tos") == 0 ||
   1076 			   matches(*argv, "dsfield") == 0) {
   1077 			__u32 tos;
   1078 			NEXT_ARG();
   1079 			if (rtnl_dsfield_a2n(&tos, *argv))
   1080 				invarg("TOS value is invalid\n", *argv);
   1081 			filter.tos = tos;
   1082 			filter.tosmask = -1;
   1083 		} else if (matches(*argv, "protocol") == 0) {
   1084 			__u32 prot = 0;
   1085 			NEXT_ARG();
   1086 			filter.protocolmask = -1;
   1087 			if (rtnl_rtprot_a2n(&prot, *argv)) {
   1088 				if (strcmp(*argv, "all") != 0)
   1089 					invarg("invalid \"protocol\"\n", *argv);
   1090 				prot = 0;
   1091 				filter.protocolmask = 0;
   1092 			}
   1093 			filter.protocol = prot;
   1094 		} else if (matches(*argv, "scope") == 0) {
   1095 			__u32 scope = 0;
   1096 			NEXT_ARG();
   1097 			filter.scopemask = -1;
   1098 			if (rtnl_rtscope_a2n(&scope, *argv)) {
   1099 				if (strcmp(*argv, "all") != 0)
   1100 					invarg("invalid \"scope\"\n", *argv);
   1101 				scope = RT_SCOPE_NOWHERE;
   1102 				filter.scopemask = 0;
   1103 			}
   1104 			filter.scope = scope;
   1105 		} else if (matches(*argv, "type") == 0) {
   1106 			int type;
   1107 			NEXT_ARG();
   1108 			filter.typemask = -1;
   1109 			if (rtnl_rtntype_a2n(&type, *argv))
   1110 				invarg("node type value is invalid\n", *argv);
   1111 			filter.type = type;
   1112 		} else if (strcmp(*argv, "dev") == 0 ||
   1113 			   strcmp(*argv, "oif") == 0) {
   1114 			NEXT_ARG();
   1115 			od = *argv;
   1116 		} else if (strcmp(*argv, "iif") == 0) {
   1117 			NEXT_ARG();
   1118 			id = *argv;
   1119 		} else if (strcmp(*argv, "via") == 0) {
   1120 			NEXT_ARG();
   1121 			get_prefix(&filter.rvia, *argv, do_ipv6);
   1122 		} else if (strcmp(*argv, "src") == 0) {
   1123 			NEXT_ARG();
   1124 			get_prefix(&filter.rprefsrc, *argv, do_ipv6);
   1125 		} else if (matches(*argv, "realms") == 0) {
   1126 			__u32 realm;
   1127 			NEXT_ARG();
   1128 			if (get_rt_realms(&realm, *argv))
   1129 				invarg("invalid realms\n", *argv);
   1130 			filter.realm = realm;
   1131 			filter.realmmask = ~0U;
   1132 			if ((filter.realm&0xFFFF) == 0 &&
   1133 			    (*argv)[strlen(*argv) - 1] == '/')
   1134 				filter.realmmask &= ~0xFFFF;
   1135 			if ((filter.realm&0xFFFF0000U) == 0 &&
   1136 			    (strchr(*argv, '/') == NULL ||
   1137 			     (*argv)[0] == '/'))
   1138 				filter.realmmask &= ~0xFFFF0000U;
   1139 		} else if (matches(*argv, "from") == 0) {
   1140 			NEXT_ARG();
   1141 			if (matches(*argv, "root") == 0) {
   1142 				NEXT_ARG();
   1143 				get_prefix(&filter.rsrc, *argv, do_ipv6);
   1144 			} else if (matches(*argv, "match") == 0) {
   1145 				NEXT_ARG();
   1146 				get_prefix(&filter.msrc, *argv, do_ipv6);
   1147 			} else {
   1148 				if (matches(*argv, "exact") == 0) {
   1149 					NEXT_ARG();
   1150 				}
   1151 				get_prefix(&filter.msrc, *argv, do_ipv6);
   1152 				filter.rsrc = filter.msrc;
   1153 			}
   1154 		} else {
   1155 			if (matches(*argv, "to") == 0) {
   1156 				NEXT_ARG();
   1157 			}
   1158 			if (matches(*argv, "root") == 0) {
   1159 				NEXT_ARG();
   1160 				get_prefix(&filter.rdst, *argv, do_ipv6);
   1161 			} else if (matches(*argv, "match") == 0) {
   1162 				NEXT_ARG();
   1163 				get_prefix(&filter.mdst, *argv, do_ipv6);
   1164 			} else {
   1165 				if (matches(*argv, "exact") == 0) {
   1166 					NEXT_ARG();
   1167 				}
   1168 				get_prefix(&filter.mdst, *argv, do_ipv6);
   1169 				filter.rdst = filter.mdst;
   1170 			}
   1171 		}
   1172 		argc--; argv++;
   1173 	}
   1174 
   1175 	if (do_ipv6 == AF_UNSPEC && filter.tb)
   1176 		do_ipv6 = AF_INET;
   1177 
   1178 	ll_init_map(&rth);
   1179 
   1180 	if (id || od)  {
   1181 		int idx;
   1182 
   1183 		if (id) {
   1184 			if ((idx = ll_name_to_index(id)) == 0) {
   1185 				fprintf(stderr, "Cannot find device \"%s\"\n", id);
   1186 				return -1;
   1187 			}
   1188 			filter.iif = idx;
   1189 			filter.iifmask = -1;
   1190 		}
   1191 		if (od) {
   1192 			if ((idx = ll_name_to_index(od)) == 0) {
   1193 				fprintf(stderr, "Cannot find device \"%s\"\n", od);
   1194 				return -1;
   1195 			}
   1196 			filter.oif = idx;
   1197 			filter.oifmask = -1;
   1198 		}
   1199 	}
   1200 
   1201 	if (flush) {
   1202 		int round = 0;
   1203 		char flushb[4096-512];
   1204 		time_t start = time(0);
   1205 
   1206 		if (filter.cloned) {
   1207 			if (do_ipv6 != AF_INET6) {
   1208 				iproute_flush_cache();
   1209 				if (show_stats)
   1210 					printf("*** IPv4 routing cache is flushed.\n");
   1211 			}
   1212 			if (do_ipv6 == AF_INET)
   1213 				return 0;
   1214 		}
   1215 
   1216 		filter.flushb = flushb;
   1217 		filter.flushp = 0;
   1218 		filter.flushe = sizeof(flushb);
   1219 
   1220 		for (;;) {
   1221 			if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
   1222 				perror("Cannot send dump request");
   1223 				exit(1);
   1224 			}
   1225 			filter.flushed = 0;
   1226 			if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
   1227 				fprintf(stderr, "Flush terminated\n");
   1228 				exit(1);
   1229 			}
   1230 			if (filter.flushed == 0) {
   1231 				if (show_stats) {
   1232 					if (round == 0 && (!filter.cloned || do_ipv6 == AF_INET6))
   1233 						printf("Nothing to flush.\n");
   1234 					else
   1235 						printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
   1236 				}
   1237 				fflush(stdout);
   1238 				return 0;
   1239 			}
   1240 			round++;
   1241 			if (flush_update() < 0)
   1242 				exit(1);
   1243 
   1244 			if (time(0) - start > 30) {
   1245 				printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
   1246 				       time(0) - start, filter.flushed);
   1247 				exit(1);
   1248 			}
   1249 
   1250 			if (show_stats) {
   1251 				printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed);
   1252 				fflush(stdout);
   1253 			}
   1254 		}
   1255 	}
   1256 
   1257 	if (!filter.cloned) {
   1258 		if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
   1259 			perror("Cannot send dump request");
   1260 			exit(1);
   1261 		}
   1262 	} else {
   1263 		if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
   1264 			perror("Cannot send dump request");
   1265 			exit(1);
   1266 		}
   1267 	}
   1268 
   1269 	if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
   1270 		fprintf(stderr, "Dump terminated\n");
   1271 		exit(1);
   1272 	}
   1273 
   1274 	exit(0);
   1275 }
   1276 
   1277 
   1278 int iproute_get(int argc, char **argv)
   1279 {
   1280 	struct {
   1281 		struct nlmsghdr 	n;
   1282 		struct rtmsg 		r;
   1283 		char   			buf[1024];
   1284 	} req;
   1285 	char  *idev = NULL;
   1286 	char  *odev = NULL;
   1287 	int connected = 0;
   1288 	int from_ok = 0;
   1289 
   1290 	memset(&req, 0, sizeof(req));
   1291 
   1292 	iproute_reset_filter();
   1293 
   1294 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
   1295 	req.n.nlmsg_flags = NLM_F_REQUEST;
   1296 	req.n.nlmsg_type = RTM_GETROUTE;
   1297 	req.r.rtm_family = preferred_family;
   1298 	req.r.rtm_table = 0;
   1299 	req.r.rtm_protocol = 0;
   1300 	req.r.rtm_scope = 0;
   1301 	req.r.rtm_type = 0;
   1302 	req.r.rtm_src_len = 0;
   1303 	req.r.rtm_dst_len = 0;
   1304 	req.r.rtm_tos = 0;
   1305 
   1306 	while (argc > 0) {
   1307 		if (strcmp(*argv, "tos") == 0 ||
   1308 		    matches(*argv, "dsfield") == 0) {
   1309 			__u32 tos;
   1310 			NEXT_ARG();
   1311 			if (rtnl_dsfield_a2n(&tos, *argv))
   1312 				invarg("TOS value is invalid\n", *argv);
   1313 			req.r.rtm_tos = tos;
   1314 		} else if (matches(*argv, "from") == 0) {
   1315 			inet_prefix addr;
   1316 			NEXT_ARG();
   1317 			if (matches(*argv, "help") == 0)
   1318 				usage();
   1319 			from_ok = 1;
   1320 			get_prefix(&addr, *argv, req.r.rtm_family);
   1321 			if (req.r.rtm_family == AF_UNSPEC)
   1322 				req.r.rtm_family = addr.family;
   1323 			if (addr.bytelen)
   1324 				addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
   1325 			req.r.rtm_src_len = addr.bitlen;
   1326 		} else if (matches(*argv, "iif") == 0) {
   1327 			NEXT_ARG();
   1328 			idev = *argv;
   1329 		} else if (matches(*argv, "oif") == 0 ||
   1330 			   strcmp(*argv, "dev") == 0) {
   1331 			NEXT_ARG();
   1332 			odev = *argv;
   1333 		} else if (matches(*argv, "notify") == 0) {
   1334 			req.r.rtm_flags |= RTM_F_NOTIFY;
   1335 		} else if (matches(*argv, "connected") == 0) {
   1336 			connected = 1;
   1337 		} else {
   1338 			inet_prefix addr;
   1339 			if (strcmp(*argv, "to") == 0) {
   1340 				NEXT_ARG();
   1341 			}
   1342 			if (matches(*argv, "help") == 0)
   1343 				usage();
   1344 			get_prefix(&addr, *argv, req.r.rtm_family);
   1345 			if (req.r.rtm_family == AF_UNSPEC)
   1346 				req.r.rtm_family = addr.family;
   1347 			if (addr.bytelen)
   1348 				addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
   1349 			req.r.rtm_dst_len = addr.bitlen;
   1350 		}
   1351 		argc--; argv++;
   1352 	}
   1353 
   1354 	if (req.r.rtm_dst_len == 0) {
   1355 		fprintf(stderr, "need at least destination address\n");
   1356 		exit(1);
   1357 	}
   1358 
   1359 	ll_init_map(&rth);
   1360 
   1361 	if (idev || odev)  {
   1362 		int idx;
   1363 
   1364 		if (idev) {
   1365 			if ((idx = ll_name_to_index(idev)) == 0) {
   1366 				fprintf(stderr, "Cannot find device \"%s\"\n", idev);
   1367 				return -1;
   1368 			}
   1369 			addattr32(&req.n, sizeof(req), RTA_IIF, idx);
   1370 		}
   1371 		if (odev) {
   1372 			if ((idx = ll_name_to_index(odev)) == 0) {
   1373 				fprintf(stderr, "Cannot find device \"%s\"\n", odev);
   1374 				return -1;
   1375 			}
   1376 			addattr32(&req.n, sizeof(req), RTA_OIF, idx);
   1377 		}
   1378 	}
   1379 
   1380 	if (req.r.rtm_family == AF_UNSPEC)
   1381 		req.r.rtm_family = AF_INET;
   1382 
   1383 	if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
   1384 		exit(2);
   1385 
   1386 	if (connected && !from_ok) {
   1387 		struct rtmsg *r = NLMSG_DATA(&req.n);
   1388 		int len = req.n.nlmsg_len;
   1389 		struct rtattr * tb[RTA_MAX+1];
   1390 
   1391 		if (print_route(NULL, &req.n, (void*)stdout) < 0) {
   1392 			fprintf(stderr, "An error :-)\n");
   1393 			exit(1);
   1394 		}
   1395 
   1396 		if (req.n.nlmsg_type != RTM_NEWROUTE) {
   1397 			fprintf(stderr, "Not a route?\n");
   1398 			return -1;
   1399 		}
   1400 		len -= NLMSG_LENGTH(sizeof(*r));
   1401 		if (len < 0) {
   1402 			fprintf(stderr, "Wrong len %d\n", len);
   1403 			return -1;
   1404 		}
   1405 
   1406 		parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
   1407 
   1408 		if (tb[RTA_PREFSRC]) {
   1409 			tb[RTA_PREFSRC]->rta_type = RTA_SRC;
   1410 			r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
   1411 		} else if (!tb[RTA_SRC]) {
   1412 			fprintf(stderr, "Failed to connect the route\n");
   1413 			return -1;
   1414 		}
   1415 		if (!odev && tb[RTA_OIF])
   1416 			tb[RTA_OIF]->rta_type = 0;
   1417 		if (tb[RTA_GATEWAY])
   1418 			tb[RTA_GATEWAY]->rta_type = 0;
   1419 		if (!idev && tb[RTA_IIF])
   1420 			tb[RTA_IIF]->rta_type = 0;
   1421 		req.n.nlmsg_flags = NLM_F_REQUEST;
   1422 		req.n.nlmsg_type = RTM_GETROUTE;
   1423 
   1424 		if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
   1425 			exit(2);
   1426 	}
   1427 
   1428 	if (print_route(NULL, &req.n, (void*)stdout) < 0) {
   1429 		fprintf(stderr, "An error :-)\n");
   1430 		exit(1);
   1431 	}
   1432 
   1433 	exit(0);
   1434 }
   1435 
   1436 void iproute_reset_filter()
   1437 {
   1438 	memset(&filter, 0, sizeof(filter));
   1439 	filter.mdst.bitlen = -1;
   1440 	filter.msrc.bitlen = -1;
   1441 }
   1442 
   1443 int do_iproute(int argc, char **argv)
   1444 {
   1445 	if (argc < 1)
   1446 		return iproute_list_or_flush(0, NULL, 0);
   1447 
   1448 	if (matches(*argv, "add") == 0)
   1449 		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL,
   1450 				      argc-1, argv+1);
   1451 	if (matches(*argv, "change") == 0 || strcmp(*argv, "chg") == 0)
   1452 		return iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE,
   1453 				      argc-1, argv+1);
   1454 	if (matches(*argv, "replace") == 0)
   1455 		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
   1456 				      argc-1, argv+1);
   1457 	if (matches(*argv, "prepend") == 0)
   1458 		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE,
   1459 				      argc-1, argv+1);
   1460 	if (matches(*argv, "append") == 0)
   1461 		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND,
   1462 				      argc-1, argv+1);
   1463 	if (matches(*argv, "test") == 0)
   1464 		return iproute_modify(RTM_NEWROUTE, NLM_F_EXCL,
   1465 				      argc-1, argv+1);
   1466 	if (matches(*argv, "delete") == 0)
   1467 		return iproute_modify(RTM_DELROUTE, 0,
   1468 				      argc-1, argv+1);
   1469 	if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
   1470 	    || matches(*argv, "lst") == 0)
   1471 		return iproute_list_or_flush(argc-1, argv+1, 0);
   1472 	if (matches(*argv, "get") == 0)
   1473 		return iproute_get(argc-1, argv+1);
   1474 	if (matches(*argv, "flush") == 0)
   1475 		return iproute_list_or_flush(argc-1, argv+1, 1);
   1476 	if (matches(*argv, "help") == 0)
   1477 		usage();
   1478 	fprintf(stderr, "Command \"%s\" is unknown, try \"ip route help\".\n", *argv);
   1479 	exit(-1);
   1480 }
   1481 
   1482