Contiki 2.5
sicslowpan.h
Go to the documentation of this file.
1 /**
2  * \addtogroup sicslowpan
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2008, Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the Institute nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * This file is part of the Contiki operating system.
35  *
36  * $Id: sicslowpan.h,v 1.13 2010/03/26 10:28:51 joxe Exp $
37  */
38 /**
39  * \file
40  * Header file for the 6lowpan implementation
41  * (RFC4944 and draft-hui-6lowpan-hc-01)
42  * \author Adam Dunkels <adam@sics.se>
43  * \author Nicolas Tsiftes <nvt@sics.se>
44  * \author Niclas Finne <nfi@sics.se>
45  * \author Mathilde Durvy <mdurvy@cisco.com>
46  * \author Julien Abeille <jabeille@cisco.com>
47  */
48 
49 #ifndef __SICSLOWPAN_H__
50 #define __SICSLOWPAN_H__
51 #include "net/uip.h"
52 #include "net/mac/mac.h"
53 
54 /**
55  * \name General sicslowpan defines
56  * @{
57  */
58 /* Min and Max compressible UDP ports - HC06 */
59 #define SICSLOWPAN_UDP_4_BIT_PORT_MIN 0xF0B0
60 #define SICSLOWPAN_UDP_4_BIT_PORT_MAX 0xF0BF /* F0B0 + 15 */
61 #define SICSLOWPAN_UDP_8_BIT_PORT_MIN 0xF000
62 #define SICSLOWPAN_UDP_8_BIT_PORT_MAX 0xF0FF /* F000 + 255 */
63 
64 /** @} */
65 
66 /**
67  * \name 6lowpan compressions
68  * @{
69  */
70 #define SICSLOWPAN_COMPRESSION_IPV6 0
71 #define SICSLOWPAN_COMPRESSION_HC1 1
72 #define SICSLOWPAN_COMPRESSION_HC06 2
73 /** @} */
74 
75 /**
76  * \name 6lowpan dispatches
77  * @{
78  */
79 #define SICSLOWPAN_DISPATCH_IPV6 0x41 /* 01000001 = 65 */
80 #define SICSLOWPAN_DISPATCH_HC1 0x42 /* 01000010 = 66 */
81 #define SICSLOWPAN_DISPATCH_IPHC 0x60 /* 011xxxxx = ... */
82 #define SICSLOWPAN_DISPATCH_FRAG1 0xc0 /* 11000xxx */
83 #define SICSLOWPAN_DISPATCH_FRAGN 0xe0 /* 11100xxx */
84 /** @} */
85 
86 /** \name HC1 encoding
87  * @{
88  */
89 #define SICSLOWPAN_HC1_NH_UDP 0x02
90 #define SICSLOWPAN_HC1_NH_TCP 0x06
91 #define SICSLOWPAN_HC1_NH_ICMP6 0x04
92 /** @} */
93 
94 /** \name HC_UDP encoding (works together with HC1)
95  * @{
96  */
97 #define SICSLOWPAN_HC_UDP_ALL_C 0xE0
98 /** @} */
99 
100 /**
101  * \name IPHC encoding
102  * @{
103  */
104 /*
105  * Values of fields within the IPHC encoding first byte
106  * (C stands for compressed and I for inline)
107  */
108 #define SICSLOWPAN_IPHC_FL_C 0x10
109 #define SICSLOWPAN_IPHC_TC_C 0x08
110 #define SICSLOWPAN_IPHC_NH_C 0x04
111 #define SICSLOWPAN_IPHC_TTL_1 0x01
112 #define SICSLOWPAN_IPHC_TTL_64 0x02
113 #define SICSLOWPAN_IPHC_TTL_255 0x03
114 #define SICSLOWPAN_IPHC_TTL_I 0x00
115 
116 
117 /* Values of fields within the IPHC encoding second byte */
118 #define SICSLOWPAN_IPHC_CID 0x80
119 
120 #define SICSLOWPAN_IPHC_SAC 0x40
121 #define SICSLOWPAN_IPHC_SAM_00 0x00
122 #define SICSLOWPAN_IPHC_SAM_01 0x10
123 #define SICSLOWPAN_IPHC_SAM_10 0x20
124 #define SICSLOWPAN_IPHC_SAM_11 0x30
125 
126 #define SICSLOWPAN_IPHC_SAM_BIT 4
127 
128 #define SICSLOWPAN_IPHC_M 0x08
129 #define SICSLOWPAN_IPHC_DAC 0x04
130 #define SICSLOWPAN_IPHC_DAM_00 0x00
131 #define SICSLOWPAN_IPHC_DAM_01 0x01
132 #define SICSLOWPAN_IPHC_DAM_10 0x02
133 #define SICSLOWPAN_IPHC_DAM_11 0x03
134 
135 #define SICSLOWPAN_IPHC_DAM_BIT 0
136 
137 /* Link local context number */
138 #define SICSLOWPAN_IPHC_ADDR_CONTEXT_LL 0
139 /* 16-bit multicast addresses compression */
140 #define SICSLOWPAN_IPHC_MCAST_RANGE 0xA0
141 /** @} */
142 
143 /* NHC_EXT_HDR */
144 #define SICSLOWPAN_NHC_MASK 0xF0
145 #define SICSLOWPAN_NHC_EXT_HDR 0xE0
146 
147 /**
148  * \name LOWPAN_UDP encoding (works together with IPHC)
149  * @{
150  */
151 /**
152  * \name LOWPAN_UDP encoding (works together with IPHC)
153  * @{
154  */
155 #define SICSLOWPAN_NHC_UDP_MASK 0xF8
156 #define SICSLOWPAN_NHC_UDP_ID 0xF0
157 #define SICSLOWPAN_NHC_UDP_CHECKSUMC 0x04
158 #define SICSLOWPAN_NHC_UDP_CHECKSUMI 0x00
159 /* values for port compression, _with checksum_ ie bit 5 set to 0 */
160 #define SICSLOWPAN_NHC_UDP_CS_P_00 0xF0 /* all inline */
161 #define SICSLOWPAN_NHC_UDP_CS_P_01 0xF1 /* source 16bit inline, dest = 0xF0 + 8 bit inline */
162 #define SICSLOWPAN_NHC_UDP_CS_P_10 0xF2 /* source = 0xF0 + 8bit inline, dest = 16 bit inline */
163 #define SICSLOWPAN_NHC_UDP_CS_P_11 0xF3 /* source & dest = 0xF0B + 4bit inline */
164 /** @} */
165 
166 
167 /**
168  * \name The 6lowpan "headers" length
169  * @{
170  */
171 
172 #define SICSLOWPAN_IPV6_HDR_LEN 1 /*one byte*/
173 #define SICSLOWPAN_HC1_HDR_LEN 3
174 #define SICSLOWPAN_HC1_HC_UDP_HDR_LEN 7
175 #define SICSLOWPAN_FRAG1_HDR_LEN 4
176 #define SICSLOWPAN_FRAGN_HDR_LEN 5
177 /** @} */
178 
179 /**
180  * \brief The header for fragments
181  * \note We do not define different structures for FRAG1
182  * and FRAGN headers, which are different. For FRAG1, the
183  * offset field is just not used
184  */
185 /* struct sicslowpan_frag_hdr { */
186 /* u16_t dispatch_size; */
187 /* u16_t tag; */
188 /* u8_t offset; */
189 /* }; */
190 
191 /**
192  * \brief The HC1 header when HC_UDP is not used
193  *
194  * When all fields are compressed and HC_UDP is not used,
195  * we use this structure. If HC_UDP is used, the ttl is
196  * in another spot, and we use the sicslowpan_hc1_hc_udp
197  * structure
198  */
199 /* struct sicslowpan_hc1_hdr { */
200 /* u8_t dispatch; */
201 /* u8_t encoding; */
202 /* u8_t ttl; */
203 /* }; */
204 
205 /**
206  * \brief HC1 followed by HC_UDP
207  */
208 /* struct sicslowpan_hc1_hc_udp_hdr { */
209 /* u8_t dispatch; */
210 /* u8_t hc1_encoding; */
211 /* u8_t hc_udp_encoding; */
212 /* u8_t ttl; */
213 /* u8_t ports; */
214 /* u16_t udpchksum; */
215 /* }; */
216 
217 /**
218  * \brief An address context for IPHC address compression
219  * each context can have upto 8 bytes
220  */
222  u8_t used; /* possibly use as prefix-length */
223  u8_t number;
224  u8_t prefix[8];
225 };
226 
227 /**
228  * \name Address compressibility test functions
229  * @{
230  */
231 
232 /**
233  * \brief check whether we can compress the IID in
234  * address 'a' to 16 bits.
235  * This is used for unicast addresses only, and is true
236  * if the address is on the format <PREFIX>::0000:00ff:fe00:XXXX
237  * NOTE: we currently assume 64-bits prefixes
238  */
239 #define sicslowpan_is_iid_16_bit_compressable(a) \
240  ((((a)->u16[4]) == 0) && \
241  (((a)->u8[10]) == 0)&& \
242  (((a)->u8[11]) == 0xff)&& \
243  (((a)->u8[12]) == 0xfe)&& \
244  (((a)->u8[13]) == 0))
245 
246 /**
247  * \brief check whether the 9-bit group-id of the
248  * compressed multicast address is known. It is true
249  * if the 9-bit group is the all nodes or all routers
250  * group.
251  * \param a is typed u8_t *
252  */
253 #define sicslowpan_is_mcast_addr_decompressable(a) \
254  (((*a & 0x01) == 0) && \
255  ((*(a + 1) == 0x01) || (*(a + 1) == 0x02)))
256 
257 /**
258  * \brief check whether the 112-bit group-id of the
259  * multicast address is mappable to a 9-bit group-id
260  * It is true if the group is the all nodes or all
261  * routers group.
262 */
263 #define sicslowpan_is_mcast_addr_compressable(a) \
264  ((((a)->u16[1]) == 0) && \
265  (((a)->u16[2]) == 0) && \
266  (((a)->u16[3]) == 0) && \
267  (((a)->u16[4]) == 0) && \
268  (((a)->u16[5]) == 0) && \
269  (((a)->u16[6]) == 0) && \
270  (((a)->u8[14]) == 0) && \
271  ((((a)->u8[15]) == 1) || (((a)->u8[15]) == 2)))
272 
273 /* FFXX::00XX:XXXX:XXXX */
274 #define sicslowpan_is_mcast_addr_compressable48(a) \
275  ((((a)->u16[1]) == 0) && \
276  (((a)->u16[2]) == 0) && \
277  (((a)->u16[3]) == 0) && \
278  (((a)->u16[4]) == 0) && \
279  (((a)->u8[10]) == 0))
280 
281 /* FFXX::00XX:XXXX */
282 #define sicslowpan_is_mcast_addr_compressable32(a) \
283  ((((a)->u16[1]) == 0) && \
284  (((a)->u16[2]) == 0) && \
285  (((a)->u16[3]) == 0) && \
286  (((a)->u16[4]) == 0) && \
287  (((a)->u16[5]) == 0) && \
288  (((a)->u8[12]) == 0))
289 
290 /* FF02::00XX */
291 #define sicslowpan_is_mcast_addr_compressable8(a) \
292  ((((a)->u8[1]) == 2) && \
293  (((a)->u16[1]) == 0) && \
294  (((a)->u16[2]) == 0) && \
295  (((a)->u16[3]) == 0) && \
296  (((a)->u16[4]) == 0) && \
297  (((a)->u16[5]) == 0) && \
298  (((a)->u16[6]) == 0) && \
299  (((a)->u8[14]) == 0))
300 
301 /** @} */
302 
303 /**
304  * The structure of a next header compressor.
305  *
306  * TODO: needs more parameters when compressing extension headers, etc.
307  */
309  int (* is_compressable)(uint8_t next_header);
310 
311  /** compress next header (TCP/UDP, etc) - ptr points to next header to
312  compress */
313  int (* compress)(uint8_t *compressed, uint8_t *uncompressed_len);
314 
315  /** uncompress next header (TCP/UDP, etc) - ptr points to next header to
316  uncompress */
317  int (* uncompress)(uint8_t *compressed, uint8_t *lowpanbuf, uint8_t *uncompressed_len);
318 
319 };
320 
321 
322 extern const struct network_driver sicslowpan_driver;
323 
324 extern const struct mac_driver *sicslowpan_mac;
325 #endif /* __SICSLOWPAN_H__ */
326 /** @} */