mac802154: Handle passive scanning
[linux-block.git] / net / mac802154 / cfg.c
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 *
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
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
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
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
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
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
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 a scanning operation */
118         if (mac802154_is_scanning(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
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
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
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
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
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
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
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
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
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
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
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
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
293 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
294 static void
295 ieee802154_get_llsec_table(struct wpan_phy *wpan_phy,
296                            struct wpan_dev *wpan_dev,
297                            struct ieee802154_llsec_table **table)
298 {
299         struct net_device *dev = wpan_dev->netdev;
300         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
301
302         *table = &sdata->sec.table;
303 }
304
305 static void
306 ieee802154_lock_llsec_table(struct wpan_phy *wpan_phy,
307                             struct wpan_dev *wpan_dev)
308 {
309         struct net_device *dev = wpan_dev->netdev;
310         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
311
312         mutex_lock(&sdata->sec_mtx);
313 }
314
315 static void
316 ieee802154_unlock_llsec_table(struct wpan_phy *wpan_phy,
317                               struct wpan_dev *wpan_dev)
318 {
319         struct net_device *dev = wpan_dev->netdev;
320         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
321
322         mutex_unlock(&sdata->sec_mtx);
323 }
324
325 static int
326 ieee802154_set_llsec_params(struct wpan_phy *wpan_phy,
327                             struct wpan_dev *wpan_dev,
328                             const struct ieee802154_llsec_params *params,
329                             int changed)
330 {
331         struct net_device *dev = wpan_dev->netdev;
332         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
333         int res;
334
335         mutex_lock(&sdata->sec_mtx);
336         res = mac802154_llsec_set_params(&sdata->sec, params, changed);
337         mutex_unlock(&sdata->sec_mtx);
338
339         return res;
340 }
341
342 static int
343 ieee802154_get_llsec_params(struct wpan_phy *wpan_phy,
344                             struct wpan_dev *wpan_dev,
345                             struct ieee802154_llsec_params *params)
346 {
347         struct net_device *dev = wpan_dev->netdev;
348         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
349         int res;
350
351         mutex_lock(&sdata->sec_mtx);
352         res = mac802154_llsec_get_params(&sdata->sec, params);
353         mutex_unlock(&sdata->sec_mtx);
354
355         return res;
356 }
357
358 static int
359 ieee802154_add_llsec_key(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
360                          const struct ieee802154_llsec_key_id *id,
361                          const struct ieee802154_llsec_key *key)
362 {
363         struct net_device *dev = wpan_dev->netdev;
364         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
365         int res;
366
367         mutex_lock(&sdata->sec_mtx);
368         res = mac802154_llsec_key_add(&sdata->sec, id, key);
369         mutex_unlock(&sdata->sec_mtx);
370
371         return res;
372 }
373
374 static int
375 ieee802154_del_llsec_key(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
376                          const struct ieee802154_llsec_key_id *id)
377 {
378         struct net_device *dev = wpan_dev->netdev;
379         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
380         int res;
381
382         mutex_lock(&sdata->sec_mtx);
383         res = mac802154_llsec_key_del(&sdata->sec, id);
384         mutex_unlock(&sdata->sec_mtx);
385
386         return res;
387 }
388
389 static int
390 ieee802154_add_seclevel(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
391                         const struct ieee802154_llsec_seclevel *sl)
392 {
393         struct net_device *dev = wpan_dev->netdev;
394         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
395         int res;
396
397         mutex_lock(&sdata->sec_mtx);
398         res = mac802154_llsec_seclevel_add(&sdata->sec, sl);
399         mutex_unlock(&sdata->sec_mtx);
400
401         return res;
402 }
403
404 static int
405 ieee802154_del_seclevel(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
406                         const struct ieee802154_llsec_seclevel *sl)
407 {
408         struct net_device *dev = wpan_dev->netdev;
409         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
410         int res;
411
412         mutex_lock(&sdata->sec_mtx);
413         res = mac802154_llsec_seclevel_del(&sdata->sec, sl);
414         mutex_unlock(&sdata->sec_mtx);
415
416         return res;
417 }
418
419 static int
420 ieee802154_add_device(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
421                       const struct ieee802154_llsec_device *dev_desc)
422 {
423         struct net_device *dev = wpan_dev->netdev;
424         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
425         int res;
426
427         mutex_lock(&sdata->sec_mtx);
428         res = mac802154_llsec_dev_add(&sdata->sec, dev_desc);
429         mutex_unlock(&sdata->sec_mtx);
430
431         return res;
432 }
433
434 static int
435 ieee802154_del_device(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
436                       __le64 extended_addr)
437 {
438         struct net_device *dev = wpan_dev->netdev;
439         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
440         int res;
441
442         mutex_lock(&sdata->sec_mtx);
443         res = mac802154_llsec_dev_del(&sdata->sec, extended_addr);
444         mutex_unlock(&sdata->sec_mtx);
445
446         return res;
447 }
448
449 static int
450 ieee802154_add_devkey(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
451                       __le64 extended_addr,
452                       const struct ieee802154_llsec_device_key *key)
453 {
454         struct net_device *dev = wpan_dev->netdev;
455         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
456         int res;
457
458         mutex_lock(&sdata->sec_mtx);
459         res = mac802154_llsec_devkey_add(&sdata->sec, extended_addr, key);
460         mutex_unlock(&sdata->sec_mtx);
461
462         return res;
463 }
464
465 static int
466 ieee802154_del_devkey(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
467                       __le64 extended_addr,
468                       const struct ieee802154_llsec_device_key *key)
469 {
470         struct net_device *dev = wpan_dev->netdev;
471         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
472         int res;
473
474         mutex_lock(&sdata->sec_mtx);
475         res = mac802154_llsec_devkey_del(&sdata->sec, extended_addr, key);
476         mutex_unlock(&sdata->sec_mtx);
477
478         return res;
479 }
480 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
481
482 const struct cfg802154_ops mac802154_config_ops = {
483         .add_virtual_intf_deprecated = ieee802154_add_iface_deprecated,
484         .del_virtual_intf_deprecated = ieee802154_del_iface_deprecated,
485         .suspend = ieee802154_suspend,
486         .resume = ieee802154_resume,
487         .add_virtual_intf = ieee802154_add_iface,
488         .del_virtual_intf = ieee802154_del_iface,
489         .set_channel = ieee802154_set_channel,
490         .set_cca_mode = ieee802154_set_cca_mode,
491         .set_cca_ed_level = ieee802154_set_cca_ed_level,
492         .set_tx_power = ieee802154_set_tx_power,
493         .set_pan_id = ieee802154_set_pan_id,
494         .set_short_addr = ieee802154_set_short_addr,
495         .set_backoff_exponent = ieee802154_set_backoff_exponent,
496         .set_max_csma_backoffs = ieee802154_set_max_csma_backoffs,
497         .set_max_frame_retries = ieee802154_set_max_frame_retries,
498         .set_lbt_mode = ieee802154_set_lbt_mode,
499         .set_ackreq_default = ieee802154_set_ackreq_default,
500         .trigger_scan = mac802154_trigger_scan,
501         .abort_scan = mac802154_abort_scan,
502 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
503         .get_llsec_table = ieee802154_get_llsec_table,
504         .lock_llsec_table = ieee802154_lock_llsec_table,
505         .unlock_llsec_table = ieee802154_unlock_llsec_table,
506         /* TODO above */
507         .set_llsec_params = ieee802154_set_llsec_params,
508         .get_llsec_params = ieee802154_get_llsec_params,
509         .add_llsec_key = ieee802154_add_llsec_key,
510         .del_llsec_key = ieee802154_del_llsec_key,
511         .add_seclevel = ieee802154_add_seclevel,
512         .del_seclevel = ieee802154_del_seclevel,
513         .add_device = ieee802154_add_device,
514         .del_device = ieee802154_del_device,
515         .add_devkey = ieee802154_add_devkey,
516         .del_devkey = ieee802154_del_devkey,
517 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
518 };