Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_lowpan6.c Source File

lwip_lowpan6.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  *
00004  * 6LowPAN output for IPv6. Uses ND tables for link-layer addressing. Fragments packets to 6LowPAN units.
00005  *
00006  * This implementation aims to conform to IEEE 802.15.4(-2015), RFC 4944 and RFC 6282.
00007  * @todo: RFC 6775.
00008  */
00009 
00010 /*
00011  * Copyright (c) 2015 Inico Technologies Ltd.
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *
00017  * 1. Redistributions of source code must retain the above copyright notice,
00018  *    this list of conditions and the following disclaimer.
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  * 3. The name of the author may not be used to endorse or promote products
00023  *    derived from this software without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00026  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00027  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00028  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00029  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00030  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00033  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00034  * OF SUCH DAMAGE.
00035  *
00036  * This file is part of the lwIP TCP/IP stack.
00037  *
00038  * Author: Ivan Delamer <delamer@inicotech.com>
00039  *
00040  *
00041  * Please coordinate changes and requests with Ivan Delamer
00042  * <delamer@inicotech.com>
00043  */
00044 
00045 /**
00046  * @defgroup sixlowpan 6LoWPAN (RFC4944)
00047  * @ingroup netifs
00048  * 6LowPAN netif implementation
00049  */
00050 
00051 #include "netif/lowpan6.h"
00052 
00053 #if LWIP_IPV6
00054 
00055 #include "lwip/ip.h"
00056 #include "lwip/pbuf.h"
00057 #include "lwip/ip_addr.h"
00058 #include "lwip/netif.h"
00059 #include "lwip/nd6.h"
00060 #include "lwip/mem.h"
00061 #include "lwip/udp.h"
00062 #include "lwip/tcpip.h"
00063 #include "lwip/snmp.h"
00064 #include "netif/ieee802154.h"
00065 
00066 #include <string.h>
00067 
00068 #if LWIP_6LOWPAN_802154_HW_CRC
00069 #define LWIP_6LOWPAN_DO_CALC_CRC(buf, len) 0
00070 #else
00071 #define LWIP_6LOWPAN_DO_CALC_CRC(buf, len) LWIP_6LOWPAN_CALC_CRC(buf, len)
00072 #endif
00073 
00074 /** This is a helper struct for reassembly of fragments
00075  * (IEEE 802.15.4 limits to 127 bytes)
00076  */
00077 struct lowpan6_reass_helper {
00078   struct lowpan6_reass_helper *next_packet;
00079   struct pbuf *reass;
00080   struct pbuf *frags;
00081   u8_t timer;
00082   struct lowpan6_link_addr sender_addr;
00083   u16_t datagram_size;
00084   u16_t datagram_tag;
00085 };
00086 
00087 /** This struct keeps track of per-netif state */
00088 struct lowpan6_ieee802154_data {
00089   /** fragment reassembly list */
00090   struct lowpan6_reass_helper *reass_list;
00091 #if LWIP_6LOWPAN_NUM_CONTEXTS > 0
00092   /** address context for compression */
00093   ip6_addr_t lowpan6_context[LWIP_6LOWPAN_NUM_CONTEXTS];
00094 #endif
00095   /** Datagram Tag for fragmentation */
00096   u16_t tx_datagram_tag;
00097   /** local PAN ID for IEEE 802.15.4 header */
00098   u16_t ieee_802154_pan_id;
00099   /** Sequence Number for IEEE 802.15.4 transmission */
00100   u8_t tx_frame_seq_num;
00101 };
00102 
00103 /* Maximum frame size is 127 bytes minus CRC size */
00104 #define LOWPAN6_MAX_PAYLOAD (127 - 2)
00105 
00106 /** Currently, this state is global, since there's only one 6LoWPAN netif */
00107 static struct lowpan6_ieee802154_data lowpan6_data;
00108 
00109 #if LWIP_6LOWPAN_NUM_CONTEXTS > 0
00110 #define LWIP_6LOWPAN_CONTEXTS(netif) lowpan6_data.lowpan6_context
00111 #else
00112 #define LWIP_6LOWPAN_CONTEXTS(netif) NULL
00113 #endif
00114 
00115 static const struct lowpan6_link_addr ieee_802154_broadcast = {2, {0xff, 0xff}};
00116 
00117 #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
00118 static struct lowpan6_link_addr short_mac_addr = {2, {0, 0}};
00119 #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
00120 
00121 /* IEEE 802.15.4 specific functions: */
00122 
00123 /** Write the IEEE 802.15.4 header that encapsulates the 6LoWPAN frame.
00124  * Src and dst PAN IDs are filled with the ID set by @ref lowpan6_set_pan_id.
00125  *
00126  * Since the length is variable:
00127  * @returns the header length
00128  */
00129 static u8_t
00130 lowpan6_write_iee802154_header(struct ieee_802154_hdr *hdr, const struct lowpan6_link_addr *src,
00131                                const struct lowpan6_link_addr *dst)
00132 {
00133   u8_t ieee_header_len;
00134   u8_t *buffer;
00135   u8_t i;
00136   u16_t fc;
00137 
00138   fc = IEEE_802154_FC_FT_DATA; /* send data packet (2003 frame version) */
00139   fc |= IEEE_802154_FC_PANID_COMPR; /* set PAN ID compression, for now src and dst PANs are equal */
00140   if (dst != &ieee_802154_broadcast) {
00141     fc |= IEEE_802154_FC_ACK_REQ; /* data packet, no broadcast: ack required. */
00142   }
00143   if (dst->addr_len == 2) {
00144     fc |= IEEE_802154_FC_DST_ADDR_MODE_SHORT;
00145   } else {
00146     LWIP_ASSERT("invalid dst address length", dst->addr_len == 8);
00147     fc |= IEEE_802154_FC_DST_ADDR_MODE_EXT;
00148   }
00149   if (src->addr_len == 2) {
00150     fc |= IEEE_802154_FC_SRC_ADDR_MODE_SHORT;
00151   } else {
00152     LWIP_ASSERT("invalid src address length", src->addr_len == 8);
00153     fc |= IEEE_802154_FC_SRC_ADDR_MODE_EXT;
00154   }
00155   hdr->frame_control = fc;
00156   hdr->sequence_number = lowpan6_data.tx_frame_seq_num++;
00157   hdr->destination_pan_id = lowpan6_data.ieee_802154_pan_id; /* pan id */
00158 
00159   buffer = (u8_t *)hdr;
00160   ieee_header_len = 5;
00161   i = dst->addr_len;
00162   /* reverse memcpy of dst addr */
00163   while (i-- > 0) {
00164     buffer[ieee_header_len++] = dst->addr[i];
00165   }
00166   /* Source PAN ID skipped due to PAN ID Compression */
00167   i = src->addr_len;
00168   /* reverse memcpy of src addr */
00169   while (i-- > 0) {
00170     buffer[ieee_header_len++] = src->addr[i];
00171   }
00172   return ieee_header_len;
00173 }
00174 
00175 /** Parse the IEEE 802.15.4 header from a pbuf.
00176  * If successful, the header is hidden from the pbuf.
00177  *
00178  * PAN IDs and seuqence number are not checked
00179  *
00180  * @param p input pbuf, p->payload pointing at the IEEE 802.15.4 header
00181  * @param src pointer to source address filled from the header
00182  * @param dest pointer to destination address filled from the header
00183  * @returns ERR_OK if successful
00184  */
00185 static err_t
00186 lowpan6_parse_iee802154_header(struct pbuf *p, struct lowpan6_link_addr *src,
00187                                struct lowpan6_link_addr *dest)
00188 {
00189   u8_t *puc;
00190   s8_t i;
00191   u16_t frame_control, addr_mode;
00192   u16_t datagram_offset;
00193 
00194   /* Parse IEEE 802.15.4 header */
00195   puc = (u8_t *)p->payload;
00196   frame_control = puc[0] | (puc[1] << 8);
00197   datagram_offset = 2;
00198   if (frame_control & IEEE_802154_FC_SEQNO_SUPPR) {
00199     if (IEEE_802154_FC_FRAME_VERSION_GET(frame_control) <= 1) {
00200       /* sequence number suppressed, this is not valid for versions 0/1 */
00201       return ERR_VAL;
00202     }
00203   } else {
00204     datagram_offset++;
00205   }
00206   datagram_offset += 2; /* Skip destination PAN ID */
00207   addr_mode = frame_control & IEEE_802154_FC_DST_ADDR_MODE_MASK;
00208   if (addr_mode == IEEE_802154_FC_DST_ADDR_MODE_EXT) {
00209     /* extended address (64 bit) */
00210     dest->addr_len = 8;
00211     /* reverse memcpy: */
00212     for (i = 0; i < 8; i++) {
00213       dest->addr[i] = puc[datagram_offset + 7 - i];
00214     }
00215     datagram_offset += 8;
00216   } else if (addr_mode == IEEE_802154_FC_DST_ADDR_MODE_SHORT) {
00217     /* short address (16 bit) */
00218     dest->addr_len = 2;
00219     /* reverse memcpy: */
00220     dest->addr[0] = puc[datagram_offset + 1];
00221     dest->addr[1] = puc[datagram_offset];
00222     datagram_offset += 2;
00223   } else {
00224     /* unsupported address mode (do we need "no address"?) */
00225     return ERR_VAL;
00226   }
00227 
00228   if (!(frame_control & IEEE_802154_FC_PANID_COMPR)) {
00229     /* No PAN ID compression, skip source PAN ID */
00230     datagram_offset += 2;
00231   }
00232 
00233   addr_mode = frame_control & IEEE_802154_FC_SRC_ADDR_MODE_MASK;
00234   if (addr_mode == IEEE_802154_FC_SRC_ADDR_MODE_EXT) {
00235     /* extended address (64 bit) */
00236     src->addr_len = 8;
00237     /* reverse memcpy: */
00238     for (i = 0; i < 8; i++) {
00239       src->addr[i] = puc[datagram_offset + 7 - i];
00240     }
00241     datagram_offset += 8;
00242   } else if (addr_mode == IEEE_802154_FC_DST_ADDR_MODE_SHORT) {
00243     /* short address (16 bit) */
00244     src->addr_len = 2;
00245     src->addr[0] = puc[datagram_offset + 1];
00246     src->addr[1] = puc[datagram_offset];
00247     datagram_offset += 2;
00248   } else {
00249     /* unsupported address mode (do we need "no address"?) */
00250     return ERR_VAL;
00251   }
00252 
00253   /* hide IEEE802.15.4 header. */
00254   if (pbuf_remove_header(p, datagram_offset)) {
00255     return ERR_VAL;
00256   }
00257   return ERR_OK;
00258 }
00259 
00260 /** Calculate the 16-bit CRC as required by IEEE 802.15.4 */
00261 u16_t
00262 lowpan6_calc_crc(const void* buf, u16_t len)
00263 {
00264 #define CCITT_POLY_16 0x8408U
00265   u16_t i;
00266   u8_t b;
00267   u16_t crc = 0;
00268   const u8_t* p = (const u8_t*)buf;
00269 
00270   for (i = 0; i < len; i++) {
00271     u8_t data = *p;
00272     for (b = 0U; b < 8U; b++) {
00273       if (((data ^ crc) & 1) != 0) {
00274         crc = (u16_t)((crc >> 1) ^ CCITT_POLY_16);
00275       } else {
00276         crc = (u16_t)(crc >> 1);
00277       }
00278       data = (u8_t)(data >> 1);
00279     }
00280     p++;
00281   }
00282   return crc;
00283 }
00284 
00285 /* Fragmentation specific functions: */
00286 
00287 static void
00288 free_reass_datagram(struct lowpan6_reass_helper *lrh)
00289 {
00290   if (lrh->reass) {
00291     pbuf_free(lrh->reass);
00292   }
00293   if (lrh->frags) {
00294     pbuf_free(lrh->frags);
00295   }
00296   mem_free(lrh);
00297 }
00298 
00299 /**
00300  * Removes a datagram from the reassembly queue.
00301  **/
00302 static void
00303 dequeue_datagram(struct lowpan6_reass_helper *lrh, struct lowpan6_reass_helper *prev)
00304 {
00305   if (lowpan6_data.reass_list == lrh) {
00306     lowpan6_data.reass_list = lowpan6_data.reass_list->next_packet;
00307   } else {
00308     /* it wasn't the first, so it must have a valid 'prev' */
00309     LWIP_ASSERT("sanity check linked list", prev != NULL);
00310     prev->next_packet = lrh->next_packet;
00311   }
00312 }
00313 
00314 /**
00315  * Periodic timer for 6LowPAN functions:
00316  *
00317  * - Remove incomplete/old packets
00318  */
00319 void
00320 lowpan6_tmr(void)
00321 {
00322   struct lowpan6_reass_helper *lrh, *lrh_next, *lrh_prev = NULL;
00323 
00324   lrh = lowpan6_data.reass_list;
00325   while (lrh != NULL) {
00326     lrh_next = lrh->next_packet;
00327     if ((--lrh->timer) == 0) {
00328       dequeue_datagram(lrh, lrh_prev);
00329       free_reass_datagram(lrh);
00330     } else {
00331       lrh_prev = lrh;
00332     }
00333     lrh = lrh_next;
00334   }
00335 }
00336 
00337 /*
00338  * Encapsulates data into IEEE 802.15.4 frames.
00339  * Fragments an IPv6 datagram into 6LowPAN units, which fit into IEEE 802.15.4 frames.
00340  * If configured, will compress IPv6 and or UDP headers.
00341  * */
00342 static err_t
00343 lowpan6_frag(struct netif *netif, struct pbuf *p, const struct lowpan6_link_addr *src, const struct lowpan6_link_addr *dst)
00344 {
00345   struct pbuf *p_frag;
00346   u16_t frag_len, remaining_len, max_data_len;
00347   u8_t *buffer;
00348   u8_t ieee_header_len;
00349   u8_t lowpan6_header_len;
00350   u8_t hidden_header_len;
00351   u16_t crc;
00352   u16_t datagram_offset;
00353   err_t err = ERR_IF;
00354 
00355   LWIP_ASSERT("lowpan6_frag: netif->linkoutput not set", netif->linkoutput != NULL);
00356 
00357   /* We'll use a dedicated pbuf for building 6LowPAN fragments. */
00358   p_frag = pbuf_alloc(PBUF_RAW, 127, PBUF_RAM);
00359   if (p_frag == NULL) {
00360     MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
00361     return ERR_MEM;
00362   }
00363   LWIP_ASSERT("this needs a pbuf in one piece", p_frag->len == p_frag->tot_len);
00364 
00365   /* Write IEEE 802.15.4 header. */
00366   buffer = (u8_t *)p_frag->payload;
00367   ieee_header_len = lowpan6_write_iee802154_header((struct ieee_802154_hdr *)buffer, src, dst);
00368   LWIP_ASSERT("ieee_header_len < p_frag->len", ieee_header_len < p_frag->len);
00369 
00370 #if LWIP_6LOWPAN_IPHC
00371   /* Perform 6LowPAN IPv6 header compression according to RFC 6282 */
00372   /* do the header compression (this does NOT copy any non-compressed data) */
00373   err = lowpan6_compress_headers(netif, (u8_t *)p->payload, p->len,
00374     &buffer[ieee_header_len], p_frag->len - ieee_header_len, &lowpan6_header_len,
00375     &hidden_header_len, LWIP_6LOWPAN_CONTEXTS(netif), src, dst);
00376   if (err != ERR_OK) {
00377     MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
00378     pbuf_free(p_frag);
00379     return err;
00380   }
00381   pbuf_remove_header(p, hidden_header_len);
00382 
00383 #else /* LWIP_6LOWPAN_IPHC */
00384   /* Send uncompressed IPv6 header with appropriate dispatch byte. */
00385   lowpan6_header_len = 1;
00386   buffer[ieee_header_len] = 0x41; /* IPv6 dispatch */
00387 #endif /* LWIP_6LOWPAN_IPHC */
00388 
00389   /* Calculate remaining packet length */
00390   remaining_len = p->tot_len;
00391 
00392   if (remaining_len > 0x7FF) {
00393     MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
00394     /* datagram_size must fit into 11 bit */
00395     pbuf_free(p_frag);
00396     return ERR_VAL;
00397   }
00398 
00399   /* Fragment, or 1 packet? */
00400   max_data_len = LOWPAN6_MAX_PAYLOAD - ieee_header_len - lowpan6_header_len;
00401   if (remaining_len > max_data_len) {
00402     u16_t data_len;
00403     /* We must move the 6LowPAN header to make room for the FRAG header. */
00404     memmove(&buffer[ieee_header_len + 4], &buffer[ieee_header_len], lowpan6_header_len);
00405 
00406     /* Now we need to fragment the packet. FRAG1 header first */
00407     buffer[ieee_header_len] = 0xc0 | (((p->tot_len + hidden_header_len) >> 8) & 0x7);
00408     buffer[ieee_header_len + 1] = (p->tot_len + hidden_header_len) & 0xff;
00409 
00410     lowpan6_data.tx_datagram_tag++;
00411     buffer[ieee_header_len + 2] = (lowpan6_data.tx_datagram_tag >> 8) & 0xff;
00412     buffer[ieee_header_len + 3] = lowpan6_data.tx_datagram_tag & 0xff;
00413 
00414     /* Fragment follows. */
00415     data_len = (max_data_len - 4) & 0xf8;
00416     frag_len = data_len + lowpan6_header_len;
00417 
00418     pbuf_copy_partial(p, buffer + ieee_header_len + lowpan6_header_len + 4, frag_len - lowpan6_header_len, 0);
00419     remaining_len -= frag_len - lowpan6_header_len;
00420     /* datagram offset holds the offset before compression */
00421     datagram_offset = frag_len - lowpan6_header_len + hidden_header_len;
00422     LWIP_ASSERT("datagram offset must be a multiple of 8", (datagram_offset & 7) == 0);
00423 
00424     /* Calculate frame length */
00425     p_frag->len = p_frag->tot_len = ieee_header_len + 4 + frag_len + 2; /* add 2 bytes for crc*/
00426 
00427     /* 2 bytes CRC */
00428     crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
00429     pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
00430 
00431     /* send the packet */
00432     MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
00433     LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
00434     err = netif->linkoutput(netif, p_frag);
00435 
00436     while ((remaining_len > 0) && (err == ERR_OK)) {
00437       struct ieee_802154_hdr *hdr = (struct ieee_802154_hdr *)buffer;
00438       /* new frame, new seq num for ACK */
00439       hdr->sequence_number = lowpan6_data.tx_frame_seq_num++;
00440 
00441       buffer[ieee_header_len] |= 0x20; /* Change FRAG1 to FRAGN */
00442 
00443       LWIP_ASSERT("datagram offset must be a multiple of 8", (datagram_offset & 7) == 0);
00444       buffer[ieee_header_len + 4] = (u8_t)(datagram_offset >> 3); /* datagram offset in FRAGN header (datagram_offset is max. 11 bit) */
00445 
00446       frag_len = (127 - ieee_header_len - 5 - 2) & 0xf8;
00447       if (frag_len > remaining_len) {
00448         frag_len = remaining_len;
00449       }
00450 
00451       pbuf_copy_partial(p, buffer + ieee_header_len + 5, frag_len, p->tot_len - remaining_len);
00452       remaining_len -= frag_len;
00453       datagram_offset += frag_len;
00454 
00455       /* Calculate frame length */
00456       p_frag->len = p_frag->tot_len = frag_len + 5 + ieee_header_len + 2;
00457 
00458       /* 2 bytes CRC */
00459       crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
00460       pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
00461 
00462       /* send the packet */
00463       MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
00464       LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
00465       err = netif->linkoutput(netif, p_frag);
00466     }
00467   } else {
00468     /* It fits in one frame. */
00469     frag_len = remaining_len;
00470 
00471     /* Copy IPv6 packet */
00472     pbuf_copy_partial(p, buffer + ieee_header_len + lowpan6_header_len, frag_len, 0);
00473     remaining_len = 0;
00474 
00475     /* Calculate frame length */
00476     p_frag->len = p_frag->tot_len = frag_len + lowpan6_header_len + ieee_header_len + 2;
00477     LWIP_ASSERT("", p_frag->len <= 127);
00478 
00479     /* 2 bytes CRC */
00480     crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
00481     pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
00482 
00483     /* send the packet */
00484     MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
00485     LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
00486     err = netif->linkoutput(netif, p_frag);
00487   }
00488 
00489   pbuf_free(p_frag);
00490 
00491   return err;
00492 }
00493 
00494 /**
00495  * @ingroup sixlowpan
00496  * Set context
00497  */
00498 err_t
00499 lowpan6_set_context(u8_t idx, const ip6_addr_t *context)
00500 {
00501 #if LWIP_6LOWPAN_NUM_CONTEXTS > 0
00502   if (idx >= LWIP_6LOWPAN_NUM_CONTEXTS) {
00503     return ERR_ARG;
00504   }
00505 
00506   IP6_ADDR_ZONECHECK(context);
00507 
00508   ip6_addr_set(&lowpan6_data.lowpan6_context[idx], context);
00509 
00510   return ERR_OK;
00511 #else
00512   LWIP_UNUSED_ARG(idx);
00513   LWIP_UNUSED_ARG(context);
00514   return ERR_ARG;
00515 #endif
00516 }
00517 
00518 #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
00519 /**
00520  * @ingroup sixlowpan
00521  * Set short address
00522  */
00523 err_t
00524 lowpan6_set_short_addr(u8_t addr_high, u8_t addr_low)
00525 {
00526   short_mac_addr.addr[0] = addr_high;
00527   short_mac_addr.addr[1] = addr_low;
00528 
00529   return ERR_OK;
00530 }
00531 #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
00532 
00533 /* Create IEEE 802.15.4 address from netif address */
00534 static err_t
00535 lowpan6_hwaddr_to_addr(struct netif *netif, struct lowpan6_link_addr *addr)
00536 {
00537   addr->addr_len = 8;
00538   if (netif->hwaddr_len == 8) {
00539     LWIP_ERROR("NETIF_MAX_HWADDR_LEN >= 8 required", sizeof(netif->hwaddr) >= 8, return ERR_VAL;);
00540     SMEMCPY(addr->addr, netif->hwaddr, 8);
00541   } else if (netif->hwaddr_len == 6) {
00542     /* Copy from MAC-48 */
00543     SMEMCPY(addr->addr, netif->hwaddr, 3);
00544     addr->addr[3] = addr->addr[4] = 0xff;
00545     SMEMCPY(&addr->addr[5], &netif->hwaddr[3], 3);
00546   } else {
00547     /* Invalid address length, don't know how to convert this */
00548     return ERR_VAL;
00549   }
00550   return ERR_OK;
00551 }
00552 
00553 /**
00554  * @ingroup sixlowpan
00555  * Resolve and fill-in IEEE 802.15.4 address header for outgoing IPv6 packet.
00556  *
00557  * Perform Header Compression and fragment if necessary.
00558  *
00559  * @param netif The lwIP network interface which the IP packet will be sent on.
00560  * @param q The pbuf(s) containing the IP packet to be sent.
00561  * @param ip6addr The IP address of the packet destination.
00562  *
00563  * @return err_t
00564  */
00565 err_t
00566 lowpan6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr)
00567 {
00568   err_t result;
00569   const u8_t *hwaddr;
00570   struct lowpan6_link_addr src, dest;
00571 #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
00572   ip6_addr_t ip6_src;
00573   struct ip6_hdr *ip6_hdr;
00574 #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
00575 
00576 #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
00577   /* Check if we can compress source address (use aligned copy) */
00578   ip6_hdr = (struct ip6_hdr *)q->payload;
00579   ip6_addr_copy_from_packed(ip6_src, ip6_hdr->src);
00580   ip6_addr_assign_zone(&ip6_src, IP6_UNICAST, netif);
00581   if (lowpan6_get_address_mode(&ip6_src, &short_mac_addr) == 3) {
00582     src.addr_len = 2;
00583     src.addr[0] = short_mac_addr.addr[0];
00584     src.addr[1] = short_mac_addr.addr[1];
00585   } else
00586 #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
00587   {
00588     result = lowpan6_hwaddr_to_addr(netif, &src);
00589     if (result != ERR_OK) {
00590       MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
00591       return result;
00592     }
00593   }
00594 
00595   /* multicast destination IP address? */
00596   if (ip6_addr_ismulticast(ip6addr)) {
00597     MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
00598     /* We need to send to the broadcast address.*/
00599     return lowpan6_frag(netif, q, &src, &ieee_802154_broadcast);
00600   }
00601 
00602   /* We have a unicast destination IP address */
00603   /* @todo anycast? */
00604 
00605 #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
00606   if (src.addr_len == 2) {
00607     /* If source address was compressable to short_mac_addr, and dest has same subnet and
00608      * is also compressable to 2-bytes, assume we can infer dest as a short address too. */
00609     dest.addr_len = 2;
00610     dest.addr[0] = ((u8_t *)q->payload)[38];
00611     dest.addr[1] = ((u8_t *)q->payload)[39];
00612     if ((src.addr_len == 2) && (ip6_addr_netcmp_zoneless(&ip6_hdr->src, &ip6_hdr->dest)) &&
00613         (lowpan6_get_address_mode(ip6addr, &dest) == 3)) {
00614       MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
00615       return lowpan6_frag(netif, q, &src, &dest);
00616     }
00617   }
00618 #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
00619 
00620   /* Ask ND6 what to do with the packet. */
00621   result = nd6_get_next_hop_addr_or_queue(netif, q, ip6addr, &hwaddr);
00622   if (result != ERR_OK) {
00623     MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
00624     return result;
00625   }
00626 
00627   /* If no hardware address is returned, nd6 has queued the packet for later. */
00628   if (hwaddr == NULL) {
00629     return ERR_OK;
00630   }
00631 
00632   /* Send out the packet using the returned hardware address. */
00633   dest.addr_len = netif->hwaddr_len;
00634   /* XXX: Inferring the length of the source address from the destination address
00635    * is not correct for IEEE 802.15.4, but currently we don't get this information
00636    * from the neighbor cache */
00637   SMEMCPY(dest.addr, hwaddr, netif->hwaddr_len);
00638   MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
00639   return lowpan6_frag(netif, q, &src, &dest);
00640 }
00641 /**
00642  * @ingroup sixlowpan
00643  * NETIF input function: don't free the input pbuf when returning != ERR_OK!
00644  */
00645 err_t
00646 lowpan6_input(struct pbuf *p, struct netif *netif)
00647 {
00648   u8_t *puc, b;
00649   s8_t i;
00650   struct lowpan6_link_addr src, dest;
00651   u16_t datagram_size = 0;
00652   u16_t datagram_offset, datagram_tag;
00653   struct lowpan6_reass_helper *lrh, *lrh_next, *lrh_prev = NULL;
00654 
00655   if (p == NULL) {
00656     return ERR_OK;
00657   }
00658 
00659   MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
00660 
00661   if (p->len != p->tot_len) {
00662     /* for now, this needs a pbuf in one piece */
00663     goto lowpan6_input_discard;
00664   }
00665 
00666   if (lowpan6_parse_iee802154_header(p, &src, &dest) != ERR_OK) {
00667     goto lowpan6_input_discard;
00668   }
00669 
00670   /* Check dispatch. */
00671   puc = (u8_t *)p->payload;
00672 
00673   b = *puc;
00674   if ((b & 0xf8) == 0xc0) {
00675     /* FRAG1 dispatch. add this packet to reassembly list. */
00676     datagram_size = ((u16_t)(puc[0] & 0x07) << 8) | (u16_t)puc[1];
00677     datagram_tag = ((u16_t)puc[2] << 8) | (u16_t)puc[3];
00678 
00679     /* check for duplicate */
00680     lrh = lowpan6_data.reass_list;
00681     while (lrh != NULL) {
00682       uint8_t discard = 0;
00683       lrh_next = lrh->next_packet;
00684       if ((lrh->sender_addr.addr_len == src.addr_len) &&
00685           (memcmp(lrh->sender_addr.addr, src.addr, src.addr_len) == 0)) {
00686         /* address match with packet in reassembly. */
00687         if ((datagram_tag == lrh->datagram_tag) && (datagram_size == lrh->datagram_size)) {
00688           /* duplicate fragment. */
00689           goto lowpan6_input_discard;
00690         } else {
00691           /* We are receiving the start of a new datagram. Discard old one (incomplete). */
00692           discard = 1;
00693         }
00694       }
00695       if (discard) {
00696         dequeue_datagram(lrh, lrh_prev);
00697         free_reass_datagram(lrh);
00698       } else {
00699         lrh_prev = lrh;
00700       }
00701       /* Check next datagram in queue. */
00702       lrh = lrh_next;
00703     }
00704 
00705     pbuf_remove_header(p, 4); /* hide frag1 dispatch */
00706 
00707     lrh = (struct lowpan6_reass_helper *) mem_malloc(sizeof(struct lowpan6_reass_helper));
00708     if (lrh == NULL) {
00709       goto lowpan6_input_discard;
00710     }
00711 
00712     lrh->sender_addr.addr_len = src.addr_len;
00713     for (i = 0; i < src.addr_len; i++) {
00714       lrh->sender_addr.addr[i] = src.addr[i];
00715     }
00716     lrh->datagram_size = datagram_size;
00717     lrh->datagram_tag = datagram_tag;
00718     lrh->frags = NULL;
00719     if (*(u8_t *)p->payload == 0x41) {
00720       /* This is a complete IPv6 packet, just skip dispatch byte. */
00721       pbuf_remove_header(p, 1); /* hide dispatch byte. */
00722       lrh->reass = p;
00723     } else if ((*(u8_t *)p->payload & 0xe0 ) == 0x60) {
00724       lrh->reass = lowpan6_decompress(p, datagram_size, LWIP_6LOWPAN_CONTEXTS(netif), &src, &dest);
00725       if (lrh->reass == NULL) {
00726         /* decompression failed */
00727         mem_free(lrh);
00728         goto lowpan6_input_discard;
00729       }
00730     }
00731     /* TODO: handle the case where we already have FRAGN received */
00732     lrh->next_packet = lowpan6_data.reass_list;
00733     lrh->timer = 2;
00734     lowpan6_data.reass_list = lrh;
00735 
00736     return ERR_OK;
00737   } else if ((b & 0xf8) == 0xe0) {
00738     /* FRAGN dispatch, find packet being reassembled. */
00739     datagram_size = ((u16_t)(puc[0] & 0x07) << 8) | (u16_t)puc[1];
00740     datagram_tag = ((u16_t)puc[2] << 8) | (u16_t)puc[3];
00741     datagram_offset = (u16_t)puc[4] << 3;
00742     pbuf_remove_header(p, 4); /* hide frag1 dispatch but keep datagram offset for reassembly */
00743 
00744     for (lrh = lowpan6_data.reass_list; lrh != NULL; lrh_prev = lrh, lrh = lrh->next_packet) {
00745       if ((lrh->sender_addr.addr_len == src.addr_len) &&
00746           (memcmp(lrh->sender_addr.addr, src.addr, src.addr_len) == 0) &&
00747           (datagram_tag == lrh->datagram_tag) &&
00748           (datagram_size == lrh->datagram_size)) {
00749         break;
00750       }
00751     }
00752     if (lrh == NULL) {
00753       /* rogue fragment */
00754       goto lowpan6_input_discard;
00755     }
00756     /* Insert new pbuf into list of fragments. Each fragment is a pbuf,
00757        this only works for unchained pbufs. */
00758     LWIP_ASSERT("p->next == NULL", p->next == NULL);
00759     if (lrh->reass != NULL) {
00760       /* FRAG1 already received, check this offset against first len */
00761       if (datagram_offset < lrh->reass->len) {
00762         /* fragment overlap, discard old fragments */
00763         dequeue_datagram(lrh, lrh_prev);
00764         free_reass_datagram(lrh);
00765         goto lowpan6_input_discard;
00766       }
00767     }
00768     if (lrh->frags == NULL) {
00769       /* first FRAGN */
00770       lrh->frags = p;
00771     } else {
00772       /* find the correct place to insert */
00773       struct pbuf *q, *last;
00774       u16_t new_frag_len = p->len - 1; /* p->len includes datagram_offset byte */
00775       for (q = lrh->frags, last = NULL; q != NULL; last = q, q = q->next) {
00776         u16_t q_datagram_offset = ((u8_t *)q->payload)[0] << 3;
00777         u16_t q_frag_len = q->len - 1;
00778         if (datagram_offset < q_datagram_offset) {
00779           if (datagram_offset + new_frag_len > q_datagram_offset) {
00780             /* overlap, discard old fragments */
00781             dequeue_datagram(lrh, lrh_prev);
00782             free_reass_datagram(lrh);
00783             goto lowpan6_input_discard;
00784           }
00785           /* insert here */
00786           break;
00787         } else if (datagram_offset == q_datagram_offset) {
00788           if (q_frag_len != new_frag_len) {
00789             /* fragment mismatch, discard old fragments */
00790             dequeue_datagram(lrh, lrh_prev);
00791             free_reass_datagram(lrh);
00792             goto lowpan6_input_discard;
00793           }
00794           /* duplicate, ignore */
00795           pbuf_free(p);
00796           return ERR_OK;
00797         }
00798       }
00799       /* insert fragment */
00800       if (last == NULL) {
00801         lrh->frags = p;
00802       } else {
00803         last->next = p;
00804         p->next = q;
00805       }
00806     }
00807     /* check if all fragments were received */
00808     if (lrh->reass) {
00809       u16_t offset = lrh->reass->len;
00810       struct pbuf *q;
00811       for (q = lrh->frags; q != NULL; q = q->next) {
00812         u16_t q_datagram_offset = ((u8_t *)q->payload)[0] << 3;
00813         if (q_datagram_offset != offset) {
00814           /* not complete, wait for more fragments */
00815           return ERR_OK;
00816         }
00817         offset += q->len - 1;
00818       }
00819       if (offset == datagram_size) {
00820         /* all fragments received, combine pbufs */
00821         u16_t datagram_left = datagram_size - lrh->reass->len;
00822         for (q = lrh->frags; q != NULL; q = q->next) {
00823           /* hide datagram_offset byte now */
00824           pbuf_remove_header(q, 1);
00825           q->tot_len = datagram_left;
00826           datagram_left -= q->len;
00827         }
00828         LWIP_ASSERT("datagram_left == 0", datagram_left == 0);
00829         q = lrh->reass;
00830         q->tot_len = datagram_size;
00831         q->next = lrh->frags;
00832         lrh->frags = NULL;
00833         lrh->reass = NULL;
00834         dequeue_datagram(lrh, lrh_prev);
00835         mem_free(lrh);
00836 
00837         /* @todo: distinguish unicast/multicast */
00838         MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
00839         return ip6_input(q, netif);
00840       }
00841     }
00842     /* pbuf enqueued, waiting for more fragments */
00843     return ERR_OK;
00844   } else {
00845     if (b == 0x41) {
00846       /* This is a complete IPv6 packet, just skip dispatch byte. */
00847       pbuf_remove_header(p, 1); /* hide dispatch byte. */
00848     } else if ((b & 0xe0 ) == 0x60) {
00849       /* IPv6 headers are compressed using IPHC. */
00850       p = lowpan6_decompress(p, datagram_size, LWIP_6LOWPAN_CONTEXTS(netif), &src, &dest);
00851       if (p == NULL) {
00852         MIB2_STATS_NETIF_INC(netif, ifindiscards);
00853         return ERR_OK;
00854       }
00855     } else {
00856       goto lowpan6_input_discard;
00857     }
00858 
00859     /* @todo: distinguish unicast/multicast */
00860     MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
00861 
00862     return ip6_input(p, netif);
00863   }
00864 lowpan6_input_discard:
00865   MIB2_STATS_NETIF_INC(netif, ifindiscards);
00866   pbuf_free(p);
00867   /* always return ERR_OK here to prevent the caller freeing the pbuf */
00868   return ERR_OK;
00869 }
00870 
00871 /**
00872  * @ingroup sixlowpan
00873  */
00874 err_t
00875 lowpan6_if_init(struct netif *netif)
00876 {
00877   netif->name[0] = 'L';
00878   netif->name[1] = '6';
00879   netif->output_ip6 = lowpan6_output;
00880 
00881   MIB2_INIT_NETIF(netif, snmp_ifType_other, 0);
00882 
00883   /* maximum transfer unit */
00884   netif->mtu = 1280;
00885 
00886   /* broadcast capability */
00887   netif->flags = NETIF_FLAG_BROADCAST /* | NETIF_FLAG_LOWPAN6 */;
00888 
00889   return ERR_OK;
00890 }
00891 
00892 /**
00893  * @ingroup sixlowpan
00894  * Set PAN ID
00895  */
00896 err_t
00897 lowpan6_set_pan_id(u16_t pan_id)
00898 {
00899   lowpan6_data.ieee_802154_pan_id = pan_id;
00900 
00901   return ERR_OK;
00902 }
00903 
00904 #if !NO_SYS
00905 /**
00906  * @ingroup sixlowpan
00907  * Pass a received packet to tcpip_thread for input processing
00908  *
00909  * @param p the received packet, p->payload pointing to the
00910  *          IEEE 802.15.4 header.
00911  * @param inp the network interface on which the packet was received
00912  */
00913 err_t
00914 tcpip_6lowpan_input(struct pbuf *p, struct netif *inp)
00915 {
00916   return tcpip_inpkt(p, inp, lowpan6_input);
00917 }
00918 #endif /* !NO_SYS */
00919 
00920 #endif /* LWIP_IPV6 */