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