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