net: Modify IPv4/6 packet finalize API's

Current finalize api's takes buf as input parameter and returns
the finalized buf. But if there are any issue while finalizing,
it failed to throw an error.

Change-Id: I6db54b7453eec41a8051fab50d5c0dc937debd54
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
This commit is contained in:
Ravi kumar Veeramally 2017-03-10 11:27:32 +02:00 committed by Jukka Rissanen
parent 84a1c87d7f
commit 8cb97ae47d
10 changed files with 81 additions and 56 deletions

View file

@ -339,7 +339,9 @@ int net_icmpv6_send_echo_request(struct net_if *iface,
NET_ICMP_BUF(buf)->chksum = 0;
NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv6(buf);
buf = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6);
if (net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6) < 0) {
goto drop;
}
#if defined(CONFIG_NET_DEBUG_ICMPV6)
do {
@ -358,6 +360,7 @@ int net_icmpv6_send_echo_request(struct net_if *iface,
return 0;
}
drop:
net_nbuf_unref(buf);
net_stats_update_icmp_drop();

View file

@ -77,8 +77,7 @@ struct net_buf *net_ipv4_create(struct net_context *context,
net_context_get_ip_proto(context));
}
struct net_buf *net_ipv4_finalize_raw(struct net_buf *buf,
uint8_t next_header)
int net_ipv4_finalize_raw(struct net_buf *buf, uint8_t next_header)
{
/* Set the length of the IPv4 header */
size_t total_len;
@ -106,11 +105,10 @@ struct net_buf *net_ipv4_finalize_raw(struct net_buf *buf,
}
#endif
return buf;
return 0;
}
struct net_buf *net_ipv4_finalize(struct net_context *context,
struct net_buf *buf)
int net_ipv4_finalize(struct net_context *context, struct net_buf *buf)
{
return net_ipv4_finalize_raw(buf,
net_context_get_ip_proto(context));

View file

@ -63,10 +63,9 @@ struct net_buf *net_ipv4_create(struct net_context *context,
* @param buf Network buffer
* @param next_header Protocol type of the next header after IPv4 header.
*
* @return Return network buffer that contains the IPv4 packet.
* @return Return 0 on Success, < 0 on Failure.
*/
struct net_buf *net_ipv4_finalize_raw(struct net_buf *buf,
uint8_t next_header);
int net_ipv4_finalize_raw(struct net_buf *buf, uint8_t next_header);
/**
* @brief Finalize IPv4 packet. It should be called right before
@ -77,9 +76,8 @@ struct net_buf *net_ipv4_finalize_raw(struct net_buf *buf,
* @param context Network context for a connection
* @param buf Network buffer
*
* @return Return network buffer that contains the IPv4 packet.
* @return Return 0 on Success, < 0 on Failure.
*/
struct net_buf *net_ipv4_finalize(struct net_context *context,
struct net_buf *buf);
int net_ipv4_finalize(struct net_context *context, struct net_buf *buf);
#endif /* __IPV4_H */

View file

@ -404,8 +404,7 @@ struct net_buf *net_ipv6_create(struct net_context *context,
net_context_get_ip_proto(context));
}
struct net_buf *net_ipv6_finalize_raw(struct net_buf *buf,
uint8_t next_header)
int net_ipv6_finalize_raw(struct net_buf *buf, uint8_t next_header)
{
/* Set the length of the IPv6 header */
size_t total_len;
@ -415,6 +414,7 @@ struct net_buf *net_ipv6_finalize_raw(struct net_buf *buf,
/* Check if we need to add RPL header to sent UDP packet. */
if (net_rpl_insert_header(buf) < 0) {
NET_DBG("RPL HBHO insert failed");
return -EINVAL;
}
}
#endif
@ -448,14 +448,12 @@ struct net_buf *net_ipv6_finalize_raw(struct net_buf *buf,
IPPROTO_ICMPV6);
}
return buf;
return 0;
}
struct net_buf *net_ipv6_finalize(struct net_context *context,
struct net_buf *buf)
int net_ipv6_finalize(struct net_context *context, struct net_buf *buf)
{
return net_ipv6_finalize_raw(buf,
net_context_get_ip_proto(context));
return net_ipv6_finalize_raw(buf, net_context_get_ip_proto(context));
}
#if defined(CONFIG_NET_IPV6_DAD)
@ -2379,7 +2377,10 @@ static int send_mldv2_raw(struct net_if *iface, struct net_buf *frags)
/* Insert the actual multicast record(s) here */
net_buf_frag_add(buf, frags);
buf = net_ipv6_finalize_raw(buf, NET_IPV6_NEXTHDR_HBHO);
ret = net_ipv6_finalize_raw(buf, NET_IPV6_NEXTHDR_HBHO);
if (ret < 0) {
goto drop;
}
net_nbuf_set_ext_len(buf, ROUTER_ALERT_LEN);
@ -2389,17 +2390,20 @@ static int send_mldv2_raw(struct net_if *iface, struct net_buf *frags)
ret = net_send_data(buf);
if (ret < 0) {
net_nbuf_unref(buf);
net_stats_update_icmp_drop();
net_stats_update_ipv6_mld_drop();
return ret;
goto drop;
}
net_stats_update_icmp_sent();
net_stats_update_ipv6_mld_sent();
return 0;
drop:
net_nbuf_unref(buf);
net_stats_update_icmp_drop();
net_stats_update_ipv6_mld_drop();
return ret;
}
static int send_mldv2(struct net_if *iface, const struct in6_addr *addr,

View file

@ -173,10 +173,9 @@ struct net_buf *net_ipv6_create(struct net_context *context,
* @param buf Network buffer
* @param next_header Protocol type of the next header after IPv6 header.
*
* @return Return network buffer that contains the IPv6 packet.
* @return Return 0 on Success, < 0 on Failure.
*/
struct net_buf *net_ipv6_finalize_raw(struct net_buf *buf,
uint8_t next_header);
int net_ipv6_finalize_raw(struct net_buf *buf, uint8_t next_header);
/**
* @brief Finalize IPv6 packet. It should be called right before
@ -187,10 +186,9 @@ struct net_buf *net_ipv6_finalize_raw(struct net_buf *buf,
* @param context Network context for a connection
* @param buf Network buffer
*
* @return Return network buffer that contains the IPv6 packet.
* @return Return 0 on Success, < 0 on Failure.
*/
struct net_buf *net_ipv6_finalize(struct net_context *context,
struct net_buf *buf);
int net_ipv6_finalize(struct net_context *context, struct net_buf *buf);
#if defined(CONFIG_NET_IPV6_MLD)
/**

View file

@ -1589,13 +1589,15 @@ static int create_udp_packet(struct net_context *context,
const struct sockaddr *dst_addr,
struct net_buf **out_buf)
{
int r = 0;
#if defined(CONFIG_NET_IPV6)
if (net_nbuf_family(buf) == AF_INET6) {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)dst_addr;
buf = net_ipv6_create(context, buf, NULL, &addr6->sin6_addr);
buf = net_udp_append(context, buf, ntohs(addr6->sin6_port));
buf = net_ipv6_finalize(context, buf);
r = net_ipv6_finalize(context, buf);
} else
#endif /* CONFIG_NET_IPV6 */
@ -1605,7 +1607,7 @@ static int create_udp_packet(struct net_context *context,
buf = net_ipv4_create(context, buf, NULL, &addr4->sin_addr);
buf = net_udp_append(context, buf, ntohs(addr4->sin_port));
buf = net_ipv4_finalize(context, buf);
r = net_ipv4_finalize(context, buf);
} else
#endif /* CONFIG_NET_IPV4 */
{
@ -1614,7 +1616,7 @@ static int create_udp_packet(struct net_context *context,
*out_buf = buf;
return 0;
return r;
}
#endif /* CONFIG_NET_UDP */

View file

@ -552,7 +552,11 @@ int net_rpl_dio_send(struct net_if *iface,
dag->prefix_info.length);
}
buf = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6);
ret = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6);
if (ret < 0) {
net_nbuf_unref(buf);
return ret;
}
ret = net_send_data(buf);
if (ret >= 0) {
@ -748,7 +752,11 @@ int net_rpl_dis_send(struct in6_addr *dst, struct net_if *iface)
&pos, 0);
net_nbuf_write_u8(buf, buf->frags, pos, &pos, 0);
buf = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6);
ret = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6);
if (ret < 0) {
net_nbuf_unref(buf);
return ret;
}
ret = net_send_data(buf);
if (ret >= 0) {
@ -3012,7 +3020,11 @@ int net_rpl_dao_send(struct net_if *iface,
net_nbuf_append_u8(buf, 0); /* path seq */
net_nbuf_append_u8(buf, lifetime);
buf = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6);
ret = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6);
if (ret < 0) {
net_nbuf_unref(buf);
return ret;
}
ret = net_send_data(buf);
if (ret >= 0) {
@ -3106,7 +3118,11 @@ static int dao_ack_send(struct net_buf *orig,
net_nbuf_append_u8(buf, sequence);
net_nbuf_append_u8(buf, 0); /* status */
buf = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6);
ret = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6);
if (ret < 0) {
net_nbuf_unref(buf);
return ret;
}
ret = net_send_data(buf);
if (ret >= 0) {

View file

@ -235,20 +235,22 @@ static inline uint8_t net_tcp_add_options(struct net_buf *header, size_t len,
return optlen;
}
static void finalize_segment(struct net_context *context, struct net_buf *buf)
static int finalize_segment(struct net_context *context, struct net_buf *buf)
{
#if defined(CONFIG_NET_IPV4)
if (net_nbuf_family(buf) == AF_INET) {
net_ipv4_finalize(context, buf);
return net_ipv4_finalize(context, buf);
} else
#endif
#if defined(CONFIG_NET_IPV6)
if (net_nbuf_family(buf) == AF_INET6) {
net_ipv6_finalize(context, buf);
return net_ipv6_finalize(context, buf);
}
#endif
{
}
return 0;
}
static struct net_buf *prepare_segment(struct net_tcp *tcp,
@ -329,7 +331,10 @@ static struct net_buf *prepare_segment(struct net_tcp *tcp,
net_buf_frag_add(buf, tail);
}
finalize_segment(context, buf);
if (finalize_segment(context, buf) < 0) {
net_nbuf_unref(buf);
return NULL;
}
net_tcp_trace("", buf);
@ -421,6 +426,9 @@ int net_tcp_prepare_segment(struct net_tcp *tcp, uint8_t flags,
segment.optlen = optlen;
*send_buf = prepare_segment(tcp, &segment, *send_buf);
if (!*send_buf) {
return -EINVAL;
}
tcp->send_seq = seq;
@ -523,10 +531,9 @@ int net_tcp_prepare_ack(struct net_tcp *tcp, const struct sockaddr *remote,
net_tcp_set_syn_opt(tcp, options, &optionlen);
net_tcp_prepare_segment(tcp, NET_TCP_SYN | NET_TCP_ACK,
options, optionlen, NULL, remote, buf);
break;
return net_tcp_prepare_segment(tcp, NET_TCP_SYN | NET_TCP_ACK,
options, optionlen, NULL, remote,
buf);
case NET_TCP_FIN_WAIT_1:
case NET_TCP_LAST_ACK:
/* In the FIN_WAIT_1 and LAST_ACK states acknowledgment must
@ -534,17 +541,14 @@ int net_tcp_prepare_ack(struct net_tcp *tcp, const struct sockaddr *remote,
*/
tcp->send_seq--;
net_tcp_prepare_segment(tcp, NET_TCP_FIN | NET_TCP_ACK,
0, 0, NULL, remote, buf);
break;
return net_tcp_prepare_segment(tcp, NET_TCP_FIN | NET_TCP_ACK,
0, 0, NULL, remote, buf);
default:
net_tcp_prepare_segment(tcp, NET_TCP_ACK, 0, 0, NULL, remote,
buf);
break;
return net_tcp_prepare_segment(tcp, NET_TCP_ACK, 0, 0, NULL,
remote, buf);
}
return 0;
return -EINVAL;
}
int net_tcp_prepare_reset(struct net_tcp *tcp,

View file

@ -332,7 +332,7 @@ static void send_query(struct net_if *iface)
net_nbuf_append_be16(buf, 0); /* Resv, S, QRV and QQIC */
net_nbuf_append_be16(buf, 0); /* number of addresses */
buf = net_ipv6_finalize_raw(buf, NET_IPV6_NEXTHDR_HBHO);
net_ipv6_finalize_raw(buf, NET_IPV6_NEXTHDR_HBHO);
net_nbuf_set_iface(buf, iface);

View file

@ -397,7 +397,9 @@ static bool net_test_send_na(struct net_if *iface,
NET_ICMPV6_NA_BUF(buf)->flags = NET_ICMPV6_NA_FLAG_SOLICITED;
buf = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6);
if (net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6) < 0) {
return false;
}
if (net_send_data(buf) < 0) {
TC_ERROR("Cannot send NA buffer\n");