This commit was generated by cvs2svn to compensate for changes in r2587,
[iproute2.git] / ip / iproute.c.initvar
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                 rta->rta_len += rtnh->rtnh_len;
620                 parse_one_nh(rta, rtnh, &argc, &argv);
621                 rtnh = RTNH_NEXT(rtnh);
622         }
623
624         if (rta->rta_len > RTA_LENGTH(0))
625                 addattr_l(n, 1024, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta));
626         return 0;
627 }
628
629
630 int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
631 {
632         struct rtnl_handle rth;
633         struct {
634                 struct nlmsghdr         n;
635                 struct rtmsg            r;
636                 char                    buf[1024];
637         } req;
638         char  mxbuf[256];
639         struct rtattr * mxrta = (void*)mxbuf;
640         unsigned mxlock = 0;
641         char  *d = NULL;
642         int gw_ok = 0;
643         int dst_ok = 0;
644         int nhs_ok = 0;
645         int scope_ok = 0;
646         int table_ok = 0;
647         int proto_ok = 0;
648         int type_ok = 0;
649
650         memset(&req, 0, sizeof(req));
651
652         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
653         req.n.nlmsg_flags = NLM_F_REQUEST|flags;
654         req.n.nlmsg_type = cmd;
655         req.r.rtm_family = preferred_family;
656         req.r.rtm_table = RT_TABLE_MAIN;
657         req.r.rtm_scope = RT_SCOPE_NOWHERE;
658
659         if (cmd != RTM_DELROUTE) {
660                 req.r.rtm_protocol = RTPROT_BOOT;
661                 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
662                 req.r.rtm_type = RTN_UNICAST;
663         }
664
665         mxrta->rta_type = RTA_METRICS;
666         mxrta->rta_len = RTA_LENGTH(0);
667
668         while (argc > 0) {
669                 if (strcmp(*argv, "src") == 0) {
670                         inet_prefix addr;
671                         NEXT_ARG();
672                         get_addr(&addr, *argv, req.r.rtm_family);
673                         if (req.r.rtm_family == AF_UNSPEC)
674                                 req.r.rtm_family = addr.family;
675                         addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
676                 } else if (strcmp(*argv, "via") == 0) {
677                         inet_prefix addr;
678                         gw_ok = 1;
679                         NEXT_ARG();
680                         get_addr(&addr, *argv, req.r.rtm_family);
681                         if (req.r.rtm_family == AF_UNSPEC)
682                                 req.r.rtm_family = addr.family;
683                         addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
684                 } else if (strcmp(*argv, "from") == 0) {
685                         inet_prefix addr;
686                         NEXT_ARG();
687                         get_prefix(&addr, *argv, req.r.rtm_family);
688                         if (req.r.rtm_family == AF_UNSPEC)
689                                 req.r.rtm_family = addr.family;
690                         if (addr.bytelen)
691                                 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
692                         req.r.rtm_src_len = addr.bitlen;
693                 } else if (strcmp(*argv, "tos") == 0 ||
694                            matches(*argv, "dsfield") == 0) {
695                         __u32 tos;
696                         NEXT_ARG();
697                         if (rtnl_dsfield_a2n(&tos, *argv))
698                                 invarg("\"tos\" value is invalid\n", *argv);
699                         req.r.rtm_tos = tos;
700                 } else if (matches(*argv, "metric") == 0 ||
701                            matches(*argv, "priority") == 0 ||
702                            matches(*argv, "preference") == 0) {
703                         __u32 metric;
704                         NEXT_ARG();
705                         if (get_u32(&metric, *argv, 0))
706                                 invarg("\"metric\" value is invalid\n", *argv);
707                         addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
708                 } else if (strcmp(*argv, "scope") == 0) {
709                         int scope = 0;
710                         NEXT_ARG();
711                         if (rtnl_rtscope_a2n(&scope, *argv))
712                                 invarg("invalid \"scope\" value\n", *argv);
713                         req.r.rtm_scope = scope;
714                         scope_ok = 1;
715                 } else if (strcmp(*argv, "mtu") == 0) {
716                         unsigned mtu;
717                         NEXT_ARG();
718                         if (strcmp(*argv, "lock") == 0) {
719                                 mxlock |= (1<<RTAX_MTU);
720                                 NEXT_ARG();
721                         }
722                         if (get_unsigned(&mtu, *argv, 0))
723                                 invarg("\"mtu\" value is invalid\n", *argv);
724                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
725 #ifdef RTAX_ADVMSS
726                 } else if (strcmp(*argv, "advmss") == 0) {
727                         unsigned mss;
728                         NEXT_ARG();
729                         if (strcmp(*argv, "lock") == 0) {
730                                 mxlock |= (1<<RTAX_ADVMSS);
731                                 NEXT_ARG();
732                         }
733                         if (get_unsigned(&mss, *argv, 0))
734                                 invarg("\"mss\" value is invalid\n", *argv);
735                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss);
736 #endif
737 #ifdef RTAX_REORDERING
738                 } else if (matches(*argv, "reordering") == 0) {
739                         unsigned reord;
740                         NEXT_ARG();
741                         if (strcmp(*argv, "lock") == 0) {
742                                 mxlock |= (1<<RTAX_REORDERING);
743                                 NEXT_ARG();
744                         }
745                         if (get_unsigned(&reord, *argv, 0))
746                                 invarg("\"reordering\" value is invalid\n", *argv);
747                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord);
748 #endif
749                 } else if (strcmp(*argv, "rtt") == 0) {
750                         unsigned rtt;
751                         NEXT_ARG();
752                         if (strcmp(*argv, "lock") == 0) {
753                                 mxlock |= (1<<RTAX_RTT);
754                                 NEXT_ARG();
755                         }
756                         if (get_unsigned(&rtt, *argv, 0))
757                                 invarg("\"rtt\" value is invalid\n", *argv);
758                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT, rtt);
759                 } else if (matches(*argv, "window") == 0) {
760                         unsigned win;
761                         NEXT_ARG();
762                         if (strcmp(*argv, "lock") == 0) {
763                                 mxlock |= (1<<RTAX_WINDOW);
764                                 NEXT_ARG();
765                         }
766                         if (get_unsigned(&win, *argv, 0))
767                                 invarg("\"window\" value is invalid\n", *argv);
768                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win);
769                 } else if (matches(*argv, "cwnd") == 0) {
770                         unsigned win;
771                         NEXT_ARG();
772                         if (strcmp(*argv, "lock") == 0) {
773                                 mxlock |= (1<<RTAX_CWND);
774                                 NEXT_ARG();
775                         }
776                         if (get_unsigned(&win, *argv, 0))
777                                 invarg("\"cwnd\" value is invalid\n", *argv);
778                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win);
779                 } else if (matches(*argv, "rttvar") == 0) {
780                         unsigned win;
781                         NEXT_ARG();
782                         if (strcmp(*argv, "lock") == 0) {
783                                 mxlock |= (1<<RTAX_RTTVAR);
784                                 NEXT_ARG();
785                         }
786                         if (get_unsigned(&win, *argv, 0))
787                                 invarg("\"rttvar\" value is invalid\n", *argv);
788                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR, win);
789                 } else if (matches(*argv, "ssthresh") == 0) {
790                         unsigned win;
791                         NEXT_ARG();
792                         if (strcmp(*argv, "lock") == 0) {
793                                 mxlock |= (1<<RTAX_SSTHRESH);
794                                 NEXT_ARG();
795                         }
796                         if (get_unsigned(&win, *argv, 0))
797                                 invarg("\"ssthresh\" value is invalid\n", *argv);
798                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win);
799                 } else if (matches(*argv, "realms") == 0) {
800                         __u32 realm;
801                         NEXT_ARG();
802                         if (get_rt_realms(&realm, *argv))
803                                 invarg("\"realm\" value is invalid\n", *argv);
804                         addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
805                 } else if (strcmp(*argv, "onlink") == 0) {
806                         req.r.rtm_flags |= RTNH_F_ONLINK;
807                 } else if (matches(*argv, "equalize") == 0 ||
808                            strcmp(*argv, "eql") == 0) {
809                         req.r.rtm_flags |= RTM_F_EQUALIZE;
810                 } else if (strcmp(*argv, "nexthop") == 0) {
811                         nhs_ok = 1;
812                         break;
813                 } else if (matches(*argv, "protocol") == 0) {
814                         int prot;
815                         NEXT_ARG();
816                         if (rtnl_rtprot_a2n(&prot, *argv))
817                                 invarg("\"protocol\" value is invalid\n", *argv);
818                         req.r.rtm_protocol = prot;
819                         proto_ok =1;
820                 } else if (matches(*argv, "table") == 0) {
821                         int tid;
822                         NEXT_ARG();
823                         if (rtnl_rttable_a2n(&tid, *argv))
824                                 invarg("\"table\" value is invalid\n", *argv);
825                         req.r.rtm_table = tid;
826                         table_ok = 1;
827                 } else if (strcmp(*argv, "dev") == 0 ||
828                            strcmp(*argv, "oif") == 0) {
829                         NEXT_ARG();
830                         d = *argv;
831                 } else {
832                         int type;
833                         inet_prefix dst;
834
835                         if (strcmp(*argv, "to") == 0) {
836                                 NEXT_ARG();
837                         }
838                         if ((**argv < '0' || **argv > '9') &&
839                             rtnl_rtntype_a2n(&type, *argv) == 0) {
840                                 NEXT_ARG();
841                                 req.r.rtm_type = type;
842                                 type_ok = 1;
843                         }
844
845                         if (matches(*argv, "help") == 0)
846                                 usage();
847                         if (dst_ok)
848                                 duparg2("to", *argv);
849                         get_prefix(&dst, *argv, req.r.rtm_family);
850                         if (req.r.rtm_family == AF_UNSPEC)
851                                 req.r.rtm_family = dst.family;
852                         req.r.rtm_dst_len = dst.bitlen;
853                         dst_ok = 1;
854                         if (dst.bytelen)
855                                 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
856                 }
857                 argc--; argv++;
858         }
859
860         if (rtnl_open(&rth, 0) < 0)
861                 exit(1);
862
863         if (d || nhs_ok)  {
864                 int idx;
865
866                 ll_init_map(&rth);
867
868                 if (d) {
869                         if ((idx = ll_name_to_index(d)) == 0) {
870                                 fprintf(stderr, "Cannot find device \"%s\"\n", d);
871                                 return -1;
872                         }
873                         addattr32(&req.n, sizeof(req), RTA_OIF, idx);
874                 }
875         }
876
877         if (mxrta->rta_len > RTA_LENGTH(0)) {
878                 if (mxlock)
879                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
880                 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
881         }
882
883         if (nhs_ok)
884                 parse_nexthops(&req.n, &req.r, argc, argv);
885
886         if (!table_ok) {
887                 if (req.r.rtm_type == RTN_LOCAL ||
888                     req.r.rtm_type == RTN_BROADCAST ||
889                     req.r.rtm_type == RTN_NAT ||
890                     req.r.rtm_type == RTN_ANYCAST)
891                         req.r.rtm_table = RT_TABLE_LOCAL;
892         }
893         if (!scope_ok) {
894                 if (req.r.rtm_type == RTN_LOCAL ||
895                     req.r.rtm_type == RTN_NAT)
896                         req.r.rtm_scope = RT_SCOPE_HOST;
897                 else if (req.r.rtm_type == RTN_BROADCAST ||
898                          req.r.rtm_type == RTN_MULTICAST ||
899                          req.r.rtm_type == RTN_ANYCAST)
900                         req.r.rtm_scope = RT_SCOPE_LINK;
901                 else if (req.r.rtm_type == RTN_UNICAST ||
902                          req.r.rtm_type == RTN_UNSPEC) {
903                         if (cmd == RTM_DELROUTE)
904                                 req.r.rtm_scope = RT_SCOPE_NOWHERE;
905                         else if (!gw_ok && !nhs_ok)
906                                 req.r.rtm_scope = RT_SCOPE_LINK;
907                 }
908         }
909
910         if (req.r.rtm_family == AF_UNSPEC)
911                 req.r.rtm_family = AF_INET;
912
913         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
914                 exit(2);
915
916         return 0;
917 }
918
919 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
920 {
921         struct {
922                 struct nlmsghdr nlh;
923                 struct rtmsg rtm;
924         } req;
925         struct sockaddr_nl nladdr;
926
927         memset(&nladdr, 0, sizeof(nladdr));
928         memset(&req, 0, sizeof(req));
929         nladdr.nl_family = AF_NETLINK;
930
931         req.nlh.nlmsg_len = sizeof(req);
932         req.nlh.nlmsg_type = RTM_GETROUTE;
933         req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
934         req.nlh.nlmsg_pid = 0;
935         req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
936         req.rtm.rtm_family = family;
937         req.rtm.rtm_flags |= RTM_F_CLONED;
938
939         return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
940 }
941
942 static int iproute_flush_cache(void)
943 {
944 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
945
946         int len;
947         int flush_fd = open (ROUTE_FLUSH_PATH, O_WRONLY);
948         char *buffer = "-1";
949
950         if (flush_fd < 0) {
951                 fprintf (stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH);
952                 return -1;
953         }
954
955         len = strlen (buffer);
956                 
957         if ((write (flush_fd, (void *)buffer, len)) < len) {
958                 fprintf (stderr, "Cannot flush routing cache\n");
959                 return -1;
960         }
961         close(flush_fd);
962         return 0;
963 }
964
965
966 static int iproute_list_or_flush(int argc, char **argv, int flush)
967 {
968         int do_ipv6 = preferred_family;
969         struct rtnl_handle rth;
970         char *id = NULL;
971         char *od = NULL;
972
973         iproute_reset_filter();
974         filter.tb = RT_TABLE_MAIN;
975
976         if (flush && argc <= 0) {
977                 fprintf(stderr, "\"ip route flush\" requires arguments.\n");
978                 return -1;
979         }
980
981         while (argc > 0) {
982                 if (matches(*argv, "table") == 0) {
983                         int tid;
984                         NEXT_ARG();
985                         if (rtnl_rttable_a2n(&tid, *argv)) {
986                                 if (strcmp(*argv, "all") == 0) {
987                                         tid = 0;
988                                 } else if (strcmp(*argv, "cache") == 0) {
989                                         tid = -1;
990                                 } else if (strcmp(*argv, "help") == 0) {
991                                         usage();
992                                 } else {
993                                         invarg("table id value is invalid\n", *argv);
994                                 }
995                         }
996                         filter.tb = tid;
997                 } else if (matches(*argv, "cached") == 0 ||
998                            matches(*argv, "cloned") == 0) {
999                         filter.tb = -1;
1000                 } else if (strcmp(*argv, "tos") == 0 ||
1001                            matches(*argv, "dsfield") == 0) {
1002                         __u32 tos;
1003                         NEXT_ARG();
1004                         if (rtnl_dsfield_a2n(&tos, *argv))
1005                                 invarg("TOS value is invalid\n", *argv);
1006                         filter.tos = tos;
1007                         filter.tosmask = -1;
1008                 } else if (matches(*argv, "protocol") == 0) {
1009                         int prot = 0;
1010                         NEXT_ARG();
1011                         filter.protocolmask = -1;
1012                         if (rtnl_rtprot_a2n(&prot, *argv)) {
1013                                 if (strcmp(*argv, "all") != 0)
1014                                         invarg("invalid \"protocol\"\n", *argv);
1015                                 prot = 0;
1016                                 filter.protocolmask = 0;
1017                         }
1018                         filter.protocol = prot;
1019                 } else if (matches(*argv, "scope") == 0) {
1020                         int scope = 0;
1021                         NEXT_ARG();
1022                         filter.scopemask = -1;
1023                         if (rtnl_rtscope_a2n(&scope, *argv)) {
1024                                 if (strcmp(*argv, "all") != 0)
1025                                         invarg("invalid \"scope\"\n", *argv);
1026                                 scope = RT_SCOPE_NOWHERE;
1027                                 filter.scopemask = 0;
1028                         }
1029                         filter.scope = scope;
1030                 } else if (matches(*argv, "type") == 0) {
1031                         int type;
1032                         NEXT_ARG();
1033                         filter.typemask = -1;
1034                         if (rtnl_rtntype_a2n(&type, *argv))
1035                                 invarg("node type value is invalid\n", *argv);
1036                         filter.type = type;
1037                 } else if (strcmp(*argv, "dev") == 0 ||
1038                            strcmp(*argv, "oif") == 0) {
1039                         NEXT_ARG();
1040                         od = *argv;
1041                 } else if (strcmp(*argv, "iif") == 0) {
1042                         NEXT_ARG();
1043                         id = *argv;
1044                 } else if (strcmp(*argv, "via") == 0) {
1045                         NEXT_ARG();
1046                         get_prefix(&filter.rvia, *argv, do_ipv6);
1047                 } else if (strcmp(*argv, "src") == 0) {
1048                         NEXT_ARG();
1049                         get_prefix(&filter.rprefsrc, *argv, do_ipv6);
1050                 } else if (matches(*argv, "realms") == 0) {
1051                         __u32 realm;
1052                         NEXT_ARG();
1053                         if (get_rt_realms(&realm, *argv))
1054                                 invarg("invalid realms\n", *argv);
1055                         filter.realm = realm;
1056                         filter.realmmask = ~0U;
1057                         if ((filter.realm&0xFFFF) == 0 &&
1058                             (*argv)[strlen(*argv) - 1] == '/')
1059                                 filter.realmmask &= ~0xFFFF;
1060                         if ((filter.realm&0xFFFF0000U) == 0 &&
1061                             (strchr(*argv, '/') == NULL ||
1062                              (*argv)[0] == '/'))
1063                                 filter.realmmask &= ~0xFFFF0000U;
1064                 } else if (matches(*argv, "from") == 0) {
1065                         NEXT_ARG();
1066                         if (matches(*argv, "root") == 0) {
1067                                 NEXT_ARG();
1068                                 get_prefix(&filter.rsrc, *argv, do_ipv6);
1069                         } else if (matches(*argv, "match") == 0) {
1070                                 NEXT_ARG();
1071                                 get_prefix(&filter.msrc, *argv, do_ipv6);
1072                         } else {
1073                                 if (matches(*argv, "exact") == 0) {
1074                                         NEXT_ARG();
1075                                 }
1076                                 get_prefix(&filter.msrc, *argv, do_ipv6);
1077                                 filter.rsrc = filter.msrc;
1078                         }
1079                 } else {
1080                         if (matches(*argv, "to") == 0) {
1081                                 NEXT_ARG();
1082                         }
1083                         if (matches(*argv, "root") == 0) {
1084                                 NEXT_ARG();
1085                                 get_prefix(&filter.rdst, *argv, do_ipv6);
1086                         } else if (matches(*argv, "match") == 0) {
1087                                 NEXT_ARG();
1088                                 get_prefix(&filter.mdst, *argv, do_ipv6);
1089                         } else {
1090                                 if (matches(*argv, "exact") == 0) {
1091                                         NEXT_ARG();
1092                                 }
1093                                 get_prefix(&filter.mdst, *argv, do_ipv6);
1094                                 filter.rdst = filter.mdst;
1095                         }
1096                 }
1097                 argc--; argv++;
1098         }
1099
1100         if (do_ipv6 == AF_UNSPEC && filter.tb)
1101                 do_ipv6 = AF_INET;
1102
1103         if (rtnl_open(&rth, 0) < 0)
1104                 exit(1);
1105
1106         ll_init_map(&rth);
1107
1108         if (id || od)  {
1109                 int idx;
1110
1111                 if (id) {
1112                         if ((idx = ll_name_to_index(id)) == 0) {
1113                                 fprintf(stderr, "Cannot find device \"%s\"\n", id);
1114                                 return -1;
1115                         }
1116                         filter.iif = idx;
1117                         filter.iifmask = -1;
1118                 }
1119                 if (od) {
1120                         if ((idx = ll_name_to_index(od)) == 0) {
1121                                 fprintf(stderr, "Cannot find device \"%s\"\n", od);
1122                                 return -1;
1123                         }
1124                         filter.oif = idx;
1125                         filter.oifmask = -1;
1126                 }
1127         }
1128
1129         if (flush) {
1130                 int round = 0;
1131                 char flushb[4096-512];
1132                 time_t start = time(0);
1133
1134                 if (filter.tb == -1) {
1135                         if (do_ipv6 != AF_INET6) {
1136                                 iproute_flush_cache();
1137                                 if (show_stats)
1138                                         printf("*** IPv4 routing cache is flushed.\n");
1139                         }
1140                         if (do_ipv6 == AF_INET)
1141                                 return 0;
1142                 }
1143
1144                 filter.flushb = flushb;
1145                 filter.flushp = 0;
1146                 filter.flushe = sizeof(flushb);
1147                 filter.rth = &rth;
1148
1149                 for (;;) {
1150                         if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
1151                                 perror("Cannot send dump request");
1152                                 exit(1);
1153                         }
1154                         filter.flushed = 0;
1155                         if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
1156                                 fprintf(stderr, "Flush terminated\n");
1157                                 exit(1);
1158                         }
1159                         if (filter.flushed == 0) {
1160                                 if (round == 0) {
1161                                         if (filter.tb != -1 || do_ipv6 == AF_INET6)
1162                                                 fprintf(stderr, "Nothing to flush.\n");
1163                                 } else if (show_stats)
1164                                         printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
1165                                 fflush(stdout);
1166                                 return 0;
1167                         }
1168                         round++;
1169                         if (flush_update() < 0)
1170                                 exit(1);
1171
1172                         if (time(0) - start > 30) {
1173                                 printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
1174                                        time(0) - start, filter.flushed);
1175                                 exit(1);
1176                         }
1177
1178                         if (show_stats) {
1179                                 printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed);
1180                                 fflush(stdout);
1181                         }
1182                 }
1183         }
1184
1185         if (filter.tb != -1) {
1186                 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
1187                         perror("Cannot send dump request");
1188                         exit(1);
1189                 }
1190         } else {
1191                 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
1192                         perror("Cannot send dump request");
1193                         exit(1);
1194                 }
1195         }
1196
1197         if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
1198                 fprintf(stderr, "Dump terminated\n");
1199                 exit(1);
1200         }
1201
1202         exit(0);
1203 }
1204
1205
1206 int iproute_get(int argc, char **argv)
1207 {
1208         struct rtnl_handle rth;
1209         struct {
1210                 struct nlmsghdr         n;
1211                 struct rtmsg            r;
1212                 char                    buf[1024];
1213         } req;
1214         char  *idev = NULL;
1215         char  *odev = NULL;
1216         int connected = 0;
1217         int from_ok = 0;
1218
1219         memset(&req, 0, sizeof(req));
1220
1221         iproute_reset_filter();
1222
1223         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1224         req.n.nlmsg_flags = NLM_F_REQUEST;
1225         req.n.nlmsg_type = RTM_GETROUTE;
1226         req.r.rtm_family = preferred_family;
1227         req.r.rtm_table = 0;
1228         req.r.rtm_protocol = 0;
1229         req.r.rtm_scope = 0;
1230         req.r.rtm_type = 0;
1231         req.r.rtm_src_len = 0;
1232         req.r.rtm_dst_len = 0;
1233         req.r.rtm_tos = 0;
1234         
1235         while (argc > 0) {
1236                 if (strcmp(*argv, "tos") == 0 ||
1237                     matches(*argv, "dsfield") == 0) {
1238                         __u32 tos;
1239                         NEXT_ARG();
1240                         if (rtnl_dsfield_a2n(&tos, *argv))
1241                                 invarg("TOS value is invalid\n", *argv);
1242                         req.r.rtm_tos = tos;
1243                 } else if (matches(*argv, "from") == 0) {
1244                         inet_prefix addr;
1245                         NEXT_ARG();
1246                         if (matches(*argv, "help") == 0)
1247                                 usage();
1248                         from_ok = 1;
1249                         get_prefix(&addr, *argv, req.r.rtm_family);
1250                         if (req.r.rtm_family == AF_UNSPEC)
1251                                 req.r.rtm_family = addr.family;
1252                         if (addr.bytelen)
1253                                 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
1254                         req.r.rtm_src_len = addr.bitlen;
1255                 } else if (matches(*argv, "iif") == 0) {
1256                         NEXT_ARG();
1257                         idev = *argv;
1258                 } else if (matches(*argv, "oif") == 0 ||
1259                            strcmp(*argv, "dev") == 0) {
1260                         NEXT_ARG();
1261                         odev = *argv;
1262                 } else if (matches(*argv, "notify") == 0) {
1263                         req.r.rtm_flags |= RTM_F_NOTIFY;
1264                 } else if (matches(*argv, "connected") == 0) {
1265                         connected = 1;
1266                 } else {
1267                         inet_prefix addr;
1268                         if (strcmp(*argv, "to") == 0) {
1269                                 NEXT_ARG();
1270                         }
1271                         if (matches(*argv, "help") == 0)
1272                                 usage();
1273                         get_prefix(&addr, *argv, req.r.rtm_family);
1274                         if (req.r.rtm_family == AF_UNSPEC)
1275                                 req.r.rtm_family = addr.family;
1276                         if (addr.bytelen)
1277                                 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
1278                         req.r.rtm_dst_len = addr.bitlen;
1279                 }
1280                 argc--; argv++;
1281         }
1282
1283         if (req.r.rtm_dst_len == 0) {
1284                 fprintf(stderr, "need at least destination address\n");
1285                 exit(1);
1286         }
1287
1288         if (rtnl_open(&rth, 0) < 0)
1289                 exit(1);
1290
1291         ll_init_map(&rth);
1292
1293         if (idev || odev)  {
1294                 int idx;
1295
1296                 if (idev) {
1297                         if ((idx = ll_name_to_index(idev)) == 0) {
1298                                 fprintf(stderr, "Cannot find device \"%s\"\n", idev);
1299                                 return -1;
1300                         }
1301                         addattr32(&req.n, sizeof(req), RTA_IIF, idx);
1302                 }
1303                 if (odev) {
1304                         if ((idx = ll_name_to_index(odev)) == 0) {
1305                                 fprintf(stderr, "Cannot find device \"%s\"\n", odev);
1306                                 return -1;
1307                         }
1308                         addattr32(&req.n, sizeof(req), RTA_OIF, idx);
1309                 }
1310         }
1311
1312         if (req.r.rtm_family == AF_UNSPEC)
1313                 req.r.rtm_family = AF_INET;
1314
1315         if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
1316                 exit(2);
1317
1318         if (connected && !from_ok) {
1319                 struct rtmsg *r = NLMSG_DATA(&req.n);
1320                 int len = req.n.nlmsg_len;
1321                 struct rtattr * tb[RTA_MAX+1];
1322
1323                 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
1324                         fprintf(stderr, "An error :-)\n");
1325                         exit(1);
1326                 }
1327
1328                 if (req.n.nlmsg_type != RTM_NEWROUTE) {
1329                         fprintf(stderr, "Not a route?\n");
1330                         return -1;
1331                 }
1332                 len -= NLMSG_LENGTH(sizeof(*r));
1333                 if (len < 0) {
1334                         fprintf(stderr, "Wrong len %d\n", len);
1335                         return -1;
1336                 }
1337
1338                 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
1339
1340                 if (tb[RTA_PREFSRC]) {
1341                         tb[RTA_PREFSRC]->rta_type = RTA_SRC;
1342                         r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
1343                 } else if (!tb[RTA_SRC]) {
1344                         fprintf(stderr, "Failed to connect the route\n");
1345                         return -1;
1346                 }
1347                 if (!odev && tb[RTA_OIF])
1348                         tb[RTA_OIF]->rta_type = 0;
1349                 if (tb[RTA_GATEWAY])
1350                         tb[RTA_GATEWAY]->rta_type = 0;
1351                 if (!idev && tb[RTA_IIF])
1352                         tb[RTA_IIF]->rta_type = 0;
1353                 req.n.nlmsg_flags = NLM_F_REQUEST;
1354                 req.n.nlmsg_type = RTM_GETROUTE;
1355
1356                 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
1357                         exit(2);
1358         }
1359
1360         if (print_route(NULL, &req.n, (void*)stdout) < 0) {
1361                 fprintf(stderr, "An error :-)\n");
1362                 exit(1);
1363         }
1364
1365         exit(0);
1366 }
1367
1368 void iproute_reset_filter()
1369 {
1370         memset(&filter, 0, sizeof(filter));
1371         filter.mdst.bitlen = -1;
1372         filter.msrc.bitlen = -1;
1373 }
1374
1375 int do_iproute(int argc, char **argv)
1376 {
1377         if (argc < 1)
1378                 return iproute_list_or_flush(0, NULL, 0);
1379         
1380         if (matches(*argv, "add") == 0)
1381                 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL,
1382                                       argc-1, argv+1);
1383         if (matches(*argv, "change") == 0 || strcmp(*argv, "chg") == 0)
1384                 return iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE,
1385                                       argc-1, argv+1);
1386         if (matches(*argv, "replace") == 0)
1387                 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
1388                                       argc-1, argv+1);
1389         if (matches(*argv, "prepend") == 0)
1390                 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE,
1391                                       argc-1, argv+1);
1392         if (matches(*argv, "append") == 0)
1393                 return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND,
1394                                       argc-1, argv+1);
1395         if (matches(*argv, "test") == 0)
1396                 return iproute_modify(RTM_NEWROUTE, NLM_F_EXCL,
1397                                       argc-1, argv+1);
1398         if (matches(*argv, "delete") == 0)
1399                 return iproute_modify(RTM_DELROUTE, 0,
1400                                       argc-1, argv+1);
1401         if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1402             || matches(*argv, "lst") == 0)
1403                 return iproute_list_or_flush(argc-1, argv+1, 0);
1404         if (matches(*argv, "get") == 0)
1405                 return iproute_get(argc-1, argv+1);
1406         if (matches(*argv, "flush") == 0)
1407                 return iproute_list_or_flush(argc-1, argv+1, 1);
1408         if (matches(*argv, "help") == 0)
1409                 usage();
1410         fprintf(stderr, "Command \"%s\" is unknown, try \"ip route help\".\n", *argv);
1411         exit(-1);
1412 }
1413