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