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