blob: 3b53c8e405e48143f667c20850db1666c402d8fa [file] [log] [blame]
Sergey Lapin9ec76712009-06-08 12:18:48 +00001/*
2 * An interface between IEEE802.15.4 device and rest of the kernel.
3 *
alex.bluesman.smirnov@gmail.com74a02fc2012-05-15 20:50:23 +00004 * Copyright (C) 2007-2012 Siemens AG
Sergey Lapin9ec76712009-06-08 12:18:48 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Written by:
20 * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
21 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
22 * Maxim Osipov <maxim.osipov@siemens.com>
23 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
alex.bluesman.smirnov@gmail.com74a02fc2012-05-15 20:50:23 +000024 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
Sergey Lapin9ec76712009-06-08 12:18:48 +000025 */
26
27#ifndef IEEE802154_NETDEVICE_H
28#define IEEE802154_NETDEVICE_H
29
Phoebe Buckheisterc3a61142014-05-14 17:43:06 +020030#include <net/ieee802154.h>
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +000031#include <net/af_ieee802154.h>
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +010032#include <linux/netdevice.h>
33#include <linux/skbuff.h>
34
35struct ieee802154_sechdr {
36#if defined(__LITTLE_ENDIAN_BITFIELD)
37 u8 level:3,
38 key_id_mode:2,
39 reserved:3;
40#elif defined(__BIG_ENDIAN_BITFIELD)
41 u8 reserved:3,
42 key_id_mode:2,
43 level:3;
44#else
45#error "Please fix <asm/byteorder.h>"
46#endif
47 u8 key_id;
48 __le32 frame_counter;
49 union {
50 __le32 short_src;
51 __le64 extended_src;
52 };
53};
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +000054
Phoebe Buckheister46ef0eb2014-03-14 21:23:58 +010055struct ieee802154_addr {
56 u8 mode;
57 __le16 pan_id;
58 union {
59 __le16 short_addr;
60 __le64 extended_addr;
61 };
62};
63
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +010064struct ieee802154_hdr_fc {
65#if defined(__LITTLE_ENDIAN_BITFIELD)
66 u16 type:3,
67 security_enabled:1,
68 frame_pending:1,
69 ack_request:1,
70 intra_pan:1,
71 reserved:3,
72 dest_addr_mode:2,
73 version:2,
74 source_addr_mode:2;
75#elif defined(__BIG_ENDIAN_BITFIELD)
76 u16 reserved:1,
77 intra_pan:1,
78 ack_request:1,
79 frame_pending:1,
80 security_enabled:1,
81 type:3,
82 source_addr_mode:2,
83 version:2,
84 dest_addr_mode:2,
85 reserved2:2;
86#else
87#error "Please fix <asm/byteorder.h>"
88#endif
89};
90
91struct ieee802154_hdr {
92 struct ieee802154_hdr_fc fc;
93 u8 seq;
94 struct ieee802154_addr source;
95 struct ieee802154_addr dest;
96 struct ieee802154_sechdr sec;
97};
98
99/* pushes hdr onto the skb. fields of hdr->fc that can be calculated from
100 * the contents of hdr will be, and the actual value of those bits in
101 * hdr->fc will be ignored. this includes the INTRA_PAN bit and the frame
102 * version, if SECEN is set.
103 */
104int ieee802154_hdr_push(struct sk_buff *skb, const struct ieee802154_hdr *hdr);
105
106/* pulls the entire 802.15.4 header off of the skb, including the security
107 * header, and performs pan id decompression
108 */
109int ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr);
110
111/* parses the frame control, sequence number of address fields in a given skb
112 * and stores them into hdr, performing pan id decompression and length checks
113 * to be suitable for use in header_ops.parse
114 */
115int ieee802154_hdr_peek_addrs(const struct sk_buff *skb,
116 struct ieee802154_hdr *hdr);
117
Phoebe Buckheisterc3a61142014-05-14 17:43:06 +0200118/* parses the full 802.15.4 header a given skb and stores them into hdr,
119 * performing pan id decompression and length checks to be suitable for use in
120 * header_ops.parse
121 */
122int ieee802154_hdr_peek(const struct sk_buff *skb, struct ieee802154_hdr *hdr);
123
124int ieee802154_max_payload(const struct ieee802154_hdr *hdr);
125
126static inline int
127ieee802154_sechdr_authtag_len(const struct ieee802154_sechdr *sec)
128{
129 switch (sec->level) {
130 case IEEE802154_SCF_SECLEVEL_MIC32:
131 case IEEE802154_SCF_SECLEVEL_ENC_MIC32:
132 return 4;
133 case IEEE802154_SCF_SECLEVEL_MIC64:
134 case IEEE802154_SCF_SECLEVEL_ENC_MIC64:
135 return 8;
136 case IEEE802154_SCF_SECLEVEL_MIC128:
137 case IEEE802154_SCF_SECLEVEL_ENC_MIC128:
138 return 16;
139 case IEEE802154_SCF_SECLEVEL_NONE:
140 case IEEE802154_SCF_SECLEVEL_ENC:
141 default:
142 return 0;
143 }
144}
145
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +0100146static inline int ieee802154_hdr_length(struct sk_buff *skb)
147{
148 struct ieee802154_hdr hdr;
149 int len = ieee802154_hdr_pull(skb, &hdr);
150
151 if (len > 0)
152 skb_push(skb, len);
153
154 return len;
155}
156
Phoebe Buckheister46ef0eb2014-03-14 21:23:58 +0100157static inline bool ieee802154_addr_equal(const struct ieee802154_addr *a1,
158 const struct ieee802154_addr *a2)
159{
160 if (a1->pan_id != a2->pan_id || a1->mode != a2->mode)
161 return false;
162
163 if ((a1->mode == IEEE802154_ADDR_LONG &&
164 a1->extended_addr != a2->extended_addr) ||
165 (a1->mode == IEEE802154_ADDR_SHORT &&
166 a1->short_addr != a2->short_addr))
167 return false;
168
169 return true;
170}
171
172static inline __le64 ieee802154_devaddr_from_raw(const void *raw)
173{
174 u64 temp;
175
176 memcpy(&temp, raw, IEEE802154_ADDR_LEN);
177 return (__force __le64)swab64(temp);
178}
179
180static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr)
181{
182 u64 temp = swab64((__force u64)addr);
183
184 memcpy(raw, &temp, IEEE802154_ADDR_LEN);
185}
186
187static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a,
188 const struct ieee802154_addr_sa *sa)
189{
190 a->mode = sa->addr_type;
191 a->pan_id = cpu_to_le16(sa->pan_id);
192
193 switch (a->mode) {
194 case IEEE802154_ADDR_SHORT:
195 a->short_addr = cpu_to_le16(sa->short_addr);
196 break;
197 case IEEE802154_ADDR_LONG:
198 a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr);
199 break;
200 }
201}
202
203static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa,
204 const struct ieee802154_addr *a)
205{
206 sa->addr_type = a->mode;
207 sa->pan_id = le16_to_cpu(a->pan_id);
208
209 switch (a->mode) {
210 case IEEE802154_ADDR_SHORT:
211 sa->short_addr = le16_to_cpu(a->short_addr);
212 break;
213 case IEEE802154_ADDR_LONG:
214 ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr);
215 break;
216 }
217}
218
Sergey Lapin9ec76712009-06-08 12:18:48 +0000219/*
220 * A control block of skb passed between the ARPHRD_IEEE802154 device
221 * and other stack parts.
222 */
223struct ieee802154_mac_cb {
224 u8 lqi;
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200225 u8 type;
226 bool ackreq;
227 bool secen;
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200228 bool secen_override;
229 u8 seclevel;
230 bool seclevel_override;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100231 struct ieee802154_addr source;
232 struct ieee802154_addr dest;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000233};
234
235static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb)
236{
237 return (struct ieee802154_mac_cb *)skb->cb;
238}
239
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200240static inline struct ieee802154_mac_cb *mac_cb_init(struct sk_buff *skb)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000241{
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200242 BUILD_BUG_ON(sizeof(struct ieee802154_mac_cb) > sizeof(skb->cb));
Sergey Lapin9ec76712009-06-08 12:18:48 +0000243
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200244 memset(skb->cb, 0, sizeof(struct ieee802154_mac_cb));
245 return mac_cb(skb);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000246}
247
Phoebe Buckheisterdc207592014-05-16 17:46:35 +0200248#define IEEE802154_LLSEC_KEY_SIZE 16
249
250struct ieee802154_llsec_key_id {
251 u8 mode;
252 u8 id;
253 union {
254 struct ieee802154_addr device_addr;
255 __le32 short_source;
256 __le64 extended_source;
257 };
258};
259
260struct ieee802154_llsec_key {
261 u8 frame_types;
262 u32 cmd_frame_ids;
263 u8 key[IEEE802154_LLSEC_KEY_SIZE];
264};
265
266struct ieee802154_llsec_key_entry {
267 struct list_head list;
268
269 struct ieee802154_llsec_key_id id;
270 struct ieee802154_llsec_key *key;
271};
272
273struct ieee802154_llsec_device_key {
274 struct list_head list;
275
276 struct ieee802154_llsec_key_id key_id;
277 u32 frame_counter;
278};
279
280enum {
281 IEEE802154_LLSEC_DEVKEY_IGNORE,
282 IEEE802154_LLSEC_DEVKEY_RESTRICT,
Phoebe Buckheisterf0f77dc2014-05-16 17:46:45 +0200283 IEEE802154_LLSEC_DEVKEY_RECORD,
Phoebe Buckheisterdc207592014-05-16 17:46:35 +0200284
285 __IEEE802154_LLSEC_DEVKEY_MAX,
286};
287
288struct ieee802154_llsec_device {
289 struct list_head list;
290
291 __le16 pan_id;
292 __le16 short_addr;
293 __le64 hwaddr;
294 u32 frame_counter;
295 bool seclevel_exempt;
296
297 u8 key_mode;
298 struct list_head keys;
299};
300
301struct ieee802154_llsec_seclevel {
302 struct list_head list;
303
304 u8 frame_type;
305 u8 cmd_frame_id;
306 bool device_override;
307 u32 sec_levels;
308};
309
310struct ieee802154_llsec_params {
311 bool enabled;
312
313 __be32 frame_counter;
314 u8 out_level;
315 struct ieee802154_llsec_key_id out_key;
316
317 __le64 default_key_source;
318
319 __le16 pan_id;
320 __le64 hwaddr;
321 __le64 coord_hwaddr;
322 __le16 coord_shortaddr;
323};
324
325struct ieee802154_llsec_table {
326 struct list_head keys;
327 struct list_head devices;
328 struct list_head security_levels;
329};
330
Sergey Lapin9ec76712009-06-08 12:18:48 +0000331#define IEEE802154_MAC_SCAN_ED 0
332#define IEEE802154_MAC_SCAN_ACTIVE 1
333#define IEEE802154_MAC_SCAN_PASSIVE 2
334#define IEEE802154_MAC_SCAN_ORPHAN 3
335
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200336struct ieee802154_mac_params {
337 s8 transmit_power;
338 u8 min_be;
339 u8 max_be;
340 u8 csma_retries;
341 s8 frame_retries;
342
343 bool lbt;
344 u8 cca_mode;
345 s32 cca_ed_level;
346};
347
Dmitry Eremin-Solenikov42723442009-11-04 17:53:23 +0300348struct wpan_phy;
Phoebe Buckheisterdc207592014-05-16 17:46:35 +0200349
350enum {
351 IEEE802154_LLSEC_PARAM_ENABLED = 1 << 0,
352 IEEE802154_LLSEC_PARAM_FRAME_COUNTER = 1 << 1,
353 IEEE802154_LLSEC_PARAM_OUT_LEVEL = 1 << 2,
354 IEEE802154_LLSEC_PARAM_OUT_KEY = 1 << 3,
355 IEEE802154_LLSEC_PARAM_KEY_SOURCE = 1 << 4,
356 IEEE802154_LLSEC_PARAM_PAN_ID = 1 << 5,
357 IEEE802154_LLSEC_PARAM_HWADDR = 1 << 6,
358 IEEE802154_LLSEC_PARAM_COORD_HWADDR = 1 << 7,
359 IEEE802154_LLSEC_PARAM_COORD_SHORTADDR = 1 << 8,
360};
361
Phoebe Buckheister29e02372014-05-16 17:46:42 +0200362struct ieee802154_llsec_ops {
363 int (*get_params)(struct net_device *dev,
364 struct ieee802154_llsec_params *params);
365 int (*set_params)(struct net_device *dev,
366 const struct ieee802154_llsec_params *params,
367 int changed);
368
369 int (*add_key)(struct net_device *dev,
370 const struct ieee802154_llsec_key_id *id,
371 const struct ieee802154_llsec_key *key);
372 int (*del_key)(struct net_device *dev,
373 const struct ieee802154_llsec_key_id *id);
374
375 int (*add_dev)(struct net_device *dev,
376 const struct ieee802154_llsec_device *llsec_dev);
377 int (*del_dev)(struct net_device *dev, __le64 dev_addr);
378
379 int (*add_devkey)(struct net_device *dev,
380 __le64 device_addr,
381 const struct ieee802154_llsec_device_key *key);
382 int (*del_devkey)(struct net_device *dev,
383 __le64 device_addr,
384 const struct ieee802154_llsec_device_key *key);
385
386 int (*add_seclevel)(struct net_device *dev,
387 const struct ieee802154_llsec_seclevel *sl);
388 int (*del_seclevel)(struct net_device *dev,
389 const struct ieee802154_llsec_seclevel *sl);
390
391 void (*lock_table)(struct net_device *dev);
392 void (*get_table)(struct net_device *dev,
393 struct ieee802154_llsec_table **t);
394 void (*unlock_table)(struct net_device *dev);
395};
Sergey Lapin9ec76712009-06-08 12:18:48 +0000396/*
397 * This should be located at net_device->ml_priv
Dmitry Eremin-Solenikov42723442009-11-04 17:53:23 +0300398 *
399 * get_phy should increment the reference counting on returned phy.
400 * Use wpan_wpy_put to put that reference.
Sergey Lapin9ec76712009-06-08 12:18:48 +0000401 */
402struct ieee802154_mlme_ops {
Werner Almesberger56aa0912013-04-04 06:32:35 +0000403 /* The following fields are optional (can be NULL). */
404
Sergey Lapin9ec76712009-06-08 12:18:48 +0000405 int (*assoc_req)(struct net_device *dev,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100406 struct ieee802154_addr *addr,
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400407 u8 channel, u8 page, u8 cap);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000408 int (*assoc_resp)(struct net_device *dev,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100409 struct ieee802154_addr *addr,
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100410 __le16 short_addr, u8 status);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000411 int (*disassoc_req)(struct net_device *dev,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100412 struct ieee802154_addr *addr,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000413 u8 reason);
414 int (*start_req)(struct net_device *dev,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100415 struct ieee802154_addr *addr,
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400416 u8 channel, u8 page, u8 bcn_ord, u8 sf_ord,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000417 u8 pan_coord, u8 blx, u8 coord_realign);
418 int (*scan_req)(struct net_device *dev,
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400419 u8 type, u32 channels, u8 page, u8 duration);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000420
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200421 int (*set_mac_params)(struct net_device *dev,
422 const struct ieee802154_mac_params *params);
423 void (*get_mac_params)(struct net_device *dev,
424 struct ieee802154_mac_params *params);
425
Phoebe Buckheister29e02372014-05-16 17:46:42 +0200426 struct ieee802154_llsec_ops *llsec;
427
Werner Almesberger56aa0912013-04-04 06:32:35 +0000428 /* The fields below are required. */
429
Dmitry Eremin-Solenikov42723442009-11-04 17:53:23 +0300430 struct wpan_phy *(*get_phy)(const struct net_device *dev);
431
Sergey Lapin9ec76712009-06-08 12:18:48 +0000432 /*
433 * FIXME: these should become the part of PIB/MIB interface.
434 * However we still don't have IB interface of any kind
435 */
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100436 __le16 (*get_pan_id)(const struct net_device *dev);
437 __le16 (*get_short_addr)(const struct net_device *dev);
Dmitry Eremin-Solenikova9966b52009-10-02 19:05:00 +0400438 u8 (*get_dsn)(const struct net_device *dev);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000439};
440
alex.bluesman.smirnov@gmail.com74a02fc2012-05-15 20:50:23 +0000441/* The IEEE 802.15.4 standard defines 2 type of the devices:
442 * - FFD - full functionality device
443 * - RFD - reduce functionality device
444 *
445 * So 2 sets of mlme operations are needed
446 */
447struct ieee802154_reduced_mlme_ops {
448 struct wpan_phy *(*get_phy)(const struct net_device *dev);
449};
450
451static inline struct ieee802154_mlme_ops *
452ieee802154_mlme_ops(const struct net_device *dev)
453{
454 return dev->ml_priv;
455}
456
457static inline struct ieee802154_reduced_mlme_ops *
458ieee802154_reduced_mlme_ops(const struct net_device *dev)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000459{
460 return dev->ml_priv;
461}
462
463#endif