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