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