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