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