Loading...
1/*
2 * An interface between IEEE802.15.4 device and rest of the kernel.
3 *
4 * Copyright (C) 2007, 2008, 2009 Siemens AG
5 *
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>
24 */
25
26#ifndef IEEE802154_NETDEVICE_H
27#define IEEE802154_NETDEVICE_H
28
29/*
30 * A control block of skb passed between the ARPHRD_IEEE802154 device
31 * and other stack parts.
32 */
33struct ieee802154_mac_cb {
34 u8 lqi;
35 struct ieee802154_addr sa;
36 struct ieee802154_addr da;
37 u8 flags;
38 u8 seq;
39};
40
41static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb)
42{
43 return (struct ieee802154_mac_cb *)skb->cb;
44}
45
46#define MAC_CB_FLAG_TYPEMASK ((1 << 3) - 1)
47
48#define MAC_CB_FLAG_ACKREQ (1 << 3)
49#define MAC_CB_FLAG_SECEN (1 << 4)
50#define MAC_CB_FLAG_INTRAPAN (1 << 5)
51
52static inline int mac_cb_is_ackreq(struct sk_buff *skb)
53{
54 return mac_cb(skb)->flags & MAC_CB_FLAG_ACKREQ;
55}
56
57static inline int mac_cb_is_secen(struct sk_buff *skb)
58{
59 return mac_cb(skb)->flags & MAC_CB_FLAG_SECEN;
60}
61
62static inline int mac_cb_is_intrapan(struct sk_buff *skb)
63{
64 return mac_cb(skb)->flags & MAC_CB_FLAG_INTRAPAN;
65}
66
67static inline int mac_cb_type(struct sk_buff *skb)
68{
69 return mac_cb(skb)->flags & MAC_CB_FLAG_TYPEMASK;
70}
71
72#define IEEE802154_MAC_SCAN_ED 0
73#define IEEE802154_MAC_SCAN_ACTIVE 1
74#define IEEE802154_MAC_SCAN_PASSIVE 2
75#define IEEE802154_MAC_SCAN_ORPHAN 3
76
77struct wpan_phy;
78/*
79 * This should be located at net_device->ml_priv
80 *
81 * get_phy should increment the reference counting on returned phy.
82 * Use wpan_wpy_put to put that reference.
83 */
84struct ieee802154_mlme_ops {
85 int (*assoc_req)(struct net_device *dev,
86 struct ieee802154_addr *addr,
87 u8 channel, u8 page, u8 cap);
88 int (*assoc_resp)(struct net_device *dev,
89 struct ieee802154_addr *addr,
90 u16 short_addr, u8 status);
91 int (*disassoc_req)(struct net_device *dev,
92 struct ieee802154_addr *addr,
93 u8 reason);
94 int (*start_req)(struct net_device *dev,
95 struct ieee802154_addr *addr,
96 u8 channel, u8 page, u8 bcn_ord, u8 sf_ord,
97 u8 pan_coord, u8 blx, u8 coord_realign);
98 int (*scan_req)(struct net_device *dev,
99 u8 type, u32 channels, u8 page, u8 duration);
100
101 struct wpan_phy *(*get_phy)(const struct net_device *dev);
102
103 /*
104 * FIXME: these should become the part of PIB/MIB interface.
105 * However we still don't have IB interface of any kind
106 */
107 u16 (*get_pan_id)(const struct net_device *dev);
108 u16 (*get_short_addr)(const struct net_device *dev);
109 u8 (*get_dsn)(const struct net_device *dev);
110 u8 (*get_bsn)(const struct net_device *dev);
111};
112
113static inline struct ieee802154_mlme_ops *ieee802154_mlme_ops(
114 const struct net_device *dev)
115{
116 return dev->ml_priv;
117}
118
119#endif
120
121
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * An interface between IEEE802.15.4 device and rest of the kernel.
4 *
5 * Copyright (C) 2007-2012 Siemens AG
6 *
7 * Written by:
8 * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
9 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
10 * Maxim Osipov <maxim.osipov@siemens.com>
11 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
12 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
13 */
14
15#ifndef IEEE802154_NETDEVICE_H
16#define IEEE802154_NETDEVICE_H
17
18#define IEEE802154_REQUIRED_SIZE(struct_type, member) \
19 (offsetof(typeof(struct_type), member) + \
20 sizeof(((typeof(struct_type) *)(NULL))->member))
21
22#define IEEE802154_ADDR_OFFSET \
23 offsetof(typeof(struct sockaddr_ieee802154), addr)
24
25#define IEEE802154_MIN_NAMELEN (IEEE802154_ADDR_OFFSET + \
26 IEEE802154_REQUIRED_SIZE(struct ieee802154_addr_sa, addr_type))
27
28#define IEEE802154_NAMELEN_SHORT (IEEE802154_ADDR_OFFSET + \
29 IEEE802154_REQUIRED_SIZE(struct ieee802154_addr_sa, short_addr))
30
31#define IEEE802154_NAMELEN_LONG (IEEE802154_ADDR_OFFSET + \
32 IEEE802154_REQUIRED_SIZE(struct ieee802154_addr_sa, hwaddr))
33
34#include <net/af_ieee802154.h>
35#include <linux/netdevice.h>
36#include <linux/skbuff.h>
37#include <linux/ieee802154.h>
38
39#include <net/cfg802154.h>
40
41struct ieee802154_sechdr {
42#if defined(__LITTLE_ENDIAN_BITFIELD)
43 u8 level:3,
44 key_id_mode:2,
45 reserved:3;
46#elif defined(__BIG_ENDIAN_BITFIELD)
47 u8 reserved:3,
48 key_id_mode:2,
49 level:3;
50#else
51#error "Please fix <asm/byteorder.h>"
52#endif
53 u8 key_id;
54 __le32 frame_counter;
55 union {
56 __le32 short_src;
57 __le64 extended_src;
58 };
59};
60
61struct ieee802154_hdr_fc {
62#if defined(__LITTLE_ENDIAN_BITFIELD)
63 u16 type:3,
64 security_enabled:1,
65 frame_pending:1,
66 ack_request:1,
67 intra_pan:1,
68 reserved:3,
69 dest_addr_mode:2,
70 version:2,
71 source_addr_mode:2;
72#elif defined(__BIG_ENDIAN_BITFIELD)
73 u16 reserved:1,
74 intra_pan:1,
75 ack_request:1,
76 frame_pending:1,
77 security_enabled:1,
78 type:3,
79 source_addr_mode:2,
80 version:2,
81 dest_addr_mode:2,
82 reserved2:2;
83#else
84#error "Please fix <asm/byteorder.h>"
85#endif
86};
87
88enum ieee802154_frame_version {
89 IEEE802154_2003_STD,
90 IEEE802154_2006_STD,
91 IEEE802154_STD,
92 IEEE802154_RESERVED_STD,
93 IEEE802154_MULTIPURPOSE_STD = IEEE802154_2003_STD,
94};
95
96struct ieee802154_hdr {
97 struct ieee802154_hdr_fc fc;
98 u8 seq;
99 struct ieee802154_addr source;
100 struct ieee802154_addr dest;
101 struct ieee802154_sechdr sec;
102};
103
104/* pushes hdr onto the skb. fields of hdr->fc that can be calculated from
105 * the contents of hdr will be, and the actual value of those bits in
106 * hdr->fc will be ignored. this includes the INTRA_PAN bit and the frame
107 * version, if SECEN is set.
108 */
109int ieee802154_hdr_push(struct sk_buff *skb, struct ieee802154_hdr *hdr);
110
111/* pulls the entire 802.15.4 header off of the skb, including the security
112 * header, and performs pan id decompression
113 */
114int ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr);
115
116/* parses the frame control, sequence number of address fields in a given skb
117 * and stores them into hdr, performing pan id decompression and length checks
118 * to be suitable for use in header_ops.parse
119 */
120int ieee802154_hdr_peek_addrs(const struct sk_buff *skb,
121 struct ieee802154_hdr *hdr);
122
123/* parses the full 802.15.4 header a given skb and stores them into hdr,
124 * performing pan id decompression and length checks to be suitable for use in
125 * header_ops.parse
126 */
127int ieee802154_hdr_peek(const struct sk_buff *skb, struct ieee802154_hdr *hdr);
128
129int ieee802154_max_payload(const struct ieee802154_hdr *hdr);
130
131static inline int
132ieee802154_sechdr_authtag_len(const struct ieee802154_sechdr *sec)
133{
134 switch (sec->level) {
135 case IEEE802154_SCF_SECLEVEL_MIC32:
136 case IEEE802154_SCF_SECLEVEL_ENC_MIC32:
137 return 4;
138 case IEEE802154_SCF_SECLEVEL_MIC64:
139 case IEEE802154_SCF_SECLEVEL_ENC_MIC64:
140 return 8;
141 case IEEE802154_SCF_SECLEVEL_MIC128:
142 case IEEE802154_SCF_SECLEVEL_ENC_MIC128:
143 return 16;
144 case IEEE802154_SCF_SECLEVEL_NONE:
145 case IEEE802154_SCF_SECLEVEL_ENC:
146 default:
147 return 0;
148 }
149}
150
151static inline int ieee802154_hdr_length(struct sk_buff *skb)
152{
153 struct ieee802154_hdr hdr;
154 int len = ieee802154_hdr_pull(skb, &hdr);
155
156 if (len > 0)
157 skb_push(skb, len);
158
159 return len;
160}
161
162static inline bool ieee802154_addr_equal(const struct ieee802154_addr *a1,
163 const struct ieee802154_addr *a2)
164{
165 if (a1->pan_id != a2->pan_id || a1->mode != a2->mode)
166 return false;
167
168 if ((a1->mode == IEEE802154_ADDR_LONG &&
169 a1->extended_addr != a2->extended_addr) ||
170 (a1->mode == IEEE802154_ADDR_SHORT &&
171 a1->short_addr != a2->short_addr))
172 return false;
173
174 return true;
175}
176
177static inline __le64 ieee802154_devaddr_from_raw(const void *raw)
178{
179 u64 temp;
180
181 memcpy(&temp, raw, IEEE802154_ADDR_LEN);
182 return (__force __le64)swab64(temp);
183}
184
185static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr)
186{
187 u64 temp = swab64((__force u64)addr);
188
189 memcpy(raw, &temp, IEEE802154_ADDR_LEN);
190}
191
192static inline int
193ieee802154_sockaddr_check_size(struct sockaddr_ieee802154 *daddr, int len)
194{
195 struct ieee802154_addr_sa *sa;
196 int ret = 0;
197
198 sa = &daddr->addr;
199 if (len < IEEE802154_MIN_NAMELEN)
200 return -EINVAL;
201 switch (sa->addr_type) {
202 case IEEE802154_ADDR_NONE:
203 break;
204 case IEEE802154_ADDR_SHORT:
205 if (len < IEEE802154_NAMELEN_SHORT)
206 ret = -EINVAL;
207 break;
208 case IEEE802154_ADDR_LONG:
209 if (len < IEEE802154_NAMELEN_LONG)
210 ret = -EINVAL;
211 break;
212 default:
213 ret = -EINVAL;
214 break;
215 }
216 return ret;
217}
218
219static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a,
220 const struct ieee802154_addr_sa *sa)
221{
222 a->mode = sa->addr_type;
223 a->pan_id = cpu_to_le16(sa->pan_id);
224
225 switch (a->mode) {
226 case IEEE802154_ADDR_SHORT:
227 a->short_addr = cpu_to_le16(sa->short_addr);
228 break;
229 case IEEE802154_ADDR_LONG:
230 a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr);
231 break;
232 }
233}
234
235static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa,
236 const struct ieee802154_addr *a)
237{
238 sa->addr_type = a->mode;
239 sa->pan_id = le16_to_cpu(a->pan_id);
240
241 switch (a->mode) {
242 case IEEE802154_ADDR_SHORT:
243 sa->short_addr = le16_to_cpu(a->short_addr);
244 break;
245 case IEEE802154_ADDR_LONG:
246 ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr);
247 break;
248 }
249}
250
251/*
252 * A control block of skb passed between the ARPHRD_IEEE802154 device
253 * and other stack parts.
254 */
255struct ieee802154_mac_cb {
256 u8 lqi;
257 u8 type;
258 bool ackreq;
259 bool secen;
260 bool secen_override;
261 u8 seclevel;
262 bool seclevel_override;
263 struct ieee802154_addr source;
264 struct ieee802154_addr dest;
265};
266
267static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb)
268{
269 return (struct ieee802154_mac_cb *)skb->cb;
270}
271
272static inline struct ieee802154_mac_cb *mac_cb_init(struct sk_buff *skb)
273{
274 BUILD_BUG_ON(sizeof(struct ieee802154_mac_cb) > sizeof(skb->cb));
275
276 memset(skb->cb, 0, sizeof(struct ieee802154_mac_cb));
277 return mac_cb(skb);
278}
279
280enum {
281 IEEE802154_LLSEC_DEVKEY_IGNORE,
282 IEEE802154_LLSEC_DEVKEY_RESTRICT,
283 IEEE802154_LLSEC_DEVKEY_RECORD,
284
285 __IEEE802154_LLSEC_DEVKEY_MAX,
286};
287
288#define IEEE802154_MAC_SCAN_ED 0
289#define IEEE802154_MAC_SCAN_ACTIVE 1
290#define IEEE802154_MAC_SCAN_PASSIVE 2
291#define IEEE802154_MAC_SCAN_ORPHAN 3
292
293struct ieee802154_mac_params {
294 s8 transmit_power;
295 u8 min_be;
296 u8 max_be;
297 u8 csma_retries;
298 s8 frame_retries;
299
300 bool lbt;
301 struct wpan_phy_cca cca;
302 s32 cca_ed_level;
303};
304
305struct wpan_phy;
306
307enum {
308 IEEE802154_LLSEC_PARAM_ENABLED = BIT(0),
309 IEEE802154_LLSEC_PARAM_FRAME_COUNTER = BIT(1),
310 IEEE802154_LLSEC_PARAM_OUT_LEVEL = BIT(2),
311 IEEE802154_LLSEC_PARAM_OUT_KEY = BIT(3),
312 IEEE802154_LLSEC_PARAM_KEY_SOURCE = BIT(4),
313 IEEE802154_LLSEC_PARAM_PAN_ID = BIT(5),
314 IEEE802154_LLSEC_PARAM_HWADDR = BIT(6),
315 IEEE802154_LLSEC_PARAM_COORD_HWADDR = BIT(7),
316 IEEE802154_LLSEC_PARAM_COORD_SHORTADDR = BIT(8),
317};
318
319struct ieee802154_llsec_ops {
320 int (*get_params)(struct net_device *dev,
321 struct ieee802154_llsec_params *params);
322 int (*set_params)(struct net_device *dev,
323 const struct ieee802154_llsec_params *params,
324 int changed);
325
326 int (*add_key)(struct net_device *dev,
327 const struct ieee802154_llsec_key_id *id,
328 const struct ieee802154_llsec_key *key);
329 int (*del_key)(struct net_device *dev,
330 const struct ieee802154_llsec_key_id *id);
331
332 int (*add_dev)(struct net_device *dev,
333 const struct ieee802154_llsec_device *llsec_dev);
334 int (*del_dev)(struct net_device *dev, __le64 dev_addr);
335
336 int (*add_devkey)(struct net_device *dev,
337 __le64 device_addr,
338 const struct ieee802154_llsec_device_key *key);
339 int (*del_devkey)(struct net_device *dev,
340 __le64 device_addr,
341 const struct ieee802154_llsec_device_key *key);
342
343 int (*add_seclevel)(struct net_device *dev,
344 const struct ieee802154_llsec_seclevel *sl);
345 int (*del_seclevel)(struct net_device *dev,
346 const struct ieee802154_llsec_seclevel *sl);
347
348 void (*lock_table)(struct net_device *dev);
349 void (*get_table)(struct net_device *dev,
350 struct ieee802154_llsec_table **t);
351 void (*unlock_table)(struct net_device *dev);
352};
353/*
354 * This should be located at net_device->ml_priv
355 *
356 * get_phy should increment the reference counting on returned phy.
357 * Use wpan_wpy_put to put that reference.
358 */
359struct ieee802154_mlme_ops {
360 /* The following fields are optional (can be NULL). */
361
362 int (*assoc_req)(struct net_device *dev,
363 struct ieee802154_addr *addr,
364 u8 channel, u8 page, u8 cap);
365 int (*assoc_resp)(struct net_device *dev,
366 struct ieee802154_addr *addr,
367 __le16 short_addr, u8 status);
368 int (*disassoc_req)(struct net_device *dev,
369 struct ieee802154_addr *addr,
370 u8 reason);
371 int (*start_req)(struct net_device *dev,
372 struct ieee802154_addr *addr,
373 u8 channel, u8 page, u8 bcn_ord, u8 sf_ord,
374 u8 pan_coord, u8 blx, u8 coord_realign);
375 int (*scan_req)(struct net_device *dev,
376 u8 type, u32 channels, u8 page, u8 duration);
377
378 int (*set_mac_params)(struct net_device *dev,
379 const struct ieee802154_mac_params *params);
380 void (*get_mac_params)(struct net_device *dev,
381 struct ieee802154_mac_params *params);
382
383 const struct ieee802154_llsec_ops *llsec;
384};
385
386static inline struct ieee802154_mlme_ops *
387ieee802154_mlme_ops(const struct net_device *dev)
388{
389 return dev->ml_priv;
390}
391
392#endif