ip/tunnel: Use tnl_parse_key() to parse tunnel key

It is added with
commit a7ed1520ee ("ip/tunnel: introduce tnl_parse_key()")
to avoid code duplication in ip6?tunnel.c.

Reuse it for gre/gre6 and vti/vti6 tunnel rtnl
configuration interface with the same purpose
it is used in tunnel ioctl interface in ip6?tunnel.c.

While there change type of key variables from
unsigned integer to __be32 to reflect nature of the
value they store and place error message in
tnl_parse_key() on a single line to make single
call to fprintf().

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
Serhey Popovych 2017-12-18 19:48:03 +02:00 committed by Stephen Hemminger
parent dac9ff35ea
commit 1f44b93744
5 changed files with 23 additions and 162 deletions

View File

@ -81,8 +81,8 @@ static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
struct rtattr *greinfo[IFLA_GRE_MAX + 1];
__u16 iflags = 0;
__u16 oflags = 0;
unsigned int ikey = 0;
unsigned int okey = 0;
__be32 ikey = 0;
__be32 okey = 0;
unsigned int saddr = 0;
unsigned int daddr = 0;
unsigned int link = 0;
@ -184,53 +184,18 @@ get_failed:
while (argc > 0) {
if (!matches(*argv, "key")) {
unsigned int uval;
NEXT_ARG();
iflags |= GRE_KEY;
oflags |= GRE_KEY;
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr,
"Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv);
exit(-1);
}
uval = htonl(uval);
}
ikey = okey = uval;
ikey = okey = tnl_parse_key("key", *argv);
} else if (!matches(*argv, "ikey")) {
unsigned int uval;
NEXT_ARG();
iflags |= GRE_KEY;
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv);
exit(-1);
}
uval = htonl(uval);
}
ikey = uval;
ikey = tnl_parse_key("ikey", *argv);
} else if (!matches(*argv, "okey")) {
unsigned int uval;
NEXT_ARG();
oflags |= GRE_KEY;
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv);
exit(-1);
}
uval = htonl(uval);
}
okey = uval;
okey = tnl_parse_key("okey", *argv);
} else if (!matches(*argv, "seq")) {
iflags |= GRE_SEQ;
oflags |= GRE_SEQ;

View File

@ -92,8 +92,8 @@ static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
struct rtattr *greinfo[IFLA_GRE_MAX + 1];
__u16 iflags = 0;
__u16 oflags = 0;
unsigned int ikey = 0;
unsigned int okey = 0;
__be32 ikey = 0;
__be32 okey = 0;
struct in6_addr raddr = IN6ADDR_ANY_INIT;
struct in6_addr laddr = IN6ADDR_ANY_INIT;
unsigned int link = 0;
@ -192,53 +192,18 @@ get_failed:
while (argc > 0) {
if (!matches(*argv, "key")) {
unsigned int uval;
NEXT_ARG();
iflags |= GRE_KEY;
oflags |= GRE_KEY;
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr,
"Invalid value for \"key\"\n");
exit(-1);
}
uval = htonl(uval);
}
ikey = okey = uval;
ikey = okey = tnl_parse_key("key", *argv);
} else if (!matches(*argv, "ikey")) {
unsigned int uval;
NEXT_ARG();
iflags |= GRE_KEY;
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr, "invalid value of \"ikey\"\n");
exit(-1);
}
uval = htonl(uval);
}
ikey = uval;
ikey = tnl_parse_key("ikey", *argv);
} else if (!matches(*argv, "okey")) {
unsigned int uval;
NEXT_ARG();
oflags |= GRE_KEY;
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr, "invalid value of \"okey\"\n");
exit(-1);
}
uval = htonl(uval);
}
okey = uval;
okey = tnl_parse_key("okey", *argv);
} else if (!matches(*argv, "seq")) {
iflags |= GRE_SEQ;
oflags |= GRE_SEQ;

View File

@ -64,8 +64,8 @@ static int vti_parse_opt(struct link_util *lu, int argc, char **argv,
struct rtattr *tb[IFLA_MAX + 1];
struct rtattr *linkinfo[IFLA_INFO_MAX+1];
struct rtattr *vtiinfo[IFLA_VTI_MAX + 1];
unsigned int ikey = 0;
unsigned int okey = 0;
__be32 ikey = 0;
__be32 okey = 0;
unsigned int saddr = 0;
unsigned int daddr = 0;
unsigned int link = 0;
@ -122,49 +122,14 @@ get_failed:
while (argc > 0) {
if (!matches(*argv, "key")) {
unsigned int uval;
NEXT_ARG();
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr,
"Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv);
exit(-1);
}
uval = htonl(uval);
}
ikey = okey = uval;
ikey = okey = tnl_parse_key("key", *argv);
} else if (!matches(*argv, "ikey")) {
unsigned int uval;
NEXT_ARG();
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv);
exit(-1);
}
uval = htonl(uval);
}
ikey = uval;
ikey = tnl_parse_key("ikey", *argv);
} else if (!matches(*argv, "okey")) {
unsigned int uval;
NEXT_ARG();
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv);
exit(-1);
}
uval = htonl(uval);
}
okey = uval;
okey = tnl_parse_key("okey", *argv);
} else if (!matches(*argv, "remote")) {
NEXT_ARG();
daddr = get_addr32(*argv);

View File

@ -61,8 +61,8 @@ static int vti6_parse_opt(struct link_util *lu, int argc, char **argv,
struct rtattr *vtiinfo[IFLA_VTI_MAX + 1];
struct in6_addr saddr = IN6ADDR_ANY_INIT;
struct in6_addr daddr = IN6ADDR_ANY_INIT;
unsigned int ikey = 0;
unsigned int okey = 0;
__be32 ikey = 0;
__be32 okey = 0;
unsigned int link = 0;
__u32 fwmark = 0;
int len;
@ -117,49 +117,14 @@ get_failed:
while (argc > 0) {
if (!matches(*argv, "key")) {
unsigned int uval;
NEXT_ARG();
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr,
"Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv);
exit(-1);
}
uval = htonl(uval);
}
ikey = okey = uval;
ikey = okey = tnl_parse_key("key", *argv);
} else if (!matches(*argv, "ikey")) {
unsigned int uval;
NEXT_ARG();
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv);
exit(-1);
}
uval = htonl(uval);
}
ikey = uval;
ikey = tnl_parse_key("ikey", *argv);
} else if (!matches(*argv, "okey")) {
unsigned int uval;
NEXT_ARG();
if (strchr(*argv, '.'))
uval = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0) < 0) {
fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv);
exit(-1);
}
uval = htonl(uval);
}
okey = uval;
okey = tnl_parse_key("okey", *argv);
} else if (!matches(*argv, "remote")) {
inet_prefix addr;

View File

@ -192,8 +192,9 @@ __be32 tnl_parse_key(const char *name, const char *key)
return get_addr32(key);
if (get_unsigned(&uval, key, 0) < 0) {
fprintf(stderr, "invalid value for \"%s\": \"%s\";", name, key);
fprintf(stderr, " it should be an unsigned integer\n");
fprintf(stderr,
"invalid value for \"%s\": \"%s\"; it should be an unsigned integer\n",
name, key);
exit(-1);
}
return htonl(uval);