nl80211.c 114 KB
Newer Older
1
2
3
/*
 * This is the new netlink-based wireless configuration interface.
 *
4
 * Copyright 2006-2009	Johannes Berg <johannes@sipsolutions.net>
5
6
7
8
9
10
11
12
13
14
15
 */

#include <linux/if.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/list.h>
#include <linux/if_ether.h>
#include <linux/ieee80211.h>
#include <linux/nl80211.h>
#include <linux/rtnetlink.h>
#include <linux/netlink.h>
16
#include <linux/etherdevice.h>
17
#include <net/net_namespace.h>
18
19
#include <net/genetlink.h>
#include <net/cfg80211.h>
20
#include <net/sock.h>
21
22
#include "core.h"
#include "nl80211.h"
23
#include "reg.h"
24
25
26
27
28
29
30
31

/* the netlink family */
static struct genl_family nl80211_fam = {
	.id = GENL_ID_GENERATE,	/* don't bother with a hardcoded ID */
	.name = "nl80211",	/* have users key off the name instead */
	.hdrsize = 0,		/* no private header */
	.version = 1,		/* no particular meaning now */
	.maxattr = NL80211_ATTR_MAX,
32
	.netnsok = true,
33
34
};

35
/* internal helper: get rdev and dev */
36
static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
37
				       struct cfg80211_registered_device **rdev,
38
39
				       struct net_device **dev)
{
40
	struct nlattr **attrs = info->attrs;
41
42
	int ifindex;

Johannes Berg's avatar
Johannes Berg committed
43
	if (!attrs[NL80211_ATTR_IFINDEX])
44
45
		return -EINVAL;

Johannes Berg's avatar
Johannes Berg committed
46
	ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
47
	*dev = dev_get_by_index(genl_info_net(info), ifindex);
48
49
50
	if (!*dev)
		return -ENODEV;

51
	*rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
52
	if (IS_ERR(*rdev)) {
53
		dev_put(*dev);
54
		return PTR_ERR(*rdev);
55
56
57
58
59
60
61
62
63
	}

	return 0;
}

/* policy for the attributes */
static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
64
				      .len = 20-1 },
65
	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
66
	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith's avatar
Sujith committed
67
	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
68
69
70
71
	[NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
	[NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
72
73
74
75

	[NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
76
77

	[NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
78
	[NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
79

80
	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
81
82
83
84
85
	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
				    .len = WLAN_MAX_KEY_LEN },
	[NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
86
	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
87
88
89
90
91
92
93

	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
	[NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
				       .len = IEEE80211_MAX_DATA_LEN },
	[NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
				       .len = IEEE80211_MAX_DATA_LEN },
94
95
96
97
98
	[NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
					       .len = NL80211_MAX_SUPP_RATES },
99
	[NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
100
	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg's avatar
Johannes Berg committed
101
	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
102
103
104
	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
				.len = IEEE80211_MAX_MESH_ID_LEN },
	[NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
105

106
107
108
	[NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },

109
110
111
	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
112
113
	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
					   .len = NL80211_MAX_SUPP_RATES },
114

115
116
	[NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },

117
118
	[NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
					 .len = NL80211_HT_CAPABILITY_LEN },
119
120
121
122

	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
	[NL80211_ATTR_IE] = { .type = NLA_BINARY,
			      .len = IEEE80211_MAX_DATA_LEN },
123
124
	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
125
126
127
128
129

	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
				.len = IEEE80211_MAX_SSID_LEN },
	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg's avatar
Johannes Berg committed
130
	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
131
	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
132
	[NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
133
134
135
	[NL80211_ATTR_STA_FLAGS2] = {
		.len = sizeof(struct nl80211_sta_flag_update),
	},
136
	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
137
138
139
	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
140
	[NL80211_ATTR_PID] = { .type = NLA_U32 },
141
142
};

143
144
145
/* policy for the attributes */
static struct nla_policy
nl80211_key_policy[NL80211_KEY_MAX + 1] __read_mostly = {
Johannes Berg's avatar
Johannes Berg committed
146
	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
147
148
149
150
151
152
153
	[NL80211_KEY_IDX] = { .type = NLA_U8 },
	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
};

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* IE validation */
static bool is_valid_ie_attr(const struct nlattr *attr)
{
	const u8 *pos;
	int len;

	if (!attr)
		return true;

	pos = nla_data(attr);
	len = nla_len(attr);

	while (len) {
		u8 elemlen;

		if (len < 2)
			return false;
		len -= 2;

		elemlen = pos[1];
		if (elemlen > len)
			return false;

		len -= elemlen;
		pos += 2 + elemlen;
	}

	return true;
}

184
185
186
187
188
189
190
191
/* message building helper */
static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
				   int flags, u8 cmd)
{
	/* since there is no private header just add the generic one */
	return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
}

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
static int nl80211_msg_put_channel(struct sk_buff *msg,
				   struct ieee80211_channel *chan)
{
	NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
		    chan->center_freq);

	if (chan->flags & IEEE80211_CHAN_DISABLED)
		NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
	if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
		NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
	if (chan->flags & IEEE80211_CHAN_NO_IBSS)
		NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
	if (chan->flags & IEEE80211_CHAN_RADAR)
		NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);

	NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
		    DBM_TO_MBM(chan->max_power));

	return 0;

 nla_put_failure:
	return -ENOBUFS;
}

216
217
/* netlink command implementations */

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
struct key_parse {
	struct key_params p;
	int idx;
	bool def, defmgmt;
};

static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
{
	struct nlattr *tb[NL80211_KEY_MAX + 1];
	int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
				   nl80211_key_policy);
	if (err)
		return err;

	k->def = !!tb[NL80211_KEY_DEFAULT];
	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];

	if (tb[NL80211_KEY_IDX])
		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);

	if (tb[NL80211_KEY_DATA]) {
		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
	}

	if (tb[NL80211_KEY_SEQ]) {
		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
	}

	if (tb[NL80211_KEY_CIPHER])
		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);

	return 0;
}

static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
{
	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
	}

	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
	}

	if (info->attrs[NL80211_ATTR_KEY_IDX])
		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);

	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);

	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];

	return 0;
}

static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
{
	int err;

	memset(k, 0, sizeof(*k));
	k->idx = -1;

	if (info->attrs[NL80211_ATTR_KEY])
		err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
	else
		err = nl80211_parse_key_old(info, k);

	if (err)
		return err;

	if (k->def && k->defmgmt)
		return -EINVAL;

	if (k->idx != -1) {
		if (k->defmgmt) {
			if (k->idx < 4 || k->idx > 5)
				return -EINVAL;
		} else if (k->def) {
			if (k->idx < 0 || k->idx > 3)
				return -EINVAL;
		} else {
			if (k->idx < 0 || k->idx > 5)
				return -EINVAL;
		}
	}

	return 0;
}

Johannes Berg's avatar
Johannes Berg committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
static struct cfg80211_cached_keys *
nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
		       struct nlattr *keys)
{
	struct key_parse parse;
	struct nlattr *key;
	struct cfg80211_cached_keys *result;
	int rem, err, def = 0;

	result = kzalloc(sizeof(*result), GFP_KERNEL);
	if (!result)
		return ERR_PTR(-ENOMEM);

	result->def = -1;
	result->defmgmt = -1;

	nla_for_each_nested(key, keys, rem) {
		memset(&parse, 0, sizeof(parse));
		parse.idx = -1;

		err = nl80211_parse_key_new(key, &parse);
		if (err)
			goto error;
		err = -EINVAL;
		if (!parse.p.key)
			goto error;
		if (parse.idx < 0 || parse.idx > 4)
			goto error;
		if (parse.def) {
			if (def)
				goto error;
			def = 1;
			result->def = parse.idx;
		} else if (parse.defmgmt)
			goto error;
		err = cfg80211_validate_key_settings(rdev, &parse.p,
						     parse.idx, NULL);
		if (err)
			goto error;
		result->params[parse.idx].cipher = parse.p.cipher;
		result->params[parse.idx].key_len = parse.p.key_len;
		result->params[parse.idx].key = result->data[parse.idx];
		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
	}

	return result;
 error:
	kfree(result);
	return ERR_PTR(err);
}

static int nl80211_key_allowed(struct wireless_dev *wdev)
{
	ASSERT_WDEV_LOCK(wdev);

	if (!netif_running(wdev->netdev))
		return -ENETDOWN;

	switch (wdev->iftype) {
	case NL80211_IFTYPE_AP:
	case NL80211_IFTYPE_AP_VLAN:
		break;
	case NL80211_IFTYPE_ADHOC:
		if (!wdev->current_bss)
			return -ENOLINK;
		break;
	case NL80211_IFTYPE_STATION:
		if (wdev->sme_state != CFG80211_SME_CONNECTED)
			return -ENOLINK;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

389
390
391
392
static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
			      struct cfg80211_registered_device *dev)
{
	void *hdr;
393
394
395
	struct nlattr *nl_bands, *nl_band;
	struct nlattr *nl_freqs, *nl_freq;
	struct nlattr *nl_rates, *nl_rate;
396
	struct nlattr *nl_modes;
397
	struct nlattr *nl_cmds;
398
399
400
401
	enum ieee80211_band band;
	struct ieee80211_channel *chan;
	struct ieee80211_rate *rate;
	int i;
402
	u16 ifmodes = dev->wiphy.interface_modes;
403
404
405
406
407

	hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
	if (!hdr)
		return -1;

408
	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
409
	NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
410

411
412
413
	NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
		    cfg80211_rdev_list_generation);

414
415
416
417
418
419
420
421
422
	NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
		   dev->wiphy.retry_short);
	NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
		   dev->wiphy.retry_long);
	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
		    dev->wiphy.frag_threshold);
	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
		    dev->wiphy.rts_threshold);

423
424
	NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
		   dev->wiphy.max_scan_ssids);
425
426
	NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
		    dev->wiphy.max_scan_ie_len);
427

428
429
430
431
	NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
		sizeof(u32) * dev->wiphy.n_cipher_suites,
		dev->wiphy.cipher_suites);

432
433
434
435
436
437
438
439
440
441
442
443
444
445
	nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
	if (!nl_modes)
		goto nla_put_failure;

	i = 0;
	while (ifmodes) {
		if (ifmodes & 1)
			NLA_PUT_FLAG(msg, i);
		ifmodes >>= 1;
		i++;
	}

	nla_nest_end(msg, nl_modes);

446
447
448
449
450
451
452
453
454
455
456
457
	nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
	if (!nl_bands)
		goto nla_put_failure;

	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
		if (!dev->wiphy.bands[band])
			continue;

		nl_band = nla_nest_start(msg, band);
		if (!nl_band)
			goto nla_put_failure;

458
459
460
461
462
463
464
465
466
467
468
469
470
		/* add HT info */
		if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
			NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
				sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
				&dev->wiphy.bands[band]->ht_cap.mcs);
			NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
				dev->wiphy.bands[band]->ht_cap.cap);
			NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
				dev->wiphy.bands[band]->ht_cap.ampdu_factor);
			NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
				dev->wiphy.bands[band]->ht_cap.ampdu_density);
		}

471
472
473
474
475
476
477
478
479
480
481
		/* add frequencies */
		nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
		if (!nl_freqs)
			goto nla_put_failure;

		for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
			nl_freq = nla_nest_start(msg, i);
			if (!nl_freq)
				goto nla_put_failure;

			chan = &dev->wiphy.bands[band]->channels[i];
482
483
484

			if (nl80211_msg_put_channel(msg, chan))
				goto nla_put_failure;
485

486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
			nla_nest_end(msg, nl_freq);
		}

		nla_nest_end(msg, nl_freqs);

		/* add bitrates */
		nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
		if (!nl_rates)
			goto nla_put_failure;

		for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
			nl_rate = nla_nest_start(msg, i);
			if (!nl_rate)
				goto nla_put_failure;

			rate = &dev->wiphy.bands[band]->bitrates[i];
			NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
				    rate->bitrate);
			if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
				NLA_PUT_FLAG(msg,
					NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);

			nla_nest_end(msg, nl_rate);
		}

		nla_nest_end(msg, nl_rates);

		nla_nest_end(msg, nl_band);
	}
	nla_nest_end(msg, nl_bands);

517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
	nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
	if (!nl_cmds)
		goto nla_put_failure;

	i = 0;
#define CMD(op, n)						\
	 do {							\
		if (dev->ops->op) {				\
			i++;					\
			NLA_PUT_U32(msg, i, NL80211_CMD_ ## n);	\
		}						\
	} while (0)

	CMD(add_virtual_intf, NEW_INTERFACE);
	CMD(change_virtual_intf, SET_INTERFACE);
	CMD(add_key, NEW_KEY);
	CMD(add_beacon, NEW_BEACON);
	CMD(add_station, NEW_STATION);
	CMD(add_mpath, NEW_MPATH);
	CMD(set_mesh_params, SET_MESH_PARAMS);
	CMD(change_bss, SET_BSS);
538
539
540
541
	CMD(auth, AUTHENTICATE);
	CMD(assoc, ASSOCIATE);
	CMD(deauth, DEAUTHENTICATE);
	CMD(disassoc, DISASSOCIATE);
Johannes Berg's avatar
Johannes Berg committed
542
	CMD(join_ibss, JOIN_IBSS);
543
544
545
546
	if (dev->wiphy.netnsok) {
		i++;
		NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
	}
547
548

#undef CMD
549

550
	if (dev->ops->connect || dev->ops->auth) {
551
552
553
554
		i++;
		NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
	}

555
	if (dev->ops->disconnect || dev->ops->deauth) {
556
557
558
559
		i++;
		NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
	}

560
561
	nla_nest_end(msg, nl_cmds);

562
563
564
	return genlmsg_end(msg, hdr);

 nla_put_failure:
565
566
	genlmsg_cancel(msg, hdr);
	return -EMSGSIZE;
567
568
569
570
571
572
573
574
}

static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
{
	int idx = 0;
	int start = cb->args[0];
	struct cfg80211_registered_device *dev;

575
	mutex_lock(&cfg80211_mutex);
576
	list_for_each_entry(dev, &cfg80211_rdev_list, list) {
577
578
		if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
			continue;
579
		if (++idx <= start)
580
581
582
			continue;
		if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
583
584
				       dev) < 0) {
			idx--;
585
			break;
586
		}
587
	}
588
	mutex_unlock(&cfg80211_mutex);
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603

	cb->args[0] = idx;

	return skb->len;
}

static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
{
	struct sk_buff *msg;
	struct cfg80211_registered_device *dev;

	dev = cfg80211_get_dev_from_info(info);
	if (IS_ERR(dev))
		return PTR_ERR(dev);

604
	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
605
606
607
608
609
610
	if (!msg)
		goto out_err;

	if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
		goto out_free;

611
	cfg80211_unlock_rdev(dev);
612

Johannes Berg's avatar
Johannes Berg committed
613
	return genlmsg_reply(msg, info);
614
615
616
617

 out_free:
	nlmsg_free(msg);
 out_err:
618
	cfg80211_unlock_rdev(dev);
619
620
621
	return -ENOBUFS;
}

622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
};

static int parse_txq_params(struct nlattr *tb[],
			    struct ieee80211_txq_params *txq_params)
{
	if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
	    !tb[NL80211_TXQ_ATTR_AIFS])
		return -EINVAL;

	txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);

	return 0;
}

647
648
649
static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
{
	struct cfg80211_registered_device *rdev;
650
651
	int result = 0, rem_txq_params = 0;
	struct nlattr *nl_txq_params;
652
653
654
	u32 changed;
	u8 retry_short = 0, retry_long = 0;
	u32 frag_threshold = 0, rts_threshold = 0;
655

656
	rtnl_lock();
657

658
659
	mutex_lock(&cfg80211_mutex);

660
	rdev = __cfg80211_rdev_from_info(info);
661
	if (IS_ERR(rdev)) {
662
		mutex_unlock(&cfg80211_mutex);
663
664
665
666
667
668
669
		result = PTR_ERR(rdev);
		goto unlock;
	}

	mutex_lock(&rdev->mtx);

	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
670
671
		result = cfg80211_dev_rename(
			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
672
673
674
675
676

	mutex_unlock(&cfg80211_mutex);

	if (result)
		goto bad_res;
677
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

	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
		struct ieee80211_txq_params txq_params;
		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];

		if (!rdev->ops->set_txq_params) {
			result = -EOPNOTSUPP;
			goto bad_res;
		}

		nla_for_each_nested(nl_txq_params,
				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
				    rem_txq_params) {
			nla_parse(tb, NL80211_TXQ_ATTR_MAX,
				  nla_data(nl_txq_params),
				  nla_len(nl_txq_params),
				  txq_params_policy);
			result = parse_txq_params(tb, &txq_params);
			if (result)
				goto bad_res;

			result = rdev->ops->set_txq_params(&rdev->wiphy,
							   &txq_params);
			if (result)
				goto bad_res;
		}
	}
704

705
	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith's avatar
Sujith committed
706
		enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
707
		u32 freq;
708

709
710
		result = -EINVAL;

Sujith's avatar
Sujith committed
711
712
713
714
715
716
717
		if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
			channel_type = nla_get_u32(info->attrs[
					   NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
			if (channel_type != NL80211_CHAN_NO_HT &&
			    channel_type != NL80211_CHAN_HT20 &&
			    channel_type != NL80211_CHAN_HT40PLUS &&
			    channel_type != NL80211_CHAN_HT40MINUS)
718
719
720
721
				goto bad_res;
		}

		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
722

723
		mutex_lock(&rdev->devlist_mtx);
724
		result = rdev_set_freq(rdev, NULL, freq, channel_type);
725
		mutex_unlock(&rdev->devlist_mtx);
726
727
728
729
		if (result)
			goto bad_res;
	}

730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
	changed = 0;

	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
		retry_short = nla_get_u8(
			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
		if (retry_short == 0) {
			result = -EINVAL;
			goto bad_res;
		}
		changed |= WIPHY_PARAM_RETRY_SHORT;
	}

	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
		retry_long = nla_get_u8(
			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
		if (retry_long == 0) {
			result = -EINVAL;
			goto bad_res;
		}
		changed |= WIPHY_PARAM_RETRY_LONG;
	}

	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
		frag_threshold = nla_get_u32(
			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
		if (frag_threshold < 256) {
			result = -EINVAL;
			goto bad_res;
		}
		if (frag_threshold != (u32) -1) {
			/*
			 * Fragments (apart from the last one) are required to
			 * have even length. Make the fragmentation code
			 * simpler by stripping LSB should someone try to use
			 * odd threshold value.
			 */
			frag_threshold &= ~0x1;
		}
		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
	}

	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
		rts_threshold = nla_get_u32(
			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
		changed |= WIPHY_PARAM_RTS_THRESHOLD;
	}

	if (changed) {
		u8 old_retry_short, old_retry_long;
		u32 old_frag_threshold, old_rts_threshold;

		if (!rdev->ops->set_wiphy_params) {
			result = -EOPNOTSUPP;
			goto bad_res;
		}

		old_retry_short = rdev->wiphy.retry_short;
		old_retry_long = rdev->wiphy.retry_long;
		old_frag_threshold = rdev->wiphy.frag_threshold;
		old_rts_threshold = rdev->wiphy.rts_threshold;

		if (changed & WIPHY_PARAM_RETRY_SHORT)
			rdev->wiphy.retry_short = retry_short;
		if (changed & WIPHY_PARAM_RETRY_LONG)
			rdev->wiphy.retry_long = retry_long;
		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
			rdev->wiphy.frag_threshold = frag_threshold;
		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
			rdev->wiphy.rts_threshold = rts_threshold;

		result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
		if (result) {
			rdev->wiphy.retry_short = old_retry_short;
			rdev->wiphy.retry_long = old_retry_long;
			rdev->wiphy.frag_threshold = old_frag_threshold;
			rdev->wiphy.rts_threshold = old_rts_threshold;
		}
	}
808

809
 bad_res:
810
811
812
	mutex_unlock(&rdev->mtx);
 unlock:
	rtnl_unlock();
813
814
815
816
817
	return result;
}


static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
818
			      struct cfg80211_registered_device *rdev,
819
820
821
822
823
824
825
826
827
			      struct net_device *dev)
{
	void *hdr;

	hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
	if (!hdr)
		return -1;

	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
828
	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
829
	NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg's avatar
Johannes Berg committed
830
	NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
831
832
833
834
835

	NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
		    rdev->devlist_generation ^
			(cfg80211_rdev_list_generation << 2));

836
837
838
	return genlmsg_end(msg, hdr);

 nla_put_failure:
839
840
	genlmsg_cancel(msg, hdr);
	return -EMSGSIZE;
841
842
843
844
845
846
847
848
}

static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
{
	int wp_idx = 0;
	int if_idx = 0;
	int wp_start = cb->args[0];
	int if_start = cb->args[1];
849
	struct cfg80211_registered_device *rdev;
850
851
	struct wireless_dev *wdev;

852
	mutex_lock(&cfg80211_mutex);
853
854
	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
855
			continue;
Johannes Berg's avatar
Johannes Berg committed
856
857
		if (wp_idx < wp_start) {
			wp_idx++;
858
			continue;
Johannes Berg's avatar
Johannes Berg committed
859
		}
860
861
		if_idx = 0;

862
863
		mutex_lock(&rdev->devlist_mtx);
		list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg's avatar
Johannes Berg committed
864
865
			if (if_idx < if_start) {
				if_idx++;
866
				continue;
Johannes Berg's avatar
Johannes Berg committed
867
			}
868
869
			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
870
871
					       rdev, wdev->netdev) < 0) {
				mutex_unlock(&rdev->devlist_mtx);
Johannes Berg's avatar
Johannes Berg committed
872
873
874
				goto out;
			}
			if_idx++;
875
		}
876
		mutex_unlock(&rdev->devlist_mtx);
Johannes Berg's avatar
Johannes Berg committed
877
878

		wp_idx++;
879
	}
Johannes Berg's avatar
Johannes Berg committed
880
 out:
881
	mutex_unlock(&cfg80211_mutex);
882
883
884
885
886
887
888
889
890
891
892
893
894
895

	cb->args[0] = wp_idx;
	cb->args[1] = if_idx;

	return skb->len;
}

static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
{
	struct sk_buff *msg;
	struct cfg80211_registered_device *dev;
	struct net_device *netdev;
	int err;

896
	err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
897
898
899
	if (err)
		return err;

900
	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
901
902
903
	if (!msg)
		goto out_err;

904
905
	if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
			       dev, netdev) < 0)
906
907
908
		goto out_free;

	dev_put(netdev);
909
	cfg80211_unlock_rdev(dev);
910

Johannes Berg's avatar
Johannes Berg committed
911
	return genlmsg_reply(msg, info);
912
913
914
915
916

 out_free:
	nlmsg_free(msg);
 out_err:
	dev_put(netdev);
917
	cfg80211_unlock_rdev(dev);
918
919
920
	return -ENOBUFS;
}

921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
};

static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
{
	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
	int flag;

	*mntrflags = 0;

	if (!nla)
		return -EINVAL;

	if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
			     nla, mntr_flags_policy))
		return -EINVAL;

	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
		if (flags[flag])
			*mntrflags |= (1<<flag);

	return 0;
}

950
951
static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
{
952
	struct cfg80211_registered_device *rdev;
953
	struct vif_params params;
954
	int err;
Johannes Berg's avatar
Johannes Berg committed
955
	enum nl80211_iftype otype, ntype;
956
	struct net_device *dev;
957
	u32 _flags, *flags = NULL;
958
	bool change = false;
959

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

Johannes Berg's avatar
Johannes Berg committed
962
963
	rtnl_lock();

964
	err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
965
	if (err)
Johannes Berg's avatar
Johannes Berg committed
966
967
		goto unlock_rtnl;

Johannes Berg's avatar
Johannes Berg committed
968
	otype = ntype = dev->ieee80211_ptr->iftype;
969

970
	if (info->attrs[NL80211_ATTR_IFTYPE]) {
971
		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg's avatar
Johannes Berg committed
972
		if (otype != ntype)
973
			change = true;
Johannes Berg's avatar
Johannes Berg committed
974
		if (ntype > NL80211_IFTYPE_MAX) {
975
			err = -EINVAL;
976
			goto unlock;
977
		}
978
979
	}

980
	if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg's avatar
Johannes Berg committed
981
		if (ntype != NL80211_IFTYPE_MESH_POINT) {
982
983
984
			err = -EINVAL;
			goto unlock;
		}
985
986
		params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
		params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
987
		change = true;
988
989
	}

990
	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg's avatar
Johannes Berg committed
991
		if (ntype != NL80211_IFTYPE_MONITOR) {
992
993
994
995
996
			err = -EINVAL;
			goto unlock;
		}
		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
					  &_flags);
997
998
999
1000
1001
		if (err)
			goto unlock;

		flags = &_flags;
		change = true;
1002
	}
Johannes Berg's avatar
Johannes Berg committed
1003

1004
	if (change)
1005
		err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
1006
1007
	else
		err = 0;
Johannes Berg's avatar
Johannes Berg committed
1008

1009
 unlock:
1010
	dev_put(dev);
1011
	cfg80211_unlock_rdev(rdev);
Johannes Berg's avatar
Johannes Berg committed
1012
1013
 unlock_rtnl:
	rtnl_unlock();
1014
1015
1016
1017
1018
	return err;
}

static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
{
1019
	struct cfg80211_registered_device *rdev;
1020
	struct vif_params params;
1021
1022
	int err;
	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1023
	u32 flags;
1024

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

1027
1028
1029
1030
1031
1032
1033
1034
1035
	if (!info->attrs[NL80211_ATTR_IFNAME])
		return -EINVAL;

	if (info->attrs[NL80211_ATTR_IFTYPE]) {
		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
		if (type > NL80211_IFTYPE_MAX)
			return -EINVAL;
	}

Johannes Berg's avatar
Johannes Berg committed
1036
1037
	rtnl_lock();

1038
1039
1040
	rdev = cfg80211_get_dev_from_info(info);
	if (IS_ERR(rdev)) {
		err = PTR_ERR(rdev);
Johannes Berg's avatar
Johannes Berg committed
1041
1042
		goto unlock_rtnl;
	}
1043

1044
1045
	if (!rdev->ops->add_virtual_intf ||
	    !(rdev->wiphy.interface_modes & (1 << type))) {
1046
1047
1048
1049
		err = -EOPNOTSUPP;
		goto unlock;
	}

1050
1051
1052
1053
1054
1055
	if (type == NL80211_IFTYPE_MESH_POINT &&
	    info->attrs[NL80211_ATTR_MESH_ID]) {
		params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
		params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
	}

1056
1057
1058
	err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
				  info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
				  &flags);
1059
	err = rdev->ops->add_virtual_intf(&rdev->wiphy,
1060
		nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1061
1062
		type, err ? NULL : &flags, &params);

1063
 unlock:
1064
	cfg80211_unlock_rdev(rdev);
Johannes Berg's avatar
Johannes Berg committed
1065
1066
 unlock_rtnl:
	rtnl_unlock();
1067
1068
1069
1070
1071
	return err;
}

static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
{
1072
	struct cfg80211_registered_device *rdev;
1073
	int err;
1074
1075
	struct net_device *dev;

Johannes Berg's avatar
Johannes Berg committed
1076
1077
	rtnl_lock();

1078
	err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1079
	if (err)
Johannes Berg's avatar
Johannes Berg committed
1080
		goto unlock_rtnl;
1081

1082
	if (!rdev->ops->del_virtual_intf) {
1083
1084
1085
1086
		err = -EOPNOTSUPP;
		goto out;
	}

1087
	err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1088
1089

 out:
1090
	cfg80211_unlock_rdev(rdev);
1091
	dev_put(dev);
Johannes Berg's avatar
Johannes Berg committed
1092
1093
 unlock_rtnl:
	rtnl_unlock();
1094
1095
1096
	return err;
}

1097
1098
1099
struct get_key_cookie {
	struct sk_buff *msg;
	int error;
1100
	int idx;
1101
1102
1103
1104
};

static void get_key_callback(void *c, struct key_params *params)
{
1105
	struct nlattr *key;
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
	struct get_key_cookie *cookie = c;

	if (params->key)
		NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
			params->key_len, params->key);

	if (params->seq)
		NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
			params->seq_len, params->seq);

	if (params->cipher)
		NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
			    params->cipher);

1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
	key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
	if (!key)
		goto nla_put_failure;

	if (params->key)
		NLA_PUT(cookie->msg, NL80211_KEY_DATA,
			params->key_len, params->key);

	if (params->seq)
		NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
			params->seq_len, params->seq);

	if (params->cipher)
		NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
			    params->cipher);

	NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);

	nla_nest_end(cookie->msg, key);

1140
1141
1142
1143
1144
1145
1146
	return;
 nla_put_failure:
	cookie->error = 1;
}

static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
{
1147
	struct cfg80211_registered_device *rdev;
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
	int err;
	struct net_device *dev;
	u8 key_idx = 0;
	u8 *mac_addr = NULL;
	struct get_key_cookie cookie = {
		.error = 0,
	};
	void *hdr;
	struct sk_buff *msg;

	if (info->attrs[NL80211_ATTR_KEY_IDX])
		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);

1161
	if (key_idx > 5)
1162
1163
1164
1165
1166
		return -EINVAL;

	if (info->attrs[NL80211_ATTR_MAC])
		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);

Johannes Berg's avatar
Johannes Berg committed
1167
1168
	rtnl_lock();

1169
	err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1170
	if (err)
Johannes Berg's avatar
Johannes Berg committed
1171
		goto unlock_rtnl;
1172

1173
	if (!rdev->ops->get_key) {
1174
1175
1176
1177
		err = -EOPNOTSUPP;
		goto out;
	}

1178
	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
	if (!msg) {
		err = -ENOMEM;
		goto out;
	}

	hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
			     NL80211_CMD_NEW_KEY);

	if (IS_ERR(hdr)) {
		err = PTR_ERR(hdr);
Niko Jokinen's avatar
Niko Jokinen committed
1189
		goto free_msg;
1190
1191
1192
	}

	cookie.msg = msg;
1193
	cookie.idx = key_idx;
1194
1195
1196
1197
1198
1199

	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
	NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
	if (mac_addr)
		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);

1200
	err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
1201
1202
1203
				&cookie, get_key_callback);

	if (err)
Niko Jokinen's avatar
Niko Jokinen committed
1204
		goto free_msg;
1205
1206
1207
1208
1209

	if (cookie.error)
		goto nla_put_failure;

	genlmsg_end(msg, hdr);
Johannes Berg's avatar
Johannes Berg committed
1210
	err = genlmsg_reply(msg, info);
1211
1212
1213
1214
	goto out;

 nla_put_failure:
	err = -ENOBUFS;
Niko Jokinen's avatar
Niko Jokinen committed
1215
 free_msg:
1216
1217
	nlmsg_free(msg);
 out:
1218
	cfg80211_unlock_rdev(rdev);
1219
	dev_put(dev);
Johannes Berg's avatar
Johannes Berg committed
1220
1221
1222
 unlock_rtnl:
	rtnl_unlock();

1223
1224
1225
1226
1227
	return err;
}

static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
{
1228
	struct cfg80211_registered_device *rdev;
1229
	struct key_parse key;
1230
1231
	int err;
	struct net_device *dev;
1232
1233
	int (*func)(struct wiphy *wiphy, struct net_device *netdev,
		    u8 key_index);
1234

1235
1236
1237
	err = nl80211_parse_key(info, &key);
	if (err)
		return err;
1238

1239
	if (key.idx < 0)
1240
1241
		return -EINVAL;

1242
1243
	/* only support setting default key */
	if (!key.def && !key.defmgmt)
1244
1245
		return -EINVAL;

Johannes Berg's avatar
Johannes Berg committed
1246
1247
	rtnl_lock();

1248
	err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1249
	if (err)
Johannes Berg's avatar
Johannes Berg committed
1250
		goto unlock_rtnl;
1251

1252
	if (key.def)
1253
		func = rdev->ops->set_default_key;
1254
	else
1255
		func = rdev->ops->set_default_mgmt_key;
1256
1257

	if (!func) {
1258
1259
1260
1261
		err = -EOPNOTSUPP;
		goto out;
	}

Johannes Berg's avatar
Johannes Berg committed
1262
1263
1264
1265
1266
	wdev_lock(dev->ieee80211_ptr);
	err = nl80211_key_allowed(dev->ieee80211_ptr);
	if (!err)
		err = func(&rdev->wiphy, dev, key.idx);

Johannes Berg's avatar
Johannes Berg committed
1267
#ifdef CONFIG_CFG80211_WEXT
1268
	if (!err) {
1269
		if (func == rdev->ops->set_default_key)
1270
			dev->ieee80211_ptr->wext.default_key = key.idx;
1271
		else
1272
			dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1273
1274
	}
#endif
Johannes Berg's avatar
Johannes Berg committed
1275
	wdev_unlock(dev->ieee80211_ptr);
1276
1277

 out:
1278
	cfg80211_unlock_rdev(rdev);
1279
	dev_put(dev);
Johannes Berg's avatar
Johannes Berg committed
1280
1281
1282
1283

 unlock_rtnl:
	rtnl_unlock();

1284
1285
1286
1287
1288
	return err;
}

static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
{
1289
	struct cfg80211_registered_device *rdev;
Johannes Berg's avatar
Johannes Berg committed
1290
	int err;
1291
	struct net_device *dev;
1292
	struct key_parse key;
1293
1294
	u8 *mac_addr = NULL;

1295
1296
1297
	err = nl80211_parse_key(info, &key);
	if (err)
		return err;
1298

1299
	if (!key.p.key)
1300
1301
1302
1303
1304
		return -EINVAL;

	if (info->attrs[NL80211_ATTR_MAC])
		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);

Johannes Berg's avatar
Johannes Berg committed
1305
1306
	rtnl_lock();

1307
	err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1308
	if (err)
Johannes Berg's avatar
Johannes Berg committed
1309
		goto unlock_rtnl;
1310