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