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