]>
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) |
39d16a02 DA |
466 | print_int(PRINT_ANY, "expires", |
467 | "expires %dsec ", 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 | 901 | if (r->rtm_family == AF_INET) { |
9107c425 | 902 | if (r->rtm_flags & RTM_F_CLONED) |
663c3cb2 | 903 | print_cache_flags(fp, r->rtm_flags); |
6e41810e | 904 | |
9107c425 DA |
905 | if (tb[RTA_CACHEINFO]) |
906 | print_rta_cacheinfo(fp, RTA_DATA(tb[RTA_CACHEINFO])); | |
aba5acdf | 907 | } else if (r->rtm_family == AF_INET6) { |
9107c425 DA |
908 | if (tb[RTA_CACHEINFO]) |
909 | print_rta_cacheinfo(fp, RTA_DATA(tb[RTA_CACHEINFO])); | |
aba5acdf | 910 | } |
6e41810e | 911 | |
968272e7 SH |
912 | if (tb[RTA_METRICS]) |
913 | print_rta_metrics(fp, tb[RTA_METRICS]); | |
aba5acdf | 914 | |
1506d8d3 SH |
915 | if (tb[RTA_IIF] && filter.iifmask != -1) |
916 | print_rta_if(fp, tb[RTA_IIF], "iif"); | |
b3ab1e68 | 917 | |
f48e1488 SH |
918 | if (tb[RTA_MULTIPATH]) |
919 | print_rta_multipath(fp, r, tb[RTA_MULTIPATH]); | |
8cfc2d47 SH |
920 | |
921 | if (tb[RTA_PREF]) | |
922 | print_rt_pref(fp, rta_getattr_u8(tb[RTA_PREF])); | |
923 | ||
c44d18ea | 924 | if (tb[RTA_TTL_PROPAGATE]) { |
663c3cb2 SH |
925 | bool propogate = rta_getattr_u8(tb[RTA_TTL_PROPAGATE]); |
926 | ||
927 | if (is_json_context()) | |
928 | print_bool(PRINT_JSON, "ttl-propogate", NULL, | |
929 | propogate); | |
c44d18ea | 930 | else |
663c3cb2 SH |
931 | print_string(PRINT_FP, NULL, |
932 | "ttl-propogate %s", | |
933 | propogate ? "enabled" : "disabled"); | |
c44d18ea | 934 | } |
663c3cb2 SH |
935 | |
936 | print_string(PRINT_FP, NULL, "\n", NULL); | |
937 | close_json_object(); | |
aba5acdf SH |
938 | fflush(fp); |
939 | return 0; | |
940 | } | |
941 | ||
1e529305 RP |
942 | static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r, |
943 | struct rtattr *rta, struct rtnexthop *rtnh, | |
d1f28cf1 | 944 | int *argcp, char ***argvp) |
aba5acdf SH |
945 | { |
946 | int argc = *argcp; | |
947 | char **argv = *argvp; | |
948 | ||
949 | while (++argv, --argc > 0) { | |
950 | if (strcmp(*argv, "via") == 0) { | |
4d6c3796 | 951 | inet_prefix addr; |
93ae2835 | 952 | int family; |
56f5daac | 953 | |
aba5acdf | 954 | NEXT_ARG(); |
93ae2835 EB |
955 | family = read_family(*argv); |
956 | if (family == AF_UNSPEC) | |
957 | family = r->rtm_family; | |
958 | else | |
959 | NEXT_ARG(); | |
960 | get_addr(&addr, *argv, family); | |
4d6c3796 VB |
961 | if (r->rtm_family == AF_UNSPEC) |
962 | r->rtm_family = addr.family; | |
93ae2835 EB |
963 | if (addr.family == r->rtm_family) { |
964 | rta_addattr_l(rta, 4096, RTA_GATEWAY, &addr.data, addr.bytelen); | |
965 | rtnh->rtnh_len += sizeof(struct rtattr) + addr.bytelen; | |
966 | } else { | |
967 | rta_addattr_l(rta, 4096, RTA_VIA, &addr.family, addr.bytelen+2); | |
ecc509f9 | 968 | rtnh->rtnh_len += RTA_SPACE(addr.bytelen+2); |
93ae2835 | 969 | } |
aba5acdf SH |
970 | } else if (strcmp(*argv, "dev") == 0) { |
971 | NEXT_ARG(); | |
cc560825 | 972 | rtnh->rtnh_ifindex = ll_name_to_index(*argv); |
fe99adbc SP |
973 | if (!rtnh->rtnh_ifindex) |
974 | return nodev(*argv); | |
aba5acdf | 975 | } else if (strcmp(*argv, "weight") == 0) { |
56f5daac SH |
976 | unsigned int w; |
977 | ||
aba5acdf SH |
978 | NEXT_ARG(); |
979 | if (get_unsigned(&w, *argv, 0) || w == 0 || w > 256) | |
980 | invarg("\"weight\" is invalid\n", *argv); | |
981 | rtnh->rtnh_hops = w - 1; | |
982 | } else if (strcmp(*argv, "onlink") == 0) { | |
983 | rtnh->rtnh_flags |= RTNH_F_ONLINK; | |
f4f6d640 PM |
984 | } else if (matches(*argv, "realms") == 0) { |
985 | __u32 realm; | |
56f5daac | 986 | |
f4f6d640 | 987 | NEXT_ARG(); |
d583e88e | 988 | if (get_rt_realms_or_raw(&realm, *argv)) |
f4f6d640 PM |
989 | invarg("\"realm\" value is invalid\n", *argv); |
990 | rta_addattr32(rta, 4096, RTA_FLOW, realm); | |
991 | rtnh->rtnh_len += sizeof(struct rtattr) + 4; | |
1e529305 RP |
992 | } else if (strcmp(*argv, "encap") == 0) { |
993 | int len = rta->rta_len; | |
994 | ||
995 | lwt_parse_encap(rta, 4096, &argc, &argv); | |
996 | rtnh->rtnh_len += rta->rta_len - len; | |
ecc509f9 RP |
997 | } else if (strcmp(*argv, "as") == 0) { |
998 | inet_prefix addr; | |
999 | ||
1000 | NEXT_ARG(); | |
1001 | if (strcmp(*argv, "to") == 0) | |
1002 | NEXT_ARG(); | |
1003 | get_addr(&addr, *argv, r->rtm_family); | |
1004 | rta_addattr_l(rta, 4096, RTA_NEWDST, &addr.data, | |
1005 | addr.bytelen); | |
1006 | rtnh->rtnh_len += sizeof(struct rtattr) + addr.bytelen; | |
aba5acdf SH |
1007 | } else |
1008 | break; | |
1009 | } | |
1010 | *argcp = argc; | |
1011 | *argvp = argv; | |
1012 | return 0; | |
1013 | } | |
1014 | ||
d1f28cf1 SH |
1015 | static int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r, |
1016 | int argc, char **argv) | |
aba5acdf SH |
1017 | { |
1018 | char buf[1024]; | |
56f5daac | 1019 | struct rtattr *rta = (void *)buf; |
aba5acdf SH |
1020 | struct rtnexthop *rtnh; |
1021 | ||
1022 | rta->rta_type = RTA_MULTIPATH; | |
1023 | rta->rta_len = RTA_LENGTH(0); | |
1024 | rtnh = RTA_DATA(rta); | |
1025 | ||
1026 | while (argc > 0) { | |
1027 | if (strcmp(*argv, "nexthop") != 0) { | |
1028 | fprintf(stderr, "Error: \"nexthop\" or end of line is expected instead of \"%s\"\n", *argv); | |
1029 | exit(-1); | |
1030 | } | |
1031 | if (argc <= 1) { | |
1032 | fprintf(stderr, "Error: unexpected end of line after \"nexthop\"\n"); | |
1033 | exit(-1); | |
1034 | } | |
1035 | memset(rtnh, 0, sizeof(*rtnh)); | |
1036 | rtnh->rtnh_len = sizeof(*rtnh); | |
1037 | rta->rta_len += rtnh->rtnh_len; | |
ee53b42f JS |
1038 | if (parse_one_nh(n, r, rta, rtnh, &argc, &argv)) { |
1039 | fprintf(stderr, "Error: cannot parse nexthop\n"); | |
1040 | exit(-1); | |
1041 | } | |
aba5acdf SH |
1042 | rtnh = RTNH_NEXT(rtnh); |
1043 | } | |
1044 | ||
1045 | if (rta->rta_len > RTA_LENGTH(0)) | |
1046 | addattr_l(n, 1024, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta)); | |
1047 | return 0; | |
1048 | } | |
1049 | ||
56f5daac | 1050 | static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv) |
aba5acdf | 1051 | { |
aba5acdf | 1052 | struct { |
4806867a SH |
1053 | struct nlmsghdr n; |
1054 | struct rtmsg r; | |
56f5daac | 1055 | char buf[1024]; |
d17b136f PS |
1056 | } req = { |
1057 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)), | |
1058 | .n.nlmsg_flags = NLM_F_REQUEST | flags, | |
1059 | .n.nlmsg_type = cmd, | |
1060 | .r.rtm_family = preferred_family, | |
1061 | .r.rtm_table = RT_TABLE_MAIN, | |
1062 | .r.rtm_scope = RT_SCOPE_NOWHERE, | |
1063 | }; | |
aba5acdf | 1064 | char mxbuf[256]; |
56f5daac SH |
1065 | struct rtattr *mxrta = (void *)mxbuf; |
1066 | unsigned int mxlock = 0; | |
aba5acdf SH |
1067 | char *d = NULL; |
1068 | int gw_ok = 0; | |
1069 | int dst_ok = 0; | |
1070 | int nhs_ok = 0; | |
1071 | int scope_ok = 0; | |
1072 | int table_ok = 0; | |
54e0b2e7 | 1073 | int raw = 0; |
f638e9f7 | 1074 | int type_ok = 0; |
aba5acdf | 1075 | |
aba5acdf SH |
1076 | if (cmd != RTM_DELROUTE) { |
1077 | req.r.rtm_protocol = RTPROT_BOOT; | |
1078 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; | |
1079 | req.r.rtm_type = RTN_UNICAST; | |
1080 | } | |
1081 | ||
1082 | mxrta->rta_type = RTA_METRICS; | |
1083 | mxrta->rta_len = RTA_LENGTH(0); | |
1084 | ||
1085 | while (argc > 0) { | |
1086 | if (strcmp(*argv, "src") == 0) { | |
1087 | inet_prefix addr; | |
56f5daac | 1088 | |
aba5acdf SH |
1089 | NEXT_ARG(); |
1090 | get_addr(&addr, *argv, req.r.rtm_family); | |
1091 | if (req.r.rtm_family == AF_UNSPEC) | |
1092 | req.r.rtm_family = addr.family; | |
21fef525 SH |
1093 | addattr_l(&req.n, sizeof(req), |
1094 | RTA_PREFSRC, &addr.data, addr.bytelen); | |
6f7a9f4d EB |
1095 | } else if (strcmp(*argv, "as") == 0) { |
1096 | inet_prefix addr; | |
56f5daac | 1097 | |
6f7a9f4d EB |
1098 | NEXT_ARG(); |
1099 | if (strcmp(*argv, "to") == 0) { | |
1100 | NEXT_ARG(); | |
1101 | } | |
1102 | get_addr(&addr, *argv, req.r.rtm_family); | |
1103 | if (req.r.rtm_family == AF_UNSPEC) | |
1104 | req.r.rtm_family = addr.family; | |
21fef525 SH |
1105 | addattr_l(&req.n, sizeof(req), |
1106 | RTA_NEWDST, &addr.data, addr.bytelen); | |
aba5acdf SH |
1107 | } else if (strcmp(*argv, "via") == 0) { |
1108 | inet_prefix addr; | |
93ae2835 | 1109 | int family; |
56f5daac | 1110 | |
11f2c753 DA |
1111 | if (gw_ok) { |
1112 | invarg("use nexthop syntax to specify multiple via\n", | |
1113 | *argv); | |
1114 | } | |
aba5acdf SH |
1115 | gw_ok = 1; |
1116 | NEXT_ARG(); | |
93ae2835 EB |
1117 | family = read_family(*argv); |
1118 | if (family == AF_UNSPEC) | |
1119 | family = req.r.rtm_family; | |
1120 | else | |
1121 | NEXT_ARG(); | |
1122 | get_addr(&addr, *argv, family); | |
aba5acdf SH |
1123 | if (req.r.rtm_family == AF_UNSPEC) |
1124 | req.r.rtm_family = addr.family; | |
93ae2835 | 1125 | if (addr.family == req.r.rtm_family) |
ecc509f9 RP |
1126 | addattr_l(&req.n, sizeof(req), RTA_GATEWAY, |
1127 | &addr.data, addr.bytelen); | |
93ae2835 | 1128 | else |
ecc509f9 RP |
1129 | addattr_l(&req.n, sizeof(req), RTA_VIA, |
1130 | &addr.family, addr.bytelen+2); | |
aba5acdf SH |
1131 | } else if (strcmp(*argv, "from") == 0) { |
1132 | inet_prefix addr; | |
56f5daac | 1133 | |
aba5acdf SH |
1134 | NEXT_ARG(); |
1135 | get_prefix(&addr, *argv, req.r.rtm_family); | |
1136 | if (req.r.rtm_family == AF_UNSPEC) | |
1137 | req.r.rtm_family = addr.family; | |
1138 | if (addr.bytelen) | |
1139 | addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen); | |
1140 | req.r.rtm_src_len = addr.bitlen; | |
1141 | } else if (strcmp(*argv, "tos") == 0 || | |
1142 | matches(*argv, "dsfield") == 0) { | |
1143 | __u32 tos; | |
56f5daac | 1144 | |
aba5acdf SH |
1145 | NEXT_ARG(); |
1146 | if (rtnl_dsfield_a2n(&tos, *argv)) | |
1147 | invarg("\"tos\" value is invalid\n", *argv); | |
1148 | req.r.rtm_tos = tos; | |
56f5daac | 1149 | } else if (strcmp(*argv, "expires") == 0) { |
68eede25 | 1150 | __u32 expires; |
56f5daac | 1151 | |
68eede25 HL |
1152 | NEXT_ARG(); |
1153 | if (get_u32(&expires, *argv, 0)) | |
1154 | invarg("\"expires\" value is invalid\n", *argv); | |
eecc0069 | 1155 | addattr32(&req.n, sizeof(req), RTA_EXPIRES, expires); |
aba5acdf SH |
1156 | } else if (matches(*argv, "metric") == 0 || |
1157 | matches(*argv, "priority") == 0 || | |
194e9b85 | 1158 | strcmp(*argv, "preference") == 0) { |
aba5acdf | 1159 | __u32 metric; |
56f5daac | 1160 | |
aba5acdf SH |
1161 | NEXT_ARG(); |
1162 | if (get_u32(&metric, *argv, 0)) | |
1163 | invarg("\"metric\" value is invalid\n", *argv); | |
1164 | addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric); | |
1165 | } else if (strcmp(*argv, "scope") == 0) { | |
f332d169 | 1166 | __u32 scope = 0; |
56f5daac | 1167 | |
aba5acdf SH |
1168 | NEXT_ARG(); |
1169 | if (rtnl_rtscope_a2n(&scope, *argv)) | |
1170 | invarg("invalid \"scope\" value\n", *argv); | |
1171 | req.r.rtm_scope = scope; | |
1172 | scope_ok = 1; | |
1173 | } else if (strcmp(*argv, "mtu") == 0) { | |
56f5daac SH |
1174 | unsigned int mtu; |
1175 | ||
aba5acdf SH |
1176 | NEXT_ARG(); |
1177 | if (strcmp(*argv, "lock") == 0) { | |
1178 | mxlock |= (1<<RTAX_MTU); | |
1179 | NEXT_ARG(); | |
1180 | } | |
1181 | if (get_unsigned(&mtu, *argv, 0)) | |
1182 | invarg("\"mtu\" value is invalid\n", *argv); | |
1183 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu); | |
71e58151 | 1184 | } else if (strcmp(*argv, "hoplimit") == 0) { |
56f5daac SH |
1185 | unsigned int hoplimit; |
1186 | ||
71e58151 GBY |
1187 | NEXT_ARG(); |
1188 | if (strcmp(*argv, "lock") == 0) { | |
1189 | mxlock |= (1<<RTAX_HOPLIMIT); | |
1190 | NEXT_ARG(); | |
1191 | } | |
ea6cbab7 | 1192 | if (get_unsigned(&hoplimit, *argv, 0) || hoplimit > 255) |
71e58151 GBY |
1193 | invarg("\"hoplimit\" value is invalid\n", *argv); |
1194 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit); | |
aba5acdf | 1195 | } else if (strcmp(*argv, "advmss") == 0) { |
56f5daac SH |
1196 | unsigned int mss; |
1197 | ||
aba5acdf SH |
1198 | NEXT_ARG(); |
1199 | if (strcmp(*argv, "lock") == 0) { | |
1200 | mxlock |= (1<<RTAX_ADVMSS); | |
1201 | NEXT_ARG(); | |
1202 | } | |
1203 | if (get_unsigned(&mss, *argv, 0)) | |
1204 | invarg("\"mss\" value is invalid\n", *argv); | |
1205 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss); | |
aba5acdf | 1206 | } else if (matches(*argv, "reordering") == 0) { |
56f5daac SH |
1207 | unsigned int reord; |
1208 | ||
aba5acdf SH |
1209 | NEXT_ARG(); |
1210 | if (strcmp(*argv, "lock") == 0) { | |
1211 | mxlock |= (1<<RTAX_REORDERING); | |
1212 | NEXT_ARG(); | |
1213 | } | |
1214 | if (get_unsigned(&reord, *argv, 0)) | |
1215 | invarg("\"reordering\" value is invalid\n", *argv); | |
1216 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord); | |
aba5acdf | 1217 | } else if (strcmp(*argv, "rtt") == 0) { |
56f5daac SH |
1218 | unsigned int rtt; |
1219 | ||
aba5acdf SH |
1220 | NEXT_ARG(); |
1221 | if (strcmp(*argv, "lock") == 0) { | |
1222 | mxlock |= (1<<RTAX_RTT); | |
1223 | NEXT_ARG(); | |
1224 | } | |
81d03dc3 | 1225 | if (get_time_rtt(&rtt, *argv, &raw)) |
aba5acdf | 1226 | invarg("\"rtt\" value is invalid\n", *argv); |
0612519e | 1227 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT, |
54e0b2e7 RJ |
1228 | (raw) ? rtt : rtt * 8); |
1229 | } else if (strcmp(*argv, "rto_min") == 0) { | |
56f5daac SH |
1230 | unsigned int rto_min; |
1231 | ||
54e0b2e7 RJ |
1232 | NEXT_ARG(); |
1233 | mxlock |= (1<<RTAX_RTO_MIN); | |
81d03dc3 | 1234 | if (get_time_rtt(&rto_min, *argv, &raw)) |
54e0b2e7 RJ |
1235 | invarg("\"rto_min\" value is invalid\n", |
1236 | *argv); | |
1237 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN, | |
1238 | rto_min); | |
aba5acdf | 1239 | } else if (matches(*argv, "window") == 0) { |
56f5daac SH |
1240 | unsigned int win; |
1241 | ||
aba5acdf SH |
1242 | NEXT_ARG(); |
1243 | if (strcmp(*argv, "lock") == 0) { | |
1244 | mxlock |= (1<<RTAX_WINDOW); | |
1245 | NEXT_ARG(); | |
1246 | } | |
1247 | if (get_unsigned(&win, *argv, 0)) | |
1248 | invarg("\"window\" value is invalid\n", *argv); | |
1249 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win); | |
1250 | } else if (matches(*argv, "cwnd") == 0) { | |
56f5daac SH |
1251 | unsigned int win; |
1252 | ||
aba5acdf SH |
1253 | NEXT_ARG(); |
1254 | if (strcmp(*argv, "lock") == 0) { | |
1255 | mxlock |= (1<<RTAX_CWND); | |
1256 | NEXT_ARG(); | |
1257 | } | |
1258 | if (get_unsigned(&win, *argv, 0)) | |
1259 | invarg("\"cwnd\" value is invalid\n", *argv); | |
1260 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win); | |
a1f1143e | 1261 | } else if (matches(*argv, "initcwnd") == 0) { |
56f5daac SH |
1262 | unsigned int win; |
1263 | ||
a1f1143e VW |
1264 | NEXT_ARG(); |
1265 | if (strcmp(*argv, "lock") == 0) { | |
1266 | mxlock |= (1<<RTAX_INITCWND); | |
1267 | NEXT_ARG(); | |
1268 | } | |
1269 | if (get_unsigned(&win, *argv, 0)) | |
1270 | invarg("\"initcwnd\" value is invalid\n", *argv); | |
21fef525 SH |
1271 | rta_addattr32(mxrta, sizeof(mxbuf), |
1272 | RTAX_INITCWND, win); | |
f5fd8003 | 1273 | } else if (matches(*argv, "initrwnd") == 0) { |
56f5daac SH |
1274 | unsigned int win; |
1275 | ||
f5fd8003 | 1276 | NEXT_ARG(); |
1277 | if (strcmp(*argv, "lock") == 0) { | |
1278 | mxlock |= (1<<RTAX_INITRWND); | |
1279 | NEXT_ARG(); | |
1280 | } | |
1281 | if (get_unsigned(&win, *argv, 0)) | |
1282 | invarg("\"initrwnd\" value is invalid\n", *argv); | |
21fef525 SH |
1283 | rta_addattr32(mxrta, sizeof(mxbuf), |
1284 | RTAX_INITRWND, win); | |
29d1f730 FW |
1285 | } else if (matches(*argv, "features") == 0) { |
1286 | unsigned int features = 0; | |
1287 | ||
1288 | while (argc > 0) { | |
1289 | NEXT_ARG(); | |
1290 | ||
1291 | if (strcmp(*argv, "ecn") == 0) | |
1292 | features |= RTAX_FEATURE_ECN; | |
1293 | else | |
1294 | invarg("\"features\" value not valid\n", *argv); | |
1295 | break; | |
1296 | } | |
1297 | ||
21fef525 SH |
1298 | rta_addattr32(mxrta, sizeof(mxbuf), |
1299 | RTAX_FEATURES, features); | |
b37f2c89 | 1300 | } else if (matches(*argv, "quickack") == 0) { |
56f5daac SH |
1301 | unsigned int quickack; |
1302 | ||
b37f2c89 CW |
1303 | NEXT_ARG(); |
1304 | if (get_unsigned(&quickack, *argv, 0)) | |
1305 | invarg("\"quickack\" value is invalid\n", *argv); | |
1306 | if (quickack != 1 && quickack != 0) | |
1307 | invarg("\"quickack\" value should be 0 or 1\n", *argv); | |
21fef525 SH |
1308 | rta_addattr32(mxrta, sizeof(mxbuf), |
1309 | RTAX_QUICKACK, quickack); | |
6ef87f9c DB |
1310 | } else if (matches(*argv, "congctl") == 0) { |
1311 | NEXT_ARG(); | |
1312 | if (strcmp(*argv, "lock") == 0) { | |
1313 | mxlock |= 1 << RTAX_CC_ALGO; | |
1314 | NEXT_ARG(); | |
1315 | } | |
1316 | rta_addattr_l(mxrta, sizeof(mxbuf), RTAX_CC_ALGO, *argv, | |
1317 | strlen(*argv)); | |
aba5acdf | 1318 | } else if (matches(*argv, "rttvar") == 0) { |
56f5daac SH |
1319 | unsigned int win; |
1320 | ||
aba5acdf SH |
1321 | NEXT_ARG(); |
1322 | if (strcmp(*argv, "lock") == 0) { | |
1323 | mxlock |= (1<<RTAX_RTTVAR); | |
1324 | NEXT_ARG(); | |
1325 | } | |
81d03dc3 | 1326 | if (get_time_rtt(&win, *argv, &raw)) |
aba5acdf | 1327 | invarg("\"rttvar\" value is invalid\n", *argv); |
54e0b2e7 RJ |
1328 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR, |
1329 | (raw) ? win : win * 4); | |
aba5acdf | 1330 | } else if (matches(*argv, "ssthresh") == 0) { |
56f5daac SH |
1331 | unsigned int win; |
1332 | ||
aba5acdf SH |
1333 | NEXT_ARG(); |
1334 | if (strcmp(*argv, "lock") == 0) { | |
1335 | mxlock |= (1<<RTAX_SSTHRESH); | |
1336 | NEXT_ARG(); | |
1337 | } | |
1338 | if (get_unsigned(&win, *argv, 0)) | |
1339 | invarg("\"ssthresh\" value is invalid\n", *argv); | |
1340 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win); | |
1341 | } else if (matches(*argv, "realms") == 0) { | |
1342 | __u32 realm; | |
56f5daac | 1343 | |
aba5acdf | 1344 | NEXT_ARG(); |
d583e88e | 1345 | if (get_rt_realms_or_raw(&realm, *argv)) |
aba5acdf SH |
1346 | invarg("\"realm\" value is invalid\n", *argv); |
1347 | addattr32(&req.n, sizeof(req), RTA_FLOW, realm); | |
1348 | } else if (strcmp(*argv, "onlink") == 0) { | |
1349 | req.r.rtm_flags |= RTNH_F_ONLINK; | |
aba5acdf SH |
1350 | } else if (strcmp(*argv, "nexthop") == 0) { |
1351 | nhs_ok = 1; | |
1352 | break; | |
1353 | } else if (matches(*argv, "protocol") == 0) { | |
f332d169 | 1354 | __u32 prot; |
56f5daac | 1355 | |
aba5acdf SH |
1356 | NEXT_ARG(); |
1357 | if (rtnl_rtprot_a2n(&prot, *argv)) | |
1358 | invarg("\"protocol\" value is invalid\n", *argv); | |
1359 | req.r.rtm_protocol = prot; | |
aba5acdf | 1360 | } else if (matches(*argv, "table") == 0) { |
f332d169 | 1361 | __u32 tid; |
56f5daac | 1362 | |
aba5acdf SH |
1363 | NEXT_ARG(); |
1364 | if (rtnl_rttable_a2n(&tid, *argv)) | |
1365 | invarg("\"table\" value is invalid\n", *argv); | |
34e95647 PM |
1366 | if (tid < 256) |
1367 | req.r.rtm_table = tid; | |
1368 | else { | |
1369 | req.r.rtm_table = RT_TABLE_UNSPEC; | |
1370 | addattr32(&req.n, sizeof(req), RTA_TABLE, tid); | |
1371 | } | |
aba5acdf | 1372 | table_ok = 1; |
0130f012 DA |
1373 | } else if (matches(*argv, "vrf") == 0) { |
1374 | __u32 tid; | |
1375 | ||
1376 | NEXT_ARG(); | |
1377 | tid = ipvrf_get_table(*argv); | |
1378 | if (tid == 0) | |
1379 | invarg("Invalid VRF\n", *argv); | |
1380 | if (tid < 256) | |
1381 | req.r.rtm_table = tid; | |
1382 | else { | |
1383 | req.r.rtm_table = RT_TABLE_UNSPEC; | |
1384 | addattr32(&req.n, sizeof(req), RTA_TABLE, tid); | |
1385 | } | |
1386 | table_ok = 1; | |
aba5acdf SH |
1387 | } else if (strcmp(*argv, "dev") == 0 || |
1388 | strcmp(*argv, "oif") == 0) { | |
1389 | NEXT_ARG(); | |
1390 | d = *argv; | |
194e9b85 LR |
1391 | } else if (matches(*argv, "pref") == 0) { |
1392 | __u8 pref; | |
56f5daac | 1393 | |
194e9b85 LR |
1394 | NEXT_ARG(); |
1395 | if (strcmp(*argv, "low") == 0) | |
1396 | pref = ICMPV6_ROUTER_PREF_LOW; | |
1397 | else if (strcmp(*argv, "medium") == 0) | |
1398 | pref = ICMPV6_ROUTER_PREF_MEDIUM; | |
1399 | else if (strcmp(*argv, "high") == 0) | |
1400 | pref = ICMPV6_ROUTER_PREF_HIGH; | |
1401 | else if (get_u8(&pref, *argv, 0)) | |
1402 | invarg("\"pref\" value is invalid\n", *argv); | |
1403 | addattr8(&req.n, sizeof(req), RTA_PREF, pref); | |
1e529305 RP |
1404 | } else if (strcmp(*argv, "encap") == 0) { |
1405 | char buf[1024]; | |
56f5daac | 1406 | struct rtattr *rta = (void *)buf; |
1e529305 RP |
1407 | |
1408 | rta->rta_type = RTA_ENCAP; | |
1409 | rta->rta_len = RTA_LENGTH(0); | |
1410 | ||
1411 | lwt_parse_encap(rta, sizeof(buf), &argc, &argv); | |
1412 | ||
1413 | if (rta->rta_len > RTA_LENGTH(0)) | |
21fef525 SH |
1414 | addraw_l(&req.n, 1024 |
1415 | , RTA_DATA(rta), RTA_PAYLOAD(rta)); | |
c44d18ea RS |
1416 | } else if (strcmp(*argv, "ttl-propagate") == 0) { |
1417 | __u8 ttl_prop; | |
1418 | ||
1419 | NEXT_ARG(); | |
1420 | if (matches(*argv, "enabled") == 0) | |
1421 | ttl_prop = 1; | |
1422 | else if (matches(*argv, "disabled") == 0) | |
1423 | ttl_prop = 0; | |
1424 | else | |
1425 | invarg("\"ttl-propagate\" value is invalid\n", | |
1426 | *argv); | |
1427 | ||
1428 | addattr8(&req.n, sizeof(req), RTA_TTL_PROPAGATE, | |
1429 | ttl_prop); | |
e54ed380 CP |
1430 | } else if (matches(*argv, "fastopen_no_cookie") == 0) { |
1431 | unsigned int fastopen_no_cookie; | |
1432 | ||
1433 | NEXT_ARG(); | |
1434 | if (get_unsigned(&fastopen_no_cookie, *argv, 0)) | |
1435 | invarg("\"fastopen_no_cookie\" value is invalid\n", *argv); | |
1436 | if (fastopen_no_cookie != 1 && fastopen_no_cookie != 0) | |
1437 | invarg("\"fastopen_no_cookie\" value should be 0 or 1\n", *argv); | |
1438 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_FASTOPEN_NO_COOKIE, fastopen_no_cookie); | |
aba5acdf SH |
1439 | } else { |
1440 | int type; | |
1441 | inet_prefix dst; | |
1442 | ||
1443 | if (strcmp(*argv, "to") == 0) { | |
1444 | NEXT_ARG(); | |
1445 | } | |
1446 | if ((**argv < '0' || **argv > '9') && | |
1447 | rtnl_rtntype_a2n(&type, *argv) == 0) { | |
1448 | NEXT_ARG(); | |
1449 | req.r.rtm_type = type; | |
f638e9f7 | 1450 | type_ok = 1; |
aba5acdf SH |
1451 | } |
1452 | ||
1453 | if (matches(*argv, "help") == 0) | |
1454 | usage(); | |
1455 | if (dst_ok) | |
1456 | duparg2("to", *argv); | |
1457 | get_prefix(&dst, *argv, req.r.rtm_family); | |
1458 | if (req.r.rtm_family == AF_UNSPEC) | |
1459 | req.r.rtm_family = dst.family; | |
1460 | req.r.rtm_dst_len = dst.bitlen; | |
1461 | dst_ok = 1; | |
1462 | if (dst.bytelen) | |
21fef525 SH |
1463 | addattr_l(&req.n, sizeof(req), |
1464 | RTA_DST, &dst.data, dst.bytelen); | |
aba5acdf SH |
1465 | } |
1466 | argc--; argv++; | |
1467 | } | |
1468 | ||
922b4822 PS |
1469 | if (!dst_ok) |
1470 | usage(); | |
1471 | ||
7b201d60 | 1472 | if (d) { |
cc560825 | 1473 | int idx = ll_name_to_index(d); |
aba5acdf | 1474 | |
fe99adbc SP |
1475 | if (!idx) |
1476 | return nodev(d); | |
7b201d60 | 1477 | addattr32(&req.n, sizeof(req), RTA_OIF, idx); |
aba5acdf SH |
1478 | } |
1479 | ||
1480 | if (mxrta->rta_len > RTA_LENGTH(0)) { | |
1481 | if (mxlock) | |
1482 | rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock); | |
1483 | addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta)); | |
1484 | } | |
1485 | ||
1486 | if (nhs_ok) | |
1487 | parse_nexthops(&req.n, &req.r, argc, argv); | |
1488 | ||
f638e9f7 RP |
1489 | if (req.r.rtm_family == AF_UNSPEC) |
1490 | req.r.rtm_family = AF_INET; | |
1491 | ||
aba5acdf SH |
1492 | if (!table_ok) { |
1493 | if (req.r.rtm_type == RTN_LOCAL || | |
1494 | req.r.rtm_type == RTN_BROADCAST || | |
1495 | req.r.rtm_type == RTN_NAT || | |
1496 | req.r.rtm_type == RTN_ANYCAST) | |
1497 | req.r.rtm_table = RT_TABLE_LOCAL; | |
1498 | } | |
1499 | if (!scope_ok) { | |
f638e9f7 RP |
1500 | if (req.r.rtm_family == AF_INET6 || |
1501 | req.r.rtm_family == AF_MPLS) | |
1502 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; | |
1503 | else if (req.r.rtm_type == RTN_LOCAL || | |
1504 | req.r.rtm_type == RTN_NAT) | |
aba5acdf SH |
1505 | req.r.rtm_scope = RT_SCOPE_HOST; |
1506 | else if (req.r.rtm_type == RTN_BROADCAST || | |
1507 | req.r.rtm_type == RTN_MULTICAST || | |
1508 | req.r.rtm_type == RTN_ANYCAST) | |
1509 | req.r.rtm_scope = RT_SCOPE_LINK; | |
1510 | else if (req.r.rtm_type == RTN_UNICAST || | |
1511 | req.r.rtm_type == RTN_UNSPEC) { | |
1512 | if (cmd == RTM_DELROUTE) | |
1513 | req.r.rtm_scope = RT_SCOPE_NOWHERE; | |
1514 | else if (!gw_ok && !nhs_ok) | |
1515 | req.r.rtm_scope = RT_SCOPE_LINK; | |
1516 | } | |
1517 | } | |
1518 | ||
f638e9f7 RP |
1519 | if (!type_ok && req.r.rtm_family == AF_MPLS) |
1520 | req.r.rtm_type = RTN_UNICAST; | |
aba5acdf | 1521 | |
86bf43c7 | 1522 | if (rtnl_talk(&rth, &req.n, NULL) < 0) |
d58ba4ba | 1523 | return -2; |
aba5acdf SH |
1524 | |
1525 | return 0; | |
1526 | } | |
1527 | ||
1528 | static int rtnl_rtcache_request(struct rtnl_handle *rth, int family) | |
1529 | { | |
1530 | struct { | |
1531 | struct nlmsghdr nlh; | |
1532 | struct rtmsg rtm; | |
d17b136f PS |
1533 | } req = { |
1534 | .nlh.nlmsg_len = sizeof(req), | |
1535 | .nlh.nlmsg_type = RTM_GETROUTE, | |
1536 | .nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST, | |
1537 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
1538 | .rtm.rtm_family = family, | |
1539 | .rtm.rtm_flags = RTM_F_CLONED, | |
1540 | }; | |
1541 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; | |
aba5acdf | 1542 | |
56f5daac | 1543 | return sendto(rth->fd, (void *)&req, sizeof(req), 0, (struct sockaddr *)&nladdr, sizeof(nladdr)); |
aba5acdf SH |
1544 | } |
1545 | ||
1546 | static int iproute_flush_cache(void) | |
1547 | { | |
1548 | #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush" | |
1549 | ||
1550 | int len; | |
56f5daac | 1551 | int flush_fd = open(ROUTE_FLUSH_PATH, O_WRONLY); |
aba5acdf SH |
1552 | char *buffer = "-1"; |
1553 | ||
1554 | if (flush_fd < 0) { | |
56f5daac | 1555 | fprintf(stderr, "Cannot open \"%s\": %s\n", |
9827fa57 | 1556 | ROUTE_FLUSH_PATH, strerror(errno)); |
aba5acdf SH |
1557 | return -1; |
1558 | } | |
1559 | ||
56f5daac | 1560 | len = strlen(buffer); |
ae665a52 | 1561 | |
56f5daac SH |
1562 | if ((write(flush_fd, (void *)buffer, len)) < len) { |
1563 | fprintf(stderr, "Cannot flush routing cache\n"); | |
25352af7 | 1564 | close(flush_fd); |
aba5acdf SH |
1565 | return -1; |
1566 | } | |
1567 | close(flush_fd); | |
1568 | return 0; | |
1569 | } | |
1570 | ||
76c61b34 PE |
1571 | static __u32 route_dump_magic = 0x45311224; |
1572 | ||
d1f28cf1 SH |
1573 | static int save_route(const struct sockaddr_nl *who, struct nlmsghdr *n, |
1574 | void *arg) | |
f4ff11e3 DS |
1575 | { |
1576 | int ret; | |
1577 | int len = n->nlmsg_len; | |
1578 | struct rtmsg *r = NLMSG_DATA(n); | |
1579 | struct rtattr *tb[RTA_MAX+1]; | |
f3a2ddc1 | 1580 | int host_len; |
f4ff11e3 | 1581 | |
f3a2ddc1 | 1582 | host_len = af_bit_len(r->rtm_family); |
f4ff11e3 DS |
1583 | len -= NLMSG_LENGTH(sizeof(*r)); |
1584 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); | |
1585 | ||
1586 | if (!filter_nlmsg(n, tb, host_len)) | |
1587 | return 0; | |
1588 | ||
1589 | ret = write(STDOUT_FILENO, n, n->nlmsg_len); | |
1590 | if ((ret > 0) && (ret != n->nlmsg_len)) { | |
1591 | fprintf(stderr, "Short write while saving nlmsg\n"); | |
1592 | ret = -EIO; | |
1593 | } | |
1594 | ||
1595 | return ret == n->nlmsg_len ? 0 : ret; | |
1596 | } | |
aba5acdf | 1597 | |
76c61b34 PE |
1598 | static int save_route_prep(void) |
1599 | { | |
1600 | int ret; | |
1601 | ||
1602 | if (isatty(STDOUT_FILENO)) { | |
14645ec2 | 1603 | fprintf(stderr, "Not sending a binary stream to stdout\n"); |
76c61b34 PE |
1604 | return -1; |
1605 | } | |
1606 | ||
1607 | ret = write(STDOUT_FILENO, &route_dump_magic, sizeof(route_dump_magic)); | |
1608 | if (ret != sizeof(route_dump_magic)) { | |
1609 | fprintf(stderr, "Can't write magic to dump file\n"); | |
1610 | return -1; | |
1611 | } | |
1612 | ||
1613 | return 0; | |
1614 | } | |
1615 | ||
432e5f97 SH |
1616 | static int iproute_flush(int do_ipv6, rtnl_filter_t filter_fn) |
1617 | { | |
1618 | time_t start = time(0); | |
1619 | char flushb[4096-512]; | |
1620 | int round = 0; | |
1621 | int ret; | |
1622 | ||
1623 | if (filter.cloned) { | |
1624 | if (do_ipv6 != AF_INET6) { | |
1625 | iproute_flush_cache(); | |
1626 | if (show_stats) | |
1627 | printf("*** IPv4 routing cache is flushed.\n"); | |
1628 | } | |
1629 | if (do_ipv6 == AF_INET) | |
1630 | return 0; | |
1631 | } | |
1632 | ||
1633 | filter.flushb = flushb; | |
1634 | filter.flushp = 0; | |
1635 | filter.flushe = sizeof(flushb); | |
1636 | ||
1637 | for (;;) { | |
1638 | if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) { | |
1639 | perror("Cannot send dump request"); | |
1640 | return -2; | |
1641 | } | |
1642 | filter.flushed = 0; | |
1643 | if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) { | |
1644 | fprintf(stderr, "Flush terminated\n"); | |
1645 | return -2; | |
1646 | } | |
1647 | if (filter.flushed == 0) { | |
1648 | if (show_stats) { | |
1649 | if (round == 0 && | |
1650 | (!filter.cloned || do_ipv6 == AF_INET6)) | |
1651 | printf("Nothing to flush.\n"); | |
1652 | else | |
1653 | printf("*** Flush is complete after %d round%s ***\n", | |
1654 | round, round > 1 ? "s" : ""); | |
1655 | } | |
1656 | fflush(stdout); | |
1657 | return 0; | |
1658 | } | |
1659 | round++; | |
1660 | ret = flush_update(); | |
1661 | if (ret < 0) | |
1662 | return ret; | |
1663 | ||
1664 | if (time(0) - start > 30) { | |
1665 | printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n", | |
1666 | (long)(time(0) - start), filter.flushed); | |
1667 | return -1; | |
1668 | } | |
1669 | ||
1670 | if (show_stats) { | |
1671 | printf("\n*** Round %d, deleting %d entries ***\n", | |
1672 | round, filter.flushed); | |
1673 | fflush(stdout); | |
1674 | } | |
1675 | } | |
1676 | } | |
1677 | ||
f4ff11e3 | 1678 | static int iproute_list_flush_or_save(int argc, char **argv, int action) |
aba5acdf SH |
1679 | { |
1680 | int do_ipv6 = preferred_family; | |
aba5acdf SH |
1681 | char *id = NULL; |
1682 | char *od = NULL; | |
b0373bfb | 1683 | unsigned int mark = 0; |
f4ff11e3 DS |
1684 | rtnl_filter_t filter_fn; |
1685 | ||
76c61b34 PE |
1686 | if (action == IPROUTE_SAVE) { |
1687 | if (save_route_prep()) | |
1688 | return -1; | |
1689 | ||
f4ff11e3 | 1690 | filter_fn = save_route; |
76c61b34 | 1691 | } else |
f4ff11e3 | 1692 | filter_fn = print_route; |
aba5acdf | 1693 | |
093b7646 | 1694 | iproute_reset_filter(0); |
aba5acdf SH |
1695 | filter.tb = RT_TABLE_MAIN; |
1696 | ||
f4ff11e3 | 1697 | if ((action == IPROUTE_FLUSH) && argc <= 0) { |
aba5acdf SH |
1698 | fprintf(stderr, "\"ip route flush\" requires arguments.\n"); |
1699 | return -1; | |
1700 | } | |
1701 | ||
1702 | while (argc > 0) { | |
1703 | if (matches(*argv, "table") == 0) { | |
f332d169 | 1704 | __u32 tid; |
56f5daac | 1705 | |
aba5acdf SH |
1706 | NEXT_ARG(); |
1707 | if (rtnl_rttable_a2n(&tid, *argv)) { | |
1708 | if (strcmp(*argv, "all") == 0) { | |
bd4bcdad | 1709 | filter.tb = 0; |
aba5acdf | 1710 | } else if (strcmp(*argv, "cache") == 0) { |
bd4bcdad | 1711 | filter.cloned = 1; |
aba5acdf SH |
1712 | } else if (strcmp(*argv, "help") == 0) { |
1713 | usage(); | |
1714 | } else { | |
1715 | invarg("table id value is invalid\n", *argv); | |
1716 | } | |
bd4bcdad PM |
1717 | } else |
1718 | filter.tb = tid; | |
0130f012 DA |
1719 | } else if (matches(*argv, "vrf") == 0) { |
1720 | __u32 tid; | |
1721 | ||
1722 | NEXT_ARG(); | |
1723 | tid = ipvrf_get_table(*argv); | |
1724 | if (tid == 0) | |
1725 | invarg("Invalid VRF\n", *argv); | |
1726 | filter.tb = tid; | |
1727 | filter.typemask = ~(1 << RTN_LOCAL | 1<<RTN_BROADCAST); | |
aba5acdf SH |
1728 | } else if (matches(*argv, "cached") == 0 || |
1729 | matches(*argv, "cloned") == 0) { | |
bd4bcdad | 1730 | filter.cloned = 1; |
aba5acdf SH |
1731 | } else if (strcmp(*argv, "tos") == 0 || |
1732 | matches(*argv, "dsfield") == 0) { | |
1733 | __u32 tos; | |
56f5daac | 1734 | |
aba5acdf SH |
1735 | NEXT_ARG(); |
1736 | if (rtnl_dsfield_a2n(&tos, *argv)) | |
1737 | invarg("TOS value is invalid\n", *argv); | |
1738 | filter.tos = tos; | |
1739 | filter.tosmask = -1; | |
1740 | } else if (matches(*argv, "protocol") == 0) { | |
f332d169 | 1741 | __u32 prot = 0; |
56f5daac | 1742 | |
aba5acdf SH |
1743 | NEXT_ARG(); |
1744 | filter.protocolmask = -1; | |
1745 | if (rtnl_rtprot_a2n(&prot, *argv)) { | |
1746 | if (strcmp(*argv, "all") != 0) | |
1747 | invarg("invalid \"protocol\"\n", *argv); | |
1748 | prot = 0; | |
1749 | filter.protocolmask = 0; | |
1750 | } | |
1751 | filter.protocol = prot; | |
1752 | } else if (matches(*argv, "scope") == 0) { | |
f332d169 | 1753 | __u32 scope = 0; |
56f5daac | 1754 | |
aba5acdf SH |
1755 | NEXT_ARG(); |
1756 | filter.scopemask = -1; | |
1757 | if (rtnl_rtscope_a2n(&scope, *argv)) { | |
1758 | if (strcmp(*argv, "all") != 0) | |
1759 | invarg("invalid \"scope\"\n", *argv); | |
1760 | scope = RT_SCOPE_NOWHERE; | |
1761 | filter.scopemask = 0; | |
1762 | } | |
1763 | filter.scope = scope; | |
1764 | } else if (matches(*argv, "type") == 0) { | |
1765 | int type; | |
56f5daac | 1766 | |
aba5acdf | 1767 | NEXT_ARG(); |
aba5acdf SH |
1768 | if (rtnl_rtntype_a2n(&type, *argv)) |
1769 | invarg("node type value is invalid\n", *argv); | |
d84b1878 | 1770 | filter.typemask = (1<<type); |
aba5acdf SH |
1771 | } else if (strcmp(*argv, "dev") == 0 || |
1772 | strcmp(*argv, "oif") == 0) { | |
1773 | NEXT_ARG(); | |
1774 | od = *argv; | |
1775 | } else if (strcmp(*argv, "iif") == 0) { | |
1776 | NEXT_ARG(); | |
1777 | id = *argv; | |
b0373bfb ED |
1778 | } else if (strcmp(*argv, "mark") == 0) { |
1779 | NEXT_ARG(); | |
7c66d898 PS |
1780 | if (get_unsigned(&mark, *argv, 0)) |
1781 | invarg("invalid mark value", *argv); | |
b0373bfb | 1782 | filter.markmask = -1; |
ed7fdc95 | 1783 | } else if (matches(*argv, "metric") == 0 || |
80d1c528 SH |
1784 | matches(*argv, "priority") == 0 || |
1785 | strcmp(*argv, "preference") == 0) { | |
ed7fdc95 AZ |
1786 | __u32 metric; |
1787 | ||
1788 | NEXT_ARG(); | |
1789 | if (get_u32(&metric, *argv, 0)) | |
1790 | invarg("\"metric\" value is invalid\n", *argv); | |
1791 | filter.metric = metric; | |
1792 | filter.metricmask = -1; | |
aba5acdf | 1793 | } else if (strcmp(*argv, "via") == 0) { |
93ae2835 | 1794 | int family; |
56f5daac | 1795 | |
aba5acdf | 1796 | NEXT_ARG(); |
93ae2835 EB |
1797 | family = read_family(*argv); |
1798 | if (family == AF_UNSPEC) | |
1799 | family = do_ipv6; | |
1800 | else | |
1801 | NEXT_ARG(); | |
1802 | get_prefix(&filter.rvia, *argv, family); | |
aba5acdf SH |
1803 | } else if (strcmp(*argv, "src") == 0) { |
1804 | NEXT_ARG(); | |
1805 | get_prefix(&filter.rprefsrc, *argv, do_ipv6); | |
1806 | } else if (matches(*argv, "realms") == 0) { | |
1807 | __u32 realm; | |
56f5daac | 1808 | |
aba5acdf | 1809 | NEXT_ARG(); |
d583e88e | 1810 | if (get_rt_realms_or_raw(&realm, *argv)) |
aba5acdf SH |
1811 | invarg("invalid realms\n", *argv); |
1812 | filter.realm = realm; | |
1813 | filter.realmmask = ~0U; | |
1814 | if ((filter.realm&0xFFFF) == 0 && | |
1815 | (*argv)[strlen(*argv) - 1] == '/') | |
1816 | filter.realmmask &= ~0xFFFF; | |
1817 | if ((filter.realm&0xFFFF0000U) == 0 && | |
1818 | (strchr(*argv, '/') == NULL || | |
1819 | (*argv)[0] == '/')) | |
1820 | filter.realmmask &= ~0xFFFF0000U; | |
1821 | } else if (matches(*argv, "from") == 0) { | |
1822 | NEXT_ARG(); | |
1823 | if (matches(*argv, "root") == 0) { | |
1824 | NEXT_ARG(); | |
1825 | get_prefix(&filter.rsrc, *argv, do_ipv6); | |
1826 | } else if (matches(*argv, "match") == 0) { | |
1827 | NEXT_ARG(); | |
1828 | get_prefix(&filter.msrc, *argv, do_ipv6); | |
1829 | } else { | |
1830 | if (matches(*argv, "exact") == 0) { | |
1831 | NEXT_ARG(); | |
1832 | } | |
1833 | get_prefix(&filter.msrc, *argv, do_ipv6); | |
1834 | filter.rsrc = filter.msrc; | |
1835 | } | |
1836 | } else { | |
1837 | if (matches(*argv, "to") == 0) { | |
1838 | NEXT_ARG(); | |
1839 | } | |
1840 | if (matches(*argv, "root") == 0) { | |
1841 | NEXT_ARG(); | |
1842 | get_prefix(&filter.rdst, *argv, do_ipv6); | |
1843 | } else if (matches(*argv, "match") == 0) { | |
1844 | NEXT_ARG(); | |
1845 | get_prefix(&filter.mdst, *argv, do_ipv6); | |
1846 | } else { | |
1847 | if (matches(*argv, "exact") == 0) { | |
1848 | NEXT_ARG(); | |
1849 | } | |
1850 | get_prefix(&filter.mdst, *argv, do_ipv6); | |
1851 | filter.rdst = filter.mdst; | |
1852 | } | |
1853 | } | |
1854 | argc--; argv++; | |
1855 | } | |
1856 | ||
1857 | if (do_ipv6 == AF_UNSPEC && filter.tb) | |
1858 | do_ipv6 = AF_INET; | |
1859 | ||
aba5acdf SH |
1860 | if (id || od) { |
1861 | int idx; | |
1862 | ||
1863 | if (id) { | |
cc560825 | 1864 | idx = ll_name_to_index(id); |
fe99adbc SP |
1865 | if (!idx) |
1866 | return nodev(id); | |
aba5acdf SH |
1867 | filter.iif = idx; |
1868 | filter.iifmask = -1; | |
1869 | } | |
1870 | if (od) { | |
cc560825 | 1871 | idx = ll_name_to_index(od); |
fe99adbc SP |
1872 | if (!idx) |
1873 | return nodev(od); | |
aba5acdf SH |
1874 | filter.oif = idx; |
1875 | filter.oifmask = -1; | |
1876 | } | |
1877 | } | |
b0373bfb | 1878 | filter.mark = mark; |
aba5acdf | 1879 | |
432e5f97 SH |
1880 | if (action == IPROUTE_FLUSH) |
1881 | return iproute_flush(do_ipv6, filter_fn); | |
aba5acdf | 1882 | |
bd4bcdad | 1883 | if (!filter.cloned) { |
aba5acdf SH |
1884 | if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) { |
1885 | perror("Cannot send dump request"); | |
2f406f2d | 1886 | return -2; |
aba5acdf SH |
1887 | } |
1888 | } else { | |
1889 | if (rtnl_rtcache_request(&rth, do_ipv6) < 0) { | |
1890 | perror("Cannot send dump request"); | |
2f406f2d | 1891 | return -2; |
aba5acdf SH |
1892 | } |
1893 | } | |
1894 | ||
663c3cb2 SH |
1895 | new_json_obj(json); |
1896 | ||
cd70f3f5 | 1897 | if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) { |
aba5acdf | 1898 | fprintf(stderr, "Dump terminated\n"); |
2f406f2d | 1899 | return -2; |
aba5acdf SH |
1900 | } |
1901 | ||
663c3cb2 SH |
1902 | delete_json_obj(); |
1903 | fflush(stdout); | |
2f406f2d | 1904 | return 0; |
aba5acdf SH |
1905 | } |
1906 | ||
1907 | ||
d1f28cf1 | 1908 | static int iproute_get(int argc, char **argv) |
aba5acdf | 1909 | { |
aba5acdf | 1910 | struct { |
4806867a SH |
1911 | struct nlmsghdr n; |
1912 | struct rtmsg r; | |
56f5daac | 1913 | char buf[1024]; |
d17b136f PS |
1914 | } req = { |
1915 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)), | |
1916 | .n.nlmsg_flags = NLM_F_REQUEST, | |
1917 | .n.nlmsg_type = RTM_GETROUTE, | |
1918 | .r.rtm_family = preferred_family, | |
1919 | }; | |
aba5acdf SH |
1920 | char *idev = NULL; |
1921 | char *odev = NULL; | |
86bf43c7 | 1922 | struct nlmsghdr *answer; |
aba5acdf | 1923 | int connected = 0; |
aa883d86 | 1924 | int fib_match = 0; |
aba5acdf | 1925 | int from_ok = 0; |
b0373bfb | 1926 | unsigned int mark = 0; |
aba5acdf | 1927 | |
093b7646 | 1928 | iproute_reset_filter(0); |
c73f3e02 | 1929 | filter.cloned = 2; |
aba5acdf | 1930 | |
aba5acdf SH |
1931 | while (argc > 0) { |
1932 | if (strcmp(*argv, "tos") == 0 || | |
1933 | matches(*argv, "dsfield") == 0) { | |
1934 | __u32 tos; | |
56f5daac | 1935 | |
aba5acdf SH |
1936 | NEXT_ARG(); |
1937 | if (rtnl_dsfield_a2n(&tos, *argv)) | |
1938 | invarg("TOS value is invalid\n", *argv); | |
1939 | req.r.rtm_tos = tos; | |
1940 | } else if (matches(*argv, "from") == 0) { | |
1941 | inet_prefix addr; | |
56f5daac | 1942 | |
aba5acdf SH |
1943 | NEXT_ARG(); |
1944 | if (matches(*argv, "help") == 0) | |
1945 | usage(); | |
1946 | from_ok = 1; | |
1947 | get_prefix(&addr, *argv, req.r.rtm_family); | |
1948 | if (req.r.rtm_family == AF_UNSPEC) | |
1949 | req.r.rtm_family = addr.family; | |
1950 | if (addr.bytelen) | |
21fef525 SH |
1951 | addattr_l(&req.n, sizeof(req), RTA_SRC, |
1952 | &addr.data, addr.bytelen); | |
aba5acdf SH |
1953 | req.r.rtm_src_len = addr.bitlen; |
1954 | } else if (matches(*argv, "iif") == 0) { | |
1955 | NEXT_ARG(); | |
1956 | idev = *argv; | |
b0373bfb ED |
1957 | } else if (matches(*argv, "mark") == 0) { |
1958 | NEXT_ARG(); | |
7c66d898 PS |
1959 | if (get_unsigned(&mark, *argv, 0)) |
1960 | invarg("invalid mark value", *argv); | |
aba5acdf SH |
1961 | } else if (matches(*argv, "oif") == 0 || |
1962 | strcmp(*argv, "dev") == 0) { | |
1963 | NEXT_ARG(); | |
1964 | odev = *argv; | |
1965 | } else if (matches(*argv, "notify") == 0) { | |
1966 | req.r.rtm_flags |= RTM_F_NOTIFY; | |
1967 | } else if (matches(*argv, "connected") == 0) { | |
1968 | connected = 1; | |
0130f012 DA |
1969 | } else if (matches(*argv, "vrf") == 0) { |
1970 | NEXT_ARG(); | |
1971 | if (!name_is_vrf(*argv)) | |
1972 | invarg("Invalid VRF\n", *argv); | |
1973 | odev = *argv; | |
82252cdc LC |
1974 | } else if (matches(*argv, "uid") == 0) { |
1975 | uid_t uid; | |
1976 | ||
1977 | NEXT_ARG(); | |
1978 | if (get_unsigned(&uid, *argv, 0)) | |
1979 | invarg("invalid UID\n", *argv); | |
1980 | addattr32(&req.n, sizeof(req), RTA_UID, uid); | |
aa883d86 RP |
1981 | } else if (matches(*argv, "fibmatch") == 0) { |
1982 | fib_match = 1; | |
2e86ed54 RP |
1983 | } else if (strcmp(*argv, "as") == 0) { |
1984 | inet_prefix addr; | |
1985 | ||
1986 | NEXT_ARG(); | |
1987 | if (strcmp(*argv, "to") == 0) | |
1988 | NEXT_ARG(); | |
1989 | get_addr(&addr, *argv, req.r.rtm_family); | |
1990 | if (req.r.rtm_family == AF_UNSPEC) | |
1991 | req.r.rtm_family = addr.family; | |
21fef525 SH |
1992 | addattr_l(&req.n, sizeof(req), RTA_NEWDST, |
1993 | &addr.data, addr.bytelen); | |
aba5acdf SH |
1994 | } else { |
1995 | inet_prefix addr; | |
56f5daac | 1996 | |
aba5acdf SH |
1997 | if (strcmp(*argv, "to") == 0) { |
1998 | NEXT_ARG(); | |
1999 | } | |
2000 | if (matches(*argv, "help") == 0) | |
2001 | usage(); | |
2002 | get_prefix(&addr, *argv, req.r.rtm_family); | |
2003 | if (req.r.rtm_family == AF_UNSPEC) | |
2004 | req.r.rtm_family = addr.family; | |
2005 | if (addr.bytelen) | |
21fef525 SH |
2006 | addattr_l(&req.n, sizeof(req), |
2007 | RTA_DST, &addr.data, addr.bytelen); | |
aba5acdf SH |
2008 | req.r.rtm_dst_len = addr.bitlen; |
2009 | } | |
2010 | argc--; argv++; | |
2011 | } | |
2012 | ||
2013 | if (req.r.rtm_dst_len == 0) { | |
14645ec2 | 2014 | fprintf(stderr, "need at least a destination address\n"); |
2f406f2d | 2015 | return -1; |
aba5acdf SH |
2016 | } |
2017 | ||
aba5acdf SH |
2018 | if (idev || odev) { |
2019 | int idx; | |
2020 | ||
2021 | if (idev) { | |
cc560825 | 2022 | idx = ll_name_to_index(idev); |
fe99adbc SP |
2023 | if (!idx) |
2024 | return nodev(idev); | |
aba5acdf SH |
2025 | addattr32(&req.n, sizeof(req), RTA_IIF, idx); |
2026 | } | |
2027 | if (odev) { | |
cc560825 | 2028 | idx = ll_name_to_index(odev); |
fe99adbc SP |
2029 | if (!idx) |
2030 | return nodev(odev); | |
aba5acdf SH |
2031 | addattr32(&req.n, sizeof(req), RTA_OIF, idx); |
2032 | } | |
2033 | } | |
b0373bfb ED |
2034 | if (mark) |
2035 | addattr32(&req.n, sizeof(req), RTA_MARK, mark); | |
aba5acdf SH |
2036 | |
2037 | if (req.r.rtm_family == AF_UNSPEC) | |
2038 | req.r.rtm_family = AF_INET; | |
2039 | ||
bc234301 | 2040 | req.r.rtm_flags |= RTM_F_LOOKUP_TABLE; |
aa883d86 RP |
2041 | if (fib_match) |
2042 | req.r.rtm_flags |= RTM_F_FIB_MATCH; | |
bc234301 | 2043 | |
86bf43c7 | 2044 | if (rtnl_talk(&rth, &req.n, &answer) < 0) |
8b21cef1 | 2045 | return -2; |
aba5acdf SH |
2046 | |
2047 | if (connected && !from_ok) { | |
86bf43c7 HL |
2048 | struct rtmsg *r = NLMSG_DATA(answer); |
2049 | int len = answer->nlmsg_len; | |
56f5daac | 2050 | struct rtattr *tb[RTA_MAX+1]; |
aba5acdf | 2051 | |
86bf43c7 | 2052 | if (print_route(NULL, answer, (void *)stdout) < 0) { |
aba5acdf | 2053 | fprintf(stderr, "An error :-)\n"); |
86bf43c7 | 2054 | free(answer); |
8be29558 | 2055 | return -1; |
aba5acdf SH |
2056 | } |
2057 | ||
86bf43c7 | 2058 | if (answer->nlmsg_type != RTM_NEWROUTE) { |
aba5acdf | 2059 | fprintf(stderr, "Not a route?\n"); |
86bf43c7 | 2060 | free(answer); |
aba5acdf SH |
2061 | return -1; |
2062 | } | |
2063 | len -= NLMSG_LENGTH(sizeof(*r)); | |
2064 | if (len < 0) { | |
2065 | fprintf(stderr, "Wrong len %d\n", len); | |
86bf43c7 | 2066 | free(answer); |
aba5acdf SH |
2067 | return -1; |
2068 | } | |
2069 | ||
aba5acdf SH |
2070 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); |
2071 | ||
2072 | if (tb[RTA_PREFSRC]) { | |
2073 | tb[RTA_PREFSRC]->rta_type = RTA_SRC; | |
2074 | r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]); | |
2075 | } else if (!tb[RTA_SRC]) { | |
2076 | fprintf(stderr, "Failed to connect the route\n"); | |
86bf43c7 | 2077 | free(answer); |
aba5acdf SH |
2078 | return -1; |
2079 | } | |
2080 | if (!odev && tb[RTA_OIF]) | |
2081 | tb[RTA_OIF]->rta_type = 0; | |
2082 | if (tb[RTA_GATEWAY]) | |
2083 | tb[RTA_GATEWAY]->rta_type = 0; | |
93ae2835 EB |
2084 | if (tb[RTA_VIA]) |
2085 | tb[RTA_VIA]->rta_type = 0; | |
aba5acdf SH |
2086 | if (!idev && tb[RTA_IIF]) |
2087 | tb[RTA_IIF]->rta_type = 0; | |
2088 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
2089 | req.n.nlmsg_type = RTM_GETROUTE; | |
2090 | ||
86bf43c7 HL |
2091 | free(answer); |
2092 | if (rtnl_talk(&rth, &req.n, &answer) < 0) | |
56d8ff0a | 2093 | return -2; |
aba5acdf SH |
2094 | } |
2095 | ||
86bf43c7 | 2096 | if (print_route(NULL, answer, (void *)stdout) < 0) { |
aba5acdf | 2097 | fprintf(stderr, "An error :-)\n"); |
86bf43c7 | 2098 | free(answer); |
56d8ff0a | 2099 | return -1; |
aba5acdf SH |
2100 | } |
2101 | ||
86bf43c7 | 2102 | free(answer); |
56d8ff0a | 2103 | return 0; |
aba5acdf SH |
2104 | } |
2105 | ||
79f4a393 | 2106 | static int rtattr_cmp(const struct rtattr *rta1, const struct rtattr *rta2) |
74af8dd9 XL |
2107 | { |
2108 | if (!rta1 || !rta2 || rta1->rta_len != rta2->rta_len) | |
2109 | return 1; | |
2110 | ||
2111 | return memcmp(RTA_DATA(rta1), RTA_DATA(rta2), RTA_PAYLOAD(rta1)); | |
2112 | } | |
2113 | ||
0628cddd ND |
2114 | static int restore_handler(const struct sockaddr_nl *nl, |
2115 | struct rtnl_ctrl_data *ctrl, | |
2116 | struct nlmsghdr *n, void *arg) | |
f4ff11e3 | 2117 | { |
74af8dd9 XL |
2118 | struct rtmsg *r = NLMSG_DATA(n); |
2119 | struct rtattr *tb[RTA_MAX+1]; | |
2120 | int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)); | |
2121 | int ret, prio = *(int *)arg; | |
2122 | ||
2123 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); | |
f4ff11e3 | 2124 | |
74af8dd9 XL |
2125 | /* Restore routes in correct order: |
2126 | * 0. ones for local addresses, | |
2127 | * 1. ones for local networks, | |
2128 | * 2. others (remote networks/hosts). | |
2129 | */ | |
2130 | if (!prio && !tb[RTA_GATEWAY] && (!tb[RTA_PREFSRC] || | |
2131 | !rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST]))) | |
2132 | goto restore; | |
c85703bb | 2133 | else if (prio == 1 && !tb[RTA_GATEWAY] && tb[RTA_PREFSRC] && |
74af8dd9 XL |
2134 | rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST])) |
2135 | goto restore; | |
2136 | else if (prio == 2 && tb[RTA_GATEWAY]) | |
2137 | goto restore; | |
2138 | ||
2139 | return 0; | |
2140 | ||
2141 | restore: | |
f4ff11e3 DS |
2142 | n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK; |
2143 | ||
2144 | ll_init_map(&rth); | |
2145 | ||
86bf43c7 | 2146 | ret = rtnl_talk(&rth, n, NULL); |
f4ff11e3 DS |
2147 | if ((ret < 0) && (errno == EEXIST)) |
2148 | ret = 0; | |
2149 | ||
2150 | return ret; | |
2151 | } | |
2152 | ||
76c61b34 PE |
2153 | static int route_dump_check_magic(void) |
2154 | { | |
2155 | int ret; | |
2156 | __u32 magic = 0; | |
2157 | ||
2158 | if (isatty(STDIN_FILENO)) { | |
2159 | fprintf(stderr, "Can't restore route dump from a terminal\n"); | |
2160 | return -1; | |
2161 | } | |
2162 | ||
2163 | ret = fread(&magic, sizeof(magic), 1, stdin); | |
2164 | if (magic != route_dump_magic) { | |
2165 | fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n", ret, magic); | |
2166 | return -1; | |
2167 | } | |
2168 | ||
2169 | return 0; | |
2170 | } | |
2171 | ||
d1f28cf1 | 2172 | static int iproute_restore(void) |
f4ff11e3 | 2173 | { |
74af8dd9 XL |
2174 | int pos, prio; |
2175 | ||
76c61b34 | 2176 | if (route_dump_check_magic()) |
2f406f2d | 2177 | return -1; |
76c61b34 | 2178 | |
74af8dd9 | 2179 | pos = ftell(stdin); |
bffb68b6 HL |
2180 | if (pos == -1) { |
2181 | perror("Failed to restore: ftell"); | |
2f406f2d | 2182 | return -1; |
bffb68b6 HL |
2183 | } |
2184 | ||
74af8dd9 XL |
2185 | for (prio = 0; prio < 3; prio++) { |
2186 | int err; | |
2187 | ||
2188 | err = rtnl_from_file(stdin, &restore_handler, &prio); | |
2189 | if (err) | |
2f406f2d | 2190 | return -2; |
74af8dd9 | 2191 | |
bffb68b6 HL |
2192 | if (fseek(stdin, pos, SEEK_SET) == -1) { |
2193 | perror("Failed to restore: fseek"); | |
2f406f2d | 2194 | return -1; |
bffb68b6 | 2195 | } |
74af8dd9 XL |
2196 | } |
2197 | ||
2f406f2d | 2198 | return 0; |
f4ff11e3 DS |
2199 | } |
2200 | ||
0628cddd ND |
2201 | static int show_handler(const struct sockaddr_nl *nl, |
2202 | struct rtnl_ctrl_data *ctrl, | |
2203 | struct nlmsghdr *n, void *arg) | |
93b79863 PE |
2204 | { |
2205 | print_route(nl, n, stdout); | |
2206 | return 0; | |
2207 | } | |
2208 | ||
2209 | static int iproute_showdump(void) | |
2210 | { | |
2211 | if (route_dump_check_magic()) | |
2f406f2d | 2212 | return -1; |
93b79863 | 2213 | |
2f406f2d ÉB |
2214 | if (rtnl_from_file(stdin, &show_handler, NULL)) |
2215 | return -2; | |
2216 | ||
2217 | return 0; | |
93b79863 PE |
2218 | } |
2219 | ||
093b7646 | 2220 | void iproute_reset_filter(int ifindex) |
aba5acdf SH |
2221 | { |
2222 | memset(&filter, 0, sizeof(filter)); | |
2223 | filter.mdst.bitlen = -1; | |
2224 | filter.msrc.bitlen = -1; | |
093b7646 | 2225 | filter.oif = ifindex; |
2226 | if (filter.oif > 0) | |
2227 | filter.oifmask = -1; | |
aba5acdf SH |
2228 | } |
2229 | ||
2230 | int do_iproute(int argc, char **argv) | |
2231 | { | |
2232 | if (argc < 1) | |
f4ff11e3 | 2233 | return iproute_list_flush_or_save(0, NULL, IPROUTE_LIST); |
ae665a52 | 2234 | |
aba5acdf SH |
2235 | if (matches(*argv, "add") == 0) |
2236 | return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL, | |
2237 | argc-1, argv+1); | |
2238 | if (matches(*argv, "change") == 0 || strcmp(*argv, "chg") == 0) | |
2239 | return iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE, | |
2240 | argc-1, argv+1); | |
2241 | if (matches(*argv, "replace") == 0) | |
2242 | return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE, | |
2243 | argc-1, argv+1); | |
2244 | if (matches(*argv, "prepend") == 0) | |
2245 | return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE, | |
2246 | argc-1, argv+1); | |
2247 | if (matches(*argv, "append") == 0) | |
2248 | return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND, | |
2249 | argc-1, argv+1); | |
2250 | if (matches(*argv, "test") == 0) | |
2251 | return iproute_modify(RTM_NEWROUTE, NLM_F_EXCL, | |
2252 | argc-1, argv+1); | |
2253 | if (matches(*argv, "delete") == 0) | |
2254 | return iproute_modify(RTM_DELROUTE, 0, | |
2255 | argc-1, argv+1); | |
2256 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 | |
2257 | || matches(*argv, "lst") == 0) | |
f4ff11e3 | 2258 | return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_LIST); |
aba5acdf SH |
2259 | if (matches(*argv, "get") == 0) |
2260 | return iproute_get(argc-1, argv+1); | |
2261 | if (matches(*argv, "flush") == 0) | |
f4ff11e3 DS |
2262 | return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_FLUSH); |
2263 | if (matches(*argv, "save") == 0) | |
2264 | return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_SAVE); | |
2265 | if (matches(*argv, "restore") == 0) | |
2266 | return iproute_restore(); | |
93b79863 PE |
2267 | if (matches(*argv, "showdump") == 0) |
2268 | return iproute_showdump(); | |
aba5acdf SH |
2269 | if (matches(*argv, "help") == 0) |
2270 | usage(); | |
80d1c528 SH |
2271 | |
2272 | fprintf(stderr, | |
2273 | "Command \"%s\" is unknown, try \"ip route help\".\n", *argv); | |
aba5acdf SH |
2274 | exit(-1); |
2275 | } |