]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/iproute.c
ipnetns: parse nsid as a signed integer
[mirror_iproute2.git] / ip / iproute.c
index 30833414a3f7f4a6597c9edd32d2d1090ae1219b..b039f35b0ccdfcef5007fb2210ee09995a023851 100644 (file)
@@ -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);
@@ -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);
@@ -941,7 +941,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 }
 
 static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
-                       struct rtattr *rta, struct rtnexthop *rtnh,
+                       struct rtattr *rta, size_t len, struct rtnexthop *rtnh,
                        int *argcp, char ***argvp)
 {
        int argc = *argcp;
@@ -962,11 +962,16 @@ static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
                        if (r->rtm_family == AF_UNSPEC)
                                r->rtm_family = addr.family;
                        if (addr.family == r->rtm_family) {
-                               rta_addattr_l(rta, 4096, RTA_GATEWAY, &addr.data, addr.bytelen);
-                               rtnh->rtnh_len += sizeof(struct rtattr) + addr.bytelen;
+                               if (rta_addattr_l(rta, len, RTA_GATEWAY,
+                                                 &addr.data, addr.bytelen))
+                                       return -1;
+                               rtnh->rtnh_len += sizeof(struct rtattr)
+                                                 + addr.bytelen;
                        } else {
-                               rta_addattr_l(rta, 4096, RTA_VIA, &addr.family, addr.bytelen+2);
-                               rtnh->rtnh_len += RTA_SPACE(addr.bytelen+2);
+                               if (rta_addattr_l(rta, len, RTA_VIA,
+                                                 &addr.family, addr.bytelen + 2))
+                                       return -1;
+                               rtnh->rtnh_len += RTA_SPACE(addr.bytelen + 2);
                        }
                } else if (strcmp(*argv, "dev") == 0) {
                        NEXT_ARG();
@@ -988,13 +993,15 @@ static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
                        NEXT_ARG();
                        if (get_rt_realms_or_raw(&realm, *argv))
                                invarg("\"realm\" value is invalid\n", *argv);
-                       rta_addattr32(rta, 4096, RTA_FLOW, realm);
+                       if (rta_addattr32(rta, len, RTA_FLOW, realm))
+                               return -1;
                        rtnh->rtnh_len += sizeof(struct rtattr) + 4;
                } else if (strcmp(*argv, "encap") == 0) {
-                       int len = rta->rta_len;
+                       int old_len = rta->rta_len;
 
-                       lwt_parse_encap(rta, 4096, &argc, &argv);
-                       rtnh->rtnh_len += rta->rta_len - len;
+                       if (lwt_parse_encap(rta, len, &argc, &argv))
+                               return -1;
+                       rtnh->rtnh_len += rta->rta_len - old_len;
                } else if (strcmp(*argv, "as") == 0) {
                        inet_prefix addr;
 
@@ -1002,8 +1009,9 @@ static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
                        if (strcmp(*argv, "to") == 0)
                                NEXT_ARG();
                        get_addr(&addr, *argv, r->rtm_family);
-                       rta_addattr_l(rta, 4096, RTA_NEWDST, &addr.data,
-                                     addr.bytelen);
+                       if (rta_addattr_l(rta, len, RTA_NEWDST,
+                                         &addr.data, addr.bytelen))
+                               return -1;
                        rtnh->rtnh_len += sizeof(struct rtattr) + addr.bytelen;
                } else
                        break;
@@ -1016,7 +1024,7 @@ static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
 static int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r,
                          int argc, char **argv)
 {
-       char buf[1024];
+       char buf[4096];
        struct rtattr *rta = (void *)buf;
        struct rtnexthop *rtnh;
 
@@ -1036,7 +1044,7 @@ static int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r,
                memset(rtnh, 0, sizeof(*rtnh));
                rtnh->rtnh_len = sizeof(*rtnh);
                rta->rta_len += rtnh->rtnh_len;
-               if (parse_one_nh(n, r, rta, rtnh, &argc, &argv)) {
+               if (parse_one_nh(n, r, rta, 4096, rtnh, &argc, &argv)) {
                        fprintf(stderr, "Error: cannot parse nexthop\n");
                        exit(-1);
                }
@@ -1044,7 +1052,8 @@ static int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r,
        }
 
        if (rta->rta_len > RTA_LENGTH(0))
-               addattr_l(n, 1024, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta));
+               return addattr_l(n, 4096, RTA_MULTIPATH,
+                                RTA_DATA(rta), RTA_PAYLOAD(rta));
        return 0;
 }
 
@@ -1053,7 +1062,7 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
        struct {
                struct nlmsghdr n;
                struct rtmsg            r;
-               char                    buf[1024];
+               char                    buf[4096];
        } req = {
                .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)),
                .n.nlmsg_flags = NLM_F_REQUEST | flags,
@@ -1484,8 +1493,8 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
                addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
        }
 
-       if (nhs_ok)
-               parse_nexthops(&req.n, &req.r, argc, argv);
+       if (nhs_ok && parse_nexthops(&req.n, &req.r, argc, argv))
+               return -1;
 
        if (req.r.rtm_family == AF_UNSPEC)
                req.r.rtm_family = AF_INET;
@@ -1571,8 +1580,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;
@@ -1636,7 +1644,7 @@ static int iproute_flush(int do_ipv6, rtnl_filter_t filter_fn)
        filter.flushe = sizeof(flushb);
 
        for (;;) {
-               if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
+               if (rtnl_routedump_req(&rth, do_ipv6) < 0) {
                        perror("Cannot send dump request");
                        return -2;
                }
@@ -1882,7 +1890,7 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
                return iproute_flush(do_ipv6, filter_fn);
 
        if (!filter.cloned) {
-               if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
+               if (rtnl_routedump_req(&rth, do_ipv6) < 0) {
                        perror("Cannot send dump request");
                        return -2;
                }
@@ -2073,7 +2081,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;
@@ -2117,7 +2125,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;
@@ -2135,8 +2143,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);
@@ -2222,11 +2229,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;
 }