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

source code of linux/net/mac80211/iface.c