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 <linux/debugfs.h>
57
58#include <net/ipv6.h>
59#include <net/net_namespace.h>
60
61
62#include <net/mac802154.h>
63
64#define EUI64_ADDR_LEN 8
65
66#define LOWPAN_NHC_MAX_ID_LEN 1
67
68
69
70#define LOWPAN_NHC_MAX_HDR_LEN (sizeof(struct udphdr))
71
72
73
74
75
76#define LOWPAN_IPHC_MAX_HEADER_LEN (2 + 1 + LOWPAN_NHC_MAX_ID_LEN)
77
78#define LOWPAN_IPHC_MAX_HC_BUF_LEN (sizeof(struct ipv6hdr) + \
79 LOWPAN_IPHC_MAX_HEADER_LEN + \
80 LOWPAN_NHC_MAX_HDR_LEN)
81
82#define LOWPAN_IPHC_CTX_TABLE_SIZE (1 << 4)
83
84#define LOWPAN_DISPATCH_IPV6 0x41
85#define LOWPAN_DISPATCH_IPHC 0x60
86#define LOWPAN_DISPATCH_IPHC_MASK 0xe0
87
88static inline bool lowpan_is_ipv6(u8 dispatch)
89{
90 return dispatch == LOWPAN_DISPATCH_IPV6;
91}
92
93static inline bool lowpan_is_iphc(u8 dispatch)
94{
95 return (dispatch & LOWPAN_DISPATCH_IPHC_MASK) == LOWPAN_DISPATCH_IPHC;
96}
97
98#define LOWPAN_PRIV_SIZE(llpriv_size) \
99 (sizeof(struct lowpan_dev) + llpriv_size)
100
101enum lowpan_lltypes {
102 LOWPAN_LLTYPE_BTLE,
103 LOWPAN_LLTYPE_IEEE802154,
104};
105
106enum lowpan_iphc_ctx_flags {
107 LOWPAN_IPHC_CTX_FLAG_ACTIVE,
108 LOWPAN_IPHC_CTX_FLAG_COMPRESSION,
109};
110
111struct lowpan_iphc_ctx {
112 u8 id;
113 struct in6_addr pfx;
114 u8 plen;
115 unsigned long flags;
116};
117
118struct lowpan_iphc_ctx_table {
119 spinlock_t lock;
120 const struct lowpan_iphc_ctx_ops *ops;
121 struct lowpan_iphc_ctx table[LOWPAN_IPHC_CTX_TABLE_SIZE];
122};
123
124static inline bool lowpan_iphc_ctx_is_active(const struct lowpan_iphc_ctx *ctx)
125{
126 return test_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE, &ctx->flags);
127}
128
129static inline bool
130lowpan_iphc_ctx_is_compression(const struct lowpan_iphc_ctx *ctx)
131{
132 return test_bit(LOWPAN_IPHC_CTX_FLAG_COMPRESSION, &ctx->flags);
133}
134
135struct lowpan_dev {
136 enum lowpan_lltypes lltype;
137 struct dentry *iface_debugfs;
138 struct lowpan_iphc_ctx_table ctx;
139
140
141 u8 priv[0] __aligned(sizeof(void *));
142};
143
144struct lowpan_802154_neigh {
145 __le16 short_addr;
146};
147
148static inline
149struct lowpan_802154_neigh *lowpan_802154_neigh(void *neigh_priv)
150{
151 return neigh_priv;
152}
153
154static inline
155struct lowpan_dev *lowpan_dev(const struct net_device *dev)
156{
157 return netdev_priv(dev);
158}
159
160
161struct lowpan_802154_dev {
162 struct net_device *wdev;
163 u16 fragment_tag;
164};
165
166static inline struct
167lowpan_802154_dev *lowpan_802154_dev(const struct net_device *dev)
168{
169 return (struct lowpan_802154_dev *)lowpan_dev(dev)->priv;
170}
171
172struct lowpan_802154_cb {
173 u16 d_tag;
174 unsigned int d_size;
175 u8 d_offset;
176};
177
178static inline
179struct lowpan_802154_cb *lowpan_802154_cb(const struct sk_buff *skb)
180{
181 BUILD_BUG_ON(sizeof(struct lowpan_802154_cb) > sizeof(skb->cb));
182 return (struct lowpan_802154_cb *)skb->cb;
183}
184
185static inline void lowpan_iphc_uncompress_eui64_lladdr(struct in6_addr *ipaddr,
186 const void *lladdr)
187{
188
189
190
191
192 ipaddr->s6_addr[0] = 0xFE;
193 ipaddr->s6_addr[1] = 0x80;
194 memcpy(&ipaddr->s6_addr[8], lladdr, EUI64_ADDR_LEN);
195
196
197
198 ipaddr->s6_addr[8] ^= 0x02;
199}
200
201static inline void lowpan_iphc_uncompress_eui48_lladdr(struct in6_addr *ipaddr,
202 const void *lladdr)
203{
204
205
206
207
208 ipaddr->s6_addr[0] = 0xFE;
209 ipaddr->s6_addr[1] = 0x80;
210 memcpy(&ipaddr->s6_addr[8], lladdr, 3);
211 ipaddr->s6_addr[11] = 0xFF;
212 ipaddr->s6_addr[12] = 0xFE;
213 memcpy(&ipaddr->s6_addr[13], lladdr + 3, 3);
214}
215
216#ifdef DEBUG
217
218static inline void raw_dump_inline(const char *caller, char *msg,
219 const unsigned char *buf, int len)
220{
221 if (msg)
222 pr_debug("%s():%s: ", caller, msg);
223
224 print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, buf, len, false);
225}
226
227
228
229
230
231
232
233static inline void raw_dump_table(const char *caller, char *msg,
234 const unsigned char *buf, int len)
235{
236 if (msg)
237 pr_debug("%s():%s:\n", caller, msg);
238
239 print_hex_dump_debug("\t", DUMP_PREFIX_OFFSET, 16, 1, buf, len, false);
240}
241#else
242static inline void raw_dump_table(const char *caller, char *msg,
243 const unsigned char *buf, int len) { }
244static inline void raw_dump_inline(const char *caller, char *msg,
245 const unsigned char *buf, int len) { }
246#endif
247
248
249
250
251
252
253
254
255
256
257
258
259
260static inline bool lowpan_fetch_skb(struct sk_buff *skb, void *data,
261 unsigned int len)
262{
263 if (unlikely(!pskb_may_pull(skb, len)))
264 return true;
265
266 skb_copy_from_linear_data(skb, data, len);
267 skb_pull(skb, len);
268
269 return false;
270}
271
272static inline bool lowpan_802154_is_valid_src_short_addr(__le16 addr)
273{
274
275 return !(addr & cpu_to_le16(0x8000));
276}
277
278static inline void lowpan_push_hc_data(u8 **hc_ptr, const void *data,
279 const size_t len)
280{
281 memcpy(*hc_ptr, data, len);
282 *hc_ptr += len;
283}
284
285int lowpan_register_netdevice(struct net_device *dev,
286 enum lowpan_lltypes lltype);
287int lowpan_register_netdev(struct net_device *dev,
288 enum lowpan_lltypes lltype);
289void lowpan_unregister_netdevice(struct net_device *dev);
290void lowpan_unregister_netdev(struct net_device *dev);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308int lowpan_header_decompress(struct sk_buff *skb, const struct net_device *dev,
309 const void *daddr, const void *saddr);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev,
328 const void *daddr, const void *saddr);
329
330#endif
331