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