bool ng_ipv6_ext_demux(kernel_pid_t iface, ng_pktsnip_t *pkt, uint8_t nh) { ng_pktsnip_t *ext_snip; ng_ipv6_ext_t *ext; unsigned int offset = 0; ext = ((ng_ipv6_ext_t *)(((uint8_t *)pkt->data) + sizeof(ng_ipv6_hdr_t))); bool c = true; while (c) { switch (nh) { case PROTNUM_IPV6_EXT_HOPOPT: case PROTNUM_IPV6_EXT_DST: case PROTNUM_IPV6_EXT_RH: case PROTNUM_IPV6_EXT_FRAG: case PROTNUM_IPV6_EXT_AH: case PROTNUM_IPV6_EXT_ESP: case PROTNUM_IPV6_EXT_MOB: /* TODO: add handling of types */ nh = ext->nh; offset += ((ext->len * NG_IPV6_EXT_LEN_UNIT) + NG_IPV6_EXT_LEN_UNIT); ext = ng_ipv6_ext_get_next((ng_ipv6_ext_t *)ext); break; default: c = false; offset += ((ext->len * NG_IPV6_EXT_LEN_UNIT) + NG_IPV6_EXT_LEN_UNIT); ext = ng_ipv6_ext_get_next((ng_ipv6_ext_t *)ext); break; } } ext_snip = ng_pktbuf_mark(pkt, offset, NG_NETTYPE_IPV6); if (ext_snip == NULL) { return false; } ng_ipv6_demux(iface, pkt, nh); /* demultiplex next header */ return true; }
static void _receive(ng_pktsnip_t *pkt) { kernel_pid_t iface = KERNEL_PID_UNDEF; ng_pktsnip_t *ipv6, *netif; ng_ipv6_hdr_t *hdr; LL_SEARCH_SCALAR(pkt, netif, type, NG_NETTYPE_NETIF); if (netif != NULL) { iface = ((ng_netif_hdr_t *)netif->data)->if_pid; } if ((pkt->next != NULL) && (pkt->next->type == NG_NETTYPE_IPV6) && (pkt->next->size == sizeof(ng_ipv6_hdr_t))) { /* IP header was already marked. Take it. */ ipv6 = pkt->next; if (!ng_ipv6_hdr_is(ipv6->data)) { DEBUG("ipv6: Received packet was not IPv6, dropping packet\n"); ng_pktbuf_release(pkt); return; } } else { if (!ng_ipv6_hdr_is(pkt->data)) { DEBUG("ipv6: Received packet was not IPv6, dropping packet\n"); ng_pktbuf_release(pkt); return; } /* seize ipv6 as a temporary variable */ ipv6 = ng_pktbuf_start_write(pkt); if (ipv6 == NULL) { DEBUG("ipv6: unable to get write access to packet, drop it\n"); ng_pktbuf_release(pkt); return; } pkt = ipv6; /* reset pkt from temporary variable */ ipv6 = ng_pktbuf_add(pkt, pkt->data, sizeof(ng_ipv6_hdr_t), NG_NETTYPE_IPV6); if (ipv6 == NULL) { DEBUG("ipv6: error marking IPv6 header, dropping packet\n"); ng_pktbuf_release(pkt); return; } } /* extract header */ hdr = (ng_ipv6_hdr_t *)ipv6->data; DEBUG("ipv6: Received (src = %s, ", ng_ipv6_addr_to_str(addr_str, &(hdr->src), sizeof(addr_str))); DEBUG("dst = %s, next header = %" PRIu8 ", length = %" PRIu16 ")\n", ng_ipv6_addr_to_str(addr_str, &(hdr->dst), sizeof(addr_str)), hdr->nh, byteorder_ntohs(hdr->len)); if (_pkt_not_for_me(&iface, hdr)) { /* if packet is not for me */ DEBUG("ipv6: packet destination not this host\n"); #ifdef MODULE_NG_IPV6_ROUTER /* only routers redirect */ /* redirect to next hop */ DEBUG("ipv6: decrement hop limit to %" PRIu8 "\n", hdr->hl - 1); /* TODO: check if receiving interface is router */ if (--(hdr->hl) > 0) { /* drop packets that *reach* Hop Limit 0 */ ng_pktsnip_t *tmp = pkt; DEBUG("ipv6: forward packet to next hop\n"); /* pkt might not be writable yet, if header was given above */ pkt = ng_pktbuf_start_write(tmp); ipv6 = ng_pktbuf_start_write(ipv6); if ((ipv6 == NULL) || (pkt == NULL)) { DEBUG("ipv6: unable to get write access to packet: dropping it\n"); ng_pktbuf_release(tmp); return; } ng_pktbuf_release(ipv6->next); /* remove headers around IPV6 */ ipv6->next = pkt; /* reorder for sending */ pkt->next = NULL; _send(ipv6, false); } else { DEBUG("ipv6: hop limit reached 0: drop packet\n"); ng_pktbuf_release(pkt); return; } #else /* MODULE_NG_IPV6_ROUTER */ DEBUG("ipv6: dropping packet\n"); /* non rounting hosts just drop the packet */ ng_pktbuf_release(pkt); return; #endif /* MODULE_NG_IPV6_ROUTER */ } /* IPv6 internal demuxing (ICMPv6, Extension headers etc.) */ ng_ipv6_demux(iface, pkt, hdr->nh); }