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