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