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