diff mbox series

ipv6: Fix gcc9 warnings

Message ID 20190930074347.18603-1-thuth@redhat.com
State Accepted
Headers show
Series ipv6: Fix gcc9 warnings | expand

Commit Message

Thomas Huth Sept. 30, 2019, 7:43 a.m. UTC
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@ozlabs.ru>
[thuth: Fixed more spots, adjusted the coding style, added patch description]
Signed-off-by: Thomas Huth <thuth@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(-)

Comments

Thomas Huth Oct. 16, 2019, 11:54 a.m. UTC | #1
On 30/09/2019 09.43, Thomas Huth wrote:
> 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@ozlabs.ru>
> [thuth: Fixed more spots, adjusted the coding style, added patch description]
> Signed-off-by: Thomas Huth <thuth@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.

Ping?

 Thomas
Alexey Kardashevskiy Oct. 17, 2019, 5:54 a.m. UTC | #2
On 16/10/2019 22:54, Thomas Huth wrote:
> On 30/09/2019 09.43, Thomas Huth wrote:
>> 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@ozlabs.ru>
>> [thuth: Fixed more spots, adjusted the coding style, added patch description]
>> Signed-off-by: Thomas Huth <thuth@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.
> 
> Ping?

I was off for 1.5 weeks, I'll test and push in the next couple of days. Cheers.
Alexey Kardashevskiy Oct. 22, 2019, 1:05 a.m. UTC | #3
How do you test ipv6? I spent an hour with dnsmasq and could not get it working (ipv4 works nicely), tftp part fails.


On 17/10/2019 16:54, Alexey Kardashevskiy wrote:
> 
> 
> On 16/10/2019 22:54, Thomas Huth wrote:
>> On 30/09/2019 09.43, Thomas Huth wrote:
>>> 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@ozlabs.ru>
>>> [thuth: Fixed more spots, adjusted the coding style, added patch description]
>>> Signed-off-by: Thomas Huth <thuth@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.
>>
>> Ping?
> 
> I was off for 1.5 weeks, I'll test and push in the next couple of days. Cheers.
> 
>
Alexey Kardashevskiy Oct. 22, 2019, 3:39 a.m. UTC | #4
On 16/10/2019 22:54, Thomas Huth wrote:
> On 30/09/2019 09.43, Thomas Huth wrote:
>> 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@ozlabs.ru>
>> [thuth: Fixed more spots, adjusted the coding style, added patch description]
>> Signed-off-by: Thomas Huth <thuth@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.
> 
> Ping?
> 
>  Thomas
> 

Thanks, applied.
Thomas Huth Oct. 22, 2019, 6:33 a.m. UTC | #5
On 22/10/2019 03.05, Alexey Kardashevskiy wrote:
> How do you test ipv6? I spent an hour with dnsmasq and could not get it working (ipv4 works nicely), tftp part fails.

I normally use slirp these days -- by starting QEMU with "-nic
user,ipv4=off,tftp=...,bootfile=..." or something similar.

But when I originally worked on the IPv6 support, I had a more fancy
setup with three VMs that were connected together via "-netdev socket".

In case it's of any help, these were my quick-n-dirty notes how to set
up these three VMs:


Basic idea is to hook up three VMs together with the "-netdev socket"
parameter, so that you get a virtual network setup like this:

    +--------+     +--------+     +-------------+
    | client |<--->| router |<--->| TFTP server |
    +--------+     +--------+     +-------------+
                ^              ^
                |              |
          fc00:1::/64     fc00:2::/64

Router:

qemu-system-ppc64 -enable-kvm -nographic -vga none -device
spapr-vlan,netdev=usernet,mac=02:ca:fe:00:02:01 -netdev user,id=usernet
-device e1000,netdev=ipv6net1,mac=02:ca:fe:00:02:02 -netdev
socket,id=ipv6net1,listen=:11122 -device
e1000,netdev=ipv6net2,mac=02:ca:fe:00:02:03 -netdev
socket,id=ipv6net2,listen=:12233 -hda
/var/lib/libvirt/images/dhcpv6router.qcow2

Server:

qemu-system-ppc64 -enable-kvm -nographic -vga none -device
spapr-vlan,netdev=usernet,mac=02:ca:fe:00:03:01 -netdev user,id=usernet
-device e1000,netdev=ipv6net,mac=02:ca:fe:00:03:02 -netdev
socket,id=ipv6net,connect=:12233 -hda
/var/lib/libvirt/images/dhcpv6server.qcow2

Client:

qemu-system-ppc64 -enable-kvm -nographic -vga none -device
spapr-vlan,netdev=usernet,mac=02:ca:fe:00:01:01 -netdev user,id=usernet
-device virtio-net-pci,netdev=ipv6net,mac=02:ca:fe:00:01:02 -netdev
socket,id=ipv6net,connect=:11122 -hda
/var/lib/libvirt/images/dhcpv6client.qcow2


Client:
=======

nmcli c add type ethernet ifname enp0s0 con-name enp0s0
nmcli c mod enp0s0 ipv4.method disabled ipv6.method auto
ip -6 route add fc00:2::/64 dev enp0s0 via fc00:1::1
firewall-cmd --zone=public --add-service tftp-client

Router:
=======

 yum install radvd

nmcli c add type ethernet ifname enp0s0 con-name enp0s0 ip6 fc00:1::1
nmcli c add type ethernet ifname enp0s1 con-name enp0s1 ip6 fc00:2::1
ifup enp0s0
ifup enp0s1

ip -6 route add fc00:1::/64 dev enp0s0
ip -6 route add fc00:2::/64 dev enp0s1

or:

echo "fc00:1::/64 dev enp0s0" >>
/etc/sysconfig/network-scripts/route6-enp0s0
echo "fc00:2::/64 dev enp0s1" >>
/etc/sysconfig/network-scripts/route6-enp0s1


cat /etc/radvd.conf :

 interface enp0s0
 {
	AdvSendAdvert on;
	MinRtrAdvInterval 30;
	MaxRtrAdvInterval 100;
	prefix fc00:1::/64
	{
		AdvOnLink on;
		AdvAutonomous on;
		AdvRouterAddr off;
	};

 };

 interface enp0s1
 {
        AdvSendAdvert on;
        MinRtrAdvInterval 30;
        MaxRtrAdvInterval 100;
        prefix fc00:2::/64
        {
                AdvOnLink on;
                AdvAutonomous on;
                AdvRouterAddr off;
        };

 };

systemctl enable radvd
systemctl start radvd
systemctl stop firewalld  # <- Sorry, too lazy to set this up properly
echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf

cat /etc/dhcp/dhcpd6.conf :
 default-lease-time 2592000;
 preferred-lifetime 604800;
 dhcpv6-lease-file-name "/var/lib/dhcpd/dhcpd6.leases";
 log-facility local7;
 subnet6 fc00:1::/64 {
        interface enp0s0;
        range6 fc00:1::1000 fc00:1::2000;
        option dhcp6.bootfile-url
"tftp://[fc00:2::ca:feff:fe00:302]/boot-me";
        option dhcp6.name-servers fc00:1::1;
 }

systemctl enable dhcpd6
systemctl start dhcpd6


TFTP server:
============

nmcli c add type ethernet ifname enp0s0 con-name enp0s0
nmcli c mod enp0s0 ipv4.method disabled ipv6.method auto
yum install tftp-server
systemctl enable tftp.socket
systemctl start tftp.socket
ip -6 route add fc00:1::/64 dev enp0s0 via fc00:2::1
firewall-cmd  --zone=public --add-port=tftp


 HTH,
  Thomas
diff mbox series

Patch

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_