]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/wireless/nl80211.c
nl80211: Remove NL80211_CMD_SET_MGMT_EXTRA_IE
[mirror_ubuntu-bionic-kernel.git] / net / wireless / nl80211.c
CommitLineData
55682965
JB
1/*
2 * This is the new netlink-based wireless configuration interface.
3 *
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
55682965
JB
10#include <linux/list.h>
11#include <linux/if_ether.h>
12#include <linux/ieee80211.h>
13#include <linux/nl80211.h>
14#include <linux/rtnetlink.h>
15#include <linux/netlink.h>
2a519311 16#include <linux/etherdevice.h>
55682965
JB
17#include <net/genetlink.h>
18#include <net/cfg80211.h>
19#include "core.h"
20#include "nl80211.h"
b2e1b302 21#include "reg.h"
55682965
JB
22
23/* the netlink family */
24static struct genl_family nl80211_fam = {
25 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
26 .name = "nl80211", /* have users key off the name instead */
27 .hdrsize = 0, /* no private header */
28 .version = 1, /* no particular meaning now */
29 .maxattr = NL80211_ATTR_MAX,
30};
31
32/* internal helper: get drv and dev */
bba95fef 33static int get_drv_dev_by_info_ifindex(struct nlattr **attrs,
55682965
JB
34 struct cfg80211_registered_device **drv,
35 struct net_device **dev)
36{
37 int ifindex;
38
bba95fef 39 if (!attrs[NL80211_ATTR_IFINDEX])
55682965
JB
40 return -EINVAL;
41
bba95fef 42 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
55682965
JB
43 *dev = dev_get_by_index(&init_net, ifindex);
44 if (!*dev)
45 return -ENODEV;
46
47 *drv = cfg80211_get_dev_from_ifindex(ifindex);
48 if (IS_ERR(*drv)) {
49 dev_put(*dev);
50 return PTR_ERR(*drv);
51 }
52
53 return 0;
54}
55
56/* policy for the attributes */
57static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
58 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
59 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
60 .len = BUS_ID_SIZE-1 },
31888487 61 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
72bdcf34 62 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
094d05dc 63 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
55682965
JB
64
65 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
66 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
67 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
41ade00f
JB
68
69 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
70
71 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
72 .len = WLAN_MAX_KEY_LEN },
73 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
74 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
75 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
ed1b6cc7
JB
76
77 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
78 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
79 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
80 .len = IEEE80211_MAX_DATA_LEN },
81 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
82 .len = IEEE80211_MAX_DATA_LEN },
5727ef1b
JB
83 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
84 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
85 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
86 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
87 .len = NL80211_MAX_SUPP_RATES },
2ec600d6 88 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
5727ef1b 89 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
0a9542ee 90 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
2ec600d6
LCC
91 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
92 .len = IEEE80211_MAX_MESH_ID_LEN },
93 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
9f1ba906 94
b2e1b302
LR
95 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
96 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
97
9f1ba906
JM
98 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
99 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
100 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
90c97a04
JM
101 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
102 .len = NL80211_MAX_SUPP_RATES },
36aedc90 103
93da9cc1 104 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
105
36aedc90
JM
106 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
107 .len = NL80211_HT_CAPABILITY_LEN },
9aed3cc1
JM
108
109 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
110 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
111 .len = IEEE80211_MAX_DATA_LEN },
2a519311
JB
112 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
113 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
636a5d36
JM
114
115 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
116 .len = IEEE80211_MAX_SSID_LEN },
117 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
118 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
55682965
JB
119};
120
121/* message building helper */
122static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
123 int flags, u8 cmd)
124{
125 /* since there is no private header just add the generic one */
126 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
127}
128
129/* netlink command implementations */
130
131static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
132 struct cfg80211_registered_device *dev)
133{
134 void *hdr;
ee688b00
JB
135 struct nlattr *nl_bands, *nl_band;
136 struct nlattr *nl_freqs, *nl_freq;
137 struct nlattr *nl_rates, *nl_rate;
f59ac048 138 struct nlattr *nl_modes;
8fdc621d 139 struct nlattr *nl_cmds;
ee688b00
JB
140 enum ieee80211_band band;
141 struct ieee80211_channel *chan;
142 struct ieee80211_rate *rate;
143 int i;
f59ac048 144 u16 ifmodes = dev->wiphy.interface_modes;
55682965
JB
145
146 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
147 if (!hdr)
148 return -1;
149
b5850a7a 150 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
55682965 151 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
2a519311
JB
152 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
153 dev->wiphy.max_scan_ssids);
ee688b00 154
f59ac048
LR
155 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
156 if (!nl_modes)
157 goto nla_put_failure;
158
159 i = 0;
160 while (ifmodes) {
161 if (ifmodes & 1)
162 NLA_PUT_FLAG(msg, i);
163 ifmodes >>= 1;
164 i++;
165 }
166
167 nla_nest_end(msg, nl_modes);
168
ee688b00
JB
169 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
170 if (!nl_bands)
171 goto nla_put_failure;
172
173 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
174 if (!dev->wiphy.bands[band])
175 continue;
176
177 nl_band = nla_nest_start(msg, band);
178 if (!nl_band)
179 goto nla_put_failure;
180
d51626df
JB
181 /* add HT info */
182 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
183 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
184 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
185 &dev->wiphy.bands[band]->ht_cap.mcs);
186 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
187 dev->wiphy.bands[band]->ht_cap.cap);
188 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
189 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
190 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
191 dev->wiphy.bands[band]->ht_cap.ampdu_density);
192 }
193
ee688b00
JB
194 /* add frequencies */
195 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
196 if (!nl_freqs)
197 goto nla_put_failure;
198
199 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
200 nl_freq = nla_nest_start(msg, i);
201 if (!nl_freq)
202 goto nla_put_failure;
203
204 chan = &dev->wiphy.bands[band]->channels[i];
205 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
206 chan->center_freq);
207
208 if (chan->flags & IEEE80211_CHAN_DISABLED)
209 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
210 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
211 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
212 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
213 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
214 if (chan->flags & IEEE80211_CHAN_RADAR)
215 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
216
bf8c1ac6
JM
217 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
218 DBM_TO_MBM(chan->max_power));
e2f367f2 219
ee688b00
JB
220 nla_nest_end(msg, nl_freq);
221 }
222
223 nla_nest_end(msg, nl_freqs);
224
225 /* add bitrates */
226 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
227 if (!nl_rates)
228 goto nla_put_failure;
229
230 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
231 nl_rate = nla_nest_start(msg, i);
232 if (!nl_rate)
233 goto nla_put_failure;
234
235 rate = &dev->wiphy.bands[band]->bitrates[i];
236 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
237 rate->bitrate);
238 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
239 NLA_PUT_FLAG(msg,
240 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
241
242 nla_nest_end(msg, nl_rate);
243 }
244
245 nla_nest_end(msg, nl_rates);
246
247 nla_nest_end(msg, nl_band);
248 }
249 nla_nest_end(msg, nl_bands);
250
8fdc621d
JB
251 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
252 if (!nl_cmds)
253 goto nla_put_failure;
254
255 i = 0;
256#define CMD(op, n) \
257 do { \
258 if (dev->ops->op) { \
259 i++; \
260 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
261 } \
262 } while (0)
263
264 CMD(add_virtual_intf, NEW_INTERFACE);
265 CMD(change_virtual_intf, SET_INTERFACE);
266 CMD(add_key, NEW_KEY);
267 CMD(add_beacon, NEW_BEACON);
268 CMD(add_station, NEW_STATION);
269 CMD(add_mpath, NEW_MPATH);
270 CMD(set_mesh_params, SET_MESH_PARAMS);
271 CMD(change_bss, SET_BSS);
636a5d36
JM
272 CMD(auth, AUTHENTICATE);
273 CMD(assoc, ASSOCIATE);
274 CMD(deauth, DEAUTHENTICATE);
275 CMD(disassoc, DISASSOCIATE);
8fdc621d
JB
276
277#undef CMD
278 nla_nest_end(msg, nl_cmds);
279
55682965
JB
280 return genlmsg_end(msg, hdr);
281
282 nla_put_failure:
bc3ed28c
TG
283 genlmsg_cancel(msg, hdr);
284 return -EMSGSIZE;
55682965
JB
285}
286
287static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
288{
289 int idx = 0;
290 int start = cb->args[0];
291 struct cfg80211_registered_device *dev;
292
a1794390 293 mutex_lock(&cfg80211_mutex);
55682965 294 list_for_each_entry(dev, &cfg80211_drv_list, list) {
b4637271 295 if (++idx <= start)
55682965
JB
296 continue;
297 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
298 cb->nlh->nlmsg_seq, NLM_F_MULTI,
b4637271
JV
299 dev) < 0) {
300 idx--;
55682965 301 break;
b4637271 302 }
55682965 303 }
a1794390 304 mutex_unlock(&cfg80211_mutex);
55682965
JB
305
306 cb->args[0] = idx;
307
308 return skb->len;
309}
310
311static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
312{
313 struct sk_buff *msg;
314 struct cfg80211_registered_device *dev;
315
316 dev = cfg80211_get_dev_from_info(info);
317 if (IS_ERR(dev))
318 return PTR_ERR(dev);
319
320 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
321 if (!msg)
322 goto out_err;
323
324 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
325 goto out_free;
326
327 cfg80211_put_dev(dev);
328
329 return genlmsg_unicast(msg, info->snd_pid);
330
331 out_free:
332 nlmsg_free(msg);
333 out_err:
334 cfg80211_put_dev(dev);
335 return -ENOBUFS;
336}
337
31888487
JM
338static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
339 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
340 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
341 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
342 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
343 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
344};
345
346static int parse_txq_params(struct nlattr *tb[],
347 struct ieee80211_txq_params *txq_params)
348{
349 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
350 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
351 !tb[NL80211_TXQ_ATTR_AIFS])
352 return -EINVAL;
353
354 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
355 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
356 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
357 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
358 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
359
360 return 0;
361}
362
55682965
JB
363static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
364{
365 struct cfg80211_registered_device *rdev;
31888487
JM
366 int result = 0, rem_txq_params = 0;
367 struct nlattr *nl_txq_params;
55682965
JB
368
369 rdev = cfg80211_get_dev_from_info(info);
370 if (IS_ERR(rdev))
371 return PTR_ERR(rdev);
372
31888487
JM
373 if (info->attrs[NL80211_ATTR_WIPHY_NAME]) {
374 result = cfg80211_dev_rename(
375 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
376 if (result)
377 goto bad_res;
378 }
379
380 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
381 struct ieee80211_txq_params txq_params;
382 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
383
384 if (!rdev->ops->set_txq_params) {
385 result = -EOPNOTSUPP;
386 goto bad_res;
387 }
388
389 nla_for_each_nested(nl_txq_params,
390 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
391 rem_txq_params) {
392 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
393 nla_data(nl_txq_params),
394 nla_len(nl_txq_params),
395 txq_params_policy);
396 result = parse_txq_params(tb, &txq_params);
397 if (result)
398 goto bad_res;
399
400 result = rdev->ops->set_txq_params(&rdev->wiphy,
401 &txq_params);
402 if (result)
403 goto bad_res;
404 }
405 }
55682965 406
72bdcf34 407 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
094d05dc 408 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
72bdcf34 409 struct ieee80211_channel *chan;
306d6112 410 struct ieee80211_sta_ht_cap *ht_cap;
72bdcf34
JM
411 u32 freq, sec_freq;
412
413 if (!rdev->ops->set_channel) {
414 result = -EOPNOTSUPP;
415 goto bad_res;
416 }
417
306d6112
JB
418 result = -EINVAL;
419
094d05dc
S
420 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
421 channel_type = nla_get_u32(info->attrs[
422 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
423 if (channel_type != NL80211_CHAN_NO_HT &&
424 channel_type != NL80211_CHAN_HT20 &&
425 channel_type != NL80211_CHAN_HT40PLUS &&
426 channel_type != NL80211_CHAN_HT40MINUS)
72bdcf34 427 goto bad_res;
72bdcf34
JM
428 }
429
430 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
431 chan = ieee80211_get_channel(&rdev->wiphy, freq);
306d6112
JB
432
433 /* Primary channel not allowed */
434 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
72bdcf34 435 goto bad_res;
306d6112 436
094d05dc 437 if (channel_type == NL80211_CHAN_HT40MINUS)
72bdcf34 438 sec_freq = freq - 20;
094d05dc 439 else if (channel_type == NL80211_CHAN_HT40PLUS)
72bdcf34
JM
440 sec_freq = freq + 20;
441 else
442 sec_freq = 0;
443
306d6112
JB
444 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
445
446 /* no HT capabilities */
094d05dc 447 if (channel_type != NL80211_CHAN_NO_HT &&
306d6112
JB
448 !ht_cap->ht_supported)
449 goto bad_res;
450
72bdcf34
JM
451 if (sec_freq) {
452 struct ieee80211_channel *schan;
306d6112
JB
453
454 /* no 40 MHz capabilities */
455 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
456 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
457 goto bad_res;
458
72bdcf34 459 schan = ieee80211_get_channel(&rdev->wiphy, sec_freq);
306d6112
JB
460
461 /* Secondary channel not allowed */
462 if (!schan || schan->flags & IEEE80211_CHAN_DISABLED)
72bdcf34 463 goto bad_res;
72bdcf34
JM
464 }
465
466 result = rdev->ops->set_channel(&rdev->wiphy, chan,
094d05dc 467 channel_type);
72bdcf34
JM
468 if (result)
469 goto bad_res;
470 }
471
472
306d6112 473 bad_res:
55682965
JB
474 cfg80211_put_dev(rdev);
475 return result;
476}
477
478
479static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
480 struct net_device *dev)
481{
482 void *hdr;
483
484 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
485 if (!hdr)
486 return -1;
487
488 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
489 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
60719ffd 490 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
55682965
JB
491 return genlmsg_end(msg, hdr);
492
493 nla_put_failure:
bc3ed28c
TG
494 genlmsg_cancel(msg, hdr);
495 return -EMSGSIZE;
55682965
JB
496}
497
498static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
499{
500 int wp_idx = 0;
501 int if_idx = 0;
502 int wp_start = cb->args[0];
503 int if_start = cb->args[1];
504 struct cfg80211_registered_device *dev;
505 struct wireless_dev *wdev;
506
a1794390 507 mutex_lock(&cfg80211_mutex);
55682965 508 list_for_each_entry(dev, &cfg80211_drv_list, list) {
bba95fef
JB
509 if (wp_idx < wp_start) {
510 wp_idx++;
55682965 511 continue;
bba95fef 512 }
55682965
JB
513 if_idx = 0;
514
515 mutex_lock(&dev->devlist_mtx);
516 list_for_each_entry(wdev, &dev->netdev_list, list) {
bba95fef
JB
517 if (if_idx < if_start) {
518 if_idx++;
55682965 519 continue;
bba95fef 520 }
55682965
JB
521 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
522 cb->nlh->nlmsg_seq, NLM_F_MULTI,
bba95fef
JB
523 wdev->netdev) < 0) {
524 mutex_unlock(&dev->devlist_mtx);
525 goto out;
526 }
527 if_idx++;
55682965
JB
528 }
529 mutex_unlock(&dev->devlist_mtx);
bba95fef
JB
530
531 wp_idx++;
55682965 532 }
bba95fef 533 out:
a1794390 534 mutex_unlock(&cfg80211_mutex);
55682965
JB
535
536 cb->args[0] = wp_idx;
537 cb->args[1] = if_idx;
538
539 return skb->len;
540}
541
542static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
543{
544 struct sk_buff *msg;
545 struct cfg80211_registered_device *dev;
546 struct net_device *netdev;
547 int err;
548
bba95fef 549 err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
55682965
JB
550 if (err)
551 return err;
552
553 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
554 if (!msg)
555 goto out_err;
556
557 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
558 goto out_free;
559
560 dev_put(netdev);
561 cfg80211_put_dev(dev);
562
563 return genlmsg_unicast(msg, info->snd_pid);
564
565 out_free:
566 nlmsg_free(msg);
567 out_err:
568 dev_put(netdev);
569 cfg80211_put_dev(dev);
570 return -ENOBUFS;
571}
572
66f7ac50
MW
573static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
574 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
575 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
576 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
577 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
578 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
579};
580
581static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
582{
583 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
584 int flag;
585
586 *mntrflags = 0;
587
588 if (!nla)
589 return -EINVAL;
590
591 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
592 nla, mntr_flags_policy))
593 return -EINVAL;
594
595 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
596 if (flags[flag])
597 *mntrflags |= (1<<flag);
598
599 return 0;
600}
601
55682965
JB
602static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
603{
604 struct cfg80211_registered_device *drv;
2ec600d6 605 struct vif_params params;
55682965
JB
606 int err, ifindex;
607 enum nl80211_iftype type;
608 struct net_device *dev;
92ffe055 609 u32 _flags, *flags = NULL;
55682965 610
2ec600d6
LCC
611 memset(&params, 0, sizeof(params));
612
3b85875a
JB
613 rtnl_lock();
614
bba95fef 615 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
55682965 616 if (err)
3b85875a
JB
617 goto unlock_rtnl;
618
55682965 619 ifindex = dev->ifindex;
723b038d 620 type = dev->ieee80211_ptr->iftype;
55682965
JB
621 dev_put(dev);
622
723b038d
JB
623 err = -EINVAL;
624 if (info->attrs[NL80211_ATTR_IFTYPE]) {
625 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
626 if (type > NL80211_IFTYPE_MAX)
627 goto unlock;
628 }
629
f59ac048
LR
630 if (!drv->ops->change_virtual_intf ||
631 !(drv->wiphy.interface_modes & (1 << type))) {
55682965
JB
632 err = -EOPNOTSUPP;
633 goto unlock;
634 }
635
92ffe055
JB
636 if (info->attrs[NL80211_ATTR_MESH_ID]) {
637 if (type != NL80211_IFTYPE_MESH_POINT) {
638 err = -EINVAL;
639 goto unlock;
640 }
2ec600d6
LCC
641 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
642 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
643 }
644
92ffe055
JB
645 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
646 if (type != NL80211_IFTYPE_MONITOR) {
647 err = -EINVAL;
648 goto unlock;
649 }
650 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
651 &_flags);
652 if (!err)
653 flags = &_flags;
654 }
3b85875a 655
66f7ac50 656 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
92ffe055 657 type, flags, &params);
60719ffd
JB
658
659 dev = __dev_get_by_index(&init_net, ifindex);
660 WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != type));
661
55682965
JB
662 unlock:
663 cfg80211_put_dev(drv);
3b85875a
JB
664 unlock_rtnl:
665 rtnl_unlock();
55682965
JB
666 return err;
667}
668
669static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
670{
671 struct cfg80211_registered_device *drv;
2ec600d6 672 struct vif_params params;
55682965
JB
673 int err;
674 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
66f7ac50 675 u32 flags;
55682965 676
2ec600d6
LCC
677 memset(&params, 0, sizeof(params));
678
55682965
JB
679 if (!info->attrs[NL80211_ATTR_IFNAME])
680 return -EINVAL;
681
682 if (info->attrs[NL80211_ATTR_IFTYPE]) {
683 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
684 if (type > NL80211_IFTYPE_MAX)
685 return -EINVAL;
686 }
687
3b85875a
JB
688 rtnl_lock();
689
55682965 690 drv = cfg80211_get_dev_from_info(info);
3b85875a
JB
691 if (IS_ERR(drv)) {
692 err = PTR_ERR(drv);
693 goto unlock_rtnl;
694 }
55682965 695
f59ac048
LR
696 if (!drv->ops->add_virtual_intf ||
697 !(drv->wiphy.interface_modes & (1 << type))) {
55682965
JB
698 err = -EOPNOTSUPP;
699 goto unlock;
700 }
701
2ec600d6
LCC
702 if (type == NL80211_IFTYPE_MESH_POINT &&
703 info->attrs[NL80211_ATTR_MESH_ID]) {
704 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
705 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
706 }
707
66f7ac50
MW
708 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
709 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
710 &flags);
55682965 711 err = drv->ops->add_virtual_intf(&drv->wiphy,
66f7ac50 712 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2ec600d6 713 type, err ? NULL : &flags, &params);
2ec600d6 714
55682965
JB
715 unlock:
716 cfg80211_put_dev(drv);
3b85875a
JB
717 unlock_rtnl:
718 rtnl_unlock();
55682965
JB
719 return err;
720}
721
722static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
723{
724 struct cfg80211_registered_device *drv;
725 int ifindex, err;
726 struct net_device *dev;
727
3b85875a
JB
728 rtnl_lock();
729
bba95fef 730 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
55682965 731 if (err)
3b85875a 732 goto unlock_rtnl;
55682965
JB
733 ifindex = dev->ifindex;
734 dev_put(dev);
735
736 if (!drv->ops->del_virtual_intf) {
737 err = -EOPNOTSUPP;
738 goto out;
739 }
740
55682965 741 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
55682965
JB
742
743 out:
744 cfg80211_put_dev(drv);
3b85875a
JB
745 unlock_rtnl:
746 rtnl_unlock();
55682965
JB
747 return err;
748}
749
41ade00f
JB
750struct get_key_cookie {
751 struct sk_buff *msg;
752 int error;
753};
754
755static void get_key_callback(void *c, struct key_params *params)
756{
757 struct get_key_cookie *cookie = c;
758
759 if (params->key)
760 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
761 params->key_len, params->key);
762
763 if (params->seq)
764 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
765 params->seq_len, params->seq);
766
767 if (params->cipher)
768 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
769 params->cipher);
770
771 return;
772 nla_put_failure:
773 cookie->error = 1;
774}
775
776static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
777{
778 struct cfg80211_registered_device *drv;
779 int err;
780 struct net_device *dev;
781 u8 key_idx = 0;
782 u8 *mac_addr = NULL;
783 struct get_key_cookie cookie = {
784 .error = 0,
785 };
786 void *hdr;
787 struct sk_buff *msg;
788
789 if (info->attrs[NL80211_ATTR_KEY_IDX])
790 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
791
3cfcf6ac 792 if (key_idx > 5)
41ade00f
JB
793 return -EINVAL;
794
795 if (info->attrs[NL80211_ATTR_MAC])
796 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
797
3b85875a
JB
798 rtnl_lock();
799
bba95fef 800 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
41ade00f 801 if (err)
3b85875a 802 goto unlock_rtnl;
41ade00f
JB
803
804 if (!drv->ops->get_key) {
805 err = -EOPNOTSUPP;
806 goto out;
807 }
808
809 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
810 if (!msg) {
811 err = -ENOMEM;
812 goto out;
813 }
814
815 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
816 NL80211_CMD_NEW_KEY);
817
818 if (IS_ERR(hdr)) {
819 err = PTR_ERR(hdr);
820 goto out;
821 }
822
823 cookie.msg = msg;
824
825 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
826 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
827 if (mac_addr)
828 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
829
41ade00f
JB
830 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
831 &cookie, get_key_callback);
41ade00f
JB
832
833 if (err)
834 goto out;
835
836 if (cookie.error)
837 goto nla_put_failure;
838
839 genlmsg_end(msg, hdr);
840 err = genlmsg_unicast(msg, info->snd_pid);
841 goto out;
842
843 nla_put_failure:
844 err = -ENOBUFS;
845 nlmsg_free(msg);
846 out:
847 cfg80211_put_dev(drv);
848 dev_put(dev);
3b85875a
JB
849 unlock_rtnl:
850 rtnl_unlock();
851
41ade00f
JB
852 return err;
853}
854
855static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
856{
857 struct cfg80211_registered_device *drv;
858 int err;
859 struct net_device *dev;
860 u8 key_idx;
3cfcf6ac
JM
861 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
862 u8 key_index);
41ade00f
JB
863
864 if (!info->attrs[NL80211_ATTR_KEY_IDX])
865 return -EINVAL;
866
867 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
868
3cfcf6ac
JM
869 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
870 if (key_idx < 4 || key_idx > 5)
871 return -EINVAL;
872 } else if (key_idx > 3)
41ade00f
JB
873 return -EINVAL;
874
875 /* currently only support setting default key */
3cfcf6ac
JM
876 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
877 !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
41ade00f
JB
878 return -EINVAL;
879
3b85875a
JB
880 rtnl_lock();
881
bba95fef 882 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
41ade00f 883 if (err)
3b85875a 884 goto unlock_rtnl;
41ade00f 885
3cfcf6ac
JM
886 if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
887 func = drv->ops->set_default_key;
888 else
889 func = drv->ops->set_default_mgmt_key;
890
891 if (!func) {
41ade00f
JB
892 err = -EOPNOTSUPP;
893 goto out;
894 }
895
3cfcf6ac 896 err = func(&drv->wiphy, dev, key_idx);
41ade00f
JB
897
898 out:
899 cfg80211_put_dev(drv);
900 dev_put(dev);
3b85875a
JB
901
902 unlock_rtnl:
903 rtnl_unlock();
904
41ade00f
JB
905 return err;
906}
907
908static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
909{
910 struct cfg80211_registered_device *drv;
911 int err;
912 struct net_device *dev;
913 struct key_params params;
914 u8 key_idx = 0;
915 u8 *mac_addr = NULL;
916
917 memset(&params, 0, sizeof(params));
918
919 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
920 return -EINVAL;
921
922 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
923 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
924 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
925 }
926
927 if (info->attrs[NL80211_ATTR_KEY_IDX])
928 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
929
930 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
931
932 if (info->attrs[NL80211_ATTR_MAC])
933 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
934
3cfcf6ac 935 if (key_idx > 5)
41ade00f
JB
936 return -EINVAL;
937
938 /*
939 * Disallow pairwise keys with non-zero index unless it's WEP
940 * (because current deployments use pairwise WEP keys with
941 * non-zero indizes but 802.11i clearly specifies to use zero)
942 */
943 if (mac_addr && key_idx &&
944 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
945 params.cipher != WLAN_CIPHER_SUITE_WEP104)
946 return -EINVAL;
947
948 /* TODO: add definitions for the lengths to linux/ieee80211.h */
949 switch (params.cipher) {
950 case WLAN_CIPHER_SUITE_WEP40:
951 if (params.key_len != 5)
952 return -EINVAL;
953 break;
954 case WLAN_CIPHER_SUITE_TKIP:
955 if (params.key_len != 32)
956 return -EINVAL;
957 break;
958 case WLAN_CIPHER_SUITE_CCMP:
959 if (params.key_len != 16)
960 return -EINVAL;
961 break;
962 case WLAN_CIPHER_SUITE_WEP104:
963 if (params.key_len != 13)
964 return -EINVAL;
965 break;
3cfcf6ac
JM
966 case WLAN_CIPHER_SUITE_AES_CMAC:
967 if (params.key_len != 16)
968 return -EINVAL;
969 break;
41ade00f
JB
970 default:
971 return -EINVAL;
972 }
973
3b85875a
JB
974 rtnl_lock();
975
bba95fef 976 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
41ade00f 977 if (err)
3b85875a 978 goto unlock_rtnl;
41ade00f
JB
979
980 if (!drv->ops->add_key) {
981 err = -EOPNOTSUPP;
982 goto out;
983 }
984
41ade00f 985 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, &params);
41ade00f
JB
986
987 out:
988 cfg80211_put_dev(drv);
989 dev_put(dev);
3b85875a
JB
990 unlock_rtnl:
991 rtnl_unlock();
992
41ade00f
JB
993 return err;
994}
995
996static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
997{
998 struct cfg80211_registered_device *drv;
999 int err;
1000 struct net_device *dev;
1001 u8 key_idx = 0;
1002 u8 *mac_addr = NULL;
1003
1004 if (info->attrs[NL80211_ATTR_KEY_IDX])
1005 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1006
3cfcf6ac 1007 if (key_idx > 5)
41ade00f
JB
1008 return -EINVAL;
1009
1010 if (info->attrs[NL80211_ATTR_MAC])
1011 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1012
3b85875a
JB
1013 rtnl_lock();
1014
bba95fef 1015 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
41ade00f 1016 if (err)
3b85875a 1017 goto unlock_rtnl;
41ade00f
JB
1018
1019 if (!drv->ops->del_key) {
1020 err = -EOPNOTSUPP;
1021 goto out;
1022 }
1023
41ade00f 1024 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
41ade00f
JB
1025
1026 out:
1027 cfg80211_put_dev(drv);
1028 dev_put(dev);
3b85875a
JB
1029
1030 unlock_rtnl:
1031 rtnl_unlock();
1032
41ade00f
JB
1033 return err;
1034}
1035
ed1b6cc7
JB
1036static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1037{
1038 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1039 struct beacon_parameters *info);
1040 struct cfg80211_registered_device *drv;
1041 int err;
1042 struct net_device *dev;
1043 struct beacon_parameters params;
1044 int haveinfo = 0;
1045
3b85875a
JB
1046 rtnl_lock();
1047
bba95fef 1048 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
ed1b6cc7 1049 if (err)
3b85875a 1050 goto unlock_rtnl;
ed1b6cc7
JB
1051
1052 switch (info->genlhdr->cmd) {
1053 case NL80211_CMD_NEW_BEACON:
1054 /* these are required for NEW_BEACON */
1055 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1056 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1057 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1058 err = -EINVAL;
1059 goto out;
1060 }
1061
1062 call = drv->ops->add_beacon;
1063 break;
1064 case NL80211_CMD_SET_BEACON:
1065 call = drv->ops->set_beacon;
1066 break;
1067 default:
1068 WARN_ON(1);
1069 err = -EOPNOTSUPP;
1070 goto out;
1071 }
1072
1073 if (!call) {
1074 err = -EOPNOTSUPP;
1075 goto out;
1076 }
1077
1078 memset(&params, 0, sizeof(params));
1079
1080 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1081 params.interval =
1082 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1083 haveinfo = 1;
1084 }
1085
1086 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1087 params.dtim_period =
1088 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1089 haveinfo = 1;
1090 }
1091
1092 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1093 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1094 params.head_len =
1095 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1096 haveinfo = 1;
1097 }
1098
1099 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1100 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1101 params.tail_len =
1102 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1103 haveinfo = 1;
1104 }
1105
1106 if (!haveinfo) {
1107 err = -EINVAL;
1108 goto out;
1109 }
1110
ed1b6cc7 1111 err = call(&drv->wiphy, dev, &params);
ed1b6cc7
JB
1112
1113 out:
1114 cfg80211_put_dev(drv);
1115 dev_put(dev);
3b85875a
JB
1116 unlock_rtnl:
1117 rtnl_unlock();
1118
ed1b6cc7
JB
1119 return err;
1120}
1121
1122static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1123{
1124 struct cfg80211_registered_device *drv;
1125 int err;
1126 struct net_device *dev;
1127
3b85875a
JB
1128 rtnl_lock();
1129
bba95fef 1130 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
ed1b6cc7 1131 if (err)
3b85875a 1132 goto unlock_rtnl;
ed1b6cc7
JB
1133
1134 if (!drv->ops->del_beacon) {
1135 err = -EOPNOTSUPP;
1136 goto out;
1137 }
1138
ed1b6cc7 1139 err = drv->ops->del_beacon(&drv->wiphy, dev);
ed1b6cc7
JB
1140
1141 out:
1142 cfg80211_put_dev(drv);
1143 dev_put(dev);
3b85875a
JB
1144 unlock_rtnl:
1145 rtnl_unlock();
1146
ed1b6cc7
JB
1147 return err;
1148}
1149
5727ef1b
JB
1150static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1151 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1152 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1153 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1154};
1155
1156static int parse_station_flags(struct nlattr *nla, u32 *staflags)
1157{
1158 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1159 int flag;
1160
1161 *staflags = 0;
1162
1163 if (!nla)
1164 return 0;
1165
1166 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1167 nla, sta_flags_policy))
1168 return -EINVAL;
1169
1170 *staflags = STATION_FLAG_CHANGED;
1171
1172 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1173 if (flags[flag])
1174 *staflags |= (1<<flag);
1175
1176 return 0;
1177}
1178
420e7fab
HR
1179static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1180{
1181 int modulation, streams, bitrate;
1182
1183 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1184 return rate->legacy;
1185
1186 /* the formula below does only work for MCS values smaller than 32 */
1187 if (rate->mcs >= 32)
1188 return 0;
1189
1190 modulation = rate->mcs & 7;
1191 streams = (rate->mcs >> 3) + 1;
1192
1193 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1194 13500000 : 6500000;
1195
1196 if (modulation < 4)
1197 bitrate *= (modulation + 1);
1198 else if (modulation == 4)
1199 bitrate *= (modulation + 2);
1200 else
1201 bitrate *= (modulation + 3);
1202
1203 bitrate *= streams;
1204
1205 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1206 bitrate = (bitrate / 9) * 10;
1207
1208 /* do NOT round down here */
1209 return (bitrate + 50000) / 100000;
1210}
1211
fd5b74dc
JB
1212static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1213 int flags, struct net_device *dev,
2ec600d6 1214 u8 *mac_addr, struct station_info *sinfo)
fd5b74dc
JB
1215{
1216 void *hdr;
420e7fab
HR
1217 struct nlattr *sinfoattr, *txrate;
1218 u16 bitrate;
fd5b74dc
JB
1219
1220 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1221 if (!hdr)
1222 return -1;
1223
1224 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1225 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1226
2ec600d6
LCC
1227 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1228 if (!sinfoattr)
fd5b74dc 1229 goto nla_put_failure;
2ec600d6
LCC
1230 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1231 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1232 sinfo->inactive_time);
1233 if (sinfo->filled & STATION_INFO_RX_BYTES)
1234 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1235 sinfo->rx_bytes);
1236 if (sinfo->filled & STATION_INFO_TX_BYTES)
1237 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1238 sinfo->tx_bytes);
1239 if (sinfo->filled & STATION_INFO_LLID)
1240 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1241 sinfo->llid);
1242 if (sinfo->filled & STATION_INFO_PLID)
1243 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1244 sinfo->plid);
1245 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1246 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1247 sinfo->plink_state);
420e7fab
HR
1248 if (sinfo->filled & STATION_INFO_SIGNAL)
1249 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1250 sinfo->signal);
1251 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1252 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1253 if (!txrate)
1254 goto nla_put_failure;
1255
1256 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1257 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1258 if (bitrate > 0)
1259 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2ec600d6 1260
420e7fab
HR
1261 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1262 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1263 sinfo->txrate.mcs);
1264 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1265 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1266 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1267 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1268
1269 nla_nest_end(msg, txrate);
1270 }
98c8a60a
JM
1271 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1272 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1273 sinfo->rx_packets);
1274 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1275 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1276 sinfo->tx_packets);
2ec600d6 1277 nla_nest_end(msg, sinfoattr);
fd5b74dc
JB
1278
1279 return genlmsg_end(msg, hdr);
1280
1281 nla_put_failure:
bc3ed28c
TG
1282 genlmsg_cancel(msg, hdr);
1283 return -EMSGSIZE;
fd5b74dc
JB
1284}
1285
2ec600d6 1286static int nl80211_dump_station(struct sk_buff *skb,
bba95fef 1287 struct netlink_callback *cb)
2ec600d6 1288{
2ec600d6
LCC
1289 struct station_info sinfo;
1290 struct cfg80211_registered_device *dev;
bba95fef 1291 struct net_device *netdev;
2ec600d6 1292 u8 mac_addr[ETH_ALEN];
bba95fef
JB
1293 int ifidx = cb->args[0];
1294 int sta_idx = cb->args[1];
2ec600d6 1295 int err;
2ec600d6 1296
bba95fef
JB
1297 if (!ifidx) {
1298 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1299 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1300 nl80211_policy);
1301 if (err)
1302 return err;
2ec600d6 1303
bba95fef
JB
1304 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1305 return -EINVAL;
2ec600d6 1306
bba95fef
JB
1307 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1308 if (!ifidx)
1309 return -EINVAL;
2ec600d6 1310 }
2ec600d6 1311
3b85875a
JB
1312 rtnl_lock();
1313
1314 netdev = __dev_get_by_index(&init_net, ifidx);
1315 if (!netdev) {
1316 err = -ENODEV;
1317 goto out_rtnl;
1318 }
2ec600d6 1319
bba95fef
JB
1320 dev = cfg80211_get_dev_from_ifindex(ifidx);
1321 if (IS_ERR(dev)) {
1322 err = PTR_ERR(dev);
3b85875a 1323 goto out_rtnl;
bba95fef
JB
1324 }
1325
1326 if (!dev->ops->dump_station) {
1327 err = -ENOSYS;
1328 goto out_err;
1329 }
1330
bba95fef
JB
1331 while (1) {
1332 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1333 mac_addr, &sinfo);
1334 if (err == -ENOENT)
1335 break;
1336 if (err)
3b85875a 1337 goto out_err;
bba95fef
JB
1338
1339 if (nl80211_send_station(skb,
1340 NETLINK_CB(cb->skb).pid,
1341 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1342 netdev, mac_addr,
1343 &sinfo) < 0)
1344 goto out;
1345
1346 sta_idx++;
1347 }
1348
1349
1350 out:
1351 cb->args[1] = sta_idx;
1352 err = skb->len;
bba95fef
JB
1353 out_err:
1354 cfg80211_put_dev(dev);
3b85875a
JB
1355 out_rtnl:
1356 rtnl_unlock();
bba95fef
JB
1357
1358 return err;
2ec600d6 1359}
fd5b74dc 1360
5727ef1b
JB
1361static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1362{
fd5b74dc
JB
1363 struct cfg80211_registered_device *drv;
1364 int err;
1365 struct net_device *dev;
2ec600d6 1366 struct station_info sinfo;
fd5b74dc
JB
1367 struct sk_buff *msg;
1368 u8 *mac_addr = NULL;
1369
2ec600d6 1370 memset(&sinfo, 0, sizeof(sinfo));
fd5b74dc
JB
1371
1372 if (!info->attrs[NL80211_ATTR_MAC])
1373 return -EINVAL;
1374
1375 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1376
3b85875a
JB
1377 rtnl_lock();
1378
bba95fef 1379 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
fd5b74dc 1380 if (err)
3b85875a 1381 goto out_rtnl;
fd5b74dc
JB
1382
1383 if (!drv->ops->get_station) {
1384 err = -EOPNOTSUPP;
1385 goto out;
1386 }
1387
2ec600d6 1388 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
2ec600d6
LCC
1389 if (err)
1390 goto out;
1391
fd5b74dc
JB
1392 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1393 if (!msg)
1394 goto out;
1395
1396 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
2ec600d6 1397 dev, mac_addr, &sinfo) < 0)
fd5b74dc
JB
1398 goto out_free;
1399
1400 err = genlmsg_unicast(msg, info->snd_pid);
1401 goto out;
1402
1403 out_free:
1404 nlmsg_free(msg);
fd5b74dc
JB
1405 out:
1406 cfg80211_put_dev(drv);
1407 dev_put(dev);
3b85875a
JB
1408 out_rtnl:
1409 rtnl_unlock();
1410
fd5b74dc 1411 return err;
5727ef1b
JB
1412}
1413
1414/*
1415 * Get vlan interface making sure it is on the right wiphy.
1416 */
1417static int get_vlan(struct nlattr *vlanattr,
1418 struct cfg80211_registered_device *rdev,
1419 struct net_device **vlan)
1420{
1421 *vlan = NULL;
1422
1423 if (vlanattr) {
1424 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1425 if (!*vlan)
1426 return -ENODEV;
1427 if (!(*vlan)->ieee80211_ptr)
1428 return -EINVAL;
1429 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1430 return -EINVAL;
1431 }
1432 return 0;
1433}
1434
1435static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1436{
1437 struct cfg80211_registered_device *drv;
1438 int err;
1439 struct net_device *dev;
1440 struct station_parameters params;
1441 u8 *mac_addr = NULL;
1442
1443 memset(&params, 0, sizeof(params));
1444
1445 params.listen_interval = -1;
1446
1447 if (info->attrs[NL80211_ATTR_STA_AID])
1448 return -EINVAL;
1449
1450 if (!info->attrs[NL80211_ATTR_MAC])
1451 return -EINVAL;
1452
1453 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1454
1455 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1456 params.supported_rates =
1457 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1458 params.supported_rates_len =
1459 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1460 }
1461
1462 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1463 params.listen_interval =
1464 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1465
36aedc90
JM
1466 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1467 params.ht_capa =
1468 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1469
5727ef1b
JB
1470 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1471 &params.station_flags))
1472 return -EINVAL;
1473
2ec600d6
LCC
1474 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1475 params.plink_action =
1476 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1477
3b85875a
JB
1478 rtnl_lock();
1479
bba95fef 1480 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
5727ef1b 1481 if (err)
3b85875a 1482 goto out_rtnl;
5727ef1b
JB
1483
1484 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1485 if (err)
1486 goto out;
1487
1488 if (!drv->ops->change_station) {
1489 err = -EOPNOTSUPP;
1490 goto out;
1491 }
1492
5727ef1b 1493 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, &params);
5727ef1b
JB
1494
1495 out:
1496 if (params.vlan)
1497 dev_put(params.vlan);
1498 cfg80211_put_dev(drv);
1499 dev_put(dev);
3b85875a
JB
1500 out_rtnl:
1501 rtnl_unlock();
1502
5727ef1b
JB
1503 return err;
1504}
1505
1506static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1507{
1508 struct cfg80211_registered_device *drv;
1509 int err;
1510 struct net_device *dev;
1511 struct station_parameters params;
1512 u8 *mac_addr = NULL;
1513
1514 memset(&params, 0, sizeof(params));
1515
1516 if (!info->attrs[NL80211_ATTR_MAC])
1517 return -EINVAL;
1518
1519 if (!info->attrs[NL80211_ATTR_STA_AID])
1520 return -EINVAL;
1521
1522 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1523 return -EINVAL;
1524
1525 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1526 return -EINVAL;
1527
1528 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1529 params.supported_rates =
1530 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1531 params.supported_rates_len =
1532 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1533 params.listen_interval =
1534 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
16f2e85d 1535 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
36aedc90
JM
1536 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1537 params.ht_capa =
1538 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5727ef1b
JB
1539
1540 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1541 &params.station_flags))
1542 return -EINVAL;
1543
3b85875a
JB
1544 rtnl_lock();
1545
bba95fef 1546 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
5727ef1b 1547 if (err)
3b85875a 1548 goto out_rtnl;
5727ef1b
JB
1549
1550 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1551 if (err)
1552 goto out;
1553
1554 if (!drv->ops->add_station) {
1555 err = -EOPNOTSUPP;
1556 goto out;
1557 }
1558
5727ef1b 1559 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
5727ef1b
JB
1560
1561 out:
1562 if (params.vlan)
1563 dev_put(params.vlan);
1564 cfg80211_put_dev(drv);
1565 dev_put(dev);
3b85875a
JB
1566 out_rtnl:
1567 rtnl_unlock();
1568
5727ef1b
JB
1569 return err;
1570}
1571
1572static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1573{
1574 struct cfg80211_registered_device *drv;
1575 int err;
1576 struct net_device *dev;
1577 u8 *mac_addr = NULL;
1578
1579 if (info->attrs[NL80211_ATTR_MAC])
1580 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1581
3b85875a
JB
1582 rtnl_lock();
1583
bba95fef 1584 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
5727ef1b 1585 if (err)
3b85875a 1586 goto out_rtnl;
5727ef1b
JB
1587
1588 if (!drv->ops->del_station) {
1589 err = -EOPNOTSUPP;
1590 goto out;
1591 }
1592
5727ef1b 1593 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
5727ef1b
JB
1594
1595 out:
1596 cfg80211_put_dev(drv);
1597 dev_put(dev);
3b85875a
JB
1598 out_rtnl:
1599 rtnl_unlock();
1600
5727ef1b
JB
1601 return err;
1602}
1603
2ec600d6
LCC
1604static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1605 int flags, struct net_device *dev,
1606 u8 *dst, u8 *next_hop,
1607 struct mpath_info *pinfo)
1608{
1609 void *hdr;
1610 struct nlattr *pinfoattr;
1611
1612 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1613 if (!hdr)
1614 return -1;
1615
1616 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1617 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1618 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1619
1620 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1621 if (!pinfoattr)
1622 goto nla_put_failure;
1623 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1624 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1625 pinfo->frame_qlen);
1626 if (pinfo->filled & MPATH_INFO_DSN)
1627 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1628 pinfo->dsn);
1629 if (pinfo->filled & MPATH_INFO_METRIC)
1630 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1631 pinfo->metric);
1632 if (pinfo->filled & MPATH_INFO_EXPTIME)
1633 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1634 pinfo->exptime);
1635 if (pinfo->filled & MPATH_INFO_FLAGS)
1636 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1637 pinfo->flags);
1638 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1639 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1640 pinfo->discovery_timeout);
1641 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1642 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1643 pinfo->discovery_retries);
1644
1645 nla_nest_end(msg, pinfoattr);
1646
1647 return genlmsg_end(msg, hdr);
1648
1649 nla_put_failure:
bc3ed28c
TG
1650 genlmsg_cancel(msg, hdr);
1651 return -EMSGSIZE;
2ec600d6
LCC
1652}
1653
1654static int nl80211_dump_mpath(struct sk_buff *skb,
bba95fef 1655 struct netlink_callback *cb)
2ec600d6 1656{
2ec600d6
LCC
1657 struct mpath_info pinfo;
1658 struct cfg80211_registered_device *dev;
bba95fef 1659 struct net_device *netdev;
2ec600d6
LCC
1660 u8 dst[ETH_ALEN];
1661 u8 next_hop[ETH_ALEN];
bba95fef
JB
1662 int ifidx = cb->args[0];
1663 int path_idx = cb->args[1];
2ec600d6 1664 int err;
2ec600d6 1665
bba95fef
JB
1666 if (!ifidx) {
1667 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1668 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1669 nl80211_policy);
1670 if (err)
1671 return err;
1672
1673 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1674 return -EINVAL;
1675
1676 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1677 if (!ifidx)
1678 return -EINVAL;
1679 }
1680
3b85875a
JB
1681 rtnl_lock();
1682
1683 netdev = __dev_get_by_index(&init_net, ifidx);
1684 if (!netdev) {
1685 err = -ENODEV;
1686 goto out_rtnl;
1687 }
bba95fef
JB
1688
1689 dev = cfg80211_get_dev_from_ifindex(ifidx);
1690 if (IS_ERR(dev)) {
1691 err = PTR_ERR(dev);
3b85875a 1692 goto out_rtnl;
bba95fef
JB
1693 }
1694
1695 if (!dev->ops->dump_mpath) {
1696 err = -ENOSYS;
1697 goto out_err;
1698 }
1699
bba95fef
JB
1700 while (1) {
1701 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1702 dst, next_hop, &pinfo);
1703 if (err == -ENOENT)
2ec600d6 1704 break;
bba95fef 1705 if (err)
3b85875a 1706 goto out_err;
2ec600d6 1707
bba95fef
JB
1708 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1709 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1710 netdev, dst, next_hop,
1711 &pinfo) < 0)
1712 goto out;
2ec600d6 1713
bba95fef 1714 path_idx++;
2ec600d6 1715 }
2ec600d6 1716
2ec600d6 1717
bba95fef
JB
1718 out:
1719 cb->args[1] = path_idx;
1720 err = skb->len;
bba95fef
JB
1721 out_err:
1722 cfg80211_put_dev(dev);
3b85875a
JB
1723 out_rtnl:
1724 rtnl_unlock();
bba95fef
JB
1725
1726 return err;
2ec600d6
LCC
1727}
1728
1729static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1730{
1731 struct cfg80211_registered_device *drv;
1732 int err;
1733 struct net_device *dev;
1734 struct mpath_info pinfo;
1735 struct sk_buff *msg;
1736 u8 *dst = NULL;
1737 u8 next_hop[ETH_ALEN];
1738
1739 memset(&pinfo, 0, sizeof(pinfo));
1740
1741 if (!info->attrs[NL80211_ATTR_MAC])
1742 return -EINVAL;
1743
1744 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1745
3b85875a
JB
1746 rtnl_lock();
1747
bba95fef 1748 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2ec600d6 1749 if (err)
3b85875a 1750 goto out_rtnl;
2ec600d6
LCC
1751
1752 if (!drv->ops->get_mpath) {
1753 err = -EOPNOTSUPP;
1754 goto out;
1755 }
1756
2ec600d6 1757 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
2ec600d6
LCC
1758 if (err)
1759 goto out;
1760
1761 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1762 if (!msg)
1763 goto out;
1764
1765 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1766 dev, dst, next_hop, &pinfo) < 0)
1767 goto out_free;
1768
1769 err = genlmsg_unicast(msg, info->snd_pid);
1770 goto out;
1771
1772 out_free:
1773 nlmsg_free(msg);
2ec600d6
LCC
1774 out:
1775 cfg80211_put_dev(drv);
1776 dev_put(dev);
3b85875a
JB
1777 out_rtnl:
1778 rtnl_unlock();
1779
2ec600d6
LCC
1780 return err;
1781}
1782
1783static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1784{
1785 struct cfg80211_registered_device *drv;
1786 int err;
1787 struct net_device *dev;
1788 u8 *dst = NULL;
1789 u8 *next_hop = NULL;
1790
1791 if (!info->attrs[NL80211_ATTR_MAC])
1792 return -EINVAL;
1793
1794 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1795 return -EINVAL;
1796
1797 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1798 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1799
3b85875a
JB
1800 rtnl_lock();
1801
bba95fef 1802 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2ec600d6 1803 if (err)
3b85875a 1804 goto out_rtnl;
2ec600d6
LCC
1805
1806 if (!drv->ops->change_mpath) {
1807 err = -EOPNOTSUPP;
1808 goto out;
1809 }
1810
2ec600d6 1811 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
2ec600d6
LCC
1812
1813 out:
1814 cfg80211_put_dev(drv);
1815 dev_put(dev);
3b85875a
JB
1816 out_rtnl:
1817 rtnl_unlock();
1818
2ec600d6
LCC
1819 return err;
1820}
1821static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1822{
1823 struct cfg80211_registered_device *drv;
1824 int err;
1825 struct net_device *dev;
1826 u8 *dst = NULL;
1827 u8 *next_hop = NULL;
1828
1829 if (!info->attrs[NL80211_ATTR_MAC])
1830 return -EINVAL;
1831
1832 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1833 return -EINVAL;
1834
1835 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1836 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1837
3b85875a
JB
1838 rtnl_lock();
1839
bba95fef 1840 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2ec600d6 1841 if (err)
3b85875a 1842 goto out_rtnl;
2ec600d6
LCC
1843
1844 if (!drv->ops->add_mpath) {
1845 err = -EOPNOTSUPP;
1846 goto out;
1847 }
1848
2ec600d6 1849 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
2ec600d6
LCC
1850
1851 out:
1852 cfg80211_put_dev(drv);
1853 dev_put(dev);
3b85875a
JB
1854 out_rtnl:
1855 rtnl_unlock();
1856
2ec600d6
LCC
1857 return err;
1858}
1859
1860static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1861{
1862 struct cfg80211_registered_device *drv;
1863 int err;
1864 struct net_device *dev;
1865 u8 *dst = NULL;
1866
1867 if (info->attrs[NL80211_ATTR_MAC])
1868 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1869
3b85875a
JB
1870 rtnl_lock();
1871
bba95fef 1872 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2ec600d6 1873 if (err)
3b85875a 1874 goto out_rtnl;
2ec600d6
LCC
1875
1876 if (!drv->ops->del_mpath) {
1877 err = -EOPNOTSUPP;
1878 goto out;
1879 }
1880
2ec600d6 1881 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
2ec600d6
LCC
1882
1883 out:
1884 cfg80211_put_dev(drv);
1885 dev_put(dev);
3b85875a
JB
1886 out_rtnl:
1887 rtnl_unlock();
1888
2ec600d6
LCC
1889 return err;
1890}
1891
9f1ba906
JM
1892static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
1893{
1894 struct cfg80211_registered_device *drv;
1895 int err;
1896 struct net_device *dev;
1897 struct bss_parameters params;
1898
1899 memset(&params, 0, sizeof(params));
1900 /* default to not changing parameters */
1901 params.use_cts_prot = -1;
1902 params.use_short_preamble = -1;
1903 params.use_short_slot_time = -1;
1904
1905 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
1906 params.use_cts_prot =
1907 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
1908 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
1909 params.use_short_preamble =
1910 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
1911 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
1912 params.use_short_slot_time =
1913 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
90c97a04
JM
1914 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
1915 params.basic_rates =
1916 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1917 params.basic_rates_len =
1918 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1919 }
9f1ba906 1920
3b85875a
JB
1921 rtnl_lock();
1922
9f1ba906
JM
1923 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1924 if (err)
3b85875a 1925 goto out_rtnl;
9f1ba906
JM
1926
1927 if (!drv->ops->change_bss) {
1928 err = -EOPNOTSUPP;
1929 goto out;
1930 }
1931
9f1ba906 1932 err = drv->ops->change_bss(&drv->wiphy, dev, &params);
9f1ba906
JM
1933
1934 out:
1935 cfg80211_put_dev(drv);
1936 dev_put(dev);
3b85875a
JB
1937 out_rtnl:
1938 rtnl_unlock();
1939
9f1ba906
JM
1940 return err;
1941}
1942
b2e1b302
LR
1943static const struct nla_policy
1944 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
1945 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1946 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1947 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1948 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1949 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1950 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1951};
1952
1953static int parse_reg_rule(struct nlattr *tb[],
1954 struct ieee80211_reg_rule *reg_rule)
1955{
1956 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
1957 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
1958
1959 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
1960 return -EINVAL;
1961 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
1962 return -EINVAL;
1963 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
1964 return -EINVAL;
1965 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1966 return -EINVAL;
1967 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1968 return -EINVAL;
1969
1970 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
1971
1972 freq_range->start_freq_khz =
1973 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
1974 freq_range->end_freq_khz =
1975 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
1976 freq_range->max_bandwidth_khz =
1977 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
1978
1979 power_rule->max_eirp =
1980 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
1981
1982 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
1983 power_rule->max_antenna_gain =
1984 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
1985
1986 return 0;
1987}
1988
1989static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
1990{
1991 int r;
1992 char *data = NULL;
1993
80778f18
LR
1994 /*
1995 * You should only get this when cfg80211 hasn't yet initialized
1996 * completely when built-in to the kernel right between the time
1997 * window between nl80211_init() and regulatory_init(), if that is
1998 * even possible.
1999 */
2000 mutex_lock(&cfg80211_mutex);
2001 if (unlikely(!cfg80211_regdomain)) {
fe33eb39
LR
2002 mutex_unlock(&cfg80211_mutex);
2003 return -EINPROGRESS;
80778f18 2004 }
fe33eb39 2005 mutex_unlock(&cfg80211_mutex);
80778f18 2006
fe33eb39
LR
2007 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2008 return -EINVAL;
b2e1b302
LR
2009
2010 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2011
2012#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2013 /* We ignore world regdom requests with the old regdom setup */
fe33eb39
LR
2014 if (is_world_regdom(data))
2015 return -EINVAL;
b2e1b302 2016#endif
fe33eb39
LR
2017
2018 r = regulatory_hint_user(data);
2019
b2e1b302
LR
2020 return r;
2021}
2022
93da9cc1 2023static int nl80211_get_mesh_params(struct sk_buff *skb,
2024 struct genl_info *info)
2025{
2026 struct cfg80211_registered_device *drv;
2027 struct mesh_config cur_params;
2028 int err;
2029 struct net_device *dev;
2030 void *hdr;
2031 struct nlattr *pinfoattr;
2032 struct sk_buff *msg;
2033
3b85875a
JB
2034 rtnl_lock();
2035
93da9cc1 2036 /* Look up our device */
2037 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2038 if (err)
3b85875a 2039 goto out_rtnl;
93da9cc1 2040
f3f92586
JM
2041 if (!drv->ops->get_mesh_params) {
2042 err = -EOPNOTSUPP;
2043 goto out;
2044 }
2045
93da9cc1 2046 /* Get the mesh params */
93da9cc1 2047 err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
93da9cc1 2048 if (err)
2049 goto out;
2050
2051 /* Draw up a netlink message to send back */
2052 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2053 if (!msg) {
2054 err = -ENOBUFS;
2055 goto out;
2056 }
2057 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2058 NL80211_CMD_GET_MESH_PARAMS);
2059 if (!hdr)
2060 goto nla_put_failure;
2061 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2062 if (!pinfoattr)
2063 goto nla_put_failure;
2064 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2065 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2066 cur_params.dot11MeshRetryTimeout);
2067 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2068 cur_params.dot11MeshConfirmTimeout);
2069 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2070 cur_params.dot11MeshHoldingTimeout);
2071 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2072 cur_params.dot11MeshMaxPeerLinks);
2073 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2074 cur_params.dot11MeshMaxRetries);
2075 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2076 cur_params.dot11MeshTTL);
2077 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2078 cur_params.auto_open_plinks);
2079 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2080 cur_params.dot11MeshHWMPmaxPREQretries);
2081 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2082 cur_params.path_refresh_time);
2083 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2084 cur_params.min_discovery_timeout);
2085 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2086 cur_params.dot11MeshHWMPactivePathTimeout);
2087 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2088 cur_params.dot11MeshHWMPpreqMinInterval);
2089 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2090 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2091 nla_nest_end(msg, pinfoattr);
2092 genlmsg_end(msg, hdr);
2093 err = genlmsg_unicast(msg, info->snd_pid);
2094 goto out;
2095
3b85875a 2096 nla_put_failure:
93da9cc1 2097 genlmsg_cancel(msg, hdr);
2098 err = -EMSGSIZE;
3b85875a 2099 out:
93da9cc1 2100 /* Cleanup */
2101 cfg80211_put_dev(drv);
2102 dev_put(dev);
3b85875a
JB
2103 out_rtnl:
2104 rtnl_unlock();
2105
93da9cc1 2106 return err;
2107}
2108
2109#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2110do {\
2111 if (table[attr_num]) {\
2112 cfg.param = nla_fn(table[attr_num]); \
2113 mask |= (1 << (attr_num - 1)); \
2114 } \
2115} while (0);\
2116
2117static struct nla_policy
2118nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2119 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2120 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2121 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2122 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2123 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2124 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2125 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2126
2127 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2128 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2129 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2130 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2131 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2132 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2133};
2134
2135static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2136{
2137 int err;
2138 u32 mask;
2139 struct cfg80211_registered_device *drv;
2140 struct net_device *dev;
2141 struct mesh_config cfg;
2142 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2143 struct nlattr *parent_attr;
2144
2145 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2146 if (!parent_attr)
2147 return -EINVAL;
2148 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2149 parent_attr, nl80211_meshconf_params_policy))
2150 return -EINVAL;
2151
3b85875a
JB
2152 rtnl_lock();
2153
93da9cc1 2154 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2155 if (err)
3b85875a 2156 goto out_rtnl;
93da9cc1 2157
f3f92586
JM
2158 if (!drv->ops->set_mesh_params) {
2159 err = -EOPNOTSUPP;
2160 goto out;
2161 }
2162
93da9cc1 2163 /* This makes sure that there aren't more than 32 mesh config
2164 * parameters (otherwise our bitfield scheme would not work.) */
2165 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2166
2167 /* Fill in the params struct */
2168 mask = 0;
2169 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2170 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2171 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2172 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2173 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2174 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2175 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2176 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2177 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2178 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2179 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2180 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2181 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2182 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2183 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2184 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2185 nla_get_u8);
2186 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2187 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2188 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2189 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2190 nla_get_u16);
2191 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2192 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2193 nla_get_u32);
2194 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2195 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2196 nla_get_u16);
2197 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2198 dot11MeshHWMPnetDiameterTraversalTime,
2199 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2200 nla_get_u16);
2201
2202 /* Apply changes */
93da9cc1 2203 err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
93da9cc1 2204
f3f92586 2205 out:
93da9cc1 2206 /* cleanup */
2207 cfg80211_put_dev(drv);
2208 dev_put(dev);
3b85875a
JB
2209 out_rtnl:
2210 rtnl_unlock();
2211
93da9cc1 2212 return err;
2213}
2214
2215#undef FILL_IN_MESH_PARAM_IF_SET
2216
f130347c
LR
2217static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2218{
2219 struct sk_buff *msg;
2220 void *hdr = NULL;
2221 struct nlattr *nl_reg_rules;
2222 unsigned int i;
2223 int err = -EINVAL;
2224
a1794390 2225 mutex_lock(&cfg80211_mutex);
f130347c
LR
2226
2227 if (!cfg80211_regdomain)
2228 goto out;
2229
2230 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2231 if (!msg) {
2232 err = -ENOBUFS;
2233 goto out;
2234 }
2235
2236 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2237 NL80211_CMD_GET_REG);
2238 if (!hdr)
2239 goto nla_put_failure;
2240
2241 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2242 cfg80211_regdomain->alpha2);
2243
2244 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2245 if (!nl_reg_rules)
2246 goto nla_put_failure;
2247
2248 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2249 struct nlattr *nl_reg_rule;
2250 const struct ieee80211_reg_rule *reg_rule;
2251 const struct ieee80211_freq_range *freq_range;
2252 const struct ieee80211_power_rule *power_rule;
2253
2254 reg_rule = &cfg80211_regdomain->reg_rules[i];
2255 freq_range = &reg_rule->freq_range;
2256 power_rule = &reg_rule->power_rule;
2257
2258 nl_reg_rule = nla_nest_start(msg, i);
2259 if (!nl_reg_rule)
2260 goto nla_put_failure;
2261
2262 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2263 reg_rule->flags);
2264 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2265 freq_range->start_freq_khz);
2266 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2267 freq_range->end_freq_khz);
2268 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2269 freq_range->max_bandwidth_khz);
2270 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2271 power_rule->max_antenna_gain);
2272 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2273 power_rule->max_eirp);
2274
2275 nla_nest_end(msg, nl_reg_rule);
2276 }
2277
2278 nla_nest_end(msg, nl_reg_rules);
2279
2280 genlmsg_end(msg, hdr);
2281 err = genlmsg_unicast(msg, info->snd_pid);
2282 goto out;
2283
2284nla_put_failure:
2285 genlmsg_cancel(msg, hdr);
2286 err = -EMSGSIZE;
2287out:
a1794390 2288 mutex_unlock(&cfg80211_mutex);
f130347c
LR
2289 return err;
2290}
2291
b2e1b302
LR
2292static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2293{
2294 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2295 struct nlattr *nl_reg_rule;
2296 char *alpha2 = NULL;
2297 int rem_reg_rules = 0, r = 0;
2298 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2299 struct ieee80211_regdomain *rd = NULL;
2300
2301 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2302 return -EINVAL;
2303
2304 if (!info->attrs[NL80211_ATTR_REG_RULES])
2305 return -EINVAL;
2306
2307 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2308
2309 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2310 rem_reg_rules) {
2311 num_rules++;
2312 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2313 goto bad_reg;
2314 }
2315
2316 if (!reg_is_valid_request(alpha2))
2317 return -EINVAL;
2318
2319 size_of_regd = sizeof(struct ieee80211_regdomain) +
2320 (num_rules * sizeof(struct ieee80211_reg_rule));
2321
2322 rd = kzalloc(size_of_regd, GFP_KERNEL);
2323 if (!rd)
2324 return -ENOMEM;
2325
2326 rd->n_reg_rules = num_rules;
2327 rd->alpha2[0] = alpha2[0];
2328 rd->alpha2[1] = alpha2[1];
2329
2330 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2331 rem_reg_rules) {
2332 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2333 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2334 reg_rule_policy);
2335 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2336 if (r)
2337 goto bad_reg;
2338
2339 rule_idx++;
2340
2341 if (rule_idx > NL80211_MAX_SUPP_REG_RULES)
2342 goto bad_reg;
2343 }
2344
2345 BUG_ON(rule_idx != num_rules);
2346
a1794390 2347 mutex_lock(&cfg80211_mutex);
b2e1b302 2348 r = set_regdom(rd);
a1794390 2349 mutex_unlock(&cfg80211_mutex);
b2e1b302
LR
2350 return r;
2351
d2372b31 2352 bad_reg:
b2e1b302
LR
2353 kfree(rd);
2354 return -EINVAL;
2355}
2356
2a519311
JB
2357static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2358{
2359 struct cfg80211_registered_device *drv;
2360 struct net_device *dev;
2361 struct cfg80211_scan_request *request;
2362 struct cfg80211_ssid *ssid;
2363 struct ieee80211_channel *channel;
2364 struct nlattr *attr;
2365 struct wiphy *wiphy;
2366 int err, tmp, n_ssids = 0, n_channels = 0, i;
2367 enum ieee80211_band band;
70692ad2 2368 size_t ie_len;
2a519311 2369
3b85875a
JB
2370 rtnl_lock();
2371
2a519311
JB
2372 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2373 if (err)
3b85875a 2374 goto out_rtnl;
2a519311
JB
2375
2376 wiphy = &drv->wiphy;
2377
2378 if (!drv->ops->scan) {
2379 err = -EOPNOTSUPP;
2380 goto out;
2381 }
2382
2a519311
JB
2383 if (drv->scan_req) {
2384 err = -EBUSY;
3b85875a 2385 goto out;
2a519311
JB
2386 }
2387
2388 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2389 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp)
2390 n_channels++;
2391 if (!n_channels) {
2392 err = -EINVAL;
3b85875a 2393 goto out;
2a519311
JB
2394 }
2395 } else {
2396 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2397 if (wiphy->bands[band])
2398 n_channels += wiphy->bands[band]->n_channels;
2399 }
2400
2401 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2402 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2403 n_ssids++;
2404
2405 if (n_ssids > wiphy->max_scan_ssids) {
2406 err = -EINVAL;
3b85875a 2407 goto out;
2a519311
JB
2408 }
2409
70692ad2
JM
2410 if (info->attrs[NL80211_ATTR_IE])
2411 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2412 else
2413 ie_len = 0;
2414
2a519311
JB
2415 request = kzalloc(sizeof(*request)
2416 + sizeof(*ssid) * n_ssids
70692ad2
JM
2417 + sizeof(channel) * n_channels
2418 + ie_len, GFP_KERNEL);
2a519311
JB
2419 if (!request) {
2420 err = -ENOMEM;
3b85875a 2421 goto out;
2a519311
JB
2422 }
2423
2424 request->channels = (void *)((char *)request + sizeof(*request));
2425 request->n_channels = n_channels;
2426 if (n_ssids)
2427 request->ssids = (void *)(request->channels + n_channels);
2428 request->n_ssids = n_ssids;
70692ad2
JM
2429 if (ie_len) {
2430 if (request->ssids)
2431 request->ie = (void *)(request->ssids + n_ssids);
2432 else
2433 request->ie = (void *)(request->channels + n_channels);
2434 }
2a519311
JB
2435
2436 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2437 /* user specified, bail out if channel not found */
2438 request->n_channels = n_channels;
2439 i = 0;
2440 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2441 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2442 if (!request->channels[i]) {
2443 err = -EINVAL;
2444 goto out_free;
2445 }
2446 i++;
2447 }
2448 } else {
2449 /* all channels */
2450 i = 0;
2451 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2452 int j;
2453 if (!wiphy->bands[band])
2454 continue;
2455 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2456 request->channels[i] = &wiphy->bands[band]->channels[j];
2457 i++;
2458 }
2459 }
2460 }
2461
2462 i = 0;
2463 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2464 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2465 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2466 err = -EINVAL;
2467 goto out_free;
2468 }
2469 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2470 request->ssids[i].ssid_len = nla_len(attr);
2471 i++;
2472 }
2473 }
2474
70692ad2
JM
2475 if (info->attrs[NL80211_ATTR_IE]) {
2476 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2477 memcpy(request->ie, nla_data(info->attrs[NL80211_ATTR_IE]),
2478 request->ie_len);
2479 }
2480
2a519311
JB
2481 request->ifidx = dev->ifindex;
2482 request->wiphy = &drv->wiphy;
2483
2484 drv->scan_req = request;
2485 err = drv->ops->scan(&drv->wiphy, dev, request);
2486
2487 out_free:
2488 if (err) {
2489 drv->scan_req = NULL;
2490 kfree(request);
2491 }
2a519311
JB
2492 out:
2493 cfg80211_put_dev(drv);
2494 dev_put(dev);
3b85875a
JB
2495 out_rtnl:
2496 rtnl_unlock();
2497
2a519311
JB
2498 return err;
2499}
2500
2501static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2502 struct cfg80211_registered_device *rdev,
2503 struct net_device *dev,
2504 struct cfg80211_bss *res)
2505{
2506 void *hdr;
2507 struct nlattr *bss;
2508
2509 hdr = nl80211hdr_put(msg, pid, seq, flags,
2510 NL80211_CMD_NEW_SCAN_RESULTS);
2511 if (!hdr)
2512 return -1;
2513
2514 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2515 rdev->bss_generation);
2516 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2517
2518 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2519 if (!bss)
2520 goto nla_put_failure;
2521 if (!is_zero_ether_addr(res->bssid))
2522 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2523 if (res->information_elements && res->len_information_elements)
2524 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2525 res->len_information_elements,
2526 res->information_elements);
2527 if (res->tsf)
2528 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2529 if (res->beacon_interval)
2530 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2531 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2532 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2533
77965c97 2534 switch (rdev->wiphy.signal_type) {
2a519311
JB
2535 case CFG80211_SIGNAL_TYPE_MBM:
2536 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2537 break;
2538 case CFG80211_SIGNAL_TYPE_UNSPEC:
2539 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2540 break;
2541 default:
2542 break;
2543 }
2544
2545 nla_nest_end(msg, bss);
2546
2547 return genlmsg_end(msg, hdr);
2548
2549 nla_put_failure:
2550 genlmsg_cancel(msg, hdr);
2551 return -EMSGSIZE;
2552}
2553
2554static int nl80211_dump_scan(struct sk_buff *skb,
2555 struct netlink_callback *cb)
2556{
2557 struct cfg80211_registered_device *dev;
2558 struct net_device *netdev;
2559 struct cfg80211_internal_bss *scan;
2560 int ifidx = cb->args[0];
2561 int start = cb->args[1], idx = 0;
2562 int err;
2563
2564 if (!ifidx) {
2565 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2566 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2567 nl80211_policy);
2568 if (err)
2569 return err;
2570
2571 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2572 return -EINVAL;
2573
2574 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2575 if (!ifidx)
2576 return -EINVAL;
2577 cb->args[0] = ifidx;
2578 }
2579
2580 netdev = dev_get_by_index(&init_net, ifidx);
2581 if (!netdev)
2582 return -ENODEV;
2583
2584 dev = cfg80211_get_dev_from_ifindex(ifidx);
2585 if (IS_ERR(dev)) {
2586 err = PTR_ERR(dev);
2587 goto out_put_netdev;
2588 }
2589
2590 spin_lock_bh(&dev->bss_lock);
2591 cfg80211_bss_expire(dev);
2592
2593 list_for_each_entry(scan, &dev->bss_list, list) {
2594 if (++idx <= start)
2595 continue;
2596 if (nl80211_send_bss(skb,
2597 NETLINK_CB(cb->skb).pid,
2598 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2599 dev, netdev, &scan->pub) < 0) {
2600 idx--;
2601 goto out;
2602 }
2603 }
2604
2605 out:
2606 spin_unlock_bh(&dev->bss_lock);
2607
2608 cb->args[1] = idx;
2609 err = skb->len;
2610 cfg80211_put_dev(dev);
2611 out_put_netdev:
2612 dev_put(netdev);
2613
2614 return err;
2615}
2616
636a5d36
JM
2617static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
2618{
2619 struct cfg80211_registered_device *drv;
2620 struct net_device *dev;
2621 struct cfg80211_auth_request req;
2622 struct wiphy *wiphy;
2623 int err;
2624
2625 rtnl_lock();
2626
2627 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2628 if (err)
2629 goto unlock_rtnl;
2630
2631 if (!drv->ops->auth) {
2632 err = -EOPNOTSUPP;
2633 goto out;
2634 }
2635
2636 if (!info->attrs[NL80211_ATTR_MAC]) {
2637 err = -EINVAL;
2638 goto out;
2639 }
2640
2641 wiphy = &drv->wiphy;
2642 memset(&req, 0, sizeof(req));
2643
2644 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2645
2646 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2647 req.chan = ieee80211_get_channel(
2648 wiphy,
2649 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2650 if (!req.chan) {
2651 err = -EINVAL;
2652 goto out;
2653 }
2654 }
2655
2656 if (info->attrs[NL80211_ATTR_SSID]) {
2657 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2658 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
2659 }
2660
2661 if (info->attrs[NL80211_ATTR_IE]) {
2662 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2663 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2664 }
2665
2666 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2667 req.auth_type =
2668 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
2669 }
2670
2671 err = drv->ops->auth(&drv->wiphy, dev, &req);
2672
2673out:
2674 cfg80211_put_dev(drv);
2675 dev_put(dev);
2676unlock_rtnl:
2677 rtnl_unlock();
2678 return err;
2679}
2680
2681static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
2682{
2683 struct cfg80211_registered_device *drv;
2684 struct net_device *dev;
2685 struct cfg80211_assoc_request req;
2686 struct wiphy *wiphy;
2687 int err;
2688
2689 rtnl_lock();
2690
2691 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2692 if (err)
2693 goto unlock_rtnl;
2694
2695 if (!drv->ops->assoc) {
2696 err = -EOPNOTSUPP;
2697 goto out;
2698 }
2699
2700 if (!info->attrs[NL80211_ATTR_MAC] ||
2701 !info->attrs[NL80211_ATTR_SSID]) {
2702 err = -EINVAL;
2703 goto out;
2704 }
2705
2706 wiphy = &drv->wiphy;
2707 memset(&req, 0, sizeof(req));
2708
2709 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2710
2711 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2712 req.chan = ieee80211_get_channel(
2713 wiphy,
2714 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2715 if (!req.chan) {
2716 err = -EINVAL;
2717 goto out;
2718 }
2719 }
2720
2721 if (nla_len(info->attrs[NL80211_ATTR_SSID]) > IEEE80211_MAX_SSID_LEN) {
2722 err = -EINVAL;
2723 goto out;
2724 }
2725 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2726 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
2727
2728 if (info->attrs[NL80211_ATTR_IE]) {
2729 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2730 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2731 }
2732
2733 err = drv->ops->assoc(&drv->wiphy, dev, &req);
2734
2735out:
2736 cfg80211_put_dev(drv);
2737 dev_put(dev);
2738unlock_rtnl:
2739 rtnl_unlock();
2740 return err;
2741}
2742
2743static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
2744{
2745 struct cfg80211_registered_device *drv;
2746 struct net_device *dev;
2747 struct cfg80211_deauth_request req;
2748 struct wiphy *wiphy;
2749 int err;
2750
2751 rtnl_lock();
2752
2753 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2754 if (err)
2755 goto unlock_rtnl;
2756
2757 if (!drv->ops->deauth) {
2758 err = -EOPNOTSUPP;
2759 goto out;
2760 }
2761
2762 if (!info->attrs[NL80211_ATTR_MAC]) {
2763 err = -EINVAL;
2764 goto out;
2765 }
2766
2767 wiphy = &drv->wiphy;
2768 memset(&req, 0, sizeof(req));
2769
2770 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2771
2772 if (info->attrs[NL80211_ATTR_REASON_CODE])
2773 req.reason_code =
2774 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
2775
2776 if (info->attrs[NL80211_ATTR_IE]) {
2777 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2778 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2779 }
2780
2781 err = drv->ops->deauth(&drv->wiphy, dev, &req);
2782
2783out:
2784 cfg80211_put_dev(drv);
2785 dev_put(dev);
2786unlock_rtnl:
2787 rtnl_unlock();
2788 return err;
2789}
2790
2791static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
2792{
2793 struct cfg80211_registered_device *drv;
2794 struct net_device *dev;
2795 struct cfg80211_disassoc_request req;
2796 struct wiphy *wiphy;
2797 int err;
2798
2799 rtnl_lock();
2800
2801 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2802 if (err)
2803 goto unlock_rtnl;
2804
2805 if (!drv->ops->disassoc) {
2806 err = -EOPNOTSUPP;
2807 goto out;
2808 }
2809
2810 if (!info->attrs[NL80211_ATTR_MAC]) {
2811 err = -EINVAL;
2812 goto out;
2813 }
2814
2815 wiphy = &drv->wiphy;
2816 memset(&req, 0, sizeof(req));
2817
2818 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2819
2820 if (info->attrs[NL80211_ATTR_REASON_CODE])
2821 req.reason_code =
2822 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
2823
2824 if (info->attrs[NL80211_ATTR_IE]) {
2825 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2826 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2827 }
2828
2829 err = drv->ops->disassoc(&drv->wiphy, dev, &req);
2830
2831out:
2832 cfg80211_put_dev(drv);
2833 dev_put(dev);
2834unlock_rtnl:
2835 rtnl_unlock();
2836 return err;
2837}
2838
55682965
JB
2839static struct genl_ops nl80211_ops[] = {
2840 {
2841 .cmd = NL80211_CMD_GET_WIPHY,
2842 .doit = nl80211_get_wiphy,
2843 .dumpit = nl80211_dump_wiphy,
2844 .policy = nl80211_policy,
2845 /* can be retrieved by unprivileged users */
2846 },
2847 {
2848 .cmd = NL80211_CMD_SET_WIPHY,
2849 .doit = nl80211_set_wiphy,
2850 .policy = nl80211_policy,
2851 .flags = GENL_ADMIN_PERM,
2852 },
2853 {
2854 .cmd = NL80211_CMD_GET_INTERFACE,
2855 .doit = nl80211_get_interface,
2856 .dumpit = nl80211_dump_interface,
2857 .policy = nl80211_policy,
2858 /* can be retrieved by unprivileged users */
2859 },
2860 {
2861 .cmd = NL80211_CMD_SET_INTERFACE,
2862 .doit = nl80211_set_interface,
2863 .policy = nl80211_policy,
2864 .flags = GENL_ADMIN_PERM,
2865 },
2866 {
2867 .cmd = NL80211_CMD_NEW_INTERFACE,
2868 .doit = nl80211_new_interface,
2869 .policy = nl80211_policy,
2870 .flags = GENL_ADMIN_PERM,
2871 },
2872 {
2873 .cmd = NL80211_CMD_DEL_INTERFACE,
2874 .doit = nl80211_del_interface,
2875 .policy = nl80211_policy,
41ade00f
JB
2876 .flags = GENL_ADMIN_PERM,
2877 },
2878 {
2879 .cmd = NL80211_CMD_GET_KEY,
2880 .doit = nl80211_get_key,
2881 .policy = nl80211_policy,
2882 .flags = GENL_ADMIN_PERM,
2883 },
2884 {
2885 .cmd = NL80211_CMD_SET_KEY,
2886 .doit = nl80211_set_key,
2887 .policy = nl80211_policy,
2888 .flags = GENL_ADMIN_PERM,
2889 },
2890 {
2891 .cmd = NL80211_CMD_NEW_KEY,
2892 .doit = nl80211_new_key,
2893 .policy = nl80211_policy,
2894 .flags = GENL_ADMIN_PERM,
2895 },
2896 {
2897 .cmd = NL80211_CMD_DEL_KEY,
2898 .doit = nl80211_del_key,
2899 .policy = nl80211_policy,
55682965
JB
2900 .flags = GENL_ADMIN_PERM,
2901 },
ed1b6cc7
JB
2902 {
2903 .cmd = NL80211_CMD_SET_BEACON,
2904 .policy = nl80211_policy,
2905 .flags = GENL_ADMIN_PERM,
2906 .doit = nl80211_addset_beacon,
2907 },
2908 {
2909 .cmd = NL80211_CMD_NEW_BEACON,
2910 .policy = nl80211_policy,
2911 .flags = GENL_ADMIN_PERM,
2912 .doit = nl80211_addset_beacon,
2913 },
2914 {
2915 .cmd = NL80211_CMD_DEL_BEACON,
2916 .policy = nl80211_policy,
2917 .flags = GENL_ADMIN_PERM,
2918 .doit = nl80211_del_beacon,
2919 },
5727ef1b
JB
2920 {
2921 .cmd = NL80211_CMD_GET_STATION,
2922 .doit = nl80211_get_station,
2ec600d6 2923 .dumpit = nl80211_dump_station,
5727ef1b 2924 .policy = nl80211_policy,
5727ef1b
JB
2925 },
2926 {
2927 .cmd = NL80211_CMD_SET_STATION,
2928 .doit = nl80211_set_station,
2929 .policy = nl80211_policy,
2930 .flags = GENL_ADMIN_PERM,
2931 },
2932 {
2933 .cmd = NL80211_CMD_NEW_STATION,
2934 .doit = nl80211_new_station,
2935 .policy = nl80211_policy,
2936 .flags = GENL_ADMIN_PERM,
2937 },
2938 {
2939 .cmd = NL80211_CMD_DEL_STATION,
2940 .doit = nl80211_del_station,
2941 .policy = nl80211_policy,
2ec600d6
LCC
2942 .flags = GENL_ADMIN_PERM,
2943 },
2944 {
2945 .cmd = NL80211_CMD_GET_MPATH,
2946 .doit = nl80211_get_mpath,
2947 .dumpit = nl80211_dump_mpath,
2948 .policy = nl80211_policy,
2949 .flags = GENL_ADMIN_PERM,
2950 },
2951 {
2952 .cmd = NL80211_CMD_SET_MPATH,
2953 .doit = nl80211_set_mpath,
2954 .policy = nl80211_policy,
2955 .flags = GENL_ADMIN_PERM,
2956 },
2957 {
2958 .cmd = NL80211_CMD_NEW_MPATH,
2959 .doit = nl80211_new_mpath,
2960 .policy = nl80211_policy,
2961 .flags = GENL_ADMIN_PERM,
2962 },
2963 {
2964 .cmd = NL80211_CMD_DEL_MPATH,
2965 .doit = nl80211_del_mpath,
2966 .policy = nl80211_policy,
9f1ba906
JM
2967 .flags = GENL_ADMIN_PERM,
2968 },
2969 {
2970 .cmd = NL80211_CMD_SET_BSS,
2971 .doit = nl80211_set_bss,
2972 .policy = nl80211_policy,
b2e1b302
LR
2973 .flags = GENL_ADMIN_PERM,
2974 },
f130347c
LR
2975 {
2976 .cmd = NL80211_CMD_GET_REG,
2977 .doit = nl80211_get_reg,
2978 .policy = nl80211_policy,
2979 /* can be retrieved by unprivileged users */
2980 },
b2e1b302
LR
2981 {
2982 .cmd = NL80211_CMD_SET_REG,
2983 .doit = nl80211_set_reg,
2984 .policy = nl80211_policy,
2985 .flags = GENL_ADMIN_PERM,
2986 },
2987 {
2988 .cmd = NL80211_CMD_REQ_SET_REG,
2989 .doit = nl80211_req_set_reg,
2990 .policy = nl80211_policy,
93da9cc1 2991 .flags = GENL_ADMIN_PERM,
2992 },
2993 {
2994 .cmd = NL80211_CMD_GET_MESH_PARAMS,
2995 .doit = nl80211_get_mesh_params,
2996 .policy = nl80211_policy,
2997 /* can be retrieved by unprivileged users */
2998 },
2999 {
3000 .cmd = NL80211_CMD_SET_MESH_PARAMS,
3001 .doit = nl80211_set_mesh_params,
3002 .policy = nl80211_policy,
9aed3cc1
JM
3003 .flags = GENL_ADMIN_PERM,
3004 },
2a519311
JB
3005 {
3006 .cmd = NL80211_CMD_TRIGGER_SCAN,
3007 .doit = nl80211_trigger_scan,
3008 .policy = nl80211_policy,
3009 .flags = GENL_ADMIN_PERM,
3010 },
3011 {
3012 .cmd = NL80211_CMD_GET_SCAN,
3013 .policy = nl80211_policy,
3014 .dumpit = nl80211_dump_scan,
3015 },
636a5d36
JM
3016 {
3017 .cmd = NL80211_CMD_AUTHENTICATE,
3018 .doit = nl80211_authenticate,
3019 .policy = nl80211_policy,
3020 .flags = GENL_ADMIN_PERM,
3021 },
3022 {
3023 .cmd = NL80211_CMD_ASSOCIATE,
3024 .doit = nl80211_associate,
3025 .policy = nl80211_policy,
3026 .flags = GENL_ADMIN_PERM,
3027 },
3028 {
3029 .cmd = NL80211_CMD_DEAUTHENTICATE,
3030 .doit = nl80211_deauthenticate,
3031 .policy = nl80211_policy,
3032 .flags = GENL_ADMIN_PERM,
3033 },
3034 {
3035 .cmd = NL80211_CMD_DISASSOCIATE,
3036 .doit = nl80211_disassociate,
3037 .policy = nl80211_policy,
3038 .flags = GENL_ADMIN_PERM,
3039 },
55682965 3040};
6039f6d2
JM
3041static struct genl_multicast_group nl80211_mlme_mcgrp = {
3042 .name = "mlme",
3043};
55682965
JB
3044
3045/* multicast groups */
3046static struct genl_multicast_group nl80211_config_mcgrp = {
3047 .name = "config",
3048};
2a519311
JB
3049static struct genl_multicast_group nl80211_scan_mcgrp = {
3050 .name = "scan",
3051};
73d54c9e
LR
3052static struct genl_multicast_group nl80211_regulatory_mcgrp = {
3053 .name = "regulatory",
3054};
55682965
JB
3055
3056/* notification functions */
3057
3058void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
3059{
3060 struct sk_buff *msg;
3061
3062 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3063 if (!msg)
3064 return;
3065
3066 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
3067 nlmsg_free(msg);
3068 return;
3069 }
3070
3071 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
3072}
3073
2a519311
JB
3074static int nl80211_send_scan_donemsg(struct sk_buff *msg,
3075 struct cfg80211_registered_device *rdev,
3076 struct net_device *netdev,
3077 u32 pid, u32 seq, int flags,
3078 u32 cmd)
3079{
3080 void *hdr;
3081
3082 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
3083 if (!hdr)
3084 return -1;
3085
b5850a7a 3086 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
2a519311
JB
3087 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3088
3089 /* XXX: we should probably bounce back the request? */
3090
3091 return genlmsg_end(msg, hdr);
3092
3093 nla_put_failure:
3094 genlmsg_cancel(msg, hdr);
3095 return -EMSGSIZE;
3096}
3097
3098void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
3099 struct net_device *netdev)
3100{
3101 struct sk_buff *msg;
3102
3103 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3104 if (!msg)
3105 return;
3106
3107 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3108 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
3109 nlmsg_free(msg);
3110 return;
3111 }
3112
3113 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3114}
3115
3116void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
3117 struct net_device *netdev)
3118{
3119 struct sk_buff *msg;
3120
3121 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3122 if (!msg)
3123 return;
3124
3125 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3126 NL80211_CMD_SCAN_ABORTED) < 0) {
3127 nlmsg_free(msg);
3128 return;
3129 }
3130
3131 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3132}
3133
73d54c9e
LR
3134/*
3135 * This can happen on global regulatory changes or device specific settings
3136 * based on custom world regulatory domains.
3137 */
3138void nl80211_send_reg_change_event(struct regulatory_request *request)
3139{
3140 struct sk_buff *msg;
3141 void *hdr;
3142
3143 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3144 if (!msg)
3145 return;
3146
3147 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
3148 if (!hdr) {
3149 nlmsg_free(msg);
3150 return;
3151 }
3152
3153 /* Userspace can always count this one always being set */
3154 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
3155
3156 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
3157 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3158 NL80211_REGDOM_TYPE_WORLD);
3159 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
3160 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3161 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
3162 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
3163 request->intersect)
3164 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3165 NL80211_REGDOM_TYPE_INTERSECTION);
3166 else {
3167 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3168 NL80211_REGDOM_TYPE_COUNTRY);
3169 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
3170 }
3171
3172 if (wiphy_idx_valid(request->wiphy_idx))
3173 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
3174
3175 if (genlmsg_end(msg, hdr) < 0) {
3176 nlmsg_free(msg);
3177 return;
3178 }
3179
3180 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL);
3181
3182 return;
3183
3184nla_put_failure:
3185 genlmsg_cancel(msg, hdr);
3186 nlmsg_free(msg);
3187}
3188
6039f6d2
JM
3189static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
3190 struct net_device *netdev,
3191 const u8 *buf, size_t len,
3192 enum nl80211_commands cmd)
3193{
3194 struct sk_buff *msg;
3195 void *hdr;
3196
3197 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3198 if (!msg)
3199 return;
3200
3201 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
3202 if (!hdr) {
3203 nlmsg_free(msg);
3204 return;
3205 }
3206
3207 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3208 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3209 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
3210
3211 if (genlmsg_end(msg, hdr) < 0) {
3212 nlmsg_free(msg);
3213 return;
3214 }
3215
3216 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_KERNEL);
3217 return;
3218
3219 nla_put_failure:
3220 genlmsg_cancel(msg, hdr);
3221 nlmsg_free(msg);
3222}
3223
3224void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
3225 struct net_device *netdev, const u8 *buf, size_t len)
3226{
3227 nl80211_send_mlme_event(rdev, netdev, buf, len,
3228 NL80211_CMD_AUTHENTICATE);
3229}
3230
3231void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
3232 struct net_device *netdev, const u8 *buf,
3233 size_t len)
3234{
3235 nl80211_send_mlme_event(rdev, netdev, buf, len, NL80211_CMD_ASSOCIATE);
3236}
3237
3238void nl80211_send_rx_deauth(struct cfg80211_registered_device *rdev,
3239 struct net_device *netdev, const u8 *buf,
3240 size_t len)
3241{
3242 nl80211_send_mlme_event(rdev, netdev, buf, len,
3243 NL80211_CMD_DEAUTHENTICATE);
3244}
3245
3246void nl80211_send_rx_disassoc(struct cfg80211_registered_device *rdev,
3247 struct net_device *netdev, const u8 *buf,
3248 size_t len)
3249{
3250 nl80211_send_mlme_event(rdev, netdev, buf, len,
3251 NL80211_CMD_DISASSOCIATE);
3252}
3253
55682965
JB
3254/* initialisation/exit functions */
3255
3256int nl80211_init(void)
3257{
3258 int err, i;
3259
3260 err = genl_register_family(&nl80211_fam);
3261 if (err)
3262 return err;
3263
3264 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
3265 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
3266 if (err)
3267 goto err_out;
3268 }
3269
3270 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
3271 if (err)
3272 goto err_out;
3273
2a519311
JB
3274 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
3275 if (err)
3276 goto err_out;
3277
73d54c9e
LR
3278 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
3279 if (err)
3280 goto err_out;
3281
6039f6d2
JM
3282 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
3283 if (err)
3284 goto err_out;
3285
55682965
JB
3286 return 0;
3287 err_out:
3288 genl_unregister_family(&nl80211_fam);
3289 return err;
3290}
3291
3292void nl80211_exit(void)
3293{
3294 genl_unregister_family(&nl80211_fam);
3295}