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