trylink: produce even more info about final link stage
[oweals/busybox.git] / networking / libiproute / iproute.c
index e251b941e485e0a7c7acabe529c0ae4ae3455c4f..fbc7210496f4948895922f87fb7fc74e0e200e3e 100644 (file)
@@ -1,10 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * iproute.c           "ip route".
  *
- *             This program is free software; you can redistribute it and/or
- *             modify it under the terms of the GNU General Public License
- *             as published by the Free Software Foundation; either version
- *             2 of the License, or (at your option) any later version.
+ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  *
  * Authors:    Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  *
  * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <syslog.h>
-#include <fcntl.h>
-#include <string.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netinet/ip.h>
-#include <arpa/inet.h>
-#include <linux/in_route.h>
-
+#include "ip_common.h" /* #include "libbb.h" is inside */
 #include "rt_names.h"
 #include "utils.h"
-#include "ip_common.h"
-
-#include "busybox.h"
 
 #ifndef RTAX_RTTVAR
 #define RTAX_RTTVAR RTAX_HOPS
 #endif
 
 
-static struct
-{
+typedef struct filter_t {
        int tb;
+       int flushed;
+       char *flushb;
        int flushp;
        int flushe;
        struct rtnl_handle *rth;
@@ -58,29 +42,66 @@ static struct
        inet_prefix mdst;
        inet_prefix rsrc;
        inet_prefix msrc;
-} filter;
+} filter_t;
+
+#define filter (*(filter_t*)&bb_common_bufsiz1)
+
+static int flush_update(void)
+{
+       if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
+               bb_perror_msg("failed to send flush request");
+               return -1;
+       }
+       filter.flushp = 0;
+       return 0;
+}
+
+static unsigned get_hz(void)
+{
+       static unsigned hz_internal;
+       FILE *fp;
+
+       if (hz_internal)
+               return hz_internal;
+
+       fp = fopen("/proc/net/psched", "r");
+       if (fp) {
+               unsigned nom, denom;
+
+               if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
+                       if (nom == 1000000)
+                               hz_internal = denom;
+               fclose(fp);
+       }
+       if (!hz_internal)
+               hz_internal = sysconf(_SC_CLK_TCK);
+       return hz_internal;
+}
 
-int print_route(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
+static int print_route(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
+               struct nlmsghdr *n, void *arg)
 {
        FILE *fp = (FILE*)arg;
        struct rtmsg *r = NLMSG_DATA(n);
        int len = n->nlmsg_len;
        struct rtattr * tb[RTA_MAX+1];
        char abuf[256];
+       inet_prefix dst;
+       inet_prefix src;
        int host_len = -1;
        SPRINT_BUF(b1);
-       
+
 
        if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
                fprintf(stderr, "Not a route: %08x %08x %08x\n",
                        n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
                return 0;
        }
+       if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
+               return 0;
        len -= NLMSG_LENGTH(sizeof(*r));
-       if (len < 0) {
-               error_msg("wrong nlmsg len %d", len);
-               return -1;
-       }
+       if (len < 0)
+               bb_error_msg_and_die("wrong nlmsg len %d", len);
 
        if (r->rtm_family == AF_INET6)
                host_len = 128;
@@ -137,6 +158,42 @@ int print_route(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
        memset(tb, 0, sizeof(tb));
        parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
 
+       if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
+               return 0;
+       if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
+           inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
+               return 0;
+
+       if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
+               return 0;
+       if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
+           inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
+               return 0;
+
+       if (filter.flushb &&
+           r->rtm_family == AF_INET6 &&
+           r->rtm_dst_len == 0 &&
+           r->rtm_type == RTN_UNREACHABLE &&
+           tb[RTA_PRIORITY] &&
+           *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
+               return 0;
+
+       if (filter.flushb) {
+               struct nlmsghdr *fn;
+               if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
+                       if (flush_update())
+                               bb_error_msg_and_die("flush");
+               }
+               fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
+               memcpy(fn, n, n->nlmsg_len);
+               fn->nlmsg_type = RTM_DELROUTE;
+               fn->nlmsg_flags = NLM_F_REQUEST;
+               fn->nlmsg_seq = ++filter.rth->seq;
+               filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
+               filter.flushed++;
+               return 0;
+       }
+
        if (n->nlmsg_type == RTM_DELROUTE) {
                fprintf(fp, "Deleted ");
        }
@@ -183,7 +240,7 @@ int print_route(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                fprintf(fp, "from 0/%u ", r->rtm_src_len);
        }
        if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
-               fprintf(fp, "via %s ", 
+               fprintf(fp, "via %s ",
                        format_host(r->rtm_family,
                                    RTA_PAYLOAD(tb[RTA_GATEWAY]),
                                    RTA_DATA(tb[RTA_GATEWAY]),
@@ -197,14 +254,14 @@ int print_route(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                /* Do not use format_host(). It is our local addr
                   and symbolic name will not be useful.
                 */
-               fprintf(fp, " src %s ", 
+               fprintf(fp, " src %s ",
                        rt_addr_n2a(r->rtm_family,
                                    RTA_PAYLOAD(tb[RTA_PREFSRC]),
                                    RTA_DATA(tb[RTA_PREFSRC]),
                                    abuf, sizeof(abuf)));
        }
        if (tb[RTA_PRIORITY]) {
-               fprintf(fp, " metric %d ", *(__u32*)RTA_DATA(tb[RTA_PRIORITY]));
+               fprintf(fp, " metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
        }
        if (r->rtm_family == AF_INET6) {
                struct rta_cacheinfo *ci = NULL;
@@ -212,15 +269,11 @@ int print_route(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                        ci = RTA_DATA(tb[RTA_CACHEINFO]);
                }
                if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
-                       static int hz;
-                       if (!hz) {
-                               hz = get_hz();
-                       }
                        if (r->rtm_flags & RTM_F_CLONED) {
-                               fprintf(fp, "%s    cache ", _SL_);
+                               fprintf(fp, "%c    cache ", _SL_);
                        }
                        if (ci->rta_expires) {
-                               fprintf(fp, " expires %dsec", ci->rta_expires/hz);
+                               fprintf(fp, " expires %dsec", ci->rta_expires / get_hz());
                        }
                        if (ci->rta_error != 0) {
                                fprintf(fp, " error %d", ci->rta_error);
@@ -233,27 +286,45 @@ int print_route(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
        if (tb[RTA_IIF] && filter.iifmask != -1) {
                fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
        }
-       fprintf(fp, "\n");
+       fputc('\n', fp);
        fflush(fp);
        return 0;
 }
 
-int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
+/* Return value becomes exitcode. It's okay to not return at all */
+static int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
 {
+       static const char keywords[] ALIGN1 =
+               "src\0""via\0""mtu\0""lock\0""protocol\0"USE_FEATURE_IP_RULE("table\0")
+               "dev\0""oif\0""to\0";
+       enum {
+               ARG_src,
+               ARG_via,
+               ARG_mtu, PARM_lock,
+               ARG_protocol,
+USE_FEATURE_IP_RULE(ARG_table,)
+               ARG_dev,
+               ARG_oif,
+               ARG_to
+       };
+       enum {
+               gw_ok = 1 << 0,
+               dst_ok = 1 << 1,
+               proto_ok = 1 << 2,
+               type_ok = 1 << 3
+       };
        struct rtnl_handle rth;
        struct {
-               struct nlmsghdr         n;
-               struct rtmsg            r;
-               char                    buf[1024];
+               struct nlmsghdr         n;
+               struct rtmsg            r;
+               char                    buf[1024];
        } req;
-       char  mxbuf[256];
+       char mxbuf[256];
        struct rtattr * mxrta = (void*)mxbuf;
        unsigned mxlock = 0;
-       char  *d = NULL;
-       int gw_ok = 0;
-       int dst_ok = 0;
-       int proto_ok = 0;
-       int type_ok = 0;
+       char *d = NULL;
+       smalluint ok = 0;
+       int arg;
 
        memset(&req, 0, sizeof(req));
 
@@ -274,60 +345,66 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
        mxrta->rta_len = RTA_LENGTH(0);
 
        while (argc > 0) {
-               if (strcmp(*argv, "src") == 0) {
+               arg = index_in_substrings(keywords, *argv);
+               if (arg == ARG_src) {
                        inet_prefix addr;
                        NEXT_ARG();
                        get_addr(&addr, *argv, req.r.rtm_family);
-                       if (req.r.rtm_family == AF_UNSPEC) {
+                       if (req.r.rtm_family == AF_UNSPEC)
                                req.r.rtm_family = addr.family;
-                       }
                        addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
-               } else if (strcmp(*argv, "via") == 0) {
+               } else if (arg == ARG_via) {
                        inet_prefix addr;
-                       gw_ok = 1;
+                       ok |= gw_ok;
                        NEXT_ARG();
                        get_addr(&addr, *argv, req.r.rtm_family);
                        if (req.r.rtm_family == AF_UNSPEC) {
                                req.r.rtm_family = addr.family;
                        }
                        addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
-               } else if (strcmp(*argv, "mtu") == 0) {
+               } else if (arg == ARG_mtu) {
                        unsigned mtu;
                        NEXT_ARG();
-                       if (strcmp(*argv, "lock") == 0) {
+                       if (index_in_strings(keywords, *argv) == PARM_lock) {
                                mxlock |= (1<<RTAX_MTU);
                                NEXT_ARG();
                        }
-                       if (get_unsigned(&mtu, *argv, 0)) {
-                               invarg("\"mtu\" value is invalid\n", *argv);
-                       }
+                       if (get_unsigned(&mtu, *argv, 0))
+                               invarg(*argv, "mtu");
                        rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
-               } else if (matches(*argv, "protocol") == 0) {
-                       int prot;
+               } else if (arg == ARG_protocol) {
+                       uint32_t prot;
                        NEXT_ARG();
                        if (rtnl_rtprot_a2n(&prot, *argv))
-                               invarg("\"protocol\" value is invalid\n", *argv);
+                               invarg(*argv, "protocol");
                        req.r.rtm_protocol = prot;
-                       proto_ok =1;
-               } else if (strcmp(*argv, "dev") == 0 ||
-                          strcmp(*argv, "oif") == 0) {
+                       ok |= proto_ok;
+#if ENABLE_FEATURE_IP_RULE
+               } else if (arg == ARG_table) {
+                       uint32_t tid;
+                       NEXT_ARG();
+                       if (rtnl_rttable_a2n(&tid, *argv))
+                               invarg(*argv, "table");
+                       req.r.rtm_table = tid;
+#endif
+               } else if (arg == ARG_dev || arg == ARG_oif) {
                        NEXT_ARG();
                        d = *argv;
                } else {
                        int type;
                        inet_prefix dst;
 
-                       if (strcmp(*argv, "to") == 0) {
+                       if (arg == ARG_to) {
                                NEXT_ARG();
                        }
-                       if ((**argv < '0' || **argv > '9') &&
-                           rtnl_rtntype_a2n(&type, *argv) == 0) {
+                       if ((**argv < '0' || **argv > '9')
+                               && rtnl_rtntype_a2n(&type, *argv) == 0) {
                                NEXT_ARG();
                                req.r.rtm_type = type;
-                               type_ok = 1;
+                               ok |= type_ok;
                        }
 
-                       if (dst_ok) {
+                       if (ok & dst_ok) {
                                duparg2("to", *argv);
                        }
                        get_prefix(&dst, *argv, req.r.rtm_family);
@@ -335,7 +412,7 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
                                req.r.rtm_family = dst.family;
                        }
                        req.r.rtm_dst_len = dst.bitlen;
-                       dst_ok = 1;
+                       ok |= dst_ok;
                        if (dst.bytelen) {
                                addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
                        }
@@ -343,9 +420,7 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
                argc--; argv++;
        }
 
-       if (rtnl_open(&rth, 0) < 0) {
-               exit(1);
-       }
+       xrtnl_open(&rth);
 
        if (d)  {
                int idx;
@@ -353,10 +428,7 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
                ll_init_map(&rth);
 
                if (d) {
-                       if ((idx = ll_name_to_index(d)) == 0) {
-                               error_msg("Cannot find device \"%s\"", d);
-                               return -1;
-                       }
+                       idx = xll_name_to_index(d);
                        addattr32(&req.n, sizeof(req), RTA_OIF, idx);
                }
        }
@@ -368,12 +440,25 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
                addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
        }
 
+       if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
+               req.r.rtm_scope = RT_SCOPE_HOST;
+       else if (req.r.rtm_type == RTN_BROADCAST ||
+                       req.r.rtm_type == RTN_MULTICAST ||
+                       req.r.rtm_type == RTN_ANYCAST)
+               req.r.rtm_scope = RT_SCOPE_LINK;
+       else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
+               if (cmd == RTM_DELROUTE)
+                       req.r.rtm_scope = RT_SCOPE_NOWHERE;
+               else if (!(ok & gw_ok))
+                       req.r.rtm_scope = RT_SCOPE_LINK;
+       }
+
        if (req.r.rtm_family == AF_UNSPEC) {
                req.r.rtm_family = AF_INET;
        }
 
        if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
-               exit(2);
+               return 2;
        }
 
        return 0;
@@ -399,82 +484,133 @@ static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
        req.rtm.rtm_family = family;
        req.rtm.rtm_flags |= RTM_F_CLONED;
 
-       return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
+       return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
 }
 
-static int iproute_list(int argc, char **argv)
+static void iproute_flush_cache(void)
+{
+       static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
+       int flush_fd = open_or_warn(fn, O_WRONLY);
+
+       if (flush_fd < 0) {
+               return;
+       }
+
+       if (write(flush_fd, "-1", 2) < 2) {
+               bb_perror_msg("cannot flush routing cache");
+               return;
+       }
+       close(flush_fd);
+}
+
+static void iproute_reset_filter(void)
+{
+       memset(&filter, 0, sizeof(filter));
+       filter.mdst.bitlen = -1;
+       filter.msrc.bitlen = -1;
+}
+
+/* Return value becomes exitcode. It's okay to not return at all */
+static int iproute_list_or_flush(int argc, char **argv, int flush)
 {
        int do_ipv6 = preferred_family;
        struct rtnl_handle rth;
        char *id = NULL;
        char *od = NULL;
-
+       static const char keywords[] ALIGN1 =
+               "protocol\0""all\0""dev\0""oif\0""iif\0""via\0""table\0""cache\0" /*all*/
+               "from\0""root\0""match\0""exact\0""to\0"/*root match exact*/;
+       enum {
+               ARG_proto, PARM_all,
+               ARG_dev,
+               ARG_oif,
+               ARG_iif,
+               ARG_via,
+               ARG_table, PARM_cache, /*PARM_all,*/
+               ARG_from, PARM_root, PARM_match, PARM_exact,
+               ARG_to  /*PARM_root, PARM_match, PARM_exact*/
+       };
+       int arg, parm;
        iproute_reset_filter();
        filter.tb = RT_TABLE_MAIN;
 
+       if (flush && argc <= 0)
+               bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
+
        while (argc > 0) {
-               if (matches(*argv, "protocol") == 0) {
-                       int prot = 0;
+               arg = index_in_substrings(keywords, *argv);
+               if (arg == ARG_proto) {
+                       uint32_t prot = 0;
                        NEXT_ARG();
                        filter.protocolmask = -1;
                        if (rtnl_rtprot_a2n(&prot, *argv)) {
-                               if (strcmp(*argv, "all") != 0) {
-                                       invarg("invalid \"protocol\"\n", *argv);
-                               }
+                               if (index_in_strings(keywords, *argv) != PARM_all)
+                                       invarg(*argv, "protocol");
                                prot = 0;
                                filter.protocolmask = 0;
                        }
                        filter.protocol = prot;
-               } else if (strcmp(*argv, "dev") == 0 ||
-                          strcmp(*argv, "oif") == 0) {
+               } else if (arg == ARG_dev || arg == ARG_oif) {
                        NEXT_ARG();
                        od = *argv;
-               } else if (strcmp(*argv, "iif") == 0) {
+               } else if (arg == ARG_iif) {
                        NEXT_ARG();
                        id = *argv;
-               } else if (matches(*argv, "from") == 0) {
+               } else if (arg == ARG_via) {
+                       NEXT_ARG();
+                       get_prefix(&filter.rvia, *argv, do_ipv6);
+               } else if (arg == ARG_table) {
                        NEXT_ARG();
-                       if (matches(*argv, "root") == 0) {
+                       parm = index_in_substrings(keywords, *argv);
+                       if (parm == PARM_cache)
+                               filter.tb = -1;
+                       else if (parm == PARM_all)
+                               filter.tb = 0;
+                       else
+                               invarg(*argv, "table");
+               } else if (arg == ARG_from) {
+                       NEXT_ARG();
+                       parm = index_in_substrings(keywords, *argv);
+                       if (parm == PARM_root) {
                                NEXT_ARG();
                                get_prefix(&filter.rsrc, *argv, do_ipv6);
-                       } else if (matches(*argv, "match") == 0) {
+                       } else if (parm == PARM_match) {
                                NEXT_ARG();
                                get_prefix(&filter.msrc, *argv, do_ipv6);
                        } else {
-                               if (matches(*argv, "exact") == 0) {
+                               if (parm == PARM_exact)
                                        NEXT_ARG();
-                               }
                                get_prefix(&filter.msrc, *argv, do_ipv6);
                                filter.rsrc = filter.msrc;
                        }
                } else {
-                       if (matches(*argv, "to") == 0) {
+                       /* parm = arg; // would be more plausible, we reuse arg here */
+                       if (arg == ARG_to) {
                                NEXT_ARG();
+                               arg = index_in_substrings(keywords, *argv);
                        }
-                       if (matches(*argv, "root") == 0) {
+                       if (arg == PARM_root) {
                                NEXT_ARG();
                                get_prefix(&filter.rdst, *argv, do_ipv6);
-                       } else if (matches(*argv, "match") == 0) {
+                       } else if (arg == PARM_match) {
                                NEXT_ARG();
                                get_prefix(&filter.mdst, *argv, do_ipv6);
                        } else {
-                               if (matches(*argv, "exact") == 0) {
+                               if (arg == PARM_exact)
                                        NEXT_ARG();
-                               }
                                get_prefix(&filter.mdst, *argv, do_ipv6);
                                filter.rdst = filter.mdst;
                        }
                }
-               argc--; argv++;
+               argc--;
+               argv++;
        }
 
        if (do_ipv6 == AF_UNSPEC && filter.tb) {
                do_ipv6 = AF_INET;
        }
 
-       if (rtnl_open(&rth, 0) < 0) {
-               exit(1);
-       }
+       xrtnl_open(&rth);
 
        ll_init_map(&rth);
 
@@ -482,52 +618,69 @@ static int iproute_list(int argc, char **argv)
                int idx;
 
                if (id) {
-                       if ((idx = ll_name_to_index(id)) == 0) {
-                               error_msg("Cannot find device \"%s\"", id);
-                               return -1;
-                       }
+                       idx = xll_name_to_index(id);
                        filter.iif = idx;
                        filter.iifmask = -1;
                }
                if (od) {
-                       if ((idx = ll_name_to_index(od)) == 0) {
-                               error_msg("Cannot find device \"%s\"", od);
-                       }
+                       idx = xll_name_to_index(od);
                        filter.oif = idx;
                        filter.oifmask = -1;
                }
        }
 
-       if (filter.tb != -1) {
-               if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
-                       perror_msg_and_die("Cannot send dump request");
+       if (flush) {
+               char flushb[4096-512];
+
+               if (filter.tb == -1) {
+                       if (do_ipv6 != AF_INET6)
+                               iproute_flush_cache();
+                       if (do_ipv6 == AF_INET)
+                               return 0;
                }
-       } else {
-               if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
-                       perror_msg_and_die("Cannot send dump request");
+
+               filter.flushb = flushb;
+               filter.flushp = 0;
+               filter.flushe = sizeof(flushb);
+               filter.rth = &rth;
+
+               for (;;) {
+                       xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
+                       filter.flushed = 0;
+                       xrtnl_dump_filter(&rth, print_route, stdout);
+                       if (filter.flushed == 0)
+                               return 0;
+                       if (flush_update())
+                               return 1;
                }
        }
 
-       if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
-               error_msg_and_die"Dump terminated");
+       if (filter.tb != -1) {
+               xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
+       } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
+               bb_perror_msg_and_die("cannot send dump request");
        }
+       xrtnl_dump_filter(&rth, print_route, stdout);
 
-       exit(0);
+       return 0;
 }
 
 
-int iproute_get(int argc, char **argv)
+/* Return value becomes exitcode. It's okay to not return at all */
+static int iproute_get(int argc, char **argv)
 {
        struct rtnl_handle rth;
        struct {
-               struct nlmsghdr         n;
-               struct rtmsg            r;
-               char                    buf[1024];
+               struct nlmsghdr n;
+               struct rtmsg    r;
+               char            buf[1024];
        } req;
-       char  *idev = NULL;
-       char  *odev = NULL;
-       int connected = 0;
-       int from_ok = 0;
+       char *idev = NULL;
+       char *odev = NULL;
+       bool connected = 0;
+       bool from_ok = 0;
+       static const char options[] ALIGN1 =
+               "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
 
        memset(&req, 0, sizeof(req));
 
@@ -544,50 +697,62 @@ int iproute_get(int argc, char **argv)
        req.r.rtm_src_len = 0;
        req.r.rtm_dst_len = 0;
        req.r.rtm_tos = 0;
-       
+
        while (argc > 0) {
-               if (matches(*argv, "from") == 0) {
-                       inet_prefix addr;
-                       NEXT_ARG();
-                       from_ok = 1;
-                       get_prefix(&addr, *argv, req.r.rtm_family);
-                       if (req.r.rtm_family == AF_UNSPEC)
-                               req.r.rtm_family = addr.family;
-                       if (addr.bytelen)
-                               addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
-                       req.r.rtm_src_len = addr.bitlen;
-               } else if (matches(*argv, "iif") == 0) {
-                       NEXT_ARG();
-                       idev = *argv;
-               } else if (matches(*argv, "oif") == 0 ||
-                          strcmp(*argv, "dev") == 0) {
-                       NEXT_ARG();
-                       odev = *argv;
-               } else if (matches(*argv, "notify") == 0) {
-                       req.r.rtm_flags |= RTM_F_NOTIFY;
-               } else if (matches(*argv, "connected") == 0) {
-                       connected = 1;
-               } else {
-                       inet_prefix addr;
-                       if (strcmp(*argv, "to") == 0) {
+               switch (index_in_strings(options, *argv)) {
+                       case 0: /* from */
+                       {
+                               inet_prefix addr;
                                NEXT_ARG();
+                               from_ok = 1;
+                               get_prefix(&addr, *argv, req.r.rtm_family);
+                               if (req.r.rtm_family == AF_UNSPEC) {
+                                       req.r.rtm_family = addr.family;
+                               }
+                               if (addr.bytelen) {
+                                       addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
+                               }
+                               req.r.rtm_src_len = addr.bitlen;
+                               break;
                        }
-                       get_prefix(&addr, *argv, req.r.rtm_family);
-                       if (req.r.rtm_family == AF_UNSPEC)
-                               req.r.rtm_family = addr.family;
-                       if (addr.bytelen)
-                               addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
-                       req.r.rtm_dst_len = addr.bitlen;
+                       case 1: /* iif */
+                               NEXT_ARG();
+                               idev = *argv;
+                               break;
+                       case 2: /* oif */
+                       case 3: /* dev */
+                               NEXT_ARG();
+                               odev = *argv;
+                               break;
+                       case 4: /* notify */
+                               req.r.rtm_flags |= RTM_F_NOTIFY;
+                               break;
+                       case 5: /* connected */
+                               connected = 1;
+                               break;
+                       case 6: /* to */
+                               NEXT_ARG();
+                       default:
+                       {
+                               inet_prefix addr;
+                               get_prefix(&addr, *argv, req.r.rtm_family);
+                               if (req.r.rtm_family == AF_UNSPEC) {
+                                       req.r.rtm_family = addr.family;
+                               }
+                               if (addr.bytelen) {
+                                       addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
+                               }
+                               req.r.rtm_dst_len = addr.bitlen;
+                       }
+                       argc--; argv++;
                }
-               argc--; argv++;
        }
 
        if (req.r.rtm_dst_len == 0) {
-               error_msg_and_die("need at least destination address");
+               bb_error_msg_and_die("need at least destination address");
        }
 
-       if (rtnl_open(&rth, 0) < 0)
-               exit(1);
+       xrtnl_open(&rth);
 
        ll_init_map(&rth);
 
@@ -595,17 +760,11 @@ int iproute_get(int argc, char **argv)
                int idx;
 
                if (idev) {
-                       if ((idx = ll_name_to_index(idev)) == 0) {
-                               error_msg("Cannot find device \"%s\"", idev);
-                               return -1;
-                       }
+                       idx = xll_name_to_index(idev);
                        addattr32(&req.n, sizeof(req), RTA_IIF, idx);
                }
                if (odev) {
-                       if ((idx = ll_name_to_index(odev)) == 0) {
-                               error_msg("Cannot find device \"%s\"", odev);
-                               return -1;
-                       }
+                       idx = xll_name_to_index(odev);
                        addattr32(&req.n, sizeof(req), RTA_OIF, idx);
                }
        }
@@ -615,7 +774,7 @@ int iproute_get(int argc, char **argv)
        }
 
        if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
-               exit(2);
+               return 2;
        }
 
        if (connected && !from_ok) {
@@ -623,18 +782,14 @@ int iproute_get(int argc, char **argv)
                int len = req.n.nlmsg_len;
                struct rtattr * tb[RTA_MAX+1];
 
-               if (print_route(NULL, &req.n, (void*)stdout) < 0) {
-                       error_msg_and_die("An error :-)");
-               }
+               print_route(NULL, &req.n, (void*)stdout);
 
                if (req.n.nlmsg_type != RTM_NEWROUTE) {
-                       error_msg("Not a route?");
-                       return -1;
+                       bb_error_msg_and_die("not a route?");
                }
                len -= NLMSG_LENGTH(sizeof(*r));
                if (len < 0) {
-                       error_msg("Wrong len %d", len);
-                       return -1;
+                       bb_error_msg_and_die("wrong len %d", len);
                }
 
                memset(tb, 0, sizeof(tb));
@@ -644,8 +799,7 @@ int iproute_get(int argc, char **argv)
                        tb[RTA_PREFSRC]->rta_type = RTA_SRC;
                        r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
                } else if (!tb[RTA_SRC]) {
-                       error_msg("Failed to connect the route");
-                       return -1;
+                       bb_error_msg_and_die("failed to connect the route");
                }
                if (!odev && tb[RTA_OIF]) {
                        tb[RTA_OIF]->rta_type = 0;
@@ -660,65 +814,59 @@ int iproute_get(int argc, char **argv)
                req.n.nlmsg_type = RTM_GETROUTE;
 
                if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
-                       exit(2);
+                       return 2;
                }
        }
-
-       if (print_route(NULL, &req.n, (void*)stdout) < 0) {
-               error_msg_and_die("An error :-)");
-       }
-
-       exit(0);
-}
-
-void iproute_reset_filter()
-{
-       memset(&filter, 0, sizeof(filter));
-       filter.mdst.bitlen = -1;
-       filter.msrc.bitlen = -1;
+       print_route(NULL, &req.n, (void*)stdout);
+       return 0;
 }
 
+/* Return value becomes exitcode. It's okay to not return at all */
 int do_iproute(int argc, char **argv)
 {
-       if (argc < 1) {
-               return iproute_list(0, NULL);
-       }
-       
-       if (matches(*argv, "add") == 0) {
-               return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL,
-                                     argc-1, argv+1);
-       }
-       if (matches(*argv, "change") == 0 || strcmp(*argv, "chg") == 0) {
-               return iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE,
-                                     argc-1, argv+1);
-       }
-       if (matches(*argv, "replace") == 0) {
-               return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
-                                     argc-1, argv+1);
-       }
-       if (matches(*argv, "prepend") == 0) {
-               return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE,
-                                     argc-1, argv+1);
-       }
-       if (matches(*argv, "append") == 0) {
-               return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND,
-                                     argc-1, argv+1);
-       }
-       if (matches(*argv, "test") == 0) {
-               return iproute_modify(RTM_NEWROUTE, NLM_F_EXCL,
-                                     argc-1, argv+1);
-       }
-       if (matches(*argv, "delete") == 0) {
-               return iproute_modify(RTM_DELROUTE, 0,
-                                     argc-1, argv+1);
-       }
-       if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
-           || matches(*argv, "lst") == 0) {
-               return iproute_list(argc-1, argv+1);
-       }
-       if (matches(*argv, "get") == 0) {
-               return iproute_get(argc-1, argv+1);
-       }
-       error_msg_and_die("Command \"%s\" is unknown, try \"ip route help\".", *argv);
+       static const char ip_route_commands[] ALIGN1 =
+       /*0-3*/ "add\0""append\0""change\0""chg\0"
+       /*4-7*/ "delete\0""get\0""list\0""show\0"
+       /*8..*/ "prepend\0""replace\0""test\0""flush\0";
+       int command_num = 6;
+       unsigned flags = 0;
+       int cmd = RTM_NEWROUTE;
+
+       /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
+       /* It probably means that it is using "first match" rule */
+       if (*argv) {
+               command_num = index_in_substrings(ip_route_commands, *argv);
+       }
+       switch (command_num) {
+               case 0: /* add */
+                       flags = NLM_F_CREATE|NLM_F_EXCL;
+                       break;
+               case 1: /* append */
+                       flags = NLM_F_CREATE|NLM_F_APPEND;
+                       break;
+               case 2: /* change */
+               case 3: /* chg */
+                       flags = NLM_F_REPLACE;
+                       break;
+               case 4: /* delete */
+                       cmd = RTM_DELROUTE;
+                       break;
+               case 5: /* get */
+                       return iproute_get(argc-1, argv+1);
+               case 6: /* list */
+               case 7: /* show */
+                       return iproute_list_or_flush(argc-1, argv+1, 0);
+               case 8: /* prepend */
+                       flags = NLM_F_CREATE;
+               case 9: /* replace */
+                       flags = NLM_F_CREATE|NLM_F_REPLACE;
+               case 10: /* test */
+                       flags = NLM_F_EXCL;
+               case 11: /* flush */
+                       return iproute_list_or_flush(argc-1, argv+1, 1);
+               default:
+                       bb_error_msg_and_die("unknown command %s", *argv);
+       }
+
+       return iproute_modify(cmd, flags, argc-1, argv+1);
 }
-