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