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