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