a7ec66c7f974ffe9321e6cb8bf5f2db730cf4f85
[oweals/busybox.git] / networking / libiproute / iproute.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * iproute.c            "ip route".
4  *
5  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6  *
7  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8  *
9  *
10  * Changes:
11  *
12  * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
13  * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
14  */
15
16 #include "ip_common.h"  /* #include "libbb.h" is inside */
17 #include "rt_names.h"
18 #include "utils.h"
19
20 #ifndef RTAX_RTTVAR
21 #define RTAX_RTTVAR RTAX_HOPS
22 #endif
23
24
25 typedef struct filter_t {
26         int tb;
27         smallint flushed;
28         char *flushb;
29         int flushp;
30         int flushe;
31         struct rtnl_handle *rth;
32         int protocol, protocolmask;
33         int scope, scopemask;
34         int type, typemask;
35         int tos, tosmask;
36         int iif, iifmask;
37         int oif, oifmask;
38         int realm, realmmask;
39         inet_prefix rprefsrc;
40         inet_prefix rvia;
41         inet_prefix rdst;
42         inet_prefix mdst;
43         inet_prefix rsrc;
44         inet_prefix msrc;
45 } filter_t;
46
47 #define filter (*(filter_t*)&bb_common_bufsiz1)
48
49 static int flush_update(void)
50 {
51         if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
52                 bb_perror_msg("failed to send flush request");
53                 return -1;
54         }
55         filter.flushp = 0;
56         return 0;
57 }
58
59 static unsigned get_hz(void)
60 {
61         static unsigned hz_internal;
62         FILE *fp;
63
64         if (hz_internal)
65                 return hz_internal;
66
67         fp = fopen_for_read("/proc/net/psched");
68         if (fp) {
69                 unsigned nom, denom;
70
71                 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
72                         if (nom == 1000000)
73                                 hz_internal = denom;
74                 fclose(fp);
75         }
76         if (!hz_internal)
77                 hz_internal = sysconf(_SC_CLK_TCK);
78         return hz_internal;
79 }
80
81 static int print_route(const struct sockaddr_nl *who UNUSED_PARAM,
82                 struct nlmsghdr *n, void *arg UNUSED_PARAM)
83 {
84         struct rtmsg *r = NLMSG_DATA(n);
85         int len = n->nlmsg_len;
86         struct rtattr * tb[RTA_MAX+1];
87         char abuf[256];
88         inet_prefix dst;
89         inet_prefix src;
90         int host_len = -1;
91         SPRINT_BUF(b1);
92
93         if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
94                 fprintf(stderr, "Not a route: %08x %08x %08x\n",
95                         n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
96                 return 0;
97         }
98         if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
99                 return 0;
100         len -= NLMSG_LENGTH(sizeof(*r));
101         if (len < 0)
102                 bb_error_msg_and_die("wrong nlmsg len %d", len);
103
104         if (r->rtm_family == AF_INET6)
105                 host_len = 128;
106         else if (r->rtm_family == AF_INET)
107                 host_len = 32;
108
109         if (r->rtm_family == AF_INET6) {
110                 if (filter.tb) {
111                         if (filter.tb < 0) {
112                                 if (!(r->rtm_flags & RTM_F_CLONED)) {
113                                         return 0;
114                                 }
115                         } else {
116                                 if (r->rtm_flags & RTM_F_CLONED) {
117                                         return 0;
118                                 }
119                                 if (filter.tb == RT_TABLE_LOCAL) {
120                                         if (r->rtm_type != RTN_LOCAL) {
121                                                 return 0;
122                                         }
123                                 } else if (filter.tb == RT_TABLE_MAIN) {
124                                         if (r->rtm_type == RTN_LOCAL) {
125                                                 return 0;
126                                         }
127                                 } else {
128                                         return 0;
129                                 }
130                         }
131                 }
132         } else {
133                 if (filter.tb > 0 && filter.tb != r->rtm_table) {
134                         return 0;
135                 }
136         }
137         if (filter.rdst.family &&
138             (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len)) {
139                 return 0;
140         }
141         if (filter.mdst.family &&
142             (r->rtm_family != filter.mdst.family ||
143              (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) {
144                 return 0;
145         }
146         if (filter.rsrc.family &&
147             (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len)) {
148                 return 0;
149         }
150         if (filter.msrc.family &&
151             (r->rtm_family != filter.msrc.family ||
152              (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) {
153                 return 0;
154         }
155
156         memset(tb, 0, sizeof(tb));
157         parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
158
159         if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
160                 return 0;
161         if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
162             inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
163                 return 0;
164
165         if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
166                 return 0;
167         if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
168             inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
169                 return 0;
170
171         if (filter.flushb &&
172             r->rtm_family == AF_INET6 &&
173             r->rtm_dst_len == 0 &&
174             r->rtm_type == RTN_UNREACHABLE &&
175             tb[RTA_PRIORITY] &&
176             *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
177                 return 0;
178
179         if (filter.flushb) {
180                 struct nlmsghdr *fn;
181                 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
182                         if (flush_update())
183                                 bb_error_msg_and_die("flush");
184                 }
185                 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
186                 memcpy(fn, n, n->nlmsg_len);
187                 fn->nlmsg_type = RTM_DELROUTE;
188                 fn->nlmsg_flags = NLM_F_REQUEST;
189                 fn->nlmsg_seq = ++filter.rth->seq;
190                 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
191                 filter.flushed = 1;
192                 return 0;
193         }
194
195         if (n->nlmsg_type == RTM_DELROUTE) {
196                 printf("Deleted ");
197         }
198         if (r->rtm_type != RTN_UNICAST && !filter.type) {
199                 printf("%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
200         }
201
202         if (tb[RTA_DST]) {
203                 if (r->rtm_dst_len != host_len) {
204                         printf("%s/%u ", rt_addr_n2a(r->rtm_family,
205                                                 RTA_PAYLOAD(tb[RTA_DST]),
206                                                 RTA_DATA(tb[RTA_DST]),
207                                                 abuf, sizeof(abuf)),
208                                         r->rtm_dst_len
209                                         );
210                 } else {
211                         printf("%s ", format_host(r->rtm_family,
212                                                 RTA_PAYLOAD(tb[RTA_DST]),
213                                                 RTA_DATA(tb[RTA_DST]),
214                                                 abuf, sizeof(abuf))
215                                         );
216                 }
217         } else if (r->rtm_dst_len) {
218                 printf("0/%d ", r->rtm_dst_len);
219         } else {
220                 printf("default ");
221         }
222         if (tb[RTA_SRC]) {
223                 if (r->rtm_src_len != host_len) {
224                         printf("from %s/%u ", rt_addr_n2a(r->rtm_family,
225                                                 RTA_PAYLOAD(tb[RTA_SRC]),
226                                                 RTA_DATA(tb[RTA_SRC]),
227                                                 abuf, sizeof(abuf)),
228                                         r->rtm_src_len
229                                         );
230                 } else {
231                         printf("from %s ", format_host(r->rtm_family,
232                                                 RTA_PAYLOAD(tb[RTA_SRC]),
233                                                 RTA_DATA(tb[RTA_SRC]),
234                                                 abuf, sizeof(abuf))
235                                         );
236                 }
237         } else if (r->rtm_src_len) {
238                 printf("from 0/%u ", r->rtm_src_len);
239         }
240         if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
241                 printf("via %s ", format_host(r->rtm_family,
242                                         RTA_PAYLOAD(tb[RTA_GATEWAY]),
243                                         RTA_DATA(tb[RTA_GATEWAY]),
244                                         abuf, sizeof(abuf)));
245         }
246         if (tb[RTA_OIF] && filter.oifmask != -1) {
247                 printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
248         }
249
250         if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
251                 /* Do not use format_host(). It is our local addr
252                    and symbolic name will not be useful.
253                  */
254                 printf(" src %s ", rt_addr_n2a(r->rtm_family,
255                                         RTA_PAYLOAD(tb[RTA_PREFSRC]),
256                                         RTA_DATA(tb[RTA_PREFSRC]),
257                                         abuf, sizeof(abuf)));
258         }
259         if (tb[RTA_PRIORITY]) {
260                 printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
261         }
262         if (r->rtm_family == AF_INET6) {
263                 struct rta_cacheinfo *ci = NULL;
264                 if (tb[RTA_CACHEINFO]) {
265                         ci = RTA_DATA(tb[RTA_CACHEINFO]);
266                 }
267                 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
268                         if (r->rtm_flags & RTM_F_CLONED) {
269                                 printf("%c    cache ", _SL_);
270                         }
271                         if (ci->rta_expires) {
272                                 printf(" expires %dsec", ci->rta_expires / get_hz());
273                         }
274                         if (ci->rta_error != 0) {
275                                 printf(" error %d", ci->rta_error);
276                         }
277                 } else if (ci) {
278                         if (ci->rta_error != 0)
279                                 printf(" error %d", ci->rta_error);
280                 }
281         }
282         if (tb[RTA_IIF] && filter.iifmask != -1) {
283                 printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
284         }
285         bb_putchar('\n');
286         return 0;
287 }
288
289 /* Return value becomes exitcode. It's okay to not return at all */
290 static int iproute_modify(int cmd, unsigned flags, char **argv)
291 {
292         static const char keywords[] ALIGN1 =
293                 "src\0""via\0""mtu\0""lock\0""protocol\0"USE_FEATURE_IP_RULE("table\0")
294                 "dev\0""oif\0""to\0""metric\0";
295         enum {
296                 ARG_src,
297                 ARG_via,
298                 ARG_mtu, PARM_lock,
299                 ARG_protocol,
300 USE_FEATURE_IP_RULE(ARG_table,)
301                 ARG_dev,
302                 ARG_oif,
303                 ARG_to,
304                 ARG_metric,
305         };
306         enum {
307                 gw_ok = 1 << 0,
308                 dst_ok = 1 << 1,
309                 proto_ok = 1 << 2,
310                 type_ok = 1 << 3
311         };
312         struct rtnl_handle rth;
313         struct {
314                 struct nlmsghdr         n;
315                 struct rtmsg            r;
316                 char                    buf[1024];
317         } req;
318         char mxbuf[256];
319         struct rtattr * mxrta = (void*)mxbuf;
320         unsigned mxlock = 0;
321         char *d = NULL;
322         smalluint ok = 0;
323         int arg;
324
325         memset(&req, 0, sizeof(req));
326
327         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
328         req.n.nlmsg_flags = NLM_F_REQUEST | flags;
329         req.n.nlmsg_type = cmd;
330         req.r.rtm_family = preferred_family;
331         if (RT_TABLE_MAIN) /* if it is zero, memset already did it */
332                 req.r.rtm_table = RT_TABLE_MAIN;
333         if (RT_SCOPE_NOWHERE)
334                 req.r.rtm_scope = RT_SCOPE_NOWHERE;
335
336         if (cmd != RTM_DELROUTE) {
337                 req.r.rtm_protocol = RTPROT_BOOT;
338                 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
339                 req.r.rtm_type = RTN_UNICAST;
340         }
341
342         mxrta->rta_type = RTA_METRICS;
343         mxrta->rta_len = RTA_LENGTH(0);
344
345         while (*argv) {
346                 arg = index_in_substrings(keywords, *argv);
347                 if (arg == ARG_src) {
348                         inet_prefix addr;
349                         NEXT_ARG();
350                         get_addr(&addr, *argv, req.r.rtm_family);
351                         if (req.r.rtm_family == AF_UNSPEC)
352                                 req.r.rtm_family = addr.family;
353                         addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
354                 } else if (arg == ARG_via) {
355                         inet_prefix addr;
356                         ok |= gw_ok;
357                         NEXT_ARG();
358                         get_addr(&addr, *argv, req.r.rtm_family);
359                         if (req.r.rtm_family == AF_UNSPEC) {
360                                 req.r.rtm_family = addr.family;
361                         }
362                         addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
363                 } else if (arg == ARG_mtu) {
364                         unsigned mtu;
365                         NEXT_ARG();
366                         if (index_in_strings(keywords, *argv) == PARM_lock) {
367                                 mxlock |= (1 << RTAX_MTU);
368                                 NEXT_ARG();
369                         }
370                         if (get_unsigned(&mtu, *argv, 0))
371                                 invarg(*argv, "mtu");
372                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
373                 } else if (arg == ARG_protocol) {
374                         uint32_t prot;
375                         NEXT_ARG();
376                         if (rtnl_rtprot_a2n(&prot, *argv))
377                                 invarg(*argv, "protocol");
378                         req.r.rtm_protocol = prot;
379                         ok |= proto_ok;
380 #if ENABLE_FEATURE_IP_RULE
381                 } else if (arg == ARG_table) {
382                         uint32_t tid;
383                         NEXT_ARG();
384                         if (rtnl_rttable_a2n(&tid, *argv))
385                                 invarg(*argv, "table");
386                         req.r.rtm_table = tid;
387 #endif
388                 } else if (arg == ARG_dev || arg == ARG_oif) {
389                         NEXT_ARG();
390                         d = *argv;
391                 } else if (arg == ARG_metric) {
392                         uint32_t metric;
393                         NEXT_ARG();
394                         if (get_u32(&metric, *argv, 0))
395                                 invarg(*argv, "metric");
396                         addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
397                 } else {
398                         int type;
399                         inet_prefix dst;
400
401                         if (arg == ARG_to) {
402                                 NEXT_ARG();
403                         }
404                         if ((**argv < '0' || **argv > '9')
405                          && rtnl_rtntype_a2n(&type, *argv) == 0) {
406                                 NEXT_ARG();
407                                 req.r.rtm_type = type;
408                                 ok |= type_ok;
409                         }
410
411                         if (ok & dst_ok) {
412                                 duparg2("to", *argv);
413                         }
414                         get_prefix(&dst, *argv, req.r.rtm_family);
415                         if (req.r.rtm_family == AF_UNSPEC) {
416                                 req.r.rtm_family = dst.family;
417                         }
418                         req.r.rtm_dst_len = dst.bitlen;
419                         ok |= dst_ok;
420                         if (dst.bytelen) {
421                                 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
422                         }
423                 }
424                 argv++;
425         }
426
427         xrtnl_open(&rth);
428
429         if (d)  {
430                 int idx;
431
432                 ll_init_map(&rth);
433
434                 if (d) {
435                         idx = xll_name_to_index(d);
436                         addattr32(&req.n, sizeof(req), RTA_OIF, idx);
437                 }
438         }
439
440         if (mxrta->rta_len > RTA_LENGTH(0)) {
441                 if (mxlock) {
442                         rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
443                 }
444                 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
445         }
446
447         if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
448                 req.r.rtm_scope = RT_SCOPE_HOST;
449         else if (req.r.rtm_type == RTN_BROADCAST ||
450                         req.r.rtm_type == RTN_MULTICAST ||
451                         req.r.rtm_type == RTN_ANYCAST)
452                 req.r.rtm_scope = RT_SCOPE_LINK;
453         else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
454                 if (cmd == RTM_DELROUTE)
455                         req.r.rtm_scope = RT_SCOPE_NOWHERE;
456                 else if (!(ok & gw_ok))
457                         req.r.rtm_scope = RT_SCOPE_LINK;
458         }
459
460         if (req.r.rtm_family == AF_UNSPEC) {
461                 req.r.rtm_family = AF_INET;
462         }
463
464         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
465                 return 2;
466         }
467
468         return 0;
469 }
470
471 static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
472 {
473         struct {
474                 struct nlmsghdr nlh;
475                 struct rtmsg rtm;
476         } req;
477         struct sockaddr_nl nladdr;
478
479         memset(&nladdr, 0, sizeof(nladdr));
480         memset(&req, 0, sizeof(req));
481         nladdr.nl_family = AF_NETLINK;
482
483         req.nlh.nlmsg_len = sizeof(req);
484         if (RTM_GETROUTE)
485                 req.nlh.nlmsg_type = RTM_GETROUTE;
486         if (NLM_F_ROOT | NLM_F_REQUEST)
487                 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
488         /*req.nlh.nlmsg_pid = 0; - memset did it already */
489         req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
490         req.rtm.rtm_family = family;
491         if (RTM_F_CLONED)
492                 req.rtm.rtm_flags = RTM_F_CLONED;
493
494         return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
495 }
496
497 static void iproute_flush_cache(void)
498 {
499         static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
500         int flush_fd = open_or_warn(fn, O_WRONLY);
501
502         if (flush_fd < 0) {
503                 return;
504         }
505
506         if (write(flush_fd, "-1", 2) < 2) {
507                 bb_perror_msg("cannot flush routing cache");
508                 return;
509         }
510         close(flush_fd);
511 }
512
513 static void iproute_reset_filter(void)
514 {
515         memset(&filter, 0, sizeof(filter));
516         filter.mdst.bitlen = -1;
517         filter.msrc.bitlen = -1;
518 }
519
520 /* Return value becomes exitcode. It's okay to not return at all */
521 static int iproute_list_or_flush(char **argv, int flush)
522 {
523         int do_ipv6 = preferred_family;
524         struct rtnl_handle rth;
525         char *id = NULL;
526         char *od = NULL;
527         static const char keywords[] ALIGN1 =
528                 /* "ip route list/flush" parameters: */
529                 "protocol\0" "dev\0"   "oif\0"   "iif\0"
530                 "via\0"      "table\0" "cache\0"
531                 "from\0"     "to\0"
532                 /* and possible further keywords */
533                 "all\0"
534                 "root\0"
535                 "match\0"
536                 "exact\0"
537                 "main\0"
538                 ;
539         enum {
540                 KW_proto, KW_dev,   KW_oif,  KW_iif,
541                 KW_via,   KW_table, KW_cache,
542                 KW_from,  KW_to,
543                 /* */
544                 KW_all,
545                 KW_root,
546                 KW_match,
547                 KW_exact,
548                 KW_main,
549         };
550         int arg, parm;
551
552         iproute_reset_filter();
553         filter.tb = RT_TABLE_MAIN;
554
555         if (flush && !*argv)
556                 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
557
558         while (*argv) {
559                 arg = index_in_substrings(keywords, *argv);
560                 if (arg == KW_proto) {
561                         uint32_t prot = 0;
562                         NEXT_ARG();
563                         filter.protocolmask = -1;
564                         if (rtnl_rtprot_a2n(&prot, *argv)) {
565                                 if (index_in_strings(keywords, *argv) != KW_all)
566                                         invarg(*argv, "protocol");
567                                 prot = 0;
568                                 filter.protocolmask = 0;
569                         }
570                         filter.protocol = prot;
571                 } else if (arg == KW_dev || arg == KW_oif) {
572                         NEXT_ARG();
573                         od = *argv;
574                 } else if (arg == KW_iif) {
575                         NEXT_ARG();
576                         id = *argv;
577                 } else if (arg == KW_via) {
578                         NEXT_ARG();
579                         get_prefix(&filter.rvia, *argv, do_ipv6);
580                 } else if (arg == KW_table) { /* table all/cache/main */
581                         NEXT_ARG();
582                         parm = index_in_substrings(keywords, *argv);
583                         if (parm == KW_cache)
584                                 filter.tb = -1;
585                         else if (parm == KW_all)
586                                 filter.tb = 0;
587                         else if (parm != KW_main) {
588 #if ENABLE_FEATURE_IP_RULE
589                                 uint32_t tid;
590                                 if (rtnl_rttable_a2n(&tid, *argv))
591                                         invarg(*argv, "table");
592                                 filter.tb = tid;
593 #else
594                                 invarg(*argv, "table");
595 #endif
596                         }
597                 } else if (arg == KW_cache) {
598                         /* The command 'ip route flush cache' is used by OpenSWAN.
599                          * Assuming it's a synonym for 'ip route flush table cache' */
600                         filter.tb = -1;
601                 } else if (arg == KW_from) {
602                         NEXT_ARG();
603                         parm = index_in_substrings(keywords, *argv);
604                         if (parm == KW_root) {
605                                 NEXT_ARG();
606                                 get_prefix(&filter.rsrc, *argv, do_ipv6);
607                         } else if (parm == KW_match) {
608                                 NEXT_ARG();
609                                 get_prefix(&filter.msrc, *argv, do_ipv6);
610                         } else {
611                                 if (parm == KW_exact)
612                                         NEXT_ARG();
613                                 get_prefix(&filter.msrc, *argv, do_ipv6);
614                                 filter.rsrc = filter.msrc;
615                         }
616                 } else { /* "to" is the default parameter */
617                         if (arg == KW_to) {
618                                 NEXT_ARG();
619                                 arg = index_in_substrings(keywords, *argv);
620                         }
621                         /* parm = arg; - would be more plausible, but we reuse 'arg' here */
622                         if (arg == KW_root) {
623                                 NEXT_ARG();
624                                 get_prefix(&filter.rdst, *argv, do_ipv6);
625                         } else if (arg == KW_match) {
626                                 NEXT_ARG();
627                                 get_prefix(&filter.mdst, *argv, do_ipv6);
628                         } else { /* "to exact" is the default */
629                                 if (arg == KW_exact)
630                                         NEXT_ARG();
631                                 get_prefix(&filter.mdst, *argv, do_ipv6);
632                                 filter.rdst = filter.mdst;
633                         }
634                 }
635                 argv++;
636         }
637
638         if (do_ipv6 == AF_UNSPEC && filter.tb) {
639                 do_ipv6 = AF_INET;
640         }
641
642         xrtnl_open(&rth);
643         ll_init_map(&rth);
644
645         if (id || od)  {
646                 int idx;
647
648                 if (id) {
649                         idx = xll_name_to_index(id);
650                         filter.iif = idx;
651                         filter.iifmask = -1;
652                 }
653                 if (od) {
654                         idx = xll_name_to_index(od);
655                         filter.oif = idx;
656                         filter.oifmask = -1;
657                 }
658         }
659
660         if (flush) {
661                 char flushb[4096-512];
662
663                 if (filter.tb == -1) { /* "flush table cache" */
664                         if (do_ipv6 != AF_INET6)
665                                 iproute_flush_cache();
666                         if (do_ipv6 == AF_INET)
667                                 return 0;
668                 }
669
670                 filter.flushb = flushb;
671                 filter.flushp = 0;
672                 filter.flushe = sizeof(flushb);
673                 filter.rth = &rth;
674
675                 for (;;) {
676                         xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
677                         filter.flushed = 0;
678                         xrtnl_dump_filter(&rth, print_route, NULL);
679                         if (filter.flushed == 0)
680                                 return 0;
681                         if (flush_update())
682                                 return 1;
683                 }
684         }
685
686         if (filter.tb != -1) {
687                 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
688         } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
689                 bb_perror_msg_and_die("cannot send dump request");
690         }
691         xrtnl_dump_filter(&rth, print_route, NULL);
692
693         return 0;
694 }
695
696
697 /* Return value becomes exitcode. It's okay to not return at all */
698 static int iproute_get(char **argv)
699 {
700         struct rtnl_handle rth;
701         struct {
702                 struct nlmsghdr n;
703                 struct rtmsg    r;
704                 char            buf[1024];
705         } req;
706         char *idev = NULL;
707         char *odev = NULL;
708         bool connected = 0;
709         bool from_ok = 0;
710         static const char options[] ALIGN1 =
711                 "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
712
713         memset(&req, 0, sizeof(req));
714
715         iproute_reset_filter();
716
717         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
718         if (NLM_F_REQUEST)
719                 req.n.nlmsg_flags = NLM_F_REQUEST;
720         if (RTM_GETROUTE)
721                 req.n.nlmsg_type = RTM_GETROUTE;
722         req.r.rtm_family = preferred_family;
723         /*req.r.rtm_table = 0; - memset did this already */
724         /*req.r.rtm_protocol = 0;*/
725         /*req.r.rtm_scope = 0;*/
726         /*req.r.rtm_type = 0;*/
727         /*req.r.rtm_src_len = 0;*/
728         /*req.r.rtm_dst_len = 0;*/
729         /*req.r.rtm_tos = 0;*/
730
731         while (*argv) {
732                 switch (index_in_strings(options, *argv)) {
733                         case 0: /* from */
734                         {
735                                 inet_prefix addr;
736                                 NEXT_ARG();
737                                 from_ok = 1;
738                                 get_prefix(&addr, *argv, req.r.rtm_family);
739                                 if (req.r.rtm_family == AF_UNSPEC) {
740                                         req.r.rtm_family = addr.family;
741                                 }
742                                 if (addr.bytelen) {
743                                         addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
744                                 }
745                                 req.r.rtm_src_len = addr.bitlen;
746                                 break;
747                         }
748                         case 1: /* iif */
749                                 NEXT_ARG();
750                                 idev = *argv;
751                                 break;
752                         case 2: /* oif */
753                         case 3: /* dev */
754                                 NEXT_ARG();
755                                 odev = *argv;
756                                 break;
757                         case 4: /* notify */
758                                 req.r.rtm_flags |= RTM_F_NOTIFY;
759                                 break;
760                         case 5: /* connected */
761                                 connected = 1;
762                                 break;
763                         case 6: /* to */
764                                 NEXT_ARG();
765                         default:
766                         {
767                                 inet_prefix addr;
768                                 get_prefix(&addr, *argv, req.r.rtm_family);
769                                 if (req.r.rtm_family == AF_UNSPEC) {
770                                         req.r.rtm_family = addr.family;
771                                 }
772                                 if (addr.bytelen) {
773                                         addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
774                                 }
775                                 req.r.rtm_dst_len = addr.bitlen;
776                         }
777                         argv++;
778                 }
779         }
780
781         if (req.r.rtm_dst_len == 0) {
782                 bb_error_msg_and_die("need at least destination address");
783         }
784
785         xrtnl_open(&rth);
786
787         ll_init_map(&rth);
788
789         if (idev || odev)  {
790                 int idx;
791
792                 if (idev) {
793                         idx = xll_name_to_index(idev);
794                         addattr32(&req.n, sizeof(req), RTA_IIF, idx);
795                 }
796                 if (odev) {
797                         idx = xll_name_to_index(odev);
798                         addattr32(&req.n, sizeof(req), RTA_OIF, idx);
799                 }
800         }
801
802         if (req.r.rtm_family == AF_UNSPEC) {
803                 req.r.rtm_family = AF_INET;
804         }
805
806         if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
807                 return 2;
808         }
809
810         if (connected && !from_ok) {
811                 struct rtmsg *r = NLMSG_DATA(&req.n);
812                 int len = req.n.nlmsg_len;
813                 struct rtattr * tb[RTA_MAX+1];
814
815                 print_route(NULL, &req.n, NULL);
816
817                 if (req.n.nlmsg_type != RTM_NEWROUTE) {
818                         bb_error_msg_and_die("not a route?");
819                 }
820                 len -= NLMSG_LENGTH(sizeof(*r));
821                 if (len < 0) {
822                         bb_error_msg_and_die("wrong len %d", len);
823                 }
824
825                 memset(tb, 0, sizeof(tb));
826                 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
827
828                 if (tb[RTA_PREFSRC]) {
829                         tb[RTA_PREFSRC]->rta_type = RTA_SRC;
830                         r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
831                 } else if (!tb[RTA_SRC]) {
832                         bb_error_msg_and_die("failed to connect the route");
833                 }
834                 if (!odev && tb[RTA_OIF]) {
835                         tb[RTA_OIF]->rta_type = 0;
836                 }
837                 if (tb[RTA_GATEWAY]) {
838                         tb[RTA_GATEWAY]->rta_type = 0;
839                 }
840                 if (!idev && tb[RTA_IIF]) {
841                         tb[RTA_IIF]->rta_type = 0;
842                 }
843                 req.n.nlmsg_flags = NLM_F_REQUEST;
844                 req.n.nlmsg_type = RTM_GETROUTE;
845
846                 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
847                         return 2;
848                 }
849         }
850         print_route(NULL, &req.n, NULL);
851         return 0;
852 }
853
854 /* Return value becomes exitcode. It's okay to not return at all */
855 int do_iproute(char **argv)
856 {
857         static const char ip_route_commands[] ALIGN1 =
858         /*0-3*/ "add\0""append\0""change\0""chg\0"
859         /*4-7*/ "delete\0""get\0""list\0""show\0"
860         /*8..*/ "prepend\0""replace\0""test\0""flush\0";
861         int command_num;
862         unsigned flags = 0;
863         int cmd = RTM_NEWROUTE;
864
865         if (!*argv)
866                 return iproute_list_or_flush(argv, 0);
867
868         /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
869         /* It probably means that it is using "first match" rule */
870         command_num = index_in_substrings(ip_route_commands, *argv);
871
872         switch (command_num) {
873                 case 0: /* add */
874                         flags = NLM_F_CREATE|NLM_F_EXCL;
875                         break;
876                 case 1: /* append */
877                         flags = NLM_F_CREATE|NLM_F_APPEND;
878                         break;
879                 case 2: /* change */
880                 case 3: /* chg */
881                         flags = NLM_F_REPLACE;
882                         break;
883                 case 4: /* delete */
884                         cmd = RTM_DELROUTE;
885                         break;
886                 case 5: /* get */
887                         return iproute_get(argv+1);
888                 case 6: /* list */
889                 case 7: /* show */
890                         return iproute_list_or_flush(argv+1, 0);
891                 case 8: /* prepend */
892                         flags = NLM_F_CREATE;
893                         break;
894                 case 9: /* replace */
895                         flags = NLM_F_CREATE|NLM_F_REPLACE;
896                         break;
897                 case 10: /* test */
898                         flags = NLM_F_EXCL;
899                         break;
900                 case 11: /* flush */
901                         return iproute_list_or_flush(argv+1, 1);
902                 default:
903                         bb_error_msg_and_die("unknown command %s", *argv);
904         }
905
906         return iproute_modify(cmd, flags, argv+1);
907 }