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