]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/iproute.c
rdma: Properly mark RDMAtool license
[mirror_iproute2.git] / ip / iproute.c
index 699635923764c0ac7a38b9b3c49b49694e20d15e..cc02a3e1b0aa65ba1e4f1c4e701d3897a1e14ff6 100644 (file)
@@ -83,7 +83,7 @@ static void usage(void)
                "INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ]...\n"
                "NH := [ encap ENCAPTYPE ENCAPHDR ] [ via [ FAMILY ] ADDRESS ]\n"
                "           [ dev STRING ] [ weight NUMBER ] NHFLAGS\n"
-               "FAMILY := [ inet | inet6 | ipx | dnet | mpls | bridge | link ]\n"
+               "FAMILY := [ inet | inet6 | mpls | bridge | link ]\n"
                "OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ] [ as [ to ] ADDRESS ]\n"
                "           [ rtt TIME ] [ rttvar TIME ] [ reordering NUMBER ]\n"
                "           [ window NUMBER ] [ cwnd NUMBER ] [ initcwnd NUMBER ]\n"
@@ -346,7 +346,7 @@ static void print_rtax_features(FILE *fp, unsigned int features)
 
        if (features)
                print_0xhex(PRINT_ANY,
-                           "features", "0x%x ", of);
+                           "features", "%#llx ", of);
 }
 
 static void print_rt_flags(FILE *fp, unsigned int flags)
@@ -483,10 +483,10 @@ static void print_rta_cacheinfo(FILE *fp, const struct rta_cacheinfo *ci)
        }
        if (ci->rta_id)
                print_0xhex(PRINT_ANY, "ipid",
-                           "ipid 0x%04x ", ci->rta_id);
+                           "ipid 0x%04llx ", ci->rta_id);
        if (ci->rta_ts || ci->rta_tsage) {
                print_0xhex(PRINT_ANY, "ts",
-                           "ts 0x%x", ci->rta_ts);
+                           "ts 0x%llx", ci->rta_ts);
                print_uint(PRINT_ANY, "tsage",
                           "tsage %usec ", ci->rta_tsage);
        }
@@ -708,7 +708,7 @@ static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
        }
 }
 
-int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
+int print_route(struct nlmsghdr *n, void *arg)
 {
        FILE *fp = (FILE *)arg;
        struct rtmsg *r = NLMSG_DATA(n);
@@ -885,7 +885,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                                print_uint(PRINT_JSON, "mark", NULL, mark);
                        else if (mark >= 16)
                                print_0xhex(PRINT_FP, NULL,
-                                           "mark 0x%x ", mark);
+                                           "mark 0x%llx ", mark);
                        else
                                print_uint(PRINT_FP, NULL,
                                           "mark %u ", mark);
@@ -923,15 +923,15 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                print_rt_pref(fp, rta_getattr_u8(tb[RTA_PREF]));
 
        if (tb[RTA_TTL_PROPAGATE]) {
-               bool propogate = rta_getattr_u8(tb[RTA_TTL_PROPAGATE]);
+               bool propagate = rta_getattr_u8(tb[RTA_TTL_PROPAGATE]);
 
                if (is_json_context())
                        print_bool(PRINT_JSON, "ttl-propogate", NULL,
-                                  propogate);
+                                  propagate);
                else
                        print_string(PRINT_FP, NULL,
                                     "ttl-propogate %s",
-                                    propogate ? "enabled" : "disabled");
+                                    propagate ? "enabled" : "disabled");
        }
 
        print_string(PRINT_FP, NULL, "\n", NULL);
@@ -1535,24 +1535,6 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
        return 0;
 }
 
-static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
-{
-       struct {
-               struct nlmsghdr nlh;
-               struct rtmsg rtm;
-       } req = {
-               .nlh.nlmsg_len = sizeof(req),
-               .nlh.nlmsg_type = RTM_GETROUTE,
-               .nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST,
-               .nlh.nlmsg_seq = rth->dump = ++rth->seq,
-               .rtm.rtm_family = family,
-               .rtm.rtm_flags = RTM_F_CLONED,
-       };
-       struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
-
-       return sendto(rth->fd, (void *)&req, sizeof(req), 0, (struct sockaddr *)&nladdr, sizeof(nladdr));
-}
-
 static int iproute_flush_cache(void)
 {
 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
@@ -1580,8 +1562,7 @@ static int iproute_flush_cache(void)
 
 static __u32 route_dump_magic = 0x45311224;
 
-static int save_route(const struct sockaddr_nl *who, struct nlmsghdr *n,
-                     void *arg)
+static int save_route(struct nlmsghdr *n, void *arg)
 {
        int ret;
        int len = n->nlmsg_len;
@@ -1623,7 +1604,7 @@ static int save_route_prep(void)
        return 0;
 }
 
-static int iproute_flush(int do_ipv6, rtnl_filter_t filter_fn)
+static int iproute_flush(int family, rtnl_filter_t filter_fn)
 {
        time_t start = time(0);
        char flushb[4096-512];
@@ -1631,12 +1612,12 @@ static int iproute_flush(int do_ipv6, rtnl_filter_t filter_fn)
        int ret;
 
        if (filter.cloned) {
-               if (do_ipv6 != AF_INET6) {
+               if (family != AF_INET6) {
                        iproute_flush_cache();
                        if (show_stats)
                                printf("*** IPv4 routing cache is flushed.\n");
                }
-               if (do_ipv6 == AF_INET)
+               if (family == AF_INET)
                        return 0;
        }
 
@@ -1645,7 +1626,7 @@ static int iproute_flush(int do_ipv6, rtnl_filter_t filter_fn)
        filter.flushe = sizeof(flushb);
 
        for (;;) {
-               if (rtnl_routedump_req(&rth, do_ipv6) < 0) {
+               if (rtnl_routedump_req(&rth, family, NULL) < 0) {
                        perror("Cannot send dump request");
                        return -2;
                }
@@ -1657,7 +1638,7 @@ static int iproute_flush(int do_ipv6, rtnl_filter_t filter_fn)
                if (filter.flushed == 0) {
                        if (show_stats) {
                                if (round == 0 &&
-                                   (!filter.cloned || do_ipv6 == AF_INET6))
+                                   (!filter.cloned || family == AF_INET6))
                                        printf("Nothing to flush.\n");
                                else
                                        printf("*** Flush is complete after %d round%s ***\n",
@@ -1685,9 +1666,33 @@ static int iproute_flush(int do_ipv6, rtnl_filter_t filter_fn)
        }
 }
 
+static int iproute_dump_filter(struct nlmsghdr *nlh, int reqlen)
+{
+       struct rtmsg *rtm = NLMSG_DATA(nlh);
+       int err;
+
+       rtm->rtm_protocol = filter.protocol;
+       if (filter.cloned)
+               rtm->rtm_flags |= RTM_F_CLONED;
+
+       if (filter.tb) {
+               err = addattr32(nlh, reqlen, RTA_TABLE, filter.tb);
+               if (err)
+                       return err;
+       }
+
+       if (filter.oif) {
+               err = addattr32(nlh, reqlen, RTA_OIF, filter.oif);
+               if (err)
+                       return err;
+       }
+
+       return 0;
+}
+
 static int iproute_list_flush_or_save(int argc, char **argv, int action)
 {
-       int do_ipv6 = preferred_family;
+       int dump_family = preferred_family;
        char *id = NULL;
        char *od = NULL;
        unsigned int mark = 0;
@@ -1806,13 +1811,13 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
                        NEXT_ARG();
                        family = read_family(*argv);
                        if (family == AF_UNSPEC)
-                               family = do_ipv6;
+                               family = dump_family;
                        else
                                NEXT_ARG();
                        get_prefix(&filter.rvia, *argv, family);
                } else if (strcmp(*argv, "src") == 0) {
                        NEXT_ARG();
-                       get_prefix(&filter.rprefsrc, *argv, do_ipv6);
+                       get_prefix(&filter.rprefsrc, *argv, dump_family);
                } else if (matches(*argv, "realms") == 0) {
                        __u32 realm;
 
@@ -1832,15 +1837,15 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
                        NEXT_ARG();
                        if (matches(*argv, "root") == 0) {
                                NEXT_ARG();
-                               get_prefix(&filter.rsrc, *argv, do_ipv6);
+                               get_prefix(&filter.rsrc, *argv, dump_family);
                        } else if (matches(*argv, "match") == 0) {
                                NEXT_ARG();
-                               get_prefix(&filter.msrc, *argv, do_ipv6);
+                               get_prefix(&filter.msrc, *argv, dump_family);
                        } else {
                                if (matches(*argv, "exact") == 0) {
                                        NEXT_ARG();
                                }
-                               get_prefix(&filter.msrc, *argv, do_ipv6);
+                               get_prefix(&filter.msrc, *argv, dump_family);
                                filter.rsrc = filter.msrc;
                        }
                } else {
@@ -1849,23 +1854,23 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
                        }
                        if (matches(*argv, "root") == 0) {
                                NEXT_ARG();
-                               get_prefix(&filter.rdst, *argv, do_ipv6);
+                               get_prefix(&filter.rdst, *argv, dump_family);
                        } else if (matches(*argv, "match") == 0) {
                                NEXT_ARG();
-                               get_prefix(&filter.mdst, *argv, do_ipv6);
+                               get_prefix(&filter.mdst, *argv, dump_family);
                        } else {
                                if (matches(*argv, "exact") == 0) {
                                        NEXT_ARG();
                                }
-                               get_prefix(&filter.mdst, *argv, do_ipv6);
+                               get_prefix(&filter.mdst, *argv, dump_family);
                                filter.rdst = filter.mdst;
                        }
                }
                argc--; argv++;
        }
 
-       if (do_ipv6 == AF_UNSPEC && filter.tb)
-               do_ipv6 = AF_INET;
+       if (dump_family == AF_UNSPEC && filter.tb)
+               dump_family = AF_INET;
 
        if (id || od)  {
                int idx;
@@ -1888,18 +1893,11 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
        filter.mark = mark;
 
        if (action == IPROUTE_FLUSH)
-               return iproute_flush(do_ipv6, filter_fn);
+               return iproute_flush(dump_family, filter_fn);
 
-       if (!filter.cloned) {
-               if (rtnl_routedump_req(&rth, do_ipv6) < 0) {
-                       perror("Cannot send dump request");
-                       return -2;
-               }
-       } else {
-               if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
-                       perror("Cannot send dump request");
-                       return -2;
-               }
+       if (rtnl_routedump_req(&rth, dump_family, iproute_dump_filter) < 0) {
+               perror("Cannot send dump request");
+               return -2;
        }
 
        new_json_obj(json);
@@ -1934,6 +1932,7 @@ static int iproute_get(int argc, char **argv)
        int fib_match = 0;
        int from_ok = 0;
        unsigned int mark = 0;
+       bool address_found = false;
 
        iproute_reset_filter(0);
        filter.cloned = 2;
@@ -2039,11 +2038,12 @@ static int iproute_get(int argc, char **argv)
                                addattr_l(&req.n, sizeof(req),
                                          RTA_DST, &addr.data, addr.bytelen);
                        req.r.rtm_dst_len = addr.bitlen;
+                       address_found = true;
                }
                argc--; argv++;
        }
 
-       if (req.r.rtm_dst_len == 0) {
+       if (!address_found) {
                fprintf(stderr, "need at least a destination address\n");
                return -1;
        }
@@ -2070,7 +2070,9 @@ static int iproute_get(int argc, char **argv)
        if (req.r.rtm_family == AF_UNSPEC)
                req.r.rtm_family = AF_INET;
 
-       req.r.rtm_flags |= RTM_F_LOOKUP_TABLE;
+       /* Only IPv4 supports the RTM_F_LOOKUP_TABLE flag */
+       if (req.r.rtm_family == AF_INET)
+               req.r.rtm_flags |= RTM_F_LOOKUP_TABLE;
        if (fib_match)
                req.r.rtm_flags |= RTM_F_FIB_MATCH;
 
@@ -2082,7 +2084,7 @@ static int iproute_get(int argc, char **argv)
                int len = answer->nlmsg_len;
                struct rtattr *tb[RTA_MAX+1];
 
-               if (print_route(NULL, answer, (void *)stdout) < 0) {
+               if (print_route(answer, (void *)stdout) < 0) {
                        fprintf(stderr, "An error :-)\n");
                        free(answer);
                        return -1;
@@ -2126,7 +2128,7 @@ static int iproute_get(int argc, char **argv)
                        return -2;
        }
 
-       if (print_route(NULL, answer, (void *)stdout) < 0) {
+       if (print_route(answer, (void *)stdout) < 0) {
                fprintf(stderr, "An error :-)\n");
                free(answer);
                return -1;
@@ -2144,8 +2146,7 @@ static int rtattr_cmp(const struct rtattr *rta1, const struct rtattr *rta2)
        return memcmp(RTA_DATA(rta1), RTA_DATA(rta2), RTA_PAYLOAD(rta1));
 }
 
-static int restore_handler(const struct sockaddr_nl *nl,
-                          struct rtnl_ctrl_data *ctrl,
+static int restore_handler(struct rtnl_ctrl_data *ctrl,
                           struct nlmsghdr *n, void *arg)
 {
        struct rtmsg *r = NLMSG_DATA(n);
@@ -2231,11 +2232,10 @@ static int iproute_restore(void)
        return 0;
 }
 
-static int show_handler(const struct sockaddr_nl *nl,
-                       struct rtnl_ctrl_data *ctrl,
+static int show_handler(struct rtnl_ctrl_data *ctrl,
                        struct nlmsghdr *n, void *arg)
 {
-       print_route(nl, n, stdout);
+       print_route(n, stdout);
        return 0;
 }