[SLOF] [PATCH] ipv6: Fix gcc9 warnings

Thomas Huth thuth at redhat.com
Mon Sep 30 17:43:47 AEST 2019


GCC 9 introduced some new compiler warnings that occur when taking the
address of a packed struct, e.g.:

 lib/libnet/icmpv6.c:173:21: warning: taking address of packed member of
 ‘struct ip6hdr’ may result in an unaligned pointer value [-Waddress-of-packed-member]
  173 |  rtr = find_router (&(ip6h->src));
      |                     ^~~~~~~~~~~~

Since these warnings are mainly about the ip6_addr_t values that are
embedded in these packed structs, and ip6_addr_t is reasonable small
(just 128 bit), let's fix it by passing around the IPv6 addresses by
value instead of pointer, which looks a little bit nicer anyway.

Signed-off-by: Alexey Kardashevskiy <aik at ozlabs.ru>
[thuth: Fixed more spots, adjusted the coding style, added patch description]
Signed-off-by: Thomas Huth <thuth at redhat.com>
---
 Alexey, can you additionally commit your "ipv6: Fix more gcc9 warnings"
 patch? ... with both patches applied, the libnet code is then finally
 free from compiler warnings for me.

 lib/libnet/icmpv6.c |  8 +++-----
 lib/libnet/ipv6.c   | 24 ++++++++++--------------
 lib/libnet/ipv6.h   |  2 +-
 lib/libnet/ndp.c    | 16 ++++++++--------
 lib/libnet/ndp.h    |  8 ++++----
 5 files changed, 26 insertions(+), 32 deletions(-)

diff --git a/lib/libnet/icmpv6.c b/lib/libnet/icmpv6.c
index d44ce84..34d7c65 100644
--- a/lib/libnet/icmpv6.c
+++ b/lib/libnet/icmpv6.c
@@ -163,16 +163,14 @@ handle_ra (struct icmp6hdr *icmp6h, uint8_t *ip6_packet)
 	struct ip6hdr *ip6h;
 	struct router_advertisement *ra;
 	struct router *rtr;
-	ip6_addr_t *rtr_ip;
 	uint8_t rtr_mac[] = {0, 0, 0, 0, 0, 0};
 
 	ip6h = (struct ip6hdr *) ip6_packet;
 	ra = (struct router_advertisement *) &icmp6h->icmp6body.ra;
-	rtr_ip = (ip6_addr_t *) &ip6h->src;
 
-	rtr = find_router (&(ip6h->src));
+	rtr = find_router(ip6h->src);
 	if (!rtr) {
-		rtr = router_create (rtr_mac, rtr_ip);
+		rtr = router_create (rtr_mac, ip6h->src);
 		router_add (rtr);
 	}
 
@@ -344,7 +342,7 @@ handle_na (int fd, uint8_t *packet)
 
 	memcpy(&(ip.addr), &(headers.ip6h->src), IPV6_ADDR_LENGTH);
 
-	n = find_neighbor (&ip);
+	n = find_neighbor(ip);
 
 	if (!n) {
 		n= (struct neighbor *)
diff --git a/lib/libnet/ipv6.c b/lib/libnet/ipv6.c
index 6c6fb54..1e97e5a 100644
--- a/lib/libnet/ipv6.c
+++ b/lib/libnet/ipv6.c
@@ -116,15 +116,12 @@ ip6_addr_t *get_ipv6_address(void)
  * @return 0 - IPv6 address is not in list
  *         1 - IPv6 address is in list
  */
-static int8_t find_ip6addr(ip6_addr_t *ip)
+static int8_t find_ip6addr(ip6_addr_t ip)
 {
 	struct ip6addr_list_entry *n = NULL;
 
-	if (ip == NULL)
-	    return 0;
-
 	for (n = first_ip6; n != NULL ; n=n->next)
-		if (ip6_cmp (&(n->addr), ip))
+		if (ip6_cmp(n->addr, ip))
 			return 1; /* IPv6 address is in  our list*/
 
 	return 0; /* not one of our IPv6 addresses*/
@@ -149,7 +146,7 @@ int8_t handle_ipv6(int fd, uint8_t * ip6_packet, uint32_t packetsize)
 	ip6 = (struct ip6hdr *) ip6_packet;
 
 	/* Only handle packets which are for us */
-	if (! find_ip6addr(&(ip6->dst)))
+	if (!find_ip6addr(ip6->dst))
 		return -1;
 
 	if (packetsize < sizeof(struct ip6hdr))
@@ -307,7 +304,7 @@ int8_t ip6addr_add(struct ip6addr_list_entry *new_address)
 		return 0;
 
 	 /* Don't add the same address twice */
-	if (find_ip6addr (&(new_address->addr)))
+	if (find_ip6addr(new_address->addr))
 		return 0;
 
 	/* If address is a unicast address, we also have to process packets
@@ -379,10 +376,9 @@ static void ipv6_init(int fd)
  * @param  ip6_addr ip_1
  * @param  ip6_addr ip_2
  */
-int8_t ip6_cmp(ip6_addr_t *ip_1, ip6_addr_t *ip_2)
+int8_t ip6_cmp(ip6_addr_t ip_1, ip6_addr_t ip_2)
 {
-	return ((int8_t) !memcmp( &(ip_1->addr[0]), &(ip_2->addr[0]),
-		IPV6_ADDR_LENGTH ));
+	return !memcmp(ip_1.addr, ip_2.addr, IPV6_ADDR_LENGTH);
 }
 
 /**
@@ -503,7 +499,7 @@ int send_ipv6(int fd, void* buffer, int len)
 	if(len + sizeof(struct ethhdr) > ETH_MTU_SIZE)
 		return -1;
 
-	if ( ip6_cmp (&ip6h->src, &null_ip6))
+	if ( ip6_cmp(ip6h->src, null_ip6))
 		memcpy (&(ip6h->src), get_ipv6_address(), IPV6_ADDR_LENGTH);
 
 	if (ip6h->nh == 17) {//UDP
@@ -518,7 +514,7 @@ int send_ipv6(int fd, void* buffer, int len)
 	}
 	else if (ip6h->nh == 0x3a) //ICMPv6
 		icmp6h->checksum = ip6_checksum (ip6h,
-						 (unsigned short *) icmp6h,
+						 buffer + sizeof(struct ip6hdr),
 						 ip6h->pl >> 1);
 
 	if (ip6_is_multicast (&ip_dst)) {
@@ -527,11 +523,11 @@ int send_ipv6(int fd, void* buffer, int len)
 	} else if (!is_ip6addr_in_my_net(&ip_dst)) {
 		/* If server is not in same subnet, user MAC of the router */
 		struct router *gw;
-		gw = ipv6_get_default_router(&ip6h->src);
+		gw = ipv6_get_default_router(ip6h->src);
 		mac_addr = gw ? gw->mac : null_mac;
 	} else {
 		/* Normal unicast, so use neighbor cache to look up MAC */
-		struct neighbor *n = find_neighbor (&ip_dst);
+		struct neighbor *n = find_neighbor(ip_dst);
 		if (n) {				/* Already cached ? */
 			if (memcmp(n->mac, null_mac, ETH_ALEN) != 0)
 				mac_addr = n->mac;		/* found it */
diff --git a/lib/libnet/ipv6.h b/lib/libnet/ipv6.h
index 7b71b50..c6b681d 100644
--- a/lib/libnet/ipv6.h
+++ b/lib/libnet/ipv6.h
@@ -161,7 +161,7 @@ struct prefix_info * ip6_create_prefix_info(void);
 void * ip6_prefix2addr (ip6_addr_t prefix);
 
 /* Compare IPv6 adresses */
-int8_t ip6_cmp( ip6_addr_t *ip_1, ip6_addr_t *ip_2 );
+int8_t ip6_cmp(ip6_addr_t ip_1, ip6_addr_t ip_2);
 
 /* Check if it is a link-local address */
 static inline int ip6_is_linklocal(ip6_addr_t *ip)
diff --git a/lib/libnet/ndp.c b/lib/libnet/ndp.c
index f1f51c7..1c420d6 100644
--- a/lib/libnet/ndp.c
+++ b/lib/libnet/ndp.c
@@ -55,7 +55,7 @@ router_add (struct router *nghb )
  * @return pointer to new router
  */
 void *
-router_create (uint8_t *mac, ip6_addr_t *ip)
+router_create(uint8_t *mac, ip6_addr_t ip)
 {
 	struct router *new_router;
 
@@ -67,18 +67,18 @@ router_create (uint8_t *mac, ip6_addr_t *ip)
 
 	/* fill neighbor struct */
 	memcpy (new_router->mac, mac, 6);
-	memcpy (&(new_router->ip.addr[0]), &(ip->addr[0]), IPV6_ADDR_LENGTH);
+	memcpy (&(new_router->ip.addr[0]), ip.addr, IPV6_ADDR_LENGTH);
 
 	return new_router;
 }
 
 struct router *
-find_router( ip6_addr_t *ip )
+find_router(ip6_addr_t ip)
 {
 	struct router *n = NULL;
 
 	for (n = first_router; n != NULL ; n=n->next)
-		if (ip6_cmp (&(n->ip), ip))
+		if (ip6_cmp(n->ip, ip))
 			return n; /* router is already in list*/
 
 	return NULL; /* router is unknown */
@@ -89,12 +89,12 @@ find_router( ip6_addr_t *ip )
  * @param  ip - IPv6 address with the prefered prefix
  * @return pointer to router, or NULL if none is available
  */
-struct router *ipv6_get_default_router(ip6_addr_t *ip)
+struct router *ipv6_get_default_router(ip6_addr_t ip)
 {
 	struct router *n = NULL;
 
 	for (n = first_router; n != NULL; n = n->next) {
-		if (n->ip.part.prefix == ip->part.prefix)
+		if (n->ip.part.prefix == ip.part.prefix)
 			return n;
 	}
 
@@ -159,12 +159,12 @@ neighbor_create (uint8_t *packet, struct packeth *headers)
  *         NULL    - Neighbor not found
  */
 struct neighbor *
-find_neighbor (ip6_addr_t *ip)
+find_neighbor(ip6_addr_t ip)
 {
 	struct neighbor *n = NULL;
 
 	for (n = first_neighbor; n != NULL ; n=n->next) {
-		if (ip6_cmp (&(n->ip), ip)) {
+		if (ip6_cmp(n->ip, ip)) {
 			return n; /* neighbor is already in cache */
 		}
 	}
diff --git a/lib/libnet/ndp.h b/lib/libnet/ndp.h
index cd18158..d0db198 100644
--- a/lib/libnet/ndp.h
+++ b/lib/libnet/ndp.h
@@ -62,11 +62,11 @@ struct neighbor {
 void ndp_init(void);
 int8_t neighbor_add (struct neighbor *);
 void * neighbor_create (uint8_t *packet, struct packeth *headers);
-struct neighbor * find_neighbor (ip6_addr_t *);
+struct neighbor *find_neighbor(ip6_addr_t ip);
 
 int8_t router_add(struct router*);
-void * router_create(uint8_t *mac, ip6_addr_t *ip);
-struct router * find_router(ip6_addr_t *);
-struct router *ipv6_get_default_router(ip6_addr_t *ip);
+void *router_create(uint8_t *mac, ip6_addr_t ip);
+struct router *find_router(ip6_addr_t ip);
+struct router *ipv6_get_default_router(ip6_addr_t ip);
 
 #endif //_NDP_H_
-- 
2.18.1



More information about the SLOF mailing list