Contiki-NG
sicslowpan.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * 6lowpan implementation (RFC4944 and draft-ietf-6lowpan-hc-06)
36  *
37  * \author Adam Dunkels <adam@sics.se>
38  * \author Nicolas Tsiftes <nvt@sics.se>
39  * \author Niclas Finne <nfi@sics.se>
40  * \author Mathilde Durvy <mdurvy@cisco.com>
41  * \author Julien Abeille <jabeille@cisco.com>
42  * \author Joakim Eriksson <joakime@sics.se>
43  * \author Joel Hoglund <joel@sics.se>
44  */
45 
46 /**
47  * \addtogroup sicslowpan
48  * \ingroup uip
49  * @{
50  */
51 
52 /**
53  * FOR HC-06 COMPLIANCE TODO:
54  * -Add compression options to UDP, currently only supports
55  * both ports compressed or both ports elided
56  *
57  * -Verify TC/FL compression works
58  *
59  * -Add stateless multicast option
60  */
61 
62 #include <string.h>
63 
64 #include "contiki.h"
65 #include "dev/watchdog.h"
66 #include "net/link-stats.h"
67 #include "net/ipv6/uipopt.h"
68 #include "net/ipv6/tcpip.h"
69 #include "net/ipv6/uip.h"
70 #include "net/ipv6/uip-ds6.h"
71 #include "net/ipv6/uipbuf.h"
72 #include "net/ipv6/sicslowpan.h"
73 #include "net/netstack.h"
74 #include "net/packetbuf.h"
75 #include "net/queuebuf.h"
76 
77 #include "net/routing/routing.h"
78 
79 /* Log configuration */
80 #include "sys/log.h"
81 #define LOG_MODULE "6LoWPAN"
82 #define LOG_LEVEL LOG_LEVEL_6LOWPAN
83 
84 #define GET16(ptr,index) (((uint16_t)((ptr)[index] << 8)) | ((ptr)[(index) + 1]))
85 #define SET16(ptr,index,value) do { \
86  (ptr)[index] = ((value) >> 8) & 0xff; \
87  (ptr)[index + 1] = (value) & 0xff; \
88 } while(0)
89 
90 /** \name Pointers in the packetbuf buffer
91  * @{
92  */
93 #define PACKETBUF_FRAG_PTR (packetbuf_ptr)
94 #define PACKETBUF_FRAG_DISPATCH_SIZE 0 /* 16 bit */
95 #define PACKETBUF_FRAG_TAG 2 /* 16 bit */
96 #define PACKETBUF_FRAG_OFFSET 4 /* 8 bit */
97 
98 /* define the buffer as a byte array */
99 #define PACKETBUF_IPHC_BUF ((uint8_t *)(packetbuf_ptr + packetbuf_hdr_len))
100 #define PACKETBUF_PAYLOAD_END ((uint8_t *)(packetbuf_ptr + mac_max_payload))
101 
102 #define PACKETBUF_6LO_PTR (packetbuf_ptr + packetbuf_hdr_len)
103 #define PACKETBUF_6LO_DISPATCH 0 /* 8 bit */
104 #define PACKETBUF_6LO_ENCODING 1 /* 8 bit */
105 #define PACKETBUF_6LO_TTL 2 /* 8 bit */
106 
107 #define PACKETBUF_6LO_HC_UDP_PTR (packetbuf_ptr + packetbuf_hdr_len)
108 #define PACKETBUF_6LO_HC_UDP_DISPATCH 0 /* 8 bit */
109 #define PACKETBUF_6LO_HC_UDP_HC1_ENCODING 1 /* 8 bit */
110 #define PACKETBUF_6LO_HC_UDP_UDP_ENCODING 2 /* 8 bit */
111 #define PACKETBUF_6LO_HC_UDP_TTL 3 /* 8 bit */
112 #define PACKETBUF_6LO_HC_UDP_PORTS 4 /* 8 bit */
113 #define PACKETBUF_6LO_HC_UDP_CHKSUM 5 /* 16 bit */
114 
115 /** \name Pointers in the sicslowpan and uip buffer
116  * @{
117  */
118 
119 /* NOTE: In the multiple-reassembly context there is only room for the header / first fragment */
120 #define SICSLOWPAN_IP_BUF(buf) ((struct uip_ip_hdr *)buf)
121 #define SICSLOWPAN_UDP_BUF(buf) ((struct uip_udp_hdr *)&buf[UIP_IPH_LEN])
122 #define SICSLOWPAN_IPPAYLOAD_BUF(buf) (&buf[UIP_IPH_LEN])
123 
124 #define UIP_IPPAYLOAD_BUF_POS(pos) (&uip_buf[UIP_IPH_LEN + (pos)])
125 #define UIP_UDP_BUF_POS(pos) ((struct uip_udp_hdr *)UIP_IPPAYLOAD_BUF_POS(pos))
126 #define UIP_EXT_HDR_LEN 2
127 
128 /** @} */
129 
130 /* set this to zero if not compressing EXT_HDR - for backwards compatibility */
131 #ifdef SICSLOWPAN_CONF_COMPRESS_EXT_HDR
132 #define COMPRESS_EXT_HDR SICSLOWPAN_CONF_COMPRESS_EXT_HDR
133 #else
134 /* Compressing on by default - turn off to be compatible with older versions */
135 #define COMPRESS_EXT_HDR 1
136 #endif
137 
138 #if COMPRESS_EXT_HDR
139 #define IS_COMPRESSABLE_PROTO(x) (x == UIP_PROTO_UDP \
140  || x == UIP_PROTO_HBHO \
141  || x == UIP_PROTO_DESTO \
142  || x == UIP_PROTO_ROUTING \
143  || x == UIP_PROTO_FRAG)
144 #else
145 #define IS_COMPRESSABLE_PROTO(x) (x == UIP_PROTO_UDP)
146 #endif /* COMPRESS_EXT_HDR */
147 
148 /** \name General variables
149  * @{
150  */
151 
152 /**
153  * A pointer to the packetbuf buffer.
154  * We initialize it to the beginning of the packetbuf buffer, then
155  * access different fields by updating the offset packetbuf_hdr_len.
156  */
157 static uint8_t *packetbuf_ptr;
158 
159 /**
160  * packetbuf_hdr_len is the total length of (the processed) 6lowpan headers
161  * (fragment headers, IPV6 or HC1, HC2, and HC1 and HC2 non compressed
162  * fields).
163  */
164 static uint8_t packetbuf_hdr_len;
165 
166 /**
167  * The length of the payload in the Packetbuf buffer.
168  * The payload is what comes after the compressed or uncompressed
169  * headers (can be the IP payload if the IP header only is compressed
170  * or the UDP payload if the UDP header is also compressed)
171  */
173 
174 /**
175  * uncomp_hdr_len is the length of the headers before compression (if HC2
176  * is used this includes the UDP header in addition to the IP header).
177  */
178 static uint8_t uncomp_hdr_len;
179 
180 /**
181  * mac_max_payload is the maimum payload space on the MAC frame.
182  */
183 static int mac_max_payload;
184 
185 /**
186  * The current page (RFC 4944)
187  */
188 static uint8_t curr_page;
189 
190 /**
191  * the result of the last transmitted fragment
192  */
193 static int last_tx_status;
194 /** @} */
195 
196 
197 static int last_rssi;
198 
199 /* ----------------------------------------------------------------- */
200 /* Support for reassembling multiple packets */
201 /* ----------------------------------------------------------------- */
202 
203 #if SICSLOWPAN_CONF_FRAG
204 static uint16_t my_tag;
205 
206 /** The total length of the IPv6 packet in the sicslowpan_buf. */
207 
208 /* This needs to be defined in NBR / Nodes depending on available RAM */
209 /* and expected reassembly requirements */
210 #ifdef SICSLOWPAN_CONF_FRAGMENT_BUFFERS
211 #define SICSLOWPAN_FRAGMENT_BUFFERS SICSLOWPAN_CONF_FRAGMENT_BUFFERS
212 #else
213 #define SICSLOWPAN_FRAGMENT_BUFFERS 12
214 #endif
215 
216 /* REASS_CONTEXTS corresponds to the number of simultaneous
217  * reassemblies that can be made. NOTE: the first buffer for each
218  * reassembly is stored in the context since it can be larger than the
219  * rest of the fragments due to header compression.
220  **/
221 #ifdef SICSLOWPAN_CONF_REASS_CONTEXTS
222 #define SICSLOWPAN_REASS_CONTEXTS SICSLOWPAN_CONF_REASS_CONTEXTS
223 #else
224 #define SICSLOWPAN_REASS_CONTEXTS 2
225 #endif
226 
227 /* The size of each fragment (IP payload) for the 6lowpan fragmentation */
228 #ifdef SICSLOWPAN_CONF_FRAGMENT_SIZE
229 #define SICSLOWPAN_FRAGMENT_SIZE SICSLOWPAN_CONF_FRAGMENT_SIZE
230 #else
231 /* The default fragment size (110 bytes for 127-2 bytes frames) */
232 #define SICSLOWPAN_FRAGMENT_SIZE (127 - 2 - 15)
233 #endif
234 
235 /* Check the selected fragment size, since we use 8-bit integers to handle it. */
236 #if SICSLOWPAN_FRAGMENT_SIZE > 255
237 #error Too large SICSLOWPAN_FRAGMENT_SIZE set.
238 #endif
239 
240 /* Assuming that the worst growth for uncompression is 38 bytes */
241 #define SICSLOWPAN_FIRST_FRAGMENT_SIZE (SICSLOWPAN_FRAGMENT_SIZE + 38)
242 
243 /* all information needed for reassembly */
244 struct sicslowpan_frag_info {
245  /** When reassembling, the source address of the fragments being merged */
246  linkaddr_t sender;
247  /** The destination address of the fragments being merged */
248  linkaddr_t receiver;
249  /** When reassembling, the tag in the fragments being merged. */
250  uint16_t tag;
251  /** Total length of the fragmented packet */
252  uint16_t len;
253  /** Current length of reassembled fragments */
254  uint16_t reassembled_len;
255  /** Reassembly %process %timer. */
256  struct timer reass_timer;
257 
258  /** Fragment size of first fragment */
259  uint16_t first_frag_len;
260  /** First fragment - needs a larger buffer since the size is uncompressed size
261  and we need to know total size to know when we have received last fragment. */
262  uint8_t first_frag[SICSLOWPAN_FIRST_FRAGMENT_SIZE];
263 };
264 
265 static struct sicslowpan_frag_info frag_info[SICSLOWPAN_REASS_CONTEXTS];
266 
267 struct sicslowpan_frag_buf {
268  /* the index of the frag_info */
269  uint8_t index;
270  /* Fragment offset */
271  uint8_t offset;
272  /* Length of this fragment (if zero this buffer is not allocated) */
273  uint8_t len;
274  uint8_t data[SICSLOWPAN_FRAGMENT_SIZE];
275 };
276 
277 static struct sicslowpan_frag_buf frag_buf[SICSLOWPAN_FRAGMENT_BUFFERS];
278 
279 /*---------------------------------------------------------------------------*/
280 static int
281 clear_fragments(uint8_t frag_info_index)
282 {
283  int i, clear_count;
284  clear_count = 0;
285  frag_info[frag_info_index].len = 0;
286  for(i = 0; i < SICSLOWPAN_FRAGMENT_BUFFERS; i++) {
287  if(frag_buf[i].len > 0 && frag_buf[i].index == frag_info_index) {
288  /* deallocate the buffer */
289  frag_buf[i].len = 0;
290  clear_count++;
291  }
292  }
293  return clear_count;
294 }
295 /*---------------------------------------------------------------------------*/
296 static int
297 timeout_fragments(int not_context)
298 {
299  int i;
300  int count = 0;
301  for(i = 0; i < SICSLOWPAN_REASS_CONTEXTS; i++) {
302  if(frag_info[i].len > 0 && i != not_context &&
303  timer_expired(&frag_info[i].reass_timer)) {
304  /* This context can be freed */
305  count += clear_fragments(i);
306  }
307  }
308  return count;
309 }
310 /*---------------------------------------------------------------------------*/
311 static int
312 store_fragment(uint8_t index, uint8_t offset)
313 {
314  int i;
315  int len;
316 
318 
319  if(len <= 0 || len > SICSLOWPAN_FRAGMENT_SIZE) {
320  /* Unacceptable fragment size. */
321  return -1;
322  }
323 
324  for(i = 0; i < SICSLOWPAN_FRAGMENT_BUFFERS; i++) {
325  if(frag_buf[i].len == 0) {
326  /* copy over the data from packetbuf into the fragment buffer,
327  and store offset and len */
328  frag_buf[i].offset = offset; /* frag offset */
329  frag_buf[i].len = len;
330  frag_buf[i].index = index;
331  memcpy(frag_buf[i].data, packetbuf_ptr + packetbuf_hdr_len, len);
332  /* return the length of the stored fragment */
333  return len;
334  }
335  }
336  /* failed */
337  return -1;
338 }
339 /*---------------------------------------------------------------------------*/
340 /* add a new fragment to the buffer */
341 static int8_t
342 add_fragment(uint16_t tag, uint16_t frag_size, uint8_t offset)
343 {
344  int i;
345  int len;
346  int8_t found = -1;
347 
348  if(offset == 0) {
349  /* This is a first fragment - check if we can add this */
350  for(i = 0; i < SICSLOWPAN_REASS_CONTEXTS; i++) {
351  /* clear all fragment info with expired timer to free all fragment buffers */
352  if(frag_info[i].len > 0 && timer_expired(&frag_info[i].reass_timer)) {
353  clear_fragments(i);
354  }
355 
356  /* We use len as indication on used or not used */
357  if(found < 0 && frag_info[i].len == 0) {
358  /* We remember the first free fragment info but must continue
359  the loop to free any other expired fragment buffers. */
360  found = i;
361  }
362  }
363 
364  if(found < 0) {
365  LOG_WARN("reassembly: failed to store new fragment session - tag: %d\n", tag);
366  return -1;
367  }
368 
369  /* Found a free fragment info to store data in */
370  frag_info[found].len = frag_size;
371  frag_info[found].tag = tag;
372  linkaddr_copy(&frag_info[found].sender,
373  packetbuf_addr(PACKETBUF_ADDR_SENDER));
374  timer_set(&frag_info[found].reass_timer, SICSLOWPAN_REASS_MAXAGE * CLOCK_SECOND / 16);
375  /* first fragment can not be stored immediately but is moved into
376  the buffer while uncompressing */
377  return found;
378  }
379 
380  /* This is a N-fragment - should find the info */
381  for(i = 0; i < SICSLOWPAN_REASS_CONTEXTS; i++) {
382  if(frag_info[i].tag == tag && frag_info[i].len > 0 &&
383  linkaddr_cmp(&frag_info[i].sender, packetbuf_addr(PACKETBUF_ADDR_SENDER))) {
384  /* Tag and Sender match - this must be the correct info to store in */
385  found = i;
386  break;
387  }
388  }
389 
390  if(found < 0) {
391  /* no entry found for storing the new fragment */
392  LOG_WARN("reassembly: failed to store N-fragment - could not find session - tag: %d offset: %d\n", tag, offset);
393  return -1;
394  }
395 
396  /* i is the index of the reassembly context */
397  len = store_fragment(i, offset);
398  if(len < 0 && timeout_fragments(i) > 0) {
399  len = store_fragment(i, offset);
400  }
401  if(len > 0) {
402  frag_info[i].reassembled_len += len;
403  return i;
404  } else {
405  /* should we also clear all fragments since we failed to store
406  this fragment? */
407  LOG_WARN("reassembly: failed to store fragment - packet reassembly will fail tag:%d l\n", frag_info[i].tag);
408  return -1;
409  }
410 }
411 /*---------------------------------------------------------------------------*/
412 /* Copy all the fragments that are associated with a specific context
413  into uip */
414 static bool
415 copy_frags2uip(int context)
416 {
417  int i;
418 
419  /* Check length fields before proceeding. */
420  if(frag_info[context].len < frag_info[context].first_frag_len ||
421  frag_info[context].len > sizeof(uip_buf)) {
422  LOG_WARN("input: invalid total size of fragments\n");
423  clear_fragments(context);
424  return false;
425  }
426 
427  /* Copy from the fragment context info buffer first */
428  memcpy((uint8_t *)UIP_IP_BUF, (uint8_t *)frag_info[context].first_frag,
429  frag_info[context].first_frag_len);
430 
431  /* Ensure that no previous data is used for reassembly in case of missing fragments. */
432  memset((uint8_t *)UIP_IP_BUF + frag_info[context].first_frag_len, 0,
433  frag_info[context].len - frag_info[context].first_frag_len);
434 
435  for(i = 0; i < SICSLOWPAN_FRAGMENT_BUFFERS; i++) {
436  /* And also copy all matching fragments */
437  if(frag_buf[i].len > 0 && frag_buf[i].index == context) {
438  if((frag_buf[i].offset << 3) + frag_buf[i].len > sizeof(uip_buf)) {
439  LOG_WARN("input: invalid fragment offset\n");
440  clear_fragments(context);
441  return false;
442  }
443  memcpy((uint8_t *)UIP_IP_BUF + (uint16_t)(frag_buf[i].offset << 3),
444  (uint8_t *)frag_buf[i].data, frag_buf[i].len);
445  }
446  }
447  /* deallocate all the fragments for this context */
448  clear_fragments(context);
449 
450  return true;
451 }
452 #endif /* SICSLOWPAN_CONF_FRAG */
453 
454 /* -------------------------------------------------------------------------- */
455 
456 /*-------------------------------------------------------------------------*/
457 /* Basic netstack sniffer */
458 /*-------------------------------------------------------------------------*/
459 static struct netstack_sniffer *callback = NULL;
460 
461 void
462 netstack_sniffer_add(struct netstack_sniffer *s)
463 {
464  callback = s;
465 }
466 
467 void
468 netstack_sniffer_remove(struct netstack_sniffer *s)
469 {
470  callback = NULL;
471 }
472 
473 static void
474 set_packet_attrs(void)
475 {
476  int c = 0;
477  /* set protocol in NETWORK_ID */
478  packetbuf_set_attr(PACKETBUF_ATTR_NETWORK_ID, UIP_IP_BUF->proto);
479 
480  /* assign values to the channel attribute (port or type + code) */
481  if(UIP_IP_BUF->proto == UIP_PROTO_UDP) {
482  c = UIP_UDP_BUF_POS(0)->srcport;
483  if(UIP_UDP_BUF_POS(0)->destport < c) {
484  c = UIP_UDP_BUF_POS(0)->destport;
485  }
486  } else if(UIP_IP_BUF->proto == UIP_PROTO_TCP) {
487  c = UIP_TCP_BUF->srcport;
488  if(UIP_TCP_BUF->destport < c) {
489  c = UIP_TCP_BUF->destport;
490  }
491  } else if(UIP_IP_BUF->proto == UIP_PROTO_ICMP6) {
492  c = UIP_ICMP_BUF->type << 8 | UIP_ICMP_BUF->icode;
493  }
494 
495  packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c);
496 
497 /* if(uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr)) { */
498 /* own = 1; */
499 /* } */
500 
501 }
502 
503 
504 
505 #if SICSLOWPAN_COMPRESSION >= SICSLOWPAN_COMPRESSION_IPHC
506 /** \name variables specific to HC06 and more recent versions
507  * @{
508  */
509 
510 /** Addresses contexts for IPHC. */
511 #if SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0
512 static struct sicslowpan_addr_context
513 addr_contexts[SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS];
514 #endif
515 
516 /** pointer to an address context. */
518 
519 /** pointer to the byte where to write next inline field. */
520 static uint8_t *hc06_ptr;
521 
522 /* Uncompression of linklocal */
523 /* 0 -> 16 bytes from packet */
524 /* 1 -> 2 bytes from prefix - bunch of zeroes and 8 from packet */
525 /* 2 -> 2 bytes from prefix - 0000::00ff:fe00:XXXX from packet */
526 /* 3 -> 2 bytes from prefix - infer 8 bytes from lladdr */
527 /* NOTE: => the uncompress function does change 0xf to 0x10 */
528 /* NOTE: 0x00 => no-autoconfig => unspecified */
529 const uint8_t unc_llconf[] = {0x0f,0x28,0x22,0x20};
530 
531 /* Uncompression of ctx-based */
532 /* 0 -> 0 bits from packet [unspecified / reserved] */
533 /* 1 -> 8 bytes from prefix - bunch of zeroes and 8 from packet */
534 /* 2 -> 8 bytes from prefix - 0000::00ff:fe00:XXXX + 2 from packet */
535 /* 3 -> 8 bytes from prefix - infer 8 bytes from lladdr */
536 const uint8_t unc_ctxconf[] = {0x00,0x88,0x82,0x80};
537 
538 /* Uncompression of ctx-based */
539 /* 0 -> 0 bits from packet */
540 /* 1 -> 2 bytes from prefix - bunch of zeroes 5 from packet */
541 /* 2 -> 2 bytes from prefix - zeroes + 3 from packet */
542 /* 3 -> 2 bytes from prefix - infer 1 bytes from lladdr */
543 const uint8_t unc_mxconf[] = {0x0f, 0x25, 0x23, 0x21};
544 
545 /* Link local prefix */
546 const uint8_t llprefix[] = {0xfe, 0x80};
547 
548 /* TTL uncompression values */
549 static const uint8_t ttl_values[] = {0, 1, 64, 255};
550 
551 /*--------------------------------------------------------------------*/
552 /** \name IPHC related functions
553  * @{ */
554 /*--------------------------------------------------------------------*/
555 /** \brief find the context corresponding to prefix ipaddr */
556 static struct sicslowpan_addr_context*
558 {
559 /* Remove code to avoid warnings and save flash if no context is used */
560 #if SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0
561  int i;
562  for(i = 0; i < SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS; i++) {
563  if((addr_contexts[i].used == 1) &&
564  uip_ipaddr_prefixcmp(&addr_contexts[i].prefix, ipaddr, 64)) {
565  return &addr_contexts[i];
566  }
567  }
568 #endif /* SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0 */
569  return NULL;
570 }
571 /*--------------------------------------------------------------------*/
572 /** \brief find the context with the given number */
573 static struct sicslowpan_addr_context*
575 {
576 /* Remove code to avoid warnings and save flash if no context is used */
577 #if SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0
578  int i;
579  for(i = 0; i < SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS; i++) {
580  if((addr_contexts[i].used == 1) &&
581  addr_contexts[i].number == number) {
582  return &addr_contexts[i];
583  }
584  }
585 #endif /* SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0 */
586  return NULL;
587 }
588 /*--------------------------------------------------------------------*/
589 static uint8_t
590 compress_addr_64(uint8_t bitpos, uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr)
591 {
592  if(uip_is_addr_mac_addr_based(ipaddr, lladdr)) {
593  return 3 << bitpos; /* 0-bits */
595  /* compress IID to 16 bits xxxx::0000:00ff:fe00:XXXX */
596  memcpy(hc06_ptr, &ipaddr->u16[7], 2);
597  hc06_ptr += 2;
598  return 2 << bitpos; /* 16-bits */
599  } else {
600  /* do not compress IID => xxxx::IID */
601  memcpy(hc06_ptr, &ipaddr->u16[4], 8);
602  hc06_ptr += 8;
603  return 1 << bitpos; /* 64-bits */
604  }
605 }
606 
607 /*-------------------------------------------------------------------- */
608 /* Uncompress addresses based on a prefix and a postfix with zeroes in
609  * between. If the postfix is zero in length it will use the link address
610  * to configure the IP address (autoconf style).
611  * pref_post_count takes a byte where the first nibble specify prefix count
612  * and the second postfix count (NOTE: 15/0xf => 16 bytes copy).
613  */
614 static void
615 uncompress_addr(uip_ipaddr_t *ipaddr, uint8_t const prefix[],
616  uint8_t pref_post_count, uip_lladdr_t *lladdr)
617 {
618  uint8_t prefcount = pref_post_count >> 4;
619  uint8_t postcount = pref_post_count & 0x0f;
620  /* full nibble 15 => 16 */
621  prefcount = prefcount == 15 ? 16 : prefcount;
622  postcount = postcount == 15 ? 16 : postcount;
623 
624  LOG_DBG("uncompression: address %d %d ", prefcount, postcount);
625 
626  if(prefcount > 0) {
627  memcpy(ipaddr, prefix, prefcount);
628  }
629  if(prefcount + postcount < 16) {
630  memset(&ipaddr->u8[prefcount], 0, 16 - (prefcount + postcount));
631  }
632  if(postcount > 0) {
633  memcpy(&ipaddr->u8[16 - postcount], hc06_ptr, postcount);
634  if(postcount == 2 && prefcount < 11) {
635  /* 16 bits uncompression => 0000:00ff:fe00:XXXX */
636  ipaddr->u8[11] = 0xff;
637  ipaddr->u8[12] = 0xfe;
638  }
639  hc06_ptr += postcount;
640  } else if (prefcount > 0) {
641  /* no IID based configuration if no prefix and no data => unspec */
642  uip_ds6_set_addr_iid(ipaddr, lladdr);
643  }
644 
645  LOG_DBG_6ADDR(ipaddr);
646  LOG_DBG_("\n");
647 }
648 
649 /*--------------------------------------------------------------------*/
650 /**
651  * \brief Compress IP/UDP header
652  *
653  * This function is called by the 6lowpan code to create a compressed
654  * 6lowpan packet in the packetbuf buffer from a full IPv6 packet in the
655  * uip_buf buffer.
656  *
657  *
658  * IPHC (RFC 6282)\n
659  * http://tools.ietf.org/html/
660  *
661  * \note We do not support ISA100_UDP header compression
662  *
663  * For LOWPAN_UDP compression, we either compress both ports or none.
664  * General format with LOWPAN_UDP compression is
665  * \verbatim
666  * 1 2 3
667  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
668  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
669  * |0|1|1|TF |N|HLI|C|S|SAM|M|D|DAM| SCI | DCI | comp. IPv6 hdr|
670  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
671  * | compressed IPv6 fields ..... |
672  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
673  * | LOWPAN_UDP | non compressed UDP fields ... |
674  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675  * | L4 data ... |
676  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
677  * \endverbatim
678  * \note The context number 00 is reserved for the link local prefix.
679  * For unicast addresses, if we cannot compress the prefix, we neither
680  * compress the IID.
681  * \param link_destaddr L2 destination address, needed to compress IP
682  * dest
683  * \return 1 if success, else 0
684  */
685 static int
686 compress_hdr_iphc(linkaddr_t *link_destaddr)
687 {
688  uint8_t tmp, iphc0, iphc1, *next_hdr, *next_nhc;
689  int ext_hdr_len;
690  struct uip_udp_hdr *udp_buf;
691 
692  if(LOG_DBG_ENABLED) {
693  uint16_t ndx;
694  LOG_DBG("compression: before (%d): ", UIP_IP_BUF->len[1]);
695  for(ndx = 0; ndx < UIP_IP_BUF->len[1] + 40; ndx++) {
696  uint8_t data = ((uint8_t *) (UIP_IP_BUF))[ndx];
697  LOG_DBG_("%02x", data);
698  }
699  LOG_DBG_("\n");
700  }
701 
702 /* Macro used only internally, during header compression. Checks if there
703  * is sufficient space in packetbuf before writing any further. */
704 #define CHECK_BUFFER_SPACE(writelen) do { \
705  if(hc06_ptr + (writelen) >= PACKETBUF_PAYLOAD_END) { \
706  LOG_WARN("Not enough packetbuf space to compress header (%u bytes, %u left). Aborting.\n", \
707  (unsigned)(writelen), (unsigned)(PACKETBUF_PAYLOAD_END - hc06_ptr)); \
708  return 0; \
709  } \
710 } while(0);
711 
712  hc06_ptr = PACKETBUF_IPHC_BUF + 2;
713 
714  /* Check if there is enough space for the compressed IPv6 header, in the
715  * worst case (least compressed case). Extension headers and transport
716  * layer will be checked when they are compressed. */
717  CHECK_BUFFER_SPACE(38);
718 
719  /*
720  * As we copy some bit-length fields, in the IPHC encoding bytes,
721  * we sometimes use |=
722  * If the field is 0, and the current bit value in memory is 1,
723  * this does not work. We therefore reset the IPHC encoding here
724  */
725 
726  iphc0 = SICSLOWPAN_DISPATCH_IPHC;
727  iphc1 = 0;
728  PACKETBUF_IPHC_BUF[2] = 0; /* might not be used - but needs to be cleared */
729 
730  /*
731  * Address handling needs to be made first since it might
732  * cause an extra byte with [ SCI | DCI ]
733  *
734  */
735 
736 
737  /* check if dest context exists (for allocating third byte) */
738  /* TODO: fix this so that it remembers the looked up values for
739  avoiding two lookups - or set the lookup values immediately */
740  if(addr_context_lookup_by_prefix(&UIP_IP_BUF->destipaddr) != NULL ||
741  addr_context_lookup_by_prefix(&UIP_IP_BUF->srcipaddr) != NULL) {
742  /* set context flag and increase hc06_ptr */
743  LOG_DBG("compression: dest or src ipaddr - setting CID\n");
744  iphc1 |= SICSLOWPAN_IPHC_CID;
745  hc06_ptr++;
746  }
747 
748  /*
749  * Traffic class, flow label
750  * If flow label is 0, compress it. If traffic class is 0, compress it
751  * We have to process both in the same time as the offset of traffic class
752  * depends on the presence of version and flow label
753  */
754 
755  /* IPHC format of tc is ECN | DSCP , original is DSCP | ECN */
756 
757  tmp = (UIP_IP_BUF->vtc << 4) | (UIP_IP_BUF->tcflow >> 4);
758  tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
759 
760  if(((UIP_IP_BUF->tcflow & 0x0F) == 0) &&
761  (UIP_IP_BUF->flow == 0)) {
762  /* flow label can be compressed */
763  iphc0 |= SICSLOWPAN_IPHC_FL_C;
764  if(((UIP_IP_BUF->vtc & 0x0F) == 0) &&
765  ((UIP_IP_BUF->tcflow & 0xF0) == 0)) {
766  /* compress (elide) all */
767  iphc0 |= SICSLOWPAN_IPHC_TC_C;
768  } else {
769  /* compress only the flow label */
770  *hc06_ptr = tmp;
771  hc06_ptr += 1;
772  }
773  } else {
774  /* Flow label cannot be compressed */
775  if(((UIP_IP_BUF->vtc & 0x0F) == 0) &&
776  ((UIP_IP_BUF->tcflow & 0xF0) == 0)) {
777  /* compress only traffic class */
778  iphc0 |= SICSLOWPAN_IPHC_TC_C;
779  *hc06_ptr = (tmp & 0xc0) |
780  (UIP_IP_BUF->tcflow & 0x0F);
781  memcpy(hc06_ptr + 1, &UIP_IP_BUF->flow, 2);
782  hc06_ptr += 3;
783  } else {
784  /* compress nothing */
785  memcpy(hc06_ptr, &UIP_IP_BUF->vtc, 4);
786  /* but replace the top byte with the new ECN | DSCP format*/
787  *hc06_ptr = tmp;
788  hc06_ptr += 4;
789  }
790  }
791 
792  /* Note that the payload length is always compressed */
793 
794  /* Next header. We compress it is compressable. */
795  if(IS_COMPRESSABLE_PROTO(UIP_IP_BUF->proto)) {
796  iphc0 |= SICSLOWPAN_IPHC_NH_C;
797  }
798 
799  /* Add proto header unless it is compressed */
800  if((iphc0 & SICSLOWPAN_IPHC_NH_C) == 0) {
801  *hc06_ptr = UIP_IP_BUF->proto;
802  hc06_ptr += 1;
803  }
804 
805  /*
806  * Hop limit
807  * if 1: compress, encoding is 01
808  * if 64: compress, encoding is 10
809  * if 255: compress, encoding is 11
810  * else do not compress
811  */
812  switch(UIP_IP_BUF->ttl) {
813  case 1:
814  iphc0 |= SICSLOWPAN_IPHC_TTL_1;
815  break;
816  case 64:
817  iphc0 |= SICSLOWPAN_IPHC_TTL_64;
818  break;
819  case 255:
820  iphc0 |= SICSLOWPAN_IPHC_TTL_255;
821  break;
822  default:
823  *hc06_ptr = UIP_IP_BUF->ttl;
824  hc06_ptr += 1;
825  break;
826  }
827 
828  /* source address - cannot be multicast */
829  if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
830  LOG_DBG("compression: addr unspecified - setting SAC\n");
831  iphc1 |= SICSLOWPAN_IPHC_SAC;
832  iphc1 |= SICSLOWPAN_IPHC_SAM_00;
833  } else if((context = addr_context_lookup_by_prefix(&UIP_IP_BUF->srcipaddr))
834  != NULL) {
835  /* elide the prefix - indicate by CID and set context + SAC */
836  LOG_DBG("compression: src with context - setting CID & SAC ctx: %d\n",
837  context->number);
838  iphc1 |= SICSLOWPAN_IPHC_CID | SICSLOWPAN_IPHC_SAC;
839  PACKETBUF_IPHC_BUF[2] |= context->number << 4;
840  /* compession compare with this nodes address (source) */
841 
842  iphc1 |= compress_addr_64(SICSLOWPAN_IPHC_SAM_BIT,
843  &UIP_IP_BUF->srcipaddr, &uip_lladdr);
844  /* No context found for this address */
845  } else if(uip_is_addr_linklocal(&UIP_IP_BUF->srcipaddr) &&
846  UIP_IP_BUF->destipaddr.u16[1] == 0 &&
847  UIP_IP_BUF->destipaddr.u16[2] == 0 &&
848  UIP_IP_BUF->destipaddr.u16[3] == 0) {
849  iphc1 |= compress_addr_64(SICSLOWPAN_IPHC_SAM_BIT,
850  &UIP_IP_BUF->srcipaddr, &uip_lladdr);
851  } else {
852  /* send the full address => SAC = 0, SAM = 00 */
853  iphc1 |= SICSLOWPAN_IPHC_SAM_00; /* 128-bits */
854  memcpy(hc06_ptr, &UIP_IP_BUF->srcipaddr.u16[0], 16);
855  hc06_ptr += 16;
856  }
857 
858  /* dest address*/
859  if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
860  /* Address is multicast, try to compress */
861  iphc1 |= SICSLOWPAN_IPHC_M;
862  if(sicslowpan_is_mcast_addr_compressable8(&UIP_IP_BUF->destipaddr)) {
863  iphc1 |= SICSLOWPAN_IPHC_DAM_11;
864  /* use last byte */
865  *hc06_ptr = UIP_IP_BUF->destipaddr.u8[15];
866  hc06_ptr += 1;
867  } else if(sicslowpan_is_mcast_addr_compressable32(&UIP_IP_BUF->destipaddr)) {
868  iphc1 |= SICSLOWPAN_IPHC_DAM_10;
869  /* second byte + the last three */
870  *hc06_ptr = UIP_IP_BUF->destipaddr.u8[1];
871  memcpy(hc06_ptr + 1, &UIP_IP_BUF->destipaddr.u8[13], 3);
872  hc06_ptr += 4;
873  } else if(sicslowpan_is_mcast_addr_compressable48(&UIP_IP_BUF->destipaddr)) {
874  iphc1 |= SICSLOWPAN_IPHC_DAM_01;
875  /* second byte + the last five */
876  *hc06_ptr = UIP_IP_BUF->destipaddr.u8[1];
877  memcpy(hc06_ptr + 1, &UIP_IP_BUF->destipaddr.u8[11], 5);
878  hc06_ptr += 6;
879  } else {
880  iphc1 |= SICSLOWPAN_IPHC_DAM_00;
881  /* full address */
882  memcpy(hc06_ptr, &UIP_IP_BUF->destipaddr.u8[0], 16);
883  hc06_ptr += 16;
884  }
885  } else {
886  /* Address is unicast, try to compress */
887  if((context = addr_context_lookup_by_prefix(&UIP_IP_BUF->destipaddr)) != NULL) {
888  /* elide the prefix */
889  iphc1 |= SICSLOWPAN_IPHC_DAC;
890  PACKETBUF_IPHC_BUF[2] |= context->number;
891  /* compession compare with link adress (destination) */
892 
893  iphc1 |= compress_addr_64(SICSLOWPAN_IPHC_DAM_BIT,
894  &UIP_IP_BUF->destipaddr,
895  (uip_lladdr_t *)link_destaddr);
896  /* No context found for this address */
897  } else if(uip_is_addr_linklocal(&UIP_IP_BUF->destipaddr) &&
898  UIP_IP_BUF->destipaddr.u16[1] == 0 &&
899  UIP_IP_BUF->destipaddr.u16[2] == 0 &&
900  UIP_IP_BUF->destipaddr.u16[3] == 0) {
901  iphc1 |= compress_addr_64(SICSLOWPAN_IPHC_DAM_BIT,
902  &UIP_IP_BUF->destipaddr, (uip_lladdr_t *)link_destaddr);
903  } else {
904  /* send the full address */
905  iphc1 |= SICSLOWPAN_IPHC_DAM_00; /* 128-bits */
906  memcpy(hc06_ptr, &UIP_IP_BUF->destipaddr.u16[0], 16);
907  hc06_ptr += 16;
908  }
909  }
910 
911  uncomp_hdr_len = UIP_IPH_LEN;
912 
913  /* Start of ext hdr compression or UDP compression */
914  /* pick out the next-header position */
915  next_hdr = &UIP_IP_BUF->proto;
916  next_nhc = hc06_ptr; /* here we set the next header is compressed. */
917  ext_hdr_len = 0;
918  /* reserve the write place of this next header position */
919  LOG_DBG("compression: first header: %d\n", *next_hdr);
920  while(next_hdr != NULL && IS_COMPRESSABLE_PROTO(*next_hdr)) {
921  LOG_DBG("compression: next header: %d\n", *next_hdr);
922  int proto = -1; /* used for the specific ext hdr */
923  /* UDP and EXT header compression */
924  switch(*next_hdr) {
925  case UIP_PROTO_HBHO:
926  proto = SICSLOWPAN_NHC_ETX_HDR_HBHO;
927  case UIP_PROTO_ROUTING:
928  proto = proto == -1 ? SICSLOWPAN_NHC_ETX_HDR_ROUTING : proto;
929  case UIP_PROTO_FRAG:
930  proto = proto == -1 ? SICSLOWPAN_NHC_ETX_HDR_FRAG : proto;
931  case UIP_PROTO_DESTO:
932  /* Handle the header here! */
933  {
934  struct uip_ext_hdr *ext_hdr =
935  (struct uip_ext_hdr *) UIP_IPPAYLOAD_BUF_POS(ext_hdr_len);
936  int len;
937  proto = proto == -1 ? SICSLOWPAN_NHC_ETX_HDR_DESTO : proto;
938  /* Len is defined to be in octets from the length byte */
939  len = (ext_hdr->len << 3) + 8;
940  LOG_DBG("compression: next header %d (len:%d)\n", *next_hdr, len);
941  /* pick up the next header */
942  next_hdr = &ext_hdr->next;
943  /* If the next header is not compressable we need to reserve the
944  NHC byte extra - before the next header here. This is due to
945  next not being elided in that case. */
946  if(!IS_COMPRESSABLE_PROTO(*next_hdr)) {
947  CHECK_BUFFER_SPACE(1);
948  hc06_ptr++;
949  LOG_DBG("compression: keeping the next header in this ext hdr: %d\n",
950  ext_hdr->next);
951  }
952  /* copy the ext-hdr into the hc06 buffer */
953  CHECK_BUFFER_SPACE(len);
954  memcpy(hc06_ptr, ext_hdr, len);
955  /* modify the len to octets */
956  ext_hdr = (struct uip_ext_hdr *) hc06_ptr;
957  ext_hdr->len = len - 2; /* Len should be in bytes from len byte*/
958  ext_hdr_len += len;
959  hc06_ptr += len;
960  uncomp_hdr_len += len;
961 
962  /* Write this next header - with its NHC header - including flag
963  to tell if next header is elided in this one also- */
964  *next_nhc = SICSLOWPAN_NHC_EXT_HDR |
965  (IS_COMPRESSABLE_PROTO(*next_hdr) ? SICSLOWPAN_NHC_BIT : 0) |
966  (proto << 1);
967  /* update the position of the next header */
968  next_nhc = hc06_ptr;
969  }
970  break;
971  case UIP_PROTO_UDP:
972  /* allocate a byte for the next header posision as UDP has no next */
973  hc06_ptr++;
974  udp_buf = UIP_UDP_BUF_POS(ext_hdr_len);
975  LOG_DBG("compression: inlined UDP ports on send side: %x, %x\n",
976  UIP_HTONS(udp_buf->srcport), UIP_HTONS(udp_buf->destport));
977  /* Mask out the last 4 bits can be used as a mask */
978  if(((UIP_HTONS(udp_buf->srcport) & 0xfff0) == SICSLOWPAN_UDP_4_BIT_PORT_MIN) &&
979  ((UIP_HTONS(udp_buf->destport) & 0xfff0) == SICSLOWPAN_UDP_4_BIT_PORT_MIN)) {
980  /* we can compress 12 bits of both source and dest */
981  *next_nhc = SICSLOWPAN_NHC_UDP_CS_P_11;
982  LOG_DBG("IPHC: remove 12 b of both source & dest with prefix 0xFOB\n");
983  CHECK_BUFFER_SPACE(1);
984  *hc06_ptr =
985  (uint8_t)((UIP_HTONS(udp_buf->srcport) -
986  SICSLOWPAN_UDP_4_BIT_PORT_MIN) << 4) +
987  (uint8_t)((UIP_HTONS(udp_buf->destport) -
988  SICSLOWPAN_UDP_4_BIT_PORT_MIN));
989  hc06_ptr += 1;
990  } else if((UIP_HTONS(udp_buf->destport) & 0xff00) == SICSLOWPAN_UDP_8_BIT_PORT_MIN) {
991  /* we can compress 8 bits of dest, leave source. */
992  *next_nhc = SICSLOWPAN_NHC_UDP_CS_P_01;
993  LOG_DBG("IPHC: leave source, remove 8 bits of dest with prefix 0xF0\n");
994  CHECK_BUFFER_SPACE(3);
995  memcpy(hc06_ptr, &udp_buf->srcport, 2);
996  *(hc06_ptr + 2) =
997  (uint8_t)((UIP_HTONS(udp_buf->destport) -
998  SICSLOWPAN_UDP_8_BIT_PORT_MIN));
999  hc06_ptr += 3;
1000  } else if((UIP_HTONS(udp_buf->srcport) & 0xff00) == SICSLOWPAN_UDP_8_BIT_PORT_MIN) {
1001  /* we can compress 8 bits of src, leave dest. Copy compressed port */
1002  *next_nhc = SICSLOWPAN_NHC_UDP_CS_P_10;
1003  LOG_DBG("IPHC: remove 8 bits of source with prefix 0xF0, leave dest. hch: %i\n", *next_nhc);
1004  CHECK_BUFFER_SPACE(3);
1005  *hc06_ptr =
1006  (uint8_t)((UIP_HTONS(udp_buf->srcport) -
1007  SICSLOWPAN_UDP_8_BIT_PORT_MIN));
1008  memcpy(hc06_ptr + 1, &udp_buf->destport, 2);
1009  hc06_ptr += 3;
1010  } else {
1011  /* we cannot compress. Copy uncompressed ports, full checksum */
1012  *next_nhc = SICSLOWPAN_NHC_UDP_CS_P_00;
1013  LOG_DBG("IPHC: cannot compress UDP headers\n");
1014  CHECK_BUFFER_SPACE(4);
1015  memcpy(hc06_ptr, &udp_buf->srcport, 4);
1016  hc06_ptr += 4;
1017  }
1018  /* always inline the checksum */
1019  CHECK_BUFFER_SPACE(2);
1020  memcpy(hc06_ptr, &udp_buf->udpchksum, 2);
1021  hc06_ptr += 2;
1022  uncomp_hdr_len += UIP_UDPH_LEN;
1023  /* this is the final header. */
1024  next_hdr = NULL;
1025  break;
1026  default:
1027  LOG_ERR("compression: could not handle compression of header");
1028  }
1029  }
1030  if(next_hdr != NULL) {
1031  /* Last header could not be compressed - we assume that this is then OK!*/
1032  /* as the last EXT_HDR should be "uncompressed" and have the next there */
1033  LOG_DBG("compression: last header could is not compressed: %d\n", *next_hdr);
1034  }
1035  /* before the packetbuf_hdr_len operation */
1036  PACKETBUF_IPHC_BUF[0] = iphc0;
1037  PACKETBUF_IPHC_BUF[1] = iphc1;
1038 
1039  if(LOG_DBG_ENABLED) {
1040  uint16_t ndx;
1041  LOG_DBG("compression: after (%d): ", (int)(hc06_ptr - packetbuf_ptr));
1042  for(ndx = 0; ndx < hc06_ptr - packetbuf_ptr; ndx++) {
1043  uint8_t data = ((uint8_t *) packetbuf_ptr)[ndx];
1044  LOG_DBG_("%02x", data);
1045  }
1046  LOG_DBG_("\n");
1047  }
1048 
1050 
1051  return 1;
1052 }
1053 
1054 /*--------------------------------------------------------------------*/
1055 /**
1056  * \brief Uncompress IPHC (i.e., IPHC and LOWPAN_UDP) headers and put
1057  * them in sicslowpan_buf
1058  *
1059  * This function is called by the input function when the dispatch is
1060  * IPHC.
1061  * We %process the packet in the packetbuf buffer, uncompress the header
1062  * fields, and copy the result in the sicslowpan buffer.
1063  * At the end of the decompression, packetbuf_hdr_len and uncompressed_hdr_len
1064  * are set to the appropriate values
1065  *
1066  * \param buf Pointer to the buffer to uncompress the packet into.
1067  * \param buf_size The size of the buffer to uncompress the packet into.
1068  * \param ip_len Equal to 0 if the packet is not a fragment (IP length
1069  * is then inferred from the L2 length), non 0 if the packet is a 1st
1070  * fragment.
1071  */
1072 static void
1073 uncompress_hdr_iphc(uint8_t *buf, uint16_t buf_size, uint16_t ip_len)
1074 {
1075  uint8_t tmp, iphc0, iphc1, nhc;
1076  struct uip_ext_hdr *exthdr;
1077  uint8_t* last_nextheader;
1078  uint8_t* ip_payload;
1079  uint8_t ext_hdr_len = 0;
1080 
1081  /* at least two byte will be used for the encoding */
1083 
1084  iphc0 = PACKETBUF_IPHC_BUF[0];
1085  iphc1 = PACKETBUF_IPHC_BUF[1];
1086 
1087  /* another if the CID flag is set */
1088  if(iphc1 & SICSLOWPAN_IPHC_CID) {
1089  LOG_DBG("uncompression: CID flag set - increase header with one\n");
1090  hc06_ptr++;
1091  }
1092 
1093  /* Traffic class and flow label */
1094  if((iphc0 & SICSLOWPAN_IPHC_FL_C) == 0) {
1095  /* Flow label are carried inline */
1096  if((iphc0 & SICSLOWPAN_IPHC_TC_C) == 0) {
1097  /* Traffic class is carried inline */
1098  memcpy(&SICSLOWPAN_IP_BUF(buf)->tcflow, hc06_ptr + 1, 3);
1099  tmp = *hc06_ptr;
1100  hc06_ptr += 4;
1101  /* IPHC format of tc is ECN | DSCP , original is DSCP | ECN */
1102  /* set version, pick highest DSCP bits and set in vtc */
1103  SICSLOWPAN_IP_BUF(buf)->vtc = 0x60 | ((tmp >> 2) & 0x0f);
1104  /* ECN rolled down two steps + lowest DSCP bits at top two bits */
1105  SICSLOWPAN_IP_BUF(buf)->tcflow = ((tmp >> 2) & 0x30) | (tmp << 6) |
1106  (SICSLOWPAN_IP_BUF(buf)->tcflow & 0x0f);
1107  } else {
1108  /* Traffic class is compressed (set version and no TC)*/
1109  SICSLOWPAN_IP_BUF(buf)->vtc = 0x60;
1110  /* highest flow label bits + ECN bits */
1111  SICSLOWPAN_IP_BUF(buf)->tcflow = (*hc06_ptr & 0x0F) |
1112  ((*hc06_ptr >> 2) & 0x30);
1113  memcpy(&SICSLOWPAN_IP_BUF(buf)->flow, hc06_ptr + 1, 2);
1114  hc06_ptr += 3;
1115  }
1116  } else {
1117  /* Version is always 6! */
1118  /* Version and flow label are compressed */
1119  if((iphc0 & SICSLOWPAN_IPHC_TC_C) == 0) {
1120  /* Traffic class is inline */
1121  SICSLOWPAN_IP_BUF(buf)->vtc = 0x60 | ((*hc06_ptr >> 2) & 0x0f);
1122  SICSLOWPAN_IP_BUF(buf)->tcflow = ((*hc06_ptr << 6) & 0xC0) | ((*hc06_ptr >> 2) & 0x30);
1123  SICSLOWPAN_IP_BUF(buf)->flow = 0;
1124  hc06_ptr += 1;
1125  } else {
1126  /* Traffic class is compressed */
1127  SICSLOWPAN_IP_BUF(buf)->vtc = 0x60;
1128  SICSLOWPAN_IP_BUF(buf)->tcflow = 0;
1129  SICSLOWPAN_IP_BUF(buf)->flow = 0;
1130  }
1131  }
1132 
1133  /* Next Header */
1134  if((iphc0 & SICSLOWPAN_IPHC_NH_C) == 0) {
1135  /* Next header is carried inline */
1136  SICSLOWPAN_IP_BUF(buf)->proto = *hc06_ptr;
1137  LOG_DBG("uncompression: next header inline: %d\n", SICSLOWPAN_IP_BUF(buf)->proto);
1138  hc06_ptr += 1;
1139  }
1140 
1141  /* Hop limit */
1142  if((iphc0 & 0x03) != SICSLOWPAN_IPHC_TTL_I) {
1143  SICSLOWPAN_IP_BUF(buf)->ttl = ttl_values[iphc0 & 0x03];
1144  } else {
1145  SICSLOWPAN_IP_BUF(buf)->ttl = *hc06_ptr;
1146  hc06_ptr += 1;
1147  }
1148 
1149  /* put the source address compression mode SAM in the tmp var */
1150  tmp = ((iphc1 & SICSLOWPAN_IPHC_SAM_11) >> SICSLOWPAN_IPHC_SAM_BIT) & 0x03;
1151 
1152  /* context based compression */
1153  if(iphc1 & SICSLOWPAN_IPHC_SAC) {
1154  uint8_t sci = (iphc1 & SICSLOWPAN_IPHC_CID) ?
1155  PACKETBUF_IPHC_BUF[2] >> 4 : 0;
1156 
1157  /* Source address - check context != NULL only if SAM bits are != 0*/
1158  if (tmp != 0) {
1159  context = addr_context_lookup_by_number(sci);
1160  if(context == NULL) {
1161  LOG_ERR("uncompression: error context not found\n");
1162  return;
1163  }
1164  }
1165  /* if tmp == 0 we do not have a context and therefore no prefix */
1166  uncompress_addr(&SICSLOWPAN_IP_BUF(buf)->srcipaddr,
1167  tmp != 0 ? context->prefix : NULL, unc_ctxconf[tmp],
1168  (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
1169  } else {
1170  /* no compression and link local */
1171  uncompress_addr(&SICSLOWPAN_IP_BUF(buf)->srcipaddr, llprefix, unc_llconf[tmp],
1172  (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_SENDER));
1173  }
1174 
1175  /* Destination address */
1176  /* put the destination address compression mode into tmp */
1177  tmp = ((iphc1 & SICSLOWPAN_IPHC_DAM_11) >> SICSLOWPAN_IPHC_DAM_BIT) & 0x03;
1178 
1179  /* multicast compression */
1180  if(iphc1 & SICSLOWPAN_IPHC_M) {
1181  /* context based multicast compression */
1182  if(iphc1 & SICSLOWPAN_IPHC_DAC) {
1183  /* TODO: implement this */
1184  } else {
1185  /* non-context based multicast compression - */
1186  /* DAM_00: 128 bits */
1187  /* DAM_01: 48 bits FFXX::00XX:XXXX:XXXX */
1188  /* DAM_10: 32 bits FFXX::00XX:XXXX */
1189  /* DAM_11: 8 bits FF02::00XX */
1190  uint8_t prefix[] = {0xff, 0x02};
1191  if(tmp > 0 && tmp < 3) {
1192  prefix[1] = *hc06_ptr;
1193  hc06_ptr++;
1194  }
1195 
1196  uncompress_addr(&SICSLOWPAN_IP_BUF(buf)->destipaddr, prefix,
1197  unc_mxconf[tmp], NULL);
1198  }
1199  } else {
1200  /* no multicast */
1201  /* Context based */
1202  if(iphc1 & SICSLOWPAN_IPHC_DAC) {
1203  uint8_t dci = (iphc1 & SICSLOWPAN_IPHC_CID) ? PACKETBUF_IPHC_BUF[2] & 0x0f : 0;
1204  context = addr_context_lookup_by_number(dci);
1205 
1206  /* all valid cases below need the context! */
1207  if(context == NULL) {
1208  LOG_ERR("uncompression: error context not found\n");
1209  return;
1210  }
1211  uncompress_addr(&SICSLOWPAN_IP_BUF(buf)->destipaddr, context->prefix,
1212  unc_ctxconf[tmp],
1213  (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
1214  } else {
1215  /* not context based => link local M = 0, DAC = 0 - same as SAC */
1216  uncompress_addr(&SICSLOWPAN_IP_BUF(buf)->destipaddr, llprefix,
1217  unc_llconf[tmp],
1218  (uip_lladdr_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
1219  }
1220  }
1221  uncomp_hdr_len += UIP_IPH_LEN;
1222 
1223  /* Next header processing - continued */
1224  nhc = iphc0 & SICSLOWPAN_IPHC_NH_C;
1225  /* The next header is compressed, NHC is following */
1226  last_nextheader = &SICSLOWPAN_IP_BUF(buf)->proto;
1227  ip_payload = SICSLOWPAN_IPPAYLOAD_BUF(buf);
1228 
1229  while(nhc && (*hc06_ptr & SICSLOWPAN_NHC_MASK) == SICSLOWPAN_NHC_EXT_HDR) {
1230  uint8_t eid = (*hc06_ptr & 0x0e) >> 1;
1231  /* next header compression flag */
1232  uint8_t nh = (*hc06_ptr & 0x01);
1233  uint8_t next = 0;
1234  uint8_t len;
1235  uint8_t proto;
1236 
1237  nhc = nh;
1238 
1239  hc06_ptr++;
1240  if(!nh) {
1241  next = *hc06_ptr;
1242  hc06_ptr++;
1243  LOG_DBG("uncompression: next header is inlined. Next: %d\n", next);
1244  }
1245  len = *hc06_ptr;
1246  hc06_ptr++;
1247 
1248  LOG_DBG("uncompression: found ext header id: %d next: %d len: %d\n", eid, next, len);
1249  switch(eid) {
1250  case SICSLOWPAN_NHC_ETX_HDR_HBHO:
1251  proto = UIP_PROTO_HBHO;
1252  break;
1253  case SICSLOWPAN_NHC_ETX_HDR_ROUTING:
1254  proto = UIP_PROTO_ROUTING;
1255  break;
1256  case SICSLOWPAN_NHC_ETX_HDR_FRAG:
1257  proto = UIP_PROTO_FRAG;
1258  break;
1259  case SICSLOWPAN_NHC_ETX_HDR_DESTO:
1260  proto = UIP_PROTO_DESTO;
1261  break;
1262  default:
1263  LOG_DBG("uncompression: error unsupported ext header\n");
1264  return;
1265  }
1266  *last_nextheader = proto;
1267 
1268  /* Check that there is enough room to write the extension header. */
1269  if((ip_payload - buf) + UIP_EXT_HDR_LEN + len > buf_size) {
1270  LOG_WARN("uncompression: cannot write ext header beyond target buffer\n");
1271  return;
1272  }
1273 
1274  /* uncompress the extension header */
1275  exthdr = (struct uip_ext_hdr *)ip_payload;
1276  exthdr->len = (UIP_EXT_HDR_LEN + len) / 8;
1277  if(exthdr->len == 0) {
1278  LOG_WARN("Extension header length is below 8\n");
1279  return;
1280  }
1281  exthdr->len--;
1282  exthdr->next = next;
1283  last_nextheader = &exthdr->next;
1284  memcpy((uint8_t *)exthdr + UIP_EXT_HDR_LEN, hc06_ptr, len);
1285 
1286  hc06_ptr += len;
1287  uncomp_hdr_len += (exthdr->len + 1) * 8;
1288  ip_payload += (exthdr->len + 1) * 8;
1289  ext_hdr_len += (exthdr->len + 1) * 8;
1290 
1291  LOG_DBG("uncompression: %d len: %d exthdr len: %d (calc: %d)\n",
1292  proto, len, exthdr->len, (exthdr->len + 1) * 8);
1293  }
1294 
1295  /* The next header is compressed, NHC is following */
1296  if(nhc && (*hc06_ptr & SICSLOWPAN_NHC_UDP_MASK) == SICSLOWPAN_NHC_UDP_ID) {
1297  struct uip_udp_hdr *udp_buf = (struct uip_udp_hdr *)ip_payload;
1298  uint16_t udp_len;
1299  uint8_t checksum_compressed;
1300  *last_nextheader = UIP_PROTO_UDP;
1301  checksum_compressed = *hc06_ptr & SICSLOWPAN_NHC_UDP_CHECKSUMC;
1302  LOG_DBG("uncompression: incoming header value: %i\n", *hc06_ptr);
1303  switch(*hc06_ptr & SICSLOWPAN_NHC_UDP_CS_P_11) {
1304  case SICSLOWPAN_NHC_UDP_CS_P_00:
1305  /* 1 byte for NHC, 4 byte for ports, 2 bytes chksum */
1306  memcpy(&udp_buf->srcport, hc06_ptr + 1, 2);
1307  memcpy(&udp_buf->destport, hc06_ptr + 3, 2);
1308  LOG_DBG("uncompression: UDP ports (ptr+5): %x, %x\n",
1309  UIP_HTONS(udp_buf->srcport),
1310  UIP_HTONS(udp_buf->destport));
1311  hc06_ptr += 5;
1312  break;
1313 
1314  case SICSLOWPAN_NHC_UDP_CS_P_01:
1315  /* 1 byte for NHC + source 16bit inline, dest = 0xF0 + 8 bit inline */
1316  LOG_DBG("uncompression: destination address\n");
1317  memcpy(&udp_buf->srcport, hc06_ptr + 1, 2);
1318  udp_buf->destport = UIP_HTONS(SICSLOWPAN_UDP_8_BIT_PORT_MIN + (*(hc06_ptr + 3)));
1319  LOG_DBG("uncompression: UDP ports (ptr+4): %x, %x\n",
1320  UIP_HTONS(udp_buf->srcport), UIP_HTONS(udp_buf->destport));
1321  hc06_ptr += 4;
1322  break;
1323 
1324  case SICSLOWPAN_NHC_UDP_CS_P_10:
1325  /* 1 byte for NHC + source = 0xF0 + 8bit inline, dest = 16 bit inline*/
1326  LOG_DBG("uncompression: source address\n");
1327  udp_buf->srcport = UIP_HTONS(SICSLOWPAN_UDP_8_BIT_PORT_MIN +
1328  (*(hc06_ptr + 1)));
1329  memcpy(&udp_buf->destport, hc06_ptr + 2, 2);
1330  LOG_DBG("uncompression: UDP ports (ptr+4): %x, %x\n",
1331  UIP_HTONS(udp_buf->srcport), UIP_HTONS(udp_buf->destport));
1332  hc06_ptr += 4;
1333  break;
1334 
1335  case SICSLOWPAN_NHC_UDP_CS_P_11:
1336  /* 1 byte for NHC, 1 byte for ports */
1337  udp_buf->srcport = UIP_HTONS(SICSLOWPAN_UDP_4_BIT_PORT_MIN +
1338  (*(hc06_ptr + 1) >> 4));
1339  udp_buf->destport = UIP_HTONS(SICSLOWPAN_UDP_4_BIT_PORT_MIN +
1340  ((*(hc06_ptr + 1)) & 0x0F));
1341  LOG_DBG("uncompression: UDP ports (ptr+2): %x, %x\n",
1342  UIP_HTONS(udp_buf->srcport), UIP_HTONS(udp_buf->destport));
1343  hc06_ptr += 2;
1344  break;
1345  default:
1346  LOG_DBG("uncompression: error unsupported UDP compression\n");
1347  return;
1348  }
1349  if(!checksum_compressed) { /* has_checksum, default */
1350  memcpy(&udp_buf->udpchksum, hc06_ptr, 2);
1351  hc06_ptr += 2;
1352  LOG_DBG("uncompression: checksum included\n");
1353  } else {
1354  LOG_DBG("uncompression: checksum *NOT* included\n");
1355  }
1356 
1357  /* length field in UDP header (8 byte header + payload) */
1358  udp_len = 8 + packetbuf_datalen() - (hc06_ptr - packetbuf_ptr);
1359  udp_buf->udplen = UIP_HTONS(ip_len == 0 ? udp_len :
1360  ip_len - UIP_IPH_LEN - ext_hdr_len);
1361  LOG_DBG("uncompression: UDP length: %u (ext: %u) ip_len: %d udp_len: %d\n",
1362  UIP_HTONS(udp_buf->udplen), ext_hdr_len, ip_len, udp_len);
1363 
1364  uncomp_hdr_len += UIP_UDPH_LEN;
1365  }
1366 
1367  packetbuf_hdr_len = hc06_ptr - packetbuf_ptr;
1368 
1369  /* IP length field. */
1370  if(ip_len == 0) {
1371  int len = packetbuf_datalen() - packetbuf_hdr_len + uncomp_hdr_len - UIP_IPH_LEN;
1372  LOG_DBG("uncompression: IP payload length: %d. %u - %u + %u - %u\n", len,
1373  packetbuf_datalen(), packetbuf_hdr_len, uncomp_hdr_len, UIP_IPH_LEN);
1374 
1375  /* This is not a fragmented packet */
1376  SICSLOWPAN_IP_BUF(buf)->len[0] = len >> 8;
1377  SICSLOWPAN_IP_BUF(buf)->len[1] = len & 0x00FF;
1378  } else {
1379  /* This is a 1st fragment */
1380  SICSLOWPAN_IP_BUF(buf)->len[0] = (ip_len - UIP_IPH_LEN) >> 8;
1381  SICSLOWPAN_IP_BUF(buf)->len[1] = (ip_len - UIP_IPH_LEN) & 0x00FF;
1382  }
1383 }
1384 /** @} */
1385 #endif /* SICSLOWPAN_COMPRESSION >= SICSLOWPAN_COMPRESSION_IPHC */
1386 
1387 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_6LORH
1388 /*--------------------------------------------------------------------*/
1389 /**
1390  * \brief Adds Paging dispatch byte
1391  */
1392 static void
1393 add_paging_dispatch(uint8_t page)
1394 {
1395  /* Add paging dispatch to Page 1 */
1396  PACKETBUF_6LO_PTR[PACKETBUF_6LO_DISPATCH] = SICSLOWPAN_DISPATCH_PAGING | (page & 0x0f);
1398 }
1399 /*--------------------------------------------------------------------*/
1400 /**
1401  * \brief Adds 6lorh headers before IPHC
1402  */
1403 static void
1405 {
1406  /* 6LoRH is not implemented yet */
1407 }
1408 #endif /* SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_6LORH */
1409 
1410 /*--------------------------------------------------------------------*/
1411 /**
1412  * \brief Digest 6lorh headers before IPHC
1413  */
1414 static void
1416 {
1417  /* Is this a paging dispatch? */
1418  if((PACKETBUF_6LO_PTR[PACKETBUF_6LO_DISPATCH] & SICSLOWPAN_DISPATCH_PAGING_MASK) == SICSLOWPAN_DISPATCH_PAGING) {
1419  /* Parse page number */
1420  curr_page = PACKETBUF_6LO_PTR[PACKETBUF_6LO_DISPATCH] & 0x0f;
1422  }
1423 }
1424 /*--------------------------------------------------------------------*/
1425 /**
1426  * \brief Digest 6lorh headers before IPHC
1427  */
1428 static void
1430 {
1431  /* 6LoRH is not implemented yet */
1432 }
1433 /*--------------------------------------------------------------------*/
1434 /** \name IPv6 dispatch "compression" function
1435  * @{ */
1436 /*--------------------------------------------------------------------*/
1437 /* \brief Packets "Compression" when only IPv6 dispatch is used
1438  *
1439  * There is no compression in this case, all fields are sent
1440  * inline. We just add the IPv6 dispatch byte before the packet.
1441  * \verbatim
1442  * 0 1 2 3
1443  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1444  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1445  * | IPv6 Dsp | IPv6 header and payload ...
1446  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1447  * \endverbatim
1448  */
1449 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_IPV6
1450 static void
1451 compress_hdr_ipv6(linkaddr_t *link_destaddr)
1452 {
1453  *packetbuf_ptr = SICSLOWPAN_DISPATCH_IPV6;
1454  packetbuf_hdr_len += SICSLOWPAN_IPV6_HDR_LEN;
1455  memcpy(packetbuf_ptr + packetbuf_hdr_len, UIP_IP_BUF, UIP_IPH_LEN);
1456  packetbuf_hdr_len += UIP_IPH_LEN;
1457  uncomp_hdr_len += UIP_IPH_LEN;
1458  return;
1459 }
1460 #endif /* SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_IPV6 */
1461 /** @} */
1462 
1463 /*--------------------------------------------------------------------*/
1464 /** \name Input/output functions common to all compression schemes
1465  * @{ */
1466 /*--------------------------------------------------------------------*/
1467 /**
1468  * Callback function for the MAC packet sent callback
1469  */
1470 static void
1471 packet_sent(void *ptr, int status, int transmissions)
1472 {
1473  const linkaddr_t *dest;
1474 
1475  if(callback != NULL) {
1476  callback->output_callback(status);
1477  }
1478  last_tx_status = status;
1479 
1480  /* What follows only applies to unicast */
1481  dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
1482  if(linkaddr_cmp(dest, &linkaddr_null)) {
1483  return;
1484  }
1485 
1486  /* Update neighbor link statistics */
1487  link_stats_packet_sent(dest, status, transmissions);
1488 
1489  /* Call routing protocol link callback */
1490  NETSTACK_ROUTING.link_callback(dest, status, transmissions);
1491 
1492  /* DS6 callback, used for UIP_DS6_LL_NUD */
1493  uip_ds6_link_callback(status, transmissions);
1494 }
1495 /*--------------------------------------------------------------------*/
1496 /**
1497  * \brief This function is called by the 6lowpan code to send out a
1498  * packet.
1499  * \param dest the link layer destination address of the packet
1500  */
1501 static void
1502 send_packet(linkaddr_t *dest)
1503 {
1504  /* Set the link layer destination address for the packet as a
1505  * packetbuf attribute. The MAC layer can access the destination
1506  * address with the function packetbuf_addr(PACKETBUF_ADDR_RECEIVER).
1507  */
1508  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, dest);
1509 
1510 #if NETSTACK_CONF_BRIDGE_MODE
1511  /* This needs to be explicitly set here for bridge mode to work */
1512  packetbuf_set_addr(PACKETBUF_ADDR_SENDER,(void*)&uip_lladdr);
1513 #endif
1514 
1515  /* Provide a callback function to receive the result of
1516  a packet transmission. */
1517  NETSTACK_MAC.send(&packet_sent, NULL);
1518 
1519  /* If we are sending multiple packets in a row, we need to let the
1520  watchdog know that we are still alive. */
1522 }
1523 #if SICSLOWPAN_CONF_FRAG
1524 /*--------------------------------------------------------------------*/
1525 /**
1526  * \brief This function is called by the 6lowpan code to copy a fragment's
1527  * payload from uIP and send it down the stack.
1528  * \param uip_offset the offset in the uIP buffer where to copy the payload from
1529  * \param dest the link layer destination address of the packet
1530  * \return 1 if success, 0 otherwise
1531  */
1532 static int
1533 fragment_copy_payload_and_send(uint16_t uip_offset, linkaddr_t *dest) {
1534  struct queuebuf *q;
1535 
1536  /* Now copy fragment payload from uip_buf */
1538  (uint8_t *)UIP_IP_BUF + uip_offset, packetbuf_payload_len);
1540 
1541  /* Backup packetbuf to queuebuf. Enables preserving attributes for all framgnets */
1542  q = queuebuf_new_from_packetbuf();
1543  if(q == NULL) {
1544  LOG_WARN("output: could not allocate queuebuf, dropping fragment\n");
1545  return 0;
1546  }
1547 
1548  /* Send fragment */
1549  send_packet(dest);
1550 
1551  /* Restore packetbuf from queuebuf */
1552  queuebuf_to_packetbuf(q);
1553  queuebuf_free(q);
1554 
1555  /* Check tx result. */
1556  if((last_tx_status == MAC_TX_COLLISION) ||
1557  (last_tx_status == MAC_TX_ERR) ||
1558  (last_tx_status == MAC_TX_ERR_FATAL)) {
1559  LOG_ERR("output: error in fragment tx, dropping subsequent fragments.\n");
1560  return 0;
1561  }
1562  return 1;
1563 }
1564 #endif /* SICSLOWPAN_CONF_FRAG */
1565 /*--------------------------------------------------------------------*/
1566 /** \brief Take an IP packet and format it to be sent on an 802.15.4
1567  * network using 6lowpan.
1568  * \param localdest The MAC address of the destination
1569  *
1570  * The IP packet is initially in uip_buf. Its header is compressed
1571  * and if necessary it is fragmented. The resulting
1572  * packet/fragments are put in packetbuf and delivered to the 802.15.4
1573  * MAC.
1574  */
1575 static uint8_t
1576 output(const linkaddr_t *localdest)
1577 {
1578  int frag_needed;
1579 
1580  /* The MAC address of the destination of the packet */
1581  linkaddr_t dest;
1582 
1583  /* init */
1584  uncomp_hdr_len = 0;
1585  packetbuf_hdr_len = 0;
1586 
1587  /* reset packetbuf buffer */
1588  packetbuf_clear();
1590 
1591  if(callback) {
1592  /* call the attribution when the callback comes, but set attributes
1593  here ! */
1594  set_packet_attrs();
1595  }
1596 
1597  /*
1598  * The destination address will be tagged to each outbound
1599  * packet. If the argument localdest is NULL, we are sending a
1600  * broadcast packet.
1601  */
1602  if(localdest == NULL) {
1603  linkaddr_copy(&dest, &linkaddr_null);
1604  } else {
1605  linkaddr_copy(&dest, localdest);
1606  }
1607 
1608  LOG_INFO("output: sending IPv6 packet with len %d\n", uip_len);
1609 
1610  /* copy over the retransmission count from uipbuf attributes */
1611  packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS,
1612  uipbuf_get_attr(UIPBUF_ATTR_MAX_MAC_TRANSMISSIONS));
1613 
1614 /* Calculate NETSTACK_FRAMER's header length, that will be added in the NETSTACK_MAC */
1615  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, &dest);
1616 #if LLSEC802154_USES_AUX_HEADER
1617  /* copy LLSEC level */
1618  packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL,
1619  uipbuf_get_attr(UIPBUF_ATTR_LLSEC_LEVEL));
1620 #if LLSEC802154_USES_EXPLICIT_KEYS
1621  packetbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX,
1622  uipbuf_get_attr(UIPBUF_ATTR_LLSEC_KEY_ID));
1623 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
1624 #endif /* LLSEC802154_USES_AUX_HEADER */
1625 
1626  mac_max_payload = NETSTACK_MAC.max_payload();
1627 
1628  if(mac_max_payload <= 0) {
1629  /* Framing failed, drop packet */
1630  LOG_WARN("output: failed to calculate payload size - dropping packet\n");
1631  return 0;
1632  }
1633 
1634  /* Try to compress the headers */
1635 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_IPV6
1636  compress_hdr_ipv6(&dest);
1637 #endif /* SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_IPV6 */
1638 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_6LORH
1639  /* Add 6LoRH headers before IPHC. Only needed on routed traffic
1640  (non link-local). */
1641  if(!uip_is_addr_linklocal(&UIP_IP_BUF->destipaddr)) {
1643  add_6lorh_hdr();
1644  }
1645 #endif /* SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_6LORH */
1646 #if SICSLOWPAN_COMPRESSION >= SICSLOWPAN_COMPRESSION_IPHC
1647  if(compress_hdr_iphc(&dest) == 0) {
1648  /* Warning should already be issued by function above */
1649  return 0;
1650  }
1651 #endif /* SICSLOWPAN_COMPRESSION >= SICSLOWPAN_COMPRESSION_IPHC */
1652 
1653  /* Use the mac_max_payload to understand what is the max payload in a MAC
1654  * packet. We calculate it here only to make a better decision of whether
1655  * the outgoing packet needs to be fragmented or not. */
1656 
1657  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, &dest);
1658 
1659  frag_needed = (int)uip_len - (int)uncomp_hdr_len + (int)packetbuf_hdr_len > mac_max_payload;
1660  LOG_INFO("output: header len %d -> %d, total len %d -> %d, MAC max payload %d, frag_needed %d\n",
1663  mac_max_payload, frag_needed);
1664 
1665  if(frag_needed) {
1666 #if SICSLOWPAN_CONF_FRAG
1667  /* Number of bytes processed. */
1668  uint16_t processed_ip_out_len;
1669  uint16_t frag_tag;
1670  int curr_frag = 0;
1671 
1672  /*
1673  * The outbound IPv6 packet is too large to fit into a single 15.4
1674  * packet, so we fragment it into multiple packets and send them.
1675  * The first fragment contains frag1 dispatch, then IPv6/IPHC/HC_UDP
1676  * dispatchs/headers and IPv6 payload (with len multiple of 8 bytes).
1677  * The subsequent fragments contain the FRAGN dispatch and more of the
1678  * IPv6 payload (still multiple of 8 bytes, except for the last fragment)
1679  */
1680  /* Total IPv6 payload */
1681  int total_payload = (uip_len - uncomp_hdr_len);
1682  /* IPv6 payload that goes to first fragment */
1683  int frag1_payload = (mac_max_payload - packetbuf_hdr_len - SICSLOWPAN_FRAG1_HDR_LEN) & 0xfffffff8;
1684  /* max IPv6 payload in each FRAGN. Must be multiple of 8 bytes */
1685  int fragn_max_payload = (mac_max_payload - SICSLOWPAN_FRAGN_HDR_LEN) & 0xfffffff8;
1686  /* max IPv6 payload in the last fragment. Needs not be multiple of 8 bytes */
1687  int last_fragn_max_payload = mac_max_payload - SICSLOWPAN_FRAGN_HDR_LEN;
1688  /* sum of all IPv6 payload that goes to non-first and non-last fragments */
1689  int middle_fragn_total_payload = MAX(total_payload - frag1_payload - last_fragn_max_payload, 0);
1690  /* Ceiling of: 2 + middle_fragn_total_payload / fragn_max_payload */
1691  int fragment_count = 2;
1692  if(middle_fragn_total_payload > 0) {
1693  fragment_count += 1 + (middle_fragn_total_payload - 1) / fragn_max_payload;
1694  }
1695 
1696  int freebuf = queuebuf_numfree() - 1;
1697  LOG_INFO("output: fragmentation needed, fragments: %u, free queuebufs: %u\n",
1698  fragment_count, freebuf);
1699 
1700  if(freebuf < fragment_count) {
1701  LOG_WARN("output: dropping packet, not enough free bufs (needed: %u, free: %u)\n",
1702  fragment_count, freebuf);
1703  return 0;
1704  }
1705 
1706  if(frag1_payload < 0) {
1707  /* The current implementation requires that all headers fit in the first
1708  * fragment. Here is a corner case where the header did fit packetbuf
1709  * but do no longer fit after truncating for a length multiple of 8. */
1710  LOG_WARN("output: compressed header does not fit first fragment\n");
1711  return 0;
1712  }
1713 
1714  /* Reset last tx status -- MAC layers most often call packet_sent asynchrously */
1716  /* Update fragment tag */
1717  frag_tag = my_tag++;
1718 
1719  /* Move IPHC/IPv6 header to make room for FRAG1 header */
1720  memmove(packetbuf_ptr + SICSLOWPAN_FRAG1_HDR_LEN, packetbuf_ptr, packetbuf_hdr_len);
1721  packetbuf_hdr_len += SICSLOWPAN_FRAG1_HDR_LEN;
1722 
1723  /* Set FRAG1 header */
1724  SET16(PACKETBUF_FRAG_PTR, PACKETBUF_FRAG_DISPATCH_SIZE,
1725  ((SICSLOWPAN_DISPATCH_FRAG1 << 8) | uip_len));
1726  SET16(PACKETBUF_FRAG_PTR, PACKETBUF_FRAG_TAG, frag_tag);
1727 
1728  /* Set frag1 payload len. Was already caulcated earlier as frag1_payload */
1729  packetbuf_payload_len = frag1_payload;
1730 
1731  /* Copy payload from uIP and send fragment */
1732  /* Send fragment */
1733  LOG_INFO("output: fragment %d/%d (tag %d, payload %d)\n",
1734  curr_frag + 1, fragment_count,
1735  frag_tag, packetbuf_payload_len);
1736  if(fragment_copy_payload_and_send(uncomp_hdr_len, &dest) == 0) {
1737  return 0;
1738  }
1739 
1740  /* Now prepare for subsequent fragments. */
1741 
1742  /* FRAGN header: tag was already set at FRAG1. Now set dispatch for all FRAGN */
1743  packetbuf_hdr_len = SICSLOWPAN_FRAGN_HDR_LEN;
1744  SET16(PACKETBUF_FRAG_PTR, PACKETBUF_FRAG_DISPATCH_SIZE,
1745  ((SICSLOWPAN_DISPATCH_FRAGN << 8) | uip_len));
1746 
1747  /* Keep track of the total length of data sent */
1748  processed_ip_out_len = uncomp_hdr_len + packetbuf_payload_len;
1749 
1750  /* Create and send subsequent fragments. */
1751  while(processed_ip_out_len < uip_len) {
1752  curr_frag++;
1753  /* FRAGN header: set offset for this fragment */
1754  PACKETBUF_FRAG_PTR[PACKETBUF_FRAG_OFFSET] = processed_ip_out_len >> 3;
1755 
1756  /* Calculate fragment len */
1757  if(uip_len - processed_ip_out_len > last_fragn_max_payload) {
1758  /* Not last fragment, send max FRAGN payload */
1759  packetbuf_payload_len = fragn_max_payload;
1760  } else {
1761  /* last fragment */
1762  packetbuf_payload_len = uip_len - processed_ip_out_len;
1763  }
1764 
1765  /* Copy payload from uIP and send fragment */
1766  /* Send fragment */
1767  LOG_INFO("output: fragment %d/%d (tag %d, payload %d, offset %d)\n",
1768  curr_frag + 1, fragment_count,
1769  frag_tag, packetbuf_payload_len, processed_ip_out_len);
1770  if(fragment_copy_payload_and_send(processed_ip_out_len, &dest) == 0) {
1771  return 0;
1772  }
1773 
1774  processed_ip_out_len += packetbuf_payload_len;
1775  }
1776 #else /* SICSLOWPAN_CONF_FRAG */
1777  LOG_ERR("output: Packet too large to be sent without fragmentation support; dropping packet\n");
1778  return 0;
1779 #endif /* SICSLOWPAN_CONF_FRAG */
1780  } else {
1781  /*
1782  * The packet does not need to be fragmented
1783  * copy "payload" and send
1784  */
1785 
1786  if(uip_len < uncomp_hdr_len) {
1787  LOG_ERR("output: uip_len is smaller than uncomp_hdr_len (%d < %d)",
1788  (int)uip_len, (int)uncomp_hdr_len);
1789  return 0;
1790  }
1791 
1795  send_packet(&dest);
1796  }
1797  return 1;
1798 }
1799 
1800 /*--------------------------------------------------------------------*/
1801 /** \brief Process a received 6lowpan packet.
1802  *
1803  * The 6lowpan packet is put in packetbuf by the MAC. If its a frag1 or
1804  * a non-fragmented packet we first uncompress the IP header. The
1805  * 6lowpan payload and possibly the uncompressed IP header are then
1806  * copied in siclowpan_buf. If the IP packet is complete it is copied
1807  * to uip_buf and the IP layer is called.
1808  *
1809  * \note We do not check for overlapping sicslowpan fragments
1810  * (it is a SHALL in the RFC 4944 and should never happen)
1811  */
1812 static void
1813 input(void)
1814 {
1815  /* size of the IP packet (read from fragment) */
1816  uint16_t frag_size = 0;
1817  /* offset of the fragment in the IP packet */
1818  uint8_t frag_offset = 0;
1819  uint8_t *buffer;
1820  uint16_t buffer_size;
1821 
1822 #if SICSLOWPAN_CONF_FRAG
1823  uint8_t is_fragment = 0;
1824  int8_t frag_context = 0;
1825 
1826  /* tag of the fragment */
1827  uint16_t frag_tag = 0;
1828  uint8_t first_fragment = 0, last_fragment = 0;
1829 #endif /*SICSLOWPAN_CONF_FRAG*/
1830 
1831  /* Update link statistics */
1832  link_stats_input_callback(packetbuf_addr(PACKETBUF_ADDR_SENDER));
1833 
1834  /* init */
1835  uncomp_hdr_len = 0;
1836  packetbuf_hdr_len = 0;
1837 
1838  /* The MAC puts the 15.4 payload inside the packetbuf data buffer */
1840 
1841  if(packetbuf_datalen() == 0) {
1842  LOG_WARN("input: empty packet\n");
1843  return;
1844  }
1845 
1846  /* Clear uipbuf and set default attributes */
1847  uipbuf_clear();
1848 
1849  /* This is default uip_buf since we assume that this is not fragmented */
1850  buffer = (uint8_t *)UIP_IP_BUF;
1851  buffer_size = UIP_BUFSIZE;
1852 
1853  /* Save the RSSI of the incoming packet in case the upper layer will
1854  want to query us for it later. */
1855  last_rssi = (signed short)packetbuf_attr(PACKETBUF_ATTR_RSSI);
1856 
1857 #if SICSLOWPAN_CONF_FRAG
1858 
1859  /*
1860  * Since we don't support the mesh and broadcast header, the first header
1861  * we look for is the fragmentation header
1862  */
1863  switch((GET16(PACKETBUF_FRAG_PTR, PACKETBUF_FRAG_DISPATCH_SIZE) >> 8) & SICSLOWPAN_DISPATCH_FRAG_MASK) {
1864  case SICSLOWPAN_DISPATCH_FRAG1:
1865  frag_offset = 0;
1866  frag_size = GET16(PACKETBUF_FRAG_PTR, PACKETBUF_FRAG_DISPATCH_SIZE) & 0x07ff;
1867  frag_tag = GET16(PACKETBUF_FRAG_PTR, PACKETBUF_FRAG_TAG);
1868  packetbuf_hdr_len += SICSLOWPAN_FRAG1_HDR_LEN;
1869  first_fragment = 1;
1870  is_fragment = 1;
1871 
1872  LOG_INFO("input: received first element of a fragmented packet (tag %d, len %d)\n",
1873  frag_tag, frag_size);
1874 
1875  /* Add the fragment to the fragmentation context */
1876  frag_context = add_fragment(frag_tag, frag_size, frag_offset);
1877 
1878  if(frag_context == -1) {
1879  LOG_ERR("input: failed to allocate new reassembly context\n");
1880  return;
1881  }
1882 
1883  buffer = frag_info[frag_context].first_frag;
1884  buffer_size = SICSLOWPAN_FIRST_FRAGMENT_SIZE;
1885  break;
1886  case SICSLOWPAN_DISPATCH_FRAGN:
1887  /*
1888  * set offset, tag, size
1889  * Offset is in units of 8 bytes
1890  */
1891  frag_offset = PACKETBUF_FRAG_PTR[PACKETBUF_FRAG_OFFSET];
1892  frag_tag = GET16(PACKETBUF_FRAG_PTR, PACKETBUF_FRAG_TAG);
1893  frag_size = GET16(PACKETBUF_FRAG_PTR, PACKETBUF_FRAG_DISPATCH_SIZE) & 0x07ff;
1894  packetbuf_hdr_len += SICSLOWPAN_FRAGN_HDR_LEN;
1895 
1896  /* Add the fragment to the fragmentation context (this will also
1897  copy the payload) */
1898  frag_context = add_fragment(frag_tag, frag_size, frag_offset);
1899 
1900  if(frag_context == -1) {
1901  LOG_ERR("input: reassembly context not found (tag %d)\n", frag_tag);
1902  return;
1903  }
1904 
1905  /* Ok - add_fragment will store the fragment automatically - so
1906  we should not store more */
1907  buffer = NULL;
1908 
1909  if(frag_info[frag_context].reassembled_len >= frag_size) {
1910  last_fragment = 1;
1911  }
1912  is_fragment = 1;
1913  break;
1914  default:
1915  break;
1916  }
1917 
1918  if(is_fragment && !first_fragment) {
1919  /* this is a FRAGN, skip the header compression dispatch section */
1920  goto copypayload;
1921  }
1922 #endif /* SICSLOWPAN_CONF_FRAG */
1923 
1924  /* First, process 6LoRH headers */
1925  curr_page = 0;
1927  if(curr_page == 1) {
1928  LOG_INFO("input: page 1, 6LoRH\n");
1929  digest_6lorh_hdr();
1930  } else if (curr_page > 1) {
1931  LOG_ERR("input: page %u not supported\n", curr_page);
1932  return;
1933  }
1934 
1935  /* Process next dispatch and headers */
1936  if(SICSLOWPAN_COMPRESSION > SICSLOWPAN_COMPRESSION_IPV6 &&
1937  (PACKETBUF_6LO_PTR[PACKETBUF_6LO_DISPATCH] & SICSLOWPAN_DISPATCH_IPHC_MASK) == SICSLOWPAN_DISPATCH_IPHC) {
1938  LOG_DBG("uncompression: IPHC dispatch\n");
1939  uncompress_hdr_iphc(buffer, buffer_size, frag_size);
1940  } else if(PACKETBUF_6LO_PTR[PACKETBUF_6LO_DISPATCH] == SICSLOWPAN_DISPATCH_IPV6) {
1941  LOG_DBG("uncompression: IPV6 dispatch\n");
1942  packetbuf_hdr_len += SICSLOWPAN_IPV6_HDR_LEN;
1943 
1944  /* Put uncompressed IP header in sicslowpan_buf. */
1945  memcpy(buffer, packetbuf_ptr + packetbuf_hdr_len, UIP_IPH_LEN);
1946 
1947  /* Update uncomp_hdr_len and packetbuf_hdr_len. */
1948  packetbuf_hdr_len += UIP_IPH_LEN;
1949  uncomp_hdr_len += UIP_IPH_LEN;
1950  } else {
1951  LOG_ERR("uncompression: unknown dispatch: 0x%02x, or IPHC disabled\n",
1952  PACKETBUF_6LO_PTR[PACKETBUF_6LO_DISPATCH] & SICSLOWPAN_DISPATCH_IPHC_MASK);
1953  return;
1954  }
1955 
1956 #if SICSLOWPAN_CONF_FRAG
1957  copypayload:
1958 #endif /*SICSLOWPAN_CONF_FRAG*/
1959  /*
1960  * copy "payload" from the packetbuf buffer to the sicslowpan_buf
1961  * if this is a first fragment or not fragmented packet,
1962  * we have already copied the compressed headers, uncomp_hdr_len
1963  * and packetbuf_hdr_len are non 0, frag_offset is.
1964  * If this is a subsequent fragment, this is the contrary.
1965  */
1967  LOG_ERR("input: packet dropped due to header > total packet\n");
1968  return;
1969  }
1971 
1972 #if SICSLOWPAN_CONF_FRAG
1973  if(is_fragment) {
1974  LOG_INFO("input: fragment (tag %d, payload %d, offset %d) -- %u %u\n",
1975  frag_tag, packetbuf_payload_len, frag_offset << 3, packetbuf_datalen(), packetbuf_hdr_len);
1976  }
1977 #endif /*SICSLOWPAN_CONF_FRAG*/
1978 
1979  /* Sanity-check size of incoming packet to avoid buffer overflow */
1980  {
1981  int req_size = uncomp_hdr_len + (uint16_t)(frag_offset << 3)
1983  if(req_size > sizeof(uip_buf)) {
1984 #if SICSLOWPAN_CONF_FRAG
1985  LOG_ERR(
1986  "input: packet and fragment context %u dropped, minimum required IP_BUF size: %d+%d+%d=%d (current size: %u)\n",
1987  frag_context,
1988  uncomp_hdr_len, (uint16_t)(frag_offset << 3),
1989  packetbuf_payload_len, req_size, (unsigned)sizeof(uip_buf));
1990  /* Discard all fragments for this contex, as reassembling this particular fragment would
1991  * cause an overflow in uipbuf */
1992  clear_fragments(frag_context);
1993 #endif /* SICSLOWPAN_CONF_FRAG */
1994  return;
1995  }
1996  }
1997 
1998  /* copy the payload if buffer is non-null - which is only the case with first fragment
1999  or packets that are non fragmented */
2000  if(buffer != NULL) {
2001  memcpy((uint8_t *)buffer + uncomp_hdr_len, packetbuf_ptr + packetbuf_hdr_len, packetbuf_payload_len);
2002  }
2003 
2004  /* update processed_ip_in_len if fragment, sicslowpan_len otherwise */
2005 
2006 #if SICSLOWPAN_CONF_FRAG
2007  if(frag_size > 0) {
2008  /* Add the size of the header only for the first fragment. */
2009  if(first_fragment != 0) {
2010  frag_info[frag_context].reassembled_len = uncomp_hdr_len + packetbuf_payload_len;
2011  frag_info[frag_context].first_frag_len = uncomp_hdr_len + packetbuf_payload_len;
2012  }
2013  /* For the last fragment, we are OK if there is extrenous bytes at
2014  the end of the packet. */
2015  if(last_fragment != 0) {
2016  frag_info[frag_context].reassembled_len = frag_size;
2017  /* copy to uip */
2018  if(!copy_frags2uip(frag_context)) {
2019  return;
2020  }
2021  }
2022  }
2023 
2024  /*
2025  * If we have a full IP packet in sicslowpan_buf, deliver it to
2026  * the IP stack
2027  */
2028  if(!is_fragment || last_fragment) {
2029  /* packet is in uip already - just set length */
2030  if(is_fragment != 0 && last_fragment != 0) {
2031  uip_len = frag_size;
2032  } else {
2034  }
2035 #else
2037 #endif /* SICSLOWPAN_CONF_FRAG */
2038  LOG_INFO("input: received IPv6 packet with len %d\n",
2039  uip_len);
2040 
2041  if(LOG_DBG_ENABLED) {
2042  uint16_t ndx;
2043  LOG_DBG("uncompression: after (%u):", UIP_IP_BUF->len[1]);
2044  for (ndx = 0; ndx < UIP_IP_BUF->len[1] + 40; ndx++) {
2045  uint8_t data = ((uint8_t *) (UIP_IP_BUF))[ndx];
2046  LOG_DBG_("%02x", data);
2047  }
2048  LOG_DBG_("\n");
2049  }
2050 
2051  /* if callback is set then set attributes and call */
2052  if(callback) {
2053  set_packet_attrs();
2054  callback->input_callback();
2055  }
2056 
2057 #if LLSEC802154_USES_AUX_HEADER
2058  /*
2059  * Assuming that the last packet in packetbuf is containing
2060  * the LLSEC state so that it can be copied to uipbuf.
2061  */
2062  uipbuf_set_attr(UIPBUF_ATTR_LLSEC_LEVEL,
2063  packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL));
2064 #if LLSEC802154_USES_EXPLICIT_KEYS
2065  uipbuf_set_attr(UIPBUF_ATTR_LLSEC_KEY_ID,
2066  packetbuf_attr(PACKETBUF_ATTR_KEY_INDEX));
2067 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
2068 #endif /* LLSEC802154_USES_AUX_HEADER */
2069 
2070  tcpip_input();
2071 #if SICSLOWPAN_CONF_FRAG
2072  }
2073 #endif /* SICSLOWPAN_CONF_FRAG */
2074 }
2075 /** @} */
2076 
2077 /*--------------------------------------------------------------------*/
2078 /* \brief 6lowpan init function (called by the MAC layer) */
2079 /*--------------------------------------------------------------------*/
2080 void
2081 sicslowpan_init(void)
2082 {
2083 
2084 #if SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_IPHC
2085 /* Preinitialize any address contexts for better header compression
2086  * (Saves up to 13 bytes per 6lowpan packet)
2087  * The platform contiki-conf.h file can override this using e.g.
2088  * #define SICSLOWPAN_CONF_ADDR_CONTEXT_0 {addr_contexts[0].prefix[0]=0xbb;addr_contexts[0].prefix[1]=0xbb;}
2089  */
2090 #if SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0
2091  addr_contexts[0].used = 1;
2092  addr_contexts[0].number = 0;
2093 #ifdef SICSLOWPAN_CONF_ADDR_CONTEXT_0
2094  SICSLOWPAN_CONF_ADDR_CONTEXT_0;
2095 #else
2096  addr_contexts[0].prefix[0] = UIP_DS6_DEFAULT_PREFIX_0;
2097  addr_contexts[0].prefix[1] = UIP_DS6_DEFAULT_PREFIX_1;
2098 #endif
2099 #endif /* SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 0 */
2100 
2101 #if SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 1
2102  {
2103  int i;
2104  for(i = 1; i < SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS; i++) {
2105 #ifdef SICSLOWPAN_CONF_ADDR_CONTEXT_1
2106  if (i==1) {
2107  addr_contexts[1].used = 1;
2108  addr_contexts[1].number = 1;
2109  SICSLOWPAN_CONF_ADDR_CONTEXT_1;
2110 #ifdef SICSLOWPAN_CONF_ADDR_CONTEXT_2
2111  } else if (i==2) {
2112  addr_contexts[2].used = 1;
2113  addr_contexts[2].number = 2;
2114  SICSLOWPAN_CONF_ADDR_CONTEXT_2;
2115 #endif
2116  } else {
2117  addr_contexts[i].used = 0;
2118  }
2119 #else
2120  addr_contexts[i].used = 0;
2121 #endif /* SICSLOWPAN_CONF_ADDR_CONTEXT_1 */
2122  }
2123  }
2124 #endif /* SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS > 1 */
2125 
2126 #endif /* SICSLOWPAN_COMPRESSION == SICSLOWPAN_COMPRESSION_IPHC */
2127 }
2128 /*--------------------------------------------------------------------*/
2129 int
2130 sicslowpan_get_last_rssi(void)
2131 {
2132  return last_rssi;
2133 }
2134 /*--------------------------------------------------------------------*/
2135 const struct network_driver sicslowpan_driver = {
2136  "sicslowpan",
2137  sicslowpan_init,
2138  input,
2139  output
2140 };
2141 /*--------------------------------------------------------------------*/
2142 /** @} */
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:116
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:143
#define UIP_IP_BUF
Direct access to IPv6 header.
Definition: uip.h:71
static uint8_t packetbuf_hdr_len
packetbuf_hdr_len is the total length of (the processed) 6lowpan headers (fragment headers...
Definition: sicslowpan.c:164
uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:107
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
Header for the Contiki/uIP interface.
static void digest_paging_dispatch(void)
Digest 6lorh headers before IPHC.
Definition: sicslowpan.c:1415
The MAC layer transmission could not be performed because of a fatal error.
Definition: mac.h:101
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:75
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip6.c:159
static int packetbuf_payload_len
The length of the payload in the Packetbuf buffer.
Definition: sicslowpan.c:172
#define UIP_ICMP_BUF
Direct access to ICMP, UDP, and TCP headers and payload, with implicit ext header offset (global uip_...
Definition: uip.h:77
void(* link_callback)(const linkaddr_t *addr, int status, int numtx)
Called by lower layers after every packet transmission.
Definition: routing.h:170
static void send_packet(linkaddr_t *dest)
This function is called by the 6lowpan code to send out a packet.
Definition: sicslowpan.c:1502
#define UIP_PROTO_HBHO
extension headers types
Definition: uip.h:1764
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition: uipopt.h:134
static void digest_6lorh_hdr(void)
Digest 6lorh headers before IPHC.
Definition: sicslowpan.c:1429
static void packet_sent(void *ptr, int status, int transmissions)
Callback function for the MAC packet sent callback.
Definition: sicslowpan.c:1471
void uip_ds6_link_callback(int status, int numtx)
The callback function to update link-layer stats in a neighbor cache.
Definition: uip-ds6-nbr.c:534
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
Definition: uip-ds6.c:576
A timer.
Definition: timer.h:82
const linkaddr_t linkaddr_null
The null link-layer address.
static uint8_t output(const linkaddr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1576
static void uncompress_hdr_iphc(uint8_t *buf, uint16_t buf_size, uint16_t ip_len)
Uncompress IPHC (i.e., IPHC and LOWPAN_UDP) headers and put them in sicslowpan_buf.
Definition: sicslowpan.c:1073
Header file for IPv6-related data structures.
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:155
Configuration options for uIP.
#define sicslowpan_is_iid_16_bit_compressable(a)
check whether we can compress the IID in address &#39;a&#39; to 16 bits.
Definition: sicslowpan.h:257
static int mac_max_payload
mac_max_payload is the maimum payload space on the MAC frame.
Definition: sicslowpan.c:183
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
#define uip_is_addr_mac_addr_based(a, m)
was addr (a) forged based on the mac address m a type is uip_ipaddr_t m type is uiplladdr_t ...
Definition: uip.h:1934
static void add_paging_dispatch(uint8_t page)
Adds Paging dispatch byte.
Definition: sicslowpan.c:1393
Header file for the Packet queue buffer management
The MAC layer did not get an acknowledgement for the packet.
Definition: mac.h:91
#define SICSLOWPAN_COMPRESSION
Do we compress the IP header or not.
Definition: uipopt.h:543
static uint8_t uncomp_hdr_len
uncomp_hdr_len is the length of the headers before compression (if HC2 is used this includes the UDP ...
Definition: sicslowpan.c:178
The MAC layer transmission was OK.
Definition: mac.h:87
static int last_tx_status
the result of the last transmitted fragment
Definition: sicslowpan.c:193
The structure of a network driver in Contiki.
Definition: netstack.h:117
static uint8_t curr_page
The current page (RFC 4944)
Definition: sicslowpan.c:188
#define SICSLOWPAN_REASS_MAXAGE
Timeout for packet reassembly at the 6lowpan layer (should be < 60s)
Definition: uipopt.h:536
static uint8_t * hc06_ptr
pointer to the byte where to write next inline field.
Definition: sicslowpan.c:520
#define uip_is_addr_unspecified(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
Definition: uip.h:1836
Routing driver header file
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition: uip.h:510
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:123
The header for fragments.
Definition: sicslowpan.h:239
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition: linkaddr.c:63
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition: uip.h:1961
#define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS
If we use IPHC compression, how many address contexts do we support.
Definition: uipopt.h:552
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1223
void(* send)(mac_callback_t sent_callback, void *ptr)
Send a packet from the packetbuf.
Definition: mac.h:69
Header file for the uIP TCP/IP stack.
static int compress_hdr_iphc(linkaddr_t *link_destaddr)
Compress IP/UDP header.
Definition: sicslowpan.c:686
static void add_6lorh_hdr(void)
Adds 6lorh headers before IPHC.
Definition: sicslowpan.c:1404
Header file for the Packet buffer (packetbuf) management
Include file for the Contiki low-layer network stack (NETSTACK)
int(* max_payload)(void)
Read out estimated max payload size based on payload in packetbuf.
Definition: mac.h:81
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:85
Header file for the 6lowpan implementation (RFC4944 and draft-hui-6lowpan-hc-01) ...
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
Definition: uip.h:1877
Header file for the logging system
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1813
static struct sicslowpan_addr_context * addr_context_lookup_by_number(uint8_t number)
find the context with the given number
Definition: sicslowpan.c:574
static struct sicslowpan_addr_context * addr_context_lookup_by_prefix(uip_ipaddr_t *ipaddr)
find the context corresponding to prefix ipaddr
Definition: sicslowpan.c:557
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:136
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
Definition: tcpip.c:445
static uint8_t * packetbuf_ptr
A pointer to the packetbuf buffer.
Definition: sicslowpan.c:157
static struct sicslowpan_addr_context * context
Addresses contexts for IPHC.
Definition: sicslowpan.c:517