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