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