lowpan6.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /**
  2. * @file
  3. *
  4. * 6LowPAN output for IPv6. Uses ND tables for link-layer addressing. Fragments packets to 6LowPAN units.
  5. *
  6. * This implementation aims to conform to IEEE 802.15.4(-2015), RFC 4944 and RFC 6282.
  7. * @todo: RFC 6775.
  8. */
  9. /*
  10. * Copyright (c) 2015 Inico Technologies Ltd.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * 3. The name of the author may not be used to endorse or promote products
  22. * derived from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  27. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  29. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  32. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  33. * OF SUCH DAMAGE.
  34. *
  35. * This file is part of the lwIP TCP/IP stack.
  36. *
  37. * Author: Ivan Delamer <delamer@inicotech.com>
  38. *
  39. *
  40. * Please coordinate changes and requests with Ivan Delamer
  41. * <delamer@inicotech.com>
  42. */
  43. /**
  44. * @defgroup sixlowpan 6LoWPAN (RFC4944)
  45. * @ingroup netifs
  46. * 6LowPAN netif implementation
  47. */
  48. #include "netif/lowpan6.h"
  49. #if LWIP_IPV6 && LWIP_6LOWPAN
  50. #include "lwip/ip.h"
  51. #include "lwip/pbuf.h"
  52. #include "lwip/ip_addr.h"
  53. #include "lwip/netif.h"
  54. #include "lwip/nd6.h"
  55. #include "lwip/mem.h"
  56. #include "lwip/udp.h"
  57. #include "lwip/tcpip.h"
  58. #include "lwip/snmp.h"
  59. #include "netif/ieee802154.h"
  60. #include <string.h>
  61. #if LWIP_6LOWPAN_802154_HW_CRC
  62. #define LWIP_6LOWPAN_DO_CALC_CRC(buf, len) 0
  63. #else
  64. #define LWIP_6LOWPAN_DO_CALC_CRC(buf, len) LWIP_6LOWPAN_CALC_CRC(buf, len)
  65. #endif
  66. /** This is a helper struct for reassembly of fragments
  67. * (IEEE 802.15.4 limits to 127 bytes)
  68. */
  69. struct lowpan6_reass_helper {
  70. struct lowpan6_reass_helper *next_packet;
  71. struct pbuf *reass;
  72. struct pbuf *frags;
  73. u8_t timer;
  74. struct lowpan6_link_addr sender_addr;
  75. u16_t datagram_size;
  76. u16_t datagram_tag;
  77. };
  78. /** This struct keeps track of per-netif state */
  79. struct lowpan6_ieee802154_data {
  80. /** fragment reassembly list */
  81. struct lowpan6_reass_helper *reass_list;
  82. #if LWIP_6LOWPAN_NUM_CONTEXTS > 0
  83. /** address context for compression */
  84. ip6_addr_t lowpan6_context[LWIP_6LOWPAN_NUM_CONTEXTS];
  85. #endif
  86. /** Datagram Tag for fragmentation */
  87. u16_t tx_datagram_tag;
  88. /** local PAN ID for IEEE 802.15.4 header */
  89. u16_t ieee_802154_pan_id;
  90. /** Sequence Number for IEEE 802.15.4 transmission */
  91. u8_t tx_frame_seq_num;
  92. };
  93. /* Maximum frame size is 127 bytes minus CRC size */
  94. #define LOWPAN6_MAX_PAYLOAD (127 - 2)
  95. /** Currently, this state is global, since there's only one 6LoWPAN netif */
  96. static struct lowpan6_ieee802154_data lowpan6_data;
  97. #if LWIP_6LOWPAN_NUM_CONTEXTS > 0
  98. #define LWIP_6LOWPAN_CONTEXTS(netif) lowpan6_data.lowpan6_context
  99. #else
  100. #define LWIP_6LOWPAN_CONTEXTS(netif) NULL
  101. #endif
  102. static const struct lowpan6_link_addr ieee_802154_broadcast = {2, {0xff, 0xff}};
  103. #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
  104. static struct lowpan6_link_addr short_mac_addr = {2, {0, 0}};
  105. #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
  106. /* IEEE 802.15.4 specific functions: */
  107. /** Write the IEEE 802.15.4 header that encapsulates the 6LoWPAN frame.
  108. * Src and dst PAN IDs are filled with the ID set by @ref lowpan6_set_pan_id.
  109. *
  110. * Since the length is variable:
  111. * @returns the header length
  112. */
  113. static u8_t
  114. lowpan6_write_iee802154_header(struct ieee_802154_hdr *hdr, const struct lowpan6_link_addr *src,
  115. const struct lowpan6_link_addr *dst)
  116. {
  117. u8_t ieee_header_len;
  118. u8_t *buffer;
  119. u8_t i;
  120. u16_t fc;
  121. fc = IEEE_802154_FC_FT_DATA; /* send data packet (2003 frame version) */
  122. fc |= IEEE_802154_FC_PANID_COMPR; /* set PAN ID compression, for now src and dst PANs are equal */
  123. if (dst != &ieee_802154_broadcast) {
  124. fc |= IEEE_802154_FC_ACK_REQ; /* data packet, no broadcast: ack required. */
  125. }
  126. if (dst->addr_len == 2) {
  127. fc |= IEEE_802154_FC_DST_ADDR_MODE_SHORT;
  128. } else {
  129. LWIP_ASSERT("invalid dst address length", dst->addr_len == 8);
  130. fc |= IEEE_802154_FC_DST_ADDR_MODE_EXT;
  131. }
  132. if (src->addr_len == 2) {
  133. fc |= IEEE_802154_FC_SRC_ADDR_MODE_SHORT;
  134. } else {
  135. LWIP_ASSERT("invalid src address length", src->addr_len == 8);
  136. fc |= IEEE_802154_FC_SRC_ADDR_MODE_EXT;
  137. }
  138. hdr->frame_control = fc;
  139. hdr->sequence_number = lowpan6_data.tx_frame_seq_num++;
  140. hdr->destination_pan_id = lowpan6_data.ieee_802154_pan_id; /* pan id */
  141. buffer = (u8_t *)hdr;
  142. ieee_header_len = 5;
  143. i = dst->addr_len;
  144. /* reverse memcpy of dst addr */
  145. while (i-- > 0) {
  146. buffer[ieee_header_len++] = dst->addr[i];
  147. }
  148. /* Source PAN ID skipped due to PAN ID Compression */
  149. i = src->addr_len;
  150. /* reverse memcpy of src addr */
  151. while (i-- > 0) {
  152. buffer[ieee_header_len++] = src->addr[i];
  153. }
  154. return ieee_header_len;
  155. }
  156. /** Parse the IEEE 802.15.4 header from a pbuf.
  157. * If successful, the header is hidden from the pbuf.
  158. *
  159. * PAN IDs and seuqence number are not checked
  160. *
  161. * @param p input pbuf, p->payload pointing at the IEEE 802.15.4 header
  162. * @param src pointer to source address filled from the header
  163. * @param dest pointer to destination address filled from the header
  164. * @returns ERR_OK if successful
  165. */
  166. static err_t
  167. lowpan6_parse_iee802154_header(struct pbuf *p, struct lowpan6_link_addr *src,
  168. struct lowpan6_link_addr *dest)
  169. {
  170. u8_t *puc;
  171. s8_t i;
  172. u16_t frame_control, addr_mode;
  173. u16_t datagram_offset;
  174. /* Parse IEEE 802.15.4 header */
  175. puc = (u8_t *)p->payload;
  176. frame_control = puc[0] | (puc[1] << 8);
  177. datagram_offset = 2;
  178. if (frame_control & IEEE_802154_FC_SEQNO_SUPPR) {
  179. if (IEEE_802154_FC_FRAME_VERSION_GET(frame_control) <= 1) {
  180. /* sequence number suppressed, this is not valid for versions 0/1 */
  181. return ERR_VAL;
  182. }
  183. } else {
  184. datagram_offset++;
  185. }
  186. datagram_offset += 2; /* Skip destination PAN ID */
  187. addr_mode = frame_control & IEEE_802154_FC_DST_ADDR_MODE_MASK;
  188. if (addr_mode == IEEE_802154_FC_DST_ADDR_MODE_EXT) {
  189. /* extended address (64 bit) */
  190. dest->addr_len = 8;
  191. /* reverse memcpy: */
  192. for (i = 0; i < 8; i++) {
  193. dest->addr[i] = puc[datagram_offset + 7 - i];
  194. }
  195. datagram_offset += 8;
  196. } else if (addr_mode == IEEE_802154_FC_DST_ADDR_MODE_SHORT) {
  197. /* short address (16 bit) */
  198. dest->addr_len = 2;
  199. /* reverse memcpy: */
  200. dest->addr[0] = puc[datagram_offset + 1];
  201. dest->addr[1] = puc[datagram_offset];
  202. datagram_offset += 2;
  203. } else {
  204. /* unsupported address mode (do we need "no address"?) */
  205. return ERR_VAL;
  206. }
  207. if (!(frame_control & IEEE_802154_FC_PANID_COMPR)) {
  208. /* No PAN ID compression, skip source PAN ID */
  209. datagram_offset += 2;
  210. }
  211. addr_mode = frame_control & IEEE_802154_FC_SRC_ADDR_MODE_MASK;
  212. if (addr_mode == IEEE_802154_FC_SRC_ADDR_MODE_EXT) {
  213. /* extended address (64 bit) */
  214. src->addr_len = 8;
  215. /* reverse memcpy: */
  216. for (i = 0; i < 8; i++) {
  217. src->addr[i] = puc[datagram_offset + 7 - i];
  218. }
  219. datagram_offset += 8;
  220. } else if (addr_mode == IEEE_802154_FC_DST_ADDR_MODE_SHORT) {
  221. /* short address (16 bit) */
  222. src->addr_len = 2;
  223. src->addr[0] = puc[datagram_offset + 1];
  224. src->addr[1] = puc[datagram_offset];
  225. datagram_offset += 2;
  226. } else {
  227. /* unsupported address mode (do we need "no address"?) */
  228. return ERR_VAL;
  229. }
  230. /* hide IEEE802.15.4 header. */
  231. if (pbuf_remove_header(p, datagram_offset)) {
  232. return ERR_VAL;
  233. }
  234. return ERR_OK;
  235. }
  236. /** Calculate the 16-bit CRC as required by IEEE 802.15.4 */
  237. u16_t
  238. lowpan6_calc_crc(const void* buf, u16_t len)
  239. {
  240. #define CCITT_POLY_16 0x8408U
  241. u16_t i;
  242. u8_t b;
  243. u16_t crc = 0;
  244. const u8_t* p = (const u8_t*)buf;
  245. for (i = 0; i < len; i++) {
  246. u8_t data = *p;
  247. for (b = 0U; b < 8U; b++) {
  248. if (((data ^ crc) & 1) != 0) {
  249. crc = (u16_t)((crc >> 1) ^ CCITT_POLY_16);
  250. } else {
  251. crc = (u16_t)(crc >> 1);
  252. }
  253. data = (u8_t)(data >> 1);
  254. }
  255. p++;
  256. }
  257. return crc;
  258. }
  259. /* Fragmentation specific functions: */
  260. static void
  261. free_reass_datagram(struct lowpan6_reass_helper *lrh)
  262. {
  263. if (lrh->reass) {
  264. pbuf_free(lrh->reass);
  265. }
  266. if (lrh->frags) {
  267. pbuf_free(lrh->frags);
  268. }
  269. mem_free(lrh);
  270. }
  271. /**
  272. * Removes a datagram from the reassembly queue.
  273. **/
  274. static void
  275. dequeue_datagram(struct lowpan6_reass_helper *lrh, struct lowpan6_reass_helper *prev)
  276. {
  277. if (lowpan6_data.reass_list == lrh) {
  278. lowpan6_data.reass_list = lowpan6_data.reass_list->next_packet;
  279. } else {
  280. /* it wasn't the first, so it must have a valid 'prev' */
  281. LWIP_ASSERT("sanity check linked list", prev != NULL);
  282. prev->next_packet = lrh->next_packet;
  283. }
  284. }
  285. /**
  286. * Periodic timer for 6LowPAN functions:
  287. *
  288. * - Remove incomplete/old packets
  289. */
  290. void
  291. lowpan6_tmr(void)
  292. {
  293. struct lowpan6_reass_helper *lrh, *lrh_next, *lrh_prev = NULL;
  294. lrh = lowpan6_data.reass_list;
  295. while (lrh != NULL) {
  296. lrh_next = lrh->next_packet;
  297. if ((--lrh->timer) == 0) {
  298. dequeue_datagram(lrh, lrh_prev);
  299. free_reass_datagram(lrh);
  300. } else {
  301. lrh_prev = lrh;
  302. }
  303. lrh = lrh_next;
  304. }
  305. }
  306. /*
  307. * Encapsulates data into IEEE 802.15.4 frames.
  308. * Fragments an IPv6 datagram into 6LowPAN units, which fit into IEEE 802.15.4 frames.
  309. * If configured, will compress IPv6 and or UDP headers.
  310. * */
  311. static err_t
  312. lowpan6_frag(struct netif *netif, struct pbuf *p, const struct lowpan6_link_addr *src, const struct lowpan6_link_addr *dst)
  313. {
  314. struct pbuf *p_frag;
  315. u16_t frag_len, remaining_len, max_data_len;
  316. u8_t *buffer;
  317. u8_t ieee_header_len;
  318. u8_t lowpan6_header_len;
  319. u8_t hidden_header_len;
  320. u16_t crc;
  321. u16_t datagram_offset;
  322. err_t err = ERR_IF;
  323. LWIP_ASSERT("lowpan6_frag: netif->linkoutput not set", netif->linkoutput != NULL);
  324. /* We'll use a dedicated pbuf for building 6LowPAN fragments. */
  325. p_frag = pbuf_alloc(PBUF_RAW, 127, PBUF_RAM);
  326. if (p_frag == NULL) {
  327. MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
  328. return ERR_MEM;
  329. }
  330. LWIP_ASSERT("this needs a pbuf in one piece", p_frag->len == p_frag->tot_len);
  331. /* Write IEEE 802.15.4 header. */
  332. buffer = (u8_t *)p_frag->payload;
  333. ieee_header_len = lowpan6_write_iee802154_header((struct ieee_802154_hdr *)buffer, src, dst);
  334. LWIP_ASSERT("ieee_header_len < p_frag->len", ieee_header_len < p_frag->len);
  335. #if LWIP_6LOWPAN_IPHC
  336. /* Perform 6LowPAN IPv6 header compression according to RFC 6282 */
  337. /* do the header compression (this does NOT copy any non-compressed data) */
  338. err = lowpan6_compress_headers(netif, (u8_t *)p->payload, p->len,
  339. &buffer[ieee_header_len], p_frag->len - ieee_header_len, &lowpan6_header_len,
  340. &hidden_header_len, LWIP_6LOWPAN_CONTEXTS(netif), src, dst);
  341. if (err != ERR_OK) {
  342. MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
  343. pbuf_free(p_frag);
  344. return err;
  345. }
  346. pbuf_remove_header(p, hidden_header_len);
  347. #else /* LWIP_6LOWPAN_IPHC */
  348. /* Send uncompressed IPv6 header with appropriate dispatch byte. */
  349. lowpan6_header_len = 1;
  350. buffer[ieee_header_len] = 0x41; /* IPv6 dispatch */
  351. #endif /* LWIP_6LOWPAN_IPHC */
  352. /* Calculate remaining packet length */
  353. remaining_len = p->tot_len;
  354. if (remaining_len > 0x7FF) {
  355. MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
  356. /* datagram_size must fit into 11 bit */
  357. pbuf_free(p_frag);
  358. return ERR_VAL;
  359. }
  360. /* Fragment, or 1 packet? */
  361. max_data_len = LOWPAN6_MAX_PAYLOAD - ieee_header_len - lowpan6_header_len;
  362. if (remaining_len > max_data_len) {
  363. u16_t data_len;
  364. /* We must move the 6LowPAN header to make room for the FRAG header. */
  365. memmove(&buffer[ieee_header_len + 4], &buffer[ieee_header_len], lowpan6_header_len);
  366. /* Now we need to fragment the packet. FRAG1 header first */
  367. buffer[ieee_header_len] = 0xc0 | (((p->tot_len + hidden_header_len) >> 8) & 0x7);
  368. buffer[ieee_header_len + 1] = (p->tot_len + hidden_header_len) & 0xff;
  369. lowpan6_data.tx_datagram_tag++;
  370. buffer[ieee_header_len + 2] = (lowpan6_data.tx_datagram_tag >> 8) & 0xff;
  371. buffer[ieee_header_len + 3] = lowpan6_data.tx_datagram_tag & 0xff;
  372. /* Fragment follows. */
  373. data_len = (max_data_len - 4) & 0xf8;
  374. frag_len = data_len + lowpan6_header_len;
  375. pbuf_copy_partial(p, buffer + ieee_header_len + lowpan6_header_len + 4, frag_len - lowpan6_header_len, 0);
  376. remaining_len -= frag_len - lowpan6_header_len;
  377. /* datagram offset holds the offset before compression */
  378. datagram_offset = frag_len - lowpan6_header_len + hidden_header_len;
  379. LWIP_ASSERT("datagram offset must be a multiple of 8", (datagram_offset & 7) == 0);
  380. /* Calculate frame length */
  381. p_frag->len = p_frag->tot_len = ieee_header_len + 4 + frag_len + 2; /* add 2 bytes for crc*/
  382. /* 2 bytes CRC */
  383. crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
  384. pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
  385. /* send the packet */
  386. MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
  387. LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
  388. err = netif->linkoutput(netif, p_frag);
  389. while ((remaining_len > 0) && (err == ERR_OK)) {
  390. struct ieee_802154_hdr *hdr = (struct ieee_802154_hdr *)buffer;
  391. /* new frame, new seq num for ACK */
  392. hdr->sequence_number = lowpan6_data.tx_frame_seq_num++;
  393. buffer[ieee_header_len] |= 0x20; /* Change FRAG1 to FRAGN */
  394. LWIP_ASSERT("datagram offset must be a multiple of 8", (datagram_offset & 7) == 0);
  395. buffer[ieee_header_len + 4] = (u8_t)(datagram_offset >> 3); /* datagram offset in FRAGN header (datagram_offset is max. 11 bit) */
  396. frag_len = (127 - ieee_header_len - 5 - 2) & 0xf8;
  397. if (frag_len > remaining_len) {
  398. frag_len = remaining_len;
  399. }
  400. pbuf_copy_partial(p, buffer + ieee_header_len + 5, frag_len, p->tot_len - remaining_len);
  401. remaining_len -= frag_len;
  402. datagram_offset += frag_len;
  403. /* Calculate frame length */
  404. p_frag->len = p_frag->tot_len = frag_len + 5 + ieee_header_len + 2;
  405. /* 2 bytes CRC */
  406. crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
  407. pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
  408. /* send the packet */
  409. MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
  410. LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
  411. err = netif->linkoutput(netif, p_frag);
  412. }
  413. } else {
  414. /* It fits in one frame. */
  415. frag_len = remaining_len;
  416. /* Copy IPv6 packet */
  417. pbuf_copy_partial(p, buffer + ieee_header_len + lowpan6_header_len, frag_len, 0);
  418. remaining_len = 0;
  419. /* Calculate frame length */
  420. p_frag->len = p_frag->tot_len = frag_len + lowpan6_header_len + ieee_header_len + 2;
  421. LWIP_ASSERT("", p_frag->len <= 127);
  422. /* 2 bytes CRC */
  423. crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
  424. pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
  425. /* send the packet */
  426. MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
  427. LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
  428. err = netif->linkoutput(netif, p_frag);
  429. }
  430. pbuf_free(p_frag);
  431. return err;
  432. }
  433. /**
  434. * @ingroup sixlowpan
  435. * Set context
  436. */
  437. err_t
  438. lowpan6_set_context(u8_t idx, const ip6_addr_t *context)
  439. {
  440. #if LWIP_6LOWPAN_NUM_CONTEXTS > 0
  441. if (idx >= LWIP_6LOWPAN_NUM_CONTEXTS) {
  442. return ERR_ARG;
  443. }
  444. IP6_ADDR_ZONECHECK(context);
  445. ip6_addr_set(&lowpan6_data.lowpan6_context[idx], context);
  446. return ERR_OK;
  447. #else
  448. LWIP_UNUSED_ARG(idx);
  449. LWIP_UNUSED_ARG(context);
  450. return ERR_ARG;
  451. #endif
  452. }
  453. #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
  454. /**
  455. * @ingroup sixlowpan
  456. * Set short address
  457. */
  458. err_t
  459. lowpan6_set_short_addr(u8_t addr_high, u8_t addr_low)
  460. {
  461. short_mac_addr.addr[0] = addr_high;
  462. short_mac_addr.addr[1] = addr_low;
  463. return ERR_OK;
  464. }
  465. #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
  466. /* Create IEEE 802.15.4 address from netif address */
  467. static err_t
  468. lowpan6_hwaddr_to_addr(struct netif *netif, struct lowpan6_link_addr *addr)
  469. {
  470. addr->addr_len = 8;
  471. if (netif->hwaddr_len == 8) {
  472. LWIP_ERROR("NETIF_MAX_HWADDR_LEN >= 8 required", sizeof(netif->hwaddr) >= 8, return ERR_VAL;);
  473. SMEMCPY(addr->addr, netif->hwaddr, 8);
  474. } else if (netif->hwaddr_len == 6) {
  475. /* Copy from MAC-48 */
  476. SMEMCPY(addr->addr, netif->hwaddr, 3);
  477. addr->addr[3] = addr->addr[4] = 0xff;
  478. SMEMCPY(&addr->addr[5], &netif->hwaddr[3], 3);
  479. } else {
  480. /* Invalid address length, don't know how to convert this */
  481. return ERR_VAL;
  482. }
  483. return ERR_OK;
  484. }
  485. /**
  486. * @ingroup sixlowpan
  487. * Resolve and fill-in IEEE 802.15.4 address header for outgoing IPv6 packet.
  488. *
  489. * Perform Header Compression and fragment if necessary.
  490. *
  491. * @param netif The lwIP network interface which the IP packet will be sent on.
  492. * @param q The pbuf(s) containing the IP packet to be sent.
  493. * @param ip6addr The IP address of the packet destination.
  494. *
  495. * @return err_t
  496. */
  497. err_t
  498. lowpan6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr)
  499. {
  500. err_t result;
  501. const u8_t *hwaddr;
  502. struct lowpan6_link_addr src, dest;
  503. #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
  504. ip6_addr_t ip6_src;
  505. struct ip6_hdr *ip6_hdr;
  506. #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
  507. #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
  508. /* Check if we can compress source address (use aligned copy) */
  509. ip6_hdr = (struct ip6_hdr *)q->payload;
  510. ip6_addr_copy_from_packed(ip6_src, ip6_hdr->src);
  511. ip6_addr_assign_zone(&ip6_src, IP6_UNICAST, netif);
  512. if (lowpan6_get_address_mode(&ip6_src, &short_mac_addr) == 3) {
  513. src.addr_len = 2;
  514. src.addr[0] = short_mac_addr.addr[0];
  515. src.addr[1] = short_mac_addr.addr[1];
  516. } else
  517. #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
  518. {
  519. result = lowpan6_hwaddr_to_addr(netif, &src);
  520. if (result != ERR_OK) {
  521. MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
  522. return result;
  523. }
  524. }
  525. /* multicast destination IP address? */
  526. if (ip6_addr_ismulticast(ip6addr)) {
  527. MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
  528. /* We need to send to the broadcast address.*/
  529. return lowpan6_frag(netif, q, &src, &ieee_802154_broadcast);
  530. }
  531. /* We have a unicast destination IP address */
  532. /* @todo anycast? */
  533. #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
  534. if (src.addr_len == 2) {
  535. /* If source address was compressable to short_mac_addr, and dest has same subnet and
  536. * is also compressable to 2-bytes, assume we can infer dest as a short address too. */
  537. dest.addr_len = 2;
  538. dest.addr[0] = ((u8_t *)q->payload)[38];
  539. dest.addr[1] = ((u8_t *)q->payload)[39];
  540. if ((src.addr_len == 2) && (ip6_addr_netcmp_zoneless(&ip6_hdr->src, &ip6_hdr->dest)) &&
  541. (lowpan6_get_address_mode(ip6addr, &dest) == 3)) {
  542. MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
  543. return lowpan6_frag(netif, q, &src, &dest);
  544. }
  545. }
  546. #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
  547. /* Ask ND6 what to do with the packet. */
  548. result = nd6_get_next_hop_addr_or_queue(netif, q, ip6addr, &hwaddr);
  549. if (result != ERR_OK) {
  550. MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
  551. return result;
  552. }
  553. /* If no hardware address is returned, nd6 has queued the packet for later. */
  554. if (hwaddr == NULL) {
  555. return ERR_OK;
  556. }
  557. /* Send out the packet using the returned hardware address. */
  558. dest.addr_len = netif->hwaddr_len;
  559. /* XXX: Inferring the length of the source address from the destination address
  560. * is not correct for IEEE 802.15.4, but currently we don't get this information
  561. * from the neighbor cache */
  562. SMEMCPY(dest.addr, hwaddr, netif->hwaddr_len);
  563. MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
  564. return lowpan6_frag(netif, q, &src, &dest);
  565. }
  566. /**
  567. * @ingroup sixlowpan
  568. * NETIF input function: don't free the input pbuf when returning != ERR_OK!
  569. */
  570. err_t
  571. lowpan6_input(struct pbuf *p, struct netif *netif)
  572. {
  573. u8_t *puc, b;
  574. s8_t i;
  575. struct lowpan6_link_addr src, dest;
  576. u16_t datagram_size = 0;
  577. u16_t datagram_offset, datagram_tag;
  578. struct lowpan6_reass_helper *lrh, *lrh_next, *lrh_prev = NULL;
  579. if (p == NULL) {
  580. return ERR_OK;
  581. }
  582. MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
  583. if (p->len != p->tot_len) {
  584. /* for now, this needs a pbuf in one piece */
  585. goto lowpan6_input_discard;
  586. }
  587. if (lowpan6_parse_iee802154_header(p, &src, &dest) != ERR_OK) {
  588. goto lowpan6_input_discard;
  589. }
  590. /* Check dispatch. */
  591. puc = (u8_t *)p->payload;
  592. b = *puc;
  593. if ((b & 0xf8) == 0xc0) {
  594. /* FRAG1 dispatch. add this packet to reassembly list. */
  595. datagram_size = ((u16_t)(puc[0] & 0x07) << 8) | (u16_t)puc[1];
  596. datagram_tag = ((u16_t)puc[2] << 8) | (u16_t)puc[3];
  597. /* check for duplicate */
  598. lrh = lowpan6_data.reass_list;
  599. while (lrh != NULL) {
  600. uint8_t discard = 0;
  601. lrh_next = lrh->next_packet;
  602. if ((lrh->sender_addr.addr_len == src.addr_len) &&
  603. (memcmp(lrh->sender_addr.addr, src.addr, src.addr_len) == 0)) {
  604. /* address match with packet in reassembly. */
  605. if ((datagram_tag == lrh->datagram_tag) && (datagram_size == lrh->datagram_size)) {
  606. /* duplicate fragment. */
  607. goto lowpan6_input_discard;
  608. } else {
  609. /* We are receiving the start of a new datagram. Discard old one (incomplete). */
  610. discard = 1;
  611. }
  612. }
  613. if (discard) {
  614. dequeue_datagram(lrh, lrh_prev);
  615. free_reass_datagram(lrh);
  616. } else {
  617. lrh_prev = lrh;
  618. }
  619. /* Check next datagram in queue. */
  620. lrh = lrh_next;
  621. }
  622. pbuf_remove_header(p, 4); /* hide frag1 dispatch */
  623. lrh = (struct lowpan6_reass_helper *) mem_malloc(sizeof(struct lowpan6_reass_helper));
  624. if (lrh == NULL) {
  625. goto lowpan6_input_discard;
  626. }
  627. lrh->sender_addr.addr_len = src.addr_len;
  628. for (i = 0; i < src.addr_len; i++) {
  629. lrh->sender_addr.addr[i] = src.addr[i];
  630. }
  631. lrh->datagram_size = datagram_size;
  632. lrh->datagram_tag = datagram_tag;
  633. lrh->frags = NULL;
  634. if (*(u8_t *)p->payload == 0x41) {
  635. /* This is a complete IPv6 packet, just skip dispatch byte. */
  636. pbuf_remove_header(p, 1); /* hide dispatch byte. */
  637. lrh->reass = p;
  638. } else if ((*(u8_t *)p->payload & 0xe0 ) == 0x60) {
  639. lrh->reass = lowpan6_decompress(p, datagram_size, LWIP_6LOWPAN_CONTEXTS(netif), &src, &dest);
  640. if (lrh->reass == NULL) {
  641. /* decompression failed */
  642. mem_free(lrh);
  643. goto lowpan6_input_discard;
  644. }
  645. }
  646. /* TODO: handle the case where we already have FRAGN received */
  647. lrh->next_packet = lowpan6_data.reass_list;
  648. lrh->timer = 2;
  649. lowpan6_data.reass_list = lrh;
  650. return ERR_OK;
  651. } else if ((b & 0xf8) == 0xe0) {
  652. /* FRAGN dispatch, find packet being reassembled. */
  653. datagram_size = ((u16_t)(puc[0] & 0x07) << 8) | (u16_t)puc[1];
  654. datagram_tag = ((u16_t)puc[2] << 8) | (u16_t)puc[3];
  655. datagram_offset = (u16_t)puc[4] << 3;
  656. pbuf_remove_header(p, 4); /* hide frag1 dispatch but keep datagram offset for reassembly */
  657. for (lrh = lowpan6_data.reass_list; lrh != NULL; lrh_prev = lrh, lrh = lrh->next_packet) {
  658. if ((lrh->sender_addr.addr_len == src.addr_len) &&
  659. (memcmp(lrh->sender_addr.addr, src.addr, src.addr_len) == 0) &&
  660. (datagram_tag == lrh->datagram_tag) &&
  661. (datagram_size == lrh->datagram_size)) {
  662. break;
  663. }
  664. }
  665. if (lrh == NULL) {
  666. /* rogue fragment */
  667. goto lowpan6_input_discard;
  668. }
  669. /* Insert new pbuf into list of fragments. Each fragment is a pbuf,
  670. this only works for unchained pbufs. */
  671. LWIP_ASSERT("p->next == NULL", p->next == NULL);
  672. if (lrh->reass != NULL) {
  673. /* FRAG1 already received, check this offset against first len */
  674. if (datagram_offset < lrh->reass->len) {
  675. /* fragment overlap, discard old fragments */
  676. dequeue_datagram(lrh, lrh_prev);
  677. free_reass_datagram(lrh);
  678. goto lowpan6_input_discard;
  679. }
  680. }
  681. if (lrh->frags == NULL) {
  682. /* first FRAGN */
  683. lrh->frags = p;
  684. } else {
  685. /* find the correct place to insert */
  686. struct pbuf *q, *last;
  687. u16_t new_frag_len = p->len - 1; /* p->len includes datagram_offset byte */
  688. for (q = lrh->frags, last = NULL; q != NULL; last = q, q = q->next) {
  689. u16_t q_datagram_offset = ((u8_t *)q->payload)[0] << 3;
  690. u16_t q_frag_len = q->len - 1;
  691. if (datagram_offset < q_datagram_offset) {
  692. if (datagram_offset + new_frag_len > q_datagram_offset) {
  693. /* overlap, discard old fragments */
  694. dequeue_datagram(lrh, lrh_prev);
  695. free_reass_datagram(lrh);
  696. goto lowpan6_input_discard;
  697. }
  698. /* insert here */
  699. break;
  700. } else if (datagram_offset == q_datagram_offset) {
  701. if (q_frag_len != new_frag_len) {
  702. /* fragment mismatch, discard old fragments */
  703. dequeue_datagram(lrh, lrh_prev);
  704. free_reass_datagram(lrh);
  705. goto lowpan6_input_discard;
  706. }
  707. /* duplicate, ignore */
  708. pbuf_free(p);
  709. return ERR_OK;
  710. }
  711. }
  712. /* insert fragment */
  713. if (last == NULL) {
  714. lrh->frags = p;
  715. } else {
  716. last->next = p;
  717. p->next = q;
  718. }
  719. }
  720. /* check if all fragments were received */
  721. if (lrh->reass) {
  722. u16_t offset = lrh->reass->len;
  723. struct pbuf *q;
  724. for (q = lrh->frags; q != NULL; q = q->next) {
  725. u16_t q_datagram_offset = ((u8_t *)q->payload)[0] << 3;
  726. if (q_datagram_offset != offset) {
  727. /* not complete, wait for more fragments */
  728. return ERR_OK;
  729. }
  730. offset += q->len - 1;
  731. }
  732. if (offset == datagram_size) {
  733. /* all fragments received, combine pbufs */
  734. u16_t datagram_left = datagram_size - lrh->reass->len;
  735. for (q = lrh->frags; q != NULL; q = q->next) {
  736. /* hide datagram_offset byte now */
  737. pbuf_remove_header(q, 1);
  738. q->tot_len = datagram_left;
  739. datagram_left -= q->len;
  740. }
  741. LWIP_ASSERT("datagram_left == 0", datagram_left == 0);
  742. q = lrh->reass;
  743. q->tot_len = datagram_size;
  744. q->next = lrh->frags;
  745. lrh->frags = NULL;
  746. lrh->reass = NULL;
  747. dequeue_datagram(lrh, lrh_prev);
  748. mem_free(lrh);
  749. /* @todo: distinguish unicast/multicast */
  750. MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
  751. return ip6_input(q, netif);
  752. }
  753. }
  754. /* pbuf enqueued, waiting for more fragments */
  755. return ERR_OK;
  756. } else {
  757. if (b == 0x41) {
  758. /* This is a complete IPv6 packet, just skip dispatch byte. */
  759. pbuf_remove_header(p, 1); /* hide dispatch byte. */
  760. } else if ((b & 0xe0 ) == 0x60) {
  761. /* IPv6 headers are compressed using IPHC. */
  762. p = lowpan6_decompress(p, datagram_size, LWIP_6LOWPAN_CONTEXTS(netif), &src, &dest);
  763. if (p == NULL) {
  764. MIB2_STATS_NETIF_INC(netif, ifindiscards);
  765. return ERR_OK;
  766. }
  767. } else {
  768. goto lowpan6_input_discard;
  769. }
  770. /* @todo: distinguish unicast/multicast */
  771. MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
  772. return ip6_input(p, netif);
  773. }
  774. lowpan6_input_discard:
  775. MIB2_STATS_NETIF_INC(netif, ifindiscards);
  776. pbuf_free(p);
  777. /* always return ERR_OK here to prevent the caller freeing the pbuf */
  778. return ERR_OK;
  779. }
  780. /**
  781. * @ingroup sixlowpan
  782. */
  783. err_t
  784. lowpan6_if_init(struct netif *netif)
  785. {
  786. netif->name[0] = 'L';
  787. netif->name[1] = '6';
  788. netif->output_ip6 = lowpan6_output;
  789. MIB2_INIT_NETIF(netif, snmp_ifType_other, 0);
  790. /* maximum transfer unit */
  791. netif->mtu = 1280;
  792. /* broadcast capability */
  793. netif->flags = NETIF_FLAG_BROADCAST /* | NETIF_FLAG_LOWPAN6 */;
  794. return ERR_OK;
  795. }
  796. /**
  797. * @ingroup sixlowpan
  798. * Set PAN ID
  799. */
  800. err_t
  801. lowpan6_set_pan_id(u16_t pan_id)
  802. {
  803. lowpan6_data.ieee_802154_pan_id = pan_id;
  804. return ERR_OK;
  805. }
  806. #if !NO_SYS
  807. /**
  808. * @ingroup sixlowpan
  809. * Pass a received packet to tcpip_thread for input processing
  810. *
  811. * @param p the received packet, p->payload pointing to the
  812. * IEEE 802.15.4 header.
  813. * @param inp the network interface on which the packet was received
  814. */
  815. err_t
  816. tcpip_6lowpan_input(struct pbuf *p, struct netif *inp)
  817. {
  818. return tcpip_inpkt(p, inp, lowpan6_input);
  819. }
  820. #endif /* !NO_SYS */
  821. #endif /* LWIP_IPV6 */