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