xref: /openbmc/linux/include/net/cfg802154.h (revision 060f03e9)
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  * @WPAN_PHY_FLAG_TRANSMIT_POWER: Indicates that transceiver will support
174  *	transmit power setting.
175  * @WPAN_PHY_FLAG_CCA_ED_LEVEL: Indicates that transceiver will support cca ed
176  *	level setting.
177  * @WPAN_PHY_FLAG_CCA_MODE: Indicates that transceiver will support cca mode
178  *	setting.
179  * @WPAN_PHY_FLAG_STATE_QUEUE_STOPPED: Indicates that the transmit queue was
180  *	temporarily stopped.
181  * @WPAN_PHY_FLAG_DATAGRAMS_ONLY: Indicates that transceiver is only able to
182  *	send/receive datagrams.
183  */
184 enum wpan_phy_flags {
185 	WPAN_PHY_FLAG_TXPOWER		= BIT(1),
186 	WPAN_PHY_FLAG_CCA_ED_LEVEL	= BIT(2),
187 	WPAN_PHY_FLAG_CCA_MODE		= BIT(3),
188 	WPAN_PHY_FLAG_STATE_QUEUE_STOPPED = BIT(4),
189 	WPAN_PHY_FLAG_DATAGRAMS_ONLY	= BIT(5),
190 };
191 
192 struct wpan_phy {
193 	/* If multiple wpan_phys are registered and you're handed e.g.
194 	 * a regular netdev with assigned ieee802154_ptr, you won't
195 	 * know whether it points to a wpan_phy your driver has registered
196 	 * or not. Assign this to something global to your driver to
197 	 * help determine whether you own this wpan_phy or not.
198 	 */
199 	const void *privid;
200 
201 	unsigned long flags;
202 
203 	/*
204 	 * This is a PIB according to 802.15.4-2011.
205 	 * We do not provide timing-related variables, as they
206 	 * aren't used outside of driver
207 	 */
208 	u8 current_channel;
209 	u8 current_page;
210 	struct wpan_phy_supported supported;
211 	/* current transmit_power in mBm */
212 	s32 transmit_power;
213 	struct wpan_phy_cca cca;
214 
215 	__le64 perm_extended_addr;
216 
217 	/* current cca ed threshold in mBm */
218 	s32 cca_ed_level;
219 
220 	/* PHY depended MAC PIB values */
221 
222 	/* 802.15.4 acronym: Tdsym in nsec */
223 	u32 symbol_duration;
224 	/* lifs and sifs periods timing */
225 	u16 lifs_period;
226 	u16 sifs_period;
227 
228 	struct device dev;
229 
230 	/* the network namespace this phy lives in currently */
231 	possible_net_t _net;
232 
233 	/* Transmission monitoring and control */
234 	spinlock_t queue_lock;
235 	atomic_t ongoing_txs;
236 	atomic_t hold_txs;
237 	wait_queue_head_t sync_txq;
238 
239 	/* Current filtering level on reception.
240 	 * Only allowed to be changed if phy is not operational.
241 	 */
242 	enum ieee802154_filtering_level filtering;
243 
244 	char priv[] __aligned(NETDEV_ALIGN);
245 };
246 
247 static inline struct net *wpan_phy_net(struct wpan_phy *wpan_phy)
248 {
249 	return read_pnet(&wpan_phy->_net);
250 }
251 
252 static inline void wpan_phy_net_set(struct wpan_phy *wpan_phy, struct net *net)
253 {
254 	write_pnet(&wpan_phy->_net, net);
255 }
256 
257 static inline bool ieee802154_chan_is_valid(struct wpan_phy *phy,
258 					    u8 page, u8 channel)
259 {
260 	if (page > IEEE802154_MAX_PAGE ||
261 	    channel > IEEE802154_MAX_CHANNEL ||
262 	    !(phy->supported.channels[page] & BIT(channel)))
263 		return false;
264 
265 	return true;
266 }
267 
268 /**
269  * struct ieee802154_addr - IEEE802.15.4 device address
270  * @mode: Address mode from frame header. Can be one of:
271  *        - @IEEE802154_ADDR_NONE
272  *        - @IEEE802154_ADDR_SHORT
273  *        - @IEEE802154_ADDR_LONG
274  * @pan_id: The PAN ID this address belongs to
275  * @short_addr: address if @mode is @IEEE802154_ADDR_SHORT
276  * @extended_addr: address if @mode is @IEEE802154_ADDR_LONG
277  */
278 struct ieee802154_addr {
279 	u8 mode;
280 	__le16 pan_id;
281 	union {
282 		__le16 short_addr;
283 		__le64 extended_addr;
284 	};
285 };
286 
287 /**
288  * struct ieee802154_coord_desc - Coordinator descriptor
289  * @addr: PAN ID and coordinator address
290  * @page: page this coordinator is using
291  * @channel: channel this coordinator is using
292  * @superframe_spec: SuperFrame specification as received
293  * @link_quality: link quality indicator at which the beacon was received
294  * @gts_permit: the coordinator accepts GTS requests
295  */
296 struct ieee802154_coord_desc {
297 	struct ieee802154_addr addr;
298 	u8 page;
299 	u8 channel;
300 	u16 superframe_spec;
301 	u8 link_quality;
302 	bool gts_permit;
303 };
304 
305 /**
306  * struct cfg802154_scan_request - Scan request
307  *
308  * @type: type of scan to be performed
309  * @page: page on which to perform the scan
310  * @channels: channels in te %page to be scanned
311  * @duration: time spent on each channel, calculated with:
312  *            aBaseSuperframeDuration * (2 ^ duration + 1)
313  * @wpan_dev: the wpan device on which to perform the scan
314  * @wpan_phy: the wpan phy on which to perform the scan
315  */
316 struct cfg802154_scan_request {
317 	enum nl802154_scan_types type;
318 	u8 page;
319 	u32 channels;
320 	u8 duration;
321 	struct wpan_dev *wpan_dev;
322 	struct wpan_phy *wpan_phy;
323 };
324 
325 /**
326  * struct cfg802154_beacon_request - Beacon request descriptor
327  *
328  * @interval: interval n between sendings, in multiple order of the super frame
329  *            duration: aBaseSuperframeDuration * (2^n) unless the interval
330  *            order is greater or equal to 15, in this case beacons won't be
331  *            passively sent out at a fixed rate but instead inform the device
332  *            that it should answer beacon requests as part of active scan
333  *            procedures
334  * @wpan_dev: the concerned wpan device
335  * @wpan_phy: the wpan phy this was for
336  */
337 struct cfg802154_beacon_request {
338 	u8 interval;
339 	struct wpan_dev *wpan_dev;
340 	struct wpan_phy *wpan_phy;
341 };
342 
343 /**
344  * struct cfg802154_mac_pkt - MAC packet descriptor (beacon/command)
345  * @node: MAC packets to process list member
346  * @skb: the received sk_buff
347  * @sdata: the interface on which @skb was received
348  * @page: page configuration when @skb was received
349  * @channel: channel configuration when @skb was received
350  */
351 struct cfg802154_mac_pkt {
352 	struct list_head node;
353 	struct sk_buff *skb;
354 	struct ieee802154_sub_if_data *sdata;
355 	u8 page;
356 	u8 channel;
357 };
358 
359 struct ieee802154_llsec_key_id {
360 	u8 mode;
361 	u8 id;
362 	union {
363 		struct ieee802154_addr device_addr;
364 		__le32 short_source;
365 		__le64 extended_source;
366 	};
367 };
368 
369 #define IEEE802154_LLSEC_KEY_SIZE 16
370 
371 struct ieee802154_llsec_key {
372 	u8 frame_types;
373 	u32 cmd_frame_ids;
374 	/* TODO replace with NL802154_KEY_SIZE */
375 	u8 key[IEEE802154_LLSEC_KEY_SIZE];
376 };
377 
378 struct ieee802154_llsec_key_entry {
379 	struct list_head list;
380 
381 	struct ieee802154_llsec_key_id id;
382 	struct ieee802154_llsec_key *key;
383 };
384 
385 struct ieee802154_llsec_params {
386 	bool enabled;
387 
388 	__be32 frame_counter;
389 	u8 out_level;
390 	struct ieee802154_llsec_key_id out_key;
391 
392 	__le64 default_key_source;
393 
394 	__le16 pan_id;
395 	__le64 hwaddr;
396 	__le64 coord_hwaddr;
397 	__le16 coord_shortaddr;
398 };
399 
400 struct ieee802154_llsec_table {
401 	struct list_head keys;
402 	struct list_head devices;
403 	struct list_head security_levels;
404 };
405 
406 struct ieee802154_llsec_seclevel {
407 	struct list_head list;
408 
409 	u8 frame_type;
410 	u8 cmd_frame_id;
411 	bool device_override;
412 	u32 sec_levels;
413 };
414 
415 struct ieee802154_llsec_device {
416 	struct list_head list;
417 
418 	__le16 pan_id;
419 	__le16 short_addr;
420 	__le64 hwaddr;
421 	u32 frame_counter;
422 	bool seclevel_exempt;
423 
424 	u8 key_mode;
425 	struct list_head keys;
426 };
427 
428 struct ieee802154_llsec_device_key {
429 	struct list_head list;
430 
431 	struct ieee802154_llsec_key_id key_id;
432 	u32 frame_counter;
433 };
434 
435 struct wpan_dev_header_ops {
436 	/* TODO create callback currently assumes ieee802154_mac_cb inside
437 	 * skb->cb. This should be changed to give these information as
438 	 * parameter.
439 	 */
440 	int	(*create)(struct sk_buff *skb, struct net_device *dev,
441 			  const struct ieee802154_addr *daddr,
442 			  const struct ieee802154_addr *saddr,
443 			  unsigned int len);
444 };
445 
446 struct wpan_dev {
447 	struct wpan_phy *wpan_phy;
448 	int iftype;
449 
450 	/* the remainder of this struct should be private to cfg802154 */
451 	struct list_head list;
452 	struct net_device *netdev;
453 
454 	const struct wpan_dev_header_ops *header_ops;
455 
456 	/* lowpan interface, set when the wpan_dev belongs to one lowpan_dev */
457 	struct net_device *lowpan_dev;
458 
459 	u32 identifier;
460 
461 	/* MAC PIB */
462 	__le16 pan_id;
463 	__le16 short_addr;
464 	__le64 extended_addr;
465 
466 	/* MAC BSN field */
467 	atomic_t bsn;
468 	/* MAC DSN field */
469 	atomic_t dsn;
470 
471 	u8 min_be;
472 	u8 max_be;
473 	u8 csma_retries;
474 	s8 frame_retries;
475 
476 	bool lbt;
477 
478 	/* fallback for acknowledgment bit setting */
479 	bool ackreq;
480 };
481 
482 #define to_phy(_dev)	container_of(_dev, struct wpan_phy, dev)
483 
484 #if IS_ENABLED(CONFIG_IEEE802154) || IS_ENABLED(CONFIG_6LOWPAN)
485 static inline int
486 wpan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
487 		     const struct ieee802154_addr *daddr,
488 		     const struct ieee802154_addr *saddr,
489 		     unsigned int len)
490 {
491 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
492 
493 	return wpan_dev->header_ops->create(skb, dev, daddr, saddr, len);
494 }
495 #endif
496 
497 struct wpan_phy *
498 wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size);
499 static inline void wpan_phy_set_dev(struct wpan_phy *phy, struct device *dev)
500 {
501 	phy->dev.parent = dev;
502 }
503 
504 int wpan_phy_register(struct wpan_phy *phy);
505 void wpan_phy_unregister(struct wpan_phy *phy);
506 void wpan_phy_free(struct wpan_phy *phy);
507 /* Same semantics as for class_for_each_device */
508 int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), void *data);
509 
510 static inline void *wpan_phy_priv(struct wpan_phy *phy)
511 {
512 	BUG_ON(!phy);
513 	return &phy->priv;
514 }
515 
516 struct wpan_phy *wpan_phy_find(const char *str);
517 
518 static inline void wpan_phy_put(struct wpan_phy *phy)
519 {
520 	put_device(&phy->dev);
521 }
522 
523 static inline const char *wpan_phy_name(struct wpan_phy *phy)
524 {
525 	return dev_name(&phy->dev);
526 }
527 
528 void ieee802154_configure_durations(struct wpan_phy *phy,
529 				    unsigned int page, unsigned int channel);
530 
531 #endif /* __NET_CFG802154_H */
532