1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Interface handling
4 *
5 * Copyright 2002-2005, Instant802 Networks, Inc.
6 * Copyright 2005-2006, Devicescape Software, Inc.
7 * Copyright (c) 2006 Jiri Benc <[email protected]>
8 * Copyright 2008, Johannes Berg <[email protected]>
9 * Copyright 2013-2014 Intel Mobile Communications GmbH
10 * Copyright (c) 2016 Intel Deutschland GmbH
11 * Copyright (C) 2018-2025 Intel Corporation
12 */
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/if_arp.h>
16 #include <linux/netdevice.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/kcov.h>
19 #include <net/mac80211.h>
20 #include <net/ieee80211_radiotap.h>
21 #include "ieee80211_i.h"
22 #include "sta_info.h"
23 #include "debugfs_netdev.h"
24 #include "mesh.h"
25 #include "led.h"
26 #include "driver-ops.h"
27 #include "wme.h"
28 #include "rate.h"
29
30 /**
31 * DOC: Interface list locking
32 *
33 * The interface list in each struct ieee80211_local is protected
34 * three-fold:
35 *
36 * (1) modifications may only be done under the RTNL *and* wiphy mutex
37 * *and* iflist_mtx
38 * (2) modifications are done in an RCU manner so atomic readers
39 * can traverse the list in RCU-safe blocks.
40 *
41 * As a consequence, reads (traversals) of the list can be protected
42 * by either the RTNL, the wiphy mutex, the iflist_mtx or RCU.
43 */
44
45 static void ieee80211_iface_work(struct wiphy *wiphy, struct wiphy_work *work);
46
__ieee80211_recalc_txpower(struct ieee80211_link_data * link)47 bool __ieee80211_recalc_txpower(struct ieee80211_link_data *link)
48 {
49 struct ieee80211_chanctx_conf *chanctx_conf;
50 int power;
51
52 rcu_read_lock();
53 chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
54 if (!chanctx_conf) {
55 rcu_read_unlock();
56 return false;
57 }
58
59 power = ieee80211_chandef_max_power(&chanctx_conf->def);
60 rcu_read_unlock();
61
62 if (link->user_power_level != IEEE80211_UNSET_POWER_LEVEL)
63 power = min(power, link->user_power_level);
64
65 if (link->ap_power_level != IEEE80211_UNSET_POWER_LEVEL)
66 power = min(power, link->ap_power_level);
67
68 if (power != link->conf->txpower) {
69 link->conf->txpower = power;
70 return true;
71 }
72
73 return false;
74 }
75
ieee80211_recalc_txpower(struct ieee80211_link_data * link,bool update_bss)76 void ieee80211_recalc_txpower(struct ieee80211_link_data *link,
77 bool update_bss)
78 {
79 if (__ieee80211_recalc_txpower(link) ||
80 (update_bss && ieee80211_sdata_running(link->sdata)))
81 ieee80211_link_info_change_notify(link->sdata, link,
82 BSS_CHANGED_TXPOWER);
83 }
84
__ieee80211_idle_off(struct ieee80211_local * local)85 static u32 __ieee80211_idle_off(struct ieee80211_local *local)
86 {
87 if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
88 return 0;
89
90 local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
91 return IEEE80211_CONF_CHANGE_IDLE;
92 }
93
__ieee80211_idle_on(struct ieee80211_local * local)94 static u32 __ieee80211_idle_on(struct ieee80211_local *local)
95 {
96 if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
97 return 0;
98
99 ieee80211_flush_queues(local, NULL, false);
100
101 local->hw.conf.flags |= IEEE80211_CONF_IDLE;
102 return IEEE80211_CONF_CHANGE_IDLE;
103 }
104
__ieee80211_recalc_idle(struct ieee80211_local * local,bool force_active)105 static u32 __ieee80211_recalc_idle(struct ieee80211_local *local,
106 bool force_active)
107 {
108 bool working, scanning, active;
109 unsigned int led_trig_start = 0, led_trig_stop = 0;
110
111 lockdep_assert_wiphy(local->hw.wiphy);
112
113 active = force_active ||
114 !list_empty(&local->chanctx_list) ||
115 local->monitors;
116
117 working = !local->ops->remain_on_channel &&
118 !list_empty(&local->roc_list);
119
120 scanning = test_bit(SCAN_SW_SCANNING, &local->scanning) ||
121 test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
122
123 if (working || scanning)
124 led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK;
125 else
126 led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK;
127
128 if (active)
129 led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
130 else
131 led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
132
133 ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop);
134
135 if (working || scanning || active)
136 return __ieee80211_idle_off(local);
137 return __ieee80211_idle_on(local);
138 }
139
ieee80211_idle_off(struct ieee80211_local * local)140 u32 ieee80211_idle_off(struct ieee80211_local *local)
141 {
142 return __ieee80211_recalc_idle(local, true);
143 }
144
ieee80211_recalc_idle(struct ieee80211_local * local)145 void ieee80211_recalc_idle(struct ieee80211_local *local)
146 {
147 u32 change = __ieee80211_recalc_idle(local, false);
148 if (change)
149 ieee80211_hw_config(local, change);
150 }
151
ieee80211_verify_mac(struct ieee80211_sub_if_data * sdata,u8 * addr,bool check_dup)152 static int ieee80211_verify_mac(struct ieee80211_sub_if_data *sdata, u8 *addr,
153 bool check_dup)
154 {
155 struct ieee80211_local *local = sdata->local;
156 struct ieee80211_sub_if_data *iter;
157 u64 new, mask, tmp;
158 u8 *m;
159 int ret = 0;
160
161 lockdep_assert_wiphy(local->hw.wiphy);
162
163 if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
164 return 0;
165
166 m = addr;
167 new = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
168 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
169 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
170
171 m = local->hw.wiphy->addr_mask;
172 mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
173 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
174 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
175
176 if (!check_dup)
177 return ret;
178
179 list_for_each_entry(iter, &local->interfaces, list) {
180 if (iter == sdata)
181 continue;
182
183 if (iter->vif.type == NL80211_IFTYPE_MONITOR &&
184 !(iter->u.mntr.flags & MONITOR_FLAG_ACTIVE))
185 continue;
186
187 m = iter->vif.addr;
188 tmp = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
189 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
190 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
191
192 if ((new & ~mask) != (tmp & ~mask)) {
193 ret = -EINVAL;
194 break;
195 }
196 }
197
198 return ret;
199 }
200
ieee80211_can_powered_addr_change(struct ieee80211_sub_if_data * sdata)201 static int ieee80211_can_powered_addr_change(struct ieee80211_sub_if_data *sdata)
202 {
203 struct ieee80211_roc_work *roc;
204 struct ieee80211_local *local = sdata->local;
205 struct ieee80211_sub_if_data *scan_sdata;
206 int ret = 0;
207
208 lockdep_assert_wiphy(local->hw.wiphy);
209
210 /* To be the most flexible here we want to only limit changing the
211 * address if the specific interface is doing offchannel work or
212 * scanning.
213 */
214 if (netif_carrier_ok(sdata->dev))
215 return -EBUSY;
216
217 /* First check no ROC work is happening on this iface */
218 list_for_each_entry(roc, &local->roc_list, list) {
219 if (roc->sdata != sdata)
220 continue;
221
222 if (roc->started) {
223 ret = -EBUSY;
224 goto unlock;
225 }
226 }
227
228 /* And if this iface is scanning */
229 if (local->scanning) {
230 scan_sdata = rcu_dereference_protected(local->scan_sdata,
231 lockdep_is_held(&local->hw.wiphy->mtx));
232 if (sdata == scan_sdata)
233 ret = -EBUSY;
234 }
235
236 switch (sdata->vif.type) {
237 case NL80211_IFTYPE_STATION:
238 case NL80211_IFTYPE_P2P_CLIENT:
239 /* More interface types could be added here but changing the
240 * address while powered makes the most sense in client modes.
241 */
242 break;
243 default:
244 ret = -EOPNOTSUPP;
245 }
246
247 unlock:
248 return ret;
249 }
250
_ieee80211_change_mac(struct ieee80211_sub_if_data * sdata,void * addr)251 static int _ieee80211_change_mac(struct ieee80211_sub_if_data *sdata,
252 void *addr)
253 {
254 struct ieee80211_local *local = sdata->local;
255 struct sockaddr *sa = addr;
256 bool check_dup = true;
257 bool live = false;
258 int ret;
259
260 if (ieee80211_sdata_running(sdata)) {
261 ret = ieee80211_can_powered_addr_change(sdata);
262 if (ret)
263 return ret;
264
265 live = true;
266 }
267
268 if (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
269 !(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
270 check_dup = false;
271
272 ret = ieee80211_verify_mac(sdata, sa->sa_data, check_dup);
273 if (ret)
274 return ret;
275
276 if (live)
277 drv_remove_interface(local, sdata);
278 ret = eth_mac_addr(sdata->dev, sa);
279
280 if (ret == 0) {
281 memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
282 ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
283 }
284
285 /* Regardless of eth_mac_addr() return we still want to add the
286 * interface back. This should not fail...
287 */
288 if (live)
289 WARN_ON(drv_add_interface(local, sdata));
290
291 return ret;
292 }
293
ieee80211_change_mac(struct net_device * dev,void * addr)294 static int ieee80211_change_mac(struct net_device *dev, void *addr)
295 {
296 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
297 struct ieee80211_local *local = sdata->local;
298
299 /*
300 * This happens during unregistration if there's a bond device
301 * active (maybe other cases?) and we must get removed from it.
302 * But we really don't care anymore if it's not registered now.
303 */
304 if (!dev->ieee80211_ptr->registered)
305 return 0;
306
307 guard(wiphy)(local->hw.wiphy);
308
309 return _ieee80211_change_mac(sdata, addr);
310 }
311
identical_mac_addr_allowed(int type1,int type2)312 static inline int identical_mac_addr_allowed(int type1, int type2)
313 {
314 return type1 == NL80211_IFTYPE_MONITOR ||
315 type2 == NL80211_IFTYPE_MONITOR ||
316 type1 == NL80211_IFTYPE_P2P_DEVICE ||
317 type2 == NL80211_IFTYPE_P2P_DEVICE ||
318 (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
319 (type1 == NL80211_IFTYPE_AP_VLAN &&
320 (type2 == NL80211_IFTYPE_AP ||
321 type2 == NL80211_IFTYPE_AP_VLAN));
322 }
323
ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype iftype)324 static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata,
325 enum nl80211_iftype iftype)
326 {
327 struct ieee80211_local *local = sdata->local;
328 struct ieee80211_sub_if_data *nsdata;
329
330 ASSERT_RTNL();
331 lockdep_assert_wiphy(local->hw.wiphy);
332
333 /* we hold the RTNL here so can safely walk the list */
334 list_for_each_entry(nsdata, &local->interfaces, list) {
335 if (nsdata != sdata && ieee80211_sdata_running(nsdata)) {
336 /*
337 * Only OCB and monitor mode may coexist
338 */
339 if ((sdata->vif.type == NL80211_IFTYPE_OCB &&
340 nsdata->vif.type != NL80211_IFTYPE_MONITOR) ||
341 (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
342 nsdata->vif.type == NL80211_IFTYPE_OCB))
343 return -EBUSY;
344
345 /*
346 * Allow only a single IBSS interface to be up at any
347 * time. This is restricted because beacon distribution
348 * cannot work properly if both are in the same IBSS.
349 *
350 * To remove this restriction we'd have to disallow them
351 * from setting the same SSID on different IBSS interfaces
352 * belonging to the same hardware. Then, however, we're
353 * faced with having to adopt two different TSF timers...
354 */
355 if (iftype == NL80211_IFTYPE_ADHOC &&
356 nsdata->vif.type == NL80211_IFTYPE_ADHOC)
357 return -EBUSY;
358 /*
359 * will not add another interface while any channel
360 * switch is active.
361 */
362 if (nsdata->vif.bss_conf.csa_active)
363 return -EBUSY;
364
365 /*
366 * The remaining checks are only performed for interfaces
367 * with the same MAC address.
368 */
369 if (!ether_addr_equal(sdata->vif.addr,
370 nsdata->vif.addr))
371 continue;
372
373 /*
374 * check whether it may have the same address
375 */
376 if (!identical_mac_addr_allowed(iftype,
377 nsdata->vif.type))
378 return -ENOTUNIQ;
379
380 /* No support for VLAN with MLO yet */
381 if (iftype == NL80211_IFTYPE_AP_VLAN &&
382 sdata->wdev.use_4addr &&
383 nsdata->vif.type == NL80211_IFTYPE_AP &&
384 nsdata->vif.valid_links)
385 return -EOPNOTSUPP;
386
387 /*
388 * can only add VLANs to enabled APs
389 */
390 if (iftype == NL80211_IFTYPE_AP_VLAN &&
391 nsdata->vif.type == NL80211_IFTYPE_AP)
392 sdata->bss = &nsdata->u.ap;
393 }
394 }
395
396 return ieee80211_check_combinations(sdata, NULL, 0, 0, -1);
397 }
398
ieee80211_check_queues(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype iftype)399 static int ieee80211_check_queues(struct ieee80211_sub_if_data *sdata,
400 enum nl80211_iftype iftype)
401 {
402 int n_queues = sdata->local->hw.queues;
403 int i;
404
405 if (iftype == NL80211_IFTYPE_NAN)
406 return 0;
407
408 if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
409 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
410 if (WARN_ON_ONCE(sdata->vif.hw_queue[i] ==
411 IEEE80211_INVAL_HW_QUEUE))
412 return -EINVAL;
413 if (WARN_ON_ONCE(sdata->vif.hw_queue[i] >=
414 n_queues))
415 return -EINVAL;
416 }
417 }
418
419 if ((iftype != NL80211_IFTYPE_AP &&
420 iftype != NL80211_IFTYPE_P2P_GO &&
421 iftype != NL80211_IFTYPE_MESH_POINT) ||
422 !ieee80211_hw_check(&sdata->local->hw, QUEUE_CONTROL)) {
423 sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
424 return 0;
425 }
426
427 if (WARN_ON_ONCE(sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE))
428 return -EINVAL;
429
430 if (WARN_ON_ONCE(sdata->vif.cab_queue >= n_queues))
431 return -EINVAL;
432
433 return 0;
434 }
435
ieee80211_open(struct net_device * dev)436 static int ieee80211_open(struct net_device *dev)
437 {
438 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
439 int err;
440
441 /* fail early if user set an invalid address */
442 if (!is_valid_ether_addr(dev->dev_addr))
443 return -EADDRNOTAVAIL;
444
445 guard(wiphy)(sdata->local->hw.wiphy);
446
447 err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type);
448 if (err)
449 return err;
450
451 return ieee80211_do_open(&sdata->wdev, true);
452 }
453
ieee80211_do_stop(struct ieee80211_sub_if_data * sdata,bool going_down)454 static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata, bool going_down)
455 {
456 struct ieee80211_local *local = sdata->local;
457 unsigned long flags;
458 struct sk_buff_head freeq;
459 struct sk_buff *skb, *tmp;
460 u32 hw_reconf_flags = 0;
461 int i, flushed;
462 struct ps_data *ps;
463 struct cfg80211_chan_def chandef;
464 bool cancel_scan;
465 struct cfg80211_nan_func *func;
466
467 lockdep_assert_wiphy(local->hw.wiphy);
468
469 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
470 synchronize_rcu(); /* flush _ieee80211_wake_txqs() */
471
472 cancel_scan = rcu_access_pointer(local->scan_sdata) == sdata;
473 if (cancel_scan)
474 ieee80211_scan_cancel(local);
475
476 ieee80211_roc_purge(local, sdata);
477
478 switch (sdata->vif.type) {
479 case NL80211_IFTYPE_STATION:
480 ieee80211_mgd_stop(sdata);
481 break;
482 case NL80211_IFTYPE_ADHOC:
483 ieee80211_ibss_stop(sdata);
484 break;
485 case NL80211_IFTYPE_MONITOR:
486 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
487 break;
488 list_del_rcu(&sdata->u.mntr.list);
489 break;
490 default:
491 break;
492 }
493
494 /*
495 * Remove all stations associated with this interface.
496 *
497 * This must be done before calling ops->remove_interface()
498 * because otherwise we can later invoke ops->sta_notify()
499 * whenever the STAs are removed, and that invalidates driver
500 * assumptions about always getting a vif pointer that is valid
501 * (because if we remove a STA after ops->remove_interface()
502 * the driver will have removed the vif info already!)
503 *
504 * For AP_VLANs stations may exist since there's nothing else that
505 * would have removed them, but in other modes there shouldn't
506 * be any stations.
507 */
508 flushed = sta_info_flush(sdata, -1);
509 WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_AP_VLAN && flushed > 0);
510
511 /* don't count this interface for allmulti while it is down */
512 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
513 atomic_dec(&local->iff_allmultis);
514
515 if (sdata->vif.type == NL80211_IFTYPE_AP) {
516 local->fif_pspoll--;
517 local->fif_probe_req--;
518 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
519 local->fif_probe_req--;
520 }
521
522 if (sdata->dev) {
523 netif_addr_lock_bh(sdata->dev);
524 spin_lock_bh(&local->filter_lock);
525 __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
526 sdata->dev->addr_len);
527 spin_unlock_bh(&local->filter_lock);
528 netif_addr_unlock_bh(sdata->dev);
529 }
530
531 del_timer_sync(&local->dynamic_ps_timer);
532 wiphy_work_cancel(local->hw.wiphy, &local->dynamic_ps_enable_work);
533
534 WARN(ieee80211_vif_is_mld(&sdata->vif),
535 "destroying interface with valid links 0x%04x\n",
536 sdata->vif.valid_links);
537
538 sdata->vif.bss_conf.csa_active = false;
539 if (sdata->vif.type == NL80211_IFTYPE_STATION)
540 sdata->deflink.u.mgd.csa.waiting_bcn = false;
541 ieee80211_vif_unblock_queues_csa(sdata);
542
543 wiphy_work_cancel(local->hw.wiphy, &sdata->deflink.csa.finalize_work);
544 wiphy_work_cancel(local->hw.wiphy,
545 &sdata->deflink.color_change_finalize_work);
546 wiphy_delayed_work_cancel(local->hw.wiphy,
547 &sdata->deflink.dfs_cac_timer_work);
548
549 if (sdata->wdev.links[0].cac_started) {
550 chandef = sdata->vif.bss_conf.chanreq.oper;
551 WARN_ON(local->suspended);
552 ieee80211_link_release_channel(&sdata->deflink);
553 cfg80211_cac_event(sdata->dev, &chandef,
554 NL80211_RADAR_CAC_ABORTED,
555 GFP_KERNEL, 0);
556 }
557
558 if (sdata->vif.type == NL80211_IFTYPE_AP) {
559 WARN_ON(!list_empty(&sdata->u.ap.vlans));
560 } else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
561 /* remove all packets in parent bc_buf pointing to this dev */
562 ps = &sdata->bss->ps;
563
564 spin_lock_irqsave(&ps->bc_buf.lock, flags);
565 skb_queue_walk_safe(&ps->bc_buf, skb, tmp) {
566 if (skb->dev == sdata->dev) {
567 __skb_unlink(skb, &ps->bc_buf);
568 local->total_ps_buffered--;
569 ieee80211_free_txskb(&local->hw, skb);
570 }
571 }
572 spin_unlock_irqrestore(&ps->bc_buf.lock, flags);
573 }
574
575 if (going_down)
576 local->open_count--;
577
578 switch (sdata->vif.type) {
579 case NL80211_IFTYPE_AP_VLAN:
580 list_del(&sdata->u.vlan.list);
581 RCU_INIT_POINTER(sdata->vif.bss_conf.chanctx_conf, NULL);
582 /* see comment in the default case below */
583 ieee80211_free_keys(sdata, true);
584 /* no need to tell driver */
585 break;
586 case NL80211_IFTYPE_MONITOR:
587 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) {
588 local->cooked_mntrs--;
589 break;
590 }
591
592 local->monitors--;
593 if (local->monitors == 0) {
594 local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
595 hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
596 }
597
598 ieee80211_adjust_monitor_flags(sdata, -1);
599 break;
600 case NL80211_IFTYPE_NAN:
601 /* clean all the functions */
602 spin_lock_bh(&sdata->u.nan.func_lock);
603
604 idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, i) {
605 idr_remove(&sdata->u.nan.function_inst_ids, i);
606 cfg80211_free_nan_func(func);
607 }
608 idr_destroy(&sdata->u.nan.function_inst_ids);
609
610 spin_unlock_bh(&sdata->u.nan.func_lock);
611 break;
612 case NL80211_IFTYPE_P2P_DEVICE:
613 /* relies on synchronize_rcu() below */
614 RCU_INIT_POINTER(local->p2p_sdata, NULL);
615 fallthrough;
616 default:
617 wiphy_work_cancel(sdata->local->hw.wiphy, &sdata->work);
618 /*
619 * When we get here, the interface is marked down.
620 * Free the remaining keys, if there are any
621 * (which can happen in AP mode if userspace sets
622 * keys before the interface is operating)
623 *
624 * Force the key freeing to always synchronize_net()
625 * to wait for the RX path in case it is using this
626 * interface enqueuing frames at this very time on
627 * another CPU.
628 */
629 ieee80211_free_keys(sdata, true);
630 skb_queue_purge(&sdata->skb_queue);
631 skb_queue_purge(&sdata->status_queue);
632 }
633
634 /*
635 * Since ieee80211_free_txskb() may issue __dev_queue_xmit()
636 * which should be called with interrupts enabled, reclamation
637 * is done in two phases:
638 */
639 __skb_queue_head_init(&freeq);
640
641 /* unlink from local queues... */
642 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
643 for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
644 skb_queue_walk_safe(&local->pending[i], skb, tmp) {
645 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
646 if (info->control.vif == &sdata->vif) {
647 __skb_unlink(skb, &local->pending[i]);
648 __skb_queue_tail(&freeq, skb);
649 }
650 }
651 }
652 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
653
654 /* ... and perform actual reclamation with interrupts enabled. */
655 skb_queue_walk_safe(&freeq, skb, tmp) {
656 __skb_unlink(skb, &freeq);
657 ieee80211_free_txskb(&local->hw, skb);
658 }
659
660 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
661 ieee80211_txq_remove_vlan(local, sdata);
662
663 if (sdata->vif.txq)
664 ieee80211_txq_purge(sdata->local, to_txq_info(sdata->vif.txq));
665
666 sdata->bss = NULL;
667
668 if (local->open_count == 0)
669 ieee80211_clear_tx_pending(local);
670
671 sdata->vif.bss_conf.beacon_int = 0;
672
673 /*
674 * If the interface goes down while suspended, presumably because
675 * the device was unplugged and that happens before our resume,
676 * then the driver is already unconfigured and the remainder of
677 * this function isn't needed.
678 * XXX: what about WoWLAN? If the device has software state, e.g.
679 * memory allocated, it might expect teardown commands from
680 * mac80211 here?
681 */
682 if (local->suspended) {
683 WARN_ON(local->wowlan);
684 WARN_ON(rcu_access_pointer(local->monitor_sdata));
685 return;
686 }
687
688 switch (sdata->vif.type) {
689 case NL80211_IFTYPE_AP_VLAN:
690 break;
691 case NL80211_IFTYPE_MONITOR:
692 if (local->monitors == 0)
693 ieee80211_del_virtual_monitor(local);
694
695 ieee80211_recalc_idle(local);
696 ieee80211_recalc_offload(local);
697
698 if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) &&
699 !ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR))
700 break;
701
702 ieee80211_link_release_channel(&sdata->deflink);
703 fallthrough;
704 default:
705 if (!going_down)
706 break;
707 drv_remove_interface(local, sdata);
708
709 /* Clear private driver data to prevent reuse */
710 memset(sdata->vif.drv_priv, 0, local->hw.vif_data_size);
711 }
712
713 ieee80211_recalc_ps(local);
714
715 if (cancel_scan)
716 wiphy_delayed_work_flush(local->hw.wiphy, &local->scan_work);
717
718 if (local->open_count == 0) {
719 ieee80211_stop_device(local, false);
720
721 /* no reconfiguring after stop! */
722 return;
723 }
724
725 /* do after stop to avoid reconfiguring when we stop anyway */
726 ieee80211_configure_filter(local);
727 ieee80211_hw_config(local, hw_reconf_flags);
728
729 if (local->monitors == local->open_count)
730 ieee80211_add_virtual_monitor(local);
731 }
732
ieee80211_stop_mbssid(struct ieee80211_sub_if_data * sdata)733 static void ieee80211_stop_mbssid(struct ieee80211_sub_if_data *sdata)
734 {
735 struct ieee80211_sub_if_data *tx_sdata, *non_tx_sdata, *tmp_sdata;
736 struct ieee80211_vif *tx_vif = sdata->vif.mbssid_tx_vif;
737
738 if (!tx_vif)
739 return;
740
741 tx_sdata = vif_to_sdata(tx_vif);
742 sdata->vif.mbssid_tx_vif = NULL;
743
744 list_for_each_entry_safe(non_tx_sdata, tmp_sdata,
745 &tx_sdata->local->interfaces, list) {
746 if (non_tx_sdata != sdata && non_tx_sdata != tx_sdata &&
747 non_tx_sdata->vif.mbssid_tx_vif == tx_vif &&
748 ieee80211_sdata_running(non_tx_sdata)) {
749 non_tx_sdata->vif.mbssid_tx_vif = NULL;
750 dev_close(non_tx_sdata->wdev.netdev);
751 }
752 }
753
754 if (sdata != tx_sdata && ieee80211_sdata_running(tx_sdata)) {
755 tx_sdata->vif.mbssid_tx_vif = NULL;
756 dev_close(tx_sdata->wdev.netdev);
757 }
758 }
759
ieee80211_stop(struct net_device * dev)760 static int ieee80211_stop(struct net_device *dev)
761 {
762 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
763
764 /* close dependent VLAN and MBSSID interfaces before locking wiphy */
765 if (sdata->vif.type == NL80211_IFTYPE_AP) {
766 struct ieee80211_sub_if_data *vlan, *tmpsdata;
767
768 list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
769 u.vlan.list)
770 dev_close(vlan->dev);
771
772 ieee80211_stop_mbssid(sdata);
773 }
774
775 guard(wiphy)(sdata->local->hw.wiphy);
776
777 wiphy_work_cancel(sdata->local->hw.wiphy, &sdata->activate_links_work);
778
779 ieee80211_do_stop(sdata, true);
780
781 return 0;
782 }
783
ieee80211_set_multicast_list(struct net_device * dev)784 static void ieee80211_set_multicast_list(struct net_device *dev)
785 {
786 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
787 struct ieee80211_local *local = sdata->local;
788 int allmulti, sdata_allmulti;
789
790 allmulti = !!(dev->flags & IFF_ALLMULTI);
791 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
792
793 if (allmulti != sdata_allmulti) {
794 if (dev->flags & IFF_ALLMULTI)
795 atomic_inc(&local->iff_allmultis);
796 else
797 atomic_dec(&local->iff_allmultis);
798 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
799 }
800
801 spin_lock_bh(&local->filter_lock);
802 __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
803 spin_unlock_bh(&local->filter_lock);
804 wiphy_work_queue(local->hw.wiphy, &local->reconfig_filter);
805 }
806
807 /*
808 * Called when the netdev is removed or, by the code below, before
809 * the interface type changes.
810 */
ieee80211_teardown_sdata(struct ieee80211_sub_if_data * sdata)811 static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
812 {
813 if (WARN_ON(!list_empty(&sdata->work.entry)))
814 wiphy_work_cancel(sdata->local->hw.wiphy, &sdata->work);
815
816 /* free extra data */
817 ieee80211_free_keys(sdata, false);
818
819 ieee80211_debugfs_remove_netdev(sdata);
820
821 ieee80211_destroy_frag_cache(&sdata->frags);
822
823 if (ieee80211_vif_is_mesh(&sdata->vif))
824 ieee80211_mesh_teardown_sdata(sdata);
825
826 ieee80211_vif_clear_links(sdata);
827 ieee80211_link_stop(&sdata->deflink);
828 }
829
ieee80211_uninit(struct net_device * dev)830 static void ieee80211_uninit(struct net_device *dev)
831 {
832 ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev));
833 }
834
ieee80211_netdev_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)835 static int ieee80211_netdev_setup_tc(struct net_device *dev,
836 enum tc_setup_type type, void *type_data)
837 {
838 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
839 struct ieee80211_local *local = sdata->local;
840
841 return drv_net_setup_tc(local, sdata, dev, type, type_data);
842 }
843
844 static const struct net_device_ops ieee80211_dataif_ops = {
845 .ndo_open = ieee80211_open,
846 .ndo_stop = ieee80211_stop,
847 .ndo_uninit = ieee80211_uninit,
848 .ndo_start_xmit = ieee80211_subif_start_xmit,
849 .ndo_set_rx_mode = ieee80211_set_multicast_list,
850 .ndo_set_mac_address = ieee80211_change_mac,
851 .ndo_setup_tc = ieee80211_netdev_setup_tc,
852 };
853
ieee80211_monitor_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)854 static u16 ieee80211_monitor_select_queue(struct net_device *dev,
855 struct sk_buff *skb,
856 struct net_device *sb_dev)
857 {
858 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
859 struct ieee80211_local *local = sdata->local;
860 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
861 struct ieee80211_hdr *hdr;
862 int len_rthdr;
863
864 if (local->hw.queues < IEEE80211_NUM_ACS)
865 return 0;
866
867 /* reset flags and info before parsing radiotap header */
868 memset(info, 0, sizeof(*info));
869
870 if (!ieee80211_parse_tx_radiotap(skb, dev))
871 return 0; /* doesn't matter, frame will be dropped */
872
873 len_rthdr = ieee80211_get_radiotap_len(skb->data);
874 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
875 if (skb->len < len_rthdr + 2 ||
876 skb->len < len_rthdr + ieee80211_hdrlen(hdr->frame_control))
877 return 0; /* doesn't matter, frame will be dropped */
878
879 return ieee80211_select_queue_80211(sdata, skb, hdr);
880 }
881
882 static const struct net_device_ops ieee80211_monitorif_ops = {
883 .ndo_open = ieee80211_open,
884 .ndo_stop = ieee80211_stop,
885 .ndo_uninit = ieee80211_uninit,
886 .ndo_start_xmit = ieee80211_monitor_start_xmit,
887 .ndo_set_rx_mode = ieee80211_set_multicast_list,
888 .ndo_set_mac_address = ieee80211_change_mac,
889 .ndo_select_queue = ieee80211_monitor_select_queue,
890 };
891
ieee80211_netdev_fill_forward_path(struct net_device_path_ctx * ctx,struct net_device_path * path)892 static int ieee80211_netdev_fill_forward_path(struct net_device_path_ctx *ctx,
893 struct net_device_path *path)
894 {
895 struct ieee80211_sub_if_data *sdata;
896 struct ieee80211_local *local;
897 struct sta_info *sta;
898 int ret = -ENOENT;
899
900 sdata = IEEE80211_DEV_TO_SUB_IF(ctx->dev);
901 local = sdata->local;
902
903 if (!local->ops->net_fill_forward_path)
904 return -EOPNOTSUPP;
905
906 rcu_read_lock();
907 switch (sdata->vif.type) {
908 case NL80211_IFTYPE_AP_VLAN:
909 sta = rcu_dereference(sdata->u.vlan.sta);
910 if (sta)
911 break;
912 if (sdata->wdev.use_4addr)
913 goto out;
914 if (is_multicast_ether_addr(ctx->daddr))
915 goto out;
916 sta = sta_info_get_bss(sdata, ctx->daddr);
917 break;
918 case NL80211_IFTYPE_AP:
919 if (is_multicast_ether_addr(ctx->daddr))
920 goto out;
921 sta = sta_info_get(sdata, ctx->daddr);
922 break;
923 case NL80211_IFTYPE_STATION:
924 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
925 sta = sta_info_get(sdata, ctx->daddr);
926 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
927 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH))
928 goto out;
929
930 break;
931 }
932 }
933
934 sta = sta_info_get(sdata, sdata->deflink.u.mgd.bssid);
935 break;
936 default:
937 goto out;
938 }
939
940 if (!sta)
941 goto out;
942
943 ret = drv_net_fill_forward_path(local, sdata, &sta->sta, ctx, path);
944 out:
945 rcu_read_unlock();
946
947 return ret;
948 }
949
950 static const struct net_device_ops ieee80211_dataif_8023_ops = {
951 .ndo_open = ieee80211_open,
952 .ndo_stop = ieee80211_stop,
953 .ndo_uninit = ieee80211_uninit,
954 .ndo_start_xmit = ieee80211_subif_start_xmit_8023,
955 .ndo_set_rx_mode = ieee80211_set_multicast_list,
956 .ndo_set_mac_address = ieee80211_change_mac,
957 .ndo_fill_forward_path = ieee80211_netdev_fill_forward_path,
958 .ndo_setup_tc = ieee80211_netdev_setup_tc,
959 };
960
ieee80211_iftype_supports_hdr_offload(enum nl80211_iftype iftype)961 static bool ieee80211_iftype_supports_hdr_offload(enum nl80211_iftype iftype)
962 {
963 switch (iftype) {
964 /* P2P GO and client are mapped to AP/STATION types */
965 case NL80211_IFTYPE_AP:
966 case NL80211_IFTYPE_STATION:
967 return true;
968 default:
969 return false;
970 }
971 }
972
ieee80211_set_sdata_offload_flags(struct ieee80211_sub_if_data * sdata)973 static bool ieee80211_set_sdata_offload_flags(struct ieee80211_sub_if_data *sdata)
974 {
975 struct ieee80211_local *local = sdata->local;
976 u32 flags;
977
978 flags = sdata->vif.offload_flags;
979
980 if (ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) &&
981 ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) {
982 flags |= IEEE80211_OFFLOAD_ENCAP_ENABLED;
983
984 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG) &&
985 local->hw.wiphy->frag_threshold != (u32)-1)
986 flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
987
988 if (local->monitors)
989 flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
990 } else {
991 flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
992 }
993
994 if (ieee80211_hw_check(&local->hw, SUPPORTS_RX_DECAP_OFFLOAD) &&
995 ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) {
996 flags |= IEEE80211_OFFLOAD_DECAP_ENABLED;
997
998 if (local->monitors &&
999 !ieee80211_hw_check(&local->hw, SUPPORTS_CONC_MON_RX_DECAP))
1000 flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
1001 } else {
1002 flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
1003 }
1004
1005 if (sdata->vif.offload_flags == flags)
1006 return false;
1007
1008 sdata->vif.offload_flags = flags;
1009 ieee80211_check_fast_rx_iface(sdata);
1010 return true;
1011 }
1012
ieee80211_set_vif_encap_ops(struct ieee80211_sub_if_data * sdata)1013 static void ieee80211_set_vif_encap_ops(struct ieee80211_sub_if_data *sdata)
1014 {
1015 struct ieee80211_local *local = sdata->local;
1016 struct ieee80211_sub_if_data *bss = sdata;
1017 bool enabled;
1018
1019 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1020 if (!sdata->bss)
1021 return;
1022
1023 bss = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1024 }
1025
1026 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) ||
1027 !ieee80211_iftype_supports_hdr_offload(bss->vif.type))
1028 return;
1029
1030 enabled = bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED;
1031 if (sdata->wdev.use_4addr &&
1032 !(bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_4ADDR))
1033 enabled = false;
1034
1035 sdata->dev->netdev_ops = enabled ? &ieee80211_dataif_8023_ops :
1036 &ieee80211_dataif_ops;
1037 }
1038
ieee80211_recalc_sdata_offload(struct ieee80211_sub_if_data * sdata)1039 static void ieee80211_recalc_sdata_offload(struct ieee80211_sub_if_data *sdata)
1040 {
1041 struct ieee80211_local *local = sdata->local;
1042 struct ieee80211_sub_if_data *vsdata;
1043
1044 if (ieee80211_set_sdata_offload_flags(sdata)) {
1045 drv_update_vif_offload(local, sdata);
1046 ieee80211_set_vif_encap_ops(sdata);
1047 }
1048
1049 list_for_each_entry(vsdata, &local->interfaces, list) {
1050 if (vsdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
1051 vsdata->bss != &sdata->u.ap)
1052 continue;
1053
1054 ieee80211_set_vif_encap_ops(vsdata);
1055 }
1056 }
1057
ieee80211_recalc_offload(struct ieee80211_local * local)1058 void ieee80211_recalc_offload(struct ieee80211_local *local)
1059 {
1060 struct ieee80211_sub_if_data *sdata;
1061
1062 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD))
1063 return;
1064
1065 lockdep_assert_wiphy(local->hw.wiphy);
1066
1067 list_for_each_entry(sdata, &local->interfaces, list) {
1068 if (!ieee80211_sdata_running(sdata))
1069 continue;
1070
1071 ieee80211_recalc_sdata_offload(sdata);
1072 }
1073 }
1074
ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data * sdata,const int offset)1075 void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
1076 const int offset)
1077 {
1078 struct ieee80211_local *local = sdata->local;
1079 u32 flags = sdata->u.mntr.flags;
1080
1081 #define ADJUST(_f, _s) do { \
1082 if (flags & MONITOR_FLAG_##_f) \
1083 local->fif_##_s += offset; \
1084 } while (0)
1085
1086 ADJUST(FCSFAIL, fcsfail);
1087 ADJUST(PLCPFAIL, plcpfail);
1088 ADJUST(CONTROL, control);
1089 ADJUST(CONTROL, pspoll);
1090 ADJUST(OTHER_BSS, other_bss);
1091 if (!(flags & MONITOR_FLAG_SKIP_TX))
1092 local->tx_mntrs += offset;
1093
1094 #undef ADJUST
1095 }
1096
ieee80211_set_default_queues(struct ieee80211_sub_if_data * sdata)1097 static void ieee80211_set_default_queues(struct ieee80211_sub_if_data *sdata)
1098 {
1099 struct ieee80211_local *local = sdata->local;
1100 int i;
1101
1102 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
1103 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
1104 sdata->vif.hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
1105 else if (local->hw.queues >= IEEE80211_NUM_ACS)
1106 sdata->vif.hw_queue[i] = i;
1107 else
1108 sdata->vif.hw_queue[i] = 0;
1109 }
1110 sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
1111 }
1112
ieee80211_sdata_init(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata)1113 static void ieee80211_sdata_init(struct ieee80211_local *local,
1114 struct ieee80211_sub_if_data *sdata)
1115 {
1116 sdata->local = local;
1117
1118 /*
1119 * Initialize the default link, so we can use link_id 0 for non-MLD,
1120 * and that continues to work for non-MLD-aware drivers that use just
1121 * vif.bss_conf instead of vif.link_conf.
1122 *
1123 * Note that we never change this, so if link ID 0 isn't used in an
1124 * MLD connection, we get a separate allocation for it.
1125 */
1126 ieee80211_link_init(sdata, -1, &sdata->deflink, &sdata->vif.bss_conf);
1127 }
1128
ieee80211_add_virtual_monitor(struct ieee80211_local * local)1129 int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
1130 {
1131 struct ieee80211_sub_if_data *sdata;
1132 int ret;
1133
1134 ASSERT_RTNL();
1135 lockdep_assert_wiphy(local->hw.wiphy);
1136
1137 if (local->monitor_sdata ||
1138 ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR))
1139 return 0;
1140
1141 sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL);
1142 if (!sdata)
1143 return -ENOMEM;
1144
1145 /* set up data */
1146 sdata->vif.type = NL80211_IFTYPE_MONITOR;
1147 snprintf(sdata->name, IFNAMSIZ, "%s-monitor",
1148 wiphy_name(local->hw.wiphy));
1149 sdata->wdev.iftype = NL80211_IFTYPE_MONITOR;
1150 sdata->wdev.wiphy = local->hw.wiphy;
1151
1152 ieee80211_sdata_init(local, sdata);
1153
1154 ieee80211_set_default_queues(sdata);
1155
1156 if (ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) {
1157 ret = drv_add_interface(local, sdata);
1158 if (WARN_ON(ret)) {
1159 /* ok .. stupid driver, it asked for this! */
1160 kfree(sdata);
1161 return ret;
1162 }
1163 }
1164
1165 set_bit(SDATA_STATE_RUNNING, &sdata->state);
1166
1167 ret = ieee80211_check_queues(sdata, NL80211_IFTYPE_MONITOR);
1168 if (ret) {
1169 kfree(sdata);
1170 return ret;
1171 }
1172
1173 mutex_lock(&local->iflist_mtx);
1174 rcu_assign_pointer(local->monitor_sdata, sdata);
1175 mutex_unlock(&local->iflist_mtx);
1176
1177 ret = ieee80211_link_use_channel(&sdata->deflink, &local->monitor_chanreq,
1178 IEEE80211_CHANCTX_EXCLUSIVE);
1179 if (ret) {
1180 mutex_lock(&local->iflist_mtx);
1181 RCU_INIT_POINTER(local->monitor_sdata, NULL);
1182 mutex_unlock(&local->iflist_mtx);
1183 synchronize_net();
1184 drv_remove_interface(local, sdata);
1185 kfree(sdata);
1186 return ret;
1187 }
1188
1189 skb_queue_head_init(&sdata->skb_queue);
1190 skb_queue_head_init(&sdata->status_queue);
1191 wiphy_work_init(&sdata->work, ieee80211_iface_work);
1192
1193 return 0;
1194 }
1195
ieee80211_del_virtual_monitor(struct ieee80211_local * local)1196 void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
1197 {
1198 struct ieee80211_sub_if_data *sdata;
1199
1200 if (ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR))
1201 return;
1202
1203 ASSERT_RTNL();
1204 lockdep_assert_wiphy(local->hw.wiphy);
1205
1206 mutex_lock(&local->iflist_mtx);
1207
1208 sdata = rcu_dereference_protected(local->monitor_sdata,
1209 lockdep_is_held(&local->iflist_mtx));
1210 if (!sdata) {
1211 mutex_unlock(&local->iflist_mtx);
1212 return;
1213 }
1214
1215 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
1216 ieee80211_link_release_channel(&sdata->deflink);
1217
1218 if (ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF))
1219 drv_remove_interface(local, sdata);
1220
1221 RCU_INIT_POINTER(local->monitor_sdata, NULL);
1222 mutex_unlock(&local->iflist_mtx);
1223
1224 synchronize_net();
1225
1226 kfree(sdata);
1227 }
1228
1229 /*
1230 * NOTE: Be very careful when changing this function, it must NOT return
1231 * an error on interface type changes that have been pre-checked, so most
1232 * checks should be in ieee80211_check_concurrent_iface.
1233 */
ieee80211_do_open(struct wireless_dev * wdev,bool coming_up)1234 int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
1235 {
1236 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
1237 struct net_device *dev = wdev->netdev;
1238 struct ieee80211_local *local = sdata->local;
1239 u64 changed = 0;
1240 int res;
1241 u32 hw_reconf_flags = 0;
1242
1243 lockdep_assert_wiphy(local->hw.wiphy);
1244
1245 switch (sdata->vif.type) {
1246 case NL80211_IFTYPE_AP_VLAN: {
1247 struct ieee80211_sub_if_data *master;
1248
1249 if (!sdata->bss)
1250 return -ENOLINK;
1251
1252 list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
1253
1254 master = container_of(sdata->bss,
1255 struct ieee80211_sub_if_data, u.ap);
1256 sdata->control_port_protocol =
1257 master->control_port_protocol;
1258 sdata->control_port_no_encrypt =
1259 master->control_port_no_encrypt;
1260 sdata->control_port_over_nl80211 =
1261 master->control_port_over_nl80211;
1262 sdata->control_port_no_preauth =
1263 master->control_port_no_preauth;
1264 sdata->vif.cab_queue = master->vif.cab_queue;
1265 memcpy(sdata->vif.hw_queue, master->vif.hw_queue,
1266 sizeof(sdata->vif.hw_queue));
1267 sdata->vif.bss_conf.chanreq = master->vif.bss_conf.chanreq;
1268
1269 sdata->crypto_tx_tailroom_needed_cnt +=
1270 master->crypto_tx_tailroom_needed_cnt;
1271
1272 break;
1273 }
1274 case NL80211_IFTYPE_AP:
1275 sdata->bss = &sdata->u.ap;
1276 break;
1277 case NL80211_IFTYPE_MESH_POINT:
1278 case NL80211_IFTYPE_STATION:
1279 case NL80211_IFTYPE_MONITOR:
1280 case NL80211_IFTYPE_ADHOC:
1281 case NL80211_IFTYPE_P2P_DEVICE:
1282 case NL80211_IFTYPE_OCB:
1283 case NL80211_IFTYPE_NAN:
1284 /* no special treatment */
1285 break;
1286 case NL80211_IFTYPE_UNSPECIFIED:
1287 case NUM_NL80211_IFTYPES:
1288 case NL80211_IFTYPE_P2P_CLIENT:
1289 case NL80211_IFTYPE_P2P_GO:
1290 case NL80211_IFTYPE_WDS:
1291 /* cannot happen */
1292 WARN_ON(1);
1293 break;
1294 }
1295
1296 if (local->open_count == 0) {
1297 /* here we can consider everything in good order (again) */
1298 local->reconfig_failure = false;
1299
1300 res = drv_start(local);
1301 if (res)
1302 goto err_del_bss;
1303 ieee80211_led_radio(local, true);
1304 ieee80211_mod_tpt_led_trig(local,
1305 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1306 }
1307
1308 /*
1309 * Copy the hopefully now-present MAC address to
1310 * this interface, if it has the special null one.
1311 */
1312 if (dev && is_zero_ether_addr(dev->dev_addr)) {
1313 eth_hw_addr_set(dev, local->hw.wiphy->perm_addr);
1314 memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
1315
1316 if (!is_valid_ether_addr(dev->dev_addr)) {
1317 res = -EADDRNOTAVAIL;
1318 goto err_stop;
1319 }
1320 }
1321
1322 sdata->vif.addr_valid = sdata->vif.type != NL80211_IFTYPE_MONITOR ||
1323 (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE);
1324 switch (sdata->vif.type) {
1325 case NL80211_IFTYPE_AP_VLAN:
1326 /* no need to tell driver, but set carrier and chanctx */
1327 if (sdata->bss->active) {
1328 ieee80211_link_vlan_copy_chanctx(&sdata->deflink);
1329 netif_carrier_on(dev);
1330 ieee80211_set_vif_encap_ops(sdata);
1331 } else {
1332 netif_carrier_off(dev);
1333 }
1334 break;
1335 case NL80211_IFTYPE_MONITOR:
1336 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) {
1337 local->cooked_mntrs++;
1338 break;
1339 }
1340
1341 if ((sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) ||
1342 ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) {
1343 res = drv_add_interface(local, sdata);
1344 if (res)
1345 goto err_stop;
1346 } else if (local->monitors == 0 && local->open_count == 0) {
1347 res = ieee80211_add_virtual_monitor(local);
1348 if (res)
1349 goto err_stop;
1350 }
1351
1352 /* must be before the call to ieee80211_configure_filter */
1353 local->monitors++;
1354 if (local->monitors == 1) {
1355 local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
1356 hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
1357 }
1358
1359 ieee80211_adjust_monitor_flags(sdata, 1);
1360 ieee80211_configure_filter(local);
1361 ieee80211_recalc_offload(local);
1362 ieee80211_recalc_idle(local);
1363
1364 netif_carrier_on(dev);
1365 break;
1366 default:
1367 if (coming_up) {
1368 ieee80211_del_virtual_monitor(local);
1369 ieee80211_set_sdata_offload_flags(sdata);
1370
1371 res = drv_add_interface(local, sdata);
1372 if (res)
1373 goto err_stop;
1374
1375 ieee80211_set_vif_encap_ops(sdata);
1376 res = ieee80211_check_queues(sdata,
1377 ieee80211_vif_type_p2p(&sdata->vif));
1378 if (res)
1379 goto err_del_interface;
1380 }
1381
1382 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1383 local->fif_pspoll++;
1384 local->fif_probe_req++;
1385
1386 ieee80211_configure_filter(local);
1387 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1388 local->fif_probe_req++;
1389 }
1390
1391 if (sdata->vif.probe_req_reg)
1392 drv_config_iface_filter(local, sdata,
1393 FIF_PROBE_REQ,
1394 FIF_PROBE_REQ);
1395
1396 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1397 sdata->vif.type != NL80211_IFTYPE_NAN)
1398 changed |= ieee80211_reset_erp_info(sdata);
1399 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
1400 changed);
1401
1402 switch (sdata->vif.type) {
1403 case NL80211_IFTYPE_STATION:
1404 case NL80211_IFTYPE_ADHOC:
1405 case NL80211_IFTYPE_AP:
1406 case NL80211_IFTYPE_MESH_POINT:
1407 case NL80211_IFTYPE_OCB:
1408 netif_carrier_off(dev);
1409 break;
1410 case NL80211_IFTYPE_P2P_DEVICE:
1411 case NL80211_IFTYPE_NAN:
1412 break;
1413 default:
1414 /* not reached */
1415 WARN_ON(1);
1416 }
1417
1418 /*
1419 * Set default queue parameters so drivers don't
1420 * need to initialise the hardware if the hardware
1421 * doesn't start up with sane defaults.
1422 * Enable QoS for anything but station interfaces.
1423 */
1424 ieee80211_set_wmm_default(&sdata->deflink, true,
1425 sdata->vif.type != NL80211_IFTYPE_STATION);
1426 }
1427
1428 switch (sdata->vif.type) {
1429 case NL80211_IFTYPE_P2P_DEVICE:
1430 rcu_assign_pointer(local->p2p_sdata, sdata);
1431 break;
1432 case NL80211_IFTYPE_MONITOR:
1433 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
1434 break;
1435 list_add_tail_rcu(&sdata->u.mntr.list, &local->mon_list);
1436 break;
1437 default:
1438 break;
1439 }
1440
1441 /*
1442 * set_multicast_list will be invoked by the networking core
1443 * which will check whether any increments here were done in
1444 * error and sync them down to the hardware as filter flags.
1445 */
1446 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
1447 atomic_inc(&local->iff_allmultis);
1448
1449 if (coming_up)
1450 local->open_count++;
1451
1452 if (local->open_count == 1)
1453 ieee80211_hw_conf_init(local);
1454 else if (hw_reconf_flags)
1455 ieee80211_hw_config(local, hw_reconf_flags);
1456
1457 ieee80211_recalc_ps(local);
1458
1459 set_bit(SDATA_STATE_RUNNING, &sdata->state);
1460
1461 return 0;
1462 err_del_interface:
1463 drv_remove_interface(local, sdata);
1464 err_stop:
1465 if (!local->open_count)
1466 drv_stop(local, false);
1467 err_del_bss:
1468 sdata->bss = NULL;
1469 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1470 list_del(&sdata->u.vlan.list);
1471 /* might already be clear but that doesn't matter */
1472 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
1473 return res;
1474 }
1475
ieee80211_if_setup(struct net_device * dev)1476 static void ieee80211_if_setup(struct net_device *dev)
1477 {
1478 ether_setup(dev);
1479 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1480 dev->priv_flags |= IFF_NO_QUEUE;
1481 dev->netdev_ops = &ieee80211_dataif_ops;
1482 dev->needs_free_netdev = true;
1483 }
1484
ieee80211_iface_process_skb(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1485 static void ieee80211_iface_process_skb(struct ieee80211_local *local,
1486 struct ieee80211_sub_if_data *sdata,
1487 struct sk_buff *skb)
1488 {
1489 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1490
1491 lockdep_assert_wiphy(local->hw.wiphy);
1492
1493 if (ieee80211_is_action(mgmt->frame_control) &&
1494 mgmt->u.action.category == WLAN_CATEGORY_BACK) {
1495 struct sta_info *sta;
1496 int len = skb->len;
1497
1498 sta = sta_info_get_bss(sdata, mgmt->sa);
1499 if (sta) {
1500 switch (mgmt->u.action.u.addba_req.action_code) {
1501 case WLAN_ACTION_ADDBA_REQ:
1502 ieee80211_process_addba_request(local, sta,
1503 mgmt, len);
1504 break;
1505 case WLAN_ACTION_ADDBA_RESP:
1506 ieee80211_process_addba_resp(local, sta,
1507 mgmt, len);
1508 break;
1509 case WLAN_ACTION_DELBA:
1510 ieee80211_process_delba(sdata, sta,
1511 mgmt, len);
1512 break;
1513 default:
1514 WARN_ON(1);
1515 break;
1516 }
1517 }
1518 } else if (ieee80211_is_action(mgmt->frame_control) &&
1519 mgmt->u.action.category == WLAN_CATEGORY_VHT) {
1520 switch (mgmt->u.action.u.vht_group_notif.action_code) {
1521 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
1522 struct ieee80211_rx_status *status;
1523 enum nl80211_band band;
1524 struct sta_info *sta;
1525 u8 opmode;
1526
1527 status = IEEE80211_SKB_RXCB(skb);
1528 band = status->band;
1529 opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
1530
1531 sta = sta_info_get_bss(sdata, mgmt->sa);
1532
1533 if (sta)
1534 ieee80211_vht_handle_opmode(sdata,
1535 &sta->deflink,
1536 opmode, band);
1537
1538 break;
1539 }
1540 case WLAN_VHT_ACTION_GROUPID_MGMT:
1541 ieee80211_process_mu_groups(sdata, &sdata->deflink,
1542 mgmt);
1543 break;
1544 default:
1545 WARN_ON(1);
1546 break;
1547 }
1548 } else if (ieee80211_is_action(mgmt->frame_control) &&
1549 mgmt->u.action.category == WLAN_CATEGORY_S1G) {
1550 switch (mgmt->u.action.u.s1g.action_code) {
1551 case WLAN_S1G_TWT_TEARDOWN:
1552 case WLAN_S1G_TWT_SETUP:
1553 ieee80211_s1g_rx_twt_action(sdata, skb);
1554 break;
1555 default:
1556 break;
1557 }
1558 } else if (ieee80211_is_action(mgmt->frame_control) &&
1559 mgmt->u.action.category == WLAN_CATEGORY_PROTECTED_EHT) {
1560 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1561 switch (mgmt->u.action.u.ttlm_req.action_code) {
1562 case WLAN_PROTECTED_EHT_ACTION_TTLM_REQ:
1563 ieee80211_process_neg_ttlm_req(sdata, mgmt,
1564 skb->len);
1565 break;
1566 case WLAN_PROTECTED_EHT_ACTION_TTLM_RES:
1567 ieee80211_process_neg_ttlm_res(sdata, mgmt,
1568 skb->len);
1569 break;
1570 case WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_RESP:
1571 ieee80211_process_ml_reconf_resp(sdata, mgmt,
1572 skb->len);
1573 break;
1574 default:
1575 break;
1576 }
1577 }
1578 } else if (ieee80211_is_ext(mgmt->frame_control)) {
1579 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1580 ieee80211_sta_rx_queued_ext(sdata, skb);
1581 else
1582 WARN_ON(1);
1583 } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
1584 struct ieee80211_hdr *hdr = (void *)mgmt;
1585 struct sta_info *sta;
1586
1587 /*
1588 * So the frame isn't mgmt, but frame_control
1589 * is at the right place anyway, of course, so
1590 * the if statement is correct.
1591 *
1592 * Warn if we have other data frame types here,
1593 * they must not get here.
1594 */
1595 WARN_ON(hdr->frame_control &
1596 cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
1597 WARN_ON(!(hdr->seq_ctrl &
1598 cpu_to_le16(IEEE80211_SCTL_FRAG)));
1599 /*
1600 * This was a fragment of a frame, received while
1601 * a block-ack session was active. That cannot be
1602 * right, so terminate the session.
1603 */
1604 sta = sta_info_get_bss(sdata, mgmt->sa);
1605 if (sta) {
1606 u16 tid = ieee80211_get_tid(hdr);
1607
1608 __ieee80211_stop_rx_ba_session(
1609 sta, tid, WLAN_BACK_RECIPIENT,
1610 WLAN_REASON_QSTA_REQUIRE_SETUP,
1611 true);
1612 }
1613 } else switch (sdata->vif.type) {
1614 case NL80211_IFTYPE_STATION:
1615 ieee80211_sta_rx_queued_mgmt(sdata, skb);
1616 break;
1617 case NL80211_IFTYPE_ADHOC:
1618 ieee80211_ibss_rx_queued_mgmt(sdata, skb);
1619 break;
1620 case NL80211_IFTYPE_MESH_POINT:
1621 if (!ieee80211_vif_is_mesh(&sdata->vif))
1622 break;
1623 ieee80211_mesh_rx_queued_mgmt(sdata, skb);
1624 break;
1625 default:
1626 WARN(1, "frame for unexpected interface type");
1627 break;
1628 }
1629 }
1630
ieee80211_iface_process_status(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1631 static void ieee80211_iface_process_status(struct ieee80211_sub_if_data *sdata,
1632 struct sk_buff *skb)
1633 {
1634 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1635
1636 if (ieee80211_is_action(mgmt->frame_control) &&
1637 mgmt->u.action.category == WLAN_CATEGORY_S1G) {
1638 switch (mgmt->u.action.u.s1g.action_code) {
1639 case WLAN_S1G_TWT_TEARDOWN:
1640 case WLAN_S1G_TWT_SETUP:
1641 ieee80211_s1g_status_twt_action(sdata, skb);
1642 break;
1643 default:
1644 break;
1645 }
1646 }
1647 }
1648
ieee80211_iface_work(struct wiphy * wiphy,struct wiphy_work * work)1649 static void ieee80211_iface_work(struct wiphy *wiphy, struct wiphy_work *work)
1650 {
1651 struct ieee80211_sub_if_data *sdata =
1652 container_of(work, struct ieee80211_sub_if_data, work);
1653 struct ieee80211_local *local = sdata->local;
1654 struct sk_buff *skb;
1655
1656 if (!ieee80211_sdata_running(sdata))
1657 return;
1658
1659 if (test_bit(SCAN_SW_SCANNING, &local->scanning))
1660 return;
1661
1662 if (!ieee80211_can_run_worker(local))
1663 return;
1664
1665 /* first process frames */
1666 while ((skb = skb_dequeue(&sdata->skb_queue))) {
1667 kcov_remote_start_common(skb_get_kcov_handle(skb));
1668
1669 if (skb->protocol == cpu_to_be16(ETH_P_TDLS))
1670 ieee80211_process_tdls_channel_switch(sdata, skb);
1671 else
1672 ieee80211_iface_process_skb(local, sdata, skb);
1673
1674 kfree_skb(skb);
1675 kcov_remote_stop();
1676 }
1677
1678 /* process status queue */
1679 while ((skb = skb_dequeue(&sdata->status_queue))) {
1680 kcov_remote_start_common(skb_get_kcov_handle(skb));
1681
1682 ieee80211_iface_process_status(sdata, skb);
1683 kfree_skb(skb);
1684
1685 kcov_remote_stop();
1686 }
1687
1688 /* then other type-dependent work */
1689 switch (sdata->vif.type) {
1690 case NL80211_IFTYPE_STATION:
1691 ieee80211_sta_work(sdata);
1692 break;
1693 case NL80211_IFTYPE_ADHOC:
1694 ieee80211_ibss_work(sdata);
1695 break;
1696 case NL80211_IFTYPE_MESH_POINT:
1697 if (!ieee80211_vif_is_mesh(&sdata->vif))
1698 break;
1699 ieee80211_mesh_work(sdata);
1700 break;
1701 case NL80211_IFTYPE_OCB:
1702 ieee80211_ocb_work(sdata);
1703 break;
1704 default:
1705 break;
1706 }
1707 }
1708
ieee80211_activate_links_work(struct wiphy * wiphy,struct wiphy_work * work)1709 static void ieee80211_activate_links_work(struct wiphy *wiphy,
1710 struct wiphy_work *work)
1711 {
1712 struct ieee80211_sub_if_data *sdata =
1713 container_of(work, struct ieee80211_sub_if_data,
1714 activate_links_work);
1715 struct ieee80211_local *local = wiphy_priv(wiphy);
1716
1717 if (local->in_reconfig)
1718 return;
1719
1720 ieee80211_set_active_links(&sdata->vif, sdata->desired_active_links);
1721 sdata->desired_active_links = 0;
1722 }
1723
1724 /*
1725 * Helper function to initialise an interface to a specific type.
1726 */
ieee80211_setup_sdata(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype type)1727 static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
1728 enum nl80211_iftype type)
1729 {
1730 static const u8 bssid_wildcard[ETH_ALEN] = {0xff, 0xff, 0xff,
1731 0xff, 0xff, 0xff};
1732
1733 /* clear type-dependent unions */
1734 memset(&sdata->u, 0, sizeof(sdata->u));
1735 memset(&sdata->deflink.u, 0, sizeof(sdata->deflink.u));
1736
1737 /* and set some type-dependent values */
1738 sdata->vif.type = type;
1739 sdata->vif.p2p = false;
1740 sdata->wdev.iftype = type;
1741
1742 sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
1743 sdata->control_port_no_encrypt = false;
1744 sdata->control_port_over_nl80211 = false;
1745 sdata->control_port_no_preauth = false;
1746 sdata->vif.cfg.idle = true;
1747 sdata->vif.bss_conf.txpower = INT_MIN; /* unset */
1748
1749 sdata->noack_map = 0;
1750
1751 /* only monitor/p2p-device differ */
1752 if (sdata->dev) {
1753 sdata->dev->netdev_ops = &ieee80211_dataif_ops;
1754 sdata->dev->type = ARPHRD_ETHER;
1755 }
1756
1757 skb_queue_head_init(&sdata->skb_queue);
1758 skb_queue_head_init(&sdata->status_queue);
1759 wiphy_work_init(&sdata->work, ieee80211_iface_work);
1760 wiphy_work_init(&sdata->activate_links_work,
1761 ieee80211_activate_links_work);
1762
1763 switch (type) {
1764 case NL80211_IFTYPE_P2P_GO:
1765 type = NL80211_IFTYPE_AP;
1766 sdata->vif.type = type;
1767 sdata->vif.p2p = true;
1768 fallthrough;
1769 case NL80211_IFTYPE_AP:
1770 skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
1771 INIT_LIST_HEAD(&sdata->u.ap.vlans);
1772 sdata->vif.bss_conf.bssid = sdata->vif.addr;
1773 break;
1774 case NL80211_IFTYPE_P2P_CLIENT:
1775 type = NL80211_IFTYPE_STATION;
1776 sdata->vif.type = type;
1777 sdata->vif.p2p = true;
1778 fallthrough;
1779 case NL80211_IFTYPE_STATION:
1780 sdata->vif.bss_conf.bssid = sdata->deflink.u.mgd.bssid;
1781 ieee80211_sta_setup_sdata(sdata);
1782 break;
1783 case NL80211_IFTYPE_OCB:
1784 sdata->vif.bss_conf.bssid = bssid_wildcard;
1785 ieee80211_ocb_setup_sdata(sdata);
1786 break;
1787 case NL80211_IFTYPE_ADHOC:
1788 sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
1789 ieee80211_ibss_setup_sdata(sdata);
1790 break;
1791 case NL80211_IFTYPE_MESH_POINT:
1792 if (ieee80211_vif_is_mesh(&sdata->vif))
1793 ieee80211_mesh_init_sdata(sdata);
1794 break;
1795 case NL80211_IFTYPE_MONITOR:
1796 sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
1797 sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
1798 sdata->u.mntr.flags = MONITOR_FLAG_CONTROL |
1799 MONITOR_FLAG_OTHER_BSS;
1800 break;
1801 case NL80211_IFTYPE_NAN:
1802 idr_init(&sdata->u.nan.function_inst_ids);
1803 spin_lock_init(&sdata->u.nan.func_lock);
1804 sdata->vif.bss_conf.bssid = sdata->vif.addr;
1805 break;
1806 case NL80211_IFTYPE_AP_VLAN:
1807 case NL80211_IFTYPE_P2P_DEVICE:
1808 sdata->vif.bss_conf.bssid = sdata->vif.addr;
1809 break;
1810 case NL80211_IFTYPE_UNSPECIFIED:
1811 case NL80211_IFTYPE_WDS:
1812 case NUM_NL80211_IFTYPES:
1813 WARN_ON(1);
1814 break;
1815 }
1816
1817 /* need to do this after the switch so vif.type is correct */
1818 ieee80211_link_setup(&sdata->deflink);
1819
1820 ieee80211_debugfs_recreate_netdev(sdata, false);
1821 }
1822
ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype type)1823 static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
1824 enum nl80211_iftype type)
1825 {
1826 struct ieee80211_local *local = sdata->local;
1827 int ret, err;
1828 enum nl80211_iftype internal_type = type;
1829 bool p2p = false;
1830
1831 ASSERT_RTNL();
1832
1833 if (!local->ops->change_interface)
1834 return -EBUSY;
1835
1836 /* for now, don't support changing while links exist */
1837 if (ieee80211_vif_is_mld(&sdata->vif))
1838 return -EBUSY;
1839
1840 switch (sdata->vif.type) {
1841 case NL80211_IFTYPE_AP:
1842 if (!list_empty(&sdata->u.ap.vlans))
1843 return -EBUSY;
1844 break;
1845 case NL80211_IFTYPE_STATION:
1846 case NL80211_IFTYPE_ADHOC:
1847 case NL80211_IFTYPE_OCB:
1848 /*
1849 * Could maybe also all others here?
1850 * Just not sure how that interacts
1851 * with the RX/config path e.g. for
1852 * mesh.
1853 */
1854 break;
1855 default:
1856 return -EBUSY;
1857 }
1858
1859 switch (type) {
1860 case NL80211_IFTYPE_AP:
1861 case NL80211_IFTYPE_STATION:
1862 case NL80211_IFTYPE_ADHOC:
1863 case NL80211_IFTYPE_OCB:
1864 /*
1865 * Could probably support everything
1866 * but here.
1867 */
1868 break;
1869 case NL80211_IFTYPE_P2P_CLIENT:
1870 p2p = true;
1871 internal_type = NL80211_IFTYPE_STATION;
1872 break;
1873 case NL80211_IFTYPE_P2P_GO:
1874 p2p = true;
1875 internal_type = NL80211_IFTYPE_AP;
1876 break;
1877 default:
1878 return -EBUSY;
1879 }
1880
1881 ret = ieee80211_check_concurrent_iface(sdata, internal_type);
1882 if (ret)
1883 return ret;
1884
1885 ieee80211_stop_vif_queues(local, sdata,
1886 IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
1887 /* do_stop will synchronize_rcu() first thing */
1888 ieee80211_do_stop(sdata, false);
1889
1890 ieee80211_teardown_sdata(sdata);
1891
1892 ieee80211_set_sdata_offload_flags(sdata);
1893 ret = drv_change_interface(local, sdata, internal_type, p2p);
1894 if (ret)
1895 type = ieee80211_vif_type_p2p(&sdata->vif);
1896
1897 /*
1898 * Ignore return value here, there's not much we can do since
1899 * the driver changed the interface type internally already.
1900 * The warnings will hopefully make driver authors fix it :-)
1901 */
1902 ieee80211_check_queues(sdata, type);
1903
1904 ieee80211_setup_sdata(sdata, type);
1905 ieee80211_set_vif_encap_ops(sdata);
1906
1907 err = ieee80211_do_open(&sdata->wdev, false);
1908 WARN(err, "type change: do_open returned %d", err);
1909
1910 ieee80211_wake_vif_queues(local, sdata,
1911 IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
1912 return ret;
1913 }
1914
ieee80211_if_change_type(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype type)1915 int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
1916 enum nl80211_iftype type)
1917 {
1918 int ret;
1919
1920 ASSERT_RTNL();
1921
1922 if (type == ieee80211_vif_type_p2p(&sdata->vif))
1923 return 0;
1924
1925 if (ieee80211_sdata_running(sdata)) {
1926 ret = ieee80211_runtime_change_iftype(sdata, type);
1927 if (ret)
1928 return ret;
1929 } else {
1930 /* Purge and reset type-dependent state. */
1931 ieee80211_teardown_sdata(sdata);
1932 ieee80211_setup_sdata(sdata, type);
1933 }
1934
1935 /* reset some values that shouldn't be kept across type changes */
1936 if (type == NL80211_IFTYPE_STATION)
1937 sdata->u.mgd.use_4addr = false;
1938
1939 return 0;
1940 }
1941
ieee80211_assign_perm_addr(struct ieee80211_local * local,u8 * perm_addr,enum nl80211_iftype type)1942 static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
1943 u8 *perm_addr, enum nl80211_iftype type)
1944 {
1945 struct ieee80211_sub_if_data *sdata;
1946 u64 mask, start, addr, val, inc;
1947 u8 *m;
1948 u8 tmp_addr[ETH_ALEN];
1949 int i;
1950
1951 lockdep_assert_wiphy(local->hw.wiphy);
1952
1953 /* default ... something at least */
1954 memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1955
1956 if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
1957 local->hw.wiphy->n_addresses <= 1)
1958 return;
1959
1960 switch (type) {
1961 case NL80211_IFTYPE_MONITOR:
1962 /* doesn't matter */
1963 break;
1964 case NL80211_IFTYPE_AP_VLAN:
1965 /* match up with an AP interface */
1966 list_for_each_entry(sdata, &local->interfaces, list) {
1967 if (sdata->vif.type != NL80211_IFTYPE_AP)
1968 continue;
1969 memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
1970 break;
1971 }
1972 /* keep default if no AP interface present */
1973 break;
1974 case NL80211_IFTYPE_P2P_CLIENT:
1975 case NL80211_IFTYPE_P2P_GO:
1976 if (ieee80211_hw_check(&local->hw, P2P_DEV_ADDR_FOR_INTF)) {
1977 list_for_each_entry(sdata, &local->interfaces, list) {
1978 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
1979 continue;
1980 if (!ieee80211_sdata_running(sdata))
1981 continue;
1982 memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
1983 return;
1984 }
1985 }
1986 fallthrough;
1987 default:
1988 /* assign a new address if possible -- try n_addresses first */
1989 for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
1990 bool used = false;
1991
1992 list_for_each_entry(sdata, &local->interfaces, list) {
1993 if (ether_addr_equal(local->hw.wiphy->addresses[i].addr,
1994 sdata->vif.addr)) {
1995 used = true;
1996 break;
1997 }
1998 }
1999
2000 if (!used) {
2001 memcpy(perm_addr,
2002 local->hw.wiphy->addresses[i].addr,
2003 ETH_ALEN);
2004 break;
2005 }
2006 }
2007
2008 /* try mask if available */
2009 if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
2010 break;
2011
2012 m = local->hw.wiphy->addr_mask;
2013 mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
2014 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
2015 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
2016
2017 if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
2018 /* not a contiguous mask ... not handled now! */
2019 pr_info("not contiguous\n");
2020 break;
2021 }
2022
2023 /*
2024 * Pick address of existing interface in case user changed
2025 * MAC address manually, default to perm_addr.
2026 */
2027 m = local->hw.wiphy->perm_addr;
2028 list_for_each_entry(sdata, &local->interfaces, list) {
2029 if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
2030 continue;
2031 m = sdata->vif.addr;
2032 break;
2033 }
2034 start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
2035 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
2036 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
2037
2038 inc = 1ULL<<__ffs64(mask);
2039 val = (start & mask);
2040 addr = (start & ~mask) | (val & mask);
2041 do {
2042 bool used = false;
2043
2044 tmp_addr[5] = addr >> 0*8;
2045 tmp_addr[4] = addr >> 1*8;
2046 tmp_addr[3] = addr >> 2*8;
2047 tmp_addr[2] = addr >> 3*8;
2048 tmp_addr[1] = addr >> 4*8;
2049 tmp_addr[0] = addr >> 5*8;
2050
2051 val += inc;
2052
2053 list_for_each_entry(sdata, &local->interfaces, list) {
2054 if (ether_addr_equal(tmp_addr, sdata->vif.addr)) {
2055 used = true;
2056 break;
2057 }
2058 }
2059
2060 if (!used) {
2061 memcpy(perm_addr, tmp_addr, ETH_ALEN);
2062 break;
2063 }
2064 addr = (start & ~mask) | (val & mask);
2065 } while (addr != start);
2066
2067 break;
2068 }
2069 }
2070
ieee80211_if_add(struct ieee80211_local * local,const char * name,unsigned char name_assign_type,struct wireless_dev ** new_wdev,enum nl80211_iftype type,struct vif_params * params)2071 int ieee80211_if_add(struct ieee80211_local *local, const char *name,
2072 unsigned char name_assign_type,
2073 struct wireless_dev **new_wdev, enum nl80211_iftype type,
2074 struct vif_params *params)
2075 {
2076 struct net_device *ndev = NULL;
2077 struct ieee80211_sub_if_data *sdata = NULL;
2078 struct txq_info *txqi;
2079 int ret, i;
2080
2081 ASSERT_RTNL();
2082 lockdep_assert_wiphy(local->hw.wiphy);
2083
2084 if (type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN) {
2085 struct wireless_dev *wdev;
2086
2087 sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
2088 GFP_KERNEL);
2089 if (!sdata)
2090 return -ENOMEM;
2091 wdev = &sdata->wdev;
2092
2093 sdata->dev = NULL;
2094 strscpy(sdata->name, name, IFNAMSIZ);
2095 ieee80211_assign_perm_addr(local, wdev->address, type);
2096 memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
2097 ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
2098 } else {
2099 int size = ALIGN(sizeof(*sdata) + local->hw.vif_data_size,
2100 sizeof(void *));
2101 int txq_size = 0;
2102
2103 if (type != NL80211_IFTYPE_AP_VLAN &&
2104 (type != NL80211_IFTYPE_MONITOR ||
2105 (params->flags & MONITOR_FLAG_ACTIVE)))
2106 txq_size += sizeof(struct txq_info) +
2107 local->hw.txq_data_size;
2108
2109 ndev = alloc_netdev_mqs(size + txq_size,
2110 name, name_assign_type,
2111 ieee80211_if_setup, 1, 1);
2112 if (!ndev)
2113 return -ENOMEM;
2114
2115 dev_net_set(ndev, wiphy_net(local->hw.wiphy));
2116
2117 ndev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
2118
2119 ndev->needed_headroom = local->tx_headroom +
2120 4*6 /* four MAC addresses */
2121 + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
2122 + 6 /* mesh */
2123 + 8 /* rfc1042/bridge tunnel */
2124 - ETH_HLEN /* ethernet hard_header_len */
2125 + IEEE80211_ENCRYPT_HEADROOM;
2126 ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
2127
2128 ret = dev_alloc_name(ndev, ndev->name);
2129 if (ret < 0) {
2130 free_netdev(ndev);
2131 return ret;
2132 }
2133
2134 ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
2135 if (is_valid_ether_addr(params->macaddr))
2136 eth_hw_addr_set(ndev, params->macaddr);
2137 else
2138 eth_hw_addr_set(ndev, ndev->perm_addr);
2139 SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
2140
2141 /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
2142 sdata = netdev_priv(ndev);
2143 ndev->ieee80211_ptr = &sdata->wdev;
2144 memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
2145 ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
2146 memcpy(sdata->name, ndev->name, IFNAMSIZ);
2147
2148 if (txq_size) {
2149 txqi = netdev_priv(ndev) + size;
2150 ieee80211_txq_init(sdata, NULL, txqi, 0);
2151 }
2152
2153 sdata->dev = ndev;
2154 }
2155
2156 /* initialise type-independent data */
2157 sdata->wdev.wiphy = local->hw.wiphy;
2158
2159 ieee80211_sdata_init(local, sdata);
2160
2161 ieee80211_init_frag_cache(&sdata->frags);
2162
2163 INIT_LIST_HEAD(&sdata->key_list);
2164
2165 wiphy_delayed_work_init(&sdata->dec_tailroom_needed_wk,
2166 ieee80211_delayed_tailroom_dec);
2167
2168 for (i = 0; i < NUM_NL80211_BANDS; i++) {
2169 struct ieee80211_supported_band *sband;
2170 sband = local->hw.wiphy->bands[i];
2171 sdata->rc_rateidx_mask[i] =
2172 sband ? (1 << sband->n_bitrates) - 1 : 0;
2173 if (sband) {
2174 __le16 cap;
2175 u16 *vht_rate_mask;
2176
2177 memcpy(sdata->rc_rateidx_mcs_mask[i],
2178 sband->ht_cap.mcs.rx_mask,
2179 sizeof(sdata->rc_rateidx_mcs_mask[i]));
2180
2181 cap = sband->vht_cap.vht_mcs.rx_mcs_map;
2182 vht_rate_mask = sdata->rc_rateidx_vht_mcs_mask[i];
2183 ieee80211_get_vht_mask_from_cap(cap, vht_rate_mask);
2184 } else {
2185 memset(sdata->rc_rateidx_mcs_mask[i], 0,
2186 sizeof(sdata->rc_rateidx_mcs_mask[i]));
2187 memset(sdata->rc_rateidx_vht_mcs_mask[i], 0,
2188 sizeof(sdata->rc_rateidx_vht_mcs_mask[i]));
2189 }
2190 }
2191
2192 ieee80211_set_default_queues(sdata);
2193
2194 /* setup type-dependent data */
2195 ieee80211_setup_sdata(sdata, type);
2196
2197 if (ndev) {
2198 ndev->ieee80211_ptr->use_4addr = params->use_4addr;
2199 if (type == NL80211_IFTYPE_STATION)
2200 sdata->u.mgd.use_4addr = params->use_4addr;
2201
2202 ndev->features |= local->hw.netdev_features;
2203 ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2204 ndev->hw_features |= ndev->features &
2205 MAC80211_SUPPORTED_FEATURES_TX;
2206 sdata->vif.netdev_features = local->hw.netdev_features;
2207
2208 netdev_set_default_ethtool_ops(ndev, &ieee80211_ethtool_ops);
2209
2210 /* MTU range is normally 256 - 2304, where the upper limit is
2211 * the maximum MSDU size. Monitor interfaces send and receive
2212 * MPDU and A-MSDU frames which may be much larger so we do
2213 * not impose an upper limit in that case.
2214 */
2215 ndev->min_mtu = 256;
2216 if (type == NL80211_IFTYPE_MONITOR)
2217 ndev->max_mtu = 0;
2218 else
2219 ndev->max_mtu = local->hw.max_mtu;
2220
2221 ret = cfg80211_register_netdevice(ndev);
2222 if (ret) {
2223 free_netdev(ndev);
2224 return ret;
2225 }
2226 }
2227
2228 mutex_lock(&local->iflist_mtx);
2229 list_add_tail_rcu(&sdata->list, &local->interfaces);
2230 mutex_unlock(&local->iflist_mtx);
2231
2232 if (new_wdev)
2233 *new_wdev = &sdata->wdev;
2234
2235 return 0;
2236 }
2237
ieee80211_if_remove(struct ieee80211_sub_if_data * sdata)2238 void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
2239 {
2240 ASSERT_RTNL();
2241 lockdep_assert_wiphy(sdata->local->hw.wiphy);
2242
2243 mutex_lock(&sdata->local->iflist_mtx);
2244 list_del_rcu(&sdata->list);
2245 mutex_unlock(&sdata->local->iflist_mtx);
2246
2247 if (sdata->vif.txq)
2248 ieee80211_txq_purge(sdata->local, to_txq_info(sdata->vif.txq));
2249
2250 synchronize_rcu();
2251
2252 cfg80211_unregister_wdev(&sdata->wdev);
2253
2254 if (!sdata->dev) {
2255 ieee80211_teardown_sdata(sdata);
2256 kfree(sdata);
2257 }
2258 }
2259
ieee80211_sdata_stop(struct ieee80211_sub_if_data * sdata)2260 void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
2261 {
2262 if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state)))
2263 return;
2264 ieee80211_do_stop(sdata, true);
2265 }
2266
ieee80211_remove_interfaces(struct ieee80211_local * local)2267 void ieee80211_remove_interfaces(struct ieee80211_local *local)
2268 {
2269 struct ieee80211_sub_if_data *sdata, *tmp;
2270 LIST_HEAD(unreg_list);
2271
2272 ASSERT_RTNL();
2273
2274 /* Before destroying the interfaces, make sure they're all stopped so
2275 * that the hardware is stopped. Otherwise, the driver might still be
2276 * iterating the interfaces during the shutdown, e.g. from a worker
2277 * or from RX processing or similar, and if it does so (using atomic
2278 * iteration) while we're manipulating the list, the iteration will
2279 * crash.
2280 *
2281 * After this, the hardware should be stopped and the driver should
2282 * have stopped all of its activities, so that we can do RCU-unaware
2283 * manipulations of the interface list below.
2284 */
2285 cfg80211_shutdown_all_interfaces(local->hw.wiphy);
2286
2287 guard(wiphy)(local->hw.wiphy);
2288
2289 WARN(local->open_count, "%s: open count remains %d\n",
2290 wiphy_name(local->hw.wiphy), local->open_count);
2291
2292 mutex_lock(&local->iflist_mtx);
2293 list_splice_init(&local->interfaces, &unreg_list);
2294 mutex_unlock(&local->iflist_mtx);
2295
2296 list_for_each_entry_safe(sdata, tmp, &unreg_list, list) {
2297 bool netdev = sdata->dev;
2298
2299 /*
2300 * Remove IP addresses explicitly, since the notifier will
2301 * skip the callbacks if wdev->registered is false, since
2302 * we can't acquire the wiphy_lock() again there if already
2303 * inside this locked section.
2304 */
2305 sdata->vif.cfg.arp_addr_cnt = 0;
2306 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2307 sdata->u.mgd.associated)
2308 ieee80211_vif_cfg_change_notify(sdata,
2309 BSS_CHANGED_ARP_FILTER);
2310
2311 list_del(&sdata->list);
2312 cfg80211_unregister_wdev(&sdata->wdev);
2313
2314 if (!netdev)
2315 kfree(sdata);
2316 }
2317 }
2318
netdev_notify(struct notifier_block * nb,unsigned long state,void * ptr)2319 static int netdev_notify(struct notifier_block *nb,
2320 unsigned long state, void *ptr)
2321 {
2322 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2323 struct ieee80211_sub_if_data *sdata;
2324
2325 if (state != NETDEV_CHANGENAME)
2326 return NOTIFY_DONE;
2327
2328 if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
2329 return NOTIFY_DONE;
2330
2331 if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
2332 return NOTIFY_DONE;
2333
2334 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2335 memcpy(sdata->name, dev->name, IFNAMSIZ);
2336 ieee80211_debugfs_rename_netdev(sdata);
2337
2338 return NOTIFY_OK;
2339 }
2340
2341 static struct notifier_block mac80211_netdev_notifier = {
2342 .notifier_call = netdev_notify,
2343 };
2344
ieee80211_iface_init(void)2345 int ieee80211_iface_init(void)
2346 {
2347 return register_netdevice_notifier(&mac80211_netdev_notifier);
2348 }
2349
ieee80211_iface_exit(void)2350 void ieee80211_iface_exit(void)
2351 {
2352 unregister_netdevice_notifier(&mac80211_netdev_notifier);
2353 }
2354
ieee80211_vif_inc_num_mcast(struct ieee80211_sub_if_data * sdata)2355 void ieee80211_vif_inc_num_mcast(struct ieee80211_sub_if_data *sdata)
2356 {
2357 if (sdata->vif.type == NL80211_IFTYPE_AP)
2358 atomic_inc(&sdata->u.ap.num_mcast_sta);
2359 else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2360 atomic_inc(&sdata->u.vlan.num_mcast_sta);
2361 }
2362
ieee80211_vif_dec_num_mcast(struct ieee80211_sub_if_data * sdata)2363 void ieee80211_vif_dec_num_mcast(struct ieee80211_sub_if_data *sdata)
2364 {
2365 if (sdata->vif.type == NL80211_IFTYPE_AP)
2366 atomic_dec(&sdata->u.ap.num_mcast_sta);
2367 else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2368 atomic_dec(&sdata->u.vlan.num_mcast_sta);
2369 }
2370
ieee80211_vif_block_queues_csa(struct ieee80211_sub_if_data * sdata)2371 void ieee80211_vif_block_queues_csa(struct ieee80211_sub_if_data *sdata)
2372 {
2373 struct ieee80211_local *local = sdata->local;
2374
2375 if (ieee80211_hw_check(&local->hw, HANDLES_QUIET_CSA))
2376 return;
2377
2378 ieee80211_stop_vif_queues_norefcount(local, sdata,
2379 IEEE80211_QUEUE_STOP_REASON_CSA);
2380 }
2381
ieee80211_vif_unblock_queues_csa(struct ieee80211_sub_if_data * sdata)2382 void ieee80211_vif_unblock_queues_csa(struct ieee80211_sub_if_data *sdata)
2383 {
2384 struct ieee80211_local *local = sdata->local;
2385
2386 ieee80211_wake_vif_queues_norefcount(local, sdata,
2387 IEEE80211_QUEUE_STOP_REASON_CSA);
2388 }
2389