xref: /linux/include/net/cfg802154.h (revision 2e7ed75e92fc493ff5484f61aed6489262c78f3e)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2007, 2008, 2009 Siemens AG
4  *
5  * Written by:
6  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
7  */
8 
9 #ifndef __NET_CFG802154_H
10 #define __NET_CFG802154_H
11 
12 #include <linux/ieee802154.h>
13 #include <linux/netdevice.h>
14 #include <linux/spinlock.h>
15 #include <linux/bug.h>
16 
17 #include <net/nl802154.h>
18 
19 struct wpan_phy;
20 struct wpan_phy_cca;
21 struct cfg802154_scan_request;
22 struct cfg802154_beacon_request;
23 
24 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
25 struct ieee802154_llsec_device_key;
26 struct ieee802154_llsec_seclevel;
27 struct ieee802154_llsec_params;
28 struct ieee802154_llsec_device;
29 struct ieee802154_llsec_table;
30 struct ieee802154_llsec_key_id;
31 struct ieee802154_llsec_key;
32 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
33 
34 struct cfg802154_ops {
35 	struct net_device * (*add_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
36 							   const char *name,
37 							   unsigned char name_assign_type,
38 							   int type);
39 	void	(*del_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
40 					       struct net_device *dev);
41 	int	(*suspend)(struct wpan_phy *wpan_phy);
42 	int	(*resume)(struct wpan_phy *wpan_phy);
43 	int	(*add_virtual_intf)(struct wpan_phy *wpan_phy,
44 				    const char *name,
45 				    unsigned char name_assign_type,
46 				    enum nl802154_iftype type,
47 				    __le64 extended_addr);
48 	int	(*del_virtual_intf)(struct wpan_phy *wpan_phy,
49 				    struct wpan_dev *wpan_dev);
50 	int	(*set_channel)(struct wpan_phy *wpan_phy, u8 page, u8 channel);
51 	int	(*set_cca_mode)(struct wpan_phy *wpan_phy,
52 				const struct wpan_phy_cca *cca);
53 	int     (*set_cca_ed_level)(struct wpan_phy *wpan_phy, s32 ed_level);
54 	int     (*set_tx_power)(struct wpan_phy *wpan_phy, s32 power);
55 	int	(*set_pan_id)(struct wpan_phy *wpan_phy,
56 			      struct wpan_dev *wpan_dev, __le16 pan_id);
57 	int	(*set_short_addr)(struct wpan_phy *wpan_phy,
58 				  struct wpan_dev *wpan_dev, __le16 short_addr);
59 	int	(*set_backoff_exponent)(struct wpan_phy *wpan_phy,
60 					struct wpan_dev *wpan_dev, u8 min_be,
61 					u8 max_be);
62 	int	(*set_max_csma_backoffs)(struct wpan_phy *wpan_phy,
63 					 struct wpan_dev *wpan_dev,
64 					 u8 max_csma_backoffs);
65 	int	(*set_max_frame_retries)(struct wpan_phy *wpan_phy,
66 					 struct wpan_dev *wpan_dev,
67 					 s8 max_frame_retries);
68 	int	(*set_lbt_mode)(struct wpan_phy *wpan_phy,
69 				struct wpan_dev *wpan_dev, bool mode);
70 	int	(*set_ackreq_default)(struct wpan_phy *wpan_phy,
71 				      struct wpan_dev *wpan_dev, bool ackreq);
72 	int	(*trigger_scan)(struct wpan_phy *wpan_phy,
73 				struct cfg802154_scan_request *request);
74 	int	(*abort_scan)(struct wpan_phy *wpan_phy,
75 			      struct wpan_dev *wpan_dev);
76 	int	(*send_beacons)(struct wpan_phy *wpan_phy,
77 				struct cfg802154_beacon_request *request);
78 	int	(*stop_beacons)(struct wpan_phy *wpan_phy,
79 				struct wpan_dev *wpan_dev);
80 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
81 	void	(*get_llsec_table)(struct wpan_phy *wpan_phy,
82 				   struct wpan_dev *wpan_dev,
83 				   struct ieee802154_llsec_table **table);
84 	void	(*lock_llsec_table)(struct wpan_phy *wpan_phy,
85 				    struct wpan_dev *wpan_dev);
86 	void	(*unlock_llsec_table)(struct wpan_phy *wpan_phy,
87 				      struct wpan_dev *wpan_dev);
88 	/* TODO remove locking/get table callbacks, this is part of the
89 	 * nl802154 interface and should be accessible from ieee802154 layer.
90 	 */
91 	int	(*get_llsec_params)(struct wpan_phy *wpan_phy,
92 				    struct wpan_dev *wpan_dev,
93 				    struct ieee802154_llsec_params *params);
94 	int	(*set_llsec_params)(struct wpan_phy *wpan_phy,
95 				    struct wpan_dev *wpan_dev,
96 				    const struct ieee802154_llsec_params *params,
97 				    int changed);
98 	int	(*add_llsec_key)(struct wpan_phy *wpan_phy,
99 				 struct wpan_dev *wpan_dev,
100 				 const struct ieee802154_llsec_key_id *id,
101 				 const struct ieee802154_llsec_key *key);
102 	int	(*del_llsec_key)(struct wpan_phy *wpan_phy,
103 				 struct wpan_dev *wpan_dev,
104 				 const struct ieee802154_llsec_key_id *id);
105 	int	(*add_seclevel)(struct wpan_phy *wpan_phy,
106 				 struct wpan_dev *wpan_dev,
107 				 const struct ieee802154_llsec_seclevel *sl);
108 	int	(*del_seclevel)(struct wpan_phy *wpan_phy,
109 				 struct wpan_dev *wpan_dev,
110 				 const struct ieee802154_llsec_seclevel *sl);
111 	int	(*add_device)(struct wpan_phy *wpan_phy,
112 			      struct wpan_dev *wpan_dev,
113 			      const struct ieee802154_llsec_device *dev);
114 	int	(*del_device)(struct wpan_phy *wpan_phy,
115 			      struct wpan_dev *wpan_dev, __le64 extended_addr);
116 	int	(*add_devkey)(struct wpan_phy *wpan_phy,
117 			      struct wpan_dev *wpan_dev,
118 			      __le64 extended_addr,
119 			      const struct ieee802154_llsec_device_key *key);
120 	int	(*del_devkey)(struct wpan_phy *wpan_phy,
121 			      struct wpan_dev *wpan_dev,
122 			      __le64 extended_addr,
123 			      const struct ieee802154_llsec_device_key *key);
124 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
125 };
126 
127 static inline bool
128 wpan_phy_supported_bool(bool b, enum nl802154_supported_bool_states st)
129 {
130 	switch (st) {
131 	case NL802154_SUPPORTED_BOOL_TRUE:
132 		return b;
133 	case NL802154_SUPPORTED_BOOL_FALSE:
134 		return !b;
135 	case NL802154_SUPPORTED_BOOL_BOTH:
136 		return true;
137 	default:
138 		WARN_ON(1);
139 	}
140 
141 	return false;
142 }
143 
144 struct wpan_phy_supported {
145 	u32 channels[IEEE802154_MAX_PAGE + 1],
146 	    cca_modes, cca_opts, iftypes;
147 	enum nl802154_supported_bool_states lbt;
148 	u8 min_minbe, max_minbe, min_maxbe, max_maxbe,
149 	   min_csma_backoffs, max_csma_backoffs;
150 	s8 min_frame_retries, max_frame_retries;
151 	size_t tx_powers_size, cca_ed_levels_size;
152 	const s32 *tx_powers, *cca_ed_levels;
153 };
154 
155 struct wpan_phy_cca {
156 	enum nl802154_cca_modes mode;
157 	enum nl802154_cca_opts opt;
158 };
159 
160 static inline bool
161 wpan_phy_cca_cmp(const struct wpan_phy_cca *a, const struct wpan_phy_cca *b)
162 {
163 	if (a->mode != b->mode)
164 		return false;
165 
166 	if (a->mode == NL802154_CCA_ENERGY_CARRIER)
167 		return a->opt == b->opt;
168 
169 	return true;
170 }
171 
172 /**
173  * enum wpan_phy_flags - WPAN PHY state flags
174  * @WPAN_PHY_FLAG_TXPOWER: Indicates that transceiver will support
175  *	transmit power setting.
176  * @WPAN_PHY_FLAG_CCA_ED_LEVEL: Indicates that transceiver will support cca ed
177  *	level setting.
178  * @WPAN_PHY_FLAG_CCA_MODE: Indicates that transceiver will support cca mode
179  *	setting.
180  * @WPAN_PHY_FLAG_STATE_QUEUE_STOPPED: Indicates that the transmit queue was
181  *	temporarily stopped.
182  * @WPAN_PHY_FLAG_DATAGRAMS_ONLY: Indicates that transceiver is only able to
183  *	send/receive datagrams.
184  */
185 enum wpan_phy_flags {
186 	WPAN_PHY_FLAG_TXPOWER		= BIT(1),
187 	WPAN_PHY_FLAG_CCA_ED_LEVEL	= BIT(2),
188 	WPAN_PHY_FLAG_CCA_MODE		= BIT(3),
189 	WPAN_PHY_FLAG_STATE_QUEUE_STOPPED = BIT(4),
190 	WPAN_PHY_FLAG_DATAGRAMS_ONLY	= BIT(5),
191 };
192 
193 struct wpan_phy {
194 	/* If multiple wpan_phys are registered and you're handed e.g.
195 	 * a regular netdev with assigned ieee802154_ptr, you won't
196 	 * know whether it points to a wpan_phy your driver has registered
197 	 * or not. Assign this to something global to your driver to
198 	 * help determine whether you own this wpan_phy or not.
199 	 */
200 	const void *privid;
201 
202 	unsigned long flags;
203 
204 	/*
205 	 * This is a PIB according to 802.15.4-2011.
206 	 * We do not provide timing-related variables, as they
207 	 * aren't used outside of driver
208 	 */
209 	u8 current_channel;
210 	u8 current_page;
211 	struct wpan_phy_supported supported;
212 	/* current transmit_power in mBm */
213 	s32 transmit_power;
214 	struct wpan_phy_cca cca;
215 
216 	__le64 perm_extended_addr;
217 
218 	/* current cca ed threshold in mBm */
219 	s32 cca_ed_level;
220 
221 	/* PHY depended MAC PIB values */
222 
223 	/* 802.15.4 acronym: Tdsym in nsec */
224 	u32 symbol_duration;
225 	/* lifs and sifs periods timing */
226 	u16 lifs_period;
227 	u16 sifs_period;
228 
229 	struct device dev;
230 
231 	/* the network namespace this phy lives in currently */
232 	possible_net_t _net;
233 
234 	/* Transmission monitoring and control */
235 	spinlock_t queue_lock;
236 	atomic_t ongoing_txs;
237 	atomic_t hold_txs;
238 	wait_queue_head_t sync_txq;
239 
240 	/* Current filtering level on reception.
241 	 * Only allowed to be changed if phy is not operational.
242 	 */
243 	enum ieee802154_filtering_level filtering;
244 
245 	char priv[] __aligned(NETDEV_ALIGN);
246 };
247 
248 static inline struct net *wpan_phy_net(struct wpan_phy *wpan_phy)
249 {
250 	return read_pnet(&wpan_phy->_net);
251 }
252 
253 static inline void wpan_phy_net_set(struct wpan_phy *wpan_phy, struct net *net)
254 {
255 	write_pnet(&wpan_phy->_net, net);
256 }
257 
258 static inline bool ieee802154_chan_is_valid(struct wpan_phy *phy,
259 					    u8 page, u8 channel)
260 {
261 	if (page > IEEE802154_MAX_PAGE ||
262 	    channel > IEEE802154_MAX_CHANNEL ||
263 	    !(phy->supported.channels[page] & BIT(channel)))
264 		return false;
265 
266 	return true;
267 }
268 
269 /**
270  * struct ieee802154_addr - IEEE802.15.4 device address
271  * @mode: Address mode from frame header. Can be one of:
272  *        - @IEEE802154_ADDR_NONE
273  *        - @IEEE802154_ADDR_SHORT
274  *        - @IEEE802154_ADDR_LONG
275  * @pan_id: The PAN ID this address belongs to
276  * @short_addr: address if @mode is @IEEE802154_ADDR_SHORT
277  * @extended_addr: address if @mode is @IEEE802154_ADDR_LONG
278  */
279 struct ieee802154_addr {
280 	u8 mode;
281 	__le16 pan_id;
282 	union {
283 		__le16 short_addr;
284 		__le64 extended_addr;
285 	};
286 };
287 
288 /**
289  * struct ieee802154_coord_desc - Coordinator descriptor
290  * @addr: PAN ID and coordinator address
291  * @page: page this coordinator is using
292  * @channel: channel this coordinator is using
293  * @superframe_spec: SuperFrame specification as received
294  * @link_quality: link quality indicator at which the beacon was received
295  * @gts_permit: the coordinator accepts GTS requests
296  */
297 struct ieee802154_coord_desc {
298 	struct ieee802154_addr addr;
299 	u8 page;
300 	u8 channel;
301 	u16 superframe_spec;
302 	u8 link_quality;
303 	bool gts_permit;
304 };
305 
306 /**
307  * struct ieee802154_pan_device - PAN device information
308  * @pan_id: the PAN ID of this device
309  * @mode: the preferred mode to reach the device
310  * @short_addr: the short address of this device
311  * @extended_addr: the extended address of this device
312  * @node: the list node
313  */
314 struct ieee802154_pan_device {
315 	__le16 pan_id;
316 	u8 mode;
317 	__le16 short_addr;
318 	__le64 extended_addr;
319 	struct list_head node;
320 };
321 
322 /**
323  * struct cfg802154_scan_request - Scan request
324  *
325  * @type: type of scan to be performed
326  * @page: page on which to perform the scan
327  * @channels: channels in te %page to be scanned
328  * @duration: time spent on each channel, calculated with:
329  *            aBaseSuperframeDuration * (2 ^ duration + 1)
330  * @wpan_dev: the wpan device on which to perform the scan
331  * @wpan_phy: the wpan phy on which to perform the scan
332  */
333 struct cfg802154_scan_request {
334 	enum nl802154_scan_types type;
335 	u8 page;
336 	u32 channels;
337 	u8 duration;
338 	struct wpan_dev *wpan_dev;
339 	struct wpan_phy *wpan_phy;
340 };
341 
342 /**
343  * struct cfg802154_beacon_request - Beacon request descriptor
344  *
345  * @interval: interval n between sendings, in multiple order of the super frame
346  *            duration: aBaseSuperframeDuration * (2^n) unless the interval
347  *            order is greater or equal to 15, in this case beacons won't be
348  *            passively sent out at a fixed rate but instead inform the device
349  *            that it should answer beacon requests as part of active scan
350  *            procedures
351  * @wpan_dev: the concerned wpan device
352  * @wpan_phy: the wpan phy this was for
353  */
354 struct cfg802154_beacon_request {
355 	u8 interval;
356 	struct wpan_dev *wpan_dev;
357 	struct wpan_phy *wpan_phy;
358 };
359 
360 /**
361  * struct cfg802154_mac_pkt - MAC packet descriptor (beacon/command)
362  * @node: MAC packets to process list member
363  * @skb: the received sk_buff
364  * @sdata: the interface on which @skb was received
365  * @page: page configuration when @skb was received
366  * @channel: channel configuration when @skb was received
367  */
368 struct cfg802154_mac_pkt {
369 	struct list_head node;
370 	struct sk_buff *skb;
371 	struct ieee802154_sub_if_data *sdata;
372 	u8 page;
373 	u8 channel;
374 };
375 
376 struct ieee802154_llsec_key_id {
377 	u8 mode;
378 	u8 id;
379 	union {
380 		struct ieee802154_addr device_addr;
381 		__le32 short_source;
382 		__le64 extended_source;
383 	};
384 };
385 
386 #define IEEE802154_LLSEC_KEY_SIZE 16
387 
388 struct ieee802154_llsec_key {
389 	u8 frame_types;
390 	u32 cmd_frame_ids;
391 	/* TODO replace with NL802154_KEY_SIZE */
392 	u8 key[IEEE802154_LLSEC_KEY_SIZE];
393 };
394 
395 struct ieee802154_llsec_key_entry {
396 	struct list_head list;
397 
398 	struct ieee802154_llsec_key_id id;
399 	struct ieee802154_llsec_key *key;
400 };
401 
402 struct ieee802154_llsec_params {
403 	bool enabled;
404 
405 	__be32 frame_counter;
406 	u8 out_level;
407 	struct ieee802154_llsec_key_id out_key;
408 
409 	__le64 default_key_source;
410 
411 	__le16 pan_id;
412 	__le64 hwaddr;
413 	__le64 coord_hwaddr;
414 	__le16 coord_shortaddr;
415 };
416 
417 struct ieee802154_llsec_table {
418 	struct list_head keys;
419 	struct list_head devices;
420 	struct list_head security_levels;
421 };
422 
423 struct ieee802154_llsec_seclevel {
424 	struct list_head list;
425 
426 	u8 frame_type;
427 	u8 cmd_frame_id;
428 	bool device_override;
429 	u32 sec_levels;
430 };
431 
432 struct ieee802154_llsec_device {
433 	struct list_head list;
434 
435 	__le16 pan_id;
436 	__le16 short_addr;
437 	__le64 hwaddr;
438 	u32 frame_counter;
439 	bool seclevel_exempt;
440 
441 	u8 key_mode;
442 	struct list_head keys;
443 };
444 
445 struct ieee802154_llsec_device_key {
446 	struct list_head list;
447 
448 	struct ieee802154_llsec_key_id key_id;
449 	u32 frame_counter;
450 };
451 
452 struct wpan_dev_header_ops {
453 	/* TODO create callback currently assumes ieee802154_mac_cb inside
454 	 * skb->cb. This should be changed to give these information as
455 	 * parameter.
456 	 */
457 	int	(*create)(struct sk_buff *skb, struct net_device *dev,
458 			  const struct ieee802154_addr *daddr,
459 			  const struct ieee802154_addr *saddr,
460 			  unsigned int len);
461 };
462 
463 struct wpan_dev {
464 	struct wpan_phy *wpan_phy;
465 	int iftype;
466 
467 	/* the remainder of this struct should be private to cfg802154 */
468 	struct list_head list;
469 	struct net_device *netdev;
470 
471 	const struct wpan_dev_header_ops *header_ops;
472 
473 	/* lowpan interface, set when the wpan_dev belongs to one lowpan_dev */
474 	struct net_device *lowpan_dev;
475 
476 	u32 identifier;
477 
478 	/* MAC PIB */
479 	__le16 pan_id;
480 	__le16 short_addr;
481 	__le64 extended_addr;
482 
483 	/* MAC BSN field */
484 	atomic_t bsn;
485 	/* MAC DSN field */
486 	atomic_t dsn;
487 
488 	u8 min_be;
489 	u8 max_be;
490 	u8 csma_retries;
491 	s8 frame_retries;
492 
493 	bool lbt;
494 
495 	/* fallback for acknowledgment bit setting */
496 	bool ackreq;
497 
498 	/* Associations */
499 	struct mutex association_lock;
500 	struct ieee802154_pan_device *parent;
501 	struct list_head children;
502 };
503 
504 #define to_phy(_dev)	container_of(_dev, struct wpan_phy, dev)
505 
506 #if IS_ENABLED(CONFIG_IEEE802154) || IS_ENABLED(CONFIG_6LOWPAN)
507 static inline int
508 wpan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
509 		     const struct ieee802154_addr *daddr,
510 		     const struct ieee802154_addr *saddr,
511 		     unsigned int len)
512 {
513 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
514 
515 	return wpan_dev->header_ops->create(skb, dev, daddr, saddr, len);
516 }
517 #endif
518 
519 struct wpan_phy *
520 wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size);
521 static inline void wpan_phy_set_dev(struct wpan_phy *phy, struct device *dev)
522 {
523 	phy->dev.parent = dev;
524 }
525 
526 int wpan_phy_register(struct wpan_phy *phy);
527 void wpan_phy_unregister(struct wpan_phy *phy);
528 void wpan_phy_free(struct wpan_phy *phy);
529 /* Same semantics as for class_for_each_device */
530 int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), void *data);
531 
532 static inline void *wpan_phy_priv(struct wpan_phy *phy)
533 {
534 	BUG_ON(!phy);
535 	return &phy->priv;
536 }
537 
538 struct wpan_phy *wpan_phy_find(const char *str);
539 
540 static inline void wpan_phy_put(struct wpan_phy *phy)
541 {
542 	put_device(&phy->dev);
543 }
544 
545 static inline const char *wpan_phy_name(struct wpan_phy *phy)
546 {
547 	return dev_name(&phy->dev);
548 }
549 
550 void ieee802154_configure_durations(struct wpan_phy *phy,
551 				    unsigned int page, unsigned int channel);
552 
553 /**
554  * cfg802154_device_is_associated - Checks whether we are associated to any device
555  * @wpan_dev: the wpan device
556  * @return: true if we are associated
557  */
558 bool cfg802154_device_is_associated(struct wpan_dev *wpan_dev);
559 
560 /**
561  * cfg802154_device_is_parent - Checks if a device is our coordinator
562  * @wpan_dev: the wpan device
563  * @target: the expected parent
564  * @return: true if @target is our coordinator
565  */
566 bool cfg802154_device_is_parent(struct wpan_dev *wpan_dev,
567 				struct ieee802154_addr *target);
568 
569 /**
570  * cfg802154_device_is_child - Checks whether a device is associated to us
571  * @wpan_dev: the wpan device
572  * @target: the expected child
573  * @return: the PAN device
574  */
575 struct ieee802154_pan_device *
576 cfg802154_device_is_child(struct wpan_dev *wpan_dev,
577 			  struct ieee802154_addr *target);
578 
579 #endif /* __NET_CFG802154_H */
580