Fix a few more dangling IPv6 issues found by code inspection

This commit is contained in:
Gregory Nutt 2015-01-18 10:33:27 -06:00
parent eb3c6e8390
commit 59c0757183
6 changed files with 85 additions and 62 deletions

View file

@ -64,7 +64,7 @@ struct rtentry
{
FAR struct sockaddr_storage *rt_target; /* Address of the network */
FAR struct sockaddr_storage *rt_netmask; /* Network mask defining the sub-net */
FAR struct sockaddr_storage *rt_router; /* Gateway address associated with the hop */
FAR struct sockaddr_storage *rt_router; /* Gateway address associated with the hop */
};
/****************************************************************************

View file

@ -82,7 +82,7 @@
#define SIOCGIFHWADDR _SIOC(0x0013) /* Get hardware address */
#define SIOCSIFHWADDR _SIOC(0x0014) /* Set hardware address */
#define SIOCDIFADDR _SIOC(0x0015) /* Delete IP address */
#define SIOCDIFADDR _SIOC(0x0015) /* Delete IP address (IPv4 and IPv6) */
#define SIOCGIFCOUNT _SIOC(0x0016) /* Get number of devices */
/* Interface flags */

View file

@ -72,10 +72,15 @@
* Pre-processor Definitions
****************************************************************************/
/* This is really kind of bogus.. When asked for an IP address, this is
* family that is returned in the ifr structure. Probably could just skip
* this since the address family has nothing to do with the Ethernet address.
*/
#ifdef CONFIG_NET_IPv6
# define AF_INETX AF_INET6
# define AF_INETX AF_INET6
#else
# define AF_INETX AF_INET
# define AF_INETX AF_INET
#endif
/****************************************************************************
@ -682,7 +687,6 @@ static int netdev_ifrioctl(FAR struct socket *psock, int cmd,
dev = netdev_ifrdev(req);
if (dev)
{
req->ifr_hwaddr.sa_family = AF_INETX;
memcpy(dev->d_mac.ether_addr_octet,
req->ifr_hwaddr.sa_data, IFHWADDRLEN);
ret = OK;
@ -871,7 +875,7 @@ static int netdev_imsfioctl(FAR struct socket *psock, int cmd,
static int netdev_rtioctl(FAR struct socket *psock, int cmd,
FAR struct rtentry *rtentry)
{
int ret = -EINVAL;
int ret = -EAFNOSUPPORT;
/* Execute the command */
@ -879,9 +883,6 @@ static int netdev_rtioctl(FAR struct socket *psock, int cmd,
{
case SIOCADDRT: /* Add an entry to the routing table */
{
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
FAR struct sockaddr_in *addr;
#endif
/* The target address and the netmask are required values */
if (!retentry || !rtentry->rt_target || !rtentry->rt_netmask)
@ -889,32 +890,28 @@ static int netdev_rtioctl(FAR struct socket *psock, int cmd,
return -EINVAL;
}
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
addr = (FAR struct sockaddr_in *)rtentry->rt_target;
if (addr->sin_family == AF_INET)
#ifdef CONFIG_NET_IPv4
if (rtentry->rt_target.ss_family == AF_INET)
#ifdef CONFIG_NET_IPv6
#endif
{
ret = ioctl_addipv4route(rtentry);
}
#endif /* CONFIG_NET_IPv4 */
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6
else
#endif
{
ret = ioctl_addipv6route(rtentry);
}
#elif defined(CONFIG_NET_IPv4)
ret = ioctl_addipv4route(rtentry);
#elif defined(CONFIG_NET_IPv6)
ret = ioctl_addipv6route(rtentry);
#else
ret = -EAFNOSUPPORT;
#endif
#endif /* CONFIG_NET_IPv4 */
}
break;
case SIOCDELRT: /* Delete an entry from the routing table */
{
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
FAR struct sockaddr_in *addr;
#endif
/* The target address and the netmask are required values */
if (!retentry || !rtentry->rt_target || !rtentry->rt_netmask)
@ -922,24 +919,23 @@ static int netdev_rtioctl(FAR struct socket *psock, int cmd,
return -EINVAL;
}
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
addr = (FAR struct sockaddr_in *)rtentry->rt_target;
if (addr->sin_family == AF_INET)
#ifdef CONFIG_NET_IPv4
if (rtentry->rt_target.ss_family == AF_INET)
#ifdef CONFIG_NET_IPv6
#endif
{
ret = ioctl_delipv4route(rtentry);
}
#endif /* CONFIG_NET_IPv4 */
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6
else
#endif
{
ret = ioctl_delipv6route(rtentry);
}
#elif defined(CONFIG_NET_IPv4)
ret = ioctl_delipv4route(rtentry);
#elif defined(CONFIG_NET_IPv6)
ret = ioctl_delipv6route(rtentry);
#else
ret = -EAFNOSUPPORT;
#endif
#endif /* CONFIG_NET_IPv4 */
}
break;

View file

@ -65,7 +65,7 @@ struct route_ipv4_match_s
#ifdef CONFIG_NET_IPv6
struct route_ipv6_match_s
{
net_ipv6addr_t target; /* arget IPv6 address on an external network to match */
net_ipv6addr_t target; /* Target IPv6 address on an external network to match */
net_ipv6addr_t router; /* IPv6 address of the router on one of our networks*/
};
#endif

View file

@ -44,6 +44,7 @@
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <debug.h>
#ifdef CONFIG_NET_PKT
# include <netpacket/packet.h>
@ -146,6 +147,7 @@ int psock_bind(FAR struct socket *psock, const struct sockaddr *addr,
#ifdef CONFIG_NET_PKT
FAR const struct sockaddr_ll *lladdr = (const struct sockaddr_ll *)addr;
#endif
socklen_t minlen;
int err;
int ret = OK;
@ -159,27 +161,35 @@ int psock_bind(FAR struct socket *psock, const struct sockaddr *addr,
/* Verify that a valid address has been provided */
if (
(
#if defined(CONFIG_NET_PKT)
addr->sa_family != AF_PACKET &&
#endif
#if defined(CONFIG_NET_IPv6)
addr->sa_family != AF_INET6
#else
addr->sa_family != AF_INET
#endif
) ||
#if defined(CONFIG_NET_PKT)
(addr->sa_family == AF_PACKET && addrlen < sizeof(struct sockaddr_ll)) ||
#endif
#if defined(CONFIG_NET_IPv6)
(addr->sa_family == AF_INET6 && addrlen < sizeof(struct sockaddr_in6))
#else
(addr->sa_family == AF_INET && addrlen < sizeof(struct sockaddr_in))
#endif
)
switch (addr->sa_family)
{
#ifdef CONFIG_NET_IPv4
case AF_INET:
minlen = sizeof(struct sockaddr_in);
break;
#endif
#ifdef CONFIG_NET_IPv6
case AF_INET6:
minlen = sizeof(struct sockaddr_in6);
break;
#endif
#ifdef CONFIG_NET_PKT
case AF_PACKET:
minlen = sizeof(struct sockaddr_ll);
break;
#endif
default:
ndbg("ERROR: Unrecognized address family: %d\n", addr->sa_family);
err = EAFNOSUPPORT;
goto errout;
}
if (addrlen < minlen)
{
ndbg("ERROR: Invalid address length: %d < %d\n", addrlen, minlen);
err = EBADF;
goto errout;
}

View file

@ -433,6 +433,7 @@ ssize_t psock_sendto(FAR struct socket *psock, FAR const void *buf,
net_lock_t save;
int ret;
#endif
socklen_t minlen;
int err;
/* If to is NULL or tolen is zero, then this function is same as send (for
@ -452,16 +453,32 @@ ssize_t psock_sendto(FAR struct socket *psock, FAR const void *buf,
/* Verify that a valid address has been provided */
#ifdef CONFIG_NET_IPv6
if (to->sa_family != AF_INET6 || tolen < sizeof(struct sockaddr_in6))
#else
if (to->sa_family != AF_INET || tolen < sizeof(struct sockaddr_in))
switch (to->sa_family)
{
#ifdef CONFIG_NET_IPv4
case AF_INET:
minlen = sizeof(struct sockaddr_in);
break;
#endif
{
ndbg("ERROR: Invalid address\n");
#ifdef CONFIG_NET_IPv6
case AF_INET6:
minlen = sizeof(struct sockaddr_in6);
break;
#endif
default:
ndbg("ERROR: Unrecognized address family: %d\n", to->sa_family);
err = EAFNOSUPPORT;
goto errout;
}
if (tolen < minlen)
{
ndbg("ERROR: Invalid address length: %d < %d\n", tolen, minlen);
err = EBADF;
goto errout;
}
}
/* Verify that the psock corresponds to valid, allocated socket */