cfg.c revision f9e10ce4cf86945eb5efcab31284c971877ed012
1/*
2 * mac80211 configuration hooks for cfg80211
3 *
4 * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
5 *
6 * This file is GPLv2 as found in COPYING.
7 */
8
9#include <linux/ieee80211.h>
10#include <linux/nl80211.h>
11#include <linux/rtnetlink.h>
12#include <linux/slab.h>
13#include <net/net_namespace.h>
14#include <linux/rcupdate.h>
15#include <net/cfg80211.h>
16#include "ieee80211_i.h"
17#include "driver-ops.h"
18#include "cfg.h"
19#include "rate.h"
20#include "mesh.h"
21
22static struct net_device *ieee80211_add_iface(struct wiphy *wiphy, char *name,
23					      enum nl80211_iftype type,
24					      u32 *flags,
25					      struct vif_params *params)
26{
27	struct ieee80211_local *local = wiphy_priv(wiphy);
28	struct net_device *dev;
29	struct ieee80211_sub_if_data *sdata;
30	int err;
31
32	err = ieee80211_if_add(local, name, &dev, type, params);
33	if (err)
34		return ERR_PTR(err);
35
36	if (type == NL80211_IFTYPE_MONITOR && flags) {
37		sdata = IEEE80211_DEV_TO_SUB_IF(dev);
38		sdata->u.mntr_flags = *flags;
39	}
40
41	return dev;
42}
43
44static int ieee80211_del_iface(struct wiphy *wiphy, struct net_device *dev)
45{
46	ieee80211_if_remove(IEEE80211_DEV_TO_SUB_IF(dev));
47
48	return 0;
49}
50
51static int ieee80211_change_iface(struct wiphy *wiphy,
52				  struct net_device *dev,
53				  enum nl80211_iftype type, u32 *flags,
54				  struct vif_params *params)
55{
56	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
57	int ret;
58
59	ret = ieee80211_if_change_type(sdata, type);
60	if (ret)
61		return ret;
62
63	if (ieee80211_vif_is_mesh(&sdata->vif) && params->mesh_id_len)
64		ieee80211_sdata_set_mesh_id(sdata,
65					    params->mesh_id_len,
66					    params->mesh_id);
67
68	if (type == NL80211_IFTYPE_AP_VLAN &&
69	    params && params->use_4addr == 0)
70		rcu_assign_pointer(sdata->u.vlan.sta, NULL);
71	else if (type == NL80211_IFTYPE_STATION &&
72		 params && params->use_4addr >= 0)
73		sdata->u.mgd.use_4addr = params->use_4addr;
74
75	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
76		struct ieee80211_local *local = sdata->local;
77
78		if (ieee80211_sdata_running(sdata)) {
79			/*
80			 * Prohibit MONITOR_FLAG_COOK_FRAMES to be
81			 * changed while the interface is up.
82			 * Else we would need to add a lot of cruft
83			 * to update everything:
84			 *	cooked_mntrs, monitor and all fif_* counters
85			 *	reconfigure hardware
86			 */
87			if ((*flags & MONITOR_FLAG_COOK_FRAMES) !=
88			    (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
89				return -EBUSY;
90
91			ieee80211_adjust_monitor_flags(sdata, -1);
92			sdata->u.mntr_flags = *flags;
93			ieee80211_adjust_monitor_flags(sdata, 1);
94
95			ieee80211_configure_filter(local);
96		} else {
97			/*
98			 * Because the interface is down, ieee80211_do_stop
99			 * and ieee80211_do_open take care of "everything"
100			 * mentioned in the comment above.
101			 */
102			sdata->u.mntr_flags = *flags;
103		}
104	}
105
106	return 0;
107}
108
109static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
110			     u8 key_idx, bool pairwise, const u8 *mac_addr,
111			     struct key_params *params)
112{
113	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
114	struct sta_info *sta = NULL;
115	struct ieee80211_key *key;
116	int err;
117
118	if (!ieee80211_sdata_running(sdata))
119		return -ENETDOWN;
120
121	/* reject WEP and TKIP keys if WEP failed to initialize */
122	switch (params->cipher) {
123	case WLAN_CIPHER_SUITE_WEP40:
124	case WLAN_CIPHER_SUITE_TKIP:
125	case WLAN_CIPHER_SUITE_WEP104:
126		if (IS_ERR(sdata->local->wep_tx_tfm))
127			return -EINVAL;
128		break;
129	default:
130		break;
131	}
132
133	key = ieee80211_key_alloc(params->cipher, key_idx, params->key_len,
134				  params->key, params->seq_len, params->seq);
135	if (IS_ERR(key))
136		return PTR_ERR(key);
137
138	if (pairwise)
139		key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
140
141	mutex_lock(&sdata->local->sta_mtx);
142
143	if (mac_addr) {
144		sta = sta_info_get_bss(sdata, mac_addr);
145		if (!sta) {
146			ieee80211_key_free(sdata->local, key);
147			err = -ENOENT;
148			goto out_unlock;
149		}
150	}
151
152	err = ieee80211_key_link(key, sdata, sta);
153	if (err)
154		ieee80211_key_free(sdata->local, key);
155
156 out_unlock:
157	mutex_unlock(&sdata->local->sta_mtx);
158
159	return err;
160}
161
162static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
163			     u8 key_idx, bool pairwise, const u8 *mac_addr)
164{
165	struct ieee80211_sub_if_data *sdata;
166	struct sta_info *sta;
167	int ret;
168
169	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
170
171	mutex_lock(&sdata->local->sta_mtx);
172
173	if (mac_addr) {
174		ret = -ENOENT;
175
176		sta = sta_info_get_bss(sdata, mac_addr);
177		if (!sta)
178			goto out_unlock;
179
180		if (pairwise) {
181			if (sta->ptk) {
182				ieee80211_key_free(sdata->local, sta->ptk);
183				ret = 0;
184			}
185		} else {
186			if (sta->gtk[key_idx]) {
187				ieee80211_key_free(sdata->local,
188						   sta->gtk[key_idx]);
189				ret = 0;
190			}
191		}
192
193		goto out_unlock;
194	}
195
196	if (!sdata->keys[key_idx]) {
197		ret = -ENOENT;
198		goto out_unlock;
199	}
200
201	ieee80211_key_free(sdata->local, sdata->keys[key_idx]);
202	WARN_ON(sdata->keys[key_idx]);
203
204	ret = 0;
205 out_unlock:
206	mutex_unlock(&sdata->local->sta_mtx);
207
208	return ret;
209}
210
211static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
212			     u8 key_idx, bool pairwise, const u8 *mac_addr,
213			     void *cookie,
214			     void (*callback)(void *cookie,
215					      struct key_params *params))
216{
217	struct ieee80211_sub_if_data *sdata;
218	struct sta_info *sta = NULL;
219	u8 seq[6] = {0};
220	struct key_params params;
221	struct ieee80211_key *key = NULL;
222	u32 iv32;
223	u16 iv16;
224	int err = -ENOENT;
225
226	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
227
228	rcu_read_lock();
229
230	if (mac_addr) {
231		sta = sta_info_get_bss(sdata, mac_addr);
232		if (!sta)
233			goto out;
234
235		if (pairwise)
236			key = sta->ptk;
237		else if (key_idx < NUM_DEFAULT_KEYS)
238			key = sta->gtk[key_idx];
239	} else
240		key = sdata->keys[key_idx];
241
242	if (!key)
243		goto out;
244
245	memset(&params, 0, sizeof(params));
246
247	params.cipher = key->conf.cipher;
248
249	switch (key->conf.cipher) {
250	case WLAN_CIPHER_SUITE_TKIP:
251		iv32 = key->u.tkip.tx.iv32;
252		iv16 = key->u.tkip.tx.iv16;
253
254		if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
255			drv_get_tkip_seq(sdata->local,
256					 key->conf.hw_key_idx,
257					 &iv32, &iv16);
258
259		seq[0] = iv16 & 0xff;
260		seq[1] = (iv16 >> 8) & 0xff;
261		seq[2] = iv32 & 0xff;
262		seq[3] = (iv32 >> 8) & 0xff;
263		seq[4] = (iv32 >> 16) & 0xff;
264		seq[5] = (iv32 >> 24) & 0xff;
265		params.seq = seq;
266		params.seq_len = 6;
267		break;
268	case WLAN_CIPHER_SUITE_CCMP:
269		seq[0] = key->u.ccmp.tx_pn[5];
270		seq[1] = key->u.ccmp.tx_pn[4];
271		seq[2] = key->u.ccmp.tx_pn[3];
272		seq[3] = key->u.ccmp.tx_pn[2];
273		seq[4] = key->u.ccmp.tx_pn[1];
274		seq[5] = key->u.ccmp.tx_pn[0];
275		params.seq = seq;
276		params.seq_len = 6;
277		break;
278	case WLAN_CIPHER_SUITE_AES_CMAC:
279		seq[0] = key->u.aes_cmac.tx_pn[5];
280		seq[1] = key->u.aes_cmac.tx_pn[4];
281		seq[2] = key->u.aes_cmac.tx_pn[3];
282		seq[3] = key->u.aes_cmac.tx_pn[2];
283		seq[4] = key->u.aes_cmac.tx_pn[1];
284		seq[5] = key->u.aes_cmac.tx_pn[0];
285		params.seq = seq;
286		params.seq_len = 6;
287		break;
288	}
289
290	params.key = key->conf.key;
291	params.key_len = key->conf.keylen;
292
293	callback(cookie, &params);
294	err = 0;
295
296 out:
297	rcu_read_unlock();
298	return err;
299}
300
301static int ieee80211_config_default_key(struct wiphy *wiphy,
302					struct net_device *dev,
303					u8 key_idx)
304{
305	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
306
307	ieee80211_set_default_key(sdata, key_idx);
308
309	return 0;
310}
311
312static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
313					     struct net_device *dev,
314					     u8 key_idx)
315{
316	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
317
318	ieee80211_set_default_mgmt_key(sdata, key_idx);
319
320	return 0;
321}
322
323static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
324{
325	struct ieee80211_sub_if_data *sdata = sta->sdata;
326
327	sinfo->generation = sdata->local->sta_generation;
328
329	sinfo->filled = STATION_INFO_INACTIVE_TIME |
330			STATION_INFO_RX_BYTES |
331			STATION_INFO_TX_BYTES |
332			STATION_INFO_RX_PACKETS |
333			STATION_INFO_TX_PACKETS |
334			STATION_INFO_TX_RETRIES |
335			STATION_INFO_TX_FAILED |
336			STATION_INFO_TX_BITRATE |
337			STATION_INFO_RX_DROP_MISC;
338
339	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
340	sinfo->rx_bytes = sta->rx_bytes;
341	sinfo->tx_bytes = sta->tx_bytes;
342	sinfo->rx_packets = sta->rx_packets;
343	sinfo->tx_packets = sta->tx_packets;
344	sinfo->tx_retries = sta->tx_retry_count;
345	sinfo->tx_failed = sta->tx_retry_failed;
346	sinfo->rx_dropped_misc = sta->rx_dropped;
347
348	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
349	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
350		sinfo->filled |= STATION_INFO_SIGNAL;
351		sinfo->signal = (s8)sta->last_signal;
352	}
353
354	sinfo->txrate.flags = 0;
355	if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
356		sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
357	if (sta->last_tx_rate.flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
358		sinfo->txrate.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
359	if (sta->last_tx_rate.flags & IEEE80211_TX_RC_SHORT_GI)
360		sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
361
362	if (!(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)) {
363		struct ieee80211_supported_band *sband;
364		sband = sta->local->hw.wiphy->bands[
365				sta->local->hw.conf.channel->band];
366		sinfo->txrate.legacy =
367			sband->bitrates[sta->last_tx_rate.idx].bitrate;
368	} else
369		sinfo->txrate.mcs = sta->last_tx_rate.idx;
370
371	if (ieee80211_vif_is_mesh(&sdata->vif)) {
372#ifdef CONFIG_MAC80211_MESH
373		sinfo->filled |= STATION_INFO_LLID |
374				 STATION_INFO_PLID |
375				 STATION_INFO_PLINK_STATE;
376
377		sinfo->llid = le16_to_cpu(sta->llid);
378		sinfo->plid = le16_to_cpu(sta->plid);
379		sinfo->plink_state = sta->plink_state;
380#endif
381	}
382}
383
384
385static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
386				 int idx, u8 *mac, struct station_info *sinfo)
387{
388	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
389	struct sta_info *sta;
390	int ret = -ENOENT;
391
392	rcu_read_lock();
393
394	sta = sta_info_get_by_idx(sdata, idx);
395	if (sta) {
396		ret = 0;
397		memcpy(mac, sta->sta.addr, ETH_ALEN);
398		sta_set_sinfo(sta, sinfo);
399	}
400
401	rcu_read_unlock();
402
403	return ret;
404}
405
406static int ieee80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
407				 int idx, struct survey_info *survey)
408{
409	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
410
411	return drv_get_survey(local, idx, survey);
412}
413
414static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
415				 u8 *mac, struct station_info *sinfo)
416{
417	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
418	struct sta_info *sta;
419	int ret = -ENOENT;
420
421	rcu_read_lock();
422
423	sta = sta_info_get_bss(sdata, mac);
424	if (sta) {
425		ret = 0;
426		sta_set_sinfo(sta, sinfo);
427	}
428
429	rcu_read_unlock();
430
431	return ret;
432}
433
434/*
435 * This handles both adding a beacon and setting new beacon info
436 */
437static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata,
438				   struct beacon_parameters *params)
439{
440	struct beacon_data *new, *old;
441	int new_head_len, new_tail_len;
442	int size;
443	int err = -EINVAL;
444
445	old = sdata->u.ap.beacon;
446
447	/* head must not be zero-length */
448	if (params->head && !params->head_len)
449		return -EINVAL;
450
451	/*
452	 * This is a kludge. beacon interval should really be part
453	 * of the beacon information.
454	 */
455	if (params->interval &&
456	    (sdata->vif.bss_conf.beacon_int != params->interval)) {
457		sdata->vif.bss_conf.beacon_int = params->interval;
458		ieee80211_bss_info_change_notify(sdata,
459						 BSS_CHANGED_BEACON_INT);
460	}
461
462	/* Need to have a beacon head if we don't have one yet */
463	if (!params->head && !old)
464		return err;
465
466	/* sorry, no way to start beaconing without dtim period */
467	if (!params->dtim_period && !old)
468		return err;
469
470	/* new or old head? */
471	if (params->head)
472		new_head_len = params->head_len;
473	else
474		new_head_len = old->head_len;
475
476	/* new or old tail? */
477	if (params->tail || !old)
478		/* params->tail_len will be zero for !params->tail */
479		new_tail_len = params->tail_len;
480	else
481		new_tail_len = old->tail_len;
482
483	size = sizeof(*new) + new_head_len + new_tail_len;
484
485	new = kzalloc(size, GFP_KERNEL);
486	if (!new)
487		return -ENOMEM;
488
489	/* start filling the new info now */
490
491	/* new or old dtim period? */
492	if (params->dtim_period)
493		new->dtim_period = params->dtim_period;
494	else
495		new->dtim_period = old->dtim_period;
496
497	/*
498	 * pointers go into the block we allocated,
499	 * memory is | beacon_data | head | tail |
500	 */
501	new->head = ((u8 *) new) + sizeof(*new);
502	new->tail = new->head + new_head_len;
503	new->head_len = new_head_len;
504	new->tail_len = new_tail_len;
505
506	/* copy in head */
507	if (params->head)
508		memcpy(new->head, params->head, new_head_len);
509	else
510		memcpy(new->head, old->head, new_head_len);
511
512	/* copy in optional tail */
513	if (params->tail)
514		memcpy(new->tail, params->tail, new_tail_len);
515	else
516		if (old)
517			memcpy(new->tail, old->tail, new_tail_len);
518
519	sdata->vif.bss_conf.dtim_period = new->dtim_period;
520
521	rcu_assign_pointer(sdata->u.ap.beacon, new);
522
523	synchronize_rcu();
524
525	kfree(old);
526
527	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
528						BSS_CHANGED_BEACON);
529	return 0;
530}
531
532static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev,
533				struct beacon_parameters *params)
534{
535	struct ieee80211_sub_if_data *sdata;
536	struct beacon_data *old;
537
538	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
539
540	old = sdata->u.ap.beacon;
541
542	if (old)
543		return -EALREADY;
544
545	return ieee80211_config_beacon(sdata, params);
546}
547
548static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev,
549				struct beacon_parameters *params)
550{
551	struct ieee80211_sub_if_data *sdata;
552	struct beacon_data *old;
553
554	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
555
556	old = sdata->u.ap.beacon;
557
558	if (!old)
559		return -ENOENT;
560
561	return ieee80211_config_beacon(sdata, params);
562}
563
564static int ieee80211_del_beacon(struct wiphy *wiphy, struct net_device *dev)
565{
566	struct ieee80211_sub_if_data *sdata;
567	struct beacon_data *old;
568
569	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
570
571	old = sdata->u.ap.beacon;
572
573	if (!old)
574		return -ENOENT;
575
576	rcu_assign_pointer(sdata->u.ap.beacon, NULL);
577	synchronize_rcu();
578	kfree(old);
579
580	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
581	return 0;
582}
583
584/* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
585struct iapp_layer2_update {
586	u8 da[ETH_ALEN];	/* broadcast */
587	u8 sa[ETH_ALEN];	/* STA addr */
588	__be16 len;		/* 6 */
589	u8 dsap;		/* 0 */
590	u8 ssap;		/* 0 */
591	u8 control;
592	u8 xid_info[3];
593} __packed;
594
595static void ieee80211_send_layer2_update(struct sta_info *sta)
596{
597	struct iapp_layer2_update *msg;
598	struct sk_buff *skb;
599
600	/* Send Level 2 Update Frame to update forwarding tables in layer 2
601	 * bridge devices */
602
603	skb = dev_alloc_skb(sizeof(*msg));
604	if (!skb)
605		return;
606	msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
607
608	/* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
609	 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
610
611	memset(msg->da, 0xff, ETH_ALEN);
612	memcpy(msg->sa, sta->sta.addr, ETH_ALEN);
613	msg->len = htons(6);
614	msg->dsap = 0;
615	msg->ssap = 0x01;	/* NULL LSAP, CR Bit: Response */
616	msg->control = 0xaf;	/* XID response lsb.1111F101.
617				 * F=0 (no poll command; unsolicited frame) */
618	msg->xid_info[0] = 0x81;	/* XID format identifier */
619	msg->xid_info[1] = 1;	/* LLC types/classes: Type 1 LLC */
620	msg->xid_info[2] = 0;	/* XID sender's receive window size (RW) */
621
622	skb->dev = sta->sdata->dev;
623	skb->protocol = eth_type_trans(skb, sta->sdata->dev);
624	memset(skb->cb, 0, sizeof(skb->cb));
625	netif_rx_ni(skb);
626}
627
628static void sta_apply_parameters(struct ieee80211_local *local,
629				 struct sta_info *sta,
630				 struct station_parameters *params)
631{
632	unsigned long flags;
633	u32 rates;
634	int i, j;
635	struct ieee80211_supported_band *sband;
636	struct ieee80211_sub_if_data *sdata = sta->sdata;
637	u32 mask, set;
638
639	sband = local->hw.wiphy->bands[local->oper_channel->band];
640
641	spin_lock_irqsave(&sta->flaglock, flags);
642	mask = params->sta_flags_mask;
643	set = params->sta_flags_set;
644
645	if (mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
646		sta->flags &= ~WLAN_STA_AUTHORIZED;
647		if (set & BIT(NL80211_STA_FLAG_AUTHORIZED))
648			sta->flags |= WLAN_STA_AUTHORIZED;
649	}
650
651	if (mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
652		sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
653		if (set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
654			sta->flags |= WLAN_STA_SHORT_PREAMBLE;
655	}
656
657	if (mask & BIT(NL80211_STA_FLAG_WME)) {
658		sta->flags &= ~WLAN_STA_WME;
659		if (set & BIT(NL80211_STA_FLAG_WME))
660			sta->flags |= WLAN_STA_WME;
661	}
662
663	if (mask & BIT(NL80211_STA_FLAG_MFP)) {
664		sta->flags &= ~WLAN_STA_MFP;
665		if (set & BIT(NL80211_STA_FLAG_MFP))
666			sta->flags |= WLAN_STA_MFP;
667	}
668	spin_unlock_irqrestore(&sta->flaglock, flags);
669
670	/*
671	 * cfg80211 validates this (1-2007) and allows setting the AID
672	 * only when creating a new station entry
673	 */
674	if (params->aid)
675		sta->sta.aid = params->aid;
676
677	/*
678	 * FIXME: updating the following information is racy when this
679	 *	  function is called from ieee80211_change_station().
680	 *	  However, all this information should be static so
681	 *	  maybe we should just reject attemps to change it.
682	 */
683
684	if (params->listen_interval >= 0)
685		sta->listen_interval = params->listen_interval;
686
687	if (params->supported_rates) {
688		rates = 0;
689
690		for (i = 0; i < params->supported_rates_len; i++) {
691			int rate = (params->supported_rates[i] & 0x7f) * 5;
692			for (j = 0; j < sband->n_bitrates; j++) {
693				if (sband->bitrates[j].bitrate == rate)
694					rates |= BIT(j);
695			}
696		}
697		sta->sta.supp_rates[local->oper_channel->band] = rates;
698	}
699
700	if (params->ht_capa)
701		ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
702						  params->ht_capa,
703						  &sta->sta.ht_cap);
704
705	if (ieee80211_vif_is_mesh(&sdata->vif) && params->plink_action) {
706		switch (params->plink_action) {
707		case PLINK_ACTION_OPEN:
708			mesh_plink_open(sta);
709			break;
710		case PLINK_ACTION_BLOCK:
711			mesh_plink_block(sta);
712			break;
713		}
714	}
715}
716
717static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
718				 u8 *mac, struct station_parameters *params)
719{
720	struct ieee80211_local *local = wiphy_priv(wiphy);
721	struct sta_info *sta;
722	struct ieee80211_sub_if_data *sdata;
723	int err;
724	int layer2_update;
725
726	if (params->vlan) {
727		sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
728
729		if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
730		    sdata->vif.type != NL80211_IFTYPE_AP)
731			return -EINVAL;
732	} else
733		sdata = IEEE80211_DEV_TO_SUB_IF(dev);
734
735	if (compare_ether_addr(mac, sdata->vif.addr) == 0)
736		return -EINVAL;
737
738	if (is_multicast_ether_addr(mac))
739		return -EINVAL;
740
741	sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
742	if (!sta)
743		return -ENOMEM;
744
745	sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
746
747	sta_apply_parameters(local, sta, params);
748
749	rate_control_rate_init(sta);
750
751	layer2_update = sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
752		sdata->vif.type == NL80211_IFTYPE_AP;
753
754	err = sta_info_insert_rcu(sta);
755	if (err) {
756		rcu_read_unlock();
757		return err;
758	}
759
760	if (layer2_update)
761		ieee80211_send_layer2_update(sta);
762
763	rcu_read_unlock();
764
765	return 0;
766}
767
768static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
769				 u8 *mac)
770{
771	struct ieee80211_local *local = wiphy_priv(wiphy);
772	struct ieee80211_sub_if_data *sdata;
773
774	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
775
776	if (mac)
777		return sta_info_destroy_addr_bss(sdata, mac);
778
779	sta_info_flush(local, sdata);
780	return 0;
781}
782
783static int ieee80211_change_station(struct wiphy *wiphy,
784				    struct net_device *dev,
785				    u8 *mac,
786				    struct station_parameters *params)
787{
788	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
789	struct ieee80211_local *local = wiphy_priv(wiphy);
790	struct sta_info *sta;
791	struct ieee80211_sub_if_data *vlansdata;
792
793	rcu_read_lock();
794
795	sta = sta_info_get_bss(sdata, mac);
796	if (!sta) {
797		rcu_read_unlock();
798		return -ENOENT;
799	}
800
801	if (params->vlan && params->vlan != sta->sdata->dev) {
802		vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
803
804		if (vlansdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
805		    vlansdata->vif.type != NL80211_IFTYPE_AP) {
806			rcu_read_unlock();
807			return -EINVAL;
808		}
809
810		if (params->vlan->ieee80211_ptr->use_4addr) {
811			if (vlansdata->u.vlan.sta) {
812				rcu_read_unlock();
813				return -EBUSY;
814			}
815
816			rcu_assign_pointer(vlansdata->u.vlan.sta, sta);
817		}
818
819		sta->sdata = vlansdata;
820		ieee80211_send_layer2_update(sta);
821	}
822
823	sta_apply_parameters(local, sta, params);
824
825	rcu_read_unlock();
826
827	return 0;
828}
829
830#ifdef CONFIG_MAC80211_MESH
831static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
832				 u8 *dst, u8 *next_hop)
833{
834	struct ieee80211_sub_if_data *sdata;
835	struct mesh_path *mpath;
836	struct sta_info *sta;
837	int err;
838
839	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
840
841	rcu_read_lock();
842	sta = sta_info_get(sdata, next_hop);
843	if (!sta) {
844		rcu_read_unlock();
845		return -ENOENT;
846	}
847
848	err = mesh_path_add(dst, sdata);
849	if (err) {
850		rcu_read_unlock();
851		return err;
852	}
853
854	mpath = mesh_path_lookup(dst, sdata);
855	if (!mpath) {
856		rcu_read_unlock();
857		return -ENXIO;
858	}
859	mesh_path_fix_nexthop(mpath, sta);
860
861	rcu_read_unlock();
862	return 0;
863}
864
865static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
866				 u8 *dst)
867{
868	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
869
870	if (dst)
871		return mesh_path_del(dst, sdata);
872
873	mesh_path_flush(sdata);
874	return 0;
875}
876
877static int ieee80211_change_mpath(struct wiphy *wiphy,
878				    struct net_device *dev,
879				    u8 *dst, u8 *next_hop)
880{
881	struct ieee80211_sub_if_data *sdata;
882	struct mesh_path *mpath;
883	struct sta_info *sta;
884
885	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
886
887	rcu_read_lock();
888
889	sta = sta_info_get(sdata, next_hop);
890	if (!sta) {
891		rcu_read_unlock();
892		return -ENOENT;
893	}
894
895	mpath = mesh_path_lookup(dst, sdata);
896	if (!mpath) {
897		rcu_read_unlock();
898		return -ENOENT;
899	}
900
901	mesh_path_fix_nexthop(mpath, sta);
902
903	rcu_read_unlock();
904	return 0;
905}
906
907static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
908			    struct mpath_info *pinfo)
909{
910	if (mpath->next_hop)
911		memcpy(next_hop, mpath->next_hop->sta.addr, ETH_ALEN);
912	else
913		memset(next_hop, 0, ETH_ALEN);
914
915	pinfo->generation = mesh_paths_generation;
916
917	pinfo->filled = MPATH_INFO_FRAME_QLEN |
918			MPATH_INFO_SN |
919			MPATH_INFO_METRIC |
920			MPATH_INFO_EXPTIME |
921			MPATH_INFO_DISCOVERY_TIMEOUT |
922			MPATH_INFO_DISCOVERY_RETRIES |
923			MPATH_INFO_FLAGS;
924
925	pinfo->frame_qlen = mpath->frame_queue.qlen;
926	pinfo->sn = mpath->sn;
927	pinfo->metric = mpath->metric;
928	if (time_before(jiffies, mpath->exp_time))
929		pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
930	pinfo->discovery_timeout =
931			jiffies_to_msecs(mpath->discovery_timeout);
932	pinfo->discovery_retries = mpath->discovery_retries;
933	pinfo->flags = 0;
934	if (mpath->flags & MESH_PATH_ACTIVE)
935		pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
936	if (mpath->flags & MESH_PATH_RESOLVING)
937		pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
938	if (mpath->flags & MESH_PATH_SN_VALID)
939		pinfo->flags |= NL80211_MPATH_FLAG_SN_VALID;
940	if (mpath->flags & MESH_PATH_FIXED)
941		pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
942	if (mpath->flags & MESH_PATH_RESOLVING)
943		pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
944
945	pinfo->flags = mpath->flags;
946}
947
948static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
949			       u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
950
951{
952	struct ieee80211_sub_if_data *sdata;
953	struct mesh_path *mpath;
954
955	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
956
957	rcu_read_lock();
958	mpath = mesh_path_lookup(dst, sdata);
959	if (!mpath) {
960		rcu_read_unlock();
961		return -ENOENT;
962	}
963	memcpy(dst, mpath->dst, ETH_ALEN);
964	mpath_set_pinfo(mpath, next_hop, pinfo);
965	rcu_read_unlock();
966	return 0;
967}
968
969static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
970				 int idx, u8 *dst, u8 *next_hop,
971				 struct mpath_info *pinfo)
972{
973	struct ieee80211_sub_if_data *sdata;
974	struct mesh_path *mpath;
975
976	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
977
978	rcu_read_lock();
979	mpath = mesh_path_lookup_by_idx(idx, sdata);
980	if (!mpath) {
981		rcu_read_unlock();
982		return -ENOENT;
983	}
984	memcpy(dst, mpath->dst, ETH_ALEN);
985	mpath_set_pinfo(mpath, next_hop, pinfo);
986	rcu_read_unlock();
987	return 0;
988}
989
990static int ieee80211_get_mesh_params(struct wiphy *wiphy,
991				struct net_device *dev,
992				struct mesh_config *conf)
993{
994	struct ieee80211_sub_if_data *sdata;
995	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
996
997	memcpy(conf, &(sdata->u.mesh.mshcfg), sizeof(struct mesh_config));
998	return 0;
999}
1000
1001static inline bool _chg_mesh_attr(enum nl80211_meshconf_params parm, u32 mask)
1002{
1003	return (mask >> (parm-1)) & 0x1;
1004}
1005
1006static int ieee80211_set_mesh_params(struct wiphy *wiphy,
1007				struct net_device *dev,
1008				const struct mesh_config *nconf, u32 mask)
1009{
1010	struct mesh_config *conf;
1011	struct ieee80211_sub_if_data *sdata;
1012	struct ieee80211_if_mesh *ifmsh;
1013
1014	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1015	ifmsh = &sdata->u.mesh;
1016
1017	/* Set the config options which we are interested in setting */
1018	conf = &(sdata->u.mesh.mshcfg);
1019	if (_chg_mesh_attr(NL80211_MESHCONF_RETRY_TIMEOUT, mask))
1020		conf->dot11MeshRetryTimeout = nconf->dot11MeshRetryTimeout;
1021	if (_chg_mesh_attr(NL80211_MESHCONF_CONFIRM_TIMEOUT, mask))
1022		conf->dot11MeshConfirmTimeout = nconf->dot11MeshConfirmTimeout;
1023	if (_chg_mesh_attr(NL80211_MESHCONF_HOLDING_TIMEOUT, mask))
1024		conf->dot11MeshHoldingTimeout = nconf->dot11MeshHoldingTimeout;
1025	if (_chg_mesh_attr(NL80211_MESHCONF_MAX_PEER_LINKS, mask))
1026		conf->dot11MeshMaxPeerLinks = nconf->dot11MeshMaxPeerLinks;
1027	if (_chg_mesh_attr(NL80211_MESHCONF_MAX_RETRIES, mask))
1028		conf->dot11MeshMaxRetries = nconf->dot11MeshMaxRetries;
1029	if (_chg_mesh_attr(NL80211_MESHCONF_TTL, mask))
1030		conf->dot11MeshTTL = nconf->dot11MeshTTL;
1031	if (_chg_mesh_attr(NL80211_MESHCONF_ELEMENT_TTL, mask))
1032		conf->dot11MeshTTL = nconf->element_ttl;
1033	if (_chg_mesh_attr(NL80211_MESHCONF_AUTO_OPEN_PLINKS, mask))
1034		conf->auto_open_plinks = nconf->auto_open_plinks;
1035	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, mask))
1036		conf->dot11MeshHWMPmaxPREQretries =
1037			nconf->dot11MeshHWMPmaxPREQretries;
1038	if (_chg_mesh_attr(NL80211_MESHCONF_PATH_REFRESH_TIME, mask))
1039		conf->path_refresh_time = nconf->path_refresh_time;
1040	if (_chg_mesh_attr(NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, mask))
1041		conf->min_discovery_timeout = nconf->min_discovery_timeout;
1042	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, mask))
1043		conf->dot11MeshHWMPactivePathTimeout =
1044			nconf->dot11MeshHWMPactivePathTimeout;
1045	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, mask))
1046		conf->dot11MeshHWMPpreqMinInterval =
1047			nconf->dot11MeshHWMPpreqMinInterval;
1048	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1049			   mask))
1050		conf->dot11MeshHWMPnetDiameterTraversalTime =
1051			nconf->dot11MeshHWMPnetDiameterTraversalTime;
1052	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOTMODE, mask)) {
1053		conf->dot11MeshHWMPRootMode = nconf->dot11MeshHWMPRootMode;
1054		ieee80211_mesh_root_setup(ifmsh);
1055	}
1056	return 0;
1057}
1058
1059#endif
1060
1061static int ieee80211_change_bss(struct wiphy *wiphy,
1062				struct net_device *dev,
1063				struct bss_parameters *params)
1064{
1065	struct ieee80211_sub_if_data *sdata;
1066	u32 changed = 0;
1067
1068	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1069
1070	if (params->use_cts_prot >= 0) {
1071		sdata->vif.bss_conf.use_cts_prot = params->use_cts_prot;
1072		changed |= BSS_CHANGED_ERP_CTS_PROT;
1073	}
1074	if (params->use_short_preamble >= 0) {
1075		sdata->vif.bss_conf.use_short_preamble =
1076			params->use_short_preamble;
1077		changed |= BSS_CHANGED_ERP_PREAMBLE;
1078	}
1079
1080	if (!sdata->vif.bss_conf.use_short_slot &&
1081	    sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ) {
1082		sdata->vif.bss_conf.use_short_slot = true;
1083		changed |= BSS_CHANGED_ERP_SLOT;
1084	}
1085
1086	if (params->use_short_slot_time >= 0) {
1087		sdata->vif.bss_conf.use_short_slot =
1088			params->use_short_slot_time;
1089		changed |= BSS_CHANGED_ERP_SLOT;
1090	}
1091
1092	if (params->basic_rates) {
1093		int i, j;
1094		u32 rates = 0;
1095		struct ieee80211_local *local = wiphy_priv(wiphy);
1096		struct ieee80211_supported_band *sband =
1097			wiphy->bands[local->oper_channel->band];
1098
1099		for (i = 0; i < params->basic_rates_len; i++) {
1100			int rate = (params->basic_rates[i] & 0x7f) * 5;
1101			for (j = 0; j < sband->n_bitrates; j++) {
1102				if (sband->bitrates[j].bitrate == rate)
1103					rates |= BIT(j);
1104			}
1105		}
1106		sdata->vif.bss_conf.basic_rates = rates;
1107		changed |= BSS_CHANGED_BASIC_RATES;
1108	}
1109
1110	if (params->ap_isolate >= 0) {
1111		if (params->ap_isolate)
1112			sdata->flags |= IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
1113		else
1114			sdata->flags &= ~IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
1115	}
1116
1117	ieee80211_bss_info_change_notify(sdata, changed);
1118
1119	return 0;
1120}
1121
1122static int ieee80211_set_txq_params(struct wiphy *wiphy,
1123				    struct ieee80211_txq_params *params)
1124{
1125	struct ieee80211_local *local = wiphy_priv(wiphy);
1126	struct ieee80211_tx_queue_params p;
1127
1128	if (!local->ops->conf_tx)
1129		return -EOPNOTSUPP;
1130
1131	memset(&p, 0, sizeof(p));
1132	p.aifs = params->aifs;
1133	p.cw_max = params->cwmax;
1134	p.cw_min = params->cwmin;
1135	p.txop = params->txop;
1136
1137	/*
1138	 * Setting tx queue params disables u-apsd because it's only
1139	 * called in master mode.
1140	 */
1141	p.uapsd = false;
1142
1143	if (drv_conf_tx(local, params->queue, &p)) {
1144		wiphy_debug(local->hw.wiphy,
1145			    "failed to set TX queue parameters for queue %d\n",
1146			    params->queue);
1147		return -EINVAL;
1148	}
1149
1150	return 0;
1151}
1152
1153static int ieee80211_set_channel(struct wiphy *wiphy,
1154				 struct net_device *netdev,
1155				 struct ieee80211_channel *chan,
1156				 enum nl80211_channel_type channel_type)
1157{
1158	struct ieee80211_local *local = wiphy_priv(wiphy);
1159	struct ieee80211_sub_if_data *sdata = NULL;
1160
1161	if (netdev)
1162		sdata = IEEE80211_DEV_TO_SUB_IF(netdev);
1163
1164	switch (ieee80211_get_channel_mode(local, NULL)) {
1165	case CHAN_MODE_HOPPING:
1166		return -EBUSY;
1167	case CHAN_MODE_FIXED:
1168		if (local->oper_channel != chan)
1169			return -EBUSY;
1170		if (!sdata && local->_oper_channel_type == channel_type)
1171			return 0;
1172		break;
1173	case CHAN_MODE_UNDEFINED:
1174		break;
1175	}
1176
1177	local->oper_channel = chan;
1178
1179	if (!ieee80211_set_channel_type(local, sdata, channel_type))
1180		return -EBUSY;
1181
1182	ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
1183	if (sdata && sdata->vif.type != NL80211_IFTYPE_MONITOR)
1184		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_HT);
1185
1186	return 0;
1187}
1188
1189#ifdef CONFIG_PM
1190static int ieee80211_suspend(struct wiphy *wiphy)
1191{
1192	return __ieee80211_suspend(wiphy_priv(wiphy));
1193}
1194
1195static int ieee80211_resume(struct wiphy *wiphy)
1196{
1197	return __ieee80211_resume(wiphy_priv(wiphy));
1198}
1199#else
1200#define ieee80211_suspend NULL
1201#define ieee80211_resume NULL
1202#endif
1203
1204static int ieee80211_scan(struct wiphy *wiphy,
1205			  struct net_device *dev,
1206			  struct cfg80211_scan_request *req)
1207{
1208	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1209
1210	switch (ieee80211_vif_type_p2p(&sdata->vif)) {
1211	case NL80211_IFTYPE_STATION:
1212	case NL80211_IFTYPE_ADHOC:
1213	case NL80211_IFTYPE_MESH_POINT:
1214	case NL80211_IFTYPE_P2P_CLIENT:
1215		break;
1216	case NL80211_IFTYPE_P2P_GO:
1217		if (sdata->local->ops->hw_scan)
1218			break;
1219		/* FIXME: implement NoA while scanning in software */
1220		return -EOPNOTSUPP;
1221	case NL80211_IFTYPE_AP:
1222		if (sdata->u.ap.beacon)
1223			return -EOPNOTSUPP;
1224		break;
1225	default:
1226		return -EOPNOTSUPP;
1227	}
1228
1229	return ieee80211_request_scan(sdata, req);
1230}
1231
1232static int ieee80211_auth(struct wiphy *wiphy, struct net_device *dev,
1233			  struct cfg80211_auth_request *req)
1234{
1235	return ieee80211_mgd_auth(IEEE80211_DEV_TO_SUB_IF(dev), req);
1236}
1237
1238static int ieee80211_assoc(struct wiphy *wiphy, struct net_device *dev,
1239			   struct cfg80211_assoc_request *req)
1240{
1241	struct ieee80211_local *local = wiphy_priv(wiphy);
1242	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1243
1244	switch (ieee80211_get_channel_mode(local, sdata)) {
1245	case CHAN_MODE_HOPPING:
1246		return -EBUSY;
1247	case CHAN_MODE_FIXED:
1248		if (local->oper_channel == req->bss->channel)
1249			break;
1250		return -EBUSY;
1251	case CHAN_MODE_UNDEFINED:
1252		break;
1253	}
1254
1255	return ieee80211_mgd_assoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
1256}
1257
1258static int ieee80211_deauth(struct wiphy *wiphy, struct net_device *dev,
1259			    struct cfg80211_deauth_request *req,
1260			    void *cookie)
1261{
1262	return ieee80211_mgd_deauth(IEEE80211_DEV_TO_SUB_IF(dev),
1263				    req, cookie);
1264}
1265
1266static int ieee80211_disassoc(struct wiphy *wiphy, struct net_device *dev,
1267			      struct cfg80211_disassoc_request *req,
1268			      void *cookie)
1269{
1270	return ieee80211_mgd_disassoc(IEEE80211_DEV_TO_SUB_IF(dev),
1271				      req, cookie);
1272}
1273
1274static int ieee80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1275			       struct cfg80211_ibss_params *params)
1276{
1277	struct ieee80211_local *local = wiphy_priv(wiphy);
1278	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1279
1280	switch (ieee80211_get_channel_mode(local, sdata)) {
1281	case CHAN_MODE_HOPPING:
1282		return -EBUSY;
1283	case CHAN_MODE_FIXED:
1284		if (!params->channel_fixed)
1285			return -EBUSY;
1286		if (local->oper_channel == params->channel)
1287			break;
1288		return -EBUSY;
1289	case CHAN_MODE_UNDEFINED:
1290		break;
1291	}
1292
1293	return ieee80211_ibss_join(sdata, params);
1294}
1295
1296static int ieee80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1297{
1298	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1299
1300	return ieee80211_ibss_leave(sdata);
1301}
1302
1303static int ieee80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1304{
1305	struct ieee80211_local *local = wiphy_priv(wiphy);
1306	int err;
1307
1308	if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1309		err = drv_set_frag_threshold(local, wiphy->frag_threshold);
1310
1311		if (err)
1312			return err;
1313	}
1314
1315	if (changed & WIPHY_PARAM_COVERAGE_CLASS) {
1316		err = drv_set_coverage_class(local, wiphy->coverage_class);
1317
1318		if (err)
1319			return err;
1320	}
1321
1322	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1323		err = drv_set_rts_threshold(local, wiphy->rts_threshold);
1324
1325		if (err)
1326			return err;
1327	}
1328
1329	if (changed & WIPHY_PARAM_RETRY_SHORT)
1330		local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
1331	if (changed & WIPHY_PARAM_RETRY_LONG)
1332		local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
1333	if (changed &
1334	    (WIPHY_PARAM_RETRY_SHORT | WIPHY_PARAM_RETRY_LONG))
1335		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
1336
1337	return 0;
1338}
1339
1340static int ieee80211_set_tx_power(struct wiphy *wiphy,
1341				  enum nl80211_tx_power_setting type, int mbm)
1342{
1343	struct ieee80211_local *local = wiphy_priv(wiphy);
1344	struct ieee80211_channel *chan = local->hw.conf.channel;
1345	u32 changes = 0;
1346
1347	switch (type) {
1348	case NL80211_TX_POWER_AUTOMATIC:
1349		local->user_power_level = -1;
1350		break;
1351	case NL80211_TX_POWER_LIMITED:
1352		if (mbm < 0 || (mbm % 100))
1353			return -EOPNOTSUPP;
1354		local->user_power_level = MBM_TO_DBM(mbm);
1355		break;
1356	case NL80211_TX_POWER_FIXED:
1357		if (mbm < 0 || (mbm % 100))
1358			return -EOPNOTSUPP;
1359		/* TODO: move to cfg80211 when it knows the channel */
1360		if (MBM_TO_DBM(mbm) > chan->max_power)
1361			return -EINVAL;
1362		local->user_power_level = MBM_TO_DBM(mbm);
1363		break;
1364	}
1365
1366	ieee80211_hw_config(local, changes);
1367
1368	return 0;
1369}
1370
1371static int ieee80211_get_tx_power(struct wiphy *wiphy, int *dbm)
1372{
1373	struct ieee80211_local *local = wiphy_priv(wiphy);
1374
1375	*dbm = local->hw.conf.power_level;
1376
1377	return 0;
1378}
1379
1380static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
1381				  const u8 *addr)
1382{
1383	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1384
1385	memcpy(&sdata->u.wds.remote_addr, addr, ETH_ALEN);
1386
1387	return 0;
1388}
1389
1390static void ieee80211_rfkill_poll(struct wiphy *wiphy)
1391{
1392	struct ieee80211_local *local = wiphy_priv(wiphy);
1393
1394	drv_rfkill_poll(local);
1395}
1396
1397#ifdef CONFIG_NL80211_TESTMODE
1398static int ieee80211_testmode_cmd(struct wiphy *wiphy, void *data, int len)
1399{
1400	struct ieee80211_local *local = wiphy_priv(wiphy);
1401
1402	if (!local->ops->testmode_cmd)
1403		return -EOPNOTSUPP;
1404
1405	return local->ops->testmode_cmd(&local->hw, data, len);
1406}
1407#endif
1408
1409int __ieee80211_request_smps(struct ieee80211_sub_if_data *sdata,
1410			     enum ieee80211_smps_mode smps_mode)
1411{
1412	const u8 *ap;
1413	enum ieee80211_smps_mode old_req;
1414	int err;
1415
1416	old_req = sdata->u.mgd.req_smps;
1417	sdata->u.mgd.req_smps = smps_mode;
1418
1419	if (old_req == smps_mode &&
1420	    smps_mode != IEEE80211_SMPS_AUTOMATIC)
1421		return 0;
1422
1423	/*
1424	 * If not associated, or current association is not an HT
1425	 * association, there's no need to send an action frame.
1426	 */
1427	if (!sdata->u.mgd.associated ||
1428	    sdata->vif.bss_conf.channel_type == NL80211_CHAN_NO_HT) {
1429		mutex_lock(&sdata->local->iflist_mtx);
1430		ieee80211_recalc_smps(sdata->local);
1431		mutex_unlock(&sdata->local->iflist_mtx);
1432		return 0;
1433	}
1434
1435	ap = sdata->u.mgd.associated->bssid;
1436
1437	if (smps_mode == IEEE80211_SMPS_AUTOMATIC) {
1438		if (sdata->u.mgd.powersave)
1439			smps_mode = IEEE80211_SMPS_DYNAMIC;
1440		else
1441			smps_mode = IEEE80211_SMPS_OFF;
1442	}
1443
1444	/* send SM PS frame to AP */
1445	err = ieee80211_send_smps_action(sdata, smps_mode,
1446					 ap, ap);
1447	if (err)
1448		sdata->u.mgd.req_smps = old_req;
1449
1450	return err;
1451}
1452
1453static int ieee80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
1454				    bool enabled, int timeout)
1455{
1456	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1457	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1458
1459	if (sdata->vif.type != NL80211_IFTYPE_STATION)
1460		return -EOPNOTSUPP;
1461
1462	if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
1463		return -EOPNOTSUPP;
1464
1465	if (enabled == sdata->u.mgd.powersave &&
1466	    timeout == local->dynamic_ps_forced_timeout)
1467		return 0;
1468
1469	sdata->u.mgd.powersave = enabled;
1470	local->dynamic_ps_forced_timeout = timeout;
1471
1472	/* no change, but if automatic follow powersave */
1473	mutex_lock(&sdata->u.mgd.mtx);
1474	__ieee80211_request_smps(sdata, sdata->u.mgd.req_smps);
1475	mutex_unlock(&sdata->u.mgd.mtx);
1476
1477	if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
1478		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1479
1480	ieee80211_recalc_ps(local, -1);
1481
1482	return 0;
1483}
1484
1485static int ieee80211_set_cqm_rssi_config(struct wiphy *wiphy,
1486					 struct net_device *dev,
1487					 s32 rssi_thold, u32 rssi_hyst)
1488{
1489	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1490	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1491	struct ieee80211_vif *vif = &sdata->vif;
1492	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1493
1494	if (rssi_thold == bss_conf->cqm_rssi_thold &&
1495	    rssi_hyst == bss_conf->cqm_rssi_hyst)
1496		return 0;
1497
1498	bss_conf->cqm_rssi_thold = rssi_thold;
1499	bss_conf->cqm_rssi_hyst = rssi_hyst;
1500
1501	if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
1502		if (sdata->vif.type != NL80211_IFTYPE_STATION)
1503			return -EOPNOTSUPP;
1504		return 0;
1505	}
1506
1507	/* tell the driver upon association, unless already associated */
1508	if (sdata->u.mgd.associated)
1509		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_CQM);
1510
1511	return 0;
1512}
1513
1514static int ieee80211_set_bitrate_mask(struct wiphy *wiphy,
1515				      struct net_device *dev,
1516				      const u8 *addr,
1517				      const struct cfg80211_bitrate_mask *mask)
1518{
1519	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1520	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1521	int i;
1522
1523	/*
1524	 * This _could_ be supported by providing a hook for
1525	 * drivers for this function, but at this point it
1526	 * doesn't seem worth bothering.
1527	 */
1528	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
1529		return -EOPNOTSUPP;
1530
1531
1532	for (i = 0; i < IEEE80211_NUM_BANDS; i++)
1533		sdata->rc_rateidx_mask[i] = mask->control[i].legacy;
1534
1535	return 0;
1536}
1537
1538static int ieee80211_remain_on_channel(struct wiphy *wiphy,
1539				       struct net_device *dev,
1540				       struct ieee80211_channel *chan,
1541				       enum nl80211_channel_type channel_type,
1542				       unsigned int duration,
1543				       u64 *cookie)
1544{
1545	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1546
1547	return ieee80211_wk_remain_on_channel(sdata, chan, channel_type,
1548					      duration, cookie);
1549}
1550
1551static int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
1552					      struct net_device *dev,
1553					      u64 cookie)
1554{
1555	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1556
1557	return ieee80211_wk_cancel_remain_on_channel(sdata, cookie);
1558}
1559
1560static enum work_done_result
1561ieee80211_offchan_tx_done(struct ieee80211_work *wk, struct sk_buff *skb)
1562{
1563	/*
1564	 * Use the data embedded in the work struct for reporting
1565	 * here so if the driver mangled the SKB before dropping
1566	 * it (which is the only way we really should get here)
1567	 * then we don't report mangled data.
1568	 *
1569	 * If there was no wait time, then by the time we get here
1570	 * the driver will likely not have reported the status yet,
1571	 * so in that case userspace will have to deal with it.
1572	 */
1573
1574	if (wk->offchan_tx.wait && wk->offchan_tx.frame)
1575		cfg80211_mgmt_tx_status(wk->sdata->dev,
1576					(unsigned long) wk->offchan_tx.frame,
1577					wk->ie, wk->ie_len, false, GFP_KERNEL);
1578
1579	return WORK_DONE_DESTROY;
1580}
1581
1582static int ieee80211_mgmt_tx(struct wiphy *wiphy, struct net_device *dev,
1583			     struct ieee80211_channel *chan, bool offchan,
1584			     enum nl80211_channel_type channel_type,
1585			     bool channel_type_valid, unsigned int wait,
1586			     const u8 *buf, size_t len, u64 *cookie)
1587{
1588	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1589	struct ieee80211_local *local = sdata->local;
1590	struct sk_buff *skb;
1591	struct sta_info *sta;
1592	struct ieee80211_work *wk;
1593	const struct ieee80211_mgmt *mgmt = (void *)buf;
1594	u32 flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1595		    IEEE80211_TX_CTL_REQ_TX_STATUS;
1596	bool is_offchan = false;
1597
1598	/* Check that we are on the requested channel for transmission */
1599	if (chan != local->tmp_channel &&
1600	    chan != local->oper_channel)
1601		is_offchan = true;
1602	if (channel_type_valid &&
1603	    (channel_type != local->tmp_channel_type &&
1604	     channel_type != local->_oper_channel_type))
1605		is_offchan = true;
1606
1607	if (is_offchan && !offchan)
1608		return -EBUSY;
1609
1610	switch (sdata->vif.type) {
1611	case NL80211_IFTYPE_ADHOC:
1612	case NL80211_IFTYPE_AP:
1613	case NL80211_IFTYPE_AP_VLAN:
1614	case NL80211_IFTYPE_P2P_GO:
1615		if (!ieee80211_is_action(mgmt->frame_control) ||
1616		    mgmt->u.action.category == WLAN_CATEGORY_PUBLIC)
1617			break;
1618		rcu_read_lock();
1619		sta = sta_info_get(sdata, mgmt->da);
1620		rcu_read_unlock();
1621		if (!sta)
1622			return -ENOLINK;
1623		break;
1624	case NL80211_IFTYPE_STATION:
1625	case NL80211_IFTYPE_P2P_CLIENT:
1626		break;
1627	default:
1628		return -EOPNOTSUPP;
1629	}
1630
1631	skb = dev_alloc_skb(local->hw.extra_tx_headroom + len);
1632	if (!skb)
1633		return -ENOMEM;
1634	skb_reserve(skb, local->hw.extra_tx_headroom);
1635
1636	memcpy(skb_put(skb, len), buf, len);
1637
1638	IEEE80211_SKB_CB(skb)->flags = flags;
1639
1640	skb->dev = sdata->dev;
1641
1642	*cookie = (unsigned long) skb;
1643
1644	/*
1645	 * Can transmit right away if the channel was the
1646	 * right one and there's no wait involved... If a
1647	 * wait is involved, we might otherwise not be on
1648	 * the right channel for long enough!
1649	 */
1650	if (!is_offchan && !wait && !sdata->vif.bss_conf.idle) {
1651		ieee80211_tx_skb(sdata, skb);
1652		return 0;
1653	}
1654
1655	wk = kzalloc(sizeof(*wk) + len, GFP_KERNEL);
1656	if (!wk) {
1657		kfree_skb(skb);
1658		return -ENOMEM;
1659	}
1660
1661	wk->type = IEEE80211_WORK_OFFCHANNEL_TX;
1662	wk->chan = chan;
1663	wk->sdata = sdata;
1664	wk->done = ieee80211_offchan_tx_done;
1665	wk->offchan_tx.frame = skb;
1666	wk->offchan_tx.wait = wait;
1667	wk->ie_len = len;
1668	memcpy(wk->ie, buf, len);
1669
1670	ieee80211_add_work(wk);
1671	return 0;
1672}
1673
1674static int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
1675					 struct net_device *dev,
1676					 u64 cookie)
1677{
1678	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1679	struct ieee80211_local *local = sdata->local;
1680	struct ieee80211_work *wk;
1681	int ret = -ENOENT;
1682
1683	mutex_lock(&local->mtx);
1684	list_for_each_entry(wk, &local->work_list, list) {
1685		if (wk->sdata != sdata)
1686			continue;
1687
1688		if (wk->type != IEEE80211_WORK_OFFCHANNEL_TX)
1689			continue;
1690
1691		if (cookie != (unsigned long) wk->offchan_tx.frame)
1692			continue;
1693
1694		wk->timeout = jiffies;
1695
1696		ieee80211_queue_work(&local->hw, &local->work_work);
1697		ret = 0;
1698		break;
1699	}
1700	mutex_unlock(&local->mtx);
1701
1702	return ret;
1703}
1704
1705static void ieee80211_mgmt_frame_register(struct wiphy *wiphy,
1706					  struct net_device *dev,
1707					  u16 frame_type, bool reg)
1708{
1709	struct ieee80211_local *local = wiphy_priv(wiphy);
1710
1711	if (frame_type != (IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_PROBE_REQ))
1712		return;
1713
1714	if (reg)
1715		local->probe_req_reg++;
1716	else
1717		local->probe_req_reg--;
1718
1719	ieee80211_queue_work(&local->hw, &local->reconfig_filter);
1720}
1721
1722static int ieee80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
1723{
1724	struct ieee80211_local *local = wiphy_priv(wiphy);
1725
1726	if (local->started)
1727		return -EOPNOTSUPP;
1728
1729	return drv_set_antenna(local, tx_ant, rx_ant);
1730}
1731
1732static int ieee80211_get_antenna(struct wiphy *wiphy, u32 *tx_ant, u32 *rx_ant)
1733{
1734	struct ieee80211_local *local = wiphy_priv(wiphy);
1735
1736	return drv_get_antenna(local, tx_ant, rx_ant);
1737}
1738
1739struct cfg80211_ops mac80211_config_ops = {
1740	.add_virtual_intf = ieee80211_add_iface,
1741	.del_virtual_intf = ieee80211_del_iface,
1742	.change_virtual_intf = ieee80211_change_iface,
1743	.add_key = ieee80211_add_key,
1744	.del_key = ieee80211_del_key,
1745	.get_key = ieee80211_get_key,
1746	.set_default_key = ieee80211_config_default_key,
1747	.set_default_mgmt_key = ieee80211_config_default_mgmt_key,
1748	.add_beacon = ieee80211_add_beacon,
1749	.set_beacon = ieee80211_set_beacon,
1750	.del_beacon = ieee80211_del_beacon,
1751	.add_station = ieee80211_add_station,
1752	.del_station = ieee80211_del_station,
1753	.change_station = ieee80211_change_station,
1754	.get_station = ieee80211_get_station,
1755	.dump_station = ieee80211_dump_station,
1756	.dump_survey = ieee80211_dump_survey,
1757#ifdef CONFIG_MAC80211_MESH
1758	.add_mpath = ieee80211_add_mpath,
1759	.del_mpath = ieee80211_del_mpath,
1760	.change_mpath = ieee80211_change_mpath,
1761	.get_mpath = ieee80211_get_mpath,
1762	.dump_mpath = ieee80211_dump_mpath,
1763	.set_mesh_params = ieee80211_set_mesh_params,
1764	.get_mesh_params = ieee80211_get_mesh_params,
1765#endif
1766	.change_bss = ieee80211_change_bss,
1767	.set_txq_params = ieee80211_set_txq_params,
1768	.set_channel = ieee80211_set_channel,
1769	.suspend = ieee80211_suspend,
1770	.resume = ieee80211_resume,
1771	.scan = ieee80211_scan,
1772	.auth = ieee80211_auth,
1773	.assoc = ieee80211_assoc,
1774	.deauth = ieee80211_deauth,
1775	.disassoc = ieee80211_disassoc,
1776	.join_ibss = ieee80211_join_ibss,
1777	.leave_ibss = ieee80211_leave_ibss,
1778	.set_wiphy_params = ieee80211_set_wiphy_params,
1779	.set_tx_power = ieee80211_set_tx_power,
1780	.get_tx_power = ieee80211_get_tx_power,
1781	.set_wds_peer = ieee80211_set_wds_peer,
1782	.rfkill_poll = ieee80211_rfkill_poll,
1783	CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
1784	.set_power_mgmt = ieee80211_set_power_mgmt,
1785	.set_bitrate_mask = ieee80211_set_bitrate_mask,
1786	.remain_on_channel = ieee80211_remain_on_channel,
1787	.cancel_remain_on_channel = ieee80211_cancel_remain_on_channel,
1788	.mgmt_tx = ieee80211_mgmt_tx,
1789	.mgmt_tx_cancel_wait = ieee80211_mgmt_tx_cancel_wait,
1790	.set_cqm_rssi_config = ieee80211_set_cqm_rssi_config,
1791	.mgmt_frame_register = ieee80211_mgmt_frame_register,
1792	.set_antenna = ieee80211_set_antenna,
1793	.get_antenna = ieee80211_get_antenna,
1794};
1795