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