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