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/mutex.h>
15 #include <linux/bug.h>
16 
17 #include <net/nl802154.h>
18 
19 struct wpan_phy;
20 struct wpan_phy_cca;
21 
22 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
23 struct ieee802154_llsec_device_key;
24 struct ieee802154_llsec_seclevel;
25 struct ieee802154_llsec_params;
26 struct ieee802154_llsec_device;
27 struct ieee802154_llsec_table;
28 struct ieee802154_llsec_key_id;
29 struct ieee802154_llsec_key;
30 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
31 
32 struct cfg802154_ops {
33 	struct net_device * (*add_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
34 							   const char *name,
35 							   unsigned char name_assign_type,
36 							   int type);
37 	void	(*del_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
38 					       struct net_device *dev);
39 	int	(*suspend)(struct wpan_phy *wpan_phy);
40 	int	(*resume)(struct wpan_phy *wpan_phy);
41 	int	(*add_virtual_intf)(struct wpan_phy *wpan_phy,
42 				    const char *name,
43 				    unsigned char name_assign_type,
44 				    enum nl802154_iftype type,
45 				    __le64 extended_addr);
46 	int	(*del_virtual_intf)(struct wpan_phy *wpan_phy,
47 				    struct wpan_dev *wpan_dev);
48 	int	(*set_channel)(struct wpan_phy *wpan_phy, u8 page, u8 channel);
49 	int	(*set_cca_mode)(struct wpan_phy *wpan_phy,
50 				const struct wpan_phy_cca *cca);
51 	int     (*set_cca_ed_level)(struct wpan_phy *wpan_phy, s32 ed_level);
52 	int     (*set_tx_power)(struct wpan_phy *wpan_phy, s32 power);
53 	int	(*set_pan_id)(struct wpan_phy *wpan_phy,
54 			      struct wpan_dev *wpan_dev, __le16 pan_id);
55 	int	(*set_short_addr)(struct wpan_phy *wpan_phy,
56 				  struct wpan_dev *wpan_dev, __le16 short_addr);
57 	int	(*set_backoff_exponent)(struct wpan_phy *wpan_phy,
58 					struct wpan_dev *wpan_dev, u8 min_be,
59 					u8 max_be);
60 	int	(*set_max_csma_backoffs)(struct wpan_phy *wpan_phy,
61 					 struct wpan_dev *wpan_dev,
62 					 u8 max_csma_backoffs);
63 	int	(*set_max_frame_retries)(struct wpan_phy *wpan_phy,
64 					 struct wpan_dev *wpan_dev,
65 					 s8 max_frame_retries);
66 	int	(*set_lbt_mode)(struct wpan_phy *wpan_phy,
67 				struct wpan_dev *wpan_dev, bool mode);
68 	int	(*set_ackreq_default)(struct wpan_phy *wpan_phy,
69 				      struct wpan_dev *wpan_dev, bool ackreq);
70 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
71 	void	(*get_llsec_table)(struct wpan_phy *wpan_phy,
72 				   struct wpan_dev *wpan_dev,
73 				   struct ieee802154_llsec_table **table);
74 	void	(*lock_llsec_table)(struct wpan_phy *wpan_phy,
75 				    struct wpan_dev *wpan_dev);
76 	void	(*unlock_llsec_table)(struct wpan_phy *wpan_phy,
77 				      struct wpan_dev *wpan_dev);
78 	/* TODO remove locking/get table callbacks, this is part of the
79 	 * nl802154 interface and should be accessible from ieee802154 layer.
80 	 */
81 	int	(*get_llsec_params)(struct wpan_phy *wpan_phy,
82 				    struct wpan_dev *wpan_dev,
83 				    struct ieee802154_llsec_params *params);
84 	int	(*set_llsec_params)(struct wpan_phy *wpan_phy,
85 				    struct wpan_dev *wpan_dev,
86 				    const struct ieee802154_llsec_params *params,
87 				    int changed);
88 	int	(*add_llsec_key)(struct wpan_phy *wpan_phy,
89 				 struct wpan_dev *wpan_dev,
90 				 const struct ieee802154_llsec_key_id *id,
91 				 const struct ieee802154_llsec_key *key);
92 	int	(*del_llsec_key)(struct wpan_phy *wpan_phy,
93 				 struct wpan_dev *wpan_dev,
94 				 const struct ieee802154_llsec_key_id *id);
95 	int	(*add_seclevel)(struct wpan_phy *wpan_phy,
96 				 struct wpan_dev *wpan_dev,
97 				 const struct ieee802154_llsec_seclevel *sl);
98 	int	(*del_seclevel)(struct wpan_phy *wpan_phy,
99 				 struct wpan_dev *wpan_dev,
100 				 const struct ieee802154_llsec_seclevel *sl);
101 	int	(*add_device)(struct wpan_phy *wpan_phy,
102 			      struct wpan_dev *wpan_dev,
103 			      const struct ieee802154_llsec_device *dev);
104 	int	(*del_device)(struct wpan_phy *wpan_phy,
105 			      struct wpan_dev *wpan_dev, __le64 extended_addr);
106 	int	(*add_devkey)(struct wpan_phy *wpan_phy,
107 			      struct wpan_dev *wpan_dev,
108 			      __le64 extended_addr,
109 			      const struct ieee802154_llsec_device_key *key);
110 	int	(*del_devkey)(struct wpan_phy *wpan_phy,
111 			      struct wpan_dev *wpan_dev,
112 			      __le64 extended_addr,
113 			      const struct ieee802154_llsec_device_key *key);
114 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
115 };
116 
117 static inline bool
wpan_phy_supported_bool(bool b,enum nl802154_supported_bool_states st)118 wpan_phy_supported_bool(bool b, enum nl802154_supported_bool_states st)
119 {
120 	switch (st) {
121 	case NL802154_SUPPORTED_BOOL_TRUE:
122 		return b;
123 	case NL802154_SUPPORTED_BOOL_FALSE:
124 		return !b;
125 	case NL802154_SUPPORTED_BOOL_BOTH:
126 		return true;
127 	default:
128 		WARN_ON(1);
129 	}
130 
131 	return false;
132 }
133 
134 struct wpan_phy_supported {
135 	u32 channels[IEEE802154_MAX_PAGE + 1],
136 	    cca_modes, cca_opts, iftypes;
137 	enum nl802154_supported_bool_states lbt;
138 	u8 min_minbe, max_minbe, min_maxbe, max_maxbe,
139 	   min_csma_backoffs, max_csma_backoffs;
140 	s8 min_frame_retries, max_frame_retries;
141 	size_t tx_powers_size, cca_ed_levels_size;
142 	const s32 *tx_powers, *cca_ed_levels;
143 };
144 
145 struct wpan_phy_cca {
146 	enum nl802154_cca_modes mode;
147 	enum nl802154_cca_opts opt;
148 };
149 
150 static inline bool
wpan_phy_cca_cmp(const struct wpan_phy_cca * a,const struct wpan_phy_cca * b)151 wpan_phy_cca_cmp(const struct wpan_phy_cca *a, const struct wpan_phy_cca *b)
152 {
153 	if (a->mode != b->mode)
154 		return false;
155 
156 	if (a->mode == NL802154_CCA_ENERGY_CARRIER)
157 		return a->opt == b->opt;
158 
159 	return true;
160 }
161 
162 /**
163  * @WPAN_PHY_FLAG_TRANSMIT_POWER: Indicates that transceiver will support
164  *	transmit power setting.
165  * @WPAN_PHY_FLAG_CCA_ED_LEVEL: Indicates that transceiver will support cca ed
166  *	level setting.
167  * @WPAN_PHY_FLAG_CCA_MODE: Indicates that transceiver will support cca mode
168  *	setting.
169  */
170 enum wpan_phy_flags {
171 	WPAN_PHY_FLAG_TXPOWER		= BIT(1),
172 	WPAN_PHY_FLAG_CCA_ED_LEVEL	= BIT(2),
173 	WPAN_PHY_FLAG_CCA_MODE		= BIT(3),
174 };
175 
176 struct wpan_phy {
177 	/* If multiple wpan_phys are registered and you're handed e.g.
178 	 * a regular netdev with assigned ieee802154_ptr, you won't
179 	 * know whether it points to a wpan_phy your driver has registered
180 	 * or not. Assign this to something global to your driver to
181 	 * help determine whether you own this wpan_phy or not.
182 	 */
183 	const void *privid;
184 
185 	u32 flags;
186 
187 	/*
188 	 * This is a PIB according to 802.15.4-2011.
189 	 * We do not provide timing-related variables, as they
190 	 * aren't used outside of driver
191 	 */
192 	u8 current_channel;
193 	u8 current_page;
194 	struct wpan_phy_supported supported;
195 	/* current transmit_power in mBm */
196 	s32 transmit_power;
197 	struct wpan_phy_cca cca;
198 
199 	__le64 perm_extended_addr;
200 
201 	/* current cca ed threshold in mBm */
202 	s32 cca_ed_level;
203 
204 	/* PHY depended MAC PIB values */
205 
206 	/* 802.15.4 acronym: Tdsym in nsec */
207 	u32 symbol_duration;
208 	/* lifs and sifs periods timing */
209 	u16 lifs_period;
210 	u16 sifs_period;
211 
212 	struct device dev;
213 
214 	/* the network namespace this phy lives in currently */
215 	possible_net_t _net;
216 
217 	char priv[] __aligned(NETDEV_ALIGN);
218 };
219 
wpan_phy_net(struct wpan_phy * wpan_phy)220 static inline struct net *wpan_phy_net(struct wpan_phy *wpan_phy)
221 {
222 	return read_pnet(&wpan_phy->_net);
223 }
224 
wpan_phy_net_set(struct wpan_phy * wpan_phy,struct net * net)225 static inline void wpan_phy_net_set(struct wpan_phy *wpan_phy, struct net *net)
226 {
227 	write_pnet(&wpan_phy->_net, net);
228 }
229 
230 /**
231  * struct ieee802154_addr - IEEE802.15.4 device address
232  * @mode: Address mode from frame header. Can be one of:
233  *        - @IEEE802154_ADDR_NONE
234  *        - @IEEE802154_ADDR_SHORT
235  *        - @IEEE802154_ADDR_LONG
236  * @pan_id: The PAN ID this address belongs to
237  * @short_addr: address if @mode is @IEEE802154_ADDR_SHORT
238  * @extended_addr: address if @mode is @IEEE802154_ADDR_LONG
239  */
240 struct ieee802154_addr {
241 	u8 mode;
242 	__le16 pan_id;
243 	union {
244 		__le16 short_addr;
245 		__le64 extended_addr;
246 	};
247 };
248 
249 struct ieee802154_llsec_key_id {
250 	u8 mode;
251 	u8 id;
252 	union {
253 		struct ieee802154_addr device_addr;
254 		__le32 short_source;
255 		__le64 extended_source;
256 	};
257 };
258 
259 #define IEEE802154_LLSEC_KEY_SIZE 16
260 
261 struct ieee802154_llsec_key {
262 	u8 frame_types;
263 	u32 cmd_frame_ids;
264 	/* TODO replace with NL802154_KEY_SIZE */
265 	u8 key[IEEE802154_LLSEC_KEY_SIZE];
266 };
267 
268 struct ieee802154_llsec_key_entry {
269 	struct list_head list;
270 
271 	struct ieee802154_llsec_key_id id;
272 	struct ieee802154_llsec_key *key;
273 };
274 
275 struct ieee802154_llsec_params {
276 	bool enabled;
277 
278 	__be32 frame_counter;
279 	u8 out_level;
280 	struct ieee802154_llsec_key_id out_key;
281 
282 	__le64 default_key_source;
283 
284 	__le16 pan_id;
285 	__le64 hwaddr;
286 	__le64 coord_hwaddr;
287 	__le16 coord_shortaddr;
288 };
289 
290 struct ieee802154_llsec_table {
291 	struct list_head keys;
292 	struct list_head devices;
293 	struct list_head security_levels;
294 };
295 
296 struct ieee802154_llsec_seclevel {
297 	struct list_head list;
298 
299 	u8 frame_type;
300 	u8 cmd_frame_id;
301 	bool device_override;
302 	u32 sec_levels;
303 };
304 
305 struct ieee802154_llsec_device {
306 	struct list_head list;
307 
308 	__le16 pan_id;
309 	__le16 short_addr;
310 	__le64 hwaddr;
311 	u32 frame_counter;
312 	bool seclevel_exempt;
313 
314 	u8 key_mode;
315 	struct list_head keys;
316 };
317 
318 struct ieee802154_llsec_device_key {
319 	struct list_head list;
320 
321 	struct ieee802154_llsec_key_id key_id;
322 	u32 frame_counter;
323 };
324 
325 struct wpan_dev_header_ops {
326 	/* TODO create callback currently assumes ieee802154_mac_cb inside
327 	 * skb->cb. This should be changed to give these information as
328 	 * parameter.
329 	 */
330 	int	(*create)(struct sk_buff *skb, struct net_device *dev,
331 			  const struct ieee802154_addr *daddr,
332 			  const struct ieee802154_addr *saddr,
333 			  unsigned int len);
334 };
335 
336 struct wpan_dev {
337 	struct wpan_phy *wpan_phy;
338 	int iftype;
339 
340 	/* the remainder of this struct should be private to cfg802154 */
341 	struct list_head list;
342 	struct net_device *netdev;
343 
344 	const struct wpan_dev_header_ops *header_ops;
345 
346 	/* lowpan interface, set when the wpan_dev belongs to one lowpan_dev */
347 	struct net_device *lowpan_dev;
348 
349 	u32 identifier;
350 
351 	/* MAC PIB */
352 	__le16 pan_id;
353 	__le16 short_addr;
354 	__le64 extended_addr;
355 
356 	/* MAC BSN field */
357 	atomic_t bsn;
358 	/* MAC DSN field */
359 	atomic_t dsn;
360 
361 	u8 min_be;
362 	u8 max_be;
363 	u8 csma_retries;
364 	s8 frame_retries;
365 
366 	bool lbt;
367 
368 	bool promiscuous_mode;
369 
370 	/* fallback for acknowledgment bit setting */
371 	bool ackreq;
372 };
373 
374 #define to_phy(_dev)	container_of(_dev, struct wpan_phy, dev)
375 
376 #if IS_ENABLED(CONFIG_IEEE802154) || IS_ENABLED(CONFIG_6LOWPAN)
377 static inline int
wpan_dev_hard_header(struct sk_buff * skb,struct net_device * dev,const struct ieee802154_addr * daddr,const struct ieee802154_addr * saddr,unsigned int len)378 wpan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
379 		     const struct ieee802154_addr *daddr,
380 		     const struct ieee802154_addr *saddr,
381 		     unsigned int len)
382 {
383 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
384 
385 	return wpan_dev->header_ops->create(skb, dev, daddr, saddr, len);
386 }
387 #endif
388 
389 struct wpan_phy *
390 wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size);
wpan_phy_set_dev(struct wpan_phy * phy,struct device * dev)391 static inline void wpan_phy_set_dev(struct wpan_phy *phy, struct device *dev)
392 {
393 	phy->dev.parent = dev;
394 }
395 
396 int wpan_phy_register(struct wpan_phy *phy);
397 void wpan_phy_unregister(struct wpan_phy *phy);
398 void wpan_phy_free(struct wpan_phy *phy);
399 /* Same semantics as for class_for_each_device */
400 int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), void *data);
401 
wpan_phy_priv(struct wpan_phy * phy)402 static inline void *wpan_phy_priv(struct wpan_phy *phy)
403 {
404 	BUG_ON(!phy);
405 	return &phy->priv;
406 }
407 
408 struct wpan_phy *wpan_phy_find(const char *str);
409 
wpan_phy_put(struct wpan_phy * phy)410 static inline void wpan_phy_put(struct wpan_phy *phy)
411 {
412 	put_device(&phy->dev);
413 }
414 
wpan_phy_name(struct wpan_phy * phy)415 static inline const char *wpan_phy_name(struct wpan_phy *phy)
416 {
417 	return dev_name(&phy->dev);
418 }
419 
420 void ieee802154_configure_durations(struct wpan_phy *phy);
421 
422 #endif /* __NET_CFG802154_H */
423