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