From f07308b07d58b549bf86986d0d3aaef746ac023f Mon Sep 17 00:00:00 2001 From: chrysn Date: Fri, 28 Sep 2018 13:02:31 +0200 Subject: [PATCH] gnrc: Extend gnrc_ipv6_get_header checks, use in sock recv gnrc_sock_recv used to duplicate functionality of gnrc_ipv6_get_header, but additionally checked whether the IPv6 snip is large enough. All checks are now included in gnrc_ipv6_get_header, but as most of them stem from programming / user errors, they were moved into asserts; this constitutes an API change. --- sys/include/net/gnrc/ipv6.h | 2 ++ sys/net/gnrc/network_layer/ipv6/gnrc_ipv6.c | 11 +++++++---- sys/net/gnrc/sock/gnrc_sock.c | 9 ++++----- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/sys/include/net/gnrc/ipv6.h b/sys/include/net/gnrc/ipv6.h index 131a4a26190f..6e42b8abf34a 100644 --- a/sys/include/net/gnrc/ipv6.h +++ b/sys/include/net/gnrc/ipv6.h @@ -158,6 +158,8 @@ void gnrc_ipv6_demux(gnrc_netif_t *netif, gnrc_pktsnip_t *current, * * This function may be used with e.g. a pointer to a (full) UDP datagram. * + * @pre Any @ref GNRC_NETTYPE_IPV6 snip in pkt is contains a full IPv6 header. + * * @param[in] pkt The pointer to the first @ref gnrc_pktsnip_t of the * packet. * diff --git a/sys/net/gnrc/network_layer/ipv6/gnrc_ipv6.c b/sys/net/gnrc/network_layer/ipv6/gnrc_ipv6.c index fbc9ff68d983..3d24e72af8fb 100644 --- a/sys/net/gnrc/network_layer/ipv6/gnrc_ipv6.c +++ b/sys/net/gnrc/network_layer/ipv6/gnrc_ipv6.c @@ -195,13 +195,16 @@ void gnrc_ipv6_demux(gnrc_netif_t *netif, gnrc_pktsnip_t *current, ipv6_hdr_t *gnrc_ipv6_get_header(gnrc_pktsnip_t *pkt) { - ipv6_hdr_t *hdr = NULL; gnrc_pktsnip_t *tmp = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6); - if ((tmp != NULL) && (tmp->data != NULL) && ipv6_hdr_is(tmp->data)) { - hdr = ((ipv6_hdr_t*) tmp->data); + if (tmp == NULL) { + return NULL; } - return hdr; + assert(tmp->data != NULL); + assert(tmp->size >= sizeof(ipv6_hdr_t)); + assert(ipv6_hdr_is(tmp->data)); + + return ((ipv6_hdr_t*) tmp->data); } /* internal functions */ diff --git a/sys/net/gnrc/sock/gnrc_sock.c b/sys/net/gnrc/sock/gnrc_sock.c index 9cbbc5381285..6f7bd9a2f2f6 100644 --- a/sys/net/gnrc/sock/gnrc_sock.c +++ b/sys/net/gnrc/sock/gnrc_sock.c @@ -17,6 +17,7 @@ #include "net/af.h" #include "net/ipv6/hdr.h" +#include "net/gnrc/ipv6.h" #include "net/gnrc/ipv6/hdr.h" #include "net/gnrc/netreg.h" #include "net/udp.h" @@ -53,7 +54,7 @@ void gnrc_sock_create(gnrc_sock_reg_t *reg, gnrc_nettype_t type, uint32_t demux_ ssize_t gnrc_sock_recv(gnrc_sock_reg_t *reg, gnrc_pktsnip_t **pkt_out, uint32_t timeout, sock_ip_ep_t *remote) { - gnrc_pktsnip_t *pkt, *ip, *netif; + gnrc_pktsnip_t *pkt, *netif; msg_t msg; if (reg->mbox.cib.mask != (SOCK_MBOX_SIZE - 1)) { @@ -95,10 +96,8 @@ ssize_t gnrc_sock_recv(gnrc_sock_reg_t *reg, gnrc_pktsnip_t **pkt_out, } /* TODO: discern NETTYPE from remote->family (set in caller), when IPv4 * was implemented */ - ipv6_hdr_t *ipv6_hdr; - ip = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6); - assert((ip != NULL) && (ip->size >= 40)); - ipv6_hdr = ip->data; + ipv6_hdr_t *ipv6_hdr = gnrc_ipv6_get_header(pkt); + assert(ipv6_hdr != NULL); memcpy(&remote->addr, &ipv6_hdr->src, sizeof(ipv6_addr_t)); remote->family = AF_INET6; netif = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_NETIF);