Merge tag 'fbdev-for-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[linux-block.git] / include / net / ieee802154_netdev.h
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
41 struct ieee802154_beacon_hdr {
42 #if defined(__LITTLE_ENDIAN_BITFIELD)
43         u16 beacon_order:4,
44             superframe_order:4,
45             final_cap_slot:4,
46             battery_life_ext:1,
47             reserved0:1,
48             pan_coordinator:1,
49             assoc_permit:1;
50         u8  gts_count:3,
51             gts_reserved:4,
52             gts_permit:1;
53         u8  pend_short_addr_count:3,
54             reserved1:1,
55             pend_ext_addr_count:3,
56             reserved2:1;
57 #elif defined(__BIG_ENDIAN_BITFIELD)
58         u16 assoc_permit:1,
59             pan_coordinator:1,
60             reserved0:1,
61             battery_life_ext:1,
62             final_cap_slot:4,
63             superframe_order:4,
64             beacon_order:4;
65         u8  gts_permit:1,
66             gts_reserved:4,
67             gts_count:3;
68         u8  reserved2:1,
69             pend_ext_addr_count:3,
70             reserved1:1,
71             pend_short_addr_count:3;
72 #else
73 #error  "Please fix <asm/byteorder.h>"
74 #endif
75 } __packed;
76
77 struct ieee802154_sechdr {
78 #if defined(__LITTLE_ENDIAN_BITFIELD)
79         u8 level:3,
80            key_id_mode:2,
81            reserved:3;
82 #elif defined(__BIG_ENDIAN_BITFIELD)
83         u8 reserved:3,
84            key_id_mode:2,
85            level:3;
86 #else
87 #error  "Please fix <asm/byteorder.h>"
88 #endif
89         u8 key_id;
90         __le32 frame_counter;
91         union {
92                 __le32 short_src;
93                 __le64 extended_src;
94         };
95 };
96
97 struct ieee802154_hdr_fc {
98 #if defined(__LITTLE_ENDIAN_BITFIELD)
99         u16 type:3,
100             security_enabled:1,
101             frame_pending:1,
102             ack_request:1,
103             intra_pan:1,
104             reserved:3,
105             dest_addr_mode:2,
106             version:2,
107             source_addr_mode:2;
108 #elif defined(__BIG_ENDIAN_BITFIELD)
109         u16 reserved:1,
110             intra_pan:1,
111             ack_request:1,
112             frame_pending:1,
113             security_enabled:1,
114             type:3,
115             source_addr_mode:2,
116             version:2,
117             dest_addr_mode:2,
118             reserved2:2;
119 #else
120 #error  "Please fix <asm/byteorder.h>"
121 #endif
122 };
123
124 enum ieee802154_frame_version {
125         IEEE802154_2003_STD,
126         IEEE802154_2006_STD,
127         IEEE802154_STD,
128         IEEE802154_RESERVED_STD,
129         IEEE802154_MULTIPURPOSE_STD = IEEE802154_2003_STD,
130 };
131
132 enum ieee802154_addressing_mode {
133         IEEE802154_NO_ADDRESSING,
134         IEEE802154_RESERVED,
135         IEEE802154_SHORT_ADDRESSING,
136         IEEE802154_EXTENDED_ADDRESSING,
137 };
138
139 struct ieee802154_hdr {
140         struct ieee802154_hdr_fc fc;
141         u8 seq;
142         struct ieee802154_addr source;
143         struct ieee802154_addr dest;
144         struct ieee802154_sechdr sec;
145 };
146
147 struct ieee802154_beacon_frame {
148         struct ieee802154_hdr mhr;
149         struct ieee802154_beacon_hdr mac_pl;
150 };
151
152 /* pushes hdr onto the skb. fields of hdr->fc that can be calculated from
153  * the contents of hdr will be, and the actual value of those bits in
154  * hdr->fc will be ignored. this includes the INTRA_PAN bit and the frame
155  * version, if SECEN is set.
156  */
157 int ieee802154_hdr_push(struct sk_buff *skb, struct ieee802154_hdr *hdr);
158
159 /* pulls the entire 802.15.4 header off of the skb, including the security
160  * header, and performs pan id decompression
161  */
162 int ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr);
163
164 /* parses the frame control, sequence number of address fields in a given skb
165  * and stores them into hdr, performing pan id decompression and length checks
166  * to be suitable for use in header_ops.parse
167  */
168 int ieee802154_hdr_peek_addrs(const struct sk_buff *skb,
169                               struct ieee802154_hdr *hdr);
170
171 /* parses the full 802.15.4 header a given skb and stores them into hdr,
172  * performing pan id decompression and length checks to be suitable for use in
173  * header_ops.parse
174  */
175 int ieee802154_hdr_peek(const struct sk_buff *skb, struct ieee802154_hdr *hdr);
176
177 /* pushes a beacon frame into an skb */
178 int ieee802154_beacon_push(struct sk_buff *skb,
179                            struct ieee802154_beacon_frame *beacon);
180
181 int ieee802154_max_payload(const struct ieee802154_hdr *hdr);
182
183 static inline int
184 ieee802154_sechdr_authtag_len(const struct ieee802154_sechdr *sec)
185 {
186         switch (sec->level) {
187         case IEEE802154_SCF_SECLEVEL_MIC32:
188         case IEEE802154_SCF_SECLEVEL_ENC_MIC32:
189                 return 4;
190         case IEEE802154_SCF_SECLEVEL_MIC64:
191         case IEEE802154_SCF_SECLEVEL_ENC_MIC64:
192                 return 8;
193         case IEEE802154_SCF_SECLEVEL_MIC128:
194         case IEEE802154_SCF_SECLEVEL_ENC_MIC128:
195                 return 16;
196         case IEEE802154_SCF_SECLEVEL_NONE:
197         case IEEE802154_SCF_SECLEVEL_ENC:
198         default:
199                 return 0;
200         }
201 }
202
203 static inline int ieee802154_hdr_length(struct sk_buff *skb)
204 {
205         struct ieee802154_hdr hdr;
206         int len = ieee802154_hdr_pull(skb, &hdr);
207
208         if (len > 0)
209                 skb_push(skb, len);
210
211         return len;
212 }
213
214 static inline bool ieee802154_addr_equal(const struct ieee802154_addr *a1,
215                                          const struct ieee802154_addr *a2)
216 {
217         if (a1->pan_id != a2->pan_id || a1->mode != a2->mode)
218                 return false;
219
220         if ((a1->mode == IEEE802154_ADDR_LONG &&
221              a1->extended_addr != a2->extended_addr) ||
222             (a1->mode == IEEE802154_ADDR_SHORT &&
223              a1->short_addr != a2->short_addr))
224                 return false;
225
226         return true;
227 }
228
229 static inline __le64 ieee802154_devaddr_from_raw(const void *raw)
230 {
231         u64 temp;
232
233         memcpy(&temp, raw, IEEE802154_ADDR_LEN);
234         return (__force __le64)swab64(temp);
235 }
236
237 static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr)
238 {
239         u64 temp = swab64((__force u64)addr);
240
241         memcpy(raw, &temp, IEEE802154_ADDR_LEN);
242 }
243
244 static inline int
245 ieee802154_sockaddr_check_size(struct sockaddr_ieee802154 *daddr, int len)
246 {
247         struct ieee802154_addr_sa *sa;
248         int ret = 0;
249
250         sa = &daddr->addr;
251         if (len < IEEE802154_MIN_NAMELEN)
252                 return -EINVAL;
253         switch (sa->addr_type) {
254         case IEEE802154_ADDR_NONE:
255                 break;
256         case IEEE802154_ADDR_SHORT:
257                 if (len < IEEE802154_NAMELEN_SHORT)
258                         ret = -EINVAL;
259                 break;
260         case IEEE802154_ADDR_LONG:
261                 if (len < IEEE802154_NAMELEN_LONG)
262                         ret = -EINVAL;
263                 break;
264         default:
265                 ret = -EINVAL;
266                 break;
267         }
268         return ret;
269 }
270
271 static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a,
272                                            const struct ieee802154_addr_sa *sa)
273 {
274         a->mode = sa->addr_type;
275         a->pan_id = cpu_to_le16(sa->pan_id);
276
277         switch (a->mode) {
278         case IEEE802154_ADDR_SHORT:
279                 a->short_addr = cpu_to_le16(sa->short_addr);
280                 break;
281         case IEEE802154_ADDR_LONG:
282                 a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr);
283                 break;
284         }
285 }
286
287 static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa,
288                                          const struct ieee802154_addr *a)
289 {
290         sa->addr_type = a->mode;
291         sa->pan_id = le16_to_cpu(a->pan_id);
292
293         switch (a->mode) {
294         case IEEE802154_ADDR_SHORT:
295                 sa->short_addr = le16_to_cpu(a->short_addr);
296                 break;
297         case IEEE802154_ADDR_LONG:
298                 ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr);
299                 break;
300         }
301 }
302
303 /*
304  * A control block of skb passed between the ARPHRD_IEEE802154 device
305  * and other stack parts.
306  */
307 struct ieee802154_mac_cb {
308         u8 lqi;
309         u8 type;
310         bool ackreq;
311         bool secen;
312         bool secen_override;
313         u8 seclevel;
314         bool seclevel_override;
315         struct ieee802154_addr source;
316         struct ieee802154_addr dest;
317 };
318
319 static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb)
320 {
321         return (struct ieee802154_mac_cb *)skb->cb;
322 }
323
324 static inline struct ieee802154_mac_cb *mac_cb_init(struct sk_buff *skb)
325 {
326         BUILD_BUG_ON(sizeof(struct ieee802154_mac_cb) > sizeof(skb->cb));
327
328         memset(skb->cb, 0, sizeof(struct ieee802154_mac_cb));
329         return mac_cb(skb);
330 }
331
332 enum {
333         IEEE802154_LLSEC_DEVKEY_IGNORE,
334         IEEE802154_LLSEC_DEVKEY_RESTRICT,
335         IEEE802154_LLSEC_DEVKEY_RECORD,
336
337         __IEEE802154_LLSEC_DEVKEY_MAX,
338 };
339
340 #define IEEE802154_MAC_SCAN_ED          0
341 #define IEEE802154_MAC_SCAN_ACTIVE      1
342 #define IEEE802154_MAC_SCAN_PASSIVE     2
343 #define IEEE802154_MAC_SCAN_ORPHAN      3
344
345 struct ieee802154_mac_params {
346         s8 transmit_power;
347         u8 min_be;
348         u8 max_be;
349         u8 csma_retries;
350         s8 frame_retries;
351
352         bool lbt;
353         struct wpan_phy_cca cca;
354         s32 cca_ed_level;
355 };
356
357 struct wpan_phy;
358
359 enum {
360         IEEE802154_LLSEC_PARAM_ENABLED          = BIT(0),
361         IEEE802154_LLSEC_PARAM_FRAME_COUNTER    = BIT(1),
362         IEEE802154_LLSEC_PARAM_OUT_LEVEL        = BIT(2),
363         IEEE802154_LLSEC_PARAM_OUT_KEY          = BIT(3),
364         IEEE802154_LLSEC_PARAM_KEY_SOURCE       = BIT(4),
365         IEEE802154_LLSEC_PARAM_PAN_ID           = BIT(5),
366         IEEE802154_LLSEC_PARAM_HWADDR           = BIT(6),
367         IEEE802154_LLSEC_PARAM_COORD_HWADDR     = BIT(7),
368         IEEE802154_LLSEC_PARAM_COORD_SHORTADDR  = BIT(8),
369 };
370
371 struct ieee802154_llsec_ops {
372         int (*get_params)(struct net_device *dev,
373                           struct ieee802154_llsec_params *params);
374         int (*set_params)(struct net_device *dev,
375                           const struct ieee802154_llsec_params *params,
376                           int changed);
377
378         int (*add_key)(struct net_device *dev,
379                        const struct ieee802154_llsec_key_id *id,
380                        const struct ieee802154_llsec_key *key);
381         int (*del_key)(struct net_device *dev,
382                        const struct ieee802154_llsec_key_id *id);
383
384         int (*add_dev)(struct net_device *dev,
385                        const struct ieee802154_llsec_device *llsec_dev);
386         int (*del_dev)(struct net_device *dev, __le64 dev_addr);
387
388         int (*add_devkey)(struct net_device *dev,
389                           __le64 device_addr,
390                           const struct ieee802154_llsec_device_key *key);
391         int (*del_devkey)(struct net_device *dev,
392                           __le64 device_addr,
393                           const struct ieee802154_llsec_device_key *key);
394
395         int (*add_seclevel)(struct net_device *dev,
396                             const struct ieee802154_llsec_seclevel *sl);
397         int (*del_seclevel)(struct net_device *dev,
398                             const struct ieee802154_llsec_seclevel *sl);
399
400         void (*lock_table)(struct net_device *dev);
401         void (*get_table)(struct net_device *dev,
402                           struct ieee802154_llsec_table **t);
403         void (*unlock_table)(struct net_device *dev);
404 };
405 /*
406  * This should be located at net_device->ml_priv
407  *
408  * get_phy should increment the reference counting on returned phy.
409  * Use wpan_wpy_put to put that reference.
410  */
411 struct ieee802154_mlme_ops {
412         /* The following fields are optional (can be NULL). */
413
414         int (*assoc_req)(struct net_device *dev,
415                         struct ieee802154_addr *addr,
416                         u8 channel, u8 page, u8 cap);
417         int (*assoc_resp)(struct net_device *dev,
418                         struct ieee802154_addr *addr,
419                         __le16 short_addr, u8 status);
420         int (*disassoc_req)(struct net_device *dev,
421                         struct ieee802154_addr *addr,
422                         u8 reason);
423         int (*start_req)(struct net_device *dev,
424                         struct ieee802154_addr *addr,
425                         u8 channel, u8 page, u8 bcn_ord, u8 sf_ord,
426                         u8 pan_coord, u8 blx, u8 coord_realign);
427         int (*scan_req)(struct net_device *dev,
428                         u8 type, u32 channels, u8 page, u8 duration);
429
430         int (*set_mac_params)(struct net_device *dev,
431                               const struct ieee802154_mac_params *params);
432         void (*get_mac_params)(struct net_device *dev,
433                                struct ieee802154_mac_params *params);
434
435         const struct ieee802154_llsec_ops *llsec;
436 };
437
438 static inline struct ieee802154_mlme_ops *
439 ieee802154_mlme_ops(const struct net_device *dev)
440 {
441         return dev->ml_priv;
442 }
443
444 #endif