Send connman mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        https://lists.01.org/mailman/listinfo/connman
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of connman digest..."


Today's Topics:

   1. [PATCH] inet: Treat NULL and any address gateways as the same
      (Jussi Laakkonen)


----------------------------------------------------------------------

Message: 1
Date: Wed,  8 Aug 2018 12:00:47 +0300
From: Jussi Laakkonen <[email protected]>
To: [email protected]
Subject: [PATCH] inet: Treat NULL and any address gateways as the same
Message-ID:
        <[email protected]>

This fixes an issue of treating an any address (IPv4 or IPv6) as gateway
address when adding routes, which causes RTF_GATEWAY flag being set and
adding of such route to fail. RTF_GATEWAY should be set only when the
address is an real IP address.

In addition, when adding a IPv6 route the return value of inet_pton() is
utilized in order to check for the address validity before enabling
RTF_GATEWAY flag.

Added a helper function (__connman_inet_is_any_addr()) to check if an
IPv4 or IPv6 address is an any address. For convenience and future use
function prototype is added to src/connman.h.
---
 src/connman.h |  2 ++
 src/inet.c    | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/src/connman.h b/src/connman.h
index 706ab98..34fdcb1 100644
--- a/src/connman.h
+++ b/src/connman.h
@@ -163,6 +163,8 @@ int __connman_inet_get_interface_address(int index, int 
family, void *address);
 int __connman_inet_get_interface_ll_address(int index, int family, void 
*address);
 int __connman_inet_get_interface_mac_address(int index, uint8_t *mac_address);
 
+bool __connman_inet_is_any_addr(const char *address, int family);
+
 #include <netinet/ip6.h>
 #include <netinet/icmp6.h>
 
diff --git a/src/inet.c b/src/inet.c
index 0992ed4..5ffc132 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -190,6 +190,40 @@ done:
        return err;
 }
 
+bool __connman_inet_is_any_addr(const char *address, int family)
+{
+       bool rval = false;
+       struct addrinfo hints;
+       struct addrinfo *result = NULL;
+       struct sockaddr_in6 *in6 = NULL;
+       struct sockaddr_in *in4 = NULL;
+
+       if (!address || !*address)
+               goto out;
+
+       memset(&hints, 0, sizeof(struct addrinfo));
+
+       hints.ai_family = family;
+
+       if (getaddrinfo(address, NULL, &hints, &result))
+               goto out;
+
+       if (result) {
+               if (result->ai_family == AF_INET6) {
+                       in6 = (struct sockaddr_in6*)result->ai_addr;
+                       rval = IN6_IS_ADDR_UNSPECIFIED(&in6->sin6_addr);
+               } else if (result->ai_family == AF_INET) {
+                       in4 = (struct sockaddr_in*)result->ai_addr;
+                       rval = in4->sin_addr.s_addr == INADDR_ANY;
+               }
+
+               freeaddrinfo(result);
+       }
+
+out:
+       return rval;
+}
+
 int connman_inet_ifindex(const char *name)
 {
        struct ifreq ifr;
@@ -512,7 +546,17 @@ int connman_inet_add_network_route(int index, const char 
*host,
 
        memset(&rt, 0, sizeof(rt));
        rt.rt_flags = RTF_UP;
-       if (gateway)
+
+       /*
+        * Set RTF_GATEWAY only when gateway is set and the gateway IP address
+        * is not IPv4 any address (0.0.0.0). If the given gateway IP address is
+        * any address adding of route will fail when RTF_GATEWAY set. Passing
+        * gateway as NULL or INADDR_ANY should have the same effect. Setting
+        * the gateway address later to the struct is not affected by this,
+        * since given IPv4 any address (0.0.0.0) equals the value set with
+        * INADDR_ANY.
+        */
+       if (gateway && !__connman_inet_is_any_addr(gateway, AF_INET))
                rt.rt_flags |= RTF_GATEWAY;
        if (!netmask)
                rt.rt_flags |= RTF_HOST;
@@ -675,10 +719,17 @@ int connman_inet_add_ipv6_network_route(int index, const 
char *host,
 
        rt.rtmsg_flags = RTF_UP | RTF_HOST;
 
-       if (gateway) {
+       /*
+        * Set RTF_GATEWAY only when gateway is set, the gateway IP address is
+        * not IPv6 any address (e.g., ::) and the address is valid (conversion
+        * succeeds). If the given gateway IP address is any address then
+        * adding of route will fail when RTF_GATEWAY set. Passing gateway as
+        * NULL or IPv6 any address should have the same effect.
+        */
+
+       if (gateway && !__connman_inet_is_any_addr(gateway, AF_INET6) &&
+               inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) > 0)
                rt.rtmsg_flags |= RTF_GATEWAY;
-               inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway);
-       }
 
        rt.rtmsg_metric = 1;
        rt.rtmsg_ifindex = index;
-- 
2.7.4



------------------------------

Subject: Digest Footer

_______________________________________________
connman mailing list
[email protected]
https://lists.01.org/mailman/listinfo/connman


------------------------------

End of connman Digest, Vol 34, Issue 3
**************************************

Reply via email to