Skip to content

Commit

Permalink
[SQUASH ME] ng_icmpv6: fix receive
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Mar 25, 2015
1 parent c7d6377 commit a052041
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions sys/net/network_layer/ng_icmpv6/ng_icmpv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static inline uint16_t _icmpv6_inet_csum(ng_ipv6_hdr_t *ipv6, ng_icmpv6_hdr_t *i
csum = ng_ipv6_hdr_inet_csum(csum, NG_PROTNUM_ICMPV6, ipv6, len);
csum = net_help_csum(csum, (uint8_t *)icmpv6, len);

return (csum == 0) ? 0 : ~csum;
return (csum == 0) ? 0xffff : csum;
}

static void _dispatch_received(ng_nettype_t type, uint32_t demux_ctx,
Expand All @@ -70,15 +70,21 @@ void ng_icmpv6_demux(ng_pktsnip_t *pkt)
ng_pktsnip_t *icmpv6, *ipv6;
ng_icmpv6_hdr_t *hdr;

LL_SEARCH_SCALAR(pkt, icmpv6, type, NG_NETTYPE_UNDEF);
LL_SEARCH_SCALAR(pkt, ipv6, type, NG_NETTYPE_IPV6);
LL_SEARCH_SCALAR(pkt, icmpv6, type, NG_NETTYPE_ICMPV6);

if (icmpv6 == NULL) {
DEBUG("icmpv6: received packet has no payload\n");
return;
}

icmpv6->type = NG_NETTYPE_ICMPV6;
/* there can be extension headers between IPv6 and ICMPv6 header so we have
* to search it */
LL_SEARCH_SCALAR(icmpv6, ipv6, type, NG_NETTYPE_IPV6);

if (ipv6 == NULL) {
DEBUG("ipv6: received packet has no IPv6 header\n");
return;
}

hdr = (ng_icmpv6_hdr_t *)icmpv6->data;

Expand Down Expand Up @@ -130,7 +136,7 @@ void ng_icmpv6_echo_req_handle(ng_ipv6_hdr_t *ipv6_hdr, ng_icmpv6_echo_t *echo,
uint16_t len)
{
uint8_t *payload = (uint8_t *)echo + 1;
ng_pktsnip_t *pkt = NULL;
ng_pktsnip_t *pkt, *echo_rep;
ng_netreg_entry_t *entry = NULL;

if ((echo == NULL) || (len < sizeof(ng_icmpv6_echo_t))) {
Expand All @@ -139,20 +145,34 @@ void ng_icmpv6_echo_req_handle(ng_ipv6_hdr_t *ipv6_hdr, ng_icmpv6_echo_t *echo,
return;
}

if ((len - sizeof(ng_icmpv6_echo_t)) > 0) {
pkt = ng_pktbuf_add(NULL, payload, len - sizeof(ng_icmpv6_echo_t),
NG_NETTYPE_ICMPV6);
echo_rep = ng_icmpv6_echo_rep_build(byteorder_ntohs(echo->id),
byteorder_ntohs(echo->seq),
payload,
len - sizeof(ng_icmpv6_echo_t));

if (echo_rep == NULL) {
DEBUG("icmpv6: no space left in packet buffer\n");
return;
}

pkt = ng_icmpv6_echo_rep_build(byteorder_ntohs(echo->id),
byteorder_ntohs(echo->seq),
pkt->data, pkt->size);
pkt = ng_ipv6_hdr_build(echo_rep,
(uint8_t *)&ipv6_hdr->dest, sizeof(ng_ipv6_addr_t),
(uint8_t *)&ipv6_hdr->src, sizeof(ng_ipv6_addr_t));

pkt = ng_ipv6_hdr_build(pkt, (uint8_t *)&ipv6_hdr->src, sizeof(ng_ipv6_addr_t),
(uint8_t *)&ipv6_hdr->dest, sizeof(ng_ipv6_addr_t));
if (pkt == NULL) {
DEBUG("icmpv6: no space left in packet buffer\n");
ng_pktbuf_release(echo_rep);
return;
}

entry = ng_netreg_lookup(NG_NETTYPE_IPV6, NG_NETREG_DEMUX_CTX_ALL);

if (entry == NULL) {
DEBUG("icmpv6: no receivers\n");
ng_pktbuf_release(pkt);
return;
}

ng_pktbuf_hold(pkt, ng_netreg_num(NG_NETTYPE_IPV6, NG_NETREG_DEMUX_CTX_ALL) - 1);

/* ICMPv6 is not interested anymore so `- 1` */
Expand Down

0 comments on commit a052041

Please sign in to comment.