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