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