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