Skip to content
Permalink
Browse files

net/ipv6: Properly separate what belongs to ipv6 from the rest

Context part should be in context. Then, rename ipv6 function for
clarity.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
  • Loading branch information...
tbursztyka authored and jukkar committed Jun 29, 2018
1 parent 68f7e96 commit d309c870c7fca7244119c5eac8dae62a405aa2b1
@@ -580,6 +580,32 @@ struct net_pkt *net_context_create_ipv4(struct net_context *context,
}
#endif /* CONFIG_NET_IPV4 */

/**
* @brief Create IPv6 packet in provided net_pkt from context
*
* @param context Network context for a connection
* @param pkt Network packet
* @param src Source address, or NULL to choose a default from context
* @param dst Destination IPv6 address
*
* @return Return network packet that contains the IPv6 packet.
*/
#if defined(CONFIG_NET_IPV6)
struct net_pkt *net_context_create_ipv6(struct net_context *context,
struct net_pkt *pkt,
const struct in6_addr *src,
const struct in6_addr *dst);
#else
static inline
struct net_pkt *net_context_create_ipv6(struct net_context *context,
struct net_pkt *pkt,
const struct in6_addr *src,
const struct in6_addr *dst)
{
return NULL;
}
#endif /* CONFIG_NET_IPV6 */

/**
* @brief Assign a socket a local address.
*
@@ -639,7 +639,7 @@ int net_icmpv6_send_echo_request(struct net_if *iface,
pkt = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, dst),
K_FOREVER);

pkt = net_ipv6_create_raw(pkt, src, dst, iface, IPPROTO_ICMPV6);
pkt = net_ipv6_create(pkt, src, dst, iface, IPPROTO_ICMPV6);

net_pkt_set_family(pkt, AF_INET6);
net_pkt_set_iface(pkt, iface);
@@ -653,7 +653,7 @@ int net_icmpv6_send_echo_request(struct net_if *iface,
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->src, src);
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->dst, dst);

if (net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6) < 0) {
if (net_ipv6_finalize(pkt, IPPROTO_ICMPV6) < 0) {
goto drop;
}

@@ -692,11 +692,11 @@ const struct in6_addr *net_ipv6_unspecified_address(void)
return &in6addr_any;
}

struct net_pkt *net_ipv6_create_raw(struct net_pkt *pkt,
const struct in6_addr *src,
const struct in6_addr *dst,
struct net_if *iface,
u8_t next_header)
struct net_pkt *net_ipv6_create(struct net_pkt *pkt,
const struct in6_addr *src,
const struct in6_addr *dst,
struct net_if *iface,
u8_t next_header_proto)
{
struct net_buf *header;

@@ -721,7 +721,7 @@ struct net_pkt *net_ipv6_create_raw(struct net_pkt *pkt,
net_ipaddr_copy(&NET_IPV6_HDR(pkt)->src, src);

net_pkt_set_ipv6_ext_len(pkt, 0);
NET_IPV6_HDR(pkt)->nexthdr = next_header;
NET_IPV6_HDR(pkt)->nexthdr = next_header_proto;

net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr));
net_pkt_set_family(pkt, AF_INET6);
@@ -731,37 +731,14 @@ struct net_pkt *net_ipv6_create_raw(struct net_pkt *pkt,
return pkt;
}

struct net_pkt *net_ipv6_create(struct net_context *context,
struct net_pkt *pkt,
const struct in6_addr *src,
const struct in6_addr *dst)
{
NET_ASSERT(((struct sockaddr_in6_ptr *)&context->local)->sin6_addr);

if (!src) {
src = ((struct sockaddr_in6_ptr *)&context->local)->sin6_addr;
}

if (net_is_ipv6_addr_unspecified(src)
|| net_is_ipv6_addr_mcast(src)) {
src = net_if_ipv6_select_src_addr(net_pkt_iface(pkt),
(struct in6_addr *)dst);
}

return net_ipv6_create_raw(pkt,
src,
dst,
net_context_get_iface(context),
net_context_get_ip_proto(context));
}

int net_ipv6_finalize_raw(struct net_pkt *pkt, u8_t next_header)
int net_ipv6_finalize(struct net_pkt *pkt, u8_t next_header_proto)
{
/* Set the length of the IPv6 header */
size_t total_len;

#if defined(CONFIG_NET_UDP) && defined(CONFIG_NET_RPL_INSERT_HBH_OPTION)
if (next_header != IPPROTO_TCP && next_header != IPPROTO_ICMPV6) {
if (next_header_proto != IPPROTO_TCP &&
next_header_proto != IPPROTO_ICMPV6) {
/* Check if we need to add RPL header to sent UDP packet. */
if (net_rpl_insert_header(pkt) < 0) {
NET_DBG("RPL HBHO insert failed");
@@ -780,31 +757,26 @@ int net_ipv6_finalize_raw(struct net_pkt *pkt, u8_t next_header)
NET_IPV6_HDR(pkt)->len[1] = total_len & 0xff;

#if defined(CONFIG_NET_UDP)
if (next_header == IPPROTO_UDP &&
if (next_header_proto == IPPROTO_UDP &&
net_if_need_calc_tx_checksum(net_pkt_iface(pkt))) {
net_udp_set_chksum(pkt, pkt->frags);
} else
#endif

#if defined(CONFIG_NET_TCP)
if (next_header == IPPROTO_TCP &&
if (next_header_proto == IPPROTO_TCP &&
net_if_need_calc_tx_checksum(net_pkt_iface(pkt))) {
net_tcp_set_chksum(pkt, pkt->frags);
} else
#endif

if (next_header == IPPROTO_ICMPV6) {
if (next_header_proto == IPPROTO_ICMPV6) {
net_icmpv6_set_chksum(pkt, pkt->frags);
}

return 0;
}

int net_ipv6_finalize(struct net_context *context, struct net_pkt *pkt)
{
return net_ipv6_finalize_raw(pkt, net_context_get_ip_proto(context));
}

#if defined(CONFIG_NET_IPV6_DAD)
int net_ipv6_start_dad(struct net_if *iface, struct net_if_addr *ifaddr)
{
@@ -2813,11 +2785,11 @@ static int send_mldv2_raw(struct net_if *iface, struct net_buf *frags)
pkt = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, &dst),
K_FOREVER);

pkt = net_ipv6_create_raw(pkt,
net_if_ipv6_select_src_addr(iface, &dst),
&dst,
iface,
NET_IPV6_NEXTHDR_HBHO);
pkt = net_ipv6_create(pkt,
net_if_ipv6_select_src_addr(iface, &dst),
&dst,
iface,
NET_IPV6_NEXTHDR_HBHO);

NET_IPV6_HDR(pkt)->hop_limit = 1; /* RFC 3810 ch 7.4 */

@@ -2849,7 +2821,7 @@ static int send_mldv2_raw(struct net_if *iface, struct net_buf *frags)
/* Insert the actual multicast record(s) here */
net_pkt_frag_add(pkt, frags);

ret = net_ipv6_finalize_raw(pkt, NET_IPV6_NEXTHDR_HBHO);
ret = net_ipv6_finalize(pkt, NET_IPV6_NEXTHDR_HBHO);
if (ret < 0) {
goto drop;
}
@@ -3663,7 +3635,7 @@ static int send_ipv6_fragment(struct net_if *iface,
ipv6->frags = NULL;

/* Update the extension length metadata so that upper layer checksum
* will be calculated properly by net_ipv6_finalize_raw().
* will be calculated properly by net_ipv6_finalize().
*/
net_pkt_set_ipv6_ext_len(ipv6,
net_pkt_ipv6_ext_len(pkt) +
@@ -3720,7 +3692,7 @@ static int send_ipv6_fragment(struct net_if *iface,
/* Note that we must not calculate possible UDP/TCP/ICMPv6 checksum
* as that is already calculated in the non-fragmented packet.
*/
ret = net_ipv6_finalize_raw(ipv6, NET_IPV6_NEXTHDR_FRAG);
ret = net_ipv6_finalize(ipv6, NET_IPV6_NEXTHDR_FRAG);
if (ret < 0) {
NET_DBG("Cannot create IPv6 packet (%d)", ret);
goto free_pkts;
@@ -132,56 +132,28 @@ int net_ipv6_send_na(struct net_if *iface, const struct in6_addr *src,
* @param src Source IPv6 address
* @param dst Destination IPv6 address
* @param iface Network interface
* @param next_header Protocol type of the next header after IPv6 header.
* @param next_header_proto Protocol type of the next header after IPv6 header.
*
* @return Return network packet that contains the IPv6 packet.
*/
struct net_pkt *net_ipv6_create_raw(struct net_pkt *pkt,
const struct in6_addr *src,
const struct in6_addr *dst,
struct net_if *iface,
u8_t next_header);

/**
* @brief Create IPv6 packet in provided net_pkt.
*
* @param context Network context for a connection
* @param pkt Network packet
* @param src_addr Source address, or NULL to choose a default from context
* @param dst_addr Destination IPv6 address
*
* @return Return network packet that contains the IPv6 packet.
*/
struct net_pkt *net_ipv6_create(struct net_context *context,
struct net_pkt *pkt,
const struct in6_addr *src_addr,
const struct in6_addr *dst_addr);

/**
* @brief Finalize IPv6 packet. It should be called right before
* sending the packet and after all the data has been added into
* the packet. This function will set the length of the
* packet and calculate the higher protocol checksum if needed.
*
* @param pkt Network packet
* @param next_header Protocol type of the next header after IPv6 header.
*
* @return Return 0 on Success, < 0 on Failure.
*/
int net_ipv6_finalize_raw(struct net_pkt *pkt, u8_t next_header);
struct net_pkt *net_ipv6_create(struct net_pkt *pkt,
const struct in6_addr *src,
const struct in6_addr *dst,
struct net_if *iface,
u8_t next_header_proto);

/**
* @brief Finalize IPv6 packet. It should be called right before
* sending the packet and after all the data has been added into
* the packet. This function will set the length of the
* packet and calculate the higher protocol checksum if needed.
*
* @param context Network context for a connection
* @param pkt Network packet
* @param next_header_proto Protocol type of the next header after IPv6 header.
*
* @return Return 0 on Success, < 0 on Failure.
*/
int net_ipv6_finalize(struct net_context *context, struct net_pkt *pkt);
int net_ipv6_finalize(struct net_pkt *pkt, u8_t next_header_proto);

#if defined(CONFIG_NET_IPV6_MLD)
/**
@@ -649,6 +649,32 @@ struct net_pkt *net_context_create_ipv4(struct net_context *context,
}
#endif /* CONFIG_NET_IPV4 */

#if defined(CONFIG_NET_IPV6)
struct net_pkt *net_context_create_ipv6(struct net_context *context,
struct net_pkt *pkt,
const struct in6_addr *src,
const struct in6_addr *dst)
{
NET_ASSERT(((struct sockaddr_in6_ptr *)&context->local)->sin6_addr);

if (!src) {
src = ((struct sockaddr_in6_ptr *)&context->local)->sin6_addr;
}

if (net_is_ipv6_addr_unspecified(src)
|| net_is_ipv6_addr_mcast(src)) {
src = net_if_ipv6_select_src_addr(net_pkt_iface(pkt),
(struct in6_addr *)dst);
}

return net_ipv6_create(pkt,
src,
dst,
net_context_get_iface(context),
net_context_get_ip_proto(context));
}
#endif /* CONFIG_NET_IPV6 */

int net_context_connect(struct net_context *context,
const struct sockaddr *addr,
socklen_t addrlen,
@@ -858,7 +884,8 @@ static int create_udp_packet(struct net_context *context,
if (net_pkt_family(pkt) == AF_INET6) {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)dst_addr;

pkt = net_ipv6_create(context, pkt, NULL, &addr6->sin6_addr);
pkt = net_context_create_ipv6(context, pkt,
NULL, &addr6->sin6_addr);
tmp = net_udp_insert(context, pkt,
net_pkt_ip_hdr_len(pkt) +
net_pkt_ipv6_ext_len(pkt),
@@ -869,7 +896,7 @@ static int create_udp_packet(struct net_context *context,

pkt = tmp;

r = net_ipv6_finalize(context, pkt);
r = net_ipv6_finalize(pkt, net_context_get_ip_proto(context));
} else
#endif /* CONFIG_NET_IPV6 */

@@ -490,7 +490,7 @@ int net_rpl_dio_send(struct net_if *iface,
dst_addr = dst;
}

pkt = net_ipv6_create_raw(pkt, src, dst_addr, iface, IPPROTO_ICMPV6);
pkt = net_ipv6_create(pkt, src, dst_addr, iface, IPPROTO_ICMPV6);

net_pkt_set_iface(pkt, iface);

@@ -581,7 +581,7 @@ int net_rpl_dio_send(struct net_if *iface,
dag->prefix_info.length);
}

ret = net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6);
ret = net_ipv6_finalize(pkt, IPPROTO_ICMPV6);
if (ret < 0) {
net_pkt_unref(pkt);
return ret;
@@ -749,7 +749,7 @@ int net_rpl_dis_send(struct in6_addr *dst, struct net_if *iface)

src = net_if_ipv6_select_src_addr(iface, dst_addr);

pkt = net_ipv6_create_raw(pkt, src, dst_addr, iface, IPPROTO_ICMPV6);
pkt = net_ipv6_create(pkt, src, dst_addr, iface, IPPROTO_ICMPV6);

net_pkt_set_iface(pkt, iface);

@@ -762,7 +762,7 @@ int net_rpl_dis_send(struct in6_addr *dst, struct net_if *iface)
&pos, 0);
net_pkt_write_u8(pkt, pkt->frags, pos, &pos, 0);

ret = net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6);
ret = net_ipv6_finalize(pkt, IPPROTO_ICMPV6);
if (ret < 0) {
net_pkt_unref(pkt);
return ret;
@@ -3071,7 +3071,7 @@ int net_rpl_dao_send(struct net_if *iface,
return -ENOMEM;
}

pkt = net_ipv6_create_raw(pkt, src, dst, iface, IPPROTO_ICMPV6);
pkt = net_ipv6_create(pkt, src, dst, iface, IPPROTO_ICMPV6);

net_pkt_set_iface(pkt, iface);

@@ -3113,7 +3113,7 @@ int net_rpl_dao_send(struct net_if *iface,
net_pkt_append_u8(pkt, 0); /* path seq */
net_pkt_append_u8(pkt, lifetime);

ret = net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6);
ret = net_ipv6_finalize(pkt, IPPROTO_ICMPV6);
if (ret < 0) {
net_pkt_unref(pkt);
return ret;
@@ -3204,7 +3204,7 @@ static int dao_ack_send(struct in6_addr *src,
return -ENOMEM;
}

pkt = net_ipv6_create_raw(pkt, src, dst, iface, IPPROTO_ICMPV6);
pkt = net_ipv6_create(pkt, src, dst, iface, IPPROTO_ICMPV6);

net_pkt_set_iface(pkt, iface);

@@ -3215,7 +3215,7 @@ static int dao_ack_send(struct in6_addr *src,
net_pkt_append_u8(pkt, sequence);
net_pkt_append_u8(pkt, status); /* status */

ret = net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6);
ret = net_ipv6_finalize(pkt, IPPROTO_ICMPV6);
if (ret < 0) {
net_pkt_unref(pkt);
return ret;
Oops, something went wrong.

0 comments on commit d309c87

Please sign in to comment.
You can’t perform that action at this time.