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