• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright (C) 2017     Intel Deutschland GmbH
8  * Copyright (C) 2018 - 2019 Intel Corporation
9  */
10 
11 #include <net/mac80211.h>
12 #include <linux/module.h>
13 #include <linux/fips.h>
14 #include <linux/init.h>
15 #include <linux/netdevice.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_arp.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/bitmap.h>
23 #include <linux/inetdevice.h>
24 #include <net/net_namespace.h>
25 #include <net/cfg80211.h>
26 #include <net/addrconf.h>
27 
28 #ifdef IPV6_FILTERING
29 #include <net/if_inet6.h>
30 #endif /*IPV6_FILTERING*/
31 
32 #include "ieee80211_i.h"
33 #include "driver-ops.h"
34 #include "rate.h"
35 #include "mesh.h"
36 #include "wep.h"
37 #include "led.h"
38 #include "debugfs.h"
39 
40 #ifdef CONFIG_DRIVERS_HDF_XR829
41 struct wiphy *g_wiphy = NULL;
42 
hdf_wiphy_register(struct wiphy * wiphy)43 void hdf_wiphy_register(struct wiphy *wiphy)
44 {
45     g_wiphy = wiphy;
46 }
47 
hdf_wiphy_unregister(void)48 void hdf_wiphy_unregister(void)
49 {
50 	g_wiphy = NULL;
51 }
52 #endif
53 
ieee80211_configure_filter(struct ieee80211_sub_if_data * sdata)54 void ieee80211_configure_filter(struct ieee80211_sub_if_data *sdata)
55 {
56 	u64 mc;
57 	unsigned int changed_flags;
58 	unsigned int new_flags = 0;
59 	struct ieee80211_local *local = sdata->local;
60 
61 
62 	if (atomic_read(&local->iff_allmultis))
63 		new_flags |= FIF_ALLMULTI;
64 
65 	if (local->monitors || test_bit(SCAN_SW_SCANNING, &local->scanning) ||
66 	    test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning))
67 		new_flags |= FIF_BCN_PRBRESP_PROMISC;
68 
69 	if (local->fif_probe_req || local->probe_req_reg)
70 		new_flags |= FIF_PROBE_REQ;
71 
72 	if (local->fif_fcsfail)
73 		new_flags |= FIF_FCSFAIL;
74 
75 	if (local->fif_plcpfail)
76 		new_flags |= FIF_PLCPFAIL;
77 
78 	if (local->fif_control)
79 		new_flags |= FIF_CONTROL;
80 
81 	if (local->fif_other_bss)
82 		new_flags |= FIF_OTHER_BSS;
83 
84 	if (local->fif_pspoll)
85 		new_flags |= FIF_PSPOLL;
86 
87 	spin_lock_bh(&local->filter_lock);
88 	changed_flags = local->filter_flags ^ new_flags;
89 
90 	mc = drv_prepare_multicast(local, sdata, &local->mc_list);
91 	spin_unlock_bh(&local->filter_lock);
92 
93 	/* be a bit nasty */
94 	new_flags |= (1<<31);
95 
96 	drv_configure_filter(local, sdata, changed_flags, &new_flags, mc);
97 
98 	WARN_ON(new_flags & (1<<31));
99 
100 	local->filter_flags = new_flags & ~(1<<31);
101 }
102 
ieee80211_reconfig_filter(struct work_struct * work)103 static void ieee80211_reconfig_filter(struct work_struct *work)
104 {
105 	struct ieee80211_local *local =
106 		container_of(work, struct ieee80211_local, reconfig_filter);
107 	struct ieee80211_sub_if_data *sdata;
108 
109 	list_for_each_entry(sdata, &local->interfaces, list)
110 		ieee80211_configure_filter(sdata);
111 }
112 
ieee80211_hw_conf_chan(struct ieee80211_local * local)113 static u32 ieee80211_hw_conf_chan(struct ieee80211_local *local)
114 {
115 	struct ieee80211_sub_if_data *sdata;
116 	struct cfg80211_chan_def chandef = {};
117 	u32 changed = 0;
118 	int power;
119 	u32 offchannel_flag;
120 
121 	offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
122 
123 	if (local->scan_chandef.chan) {
124 		chandef = local->scan_chandef;
125 	} else if (local->tmp_channel) {
126 		chandef.chan = local->tmp_channel;
127 		chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
128 		chandef.center_freq1 = chandef.chan->center_freq;
129 	} else
130 		chandef = local->_oper_chandef;
131 
132 	WARN(!cfg80211_chandef_valid(&chandef),
133 	     "control:%d MHz width:%d center: %d/%d MHz",
134 	     chandef.chan->center_freq, chandef.width,
135 	     chandef.center_freq1, chandef.center_freq2);
136 
137 	if (!cfg80211_chandef_identical(&chandef, &local->_oper_chandef))
138 		local->hw.conf.flags |= IEEE80211_CONF_OFFCHANNEL;
139 	else
140 		local->hw.conf.flags &= ~IEEE80211_CONF_OFFCHANNEL;
141 
142 	offchannel_flag ^= local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
143 
144 	if (offchannel_flag ||
145 	    !cfg80211_chandef_identical(&local->hw.conf.chandef,
146 					&local->_oper_chandef)) {
147 		local->hw.conf.chandef = chandef;
148 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
149 	}
150 
151 	if (!conf_is_ht(&local->hw.conf)) {
152 		/*
153 		 * mac80211.h documents that this is only valid
154 		 * when the channel is set to an HT type, and
155 		 * that otherwise STATIC is used.
156 		 */
157 		local->hw.conf.smps_mode = IEEE80211_SMPS_STATIC;
158 	} else if (local->hw.conf.smps_mode != local->smps_mode) {
159 		local->hw.conf.smps_mode = local->smps_mode;
160 		changed |= IEEE80211_CONF_CHANGE_SMPS;
161 	}
162 
163 	power = ieee80211_chandef_max_power(&chandef);
164 
165 	rcu_read_lock();
166 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
167 		if (!rcu_access_pointer(sdata->vif.chanctx_conf))
168 			continue;
169 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
170 			continue;
171 		power = min(power, sdata->vif.bss_conf.txpower);
172 	}
173 	rcu_read_unlock();
174 
175 	if (local->hw.conf.power_level != power) {
176 		changed |= IEEE80211_CONF_CHANGE_POWER;
177 		local->hw.conf.power_level = power;
178 	}
179 
180 	return changed;
181 }
182 
ieee80211_hw_config(struct ieee80211_local * local,u32 changed)183 int ieee80211_hw_config(struct ieee80211_local *local, u32 changed)
184 {
185 	int ret = 0;
186 
187 	might_sleep();
188 
189 	if (!local->use_chanctx)
190 		changed |= ieee80211_hw_conf_chan(local);
191 	else
192 		changed &= ~(IEEE80211_CONF_CHANGE_CHANNEL |
193 			     IEEE80211_CONF_CHANGE_POWER);
194 
195 	if (changed && local->open_count) {
196 		ret = drv_config(local, changed);
197 		/*
198 		 * Goal:
199 		 * HW reconfiguration should never fail, the driver has told
200 		 * us what it can support so it should live up to that promise.
201 		 *
202 		 * Current status:
203 		 * rfkill is not integrated with mac80211 and a
204 		 * configuration command can thus fail if hardware rfkill
205 		 * is enabled
206 		 *
207 		 * FIXME: integrate rfkill with mac80211 and then add this
208 		 * WARN_ON() back
209 		 *
210 		 */
211 		/* WARN_ON(ret); */
212 	}
213 
214 	return ret;
215 }
216 
ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data * sdata,u32 changed)217 void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
218 				      u32 changed)
219 {
220 	struct ieee80211_local *local = sdata->local;
221 
222 	if (!changed || sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
223 		return;
224 
225 	drv_bss_info_changed(local, sdata, &sdata->vif.bss_conf, changed);
226 }
227 
ieee80211_reset_erp_info(struct ieee80211_sub_if_data * sdata)228 u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
229 {
230 	sdata->vif.bss_conf.use_cts_prot = false;
231 	sdata->vif.bss_conf.use_short_preamble = false;
232 	sdata->vif.bss_conf.use_short_slot = false;
233 	return BSS_CHANGED_ERP_CTS_PROT |
234 	       BSS_CHANGED_ERP_PREAMBLE |
235 	       BSS_CHANGED_ERP_SLOT;
236 }
237 
ieee80211_tasklet_handler(unsigned long data)238 static void ieee80211_tasklet_handler(unsigned long data)
239 {
240 	struct ieee80211_local *local = (struct ieee80211_local *) data;
241 	struct sk_buff *skb;
242 
243 	while ((skb = skb_dequeue(&local->skb_queue)) ||
244 	       (skb = skb_dequeue(&local->skb_queue_unreliable))) {
245 		switch (skb->pkt_type) {
246 		case IEEE80211_RX_MSG:
247 			/* Clear skb->pkt_type in order to not confuse kernel
248 			 * netstack. */
249 			skb->pkt_type = 0;
250 			ieee80211_rx(&local->hw, skb);
251 			break;
252 		case IEEE80211_TX_STATUS_MSG:
253 			skb->pkt_type = 0;
254 			mac80211_tx_status(&local->hw, skb);
255 			break;
256 		default:
257 			WARN(1, "mac80211: Packet is of unknown type %d\n",
258 			     skb->pkt_type);
259 			dev_kfree_skb(skb);
260 			break;
261 		}
262 	}
263 }
264 
ieee80211_restart_work(struct work_struct * work)265 static void ieee80211_restart_work(struct work_struct *work)
266 {
267 	struct ieee80211_local *local =
268 		container_of(work, struct ieee80211_local, restart_work);
269 	struct ieee80211_sub_if_data *sdata;
270 
271 	/* wait for scan work complete */
272 	flush_workqueue(local->workqueue);
273 	flush_work(&local->sched_scan_stopped_work);
274 
275 	WARN(test_bit(SCAN_HW_SCANNING, &local->scanning),
276 	     "%s called with hardware scan in progress\n", __func__);
277 
278 	flush_work(&local->radar_detected_work);
279 	rtnl_lock();
280 	list_for_each_entry(sdata, &local->interfaces, list) {
281 		/*
282 		 * XXX: there may be more work for other vif types and even
283 		 * for station mode: a good thing would be to run most of
284 		 * the iface type's dependent _stop (ieee80211_mg_stop,
285 		 * ieee80211_ibss_stop) etc...
286 		 * For now, fix only the specific bug that was seen: race
287 		 * between csa_connection_drop_work and us.
288 		 */
289 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
290 			/*
291 			 * This worker is scheduled from the iface worker that
292 			 * runs on mac80211's workqueue, so we can't be
293 			 * scheduling this worker after the cancel right here.
294 			 * The exception is ieee80211_chswitch_done.
295 			 * Then we can have a race...
296 			 */
297 			cancel_work_sync(&sdata->u.mgd.csa_connection_drop_work);
298 		}
299 		flush_delayed_work(&sdata->dec_tailroom_needed_wk);
300 	}
301 	ieee80211_scan_cancel(local);
302 
303 	/* make sure any new ROC will consider local->in_reconfig */
304 	flush_delayed_work(&local->roc_work);
305 	flush_work(&local->hw_roc_done);
306 
307 	/* wait for all packet processing to be done */
308 	synchronize_net();
309 
310 	ieee80211_reconfig(local);
311 	rtnl_unlock();
312 }
313 
mac80211_restart_hw(struct ieee80211_hw * hw)314 void mac80211_restart_hw(struct ieee80211_hw *hw)
315 {
316 	struct ieee80211_local *local = hw_to_local(hw);
317 
318 	trace_api_restart_hw(local);
319 
320 	wiphy_info(hw->wiphy,
321 		   "Hardware restart was requested\n");
322 
323 	/* use this reason, ieee80211_reconfig will unblock it */
324 	ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
325 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
326 					false);
327 
328 	/*
329 	 * Stop all Rx during the reconfig. We don't want state changes
330 	 * or driver callbacks while this is in progress.
331 	 */
332 	local->in_reconfig = true;
333 	barrier();
334 
335 	queue_work(system_freezable_wq, &local->restart_work);
336 }
337 
mac80211_ifdev_move(struct ieee80211_hw * hw,struct device * new_parent,int dpm_order)338 int mac80211_ifdev_move(struct ieee80211_hw *hw,
339 			struct device *new_parent, int dpm_order)
340 {
341 	struct ieee80211_local *local = hw_to_local(hw);
342 	struct ieee80211_sub_if_data *sdata;
343 	int ret;
344 
345 	list_for_each_entry(sdata, &local->interfaces, list) {
346 		if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
347 			continue;
348 		ret = device_move(&sdata->dev->dev, new_parent, dpm_order);
349 		if (ret < 0)
350 			return ret;
351 	}
352 	return 0;
353 }
354 
355 
356 #ifdef CONFIG_INET
ieee80211_ifa_changed(struct notifier_block * nb,unsigned long data,void * arg)357 static int ieee80211_ifa_changed(struct notifier_block *nb,
358 				 unsigned long data, void *arg)
359 {
360 	struct in_ifaddr *ifa = arg;
361 	struct ieee80211_local *local =
362 		container_of(nb, struct ieee80211_local,
363 			     ifa_notifier);
364 	struct net_device *ndev = ifa->ifa_dev->dev;
365 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
366 	struct in_device *idev;
367 	struct ieee80211_sub_if_data *sdata;
368 	struct ieee80211_bss_conf *bss_conf;
369 	struct ieee80211_if_managed *ifmgd;
370 	int c = 0;
371 
372 	/* Make sure it's our interface that got changed */
373 	if (!wdev)
374 		return NOTIFY_DONE;
375 
376 	if (wdev->wiphy != local->hw.wiphy)
377 		return NOTIFY_DONE;
378 
379 	sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
380 	bss_conf = &sdata->vif.bss_conf;
381 
382 	/* ARP filtering is only supported in managed mode */
383 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
384 		return NOTIFY_DONE;
385 
386 	idev = __in_dev_get_rtnl(sdata->dev);
387 	if (!idev)
388 		return NOTIFY_DONE;
389 
390 	ifmgd = &sdata->u.mgd;
391 	sdata_lock(sdata);
392 
393 	/* Copy the addresses to the bss_conf list */
394 	ifa = rtnl_dereference(idev->ifa_list);
395 	while (ifa) {
396 		if (c < IEEE80211_BSS_ARP_ADDR_LIST_LEN)
397 			bss_conf->arp_addr_list[c] = ifa->ifa_address;
398 		ifa = rtnl_dereference(ifa->ifa_next);
399 		c++;
400 	}
401 
402 	bss_conf->arp_addr_cnt = c;
403 
404 	/* Configure driver only if associated (which also implies it is up) */
405 	if (ifmgd->associated)
406 		ieee80211_bss_info_change_notify(sdata,
407 						 BSS_CHANGED_ARP_FILTER);
408 
409 	sdata_unlock(sdata);
410 
411 	return NOTIFY_OK;
412 }
413 #endif
414 
415 #if IPV6_FILTERING//IS_ENABLED(CONFIG_IPV6)
ieee80211_ifa6_changed_work(struct work_struct * work)416 static void ieee80211_ifa6_changed_work(struct work_struct *work)
417 {
418 	struct ieee80211_local *local =
419 			container_of(work, struct ieee80211_local, ifa6_changed_work);
420 	struct ieee80211_sub_if_data *sdata = local->ifa6_sdata;
421 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
422 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
423 
424 	if (ifmgd->associated) {
425 		bss_conf->ndp_filter_enabled = sdata->ndp_filter_state;
426 		ieee80211_bss_info_change_notify(sdata,
427 							BSS_CHANGED_NDP_FILTER);
428 	}
429 }
430 
ieee80211_ifa6_changed(struct notifier_block * nb,unsigned long data,void * arg)431 static int ieee80211_ifa6_changed(struct notifier_block *nb,
432 				  unsigned long data, void *arg)
433 {
434 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)arg;
435 	struct inet6_dev *idev = ifa->idev;
436 	struct net_device *ndev = ifa->idev->dev;
437 	struct ieee80211_local *local =
438 		container_of(nb, struct ieee80211_local, ifa6_notifier);
439 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
440 	struct ieee80211_sub_if_data *sdata;
441 	struct ieee80211_bss_conf *bss_conf;
442 	int c = 0;
443 
444 	/* Make sure it's our interface that got changed */
445 	if (!wdev || wdev->wiphy != local->hw.wiphy)
446 		return NOTIFY_DONE;
447 
448 	sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
449 	bss_conf = &sdata->vif.bss_conf;
450 
451 	/* NDP filtering is only supported in managed mode */
452 	switch (sdata->vif.type) {
453 	case NL80211_IFTYPE_STATION:
454 	case NL80211_IFTYPE_AP:
455 	case NL80211_IFTYPE_P2P_GO:
456 		break;
457 	default:
458 		return NOTIFY_DONE;
459 	}
460 
461 #if 0
462 	/*
463 	 * For now only support station mode. This is mostly because
464 	 * doing AP would have to handle AP_VLAN in some way ...
465 	 */
466 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
467 		return NOTIFY_DONE;
468 #endif
469 
470 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
471 		c++;
472 		if (c > IEEE80211_BSS_NDP_ADDR_LIST_LEN)
473 			break;
474 		memcpy(&bss_conf->ndp_addr_list[c-1],
475 				&ifa->addr, sizeof(struct in6_addr));
476 	}
477 	if (c > IEEE80211_BSS_NDP_ADDR_LIST_LEN) {
478 		sdata->ndp_filter_state = false;
479 		c = 0;
480 	} else {
481 		sdata->ndp_filter_state = true;
482 	}
483 	bss_conf->ndp_addr_cnt = c;
484 
485 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
486 		local->ifa6_sdata = sdata;
487 		/*drv_ipv6_addr_change(local, sdata, idev);*/
488 		schedule_work(&local->ifa6_changed_work);
489 	} else {
490 		bss_conf->ndp_filter_enabled = sdata->ndp_filter_state;
491 	}
492 	return NOTIFY_OK;
493 }
494 #endif/*IPV6_FILTERING*/
495 
496 /* There isn't a lot of sense in it, but you can transmit anything you like */
497 static const struct ieee80211_txrx_stypes
498 ieee80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = {
499 	[NL80211_IFTYPE_ADHOC] = {
500 		.tx = 0xffff,
501 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
502 			BIT(IEEE80211_STYPE_AUTH >> 4) |
503 			BIT(IEEE80211_STYPE_DEAUTH >> 4) |
504 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
505 	},
506 	[NL80211_IFTYPE_STATION] = {
507 		.tx = 0xffff,
508 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
509 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
510 	},
511 	[NL80211_IFTYPE_AP] = {
512 		.tx = 0xffff,
513 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
514 			BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
515 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
516 			BIT(IEEE80211_STYPE_DISASSOC >> 4) |
517 			BIT(IEEE80211_STYPE_AUTH >> 4) |
518 			BIT(IEEE80211_STYPE_DEAUTH >> 4) |
519 			BIT(IEEE80211_STYPE_ACTION >> 4),
520 	},
521 	[NL80211_IFTYPE_AP_VLAN] = {
522 		/* copy AP */
523 		.tx = 0xffff,
524 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
525 			BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
526 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
527 			BIT(IEEE80211_STYPE_DISASSOC >> 4) |
528 			BIT(IEEE80211_STYPE_AUTH >> 4) |
529 			BIT(IEEE80211_STYPE_DEAUTH >> 4) |
530 			BIT(IEEE80211_STYPE_ACTION >> 4),
531 	},
532 	[NL80211_IFTYPE_P2P_CLIENT] = {
533 		.tx = 0xffff,
534 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
535 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
536 	},
537 	[NL80211_IFTYPE_P2P_GO] = {
538 		.tx = 0xffff,
539 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
540 			BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
541 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
542 			BIT(IEEE80211_STYPE_DISASSOC >> 4) |
543 			BIT(IEEE80211_STYPE_AUTH >> 4) |
544 			BIT(IEEE80211_STYPE_DEAUTH >> 4) |
545 			BIT(IEEE80211_STYPE_ACTION >> 4),
546 	},
547 	[NL80211_IFTYPE_MESH_POINT] = {
548 		.tx = 0xffff,
549 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
550 			BIT(IEEE80211_STYPE_AUTH >> 4) |
551 			BIT(IEEE80211_STYPE_DEAUTH >> 4),
552 	},
553 	[NL80211_IFTYPE_P2P_DEVICE] = {
554 		.tx = 0xffff,
555 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
556 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
557 	},
558 };
559 
560 static const struct ieee80211_ht_cap mac80211_ht_capa_mod_mask = {
561 	.ampdu_params_info = IEEE80211_HT_AMPDU_PARM_FACTOR |
562 			     IEEE80211_HT_AMPDU_PARM_DENSITY,
563 
564 	.cap_info = cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
565 				IEEE80211_HT_CAP_MAX_AMSDU |
566 				IEEE80211_HT_CAP_SGI_20 |
567 				IEEE80211_HT_CAP_SGI_40 |
568 				IEEE80211_HT_CAP_TX_STBC |
569 				IEEE80211_HT_CAP_RX_STBC |
570 				IEEE80211_HT_CAP_LDPC_CODING |
571 				IEEE80211_HT_CAP_40MHZ_INTOLERANT),
572 	.mcs = {
573 		.rx_mask = { 0xff, 0xff, 0xff, 0xff, 0xff,
574 			     0xff, 0xff, 0xff, 0xff, 0xff, },
575 	},
576 };
577 
578 static const struct ieee80211_vht_cap mac80211_vht_capa_mod_mask = {
579 	.vht_cap_info =
580 		cpu_to_le32(IEEE80211_VHT_CAP_RXLDPC |
581 			    IEEE80211_VHT_CAP_SHORT_GI_80 |
582 			    IEEE80211_VHT_CAP_SHORT_GI_160 |
583 			    IEEE80211_VHT_CAP_RXSTBC_MASK |
584 			    IEEE80211_VHT_CAP_TXSTBC |
585 			    IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
586 			    IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
587 			    IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
588 			    IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
589 			    IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK),
590 	.supp_mcs = {
591 		.rx_mcs_map = cpu_to_le16(~0),
592 		.tx_mcs_map = cpu_to_le16(~0),
593 	},
594 };
595 
596 #ifdef CONFIG_DRIVERS_HDF_XR829
597 static const struct ieee80211_regdomain xr829_regdom = {
598 	.n_reg_rules = 4,
599 	.alpha2 =  "99",
600 	.reg_rules = {
601 		/* IEEE 802.11b/g, channels 1..11 */
602 		REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
603 		/* If any */
604 		/* IEEE 802.11 channel 14 - Only JP enables
605 		 * this and for 802.11b only
606 		 */
607 		REG_RULE(2484-10, 2484+10, 20, 6, 20, 0),
608 		/* IEEE 802.11a, channel 36..64 */
609 		REG_RULE(5150-10, 5350+10, 40, 6, 20, 0),
610 		/* IEEE 802.11a, channel 100..165 */
611 		REG_RULE(5470-10, 5850+10, 40, 6, 20, 0), }
612 };
613 #endif
614 
mac80211_alloc_hw_nm(size_t priv_data_len,const struct ieee80211_ops * ops,const char * requested_name)615 struct ieee80211_hw *mac80211_alloc_hw_nm(size_t priv_data_len,
616 					   const struct ieee80211_ops *ops,
617 					   const char *requested_name)
618 {
619 	struct ieee80211_local *local;
620 	int priv_size, i;
621 	struct wiphy *wiphy;
622 	bool use_chanctx;
623 
624 	if (WARN_ON(!ops->tx || !ops->start || !ops->stop || !ops->config ||
625 		    !ops->add_interface || !ops->remove_interface ||
626 		    !ops->configure_filter))
627 		return NULL;
628 
629 	if (WARN_ON(ops->sta_state && (ops->sta_add || ops->sta_remove)))
630 		return NULL;
631 
632 	/* check all or no channel context operations exist */
633 	i = !!ops->add_chanctx + !!ops->remove_chanctx +
634 	    !!ops->change_chanctx + !!ops->assign_vif_chanctx +
635 	    !!ops->unassign_vif_chanctx;
636 	if (WARN_ON(i != 0 && i != 5))
637 		return NULL;
638 	use_chanctx = i == 5;
639 
640 	/* Ensure 32-byte alignment of our private data and hw private data.
641 	 * We use the wiphy priv data for both our ieee80211_local and for
642 	 * the driver's private data
643 	 *
644 	 * In memory it'll be like this:
645 	 *
646 	 * +-------------------------+
647 	 * | struct wiphy	    |
648 	 * +-------------------------+
649 	 * | struct ieee80211_local  |
650 	 * +-------------------------+
651 	 * | driver's private data   |
652 	 * +-------------------------+
653 	 *
654 	 */
655 	priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
656 
657 #ifdef CONFIG_DRIVERS_HDF_XR829
658 	wiphy = wiphy_new_nm(&xrmac_config_ops, priv_size, requested_name);
659 #else
660 	wiphy = wiphy_new_nm(&mac80211_config_ops, priv_size, requested_name);
661 #endif
662 
663 	if (!wiphy)
664 		return NULL;
665 
666 	wiphy->mgmt_stypes = ieee80211_default_mgmt_stypes;
667 
668 	wiphy->privid = mac80211_wiphy_privid;
669 
670 	wiphy->flags |= WIPHY_FLAG_NETNS_OK |
671 			WIPHY_FLAG_4ADDR_AP |
672 			WIPHY_FLAG_4ADDR_STATION |
673 			WIPHY_FLAG_REPORTS_OBSS |
674 			WIPHY_FLAG_OFFCHAN_TX;
675 
676 	if (ops->remain_on_channel)
677 		wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
678 
679 	wiphy->features |= NL80211_FEATURE_SK_TX_STATUS |
680 			   NL80211_FEATURE_SAE |
681 			   NL80211_FEATURE_HT_IBSS |
682 			   NL80211_FEATURE_VIF_TXPOWER |
683 			   NL80211_FEATURE_MAC_ON_CREATE |
684 			   NL80211_FEATURE_USERSPACE_MPM |
685 			   NL80211_FEATURE_FULL_AP_CLIENT_STATE;
686 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_FILS_STA);
687 	wiphy_ext_feature_set(wiphy,
688 			      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211);
689 
690 	if (!ops->hw_scan) {
691 		wiphy->features |= NL80211_FEATURE_LOW_PRIORITY_SCAN |
692 				   NL80211_FEATURE_AP_SCAN;
693 		/*
694 		 * if the driver behaves correctly using the probe request
695 		 * (template) from mac80211, then both of these should be
696 		 * supported even with hw scan - but let drivers opt in.
697 		 */
698 		wiphy_ext_feature_set(wiphy,
699 				      NL80211_EXT_FEATURE_SCAN_RANDOM_SN);
700 		wiphy_ext_feature_set(wiphy,
701 				      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
702 	}
703 
704 	if (!ops->set_key)
705 		wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
706 
707 	if (ops->wake_tx_queue)
708 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_TXQS);
709 
710 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_RRM);
711 
712 	wiphy->bss_priv_size = sizeof(struct ieee80211_bss);
713 
714 	local = wiphy_priv(wiphy);
715 
716 	if (sta_info_init(local))
717 		goto err_free;
718 
719 	local->hw.wiphy = wiphy;
720 
721 #ifdef CONFIG_DRIVERS_HDF_XR829
722 	hdf_wiphy_register(wiphy);
723 #endif
724 
725 	local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
726 
727 	local->ops = ops;
728 	local->use_chanctx = use_chanctx;
729 
730 	/*
731 	 * We need a bit of data queued to build aggregates properly, so
732 	 * instruct the TCP stack to allow more than a single ms of data
733 	 * to be queued in the stack. The value is a bit-shift of 1
734 	 * second, so 7 is ~8ms of queued data. Only affects local TCP
735 	 * sockets.
736 	 * This is the default, anyhow - drivers may need to override it
737 	 * for local reasons (longer buffers, longer completion time, or
738 	 * similar).
739 	 */
740 	local->hw.tx_sk_pacing_shift = 7;
741 
742 	/* set up some defaults */
743 	local->hw.queues = 1;
744 	local->hw.max_rates = 1;
745 	local->hw.max_report_rates = 0;
746 	local->hw.max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HT;
747 	local->hw.max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HT;
748 	local->hw.offchannel_tx_hw_queue = IEEE80211_INVAL_HW_QUEUE;
749 	local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
750 	local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
751 	local->hw.radiotap_mcs_details = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
752 					 IEEE80211_RADIOTAP_MCS_HAVE_GI |
753 					 IEEE80211_RADIOTAP_MCS_HAVE_BW;
754 	local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
755 					 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
756 	local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
757 	local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
758 	local->hw.max_mtu = IEEE80211_MAX_DATA_LEN;
759 	local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
760 	wiphy->ht_capa_mod_mask = &mac80211_ht_capa_mod_mask;
761 	wiphy->vht_capa_mod_mask = &mac80211_vht_capa_mod_mask;
762 
763 	local->ext_capa[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF;
764 
765 	wiphy->extended_capabilities = local->ext_capa;
766 	wiphy->extended_capabilities_mask = local->ext_capa;
767 	wiphy->extended_capabilities_len =
768 		ARRAY_SIZE(local->ext_capa);
769 
770 	INIT_LIST_HEAD(&local->interfaces);
771 	INIT_LIST_HEAD(&local->mon_list);
772 
773 	__hw_addr_init(&local->mc_list);
774 
775 	mutex_init(&local->iflist_mtx);
776 	mutex_init(&local->mtx);
777 
778 	mutex_init(&local->key_mtx);
779 	spin_lock_init(&local->filter_lock);
780 	spin_lock_init(&local->rx_path_lock);
781 	spin_lock_init(&local->queue_stop_reason_lock);
782 
783 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
784 		INIT_LIST_HEAD(&local->active_txqs[i]);
785 		spin_lock_init(&local->active_txq_lock[i]);
786 	}
787 	local->airtime_flags = AIRTIME_USE_TX | AIRTIME_USE_RX;
788 
789 	INIT_LIST_HEAD(&local->chanctx_list);
790 	mutex_init(&local->chanctx_mtx);
791 
792 	INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
793 
794 	INIT_WORK(&local->restart_work, ieee80211_restart_work);
795 
796 #ifdef CONFIG_INET
797 #ifdef IPV6_FILTERING
798 	INIT_WORK(&local->ifa6_changed_work, ieee80211_ifa6_changed_work);
799 #endif /*IPV6_FILTERING*/
800 #endif
801 
802 	INIT_WORK(&local->radar_detected_work,
803 		  ieee80211_dfs_radar_detected_work);
804 
805 	INIT_WORK(&local->reconfig_filter, ieee80211_reconfig_filter);
806 	local->smps_mode = IEEE80211_SMPS_OFF;
807 
808 	INIT_WORK(&local->dynamic_ps_enable_work,
809 		  ieee80211_dynamic_ps_enable_work);
810 	INIT_WORK(&local->dynamic_ps_disable_work,
811 		  ieee80211_dynamic_ps_disable_work);
812 	timer_setup(&local->dynamic_ps_timer, ieee80211_dynamic_ps_timer, 0);
813 
814 	INIT_WORK(&local->sched_scan_stopped_work,
815 		  ieee80211_sched_scan_stopped_work);
816 
817 	INIT_WORK(&local->tdls_chsw_work, ieee80211_tdls_chsw_work);
818 
819 	spin_lock_init(&local->ack_status_lock);
820 	idr_init(&local->ack_status_frames);
821 
822 	for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
823 		skb_queue_head_init(&local->pending[i]);
824 		atomic_set(&local->agg_queue_stop[i], 0);
825 	}
826 	tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
827 		     (unsigned long)local);
828 
829 	if (ops->wake_tx_queue)
830 		tasklet_init(&local->wake_txqs_tasklet, ieee80211_wake_txqs,
831 			     (unsigned long)local);
832 
833 	tasklet_init(&local->tasklet,
834 		     ieee80211_tasklet_handler,
835 		     (unsigned long) local);
836 
837 	skb_queue_head_init(&local->skb_queue);
838 	skb_queue_head_init(&local->skb_queue_unreliable);
839 	skb_queue_head_init(&local->skb_queue_tdls_chsw);
840 
841 	ieee80211_alloc_led_names(local);
842 
843 	ieee80211_roc_setup(local);
844 
845 	local->hw.radiotap_timestamp.units_pos = -1;
846 	local->hw.radiotap_timestamp.accuracy = -1;
847 
848 	return &local->hw;
849  err_free:
850 	wiphy_free(wiphy);
851 	return NULL;
852 }
853 
854 
ieee80211_init_cipher_suites(struct ieee80211_local * local)855 static int ieee80211_init_cipher_suites(struct ieee80211_local *local)
856 {
857 	bool have_wep = !fips_enabled; /* FIPS does not permit the use of RC4 */
858 	bool have_mfp = ieee80211_hw_check(&local->hw, MFP_CAPABLE);
859 	int n_suites = 0, r = 0, w = 0;
860 	u32 *suites;
861 	static const u32 cipher_suites[] = {
862 		/* keep WEP first, it may be removed below */
863 		WLAN_CIPHER_SUITE_WEP40,
864 		WLAN_CIPHER_SUITE_WEP104,
865 		WLAN_CIPHER_SUITE_TKIP,
866 		WLAN_CIPHER_SUITE_CCMP,
867 		WLAN_CIPHER_SUITE_CCMP_256,
868 		WLAN_CIPHER_SUITE_GCMP,
869 		WLAN_CIPHER_SUITE_GCMP_256,
870 
871 		/* keep last -- depends on hw flags! */
872 		WLAN_CIPHER_SUITE_AES_CMAC,
873 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
874 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
875 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
876 	};
877 
878 	if (ieee80211_hw_check(&local->hw, SW_CRYPTO_CONTROL) ||
879 	    local->hw.wiphy->cipher_suites) {
880 		/* If the driver advertises, or doesn't support SW crypto,
881 		 * we only need to remove WEP if necessary.
882 		 */
883 		if (have_wep)
884 			return 0;
885 
886 		/* well if it has _no_ ciphers ... fine */
887 		if (!local->hw.wiphy->n_cipher_suites)
888 			return 0;
889 
890 		/* Driver provides cipher suites, but we need to exclude WEP */
891 		suites = kmemdup(local->hw.wiphy->cipher_suites,
892 				 sizeof(u32) * local->hw.wiphy->n_cipher_suites,
893 				 GFP_KERNEL);
894 		if (!suites)
895 			return -ENOMEM;
896 
897 		for (r = 0; r < local->hw.wiphy->n_cipher_suites; r++) {
898 			u32 suite = local->hw.wiphy->cipher_suites[r];
899 
900 			if (suite == WLAN_CIPHER_SUITE_WEP40 ||
901 			    suite == WLAN_CIPHER_SUITE_WEP104)
902 				continue;
903 			suites[w++] = suite;
904 		}
905 	} else if (!local->hw.cipher_schemes) {
906 		/* If the driver doesn't have cipher schemes, there's nothing
907 		 * else to do other than assign the (software supported and
908 		 * perhaps offloaded) cipher suites.
909 		 */
910 		local->hw.wiphy->cipher_suites = cipher_suites;
911 		local->hw.wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
912 
913 		if (!have_mfp)
914 			local->hw.wiphy->n_cipher_suites -= 4;
915 
916 		if (!have_wep) {
917 			local->hw.wiphy->cipher_suites += 2;
918 			local->hw.wiphy->n_cipher_suites -= 2;
919 		}
920 
921 		/* not dynamically allocated, so just return */
922 		return 0;
923 	} else {
924 		const struct ieee80211_cipher_scheme *cs;
925 
926 		cs = local->hw.cipher_schemes;
927 
928 		/* Driver specifies cipher schemes only (but not cipher suites
929 		 * including the schemes)
930 		 *
931 		 * We start counting ciphers defined by schemes, TKIP, CCMP,
932 		 * CCMP-256, GCMP, and GCMP-256
933 		 */
934 		n_suites = local->hw.n_cipher_schemes + 5;
935 
936 		/* check if we have WEP40 and WEP104 */
937 		if (have_wep)
938 			n_suites += 2;
939 
940 		/* check if we have AES_CMAC, BIP-CMAC-256, BIP-GMAC-128,
941 		 * BIP-GMAC-256
942 		 */
943 		if (have_mfp)
944 			n_suites += 4;
945 
946 		suites = kmalloc_array(n_suites, sizeof(u32), GFP_KERNEL);
947 		if (!suites)
948 			return -ENOMEM;
949 
950 		suites[w++] = WLAN_CIPHER_SUITE_CCMP;
951 		suites[w++] = WLAN_CIPHER_SUITE_CCMP_256;
952 		suites[w++] = WLAN_CIPHER_SUITE_TKIP;
953 		suites[w++] = WLAN_CIPHER_SUITE_GCMP;
954 		suites[w++] = WLAN_CIPHER_SUITE_GCMP_256;
955 
956 		if (have_wep) {
957 			suites[w++] = WLAN_CIPHER_SUITE_WEP40;
958 			suites[w++] = WLAN_CIPHER_SUITE_WEP104;
959 		}
960 
961 		if (have_mfp) {
962 			suites[w++] = WLAN_CIPHER_SUITE_AES_CMAC;
963 			suites[w++] = WLAN_CIPHER_SUITE_BIP_CMAC_256;
964 			suites[w++] = WLAN_CIPHER_SUITE_BIP_GMAC_128;
965 			suites[w++] = WLAN_CIPHER_SUITE_BIP_GMAC_256;
966 		}
967 
968 		for (r = 0; r < local->hw.n_cipher_schemes; r++) {
969 			suites[w++] = cs[r].cipher;
970 			if (WARN_ON(cs[r].pn_len > IEEE80211_MAX_PN_LEN)) {
971 				kfree(suites);
972 				return -EINVAL;
973 			}
974 		}
975 	}
976 
977 	local->hw.wiphy->cipher_suites = suites;
978 	local->hw.wiphy->n_cipher_suites = w;
979 	local->wiphy_ciphers_allocated = true;
980 
981 	return 0;
982 }
983 
mac80211_register_hw(struct ieee80211_hw * hw)984 int mac80211_register_hw(struct ieee80211_hw *hw)
985 {
986 	struct ieee80211_local *local = hw_to_local(hw);
987 	int result, i;
988 	enum nl80211_band band;
989 	int channels, max_bitrates;
990 	bool supp_ht, supp_vht, supp_he;
991 	netdev_features_t feature_whitelist;
992 	struct cfg80211_chan_def dflt_chandef = {};
993 
994 	if (ieee80211_hw_check(hw, QUEUE_CONTROL) &&
995 	    (local->hw.offchannel_tx_hw_queue == IEEE80211_INVAL_HW_QUEUE ||
996 	     local->hw.offchannel_tx_hw_queue >= local->hw.queues))
997 		return -EINVAL;
998 
999 	if ((hw->wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) &&
1000 	    (!local->ops->tdls_channel_switch ||
1001 	     !local->ops->tdls_cancel_channel_switch ||
1002 	     !local->ops->tdls_recv_channel_switch))
1003 		return -EOPNOTSUPP;
1004 
1005 	if (WARN_ON(ieee80211_hw_check(hw, SUPPORTS_TX_FRAG) &&
1006 		    !local->ops->set_frag_threshold))
1007 		return -EINVAL;
1008 
1009 	if (WARN_ON(local->hw.wiphy->interface_modes &
1010 			BIT(NL80211_IFTYPE_NAN) &&
1011 		    (!local->ops->start_nan || !local->ops->stop_nan)))
1012 		return -EINVAL;
1013 
1014 #ifdef CONFIG_PM
1015 	if (hw->wiphy->wowlan && (!local->ops->suspend || !local->ops->resume))
1016 		return -EINVAL;
1017 #endif
1018 
1019 	if (!local->use_chanctx) {
1020 		for (i = 0; i < local->hw.wiphy->n_iface_combinations; i++) {
1021 			const struct ieee80211_iface_combination *comb;
1022 
1023 			comb = &local->hw.wiphy->iface_combinations[i];
1024 
1025 			if (comb->num_different_channels > 1)
1026 				return -EINVAL;
1027 		}
1028 	} else {
1029 		/*
1030 		 * WDS is currently prohibited when channel contexts are used
1031 		 * because there's no clear definition of which channel WDS
1032 		 * type interfaces use
1033 		 */
1034 		if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_WDS))
1035 			return -EINVAL;
1036 
1037 		/* DFS is not supported with multi-channel combinations yet */
1038 		for (i = 0; i < local->hw.wiphy->n_iface_combinations; i++) {
1039 			const struct ieee80211_iface_combination *comb;
1040 
1041 			comb = &local->hw.wiphy->iface_combinations[i];
1042 
1043 			if (comb->radar_detect_widths &&
1044 			    comb->num_different_channels > 1)
1045 				return -EINVAL;
1046 		}
1047 	}
1048 
1049 	/* Only HW csum features are currently compatible with mac80211 */
1050 	feature_whitelist = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1051 			    NETIF_F_HW_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA |
1052 			    NETIF_F_GSO_SOFTWARE | NETIF_F_RXCSUM;
1053 	if (WARN_ON(hw->netdev_features & ~feature_whitelist))
1054 		return -EINVAL;
1055 
1056 	if (hw->max_report_rates == 0)
1057 		hw->max_report_rates = hw->max_rates;
1058 
1059 	local->rx_chains = 1;
1060 
1061 	/*
1062 	 * generic code guarantees at least one band,
1063 	 * set this very early because much code assumes
1064 	 * that hw.conf.channel is assigned
1065 	 */
1066 	channels = 0;
1067 	max_bitrates = 0;
1068 	supp_ht = false;
1069 	supp_vht = false;
1070 	supp_he = false;
1071 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1072 		struct ieee80211_supported_band *sband;
1073 
1074 		sband = local->hw.wiphy->bands[band];
1075 		if (!sband)
1076 			continue;
1077 
1078 		if (!dflt_chandef.chan) {
1079 			cfg80211_chandef_create(&dflt_chandef,
1080 						&sband->channels[0],
1081 						NL80211_CHAN_NO_HT);
1082 			/* init channel we're on */
1083 			if (!local->use_chanctx && !local->_oper_chandef.chan) {
1084 				local->hw.conf.chandef = dflt_chandef;
1085 				local->_oper_chandef = dflt_chandef;
1086 			}
1087 			local->monitor_chandef = dflt_chandef;
1088 		}
1089 
1090 		channels += sband->n_channels;
1091 
1092 		if (max_bitrates < sband->n_bitrates)
1093 			max_bitrates = sband->n_bitrates;
1094 		supp_ht = supp_ht || sband->ht_cap.ht_supported;
1095 		supp_vht = supp_vht || sband->vht_cap.vht_supported;
1096 
1097 		if (!supp_he)
1098 			supp_he = !!ieee80211_get_he_sta_cap(sband);
1099 
1100 		if (!sband->ht_cap.ht_supported)
1101 			continue;
1102 
1103 		/* TODO: consider VHT for RX chains, hopefully it's the same */
1104 		local->rx_chains =
1105 			max(ieee80211_mcs_to_chains(&sband->ht_cap.mcs),
1106 			    local->rx_chains);
1107 
1108 		/* no need to mask, SM_PS_DISABLED has all bits set */
1109 		sband->ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
1110 					IEEE80211_HT_CAP_SM_PS_SHIFT;
1111 	}
1112 
1113 	/* if low-level driver supports AP, we also support VLAN.
1114 	 * drivers advertising SW_CRYPTO_CONTROL should enable AP_VLAN
1115 	 * based on their support to transmit SW encrypted packets.
1116 	 */
1117 	if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP) &&
1118 	    !ieee80211_hw_check(&local->hw, SW_CRYPTO_CONTROL)) {
1119 		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
1120 		hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_AP_VLAN);
1121 	}
1122 
1123 	/* mac80211 always supports monitor */
1124 	hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
1125 	hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR);
1126 
1127 	/* mac80211 doesn't support more than one IBSS interface right now */
1128 	for (i = 0; i < hw->wiphy->n_iface_combinations; i++) {
1129 		const struct ieee80211_iface_combination *c;
1130 		int j;
1131 
1132 		c = &hw->wiphy->iface_combinations[i];
1133 
1134 		for (j = 0; j < c->n_limits; j++)
1135 			if ((c->limits[j].types & BIT(NL80211_IFTYPE_ADHOC)) &&
1136 			    c->limits[j].max > 1)
1137 				return -EINVAL;
1138 	}
1139 
1140 	local->int_scan_req = kzalloc(sizeof(*local->int_scan_req) +
1141 				      sizeof(void *) * channels, GFP_KERNEL);
1142 	if (!local->int_scan_req)
1143 		return -ENOMEM;
1144 
1145 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1146 		if (!local->hw.wiphy->bands[band])
1147 			continue;
1148 		local->int_scan_req->rates[band] = (u32) -1;
1149 	}
1150 
1151 #ifndef CONFIG_MAC80211_MESH
1152 	/* mesh depends on Kconfig, but drivers should set it if they want */
1153 	local->hw.wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MESH_POINT);
1154 #endif
1155 
1156 	/* if the underlying driver supports mesh, mac80211 will (at least)
1157 	 * provide routing of mesh authentication frames to userspace */
1158 	if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
1159 		local->hw.wiphy->flags |= WIPHY_FLAG_MESH_AUTH;
1160 
1161 	/* mac80211 supports control port protocol changing */
1162 	local->hw.wiphy->flags |= WIPHY_FLAG_CONTROL_PORT_PROTOCOL;
1163 
1164 	if (ieee80211_hw_check(&local->hw, SIGNAL_DBM)) {
1165 		printk("mac80211_register_hw signal_type CFG80211_SIGNAL_TYPE_MBM");
1166 		local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1167 	} else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC)) {
1168 		local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
1169 		printk("mac80211_register_hw signal_type CFG80211_SIGNAL_TYPE_UNSPEC");
1170 		if (hw->max_signal <= 0) {
1171 			result = -EINVAL;
1172 			goto fail_workqueue;
1173 		}
1174 	}
1175 
1176 	/* Mac80211 and therefore all drivers using SW crypto only
1177 	 * are able to handle PTK rekeys and Extended Key ID.
1178 	 */
1179 	if (!local->ops->set_key) {
1180 		wiphy_ext_feature_set(local->hw.wiphy,
1181 				      NL80211_EXT_FEATURE_CAN_REPLACE_PTK0);
1182 		wiphy_ext_feature_set(local->hw.wiphy,
1183 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
1184 	}
1185 
1186 	/*
1187 	 * Calculate scan IE length -- we need this to alloc
1188 	 * memory and to subtract from the driver limit. It
1189 	 * includes the DS Params, (extended) supported rates, and HT
1190 	 * information -- SSID is the driver's responsibility.
1191 	 */
1192 	local->scan_ies_len = 4 + max_bitrates /* (ext) supp rates */ +
1193 		3 /* DS Params */;
1194 	if (supp_ht)
1195 		local->scan_ies_len += 2 + sizeof(struct ieee80211_ht_cap);
1196 
1197 	if (supp_vht)
1198 		local->scan_ies_len +=
1199 			2 + sizeof(struct ieee80211_vht_cap);
1200 
1201 	/* HE cap element is variable in size - set len to allow max size */
1202 	/*
1203 	 * TODO: 1 is added at the end of the calculation to accommodate for
1204 	 *	the temporary placing of the HE capabilities IE under EXT.
1205 	 *	Remove it once it is placed in the final place.
1206 	 */
1207 	if (supp_he)
1208 		local->scan_ies_len +=
1209 			2 + sizeof(struct ieee80211_he_cap_elem) +
1210 			sizeof(struct ieee80211_he_mcs_nss_supp) +
1211 			IEEE80211_HE_PPE_THRES_MAX_LEN + 1;
1212 
1213 	if (!local->ops->hw_scan) {
1214 		/* For hw_scan, driver needs to set these up. */
1215 		local->hw.wiphy->max_scan_ssids = 4;
1216 		local->hw.wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
1217 	}
1218 
1219 	/*
1220 	 * If the driver supports any scan IEs, then assume the
1221 	 * limit includes the IEs mac80211 will add, otherwise
1222 	 * leave it at zero and let the driver sort it out; we
1223 	 * still pass our IEs to the driver but userspace will
1224 	 * not be allowed to in that case.
1225 	 */
1226 	if (local->hw.wiphy->max_scan_ie_len)
1227 		local->hw.wiphy->max_scan_ie_len -= local->scan_ies_len;
1228 
1229 	WARN_ON(!ieee80211_cs_list_valid(local->hw.cipher_schemes,
1230 					 local->hw.n_cipher_schemes));
1231 
1232 	result = ieee80211_init_cipher_suites(local);
1233 	if (result < 0)
1234 		goto fail_workqueue;
1235 
1236 	if (!local->ops->remain_on_channel)
1237 		local->hw.wiphy->max_remain_on_channel_duration = 5000;
1238 
1239 	/* mac80211 based drivers don't support internal TDLS setup */
1240 	if (local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS)
1241 		local->hw.wiphy->flags |= WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
1242 
1243 	/* mac80211 supports eCSA, if the driver supports STA CSA at all */
1244 	if (ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA))
1245 		local->ext_capa[0] |= WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING;
1246 
1247 	/* mac80211 supports multi BSSID, if the driver supports it */
1248 	if (ieee80211_hw_check(&local->hw, SUPPORTS_MULTI_BSSID)) {
1249 		local->hw.wiphy->support_mbssid = true;
1250 		if (ieee80211_hw_check(&local->hw,
1251 				       SUPPORTS_ONLY_HE_MULTI_BSSID))
1252 			local->hw.wiphy->support_only_he_mbssid = true;
1253 		else
1254 			local->ext_capa[2] |=
1255 				WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
1256 	}
1257 
1258 	local->hw.wiphy->max_num_csa_counters = IEEE80211_MAX_CSA_COUNTERS_NUM;
1259 
1260 	/*
1261 	 * We use the number of queues for feature tests (QoS, HT) internally
1262 	 * so restrict them appropriately.
1263 	 */
1264 	if (hw->queues > IEEE80211_MAX_QUEUES)
1265 		hw->queues = IEEE80211_MAX_QUEUES;
1266 
1267 	local->workqueue =
1268 		alloc_ordered_workqueue("%s", 0, wiphy_name(local->hw.wiphy));
1269 	if (!local->workqueue) {
1270 		result = -ENOMEM;
1271 		goto fail_workqueue;
1272 	}
1273 
1274 	/*
1275 	 * The hardware needs headroom for sending the frame,
1276 	 * and we need some headroom for passing the frame to monitor
1277 	 * interfaces, but never both at the same time.
1278 	 */
1279 	local->tx_headroom = max_t(unsigned int, local->hw.extra_tx_headroom,
1280 				   IEEE80211_TX_STATUS_HEADROOM);
1281 
1282 	/*
1283 	 * if the driver doesn't specify a max listen interval we
1284 	 * use 5 which should be a safe default
1285 	 */
1286 	if (local->hw.max_listen_interval == 0)
1287 		local->hw.max_listen_interval = 5;
1288 
1289 	local->hw.conf.listen_interval = local->hw.max_listen_interval;
1290 
1291 	local->dynamic_ps_forced_timeout = -1;
1292 
1293 	if (!local->hw.max_nan_de_entries)
1294 		local->hw.max_nan_de_entries = IEEE80211_MAX_NAN_INSTANCE_ID;
1295 
1296 	if (!local->hw.weight_multiplier)
1297 		local->hw.weight_multiplier = 1;
1298 
1299 	result = ieee80211_wep_init(local);
1300 	if (result < 0)
1301 		wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
1302 			    result);
1303 
1304 	local->hw.conf.flags = IEEE80211_CONF_IDLE;
1305 
1306 	ieee80211_led_init(local);
1307 
1308 	result = ieee80211_txq_setup_flows(local);
1309 	if (result)
1310 		goto fail_flows;
1311 
1312 	rtnl_lock();
1313 	result = ieee80211_init_rate_ctrl_alg(local,
1314 					      hw->rate_control_algorithm);
1315 	rtnl_unlock();
1316 	if (result < 0) {
1317 		wiphy_debug(local->hw.wiphy,
1318 			    "Failed to initialize rate control algorithm\n");
1319 		goto fail_rate;
1320 	}
1321 
1322 	if (local->rate_ctrl) {
1323 		clear_bit(IEEE80211_HW_SUPPORTS_VHT_EXT_NSS_BW, hw->flags);
1324 		if (local->rate_ctrl->ops->capa & RATE_CTRL_CAPA_VHT_EXT_NSS_BW)
1325 			ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
1326 	}
1327 
1328 	/*
1329 	 * If the VHT capabilities don't have IEEE80211_VHT_EXT_NSS_BW_CAPABLE,
1330 	 * or have it when we don't, copy the sband structure and set/clear it.
1331 	 * This is necessary because rate scaling algorithms could be switched
1332 	 * and have different support values.
1333 	 * Print a message so that in the common case the reallocation can be
1334 	 * avoided.
1335 	 */
1336 	BUILD_BUG_ON(NUM_NL80211_BANDS > 8 * sizeof(local->sband_allocated));
1337 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1338 		struct ieee80211_supported_band *sband;
1339 		bool local_cap, ie_cap;
1340 
1341 		local_cap = ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW);
1342 
1343 		sband = local->hw.wiphy->bands[band];
1344 		if (!sband || !sband->vht_cap.vht_supported)
1345 			continue;
1346 
1347 		ie_cap = !!(sband->vht_cap.vht_mcs.tx_highest &
1348 			    cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE));
1349 
1350 		if (local_cap == ie_cap)
1351 			continue;
1352 
1353 		sband = kmemdup(sband, sizeof(*sband), GFP_KERNEL);
1354 		if (!sband) {
1355 			result = -ENOMEM;
1356 			goto fail_rate;
1357 		}
1358 
1359 		wiphy_dbg(hw->wiphy, "copying sband (band %d) due to VHT EXT NSS BW flag\n",
1360 			  band);
1361 
1362 		sband->vht_cap.vht_mcs.tx_highest ^=
1363 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
1364 
1365 		local->hw.wiphy->bands[band] = sband;
1366 		local->sband_allocated |= BIT(band);
1367 	}
1368 
1369 #ifdef CONFIG_DRIVERS_HDF_XR829
1370 	local->hw.wiphy->regulatory_flags |= REGULATORY_IGNORE_STALE_KICKOFF | REGULATORY_CUSTOM_REG;
1371 	wiphy_apply_custom_regulatory(local->hw.wiphy, &xr829_regdom);
1372 #endif
1373 
1374 	result = wiphy_register(local->hw.wiphy);
1375 	if (result < 0)
1376 		goto fail_wiphy_register;
1377 
1378 	debugfs_hw_add(local);
1379 	rate_control_add_debugfs(local);
1380 
1381 	rtnl_lock();
1382 
1383 	/* add one default STA interface if supported */
1384 	if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION) &&
1385 	    !ieee80211_hw_check(hw, NO_AUTO_VIF)) {
1386 		struct vif_params params = {0};
1387 
1388 		result = ieee80211_if_add(local, "wlan%d", NET_NAME_ENUM, NULL,
1389 					  NL80211_IFTYPE_STATION, &params);
1390 		if (result)
1391 			wiphy_warn(local->hw.wiphy,
1392 				   "Failed to add default virtual iface\n");
1393 
1394 		/*
1395 		result = ieee80211_if_add(local, "p2p%d", NET_NAME_ENUM, NULL,
1396 							  NL80211_IFTYPE_P2P_DEVICE, NULL);
1397 
1398 		if (result)
1399 			wiphy_warn(local->hw.wiphy,
1400 				   "Failed to add default virtual p2p iface\n");*/
1401 	}
1402 
1403 	rtnl_unlock();
1404 
1405 #ifdef CONFIG_INET
1406 	local->ifa_notifier.notifier_call = ieee80211_ifa_changed;
1407 	result = register_inetaddr_notifier(&local->ifa_notifier);
1408 	if (result)
1409 		goto fail_ifa;
1410 #endif
1411 
1412 #if IPV6_FILTERING//IS_ENABLED(CONFIG_IPV6)
1413 	local->ifa6_notifier.notifier_call = ieee80211_ifa6_changed;
1414 	result = register_inet6addr_notifier(&local->ifa6_notifier);
1415 	if (result)
1416 		goto fail_ifa6;
1417 #endif/*IPV6_FILTERING*/
1418 
1419 	return 0;
1420 
1421 #if IPV6_FILTERING//IS_ENABLED(CONFIG_IPV6)
1422  fail_ifa6:
1423 #ifdef CONFIG_INET
1424 	unregister_inetaddr_notifier(&local->ifa_notifier);
1425 #endif/*IPV6_FILTERING*/
1426 #endif
1427 #if defined(CONFIG_INET) || defined(CONFIG_IPV6)
1428  fail_ifa:
1429 #endif
1430 	wiphy_unregister(local->hw.wiphy);
1431  fail_wiphy_register:
1432 	rtnl_lock();
1433 	rate_control_deinitialize(local);
1434 	ieee80211_remove_interfaces(local);
1435 	rtnl_unlock();
1436  fail_rate:
1437  fail_flows:
1438 	ieee80211_led_exit(local);
1439 	destroy_workqueue(local->workqueue);
1440  fail_workqueue:
1441 	if (local->wiphy_ciphers_allocated)
1442 		kfree(local->hw.wiphy->cipher_suites);
1443 	kfree(local->int_scan_req);
1444 	return result;
1445 }
1446 
mac80211_unregister_hw(struct ieee80211_hw * hw)1447 void mac80211_unregister_hw(struct ieee80211_hw *hw)
1448 {
1449 	struct ieee80211_local *local = hw_to_local(hw);
1450 
1451 	tasklet_kill(&local->tx_pending_tasklet);
1452 	tasklet_kill(&local->tasklet);
1453 
1454 #ifdef CONFIG_INET
1455 	unregister_inetaddr_notifier(&local->ifa_notifier);
1456 #endif
1457 #if IPV6_FILTERING//IS_ENABLED(CONFIG_IPV6)
1458 	unregister_inet6addr_notifier(&local->ifa6_notifier);
1459 #endif/*IPV6_FILTERING*/
1460 
1461 	rtnl_lock();
1462 
1463 	/*
1464 	 * At this point, interface list manipulations are fine
1465 	 * because the driver cannot be handing us frames any
1466 	 * more and the tasklet is killed.
1467 	 */
1468 	ieee80211_remove_interfaces(local);
1469 
1470 	rtnl_unlock();
1471 
1472 	cancel_delayed_work_sync(&local->roc_work);
1473 	cancel_work_sync(&local->restart_work);
1474 #ifdef IPV6_FILTERING
1475 #ifdef CONFIG_INET
1476 	cancel_work_sync(&local->ifa6_changed_work);
1477 #endif
1478 #endif /*IPV6_FILTERING*/
1479 	cancel_work_sync(&local->reconfig_filter);
1480 	cancel_work_sync(&local->tdls_chsw_work);
1481 	flush_work(&local->sched_scan_stopped_work);
1482 	flush_work(&local->radar_detected_work);
1483 
1484 	ieee80211_clear_tx_pending(local);
1485 	rate_control_deinitialize(local);
1486 
1487 	if (skb_queue_len(&local->skb_queue) ||
1488 	    skb_queue_len(&local->skb_queue_unreliable))
1489 		wiphy_warn(local->hw.wiphy, "skb_queue not empty\n");
1490 	skb_queue_purge(&local->skb_queue);
1491 	skb_queue_purge(&local->skb_queue_unreliable);
1492 	skb_queue_purge(&local->skb_queue_tdls_chsw);
1493 
1494 	wiphy_unregister(local->hw.wiphy);
1495 	destroy_workqueue(local->workqueue);
1496 	ieee80211_led_exit(local);
1497 	kfree(local->int_scan_req);
1498 }
1499 
ieee80211_free_ack_frame(int id,void * p,void * data)1500 static int ieee80211_free_ack_frame(int id, void *p, void *data)
1501 {
1502 	WARN_ONCE(1, "Have pending ack frames!\n");
1503 	kfree_skb(p);
1504 	return 0;
1505 }
1506 
mac80211_free_hw(struct ieee80211_hw * hw)1507 void mac80211_free_hw(struct ieee80211_hw *hw)
1508 {
1509 	struct ieee80211_local *local = hw_to_local(hw);
1510 	enum nl80211_band band;
1511 
1512 	mutex_destroy(&local->iflist_mtx);
1513 	mutex_destroy(&local->mtx);
1514 
1515 #ifdef CONFIG_DRIVERS_HDF_XR829
1516 	hdf_wiphy_unregister();
1517 #endif
1518 
1519 	if (local->wiphy_ciphers_allocated)
1520 		kfree(local->hw.wiphy->cipher_suites);
1521 
1522 	idr_for_each(&local->ack_status_frames,
1523 		     ieee80211_free_ack_frame, NULL);
1524 	idr_destroy(&local->ack_status_frames);
1525 
1526 	sta_info_stop(local);
1527 
1528 	ieee80211_free_led_names(local);
1529 
1530 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1531 		if (!(local->sband_allocated & BIT(band)))
1532 			continue;
1533 		kfree(local->hw.wiphy->bands[band]);
1534 	}
1535 
1536 	wiphy_free(local->hw.wiphy);
1537 }
1538 
1539 #ifdef CONFIG_DRIVERS_HDF_XR829
wrap_get_wiphy(void)1540 struct wiphy* wrap_get_wiphy(void)
1541 {
1542 	return g_wiphy;
1543 }
1544 #endif
1545 
1546 #ifndef CONFIG_DRIVERS_HDF_XR829
ieee80211_init(void)1547 int __init ieee80211_init(void)
1548 #else
1549 int ieee80211_init(void)
1550 #endif
1551 {
1552 	struct sk_buff *skb;
1553 	int ret;
1554 
1555 	BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
1556 	BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
1557 		     IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
1558 
1559 	ret = rc80211_minstrel_init();
1560 	if (ret)
1561 		return ret;
1562 
1563 	ret = ieee80211_iface_init();
1564 	if (ret)
1565 		goto err_netdev;
1566 
1567 	return 0;
1568  err_netdev:
1569 	rc80211_minstrel_exit();
1570 
1571 	return ret;
1572 }
1573 
ieee80211_exit(void)1574 void ieee80211_exit(void)
1575 {
1576 	rc80211_minstrel_exit();
1577 
1578 	ieee80211s_stop();
1579 
1580 	ieee80211_iface_exit();
1581 
1582 	rcu_barrier();
1583 }
1584 
1585 
1586 //subsys_initcall(ieee80211_init);
1587 //module_exit(ieee80211_exit);
1588 
1589 #ifdef CONFIG_DRIVERS_HDF_XR829
1590 EXPORT_SYMBOL(xrmac_config_ops);
1591 EXPORT_SYMBOL(wrap_get_wiphy);
1592 #else
1593 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1594 MODULE_LICENSE("GPL");
1595 #endif
1596