Skip to content

Commit

Permalink
ipv6: check for IPv4 mapped addresses when connecting IPv6 sockets
Browse files Browse the repository at this point in the history
When support for binding to 'mapped INADDR_ANY (::ffff.0.0.0.0)' was added
in 0f8d3c7 the rest of the code
wasn't told so now it's possible to bind IPv6 datagram socket to
::ffff.0.0.0.0, connect it to another IPv4 address and it will all
work except for getsockhame() which does not return the local address
as expected.

To give getsockname() something to work with check for 'mapped INADDR_ANY'
when connecting and update the in-core source addresses appropriately.

Signed-off-by: Max Matveev <makc@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Max Matveev authored and David S. Miller committed Aug 5, 2011
1 parent 20e72a4 commit c15fea2
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions net/ipv6/datagram.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
#include <linux/errqueue.h>
#include <asm/uaccess.h>

static inline int ipv6_mapped_addr_any(const struct in6_addr *a)
{
return (ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0));
}

int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
{
struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
Expand Down Expand Up @@ -102,10 +107,12 @@ int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)

ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr);

if (ipv6_addr_any(&np->saddr))
if (ipv6_addr_any(&np->saddr) ||
ipv6_mapped_addr_any(&np->saddr))
ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);

if (ipv6_addr_any(&np->rcv_saddr)) {
if (ipv6_addr_any(&np->rcv_saddr) ||
ipv6_mapped_addr_any(&np->rcv_saddr)) {
ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
&np->rcv_saddr);
if (sk->sk_prot->rehash)
Expand Down

0 comments on commit c15fea2

Please sign in to comment.