From: Serhey Popovych <serhe.popovych@gmail.com>
To: netdev@vger.kernel.org
Cc: dsahern@gmail.com
Subject: [PATCH iproute2-next v3 6/8] iplink: Perform most of request buffer setups and checks in iplink_parse()
Date: Thu, 22 Feb 2018 15:02:04 +0200 [thread overview]
Message-ID: <1519304526-18848-7-git-send-email-serhe.popovych@gmail.com> (raw)
In-Reply-To: <1519304526-18848-1-git-send-email-serhe.popovych@gmail.com>
To benefit other users (e.g. link_veth.c) of iplink_parse() from
additional attribute checks and setups made in iplink_modify(). This
catches most of weired cobination of parameters to peer device
configuration.
Drop @link, @group and @index from iplink_parse() parameters list: they
are not needed outside.
While there change return -1 to exit(-1) for group parsing errors: we
want to stop further command processing unless -force option is given
to get error line easily.
Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
---
ip/ip_common.h | 3 +-
ip/iplink.c | 118 +++++++++++++++++++++++++----------------------------
ip/iplink_vxcan.c | 13 +-----
ip/link_veth.c | 13 +-----
4 files changed, 59 insertions(+), 88 deletions(-)
diff --git a/ip/ip_common.h b/ip/ip_common.h
index e4e628b..f762821 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -133,8 +133,7 @@ struct link_util {
struct link_util *get_link_kind(const char *kind);
int iplink_parse(int argc, char **argv, struct iplink_req *req,
- char **name, char **type, char **link, char **dev,
- int *group, int *index);
+ char **name, char **type, char **dev);
/* iplink_bridge.c */
void br_dump_bridge_id(const struct ifla_bridge_id *id, char *buf, size_t len);
diff --git a/ip/iplink.c b/ip/iplink.c
index 4e9f571..e53d890 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -578,9 +578,9 @@ static void has_dev(const char *dev, int dev_index)
}
int iplink_parse(int argc, char **argv, struct iplink_req *req,
- char **name, char **type, char **link, char **dev,
- int *group, int *index)
+ char **name, char **type, char **dev)
{
+ char *link = NULL;
int ret, len;
char abuf[32];
int qlen = -1;
@@ -591,9 +591,10 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req,
int numrxqueues = -1;
int dev_index = 0;
int link_netnsid = -1;
+ int index = 0;
+ int group = -1;
int addr_len = 0;
- *group = -1;
ret = argc;
while (argc > 0) {
@@ -616,14 +617,14 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req,
}
} else if (strcmp(*argv, "index") == 0) {
NEXT_ARG();
- if (*index)
+ if (index)
duparg("index", *argv);
- *index = atoi(*argv);
- if (*index <= 0)
+ index = atoi(*argv);
+ if (index <= 0)
invarg("Invalid \"index\" value", *argv);
} else if (matches(*argv, "link") == 0) {
NEXT_ARG();
- *link = *argv;
+ link = *argv;
} else if (matches(*argv, "address") == 0) {
NEXT_ARG();
addr_len = ll_addr_a2n(abuf, sizeof(abuf), *argv);
@@ -816,10 +817,11 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req,
*argv, len);
} else if (strcmp(*argv, "group") == 0) {
NEXT_ARG();
- if (*group != -1)
+ if (group != -1)
duparg("group", *argv);
- if (rtnl_group_a2n(group, *argv))
+ if (rtnl_group_a2n(&group, *argv))
invarg("Invalid \"group\" value\n", *argv);
+ addattr32(&req->n, sizeof(*req), IFLA_GROUP, group);
} else if (strcmp(*argv, "mode") == 0) {
int mode;
@@ -946,80 +948,47 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req,
}
}
- return ret - argc;
-}
-
-static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
-{
- char *dev = NULL;
- char *name = NULL;
- char *link = NULL;
- char *type = NULL;
- int index = 0;
- int group;
- struct link_util *lu = NULL;
- struct iplink_req req = {
- .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
- .n.nlmsg_flags = NLM_F_REQUEST | flags,
- .n.nlmsg_type = cmd,
- .i.ifi_family = preferred_family,
- };
- int ret;
-
- ret = iplink_parse(argc, argv,
- &req, &name, &type, &link, &dev, &group, &index);
- if (ret < 0)
- return ret;
-
- argc -= ret;
- argv += ret;
-
- if (!(flags & NLM_F_CREATE) && index) {
+ if (!(req->n.nlmsg_flags & NLM_F_CREATE) && index) {
fprintf(stderr,
"index can be used only when creating devices.\n");
exit(-1);
}
if (group != -1) {
- if (dev)
- addattr_l(&req.n, sizeof(req), IFLA_GROUP,
- &group, sizeof(group));
- else {
+ if (!*dev) {
if (argc) {
fprintf(stderr,
"Garbage instead of arguments \"%s ...\". Try \"ip link help\".\n",
*argv);
- return -1;
+ exit(-1);
}
- if (flags & NLM_F_CREATE) {
+ if (req->n.nlmsg_flags & NLM_F_CREATE) {
fprintf(stderr,
"group cannot be used when creating devices.\n");
- return -1;
+ exit(-1);
}
- addattr32(&req.n, sizeof(req), IFLA_GROUP, group);
- if (rtnl_talk(&rth, &req.n, NULL) < 0)
- return -2;
- return 0;
+ *type = NULL;
+ goto out;
}
}
- if (!(flags & NLM_F_CREATE)) {
- if (!dev) {
+ if (!(req->n.nlmsg_flags & NLM_F_CREATE)) {
+ if (!*dev) {
fprintf(stderr,
"Not enough information: \"dev\" argument is required.\n");
exit(-1);
}
- req.i.ifi_index = ll_name_to_index(dev);
- if (!req.i.ifi_index)
- return nodev(dev);
+ req->i.ifi_index = ll_name_to_index(*dev);
+ if (!req->i.ifi_index)
+ return nodev(*dev);
/* Not renaming to the same name */
- if (name == dev)
- name = NULL;
+ if (*name == *dev)
+ *name = NULL;
} else {
- if (name != dev) {
+ if (*name != *dev) {
fprintf(stderr,
"both \"name\" and \"dev\" cannot be used when creating devices.\n");
exit(-1);
@@ -1031,18 +1000,39 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
ifindex = ll_name_to_index(link);
if (!ifindex)
return nodev(link);
- addattr_l(&req.n, sizeof(req), IFLA_LINK, &ifindex, 4);
+ addattr32(&req->n, sizeof(*req), IFLA_LINK, ifindex);
}
- req.i.ifi_index = index;
+ req->i.ifi_index = index;
}
- if (name) {
- addattr_l(&req.n, sizeof(req),
- IFLA_IFNAME, name, strlen(name) + 1);
+ if (*name) {
+ addattr_l(&req->n, sizeof(*req),
+ IFLA_IFNAME, *name, strlen(*name) + 1);
}
+out:
+ return ret - argc;
+}
+
+static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
+{
+ char *dev = NULL;
+ char *name = NULL;
+ char *type = NULL;
+ struct iplink_req req = {
+ .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
+ .n.nlmsg_flags = NLM_F_REQUEST | flags,
+ .n.nlmsg_type = cmd,
+ .i.ifi_family = preferred_family,
+ };
+ int ret;
+
+ ret = iplink_parse(argc, argv, &req, &name, &type, &dev);
+ if (ret < 0)
+ return ret;
if (type) {
+ struct link_util *lu;
struct rtattr *linkinfo;
char *ulinep = strchr(type, '_');
int iflatype;
@@ -1056,6 +1046,10 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
iflatype = IFLA_INFO_SLAVE_DATA;
else
iflatype = IFLA_INFO_DATA;
+
+ argc -= ret;
+ argv += ret;
+
if (lu && argc) {
struct rtattr *data;
diff --git a/ip/iplink_vxcan.c b/ip/iplink_vxcan.c
index d7e94a0..7d44e48 100644
--- a/ip/iplink_vxcan.c
+++ b/ip/iplink_vxcan.c
@@ -35,10 +35,7 @@ static int vxcan_parse_opt(struct link_util *lu, int argc, char **argv,
{
char *dev = NULL;
char *name = NULL;
- char *link = NULL;
char *type = NULL;
- int index = 0;
- int group;
int err;
struct rtattr *data;
struct ifinfomsg ifm_save, *ifm, *peer_ifm;
@@ -57,21 +54,13 @@ static int vxcan_parse_opt(struct link_util *lu, int argc, char **argv,
n->nlmsg_len += sizeof(struct ifinfomsg);
err = iplink_parse(argc - 1, argv + 1, (struct iplink_req *)n,
- &name, &type, &link, &dev, &group, &index);
+ &name, &type, &dev);
if (err < 0)
return err;
if (type)
duparg("type", argv[err]);
- if (name) {
- addattr_l(n, 1024,
- IFLA_IFNAME, name, strlen(name) + 1);
- }
-
- if (group != -1)
- addattr32(n, 1024, IFLA_GROUP, group);
-
peer_ifm = RTA_DATA(data);
memcpy(peer_ifm, ifm, sizeof(*ifm));
memcpy(ifm, &ifm_save, sizeof(*ifm));
diff --git a/ip/link_veth.c b/ip/link_veth.c
index b6d37b9..a42ea4e 100644
--- a/ip/link_veth.c
+++ b/ip/link_veth.c
@@ -33,10 +33,7 @@ static int veth_parse_opt(struct link_util *lu, int argc, char **argv,
{
char *dev = NULL;
char *name = NULL;
- char *link = NULL;
char *type = NULL;
- int index = 0;
- int group;
int err;
struct rtattr *data;
struct ifinfomsg ifm_save, *ifm, *peer_ifm;
@@ -55,21 +52,13 @@ static int veth_parse_opt(struct link_util *lu, int argc, char **argv,
n->nlmsg_len += sizeof(struct ifinfomsg);
err = iplink_parse(argc - 1, argv + 1, (struct iplink_req *)n,
- &name, &type, &link, &dev, &group, &index);
+ &name, &type, &dev);
if (err < 0)
return err;
if (type)
duparg("type", argv[err]);
- if (name) {
- addattr_l(n, 1024,
- IFLA_IFNAME, name, strlen(name) + 1);
- }
-
- if (group != -1)
- addattr32(n, 1024, IFLA_GROUP, group);
-
peer_ifm = RTA_DATA(data);
memcpy(peer_ifm, ifm, sizeof(*ifm));
memcpy(ifm, &ifm_save, sizeof(*ifm));
--
1.7.10.4
next prev parent reply other threads:[~2018-02-22 13:02 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-22 13:01 [PATCH iproute2-next v3 0/8] iplink: Improve iplink_parse() Serhey Popovych
2018-02-22 13:01 ` [PATCH iproute2-next v3 1/8] utils: Introduce and use nodev() helper routine Serhey Popovych
2018-02-22 13:02 ` [PATCH iproute2-next v3 2/8] iplink: Correctly report error when network device isn't found Serhey Popovych
2018-02-23 23:41 ` David Ahern
2018-02-25 12:07 ` Serhey Popovych
2018-02-22 13:02 ` [PATCH iproute2-next v3 3/8] iplink: Use "dev" and "name" parameters interchangeable when possible Serhey Popovych
2018-02-22 13:02 ` [PATCH iproute2-next v3 4/8] iplink: Follow documented behaviour when "index" is given Serhey Popovych
2018-02-22 13:02 ` [PATCH iproute2-next v3 5/8] veth,vxcan: Save/reinitialize/restore whole @struct ifinfomsg Serhey Popovych
2018-02-26 3:28 ` David Ahern
2018-02-26 15:48 ` Serhey Popovych
2018-02-26 15:57 ` Serhey Popovych
2018-02-22 13:02 ` Serhey Popovych [this message]
2018-02-26 3:31 ` [PATCH iproute2-next v3 6/8] iplink: Perform most of request buffer setups and checks in iplink_parse() David Ahern
2018-02-26 15:51 ` Serhey Popovych
2018-02-22 13:02 ` [PATCH iproute2-next v3 7/8] iplink: Move data structures to block of their users Serhey Popovych
2018-02-22 13:02 ` [PATCH iproute2-next v3 8/8] iplink: Reduce number of arguments to iplink_parse() Serhey Popovych
2018-02-26 3:35 ` David Ahern
2018-02-26 15:44 ` Serhey Popovych
2018-02-26 16:07 ` Serhey Popovych
2018-02-26 18:06 ` Stephen Hemminger
2018-02-26 18:20 ` Serhey Popovych
2018-02-26 18:27 ` David Ahern
2018-02-26 18:38 ` Serhey Popovych
2018-02-26 20:09 ` Serhey Popovych
2018-02-26 21:35 ` Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1519304526-18848-7-git-send-email-serhe.popovych@gmail.com \
--to=serhe.popovych@gmail.com \
--cc=dsahern@gmail.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).