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