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