Contiki-NG
sicslowpan.h
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  * Header file for the 6lowpan implementation
36  * (RFC4944 and draft-hui-6lowpan-hc-01)
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  */
43 
44 /**
45  * \addtogroup sicslowpan
46  * @{
47  */
48 
49 #ifndef SICSLOWPAN_H_
50 #define SICSLOWPAN_H_
51 
52 #include "net/ipv6/uip.h"
53 #include "net/mac/mac.h"
54 
55 /**
56  * \name General sicslowpan defines
57  * @{
58  */
59 /* Min and Max compressible UDP ports - HC06 */
60 #define SICSLOWPAN_UDP_4_BIT_PORT_MIN 0xF0B0
61 #define SICSLOWPAN_UDP_4_BIT_PORT_MAX 0xF0BF /* F0B0 + 15 */
62 #define SICSLOWPAN_UDP_8_BIT_PORT_MIN 0xF000
63 #define SICSLOWPAN_UDP_8_BIT_PORT_MAX 0xF0FF /* F000 + 255 */
64 
65 /** @} */
66 
67 /**
68  * \name 6lowpan compressions
69  * @{
70  */
71 #define SICSLOWPAN_COMPRESSION_IPV6 0 /* No compression */
72 #define SICSLOWPAN_COMPRESSION_IPHC 1 /* RFC 6282 */
73 #define SICSLOWPAN_COMPRESSION_6LORH 2 /* RFC 8025 for paging dispatch,
74  * draft-ietf-6lo-routin-dispatch-05 for 6LoRH. 6LoRH is not
75  * implemented yet -- only support for paging dispatch. */
76 /** @} */
77 
78 /**
79  * \name 6lowpan dispatches
80  * @{
81  */
82 #define SICSLOWPAN_DISPATCH_IPV6 0x41 /* 01000001 = 65 */
83 #define SICSLOWPAN_DISPATCH_HC1 0x42 /* 01000010 = 66 */
84 #define SICSLOWPAN_DISPATCH_IPHC 0x60 /* 011xxxxx = ... */
85 #define SICSLOWPAN_DISPATCH_IPHC_MASK 0xe0
86 #define SICSLOWPAN_DISPATCH_FRAG1 0xc0 /* 11000xxx */
87 #define SICSLOWPAN_DISPATCH_FRAGN 0xe0 /* 11100xxx */
88 #define SICSLOWPAN_DISPATCH_FRAG_MASK 0xf8
89 #define SICSLOWPAN_DISPATCH_PAGING 0xf0 /* 1111xxxx */
90 #define SICSLOWPAN_DISPATCH_PAGING_MASK 0xf0
91 /** @} */
92 
93 /** \name HC1 encoding
94  * @{
95  */
96 #define SICSLOWPAN_HC1_NH_UDP 0x02
97 #define SICSLOWPAN_HC1_NH_TCP 0x06
98 #define SICSLOWPAN_HC1_NH_ICMP6 0x04
99 /** @} */
100 
101 /** \name HC_UDP encoding (works together with HC1)
102  * @{
103  */
104 #define SICSLOWPAN_HC_UDP_ALL_C 0xE0
105 /** @} */
106 
107 /**
108  * \name IPHC encoding
109  * @{
110  */
111 /*
112  * Values of fields within the IPHC encoding first byte
113  * (C stands for compressed and I for inline)
114  */
115 #define SICSLOWPAN_IPHC_FL_C 0x10
116 #define SICSLOWPAN_IPHC_TC_C 0x08
117 #define SICSLOWPAN_IPHC_NH_C 0x04
118 #define SICSLOWPAN_IPHC_TTL_1 0x01
119 #define SICSLOWPAN_IPHC_TTL_64 0x02
120 #define SICSLOWPAN_IPHC_TTL_255 0x03
121 #define SICSLOWPAN_IPHC_TTL_I 0x00
122 
123 
124 /* Values of fields within the IPHC encoding second byte */
125 #define SICSLOWPAN_IPHC_CID 0x80
126 
127 #define SICSLOWPAN_IPHC_SAC 0x40
128 #define SICSLOWPAN_IPHC_SAM_00 0x00
129 #define SICSLOWPAN_IPHC_SAM_01 0x10
130 #define SICSLOWPAN_IPHC_SAM_10 0x20
131 #define SICSLOWPAN_IPHC_SAM_11 0x30
132 
133 #define SICSLOWPAN_IPHC_SAM_BIT 4
134 
135 #define SICSLOWPAN_IPHC_M 0x08
136 #define SICSLOWPAN_IPHC_DAC 0x04
137 #define SICSLOWPAN_IPHC_DAM_00 0x00
138 #define SICSLOWPAN_IPHC_DAM_01 0x01
139 #define SICSLOWPAN_IPHC_DAM_10 0x02
140 #define SICSLOWPAN_IPHC_DAM_11 0x03
141 
142 #define SICSLOWPAN_IPHC_DAM_BIT 0
143 
144 /* Link local context number */
145 #define SICSLOWPAN_IPHC_ADDR_CONTEXT_LL 0
146 /* 16-bit multicast addresses compression */
147 #define SICSLOWPAN_IPHC_MCAST_RANGE 0xA0
148 /** @} */
149 
150 /* NHC_EXT_HDR */
151 #define SICSLOWPAN_NHC_MASK 0xF0
152 #define SICSLOWPAN_NHC_EXT_HDR 0xE0
153 #define SICSLOWPAN_NHC_BIT 0x01
154 
155 /* The header values */
156 #define SICSLOWPAN_NHC_ETX_HDR_HBHO 0x00
157 #define SICSLOWPAN_NHC_ETX_HDR_ROUTING 0x01
158 #define SICSLOWPAN_NHC_ETX_HDR_FRAG 0x02
159 #define SICSLOWPAN_NHC_ETX_HDR_DESTO 0x03
160 #define SICSLOWPAN_NHC_ETX_HDR_MOH 0x04
161 #define SICSLOWPAN_NHC_ETX_HDR_IPV6 0x07
162 
163 /**
164  * \name LOWPAN_UDP encoding (works together with IPHC)
165  * @{
166  */
167 /**
168  * \name LOWPAN_UDP encoding (works together with IPHC)
169  * @{
170  */
171 #define SICSLOWPAN_NHC_UDP_MASK 0xF8
172 #define SICSLOWPAN_NHC_UDP_ID 0xF0
173 #define SICSLOWPAN_NHC_UDP_CHECKSUMC 0x04
174 #define SICSLOWPAN_NHC_UDP_CHECKSUMI 0x00
175 /* values for port compression, _with checksum_ ie bit 5 set to 0 */
176 #define SICSLOWPAN_NHC_UDP_CS_P_00 0xF0 /* all inline */
177 #define SICSLOWPAN_NHC_UDP_CS_P_01 0xF1 /* source 16bit inline, dest = 0xF0 + 8 bit inline */
178 #define SICSLOWPAN_NHC_UDP_CS_P_10 0xF2 /* source = 0xF0 + 8bit inline, dest = 16 bit inline */
179 #define SICSLOWPAN_NHC_UDP_CS_P_11 0xF3 /* source & dest = 0xF0B + 4bit inline */
180 /** @} */
181 
182 
183 /**
184  * \name The 6lowpan "headers" length
185  * @{
186  */
187 
188 #define SICSLOWPAN_IPV6_HDR_LEN 1 /*one byte*/
189 #define SICSLOWPAN_HC1_HDR_LEN 3
190 #define SICSLOWPAN_HC1_HC_UDP_HDR_LEN 7
191 #define SICSLOWPAN_FRAG1_HDR_LEN 4
192 #define SICSLOWPAN_FRAGN_HDR_LEN 5
193 /** @} */
194 
195 /**
196  * \brief The header for fragments
197  * \note We do not define different structures for FRAG1
198  * and FRAGN headers, which are different. For FRAG1, the
199  * offset field is just not used
200  */
201 /* struct sicslowpan_frag_hdr { */
202 /* uint16_t dispatch_size; */
203 /* uint16_t tag; */
204 /* uint8_t offset; */
205 /* }; */
206 
207 /**
208  * \brief The HC1 header when HC_UDP is not used
209  *
210  * When all fields are compressed and HC_UDP is not used,
211  * we use this structure. If HC_UDP is used, the ttl is
212  * in another spot, and we use the sicslowpan_hc1_hc_udp
213  * structure
214  */
215 /* struct sicslowpan_hc1_hdr { */
216 /* uint8_t dispatch; */
217 /* uint8_t encoding; */
218 /* uint8_t ttl; */
219 /* }; */
220 
221 /**
222  * \brief HC1 followed by HC_UDP
223  */
224 /* struct sicslowpan_hc1_hc_udp_hdr { */
225 /* uint8_t dispatch; */
226 /* uint8_t hc1_encoding; */
227 /* uint8_t hc_udp_encoding; */
228 /* uint8_t ttl; */
229 /* uint8_t ports; */
230 /* uint16_t udpchksum; */
231 /* }; */
232 
233 /**
234  * \brief An address context for IPHC address compression
235  * each context can have upto 8 bytes
236  */
238  uint8_t used; /* possibly use as prefix-length */
239  uint8_t number;
240  uint8_t prefix[8];
241 };
242 
243 /**
244  * \name Address compressibility test functions
245  * @{
246  */
247 
248 /**
249  * \brief check whether we can compress the IID in
250  * address 'a' to 16 bits.
251  * This is used for unicast addresses only, and is true
252  * if the address is on the format <PREFIX>::0000:00ff:fe00:XXXX
253  * NOTE: we currently assume 64-bits prefixes
254  */
255 #define sicslowpan_is_iid_16_bit_compressable(a) \
256  ((((a)->u16[4]) == 0) && \
257  (((a)->u8[10]) == 0)&& \
258  (((a)->u8[11]) == 0xff)&& \
259  (((a)->u8[12]) == 0xfe)&& \
260  (((a)->u8[13]) == 0))
261 
262 /**
263  * \brief check whether the 9-bit group-id of the
264  * compressed multicast address is known. It is true
265  * if the 9-bit group is the all nodes or all routers
266  * group.
267  * \param a is typed uint8_t *
268  */
269 #define sicslowpan_is_mcast_addr_decompressable(a) \
270  (((*a & 0x01) == 0) && \
271  ((*(a + 1) == 0x01) || (*(a + 1) == 0x02)))
272 
273 /**
274  * \brief check whether the 112-bit group-id of the
275  * multicast address is mappable to a 9-bit group-id
276  * It is true if the group is the all nodes or all
277  * routers group.
278 */
279 #define sicslowpan_is_mcast_addr_compressable(a) \
280  ((((a)->u16[1]) == 0) && \
281  (((a)->u16[2]) == 0) && \
282  (((a)->u16[3]) == 0) && \
283  (((a)->u16[4]) == 0) && \
284  (((a)->u16[5]) == 0) && \
285  (((a)->u16[6]) == 0) && \
286  (((a)->u8[14]) == 0) && \
287  ((((a)->u8[15]) == 1) || (((a)->u8[15]) == 2)))
288 
289 /* FFXX::00XX:XXXX:XXXX */
290 #define sicslowpan_is_mcast_addr_compressable48(a) \
291  ((((a)->u16[1]) == 0) && \
292  (((a)->u16[2]) == 0) && \
293  (((a)->u16[3]) == 0) && \
294  (((a)->u16[4]) == 0) && \
295  (((a)->u8[10]) == 0))
296 
297 /* FFXX::00XX:XXXX */
298 #define sicslowpan_is_mcast_addr_compressable32(a) \
299  ((((a)->u16[1]) == 0) && \
300  (((a)->u16[2]) == 0) && \
301  (((a)->u16[3]) == 0) && \
302  (((a)->u16[4]) == 0) && \
303  (((a)->u16[5]) == 0) && \
304  (((a)->u8[12]) == 0))
305 
306 /* FF02::00XX */
307 #define sicslowpan_is_mcast_addr_compressable8(a) \
308  ((((a)->u8[1]) == 2) && \
309  (((a)->u16[1]) == 0) && \
310  (((a)->u16[2]) == 0) && \
311  (((a)->u16[3]) == 0) && \
312  (((a)->u16[4]) == 0) && \
313  (((a)->u16[5]) == 0) && \
314  (((a)->u16[6]) == 0) && \
315  (((a)->u8[14]) == 0))
316 
317 /** @} */
318 
319 /**
320  * The structure of a next header compressor.
321  *
322  * TODO: needs more parameters when compressing extension headers, etc.
323  */
325  int (* is_compressable)(uint8_t next_header);
326 
327  /** compress next header (TCP/UDP, etc) - ptr points to next header to
328  compress */
329  int (* compress)(uint8_t *compressed, uint8_t *uncompressed_len);
330 
331  /** uncompress next header (TCP/UDP, etc) - ptr points to next header to
332  uncompress */
333  int (* uncompress)(uint8_t *compressed, uint8_t *lowpanbuf, uint8_t *uncompressed_len);
334 
335 };
336 
337 int sicslowpan_get_last_rssi(void);
338 
339 extern const struct network_driver sicslowpan_driver;
340 
341 #endif /* SICSLOWPAN_H_ */
342 /** @} */
The structure of a next header compressor.
Definition: sicslowpan.h:324
The structure of a network driver in Contiki.
Definition: netstack.h:117
The header for fragments.
Definition: sicslowpan.h:237
Header file for the uIP TCP/IP stack.
MAC driver header file