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