Re: [PATCH iproute2-next 3/3] iptnl/ip6tnl: Unify local/remote endpoint and 6rd address parsing

2018-02-10 Thread David Ahern
On 2/9/18 11:06 AM, Serhey Popovych wrote:
> @@ -289,29 +293,16 @@ get_failed:
>   } else if (strcmp(*argv, "external") == 0) {
>   metadata = 1;
>   } else if (strcmp(*argv, "6rd-prefix") == 0) {
> - inet_prefix prefix;
> -
>   NEXT_ARG();
> - if (get_prefix(, *argv, AF_INET6))
> + if (get_prefix(, *argv, AF_INET6))
>   invarg("invalid 6rd_prefix\n", *argv);
> - memcpy(, prefix.data, 16);
> - ip6rdprefixlen = prefix.bitlen;
>   } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
> - inet_prefix prefix;
> -
>   NEXT_ARG();
> - if (get_prefix(, *argv, AF_INET))
> + if (get_prefix(, *argv, AF_INET))
>   invarg("invalid 6rd-relay_prefix\n", *argv);
> - memcpy(, prefix.data, 4);
> - ip6rdrelayprefixlen = prefix.bitlen;
>   } else if (strcmp(*argv, "6rd-reset") == 0) {
> - inet_prefix prefix;
> -
> - get_prefix(, "2002::", AF_INET6);
> - memcpy(, prefix.data, 16);
> - ip6rdprefixlen = 16;
> - ip6rdrelayprefix = 0;
> - ip6rdrelayprefixlen = 0;
> + get_prefix(, "2002::/16", AF_INET6);
> + ip6rdrelayprefix.flags = 0;
>   } else if (strcmp(*argv, "fwmark") == 0) {
>   NEXT_ARG();
>   if (get_u32(, *argv, 0))

I spent far too long staring at the patches to verify you are not
breaking anything. A key reason is the reset of inet_prefix.flags to 0.
That is a low-level detail that needs a properly named helper to make it
clear you are resetting the address to uninitialized.

That is needed in all of the places you have 'inet_prefix.flags = 0;' in
these.


[PATCH iproute2-next 3/3] iptnl/ip6tnl: Unify local/remote endpoint and 6rd address parsing

2018-02-09 Thread Serhey Popovych
We are going to merge link_iptnl.c and link_ip6tnl.c and this is final
step to make their diffs clear and show what needs to be changed during
merge.

Note that it is safe to omit endpoint address(es) from netlink create
request as kernel is aware of such case and will use zero for that
endpoint(s).

Signed-off-by: Serhey Popovych 
---
 ip/link_ip6tnl.c |   39 +++--
 ip/link_iptnl.c  |  103 +++---
 2 files changed, 72 insertions(+), 70 deletions(-)

diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c
index 8a45d42..5253086 100644
--- a/ip/link_ip6tnl.c
+++ b/ip/link_ip6tnl.c
@@ -93,8 +93,7 @@ static int ip6tunnel_parse_opt(struct link_util *lu, int 
argc, char **argv,
struct rtattr *linkinfo[IFLA_INFO_MAX+1];
struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1];
int len;
-   struct in6_addr laddr = IN6ADDR_ANY_INIT;
-   struct in6_addr raddr = IN6ADDR_ANY_INIT;
+   inet_prefix saddr, daddr;
__u8 hop_limit = DEFAULT_TNL_HOP_LIMIT;
__u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
__u32 flowinfo = 0;
@@ -108,7 +107,11 @@ static int ip6tunnel_parse_opt(struct link_util *lu, int 
argc, char **argv,
__u8 metadata = 0;
__u32 fwmark = 0;
 
+   saddr.flags = daddr.flags = 0;
+
if (!(n->nlmsg_flags & NLM_F_CREATE)) {
+   const struct rtattr *rta;
+
if (rtnl_talk(, , ) < 0) {
 get_failed:
fprintf(stderr,
@@ -134,13 +137,13 @@ get_failed:
parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX,
linkinfo[IFLA_INFO_DATA]);
 
-   if (iptuninfo[IFLA_IPTUN_LOCAL])
-   memcpy(, RTA_DATA(iptuninfo[IFLA_IPTUN_LOCAL]),
-  sizeof(laddr));
+   rta = iptuninfo[IFLA_IPTUN_LOCAL];
+   if (rta && get_addr_rta(, rta, AF_INET6))
+   goto get_failed;
 
-   if (iptuninfo[IFLA_IPTUN_REMOTE])
-   memcpy(, RTA_DATA(iptuninfo[IFLA_IPTUN_REMOTE]),
-  sizeof(raddr));
+   rta = iptuninfo[IFLA_IPTUN_REMOTE];
+   if (rta && get_addr_rta(, rta, AF_INET6))
+   goto get_failed;
 
if (iptuninfo[IFLA_IPTUN_TTL])
hop_limit = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]);
@@ -185,17 +188,11 @@ get_failed:
else
invarg("Cannot guess tunnel mode.", *argv);
} else if (strcmp(*argv, "remote") == 0) {
-   inet_prefix addr;
-
NEXT_ARG();
-   get_addr(, *argv, AF_INET6);
-   memcpy(, addr.data, sizeof(raddr));
+   get_addr(, *argv, AF_INET6);
} else if (strcmp(*argv, "local") == 0) {
-   inet_prefix addr;
-
NEXT_ARG();
-   get_addr(, *argv, AF_INET6);
-   memcpy(, addr.data, sizeof(laddr));
+   get_addr(, *argv, AF_INET6);
} else if (matches(*argv, "dev") == 0) {
NEXT_ARG();
link = ll_name_to_index(*argv);
@@ -322,8 +319,14 @@ get_failed:
return 0;
}
 
-   addattr_l(n, 1024, IFLA_IPTUN_LOCAL, , sizeof(laddr));
-   addattr_l(n, 1024, IFLA_IPTUN_REMOTE, , sizeof(raddr));
+   if (is_addrtype_inet()) {
+   addattr_l(n, 1024, IFLA_IPTUN_LOCAL,
+ saddr.data, saddr.bytelen);
+   }
+   if (is_addrtype_inet()) {
+   addattr_l(n, 1024, IFLA_IPTUN_REMOTE,
+ daddr.data, daddr.bytelen);
+   }
addattr8(n, 1024, IFLA_IPTUN_TTL, hop_limit);
addattr8(n, 1024, IFLA_IPTUN_ENCAP_LIMIT, encap_limit);
addattr32(n, 1024, IFLA_IPTUN_FLOWINFO, flowinfo);
diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c
index bc1074e..e1f29bf 100644
--- a/ip/link_iptnl.c
+++ b/ip/link_iptnl.c
@@ -90,16 +90,11 @@ static int iptunnel_parse_opt(struct link_util *lu, int 
argc, char **argv,
struct rtattr *linkinfo[IFLA_INFO_MAX+1];
struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1];
int len;
-   __u32 laddr = 0;
-   __u32 raddr = 0;
+   inet_prefix saddr, daddr, ip6rdprefix, ip6rdrelayprefix;
__u8 pmtudisc = 1;
__u8 tos = 0;
__u16 iflags = 0;
__u8 ttl = 0;
-   struct in6_addr ip6rdprefix = {};
-   __u16 ip6rdprefixlen = 0;
-   __u32 ip6rdrelayprefix = 0;
-   __u16 ip6rdrelayprefixlen = 0;
__u8 proto = 0;
__u32 link = 0;
__u16 encaptype = 0;
@@ -109,7 +104,13 @@ static int iptunnel_parse_opt(struct link_util *lu, int 
argc, char **argv,
__u8 metadata = 0;
__u32 fwmark = 0;
 
+   saddr.flags =