• 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   buffer[ieee_header_len] = 0x41; /* IPv6 dispatch */
412 #endif /* LWIP_6LOWPAN_IPHC */
413 
414   /* Calculate remaining packet length */
415   remaining_len = p->tot_len;
416 
417   if (remaining_len > 0x7FF) {
418     MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
419     /* datagram_size must fit into 11 bit */
420     pbuf_free(p_frag);
421     return ERR_VAL;
422   }
423 
424   /* Fragment, or 1 packet? */
425   max_data_len = LOWPAN6_MAX_PAYLOAD - ieee_header_len - lowpan6_header_len;
426   if (remaining_len > max_data_len) {
427     u16_t data_len;
428     /* We must move the 6LowPAN header to make room for the FRAG header. */
429     memmove(&buffer[ieee_header_len + 4], &buffer[ieee_header_len], lowpan6_header_len);
430 
431     /* Now we need to fragment the packet. FRAG1 header first */
432     buffer[ieee_header_len] = 0xc0 | (((p->tot_len + hidden_header_len) >> 8) & 0x7);
433     buffer[ieee_header_len + 1] = (p->tot_len + hidden_header_len) & 0xff;
434 
435     lowpan6_data.tx_datagram_tag++;
436     buffer[ieee_header_len + 2] = (lowpan6_data.tx_datagram_tag >> 8) & 0xff;
437     buffer[ieee_header_len + 3] = lowpan6_data.tx_datagram_tag & 0xff;
438 
439     /* Fragment follows. */
440     data_len = (max_data_len - 4) & 0xf8;
441     frag_len = data_len + lowpan6_header_len;
442 
443     pbuf_copy_partial(p, buffer + ieee_header_len + lowpan6_header_len + 4, frag_len - lowpan6_header_len, 0);
444     remaining_len -= frag_len - lowpan6_header_len;
445     /* datagram offset holds the offset before compression */
446     datagram_offset = frag_len - lowpan6_header_len + hidden_header_len;
447     LWIP_ASSERT("datagram offset must be a multiple of 8", (datagram_offset & 7) == 0);
448 
449     /* Calculate frame length */
450     p_frag->len = p_frag->tot_len = ieee_header_len + 4 + frag_len + 2; /* add 2 bytes for crc*/
451 
452     /* 2 bytes CRC */
453     crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
454     pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
455 
456     /* send the packet */
457     MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
458     LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
459     err = netif->linkoutput(netif, p_frag);
460 
461     while ((remaining_len > 0) && (err == ERR_OK)) {
462       struct ieee_802154_hdr *hdr = (struct ieee_802154_hdr *)buffer;
463       /* new frame, new seq num for ACK */
464       hdr->sequence_number = lowpan6_data.tx_frame_seq_num++;
465 
466       buffer[ieee_header_len] |= 0x20; /* Change FRAG1 to FRAGN */
467 
468       LWIP_ASSERT("datagram offset must be a multiple of 8", (datagram_offset & 7) == 0);
469       buffer[ieee_header_len + 4] = (u8_t)(datagram_offset >> 3); /* datagram offset in FRAGN header (datagram_offset is max. 11 bit) */
470 
471       frag_len = (127 - ieee_header_len - 5 - 2) & 0xf8;
472       if (frag_len > remaining_len) {
473         frag_len = remaining_len;
474       }
475 
476       pbuf_copy_partial(p, buffer + ieee_header_len + 5, frag_len, p->tot_len - remaining_len);
477       remaining_len -= frag_len;
478       datagram_offset += frag_len;
479 
480       /* Calculate frame length */
481       p_frag->len = p_frag->tot_len = frag_len + 5 + ieee_header_len + 2;
482 
483       /* 2 bytes CRC */
484       crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
485       pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
486 
487       /* send the packet */
488       MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
489       LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
490       err = netif->linkoutput(netif, p_frag);
491     }
492   } else {
493     /* It fits in one frame. */
494     frag_len = remaining_len;
495 
496     /* Copy IPv6 packet */
497     pbuf_copy_partial(p, buffer + ieee_header_len + lowpan6_header_len, frag_len, 0);
498     remaining_len = 0;
499 
500     /* Calculate frame length */
501     p_frag->len = p_frag->tot_len = frag_len + lowpan6_header_len + ieee_header_len + 2;
502     LWIP_ASSERT("", p_frag->len <= 127);
503 
504     /* 2 bytes CRC */
505     crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
506     pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
507 
508     /* send the packet */
509     MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
510     LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
511     err = netif->linkoutput(netif, p_frag);
512   }
513 
514   pbuf_free(p_frag);
515 
516   return err;
517 }
518 
519 /**
520  * @ingroup sixlowpan
521  * Set context
522  */
523 err_t
lowpan6_set_context(u8_t idx,const ip6_addr_t * context)524 lowpan6_set_context(u8_t idx, const ip6_addr_t *context)
525 {
526 #if LWIP_6LOWPAN_NUM_CONTEXTS > 0
527   if (idx >= LWIP_6LOWPAN_NUM_CONTEXTS) {
528     return ERR_ARG;
529   }
530 
531   IP6_ADDR_ZONECHECK(context);
532 
533   ip6_addr_set(&lowpan6_data.lowpan6_context[idx], context);
534 
535   return ERR_OK;
536 #else
537   LWIP_UNUSED_ARG(idx);
538   LWIP_UNUSED_ARG(context);
539   return ERR_ARG;
540 #endif
541 }
542 
543 #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
544 /**
545  * @ingroup sixlowpan
546  * Set short address
547  */
548 err_t
lowpan6_set_short_addr(u8_t addr_high,u8_t addr_low)549 lowpan6_set_short_addr(u8_t addr_high, u8_t addr_low)
550 {
551   short_mac_addr.addr[0] = addr_high;
552   short_mac_addr.addr[1] = addr_low;
553 
554   return ERR_OK;
555 }
556 #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
557 
558 /* Create IEEE 802.15.4 address from netif address */
559 static err_t
lowpan6_hwaddr_to_addr(struct netif * netif,struct lowpan6_link_addr * addr)560 lowpan6_hwaddr_to_addr(struct netif *netif, struct lowpan6_link_addr *addr)
561 {
562   addr->addr_len = 8;
563   if (netif->hwaddr_len == 8) {
564     LWIP_ERROR("NETIF_MAX_HWADDR_LEN >= 8 required", sizeof(netif->hwaddr) >= 8, return ERR_VAL;);
565     SMEMCPY(addr->addr, netif->hwaddr, 8);
566   } else if (netif->hwaddr_len == 6) {
567     /* Copy from MAC-48 */
568     SMEMCPY(addr->addr, netif->hwaddr, 3);
569     addr->addr[3] = addr->addr[4] = 0xff;
570     SMEMCPY(&addr->addr[5], &netif->hwaddr[3], 3);
571   } else {
572     /* Invalid address length, don't know how to convert this */
573     return ERR_VAL;
574   }
575   return ERR_OK;
576 }
577 
578 /**
579  * @ingroup sixlowpan
580  * Resolve and fill-in IEEE 802.15.4 address header for outgoing IPv6 packet.
581  *
582  * Perform Header Compression and fragment if necessary.
583  *
584  * @param netif The lwIP network interface which the IP packet will be sent on.
585  * @param q The pbuf(s) containing the IP packet to be sent.
586  * @param ip6addr The IP address of the packet destination.
587  *
588  * @return err_t
589  */
590 err_t
lowpan6_output(struct netif * netif,struct pbuf * q,const ip6_addr_t * ip6addr)591 lowpan6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr)
592 {
593   err_t result;
594   const u8_t *hwaddr;
595   struct lowpan6_link_addr src, dest;
596 #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
597   ip6_addr_t ip6_src;
598   struct ip6_hdr *ip6_hdr;
599 #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
600 
601 #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
602   /* Check if we can compress source address (use aligned copy) */
603   ip6_hdr = (struct ip6_hdr *)q->payload;
604   ip6_addr_copy_from_packed(ip6_src, ip6_hdr->src);
605   ip6_addr_assign_zone(&ip6_src, IP6_UNICAST, netif);
606   if (lowpan6_get_address_mode(&ip6_src, &short_mac_addr) == 3) {
607     src.addr_len = 2;
608     src.addr[0] = short_mac_addr.addr[0];
609     src.addr[1] = short_mac_addr.addr[1];
610   } else
611 #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
612   {
613     result = lowpan6_hwaddr_to_addr(netif, &src);
614     if (result != ERR_OK) {
615       MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
616       return result;
617     }
618   }
619 
620   /* multicast destination IP address? */
621   if (ip6_addr_ismulticast(ip6addr)) {
622     MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
623     /* We need to send to the broadcast address.*/
624     return lowpan6_frag(netif, q, &src, &ieee_802154_broadcast);
625   }
626 
627   /* We have a unicast destination IP address */
628   /* @todo anycast? */
629 
630 #if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
631   if (src.addr_len == 2) {
632     /* If source address was compressable to short_mac_addr, and dest has same subnet and
633      * is also compressable to 2-bytes, assume we can infer dest as a short address too. */
634     dest.addr_len = 2;
635     dest.addr[0] = ((u8_t *)q->payload)[38];
636     dest.addr[1] = ((u8_t *)q->payload)[39];
637     if ((src.addr_len == 2) && (ip6_addr_netcmp_zoneless(&ip6_hdr->src, &ip6_hdr->dest)) &&
638         (lowpan6_get_address_mode(ip6addr, &dest) == 3)) {
639       MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
640       return lowpan6_frag(netif, q, &src, &dest);
641     }
642   }
643 #endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
644 
645   /* Ask ND6 what to do with the packet. */
646   result = nd6_get_next_hop_addr_or_queue(netif, q, ip6addr, &hwaddr);
647   if (result != ERR_OK) {
648     MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
649     return result;
650   }
651 
652   /* If no hardware address is returned, nd6 has queued the packet for later. */
653   if (hwaddr == NULL) {
654     return ERR_OK;
655   }
656 
657   /* Send out the packet using the returned hardware address. */
658   dest.addr_len = netif->hwaddr_len;
659   /* XXX: Inferring the length of the source address from the destination address
660    * is not correct for IEEE 802.15.4, but currently we don't get this information
661    * from the neighbor cache */
662   SMEMCPY(dest.addr, hwaddr, netif->hwaddr_len);
663   MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
664   return lowpan6_frag(netif, q, &src, &dest);
665 }
666 /**
667  * @ingroup sixlowpan
668  * NETIF input function: don't free the input pbuf when returning != ERR_OK!
669  */
670 err_t
lowpan6_input(struct pbuf * p,struct netif * netif)671 lowpan6_input(struct pbuf *p, struct netif *netif)
672 {
673   u8_t *puc, b;
674   s8_t i;
675   struct lowpan6_link_addr src, dest;
676   u16_t datagram_size = 0;
677   u16_t datagram_offset, datagram_tag;
678   struct lowpan6_reass_helper *lrh, *lrh_next, *lrh_prev = NULL;
679 
680   if (p == NULL) {
681     return ERR_OK;
682   }
683 
684   MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
685 
686   if (p->len != p->tot_len) {
687     /* for now, this needs a pbuf in one piece */
688     goto lowpan6_input_discard;
689   }
690 
691   if (lowpan6_parse_iee802154_header(p, &src, &dest) != ERR_OK) {
692     goto lowpan6_input_discard;
693   }
694 
695   /* Check dispatch. */
696   puc = (u8_t *)p->payload;
697 
698   b = *puc;
699   if ((b & 0xf8) == 0xc0) {
700     /* FRAG1 dispatch. add this packet to reassembly list. */
701     datagram_size = ((u16_t)(puc[0] & 0x07) << 8) | (u16_t)puc[1];
702     datagram_tag = ((u16_t)puc[2] << 8) | (u16_t)puc[3];
703 
704     /* check for duplicate */
705     lrh = lowpan6_data.reass_list;
706     while (lrh != NULL) {
707       uint8_t discard = 0;
708       lrh_next = lrh->next_packet;
709       if ((lrh->sender_addr.addr_len == src.addr_len) &&
710           (memcmp(lrh->sender_addr.addr, src.addr, src.addr_len) == 0)) {
711         /* address match with packet in reassembly. */
712         if ((datagram_tag == lrh->datagram_tag) && (datagram_size == lrh->datagram_size)) {
713           /* duplicate fragment. */
714           goto lowpan6_input_discard;
715         } else {
716           /* We are receiving the start of a new datagram. Discard old one (incomplete). */
717           discard = 1;
718         }
719       }
720       if (discard) {
721         dequeue_datagram(lrh, lrh_prev);
722         free_reass_datagram(lrh);
723       } else {
724         lrh_prev = lrh;
725       }
726       /* Check next datagram in queue. */
727       lrh = lrh_next;
728     }
729 
730     pbuf_remove_header(p, 4); /* hide frag1 dispatch */
731 
732     lrh = (struct lowpan6_reass_helper *) mem_malloc(sizeof(struct lowpan6_reass_helper));
733     if (lrh == NULL) {
734       goto lowpan6_input_discard;
735     }
736 
737     lrh->sender_addr.addr_len = src.addr_len;
738     for (i = 0; i < src.addr_len; i++) {
739       lrh->sender_addr.addr[i] = src.addr[i];
740     }
741     lrh->datagram_size = datagram_size;
742     lrh->datagram_tag = datagram_tag;
743     lrh->frags = NULL;
744     if (*(u8_t *)p->payload == 0x41) {
745       /* This is a complete IPv6 packet, just skip dispatch byte. */
746       pbuf_remove_header(p, 1); /* hide dispatch byte. */
747       lrh->reass = p;
748     } else if ((*(u8_t *)p->payload & 0xe0 ) == 0x60) {
749       lrh->reass = lowpan6_decompress(p, datagram_size, LWIP_6LOWPAN_CONTEXTS(netif), &src, &dest);
750       if (lrh->reass == NULL) {
751         /* decompression failed */
752         mem_free(lrh);
753         goto lowpan6_input_discard;
754       }
755     }
756     /* TODO: handle the case where we already have FRAGN received */
757     lrh->next_packet = lowpan6_data.reass_list;
758     lrh->timer = 2;
759     lowpan6_data.reass_list = lrh;
760 
761     return ERR_OK;
762   } else if ((b & 0xf8) == 0xe0) {
763     /* FRAGN dispatch, find packet being reassembled. */
764     datagram_size = ((u16_t)(puc[0] & 0x07) << 8) | (u16_t)puc[1];
765     datagram_tag = ((u16_t)puc[2] << 8) | (u16_t)puc[3];
766     datagram_offset = (u16_t)puc[4] << 3;
767     pbuf_remove_header(p, 4); /* hide frag1 dispatch but keep datagram offset for reassembly */
768 
769     for (lrh = lowpan6_data.reass_list; lrh != NULL; lrh_prev = lrh, lrh = lrh->next_packet) {
770       if ((lrh->sender_addr.addr_len == src.addr_len) &&
771           (memcmp(lrh->sender_addr.addr, src.addr, src.addr_len) == 0) &&
772           (datagram_tag == lrh->datagram_tag) &&
773           (datagram_size == lrh->datagram_size)) {
774         break;
775       }
776     }
777     if (lrh == NULL) {
778       /* rogue fragment */
779       goto lowpan6_input_discard;
780     }
781     /* Insert new pbuf into list of fragments. Each fragment is a pbuf,
782        this only works for unchained pbufs. */
783     LWIP_ASSERT("p->next == NULL", p->next == NULL);
784     if (lrh->reass != NULL) {
785       /* FRAG1 already received, check this offset against first len */
786       if (datagram_offset < lrh->reass->len) {
787         /* fragment overlap, discard old fragments */
788         dequeue_datagram(lrh, lrh_prev);
789         free_reass_datagram(lrh);
790         goto lowpan6_input_discard;
791       }
792     }
793     if (lrh->frags == NULL) {
794       /* first FRAGN */
795       lrh->frags = p;
796     } else {
797       /* find the correct place to insert */
798       struct pbuf *q, *last;
799       u16_t new_frag_len = p->len - 1; /* p->len includes datagram_offset byte */
800       for (q = lrh->frags, last = NULL; q != NULL; last = q, q = q->next) {
801         u16_t q_datagram_offset = ((u8_t *)q->payload)[0] << 3;
802         u16_t q_frag_len = q->len - 1;
803         if (datagram_offset < q_datagram_offset) {
804           if (datagram_offset + new_frag_len > q_datagram_offset) {
805             /* overlap, discard old fragments */
806             dequeue_datagram(lrh, lrh_prev);
807             free_reass_datagram(lrh);
808             goto lowpan6_input_discard;
809           }
810           /* insert here */
811           break;
812         } else if (datagram_offset == q_datagram_offset) {
813           if (q_frag_len != new_frag_len) {
814             /* fragment mismatch, discard old fragments */
815             dequeue_datagram(lrh, lrh_prev);
816             free_reass_datagram(lrh);
817             goto lowpan6_input_discard;
818           }
819           /* duplicate, ignore */
820           pbuf_free(p);
821           return ERR_OK;
822         }
823       }
824       /* insert fragment */
825       if (last == NULL) {
826         lrh->frags = p;
827       } else {
828         last->next = p;
829         p->next = q;
830       }
831     }
832     /* check if all fragments were received */
833     if (lrh->reass) {
834       u16_t offset = lrh->reass->len;
835       struct pbuf *q;
836       for (q = lrh->frags; q != NULL; q = q->next) {
837         u16_t q_datagram_offset = ((u8_t *)q->payload)[0] << 3;
838         if (q_datagram_offset != offset) {
839           /* not complete, wait for more fragments */
840           return ERR_OK;
841         }
842         offset += q->len - 1;
843       }
844       if (offset == datagram_size) {
845         /* all fragments received, combine pbufs */
846         u16_t datagram_left = datagram_size - lrh->reass->len;
847         for (q = lrh->frags; q != NULL; q = q->next) {
848           /* hide datagram_offset byte now */
849           pbuf_remove_header(q, 1);
850           q->tot_len = datagram_left;
851           datagram_left -= q->len;
852         }
853         LWIP_ASSERT("datagram_left == 0", datagram_left == 0);
854         q = lrh->reass;
855         q->tot_len = datagram_size;
856         q->next = lrh->frags;
857         lrh->frags = NULL;
858         lrh->reass = NULL;
859         dequeue_datagram(lrh, lrh_prev);
860         mem_free(lrh);
861 
862         /* @todo: distinguish unicast/multicast */
863         MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
864         return ip6_input(q, netif);
865       }
866     }
867     /* pbuf enqueued, waiting for more fragments */
868     return ERR_OK;
869   } else {
870     if (b == 0x41) {
871       /* This is a complete IPv6 packet, just skip dispatch byte. */
872       pbuf_remove_header(p, 1); /* hide dispatch byte. */
873     } else if ((b & 0xe0 ) == 0x60) {
874       /* IPv6 headers are compressed using IPHC. */
875       p = lowpan6_decompress(p, datagram_size, LWIP_6LOWPAN_CONTEXTS(netif), &src, &dest);
876       if (p == NULL) {
877         MIB2_STATS_NETIF_INC(netif, ifindiscards);
878         return ERR_OK;
879       }
880     } else {
881       goto lowpan6_input_discard;
882     }
883 
884     /* @todo: distinguish unicast/multicast */
885     MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
886 
887     return ip6_input(p, netif);
888   }
889 lowpan6_input_discard:
890   MIB2_STATS_NETIF_INC(netif, ifindiscards);
891   pbuf_free(p);
892   /* always return ERR_OK here to prevent the caller freeing the pbuf */
893   return ERR_OK;
894 }
895 
896 /**
897  * @ingroup sixlowpan
898  */
899 err_t
lowpan6_if_init(struct netif * netif)900 lowpan6_if_init(struct netif *netif)
901 {
902   netif->name[0] = 'L';
903   netif->name[1] = '6';
904   netif->output_ip6 = lowpan6_output;
905 
906   MIB2_INIT_NETIF(netif, snmp_ifType_other, 0);
907 
908   /* maximum transfer unit */
909   netif->mtu = IP6_MIN_MTU_LENGTH;
910 
911   /* broadcast capability */
912   netif->flags = NETIF_FLAG_BROADCAST /* | NETIF_FLAG_LOWPAN6 */;
913 
914   return ERR_OK;
915 }
916 
917 /**
918  * @ingroup sixlowpan
919  * Set PAN ID
920  */
921 err_t
lowpan6_set_pan_id(u16_t pan_id)922 lowpan6_set_pan_id(u16_t pan_id)
923 {
924   lowpan6_data.ieee_802154_pan_id = pan_id;
925 
926   return ERR_OK;
927 }
928 
929 #if !NO_SYS
930 /**
931  * @ingroup sixlowpan
932  * Pass a received packet to tcpip_thread for input processing
933  *
934  * @param p the received packet, p->payload pointing to the
935  *          IEEE 802.15.4 header.
936  * @param inp the network interface on which the packet was received
937  */
938 err_t
tcpip_6lowpan_input(struct pbuf * p,struct netif * inp)939 tcpip_6lowpan_input(struct pbuf *p, struct netif *inp)
940 {
941   return tcpip_inpkt(p, inp, lowpan6_input);
942 }
943 #endif /* !NO_SYS */
944 
945 #endif /* LWIP_IPV6 */
946