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