xref: /linux/net/mac802154/cfg.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Authors:
5  * Alexander Aring <aar@pengutronix.de>
6  *
7  * Based on: net/mac80211/cfg.c
8  */
9 
10 #include <net/rtnetlink.h>
11 #include <net/cfg802154.h>
12 
13 #include "ieee802154_i.h"
14 #include "driver-ops.h"
15 #include "cfg.h"
16 
17 static struct net_device *
ieee802154_add_iface_deprecated(struct wpan_phy * wpan_phy,const char * name,unsigned char name_assign_type,int type)18 ieee802154_add_iface_deprecated(struct wpan_phy *wpan_phy,
19 				const char *name,
20 				unsigned char name_assign_type, int type)
21 {
22 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
23 	struct net_device *dev;
24 
25 	rtnl_lock();
26 	dev = ieee802154_if_add(local, name, name_assign_type, type,
27 				cpu_to_le64(0x0000000000000000ULL));
28 	rtnl_unlock();
29 
30 	return dev;
31 }
32 
ieee802154_del_iface_deprecated(struct wpan_phy * wpan_phy,struct net_device * dev)33 static void ieee802154_del_iface_deprecated(struct wpan_phy *wpan_phy,
34 					    struct net_device *dev)
35 {
36 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
37 
38 	ieee802154_if_remove(sdata);
39 }
40 
41 #ifdef CONFIG_PM
ieee802154_suspend(struct wpan_phy * wpan_phy)42 static int ieee802154_suspend(struct wpan_phy *wpan_phy)
43 {
44 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
45 
46 	if (!local->open_count)
47 		goto suspend;
48 
49 	ieee802154_sync_and_hold_queue(local);
50 	synchronize_net();
51 
52 	/* stop hardware - this must stop RX */
53 	ieee802154_stop_device(local);
54 
55 suspend:
56 	local->suspended = true;
57 	return 0;
58 }
59 
ieee802154_resume(struct wpan_phy * wpan_phy)60 static int ieee802154_resume(struct wpan_phy *wpan_phy)
61 {
62 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
63 	int ret;
64 
65 	/* nothing to do if HW shouldn't run */
66 	if (!local->open_count)
67 		goto wake_up;
68 
69 	/* restart hardware */
70 	ret = drv_start(local, local->phy->filtering, &local->addr_filt);
71 	if (ret)
72 		return ret;
73 
74 wake_up:
75 	ieee802154_release_queue(local);
76 	local->suspended = false;
77 	return 0;
78 }
79 #else
80 #define ieee802154_suspend NULL
81 #define ieee802154_resume NULL
82 #endif
83 
84 static int
ieee802154_add_iface(struct wpan_phy * phy,const char * name,unsigned char name_assign_type,enum nl802154_iftype type,__le64 extended_addr)85 ieee802154_add_iface(struct wpan_phy *phy, const char *name,
86 		     unsigned char name_assign_type,
87 		     enum nl802154_iftype type, __le64 extended_addr)
88 {
89 	struct ieee802154_local *local = wpan_phy_priv(phy);
90 	struct net_device *err;
91 
92 	err = ieee802154_if_add(local, name, name_assign_type, type,
93 				extended_addr);
94 	return PTR_ERR_OR_ZERO(err);
95 }
96 
97 static int
ieee802154_del_iface(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev)98 ieee802154_del_iface(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev)
99 {
100 	ieee802154_if_remove(IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev));
101 
102 	return 0;
103 }
104 
105 static int
ieee802154_set_channel(struct wpan_phy * wpan_phy,u8 page,u8 channel)106 ieee802154_set_channel(struct wpan_phy *wpan_phy, u8 page, u8 channel)
107 {
108 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
109 	int ret;
110 
111 	ASSERT_RTNL();
112 
113 	if (wpan_phy->current_page == page &&
114 	    wpan_phy->current_channel == channel)
115 		return 0;
116 
117 	/* Refuse to change channels during scanning or beaconing */
118 	if (mac802154_is_scanning(local) || mac802154_is_beaconing(local))
119 		return -EBUSY;
120 
121 	ret = drv_set_channel(local, page, channel);
122 	if (!ret) {
123 		wpan_phy->current_page = page;
124 		wpan_phy->current_channel = channel;
125 		ieee802154_configure_durations(wpan_phy, page, channel);
126 	}
127 
128 	return ret;
129 }
130 
131 static int
ieee802154_set_cca_mode(struct wpan_phy * wpan_phy,const struct wpan_phy_cca * cca)132 ieee802154_set_cca_mode(struct wpan_phy *wpan_phy,
133 			const struct wpan_phy_cca *cca)
134 {
135 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
136 	int ret;
137 
138 	ASSERT_RTNL();
139 
140 	if (wpan_phy_cca_cmp(&wpan_phy->cca, cca))
141 		return 0;
142 
143 	ret = drv_set_cca_mode(local, cca);
144 	if (!ret)
145 		wpan_phy->cca = *cca;
146 
147 	return ret;
148 }
149 
150 static int
ieee802154_set_cca_ed_level(struct wpan_phy * wpan_phy,s32 ed_level)151 ieee802154_set_cca_ed_level(struct wpan_phy *wpan_phy, s32 ed_level)
152 {
153 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
154 	int ret;
155 
156 	ASSERT_RTNL();
157 
158 	if (wpan_phy->cca_ed_level == ed_level)
159 		return 0;
160 
161 	ret = drv_set_cca_ed_level(local, ed_level);
162 	if (!ret)
163 		wpan_phy->cca_ed_level = ed_level;
164 
165 	return ret;
166 }
167 
168 static int
ieee802154_set_tx_power(struct wpan_phy * wpan_phy,s32 power)169 ieee802154_set_tx_power(struct wpan_phy *wpan_phy, s32 power)
170 {
171 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
172 	int ret;
173 
174 	ASSERT_RTNL();
175 
176 	if (wpan_phy->transmit_power == power)
177 		return 0;
178 
179 	ret = drv_set_tx_power(local, power);
180 	if (!ret)
181 		wpan_phy->transmit_power = power;
182 
183 	return ret;
184 }
185 
186 static int
ieee802154_set_pan_id(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,__le16 pan_id)187 ieee802154_set_pan_id(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
188 		      __le16 pan_id)
189 {
190 	int ret;
191 
192 	ASSERT_RTNL();
193 
194 	if (wpan_dev->pan_id == pan_id)
195 		return 0;
196 
197 	ret = mac802154_wpan_update_llsec(wpan_dev->netdev);
198 	if (!ret)
199 		wpan_dev->pan_id = pan_id;
200 
201 	return ret;
202 }
203 
204 static int
ieee802154_set_backoff_exponent(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,u8 min_be,u8 max_be)205 ieee802154_set_backoff_exponent(struct wpan_phy *wpan_phy,
206 				struct wpan_dev *wpan_dev,
207 				u8 min_be, u8 max_be)
208 {
209 	ASSERT_RTNL();
210 
211 	wpan_dev->min_be = min_be;
212 	wpan_dev->max_be = max_be;
213 	return 0;
214 }
215 
216 static int
ieee802154_set_short_addr(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,__le16 short_addr)217 ieee802154_set_short_addr(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
218 			  __le16 short_addr)
219 {
220 	ASSERT_RTNL();
221 
222 	wpan_dev->short_addr = short_addr;
223 	return 0;
224 }
225 
226 static int
ieee802154_set_max_csma_backoffs(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,u8 max_csma_backoffs)227 ieee802154_set_max_csma_backoffs(struct wpan_phy *wpan_phy,
228 				 struct wpan_dev *wpan_dev,
229 				 u8 max_csma_backoffs)
230 {
231 	ASSERT_RTNL();
232 
233 	wpan_dev->csma_retries = max_csma_backoffs;
234 	return 0;
235 }
236 
237 static int
ieee802154_set_max_frame_retries(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,s8 max_frame_retries)238 ieee802154_set_max_frame_retries(struct wpan_phy *wpan_phy,
239 				 struct wpan_dev *wpan_dev,
240 				 s8 max_frame_retries)
241 {
242 	ASSERT_RTNL();
243 
244 	wpan_dev->frame_retries = max_frame_retries;
245 	return 0;
246 }
247 
248 static int
ieee802154_set_lbt_mode(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,bool mode)249 ieee802154_set_lbt_mode(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
250 			bool mode)
251 {
252 	ASSERT_RTNL();
253 
254 	wpan_dev->lbt = mode;
255 	return 0;
256 }
257 
258 static int
ieee802154_set_ackreq_default(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,bool ackreq)259 ieee802154_set_ackreq_default(struct wpan_phy *wpan_phy,
260 			      struct wpan_dev *wpan_dev, bool ackreq)
261 {
262 	ASSERT_RTNL();
263 
264 	wpan_dev->ackreq = ackreq;
265 	return 0;
266 }
267 
mac802154_trigger_scan(struct wpan_phy * wpan_phy,struct cfg802154_scan_request * request)268 static int mac802154_trigger_scan(struct wpan_phy *wpan_phy,
269 				  struct cfg802154_scan_request *request)
270 {
271 	struct ieee802154_sub_if_data *sdata;
272 
273 	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(request->wpan_dev);
274 
275 	ASSERT_RTNL();
276 
277 	return mac802154_trigger_scan_locked(sdata, request);
278 }
279 
mac802154_abort_scan(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev)280 static int mac802154_abort_scan(struct wpan_phy *wpan_phy,
281 				struct wpan_dev *wpan_dev)
282 {
283 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
284 	struct ieee802154_sub_if_data *sdata;
285 
286 	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev);
287 
288 	ASSERT_RTNL();
289 
290 	return mac802154_abort_scan_locked(local, sdata);
291 }
292 
mac802154_send_beacons(struct wpan_phy * wpan_phy,struct cfg802154_beacon_request * request)293 static int mac802154_send_beacons(struct wpan_phy *wpan_phy,
294 				  struct cfg802154_beacon_request *request)
295 {
296 	struct ieee802154_sub_if_data *sdata;
297 
298 	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(request->wpan_dev);
299 
300 	ASSERT_RTNL();
301 
302 	return mac802154_send_beacons_locked(sdata, request);
303 }
304 
mac802154_stop_beacons(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev)305 static int mac802154_stop_beacons(struct wpan_phy *wpan_phy,
306 				  struct wpan_dev *wpan_dev)
307 {
308 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
309 	struct ieee802154_sub_if_data *sdata;
310 
311 	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev);
312 
313 	ASSERT_RTNL();
314 
315 	return mac802154_stop_beacons_locked(local, sdata);
316 }
317 
mac802154_associate(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,struct ieee802154_addr * coord)318 static int mac802154_associate(struct wpan_phy *wpan_phy,
319 			       struct wpan_dev *wpan_dev,
320 			       struct ieee802154_addr *coord)
321 {
322 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
323 	u64 ceaddr = swab64((__force u64)coord->extended_addr);
324 	struct ieee802154_sub_if_data *sdata;
325 	struct ieee802154_pan_device *parent;
326 	__le16 short_addr;
327 	int ret;
328 
329 	ASSERT_RTNL();
330 
331 	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev);
332 
333 	if (wpan_dev->parent) {
334 		dev_err(&sdata->dev->dev,
335 			"Device %8phC is already associated\n", &ceaddr);
336 		return -EPERM;
337 	}
338 
339 	if (coord->mode == IEEE802154_SHORT_ADDRESSING)
340 		return -EINVAL;
341 
342 	parent = kzalloc(sizeof(*parent), GFP_KERNEL);
343 	if (!parent)
344 		return -ENOMEM;
345 
346 	parent->pan_id = coord->pan_id;
347 	parent->mode = coord->mode;
348 	parent->extended_addr = coord->extended_addr;
349 	parent->short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST);
350 
351 	/* Set the PAN ID hardware address filter beforehand to avoid dropping
352 	 * the association response with a destination PAN ID field set to the
353 	 * "new" PAN ID.
354 	 */
355 	if (local->hw.flags & IEEE802154_HW_AFILT) {
356 		ret = drv_set_pan_id(local, coord->pan_id);
357 		if (ret < 0)
358 			goto free_parent;
359 	}
360 
361 	ret = mac802154_perform_association(sdata, parent, &short_addr);
362 	if (ret)
363 		goto reset_panid;
364 
365 	if (local->hw.flags & IEEE802154_HW_AFILT) {
366 		ret = drv_set_short_addr(local, short_addr);
367 		if (ret < 0)
368 			goto reset_panid;
369 	}
370 
371 	wpan_dev->pan_id = coord->pan_id;
372 	wpan_dev->short_addr = short_addr;
373 	wpan_dev->parent = parent;
374 
375 	return 0;
376 
377 reset_panid:
378 	if (local->hw.flags & IEEE802154_HW_AFILT)
379 		drv_set_pan_id(local, cpu_to_le16(IEEE802154_PAN_ID_BROADCAST));
380 
381 free_parent:
382 	kfree(parent);
383 	return ret;
384 }
385 
mac802154_disassociate_from_parent(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev)386 static int mac802154_disassociate_from_parent(struct wpan_phy *wpan_phy,
387 					      struct wpan_dev *wpan_dev)
388 {
389 	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
390 	struct ieee802154_pan_device *child, *tmp;
391 	struct ieee802154_sub_if_data *sdata;
392 	unsigned int max_assoc;
393 	u64 eaddr;
394 	int ret;
395 
396 	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev);
397 
398 	/* Start by disassociating all the children and preventing new ones to
399 	 * attempt associations.
400 	 */
401 	max_assoc = cfg802154_set_max_associations(wpan_dev, 0);
402 	list_for_each_entry_safe(child, tmp, &wpan_dev->children, node) {
403 		ret = mac802154_send_disassociation_notif(sdata, child,
404 							  IEEE802154_COORD_WISHES_DEVICE_TO_LEAVE);
405 		if (ret) {
406 			eaddr = swab64((__force u64)child->extended_addr);
407 			dev_err(&sdata->dev->dev,
408 				"Disassociation with %8phC may have failed (%d)\n",
409 				&eaddr, ret);
410 		}
411 
412 		list_del(&child->node);
413 	}
414 
415 	ret = mac802154_send_disassociation_notif(sdata, wpan_dev->parent,
416 						  IEEE802154_DEVICE_WISHES_TO_LEAVE);
417 	if (ret) {
418 		eaddr = swab64((__force u64)wpan_dev->parent->extended_addr);
419 		dev_err(&sdata->dev->dev,
420 			"Disassociation from %8phC may have failed (%d)\n",
421 			&eaddr, ret);
422 	}
423 
424 	ret = 0;
425 
426 	kfree(wpan_dev->parent);
427 	wpan_dev->parent = NULL;
428 	wpan_dev->pan_id = cpu_to_le16(IEEE802154_PAN_ID_BROADCAST);
429 	wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST);
430 
431 	if (local->hw.flags & IEEE802154_HW_AFILT) {
432 		ret = drv_set_pan_id(local, wpan_dev->pan_id);
433 		if (ret < 0)
434 			goto reset_mac_assoc;
435 
436 		ret = drv_set_short_addr(local, wpan_dev->short_addr);
437 		if (ret < 0)
438 			goto reset_mac_assoc;
439 	}
440 
441 reset_mac_assoc:
442 	cfg802154_set_max_associations(wpan_dev, max_assoc);
443 
444 	return ret;
445 }
446 
mac802154_disassociate_child(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,struct ieee802154_pan_device * child)447 static int mac802154_disassociate_child(struct wpan_phy *wpan_phy,
448 					struct wpan_dev *wpan_dev,
449 					struct ieee802154_pan_device *child)
450 {
451 	struct ieee802154_sub_if_data *sdata;
452 	int ret;
453 
454 	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev);
455 
456 	ret = mac802154_send_disassociation_notif(sdata, child,
457 						  IEEE802154_COORD_WISHES_DEVICE_TO_LEAVE);
458 	if (ret)
459 		return ret;
460 
461 	list_del(&child->node);
462 	wpan_dev->nchildren--;
463 	kfree(child);
464 
465 	return 0;
466 }
467 
mac802154_disassociate(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,struct ieee802154_addr * target)468 static int mac802154_disassociate(struct wpan_phy *wpan_phy,
469 				  struct wpan_dev *wpan_dev,
470 				  struct ieee802154_addr *target)
471 {
472 	u64 teaddr = swab64((__force u64)target->extended_addr);
473 	struct ieee802154_pan_device *pan_device;
474 
475 	ASSERT_RTNL();
476 
477 	if (cfg802154_device_is_parent(wpan_dev, target))
478 		return mac802154_disassociate_from_parent(wpan_phy, wpan_dev);
479 
480 	pan_device = cfg802154_device_is_child(wpan_dev, target);
481 	if (pan_device)
482 		return mac802154_disassociate_child(wpan_phy, wpan_dev,
483 						    pan_device);
484 
485 	dev_err(&wpan_dev->netdev->dev,
486 		"Device %8phC is not associated with us\n", &teaddr);
487 
488 	return -EINVAL;
489 }
490 
491 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
492 static void
ieee802154_get_llsec_table(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,struct ieee802154_llsec_table ** table)493 ieee802154_get_llsec_table(struct wpan_phy *wpan_phy,
494 			   struct wpan_dev *wpan_dev,
495 			   struct ieee802154_llsec_table **table)
496 {
497 	struct net_device *dev = wpan_dev->netdev;
498 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
499 
500 	*table = &sdata->sec.table;
501 }
502 
503 static void
ieee802154_lock_llsec_table(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev)504 ieee802154_lock_llsec_table(struct wpan_phy *wpan_phy,
505 			    struct wpan_dev *wpan_dev)
506 {
507 	struct net_device *dev = wpan_dev->netdev;
508 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
509 
510 	mutex_lock(&sdata->sec_mtx);
511 }
512 
513 static void
ieee802154_unlock_llsec_table(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev)514 ieee802154_unlock_llsec_table(struct wpan_phy *wpan_phy,
515 			      struct wpan_dev *wpan_dev)
516 {
517 	struct net_device *dev = wpan_dev->netdev;
518 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
519 
520 	mutex_unlock(&sdata->sec_mtx);
521 }
522 
523 static int
ieee802154_set_llsec_params(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,const struct ieee802154_llsec_params * params,int changed)524 ieee802154_set_llsec_params(struct wpan_phy *wpan_phy,
525 			    struct wpan_dev *wpan_dev,
526 			    const struct ieee802154_llsec_params *params,
527 			    int changed)
528 {
529 	struct net_device *dev = wpan_dev->netdev;
530 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
531 	int res;
532 
533 	mutex_lock(&sdata->sec_mtx);
534 	res = mac802154_llsec_set_params(&sdata->sec, params, changed);
535 	mutex_unlock(&sdata->sec_mtx);
536 
537 	return res;
538 }
539 
540 static int
ieee802154_get_llsec_params(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,struct ieee802154_llsec_params * params)541 ieee802154_get_llsec_params(struct wpan_phy *wpan_phy,
542 			    struct wpan_dev *wpan_dev,
543 			    struct ieee802154_llsec_params *params)
544 {
545 	struct net_device *dev = wpan_dev->netdev;
546 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
547 	int res;
548 
549 	mutex_lock(&sdata->sec_mtx);
550 	res = mac802154_llsec_get_params(&sdata->sec, params);
551 	mutex_unlock(&sdata->sec_mtx);
552 
553 	return res;
554 }
555 
556 static int
ieee802154_add_llsec_key(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,const struct ieee802154_llsec_key_id * id,const struct ieee802154_llsec_key * key)557 ieee802154_add_llsec_key(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
558 			 const struct ieee802154_llsec_key_id *id,
559 			 const struct ieee802154_llsec_key *key)
560 {
561 	struct net_device *dev = wpan_dev->netdev;
562 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
563 	int res;
564 
565 	mutex_lock(&sdata->sec_mtx);
566 	res = mac802154_llsec_key_add(&sdata->sec, id, key);
567 	mutex_unlock(&sdata->sec_mtx);
568 
569 	return res;
570 }
571 
572 static int
ieee802154_del_llsec_key(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,const struct ieee802154_llsec_key_id * id)573 ieee802154_del_llsec_key(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
574 			 const struct ieee802154_llsec_key_id *id)
575 {
576 	struct net_device *dev = wpan_dev->netdev;
577 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
578 	int res;
579 
580 	mutex_lock(&sdata->sec_mtx);
581 	res = mac802154_llsec_key_del(&sdata->sec, id);
582 	mutex_unlock(&sdata->sec_mtx);
583 
584 	return res;
585 }
586 
587 static int
ieee802154_add_seclevel(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,const struct ieee802154_llsec_seclevel * sl)588 ieee802154_add_seclevel(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
589 			const struct ieee802154_llsec_seclevel *sl)
590 {
591 	struct net_device *dev = wpan_dev->netdev;
592 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
593 	int res;
594 
595 	mutex_lock(&sdata->sec_mtx);
596 	res = mac802154_llsec_seclevel_add(&sdata->sec, sl);
597 	mutex_unlock(&sdata->sec_mtx);
598 
599 	return res;
600 }
601 
602 static int
ieee802154_del_seclevel(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,const struct ieee802154_llsec_seclevel * sl)603 ieee802154_del_seclevel(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
604 			const struct ieee802154_llsec_seclevel *sl)
605 {
606 	struct net_device *dev = wpan_dev->netdev;
607 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
608 	int res;
609 
610 	mutex_lock(&sdata->sec_mtx);
611 	res = mac802154_llsec_seclevel_del(&sdata->sec, sl);
612 	mutex_unlock(&sdata->sec_mtx);
613 
614 	return res;
615 }
616 
617 static int
ieee802154_add_device(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,const struct ieee802154_llsec_device * dev_desc)618 ieee802154_add_device(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
619 		      const struct ieee802154_llsec_device *dev_desc)
620 {
621 	struct net_device *dev = wpan_dev->netdev;
622 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
623 	int res;
624 
625 	mutex_lock(&sdata->sec_mtx);
626 	res = mac802154_llsec_dev_add(&sdata->sec, dev_desc);
627 	mutex_unlock(&sdata->sec_mtx);
628 
629 	return res;
630 }
631 
632 static int
ieee802154_del_device(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,__le64 extended_addr)633 ieee802154_del_device(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
634 		      __le64 extended_addr)
635 {
636 	struct net_device *dev = wpan_dev->netdev;
637 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
638 	int res;
639 
640 	mutex_lock(&sdata->sec_mtx);
641 	res = mac802154_llsec_dev_del(&sdata->sec, extended_addr);
642 	mutex_unlock(&sdata->sec_mtx);
643 
644 	return res;
645 }
646 
647 static int
ieee802154_add_devkey(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,__le64 extended_addr,const struct ieee802154_llsec_device_key * key)648 ieee802154_add_devkey(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
649 		      __le64 extended_addr,
650 		      const struct ieee802154_llsec_device_key *key)
651 {
652 	struct net_device *dev = wpan_dev->netdev;
653 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
654 	int res;
655 
656 	mutex_lock(&sdata->sec_mtx);
657 	res = mac802154_llsec_devkey_add(&sdata->sec, extended_addr, key);
658 	mutex_unlock(&sdata->sec_mtx);
659 
660 	return res;
661 }
662 
663 static int
ieee802154_del_devkey(struct wpan_phy * wpan_phy,struct wpan_dev * wpan_dev,__le64 extended_addr,const struct ieee802154_llsec_device_key * key)664 ieee802154_del_devkey(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
665 		      __le64 extended_addr,
666 		      const struct ieee802154_llsec_device_key *key)
667 {
668 	struct net_device *dev = wpan_dev->netdev;
669 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
670 	int res;
671 
672 	mutex_lock(&sdata->sec_mtx);
673 	res = mac802154_llsec_devkey_del(&sdata->sec, extended_addr, key);
674 	mutex_unlock(&sdata->sec_mtx);
675 
676 	return res;
677 }
678 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
679 
680 const struct cfg802154_ops mac802154_config_ops = {
681 	.add_virtual_intf_deprecated = ieee802154_add_iface_deprecated,
682 	.del_virtual_intf_deprecated = ieee802154_del_iface_deprecated,
683 	.suspend = ieee802154_suspend,
684 	.resume = ieee802154_resume,
685 	.add_virtual_intf = ieee802154_add_iface,
686 	.del_virtual_intf = ieee802154_del_iface,
687 	.set_channel = ieee802154_set_channel,
688 	.set_cca_mode = ieee802154_set_cca_mode,
689 	.set_cca_ed_level = ieee802154_set_cca_ed_level,
690 	.set_tx_power = ieee802154_set_tx_power,
691 	.set_pan_id = ieee802154_set_pan_id,
692 	.set_short_addr = ieee802154_set_short_addr,
693 	.set_backoff_exponent = ieee802154_set_backoff_exponent,
694 	.set_max_csma_backoffs = ieee802154_set_max_csma_backoffs,
695 	.set_max_frame_retries = ieee802154_set_max_frame_retries,
696 	.set_lbt_mode = ieee802154_set_lbt_mode,
697 	.set_ackreq_default = ieee802154_set_ackreq_default,
698 	.trigger_scan = mac802154_trigger_scan,
699 	.abort_scan = mac802154_abort_scan,
700 	.send_beacons = mac802154_send_beacons,
701 	.stop_beacons = mac802154_stop_beacons,
702 	.associate = mac802154_associate,
703 	.disassociate = mac802154_disassociate,
704 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
705 	.get_llsec_table = ieee802154_get_llsec_table,
706 	.lock_llsec_table = ieee802154_lock_llsec_table,
707 	.unlock_llsec_table = ieee802154_unlock_llsec_table,
708 	/* TODO above */
709 	.set_llsec_params = ieee802154_set_llsec_params,
710 	.get_llsec_params = ieee802154_get_llsec_params,
711 	.add_llsec_key = ieee802154_add_llsec_key,
712 	.del_llsec_key = ieee802154_del_llsec_key,
713 	.add_seclevel = ieee802154_add_seclevel,
714 	.del_seclevel = ieee802154_del_seclevel,
715 	.add_device = ieee802154_add_device,
716 	.del_device = ieee802154_del_device,
717 	.add_devkey = ieee802154_add_devkey,
718 	.del_devkey = ieee802154_del_devkey,
719 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
720 };
721