1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53#ifndef __6LOWPAN_H__
54#define __6LOWPAN_H__
55
56#include <net/ipv6.h>
57#include <net/net_namespace.h>
58
59#define UIP_802154_SHORTADDR_LEN 2
60#define UIP_IPH_LEN 40
61#define UIP_PROTO_UDP 17
62#define UIP_FRAGH_LEN 8
63
64
65
66
67
68#define is_addr_mac_addr_based(a, m) \
69 ((((a)->s6_addr[8]) == (((m)[0]) ^ 0x02)) && \
70 (((a)->s6_addr[9]) == (m)[1]) && \
71 (((a)->s6_addr[10]) == (m)[2]) && \
72 (((a)->s6_addr[11]) == (m)[3]) && \
73 (((a)->s6_addr[12]) == (m)[4]) && \
74 (((a)->s6_addr[13]) == (m)[5]) && \
75 (((a)->s6_addr[14]) == (m)[6]) && \
76 (((a)->s6_addr[15]) == (m)[7]))
77
78
79
80
81
82#define lowpan_is_iid_16_bit_compressable(a) \
83 ((((a)->s6_addr16[4]) == 0) && \
84 (((a)->s6_addr[10]) == 0) && \
85 (((a)->s6_addr[11]) == 0xff) && \
86 (((a)->s6_addr[12]) == 0xfe) && \
87 (((a)->s6_addr[13]) == 0))
88
89
90
91
92#define lowpan_is_mcast_addr_compressable48(a) \
93 ((((a)->s6_addr16[1]) == 0) && \
94 (((a)->s6_addr16[2]) == 0) && \
95 (((a)->s6_addr16[3]) == 0) && \
96 (((a)->s6_addr16[4]) == 0) && \
97 (((a)->s6_addr[10]) == 0))
98
99
100#define lowpan_is_mcast_addr_compressable32(a) \
101 ((((a)->s6_addr16[1]) == 0) && \
102 (((a)->s6_addr16[2]) == 0) && \
103 (((a)->s6_addr16[3]) == 0) && \
104 (((a)->s6_addr16[4]) == 0) && \
105 (((a)->s6_addr16[5]) == 0) && \
106 (((a)->s6_addr[12]) == 0))
107
108
109#define lowpan_is_mcast_addr_compressable8(a) \
110 ((((a)->s6_addr[1]) == 2) && \
111 (((a)->s6_addr16[1]) == 0) && \
112 (((a)->s6_addr16[2]) == 0) && \
113 (((a)->s6_addr16[3]) == 0) && \
114 (((a)->s6_addr16[4]) == 0) && \
115 (((a)->s6_addr16[5]) == 0) && \
116 (((a)->s6_addr16[6]) == 0) && \
117 (((a)->s6_addr[14]) == 0))
118
119#define lowpan_is_addr_broadcast(a) \
120 ((((a)[0]) == 0xFF) && \
121 (((a)[1]) == 0xFF) && \
122 (((a)[2]) == 0xFF) && \
123 (((a)[3]) == 0xFF) && \
124 (((a)[4]) == 0xFF) && \
125 (((a)[5]) == 0xFF) && \
126 (((a)[6]) == 0xFF) && \
127 (((a)[7]) == 0xFF))
128
129#define LOWPAN_DISPATCH_IPV6 0x41
130#define LOWPAN_DISPATCH_HC1 0x42
131#define LOWPAN_DISPATCH_IPHC 0x60
132#define LOWPAN_DISPATCH_FRAG1 0xc0
133#define LOWPAN_DISPATCH_FRAGN 0xe0
134
135#define LOWPAN_DISPATCH_MASK 0xf8
136
137#define LOWPAN_FRAG_TIMEOUT (HZ * 60)
138
139#define LOWPAN_FRAG1_HEAD_SIZE 0x4
140#define LOWPAN_FRAGN_HEAD_SIZE 0x5
141
142
143
144
145
146#define LOWPAN_IPHC_TF 0x18
147
148#define LOWPAN_IPHC_FL_C 0x10
149#define LOWPAN_IPHC_TC_C 0x08
150#define LOWPAN_IPHC_NH_C 0x04
151#define LOWPAN_IPHC_TTL_1 0x01
152#define LOWPAN_IPHC_TTL_64 0x02
153#define LOWPAN_IPHC_TTL_255 0x03
154#define LOWPAN_IPHC_TTL_I 0x00
155
156
157
158#define LOWPAN_IPHC_CID 0x80
159
160#define LOWPAN_IPHC_ADDR_00 0x00
161#define LOWPAN_IPHC_ADDR_01 0x01
162#define LOWPAN_IPHC_ADDR_02 0x02
163#define LOWPAN_IPHC_ADDR_03 0x03
164
165#define LOWPAN_IPHC_SAC 0x40
166#define LOWPAN_IPHC_SAM 0x30
167
168#define LOWPAN_IPHC_SAM_BIT 4
169
170#define LOWPAN_IPHC_M 0x08
171#define LOWPAN_IPHC_DAC 0x04
172#define LOWPAN_IPHC_DAM_00 0x00
173#define LOWPAN_IPHC_DAM_01 0x01
174#define LOWPAN_IPHC_DAM_10 0x02
175#define LOWPAN_IPHC_DAM_11 0x03
176
177#define LOWPAN_IPHC_DAM_BIT 0
178
179
180
181#define LOWPAN_NHC_UDP_MASK 0xF8
182#define LOWPAN_NHC_UDP_ID 0xF0
183#define LOWPAN_NHC_UDP_CHECKSUMC 0x04
184#define LOWPAN_NHC_UDP_CHECKSUMI 0x00
185
186#define LOWPAN_NHC_UDP_4BIT_PORT 0xF0B0
187#define LOWPAN_NHC_UDP_4BIT_MASK 0xFFF0
188#define LOWPAN_NHC_UDP_8BIT_PORT 0xF000
189#define LOWPAN_NHC_UDP_8BIT_MASK 0xFF00
190
191
192#define LOWPAN_NHC_UDP_CS_P_00 0xF0
193#define LOWPAN_NHC_UDP_CS_P_01 0xF1
194
195#define LOWPAN_NHC_UDP_CS_P_10 0xF2
196
197#define LOWPAN_NHC_UDP_CS_P_11 0xF3
198#define LOWPAN_NHC_UDP_CS_C 0x04
199
200#ifdef DEBUG
201
202static inline void raw_dump_inline(const char *caller, char *msg,
203 unsigned char *buf, int len)
204{
205 if (msg)
206 pr_debug("%s():%s: ", caller, msg);
207
208 print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, buf, len, false);
209}
210
211
212
213
214
215
216
217static inline void raw_dump_table(const char *caller, char *msg,
218 unsigned char *buf, int len)
219{
220 if (msg)
221 pr_debug("%s():%s:\n", caller, msg);
222
223 print_hex_dump_debug("\t", DUMP_PREFIX_OFFSET, 16, 1, buf, len, false);
224}
225#else
226static inline void raw_dump_table(const char *caller, char *msg,
227 unsigned char *buf, int len) { }
228static inline void raw_dump_inline(const char *caller, char *msg,
229 unsigned char *buf, int len) { }
230#endif
231
232static inline int lowpan_fetch_skb_u8(struct sk_buff *skb, u8 *val)
233{
234 if (unlikely(!pskb_may_pull(skb, 1)))
235 return -EINVAL;
236
237 *val = skb->data[0];
238 skb_pull(skb, 1);
239
240 return 0;
241}
242
243static inline bool lowpan_fetch_skb(struct sk_buff *skb,
244 void *data, const unsigned int len)
245{
246 if (unlikely(!pskb_may_pull(skb, len)))
247 return true;
248
249 skb_copy_from_linear_data(skb, data, len);
250 skb_pull(skb, len);
251
252 return false;
253}
254
255static inline void lowpan_push_hc_data(u8 **hc_ptr, const void *data,
256 const size_t len)
257{
258 memcpy(*hc_ptr, data, len);
259 *hc_ptr += len;
260}
261
262static inline u8 lowpan_addr_mode_size(const u8 addr_mode)
263{
264 static const u8 addr_sizes[] = {
265 [LOWPAN_IPHC_ADDR_00] = 16,
266 [LOWPAN_IPHC_ADDR_01] = 8,
267 [LOWPAN_IPHC_ADDR_02] = 2,
268 [LOWPAN_IPHC_ADDR_03] = 0,
269 };
270 return addr_sizes[addr_mode];
271}
272
273static inline u8 lowpan_next_hdr_size(const u8 h_enc, u16 *uncomp_header)
274{
275 u8 ret = 1;
276
277 if ((h_enc & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
278 *uncomp_header += sizeof(struct udphdr);
279
280 switch (h_enc & LOWPAN_NHC_UDP_CS_P_11) {
281 case LOWPAN_NHC_UDP_CS_P_00:
282 ret += 4;
283 break;
284 case LOWPAN_NHC_UDP_CS_P_01:
285 case LOWPAN_NHC_UDP_CS_P_10:
286 ret += 3;
287 break;
288 case LOWPAN_NHC_UDP_CS_P_11:
289 ret++;
290 break;
291 default:
292 break;
293 }
294
295 if (!(h_enc & LOWPAN_NHC_UDP_CS_C))
296 ret += 2;
297 }
298
299 return ret;
300}
301
302
303
304
305
306
307
308
309static inline u16
310lowpan_uncompress_size(const struct sk_buff *skb, u16 *dgram_offset)
311{
312 u16 ret = 2, uncomp_header = sizeof(struct ipv6hdr);
313 u8 iphc0, iphc1, h_enc;
314
315 iphc0 = skb_network_header(skb)[0];
316 iphc1 = skb_network_header(skb)[1];
317
318 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
319 case 0:
320 ret += 4;
321 break;
322 case 1:
323 ret += 3;
324 break;
325 case 2:
326 ret++;
327 break;
328 default:
329 break;
330 }
331
332 if (!(iphc0 & LOWPAN_IPHC_NH_C))
333 ret++;
334
335 if (!(iphc0 & 0x03))
336 ret++;
337
338 ret += lowpan_addr_mode_size((iphc1 & LOWPAN_IPHC_SAM) >>
339 LOWPAN_IPHC_SAM_BIT);
340
341 if (iphc1 & LOWPAN_IPHC_M) {
342 switch ((iphc1 & LOWPAN_IPHC_DAM_11) >>
343 LOWPAN_IPHC_DAM_BIT) {
344 case LOWPAN_IPHC_DAM_00:
345 ret += 16;
346 break;
347 case LOWPAN_IPHC_DAM_01:
348 ret += 6;
349 break;
350 case LOWPAN_IPHC_DAM_10:
351 ret += 4;
352 break;
353 case LOWPAN_IPHC_DAM_11:
354 ret++;
355 break;
356 default:
357 break;
358 }
359 } else {
360 ret += lowpan_addr_mode_size((iphc1 & LOWPAN_IPHC_DAM_11) >>
361 LOWPAN_IPHC_DAM_BIT);
362 }
363
364 if (iphc0 & LOWPAN_IPHC_NH_C) {
365 h_enc = skb_network_header(skb)[ret];
366 ret += lowpan_next_hdr_size(h_enc, &uncomp_header);
367 }
368
369 if (dgram_offset)
370 *dgram_offset = uncomp_header;
371
372 return skb->len + uncomp_header - ret;
373}
374
375int
376lowpan_header_decompress(struct sk_buff *skb, struct net_device *dev,
377 const u8 *saddr, const u8 saddr_type,
378 const u8 saddr_len, const u8 *daddr,
379 const u8 daddr_type, const u8 daddr_len,
380 u8 iphc0, u8 iphc1);
381int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
382 unsigned short type, const void *_daddr,
383 const void *_saddr, unsigned int len);
384
385#endif
386