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