cfg.c 88.3 KB
Newer Older
1
2
3
/*
 * mac80211 configuration hooks for cfg80211
 *
4
 * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
5
6
7
8
 *
 * This file is GPLv2 as found in COPYING.
 */

9
#include <linux/ieee80211.h>
10
11
#include <linux/nl80211.h>
#include <linux/rtnetlink.h>
12
#include <linux/slab.h>
13
#include <net/net_namespace.h>
14
#include <linux/rcupdate.h>
15
#include <linux/if_ether.h>
16
17
#include <net/cfg80211.h>
#include "ieee80211_i.h"
18
#include "driver-ops.h"
19
#include "cfg.h"
Johannes Berg's avatar
Johannes Berg committed
20
#include "rate.h"
21
22
#include "mesh.h"

23
24
static struct wireless_dev *ieee80211_add_iface(struct wiphy *wiphy,
						const char *name,
25
26
27
						enum nl80211_iftype type,
						u32 *flags,
						struct vif_params *params)
28
29
{
	struct ieee80211_local *local = wiphy_priv(wiphy);
30
	struct wireless_dev *wdev;
31
32
	struct ieee80211_sub_if_data *sdata;
	int err;
33

34
	err = ieee80211_if_add(local, name, &wdev, type, params);
35
36
	if (err)
		return ERR_PTR(err);
37

38
	if (type == NL80211_IFTYPE_MONITOR && flags) {
39
		sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
40
41
42
		sdata->u.mntr_flags = *flags;
	}

43
	return wdev;
44
45
}

46
static int ieee80211_del_iface(struct wiphy *wiphy, struct wireless_dev *wdev)
47
{
48
	ieee80211_if_remove(IEEE80211_WDEV_TO_SUB_IF(wdev));
49

50
	return 0;
51
52
}

53
54
static int ieee80211_change_iface(struct wiphy *wiphy,
				  struct net_device *dev,
55
56
				  enum nl80211_iftype type, u32 *flags,
				  struct vif_params *params)
57
{
58
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
59
	int ret;
60

61
	ret = ieee80211_if_change_type(sdata, type);
62
63
	if (ret)
		return ret;
64

65
66
	if (type == NL80211_IFTYPE_AP_VLAN &&
	    params && params->use_4addr == 0)
67
		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
68
69
70
71
	else if (type == NL80211_IFTYPE_STATION &&
		 params && params->use_4addr >= 0)
		sdata->u.mgd.use_4addr = params->use_4addr;

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
		struct ieee80211_local *local = sdata->local;

		if (ieee80211_sdata_running(sdata)) {
			/*
			 * Prohibit MONITOR_FLAG_COOK_FRAMES to be
			 * changed while the interface is up.
			 * Else we would need to add a lot of cruft
			 * to update everything:
			 *	cooked_mntrs, monitor and all fif_* counters
			 *	reconfigure hardware
			 */
			if ((*flags & MONITOR_FLAG_COOK_FRAMES) !=
			    (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
				return -EBUSY;

			ieee80211_adjust_monitor_flags(sdata, -1);
			sdata->u.mntr_flags = *flags;
			ieee80211_adjust_monitor_flags(sdata, 1);

			ieee80211_configure_filter(local);
		} else {
			/*
			 * Because the interface is down, ieee80211_do_stop
			 * and ieee80211_do_open take care of "everything"
			 * mentioned in the comment above.
			 */
			sdata->u.mntr_flags = *flags;
		}
	}
102

103
104
105
	return 0;
}

106
107
108
109
110
111
112
113
114
115
116
117
static int ieee80211_start_p2p_device(struct wiphy *wiphy,
				      struct wireless_dev *wdev)
{
	return ieee80211_do_open(wdev, true);
}

static void ieee80211_stop_p2p_device(struct wiphy *wiphy,
				      struct wireless_dev *wdev)
{
	ieee80211_sdata_stop(IEEE80211_WDEV_TO_SUB_IF(wdev));
}

118
119
120
121
122
123
124
125
126
127
static int ieee80211_set_noack_map(struct wiphy *wiphy,
				  struct net_device *dev,
				  u16 noack_map)
{
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);

	sdata->noack_map = noack_map;
	return 0;
}

128
static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
129
			     u8 key_idx, bool pairwise, const u8 *mac_addr,
130
131
			     struct key_params *params)
{
132
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
133
	struct sta_info *sta = NULL;
134
	struct ieee80211_key *key;
135
	int err;
136

137
	if (!ieee80211_sdata_running(sdata))
Johannes Berg's avatar
Johannes Berg committed
138
139
		return -ENETDOWN;

140
	/* reject WEP and TKIP keys if WEP failed to initialize */
141
142
143
	switch (params->cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_TKIP:
144
145
146
	case WLAN_CIPHER_SUITE_WEP104:
		if (IS_ERR(sdata->local->wep_tx_tfm))
			return -EINVAL;
147
		break;
148
	default:
149
		break;
150
151
	}

152
153
	key = ieee80211_key_alloc(params->cipher, key_idx, params->key_len,
				  params->key, params->seq_len, params->seq);
154
155
	if (IS_ERR(key))
		return PTR_ERR(key);
156

157
158
159
	if (pairwise)
		key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;

Johannes Berg's avatar
Johannes Berg committed
160
	mutex_lock(&sdata->local->sta_mtx);
161

162
	if (mac_addr) {
163
164
165
166
		if (ieee80211_vif_is_mesh(&sdata->vif))
			sta = sta_info_get(sdata, mac_addr);
		else
			sta = sta_info_get_bss(sdata, mac_addr);
Johannes Berg's avatar
Johannes Berg committed
167
168
169
170
171
172
173
174
175
176
177
		/*
		 * The ASSOC test makes sure the driver is ready to
		 * receive the key. When wpa_supplicant has roamed
		 * using FT, it attempts to set the key before
		 * association has completed, this rejects that attempt
		 * so it will set the key again after assocation.
		 *
		 * TODO: accept the key if we have a station entry and
		 *       add it to the device after the station.
		 */
		if (!sta || !test_sta_flag(sta, WLAN_STA_ASSOC)) {
178
			ieee80211_key_free(sdata->local, key);
179
180
			err = -ENOENT;
			goto out_unlock;
181
		}
182
183
	}

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
	switch (sdata->vif.type) {
	case NL80211_IFTYPE_STATION:
		if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
			key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
		break;
	case NL80211_IFTYPE_AP:
	case NL80211_IFTYPE_AP_VLAN:
		/* Keys without a station are used for TX only */
		if (key->sta && test_sta_flag(key->sta, WLAN_STA_MFP))
			key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
		break;
	case NL80211_IFTYPE_ADHOC:
		/* no MFP (yet) */
		break;
	case NL80211_IFTYPE_MESH_POINT:
#ifdef CONFIG_MAC80211_MESH
		if (sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
			key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
		break;
#endif
	case NL80211_IFTYPE_WDS:
	case NL80211_IFTYPE_MONITOR:
	case NL80211_IFTYPE_P2P_DEVICE:
	case NL80211_IFTYPE_UNSPECIFIED:
	case NUM_NL80211_IFTYPES:
	case NL80211_IFTYPE_P2P_CLIENT:
	case NL80211_IFTYPE_P2P_GO:
		/* shouldn't happen */
		WARN_ON_ONCE(1);
		break;
	}

216
217
218
	err = ieee80211_key_link(key, sdata, sta);
	if (err)
		ieee80211_key_free(sdata->local, key);
219

220
 out_unlock:
Johannes Berg's avatar
Johannes Berg committed
221
	mutex_unlock(&sdata->local->sta_mtx);
222
223

	return err;
224
225
226
}

static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
227
			     u8 key_idx, bool pairwise, const u8 *mac_addr)
228
{
229
230
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	struct ieee80211_local *local = sdata->local;
231
	struct sta_info *sta;
232
	struct ieee80211_key *key = NULL;
233
234
	int ret;

235
236
	mutex_lock(&local->sta_mtx);
	mutex_lock(&local->key_mtx);
237

238
	if (mac_addr) {
239
240
		ret = -ENOENT;

241
		sta = sta_info_get_bss(sdata, mac_addr);
242
		if (!sta)
243
			goto out_unlock;
244

245
		if (pairwise)
246
			key = key_mtx_dereference(local, sta->ptk);
247
		else
248
			key = key_mtx_dereference(local, sta->gtk[key_idx]);
249
	} else
250
		key = key_mtx_dereference(local, sdata->keys[key_idx]);
251

252
	if (!key) {
253
254
255
		ret = -ENOENT;
		goto out_unlock;
	}
256

257
	__ieee80211_key_free(key);
258

259
260
	ret = 0;
 out_unlock:
261
262
	mutex_unlock(&local->key_mtx);
	mutex_unlock(&local->sta_mtx);
263
264

	return ret;
265
266
}

267
static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
268
269
			     u8 key_idx, bool pairwise, const u8 *mac_addr,
			     void *cookie,
270
271
272
			     void (*callback)(void *cookie,
					      struct key_params *params))
{
273
	struct ieee80211_sub_if_data *sdata;
274
275
276
	struct sta_info *sta = NULL;
	u8 seq[6] = {0};
	struct key_params params;
277
	struct ieee80211_key *key = NULL;
Johannes Berg's avatar
Johannes Berg committed
278
	u64 pn64;
279
280
281
282
	u32 iv32;
	u16 iv16;
	int err = -ENOENT;

283
284
	sdata = IEEE80211_DEV_TO_SUB_IF(dev);

285
286
	rcu_read_lock();

287
	if (mac_addr) {
288
		sta = sta_info_get_bss(sdata, mac_addr);
289
290
291
		if (!sta)
			goto out;

292
		if (pairwise)
Johannes Berg's avatar
Johannes Berg committed
293
			key = rcu_dereference(sta->ptk);
294
		else if (key_idx < NUM_DEFAULT_KEYS)
Johannes Berg's avatar
Johannes Berg committed
295
			key = rcu_dereference(sta->gtk[key_idx]);
296
	} else
Johannes Berg's avatar
Johannes Berg committed
297
		key = rcu_dereference(sdata->keys[key_idx]);
298
299
300
301
302
303

	if (!key)
		goto out;

	memset(&params, 0, sizeof(params));

304
	params.cipher = key->conf.cipher;
305

306
307
	switch (key->conf.cipher) {
	case WLAN_CIPHER_SUITE_TKIP:
308
309
		iv32 = key->u.tkip.tx.iv32;
		iv16 = key->u.tkip.tx.iv16;
310

311
312
313
314
		if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
			drv_get_tkip_seq(sdata->local,
					 key->conf.hw_key_idx,
					 &iv32, &iv16);
315
316
317
318
319
320
321
322
323
324

		seq[0] = iv16 & 0xff;
		seq[1] = (iv16 >> 8) & 0xff;
		seq[2] = iv32 & 0xff;
		seq[3] = (iv32 >> 8) & 0xff;
		seq[4] = (iv32 >> 16) & 0xff;
		seq[5] = (iv32 >> 24) & 0xff;
		params.seq = seq;
		params.seq_len = 6;
		break;
325
	case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg's avatar
Johannes Berg committed
326
327
328
329
330
331
332
		pn64 = atomic64_read(&key->u.ccmp.tx_pn);
		seq[0] = pn64;
		seq[1] = pn64 >> 8;
		seq[2] = pn64 >> 16;
		seq[3] = pn64 >> 24;
		seq[4] = pn64 >> 32;
		seq[5] = pn64 >> 40;
333
334
335
		params.seq = seq;
		params.seq_len = 6;
		break;
336
	case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg's avatar
Johannes Berg committed
337
338
339
340
341
342
343
		pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
		seq[0] = pn64;
		seq[1] = pn64 >> 8;
		seq[2] = pn64 >> 16;
		seq[3] = pn64 >> 24;
		seq[4] = pn64 >> 32;
		seq[5] = pn64 >> 40;
344
345
346
		params.seq = seq;
		params.seq_len = 6;
		break;
347
348
349
350
351
352
353
354
355
	}

	params.key = key->conf.key;
	params.key_len = key->conf.keylen;

	callback(cookie, &params);
	err = 0;

 out:
356
	rcu_read_unlock();
357
358
359
	return err;
}

360
361
static int ieee80211_config_default_key(struct wiphy *wiphy,
					struct net_device *dev,
362
363
					u8 key_idx, bool uni,
					bool multi)
364
{
Johannes Berg's avatar
Johannes Berg committed
365
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
366

367
	ieee80211_set_default_key(sdata, key_idx, uni, multi);
368
369
370
371

	return 0;
}

372
373
374
375
static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
					     struct net_device *dev,
					     u8 key_idx)
{
376
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
377
378
379
380
381
382

	ieee80211_set_default_mgmt_key(sdata, key_idx);

	return 0;
}

383
384
385
386
387
void sta_set_rate_info_tx(struct sta_info *sta,
			  const struct ieee80211_tx_rate *rate,
			  struct rate_info *rinfo)
{
	rinfo->flags = 0;
388
	if (rate->flags & IEEE80211_TX_RC_MCS) {
389
		rinfo->flags |= RATE_INFO_FLAGS_MCS;
390
391
392
393
394
395
396
397
398
399
400
		rinfo->mcs = rate->idx;
	} else if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
		rinfo->mcs = ieee80211_rate_get_vht_mcs(rate);
		rinfo->nss = ieee80211_rate_get_vht_nss(rate);
	} else {
		struct ieee80211_supported_band *sband;
		sband = sta->local->hw.wiphy->bands[
				ieee80211_get_sdata_band(sta->sdata)];
		rinfo->legacy = sband->bitrates[rate->idx].bitrate;
	}
401
402
	if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
		rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
403
404
405
406
	if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
		rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
	if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
		rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
407
408
409
410
	if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
}

411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
{
	rinfo->flags = 0;

	if (sta->last_rx_rate_flag & RX_FLAG_HT) {
		rinfo->flags |= RATE_INFO_FLAGS_MCS;
		rinfo->mcs = sta->last_rx_rate_idx;
	} else if (sta->last_rx_rate_flag & RX_FLAG_VHT) {
		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
		rinfo->nss = sta->last_rx_rate_vht_nss;
		rinfo->mcs = sta->last_rx_rate_idx;
	} else {
		struct ieee80211_supported_band *sband;

		sband = sta->local->hw.wiphy->bands[
				ieee80211_get_sdata_band(sta->sdata)];
		rinfo->legacy =
			sband->bitrates[sta->last_rx_rate_idx].bitrate;
	}

	if (sta->last_rx_rate_flag & RX_FLAG_40MHZ)
		rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
	if (sta->last_rx_rate_flag & RX_FLAG_SHORT_GI)
		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
	if (sta->last_rx_rate_flag & RX_FLAG_80MHZ)
		rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
	if (sta->last_rx_rate_flag & RX_FLAG_80P80MHZ)
		rinfo->flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
	if (sta->last_rx_rate_flag & RX_FLAG_160MHZ)
		rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
}

443
444
static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
{
445
	struct ieee80211_sub_if_data *sdata = sta->sdata;
446
	struct ieee80211_local *local = sdata->local;
447
	struct timespec uptime;
448

449
450
	sinfo->generation = sdata->local->sta_generation;

451
452
	sinfo->filled = STATION_INFO_INACTIVE_TIME |
			STATION_INFO_RX_BYTES |
453
			STATION_INFO_TX_BYTES |
454
455
			STATION_INFO_RX_PACKETS |
			STATION_INFO_TX_PACKETS |
456
457
			STATION_INFO_TX_RETRIES |
			STATION_INFO_TX_FAILED |
458
			STATION_INFO_TX_BITRATE |
459
			STATION_INFO_RX_BITRATE |
460
			STATION_INFO_RX_DROP_MISC |
461
			STATION_INFO_BSS_PARAM |
462
			STATION_INFO_CONNECTED_TIME |
463
464
			STATION_INFO_STA_FLAGS |
			STATION_INFO_BEACON_LOSS_COUNT;
465
466
467

	do_posix_clock_monotonic_gettime(&uptime);
	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
468
469
470
471

	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
	sinfo->rx_bytes = sta->rx_bytes;
	sinfo->tx_bytes = sta->tx_bytes;
472
473
	sinfo->rx_packets = sta->rx_packets;
	sinfo->tx_packets = sta->tx_packets;
474
475
	sinfo->tx_retries = sta->tx_retry_count;
	sinfo->tx_failed = sta->tx_retry_failed;
476
	sinfo->rx_dropped_misc = sta->rx_dropped;
477
	sinfo->beacon_loss_count = sta->beacon_loss_count;
478

479
480
	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
481
		sinfo->filled |= STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
482
483
484
		if (!local->ops->get_rssi ||
		    drv_get_rssi(local, sdata, &sta->sta, &sinfo->signal))
			sinfo->signal = (s8)sta->last_signal;
485
		sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
486
487
	}

488
	sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
489
	sta_set_rate_info_rx(sta, &sinfo->rxrate);
490

Johannes Berg's avatar
Johannes Berg committed
491
	if (ieee80211_vif_is_mesh(&sdata->vif)) {
492
493
494
#ifdef CONFIG_MAC80211_MESH
		sinfo->filled |= STATION_INFO_LLID |
				 STATION_INFO_PLID |
495
496
497
498
				 STATION_INFO_PLINK_STATE |
				 STATION_INFO_LOCAL_PM |
				 STATION_INFO_PEER_PM |
				 STATION_INFO_NONPEER_PM;
499
500
501
502

		sinfo->llid = le16_to_cpu(sta->llid);
		sinfo->plid = le16_to_cpu(sta->plid);
		sinfo->plink_state = sta->plink_state;
503
504
505
506
		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
			sinfo->filled |= STATION_INFO_T_OFFSET;
			sinfo->t_offset = sta->t_offset;
		}
507
508
509
		sinfo->local_pm = sta->local_pm;
		sinfo->peer_pm = sta->peer_pm;
		sinfo->nonpeer_pm = sta->nonpeer_pm;
510
#endif
Johannes Berg's avatar
Johannes Berg committed
511
	}
512
513
514
515
516
517
518
519
520
521

	sinfo->bss_param.flags = 0;
	if (sdata->vif.bss_conf.use_cts_prot)
		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
	if (sdata->vif.bss_conf.use_short_preamble)
		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
	if (sdata->vif.bss_conf.use_short_slot)
		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
	sinfo->bss_param.dtim_period = sdata->local->hw.conf.ps_dtim_period;
	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
522
523
524
525
526
527

	sinfo->sta_flags.set = 0;
	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
				BIT(NL80211_STA_FLAG_WME) |
				BIT(NL80211_STA_FLAG_MFP) |
528
				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
529
				BIT(NL80211_STA_FLAG_ASSOCIATED) |
530
				BIT(NL80211_STA_FLAG_TDLS_PEER);
531
532
533
534
535
536
537
538
539
540
	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
	if (test_sta_flag(sta, WLAN_STA_WME))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
	if (test_sta_flag(sta, WLAN_STA_MFP))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
	if (test_sta_flag(sta, WLAN_STA_AUTH))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
541
542
	if (test_sta_flag(sta, WLAN_STA_ASSOC))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
543
544
	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
545
546
}

547
548
549
550
551
static const char ieee80211_gstrings_sta_stats[][ETH_GSTRING_LEN] = {
	"rx_packets", "rx_bytes", "wep_weak_iv_count",
	"rx_duplicates", "rx_fragments", "rx_dropped",
	"tx_packets", "tx_bytes", "tx_fragments",
	"tx_filtered", "tx_retry_failed", "tx_retries",
552
553
554
	"beacon_loss", "sta_state", "txrate", "rxrate", "signal",
	"channel", "noise", "ch_time", "ch_time_busy",
	"ch_time_ext_busy", "ch_time_rx", "ch_time_tx"
555
556
557
558
559
560
561
};
#define STA_STATS_LEN	ARRAY_SIZE(ieee80211_gstrings_sta_stats)

static int ieee80211_get_et_sset_count(struct wiphy *wiphy,
				       struct net_device *dev,
				       int sset)
{
562
563
564
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	int rv = 0;

565
	if (sset == ETH_SS_STATS)
566
567
568
		rv += STA_STATS_LEN;

	rv += drv_get_et_sset_count(sdata, sset);
569

570
571
572
	if (rv == 0)
		return -EOPNOTSUPP;
	return rv;
573
574
575
576
577
578
579
580
}

static void ieee80211_get_et_stats(struct wiphy *wiphy,
				   struct net_device *dev,
				   struct ethtool_stats *stats,
				   u64 *data)
{
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg's avatar
Johannes Berg committed
581
582
	struct ieee80211_chanctx_conf *chanctx_conf;
	struct ieee80211_channel *channel;
583
584
	struct sta_info *sta;
	struct ieee80211_local *local = sdata->local;
585
586
587
588
	struct station_info sinfo;
	struct survey_info survey;
	int i, q;
#define STA_STATS_SURVEY_LEN 7
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615

	memset(data, 0, sizeof(u64) * STA_STATS_LEN);

#define ADD_STA_STATS(sta)				\
	do {						\
		data[i++] += sta->rx_packets;		\
		data[i++] += sta->rx_bytes;		\
		data[i++] += sta->wep_weak_iv_count;	\
		data[i++] += sta->num_duplicates;	\
		data[i++] += sta->rx_fragments;		\
		data[i++] += sta->rx_dropped;		\
							\
		data[i++] += sta->tx_packets;		\
		data[i++] += sta->tx_bytes;		\
		data[i++] += sta->tx_fragments;		\
		data[i++] += sta->tx_filtered_count;	\
		data[i++] += sta->tx_retry_failed;	\
		data[i++] += sta->tx_retry_count;	\
		data[i++] += sta->beacon_loss_count;	\
	} while (0)

	/* For Managed stations, find the single station based on BSSID
	 * and use that.  For interface types, iterate through all available
	 * stations and add stats for any station that is assigned to this
	 * network device.
	 */

616
	mutex_lock(&local->sta_mtx);
617
618
619

	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
		sta = sta_info_get_bss(sdata, sdata->u.mgd.bssid);
620
621
622
623
624
625
626
627
628
629
630
631

		if (!(sta && !WARN_ON(sta->sdata->dev != dev)))
			goto do_survey;

		i = 0;
		ADD_STA_STATS(sta);

		data[i++] = sta->sta_state;

		sinfo.filled = 0;
		sta_set_sinfo(sta, &sinfo);

632
		if (sinfo.filled & STATION_INFO_TX_BITRATE)
633
634
635
			data[i] = 100000 *
				cfg80211_calculate_bitrate(&sinfo.txrate);
		i++;
636
		if (sinfo.filled & STATION_INFO_RX_BITRATE)
637
638
639
640
			data[i] = 100000 *
				cfg80211_calculate_bitrate(&sinfo.rxrate);
		i++;

641
		if (sinfo.filled & STATION_INFO_SIGNAL_AVG)
642
643
			data[i] = (u8)sinfo.signal_avg;
		i++;
644
	} else {
645
		list_for_each_entry(sta, &local->sta_list, list) {
646
647
648
649
650
651
652
653
654
			/* Make sure this station belongs to the proper dev */
			if (sta->sdata->dev != dev)
				continue;

			i = 0;
			ADD_STA_STATS(sta);
		}
	}

655
656
657
do_survey:
	i = STA_STATS_LEN - STA_STATS_SURVEY_LEN;
	/* Get survey stats for current channel */
Johannes Berg's avatar
Johannes Berg committed
658
	survey.filled = 0;
659

Johannes Berg's avatar
Johannes Berg committed
660
661
662
	rcu_read_lock();
	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
	if (chanctx_conf)
663
		channel = chanctx_conf->def.chan;
Johannes Berg's avatar
Johannes Berg committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
	else
		channel = NULL;
	rcu_read_unlock();

	if (channel) {
		q = 0;
		do {
			survey.filled = 0;
			if (drv_get_survey(local, q, &survey) != 0) {
				survey.filled = 0;
				break;
			}
			q++;
		} while (channel != survey.channel);
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
	}

	if (survey.filled)
		data[i++] = survey.channel->center_freq;
	else
		data[i++] = 0;
	if (survey.filled & SURVEY_INFO_NOISE_DBM)
		data[i++] = (u8)survey.noise;
	else
		data[i++] = -1LL;
	if (survey.filled & SURVEY_INFO_CHANNEL_TIME)
		data[i++] = survey.channel_time;
	else
		data[i++] = -1LL;
	if (survey.filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
		data[i++] = survey.channel_time_busy;
	else
		data[i++] = -1LL;
	if (survey.filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
		data[i++] = survey.channel_time_ext_busy;
	else
		data[i++] = -1LL;
	if (survey.filled & SURVEY_INFO_CHANNEL_TIME_RX)
		data[i++] = survey.channel_time_rx;
	else
		data[i++] = -1LL;
	if (survey.filled & SURVEY_INFO_CHANNEL_TIME_TX)
		data[i++] = survey.channel_time_tx;
	else
		data[i++] = -1LL;

709
	mutex_unlock(&local->sta_mtx);
710

711
712
713
	if (WARN_ON(i != STA_STATS_LEN))
		return;

714
	drv_get_et_stats(sdata, stats, &(data[STA_STATS_LEN]));
715
716
717
718
719
720
}

static void ieee80211_get_et_strings(struct wiphy *wiphy,
				     struct net_device *dev,
				     u32 sset, u8 *data)
{
721
722
723
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	int sz_sta_stats = 0;

724
	if (sset == ETH_SS_STATS) {
725
		sz_sta_stats = sizeof(ieee80211_gstrings_sta_stats);
726
727
		memcpy(data, *ieee80211_gstrings_sta_stats, sz_sta_stats);
	}
728
	drv_get_et_strings(sdata, sset, &(data[sz_sta_stats]));
729
}
730
731
732
733

static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
				 int idx, u8 *mac, struct station_info *sinfo)
{
734
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
735
	struct ieee80211_local *local = sdata->local;
736
	struct sta_info *sta;
737
738
	int ret = -ENOENT;

739
	mutex_lock(&local->sta_mtx);
740

741
	sta = sta_info_get_by_idx(sdata, idx);
742
743
	if (sta) {
		ret = 0;
744
		memcpy(mac, sta->sta.addr, ETH_ALEN);
745
746
		sta_set_sinfo(sta, sinfo);
	}
747

748
	mutex_unlock(&local->sta_mtx);
749

750
	return ret;
751
752
}

753
754
755
756
757
758
759
760
static int ieee80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
				 int idx, struct survey_info *survey)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

	return drv_get_survey(local, idx, survey);
}

761
static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
762
				 u8 *mac, struct station_info *sinfo)
763
{
764
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
765
	struct ieee80211_local *local = sdata->local;
766
	struct sta_info *sta;
767
	int ret = -ENOENT;
768

769
	mutex_lock(&local->sta_mtx);
770

771
	sta = sta_info_get_bss(sdata, mac);
772
773
774
775
776
	if (sta) {
		ret = 0;
		sta_set_sinfo(sta, sinfo);
	}

777
	mutex_unlock(&local->sta_mtx);
778
779

	return ret;
780
781
}

Johannes Berg's avatar
Johannes Berg committed
782
static int ieee80211_set_monitor_channel(struct wiphy *wiphy,
783
					 struct cfg80211_chan_def *chandef)
784
785
{
	struct ieee80211_local *local = wiphy_priv(wiphy);
Johannes Berg's avatar
Johannes Berg committed
786
787
	struct ieee80211_sub_if_data *sdata;
	int ret = 0;
788

789
	if (cfg80211_chandef_identical(&local->monitor_chandef, chandef))
Johannes Berg's avatar
Johannes Berg committed
790
		return 0;
791

Johannes Berg's avatar
Johannes Berg committed
792
793
794
795
796
797
798
	mutex_lock(&local->iflist_mtx);
	if (local->use_chanctx) {
		sdata = rcu_dereference_protected(
				local->monitor_sdata,
				lockdep_is_held(&local->iflist_mtx));
		if (sdata) {
			ieee80211_vif_release_channel(sdata);
799
			ret = ieee80211_vif_use_channel(sdata, chandef,
Johannes Berg's avatar
Johannes Berg committed
800
801
802
					IEEE80211_CHANCTX_EXCLUSIVE);
		}
	} else if (local->open_count == local->monitors) {
803
		local->_oper_channel = chandef->chan;
804
		local->_oper_channel_type = cfg80211_get_chandef_type(chandef);
Johannes Berg's avatar
Johannes Berg committed
805
806
		ieee80211_hw_config(local, 0);
	}
807

808
809
	if (ret == 0)
		local->monitor_chandef = *chandef;
Johannes Berg's avatar
Johannes Berg committed
810
	mutex_unlock(&local->iflist_mtx);
811

Johannes Berg's avatar
Johannes Berg committed
812
	return ret;
813
814
}

815
static int ieee80211_set_probe_resp(struct ieee80211_sub_if_data *sdata,
816
				    const u8 *resp, size_t resp_len)
817
{
818
	struct probe_resp *new, *old;
819
820

	if (!resp || !resp_len)
821
		return 1;
822

823
	old = rtnl_dereference(sdata->u.ap.probe_resp);
824

825
	new = kzalloc(sizeof(struct probe_resp) + resp_len, GFP_KERNEL);
826
827
828
	if (!new)
		return -ENOMEM;

829
830
	new->len = resp_len;
	memcpy(new->data, resp, resp_len);
831
832

	rcu_assign_pointer(sdata->u.ap.probe_resp, new);
833
834
	if (old)
		kfree_rcu(old, rcu_head);
835
836
837
838

	return 0;
}

839
840
static int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
				   struct cfg80211_beacon_data *params)
841
842
843
{
	struct beacon_data *new, *old;
	int new_head_len, new_tail_len;
844
845
	int size, err;
	u32 changed = BSS_CHANGED_BEACON;
846

847
	old = rtnl_dereference(sdata->u.ap.beacon);
848
849
850

	/* Need to have a beacon head if we don't have one yet */
	if (!params->head && !old)
851
		return -EINVAL;
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895

	/* new or old head? */
	if (params->head)
		new_head_len = params->head_len;
	else
		new_head_len = old->head_len;

	/* new or old tail? */
	if (params->tail || !old)
		/* params->tail_len will be zero for !params->tail */
		new_tail_len = params->tail_len;
	else
		new_tail_len = old->tail_len;

	size = sizeof(*new) + new_head_len + new_tail_len;

	new = kzalloc(size, GFP_KERNEL);
	if (!new)
		return -ENOMEM;

	/* start filling the new info now */

	/*
	 * pointers go into the block we allocated,
	 * memory is | beacon_data | head | tail |
	 */
	new->head = ((u8 *) new) + sizeof(*new);
	new->tail = new->head + new_head_len;
	new->head_len = new_head_len;
	new->tail_len = new_tail_len;

	/* copy in head */
	if (params->head)
		memcpy(new->head, params->head, new_head_len);
	else
		memcpy(new->head, old->head, new_head_len);

	/* copy in optional tail */
	if (params->tail)
		memcpy(new->tail, params->tail, new_tail_len);
	else
		if (old)
			memcpy(new->tail, old->tail, new_tail_len);

896
897
	err = ieee80211_set_probe_resp(sdata, params->probe_resp,
				       params->probe_resp_len);
898
899
900
	if (err < 0)
		return err;
	if (err == 0)
901
902
		changed |= BSS_CHANGED_AP_PROBE_RESP;

903
904
905
906
	rcu_assign_pointer(sdata->u.ap.beacon, new);

	if (old)
		kfree_rcu(old, rcu_head);
907

908
	return changed;
909
910
}

911
912
static int ieee80211_start_ap(struct wiphy *wiphy, struct net_device *dev,
			      struct cfg80211_ap_settings *params)
913
{
914
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
915
	struct beacon_data *old;
916
	struct ieee80211_sub_if_data *vlan;
917
918
919
	u32 changed = BSS_CHANGED_BEACON_INT |
		      BSS_CHANGED_BEACON_ENABLED |
		      BSS_CHANGED_BEACON |
920
921
		      BSS_CHANGED_SSID |
		      BSS_CHANGED_P2P_PS;
922
	int err;
923

924
	old = rtnl_dereference(sdata->u.ap.beacon);
925
926
927
	if (old)
		return -EALREADY;

928
929
930
931
	/* TODO: make hostapd tell us what it wants */
	sdata->smps_mode = IEEE80211_SMPS_OFF;
	sdata->needed_rx_chains = sdata->local->rx_chains;

932
	err = ieee80211_vif_use_channel(sdata, &params->chandef,
Johannes Berg's avatar
Johannes Berg committed
933
					IEEE80211_CHANCTX_SHARED);
934
935
	if (err)
		return err;
936
	ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
937

938
939
940
941
942
943
944
945
946
947
948
949
950
	/*
	 * Apply control port protocol, this allows us to
	 * not encrypt dynamic WEP control frames.
	 */
	sdata->control_port_protocol = params->crypto.control_port_ethertype;
	sdata->control_port_no_encrypt = params->crypto.control_port_no_encrypt;
	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
		vlan->control_port_protocol =
			params->crypto.control_port_ethertype;
		vlan->control_port_no_encrypt =
			params->crypto.control_port_no_encrypt;
	}

951
952
	sdata->vif.bss_conf.beacon_int = params->beacon_interval;
	sdata->vif.bss_conf.dtim_period = params->dtim_period;
953
	sdata->vif.bss_conf.enable_beacon = true;
954
955
956
957
958
959
960
961

	sdata->vif.bss_conf.ssid_len = params->ssid_len;
	if (params->ssid_len)
		memcpy(sdata->vif.bss_conf.ssid, params->ssid,
		       params->ssid_len);
	sdata->vif.bss_conf.hidden_ssid =
		(params->hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE);

962
963
964
	sdata->vif.bss_conf.p2p_ctwindow = params->p2p_ctwindow;
	sdata->vif.bss_conf.p2p_oppps = params->p2p_opp_ps;

965
966
967
968
969
	err = ieee80211_assign_beacon(sdata, &params->beacon);
	if (err < 0)
		return err;
	changed |= err;

970
971
972
973
974
975
976
977
978
	err = drv_start_ap(sdata->local, sdata);
	if (err) {
		old = rtnl_dereference(sdata->u.ap.beacon);
		if (old)
			kfree_rcu(old, rcu_head);
		RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
		return err;
	}

979
980
	ieee80211_bss_info_change_notify(sdata, changed);

981
982
983
984
	netif_carrier_on(dev);
	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
		netif_carrier_on(vlan->dev);

985
	return 0;
986
987
}

988
989
static int ieee80211_change_beacon(struct wiphy *wiphy, struct net_device *dev,
				   struct cfg80211_beacon_data *params)
990
{
991
	struct ieee80211_sub_if_data *sdata;
992
	struct beacon_data *old;
993
	int err;
994

995
996
	sdata = IEEE80211_DEV_TO_SUB_IF(dev);

997
	old = rtnl_dereference(sdata->u.ap.beacon);
998
999
1000
	if (!old)
		return -ENOENT;

1001
1002
1003
1004
1005
	err = ieee80211_assign_beacon(sdata, params);
	if (err < 0)
		return err;
	ieee80211_bss_info_change_notify(sdata, err);
	return 0;
1006
1007
}

1008
static int ieee80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
1009
{
1010
1011
1012
1013
1014
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	struct ieee80211_sub_if_data *vlan;
	struct ieee80211_local *local = sdata->local;
	struct beacon_data *old_beacon;
	struct probe_resp *old_probe_resp;
1015

1016
1017
	old_beacon = rtnl_dereference(sdata->u.ap.beacon);
	if (!old_beacon)
1018
		return -ENOENT;
1019
	old_probe_resp = rtnl_dereference(sdata->u.ap.probe_resp);
1020

1021
	/* turn off carrier for this interface and dependent VLANs */
1022
1023
1024
1025
	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
		netif_carrier_off(vlan->dev);
	netif_carrier_off(dev);

1026
	/* remove beacon and probe response */
1027
	RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
1028
1029
1030
1031
	RCU_INIT_POINTER(sdata->u.ap.probe_resp, NULL);
	kfree_rcu(old_beacon, rcu_head);
	if (old_probe_resp)
		kfree_rcu(old_probe_resp, rcu_head);
1032

1033
	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1034
1035
1036
1037
1038
1039
1040
		sta_info_flush_defer(vlan);
	sta_info_flush_defer(sdata);
	rcu_barrier();
	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
		sta_info_flush_cleanup(vlan);
	sta_info_flush_cleanup(sdata);

1041
1042
	sdata->vif.bss_conf.enable_beacon = false;
	clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
1043
	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
1044

1045
1046
	drv_stop_ap(sdata->local, sdata);

1047
1048
1049
1050
	/* free all potentially still buffered bcast frames */
	local->total_ps_buffered -= skb_queue_len(&sdata->u.ap.ps.bc_buf);
	skb_queue_purge(&sdata->u.ap.ps.bc_buf);

1051
	ieee80211_vif_copy_chanctx_to_vlans(sdata, true);
Johannes Berg's avatar
Johannes Berg committed
1052
1053
	ieee80211_vif_release_channel(sdata);

1054
	return 0;
1055
1056
}

1057
1058
1059
1060
1061
1062
1063
1064
1065
/* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
struct iapp_layer2_update {
	u8 da[ETH_ALEN];	/* broadcast */
	u8 sa[ETH_ALEN];	/* STA addr */
	__be16 len;		/* 6 */
	u8 dsap;		/* 0 */
	u8 ssap;		/* 0 */
	u8 control;
	u8 xid_info[3];
Eric Dumazet's avatar
Eric Dumazet committed
1066
} __packed;
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083

static void ieee80211_send_layer2_update(struct sta_info *sta)
{
	struct iapp_layer2_update *msg;
	struct sk_buff *skb;

	/* Send Level 2 Update Frame to update forwarding tables in layer 2
	 * bridge devices */

	skb = dev_alloc_skb(sizeof(*msg));
	if (!skb)
		return;
	msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));

	/* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
	 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */

1084
	eth_broadcast_addr(msg->da);
1085
	memcpy(msg->sa, sta->sta.addr, ETH_ALEN);
1086
1087
1088
1089
1090
1091
1092
1093
1094
	msg->len = htons(6);
	msg->dsap = 0;
	msg->ssap = 0x01;	/* NULL LSAP, CR Bit: Response */
	msg->control = 0xaf;	/* XID response lsb.1111F101.
				 * F=0 (no poll command;