blob: 67b18b3a93a03f8407d7971ef92d8a0db97b0c22 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
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>
10#include <linux/mutex.h>
11#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg55682962007-09-20 13:09:35 -040018#include <net/genetlink.h>
19#include <net/cfg80211.h>
20#include "core.h"
21#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070022#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040023
24/* the netlink family */
25static struct genl_family nl80211_fam = {
26 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
27 .name = "nl80211", /* have users key off the name instead */
28 .hdrsize = 0, /* no private header */
29 .version = 1, /* no particular meaning now */
30 .maxattr = NL80211_ATTR_MAX,
31};
32
33/* internal helper: get drv and dev */
Johannes Bergbba95fe2008-07-29 13:22:51 +020034static int get_drv_dev_by_info_ifindex(struct nlattr **attrs,
Johannes Berg55682962007-09-20 13:09:35 -040035 struct cfg80211_registered_device **drv,
36 struct net_device **dev)
37{
38 int ifindex;
39
Johannes Bergbba95fe2008-07-29 13:22:51 +020040 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040041 return -EINVAL;
42
Johannes Bergbba95fe2008-07-29 13:22:51 +020043 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg55682962007-09-20 13:09:35 -040044 *dev = dev_get_by_index(&init_net, ifindex);
45 if (!*dev)
46 return -ENODEV;
47
48 *drv = cfg80211_get_dev_from_ifindex(ifindex);
49 if (IS_ERR(*drv)) {
50 dev_put(*dev);
51 return PTR_ERR(*drv);
52 }
53
54 return 0;
55}
56
57/* policy for the attributes */
58static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
59 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
60 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
61 .len = BUS_ID_SIZE-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020062 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020063 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053064 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -040065
66 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
67 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
68 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010069
70 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
71
72 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
73 .len = WLAN_MAX_KEY_LEN },
74 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
75 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
76 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Johannes Berged1b6cc2007-12-19 02:03:32 +010077
78 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
79 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
80 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
81 .len = IEEE80211_MAX_DATA_LEN },
82 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
83 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010084 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
85 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
86 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
87 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
88 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010089 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +010090 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +020091 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010092 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
93 .len = IEEE80211_MAX_MESH_ID_LEN },
94 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +030095
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070096 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
97 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
98
Jouni Malinen9f1ba902008-08-07 20:07:01 +030099 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
100 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
101 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200102 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
103 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300104
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700105 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
106
Jouni Malinen36aedc92008-08-25 11:58:58 +0300107 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
108 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200109
110 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
111 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
112 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100113 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
114 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Johannes Berg55682962007-09-20 13:09:35 -0400115};
116
117/* message building helper */
118static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
119 int flags, u8 cmd)
120{
121 /* since there is no private header just add the generic one */
122 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
123}
124
125/* netlink command implementations */
126
127static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
128 struct cfg80211_registered_device *dev)
129{
130 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100131 struct nlattr *nl_bands, *nl_band;
132 struct nlattr *nl_freqs, *nl_freq;
133 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700134 struct nlattr *nl_modes;
Johannes Bergee688b002008-01-24 19:38:39 +0100135 enum ieee80211_band band;
136 struct ieee80211_channel *chan;
137 struct ieee80211_rate *rate;
138 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700139 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400140
141 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
142 if (!hdr)
143 return -1;
144
145 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->idx);
146 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Johannes Berg2a519312009-02-10 21:25:55 +0100147 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
148 dev->wiphy.max_scan_ssids);
Johannes Bergee688b002008-01-24 19:38:39 +0100149
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700150 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
151 if (!nl_modes)
152 goto nla_put_failure;
153
154 i = 0;
155 while (ifmodes) {
156 if (ifmodes & 1)
157 NLA_PUT_FLAG(msg, i);
158 ifmodes >>= 1;
159 i++;
160 }
161
162 nla_nest_end(msg, nl_modes);
163
Johannes Bergee688b002008-01-24 19:38:39 +0100164 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
165 if (!nl_bands)
166 goto nla_put_failure;
167
168 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
169 if (!dev->wiphy.bands[band])
170 continue;
171
172 nl_band = nla_nest_start(msg, band);
173 if (!nl_band)
174 goto nla_put_failure;
175
Johannes Bergd51626d2008-10-09 12:20:13 +0200176 /* add HT info */
177 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
178 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
179 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
180 &dev->wiphy.bands[band]->ht_cap.mcs);
181 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
182 dev->wiphy.bands[band]->ht_cap.cap);
183 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
184 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
185 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
186 dev->wiphy.bands[band]->ht_cap.ampdu_density);
187 }
188
Johannes Bergee688b002008-01-24 19:38:39 +0100189 /* add frequencies */
190 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
191 if (!nl_freqs)
192 goto nla_put_failure;
193
194 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
195 nl_freq = nla_nest_start(msg, i);
196 if (!nl_freq)
197 goto nla_put_failure;
198
199 chan = &dev->wiphy.bands[band]->channels[i];
200 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
201 chan->center_freq);
202
203 if (chan->flags & IEEE80211_CHAN_DISABLED)
204 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
205 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
206 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
207 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
208 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
209 if (chan->flags & IEEE80211_CHAN_RADAR)
210 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
211
Jouni Malinenbf8c1ac2008-11-22 22:00:31 +0200212 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
213 DBM_TO_MBM(chan->max_power));
Jouni Malinene2f367f262008-11-21 19:01:30 +0200214
Johannes Bergee688b002008-01-24 19:38:39 +0100215 nla_nest_end(msg, nl_freq);
216 }
217
218 nla_nest_end(msg, nl_freqs);
219
220 /* add bitrates */
221 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
222 if (!nl_rates)
223 goto nla_put_failure;
224
225 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
226 nl_rate = nla_nest_start(msg, i);
227 if (!nl_rate)
228 goto nla_put_failure;
229
230 rate = &dev->wiphy.bands[band]->bitrates[i];
231 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
232 rate->bitrate);
233 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
234 NLA_PUT_FLAG(msg,
235 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
236
237 nla_nest_end(msg, nl_rate);
238 }
239
240 nla_nest_end(msg, nl_rates);
241
242 nla_nest_end(msg, nl_band);
243 }
244 nla_nest_end(msg, nl_bands);
245
Johannes Berg55682962007-09-20 13:09:35 -0400246 return genlmsg_end(msg, hdr);
247
248 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700249 genlmsg_cancel(msg, hdr);
250 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400251}
252
253static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
254{
255 int idx = 0;
256 int start = cb->args[0];
257 struct cfg80211_registered_device *dev;
258
259 mutex_lock(&cfg80211_drv_mutex);
260 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Julius Volzb4637272008-07-08 14:02:19 +0200261 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400262 continue;
263 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
264 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200265 dev) < 0) {
266 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400267 break;
Julius Volzb4637272008-07-08 14:02:19 +0200268 }
Johannes Berg55682962007-09-20 13:09:35 -0400269 }
270 mutex_unlock(&cfg80211_drv_mutex);
271
272 cb->args[0] = idx;
273
274 return skb->len;
275}
276
277static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
278{
279 struct sk_buff *msg;
280 struct cfg80211_registered_device *dev;
281
282 dev = cfg80211_get_dev_from_info(info);
283 if (IS_ERR(dev))
284 return PTR_ERR(dev);
285
286 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
287 if (!msg)
288 goto out_err;
289
290 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
291 goto out_free;
292
293 cfg80211_put_dev(dev);
294
295 return genlmsg_unicast(msg, info->snd_pid);
296
297 out_free:
298 nlmsg_free(msg);
299 out_err:
300 cfg80211_put_dev(dev);
301 return -ENOBUFS;
302}
303
Jouni Malinen31888482008-10-30 16:59:24 +0200304static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
305 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
306 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
307 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
308 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
309 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
310};
311
312static int parse_txq_params(struct nlattr *tb[],
313 struct ieee80211_txq_params *txq_params)
314{
315 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
316 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
317 !tb[NL80211_TXQ_ATTR_AIFS])
318 return -EINVAL;
319
320 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
321 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
322 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
323 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
324 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
325
326 return 0;
327}
328
Johannes Berg55682962007-09-20 13:09:35 -0400329static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
330{
331 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200332 int result = 0, rem_txq_params = 0;
333 struct nlattr *nl_txq_params;
Johannes Berg55682962007-09-20 13:09:35 -0400334
335 rdev = cfg80211_get_dev_from_info(info);
336 if (IS_ERR(rdev))
337 return PTR_ERR(rdev);
338
Jouni Malinen31888482008-10-30 16:59:24 +0200339 if (info->attrs[NL80211_ATTR_WIPHY_NAME]) {
340 result = cfg80211_dev_rename(
341 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
342 if (result)
343 goto bad_res;
344 }
Johannes Berg55682962007-09-20 13:09:35 -0400345
Jouni Malinen31888482008-10-30 16:59:24 +0200346 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
347 struct ieee80211_txq_params txq_params;
348 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
349
350 if (!rdev->ops->set_txq_params) {
351 result = -EOPNOTSUPP;
352 goto bad_res;
353 }
354
355 nla_for_each_nested(nl_txq_params,
356 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
357 rem_txq_params) {
358 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
359 nla_data(nl_txq_params),
360 nla_len(nl_txq_params),
361 txq_params_policy);
362 result = parse_txq_params(tb, &txq_params);
363 if (result)
364 goto bad_res;
365
366 result = rdev->ops->set_txq_params(&rdev->wiphy,
367 &txq_params);
368 if (result)
369 goto bad_res;
370 }
371 }
372
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200373 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530374 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200375 struct ieee80211_channel *chan;
Johannes Berg306d6112008-12-08 12:39:04 +0100376 struct ieee80211_sta_ht_cap *ht_cap;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200377 u32 freq, sec_freq;
378
379 if (!rdev->ops->set_channel) {
380 result = -EOPNOTSUPP;
381 goto bad_res;
382 }
383
Johannes Berg306d6112008-12-08 12:39:04 +0100384 result = -EINVAL;
385
Sujith094d05d2008-12-12 11:57:43 +0530386 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
387 channel_type = nla_get_u32(info->attrs[
388 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
389 if (channel_type != NL80211_CHAN_NO_HT &&
390 channel_type != NL80211_CHAN_HT20 &&
391 channel_type != NL80211_CHAN_HT40PLUS &&
392 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200393 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200394 }
395
396 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
397 chan = ieee80211_get_channel(&rdev->wiphy, freq);
Johannes Berg306d6112008-12-08 12:39:04 +0100398
399 /* Primary channel not allowed */
400 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200401 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100402
Sujith094d05d2008-12-12 11:57:43 +0530403 if (channel_type == NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200404 sec_freq = freq - 20;
Sujith094d05d2008-12-12 11:57:43 +0530405 else if (channel_type == NL80211_CHAN_HT40PLUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200406 sec_freq = freq + 20;
407 else
408 sec_freq = 0;
409
Johannes Berg306d6112008-12-08 12:39:04 +0100410 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
411
412 /* no HT capabilities */
Sujith094d05d2008-12-12 11:57:43 +0530413 if (channel_type != NL80211_CHAN_NO_HT &&
Johannes Berg306d6112008-12-08 12:39:04 +0100414 !ht_cap->ht_supported)
415 goto bad_res;
416
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200417 if (sec_freq) {
418 struct ieee80211_channel *schan;
Johannes Berg306d6112008-12-08 12:39:04 +0100419
420 /* no 40 MHz capabilities */
421 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
422 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200423 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100424
425 schan = ieee80211_get_channel(&rdev->wiphy, sec_freq);
426
427 /* Secondary channel not allowed */
428 if (!schan || schan->flags & IEEE80211_CHAN_DISABLED)
429 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200430 }
431
432 result = rdev->ops->set_channel(&rdev->wiphy, chan,
Sujith094d05d2008-12-12 11:57:43 +0530433 channel_type);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200434 if (result)
435 goto bad_res;
436 }
437
438
Johannes Berg306d6112008-12-08 12:39:04 +0100439 bad_res:
Johannes Berg55682962007-09-20 13:09:35 -0400440 cfg80211_put_dev(rdev);
441 return result;
442}
443
444
445static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
446 struct net_device *dev)
447{
448 void *hdr;
449
450 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
451 if (!hdr)
452 return -1;
453
454 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
455 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200456 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Berg55682962007-09-20 13:09:35 -0400457 return genlmsg_end(msg, hdr);
458
459 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700460 genlmsg_cancel(msg, hdr);
461 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400462}
463
464static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
465{
466 int wp_idx = 0;
467 int if_idx = 0;
468 int wp_start = cb->args[0];
469 int if_start = cb->args[1];
470 struct cfg80211_registered_device *dev;
471 struct wireless_dev *wdev;
472
473 mutex_lock(&cfg80211_drv_mutex);
474 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200475 if (wp_idx < wp_start) {
476 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400477 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200478 }
Johannes Berg55682962007-09-20 13:09:35 -0400479 if_idx = 0;
480
481 mutex_lock(&dev->devlist_mtx);
482 list_for_each_entry(wdev, &dev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200483 if (if_idx < if_start) {
484 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400485 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200486 }
Johannes Berg55682962007-09-20 13:09:35 -0400487 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
488 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergbba95fe2008-07-29 13:22:51 +0200489 wdev->netdev) < 0) {
490 mutex_unlock(&dev->devlist_mtx);
491 goto out;
492 }
493 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400494 }
495 mutex_unlock(&dev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200496
497 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400498 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200499 out:
Johannes Berg55682962007-09-20 13:09:35 -0400500 mutex_unlock(&cfg80211_drv_mutex);
501
502 cb->args[0] = wp_idx;
503 cb->args[1] = if_idx;
504
505 return skb->len;
506}
507
508static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
509{
510 struct sk_buff *msg;
511 struct cfg80211_registered_device *dev;
512 struct net_device *netdev;
513 int err;
514
Johannes Bergbba95fe2008-07-29 13:22:51 +0200515 err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400516 if (err)
517 return err;
518
519 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
520 if (!msg)
521 goto out_err;
522
523 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
524 goto out_free;
525
526 dev_put(netdev);
527 cfg80211_put_dev(dev);
528
529 return genlmsg_unicast(msg, info->snd_pid);
530
531 out_free:
532 nlmsg_free(msg);
533 out_err:
534 dev_put(netdev);
535 cfg80211_put_dev(dev);
536 return -ENOBUFS;
537}
538
Michael Wu66f7ac52008-01-31 19:48:22 +0100539static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
540 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
541 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
542 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
543 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
544 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
545};
546
547static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
548{
549 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
550 int flag;
551
552 *mntrflags = 0;
553
554 if (!nla)
555 return -EINVAL;
556
557 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
558 nla, mntr_flags_policy))
559 return -EINVAL;
560
561 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
562 if (flags[flag])
563 *mntrflags |= (1<<flag);
564
565 return 0;
566}
567
Johannes Berg55682962007-09-20 13:09:35 -0400568static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
569{
570 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100571 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400572 int err, ifindex;
573 enum nl80211_iftype type;
574 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +0200575 u32 _flags, *flags = NULL;
Johannes Berg55682962007-09-20 13:09:35 -0400576
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100577 memset(&params, 0, sizeof(params));
578
Johannes Bergbba95fe2008-07-29 13:22:51 +0200579 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400580 if (err)
581 return err;
582 ifindex = dev->ifindex;
Johannes Berg723b0382008-09-16 20:22:09 +0200583 type = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -0400584 dev_put(dev);
585
Johannes Berg723b0382008-09-16 20:22:09 +0200586 err = -EINVAL;
587 if (info->attrs[NL80211_ATTR_IFTYPE]) {
588 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
589 if (type > NL80211_IFTYPE_MAX)
590 goto unlock;
591 }
592
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700593 if (!drv->ops->change_virtual_intf ||
594 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400595 err = -EOPNOTSUPP;
596 goto unlock;
597 }
598
Johannes Berg92ffe052008-09-16 20:39:36 +0200599 if (info->attrs[NL80211_ATTR_MESH_ID]) {
600 if (type != NL80211_IFTYPE_MESH_POINT) {
601 err = -EINVAL;
602 goto unlock;
603 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100604 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
605 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
606 }
607
Johannes Berg92ffe052008-09-16 20:39:36 +0200608 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
609 if (type != NL80211_IFTYPE_MONITOR) {
610 err = -EINVAL;
611 goto unlock;
612 }
613 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
614 &_flags);
615 if (!err)
616 flags = &_flags;
617 }
Johannes Berg55682962007-09-20 13:09:35 -0400618 rtnl_lock();
Michael Wu66f7ac52008-01-31 19:48:22 +0100619 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
Johannes Berg92ffe052008-09-16 20:39:36 +0200620 type, flags, &params);
Johannes Berg60719ff2008-09-16 14:55:09 +0200621
622 dev = __dev_get_by_index(&init_net, ifindex);
623 WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != type));
624
Johannes Berg55682962007-09-20 13:09:35 -0400625 rtnl_unlock();
626
627 unlock:
628 cfg80211_put_dev(drv);
629 return err;
630}
631
632static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
633{
634 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100635 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400636 int err;
637 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +0100638 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -0400639
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100640 memset(&params, 0, sizeof(params));
641
Johannes Berg55682962007-09-20 13:09:35 -0400642 if (!info->attrs[NL80211_ATTR_IFNAME])
643 return -EINVAL;
644
645 if (info->attrs[NL80211_ATTR_IFTYPE]) {
646 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
647 if (type > NL80211_IFTYPE_MAX)
648 return -EINVAL;
649 }
650
651 drv = cfg80211_get_dev_from_info(info);
652 if (IS_ERR(drv))
653 return PTR_ERR(drv);
654
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700655 if (!drv->ops->add_virtual_intf ||
656 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400657 err = -EOPNOTSUPP;
658 goto unlock;
659 }
660
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100661 if (type == NL80211_IFTYPE_MESH_POINT &&
662 info->attrs[NL80211_ATTR_MESH_ID]) {
663 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
664 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
665 }
666
Johannes Berg55682962007-09-20 13:09:35 -0400667 rtnl_lock();
Michael Wu66f7ac52008-01-31 19:48:22 +0100668 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
669 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
670 &flags);
Johannes Berg55682962007-09-20 13:09:35 -0400671 err = drv->ops->add_virtual_intf(&drv->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +0100672 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100673 type, err ? NULL : &flags, &params);
Johannes Berg55682962007-09-20 13:09:35 -0400674 rtnl_unlock();
675
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100676
Johannes Berg55682962007-09-20 13:09:35 -0400677 unlock:
678 cfg80211_put_dev(drv);
679 return err;
680}
681
682static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
683{
684 struct cfg80211_registered_device *drv;
685 int ifindex, err;
686 struct net_device *dev;
687
Johannes Bergbba95fe2008-07-29 13:22:51 +0200688 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400689 if (err)
690 return err;
691 ifindex = dev->ifindex;
692 dev_put(dev);
693
694 if (!drv->ops->del_virtual_intf) {
695 err = -EOPNOTSUPP;
696 goto out;
697 }
698
699 rtnl_lock();
700 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
701 rtnl_unlock();
702
703 out:
704 cfg80211_put_dev(drv);
705 return err;
706}
707
Johannes Berg41ade002007-12-19 02:03:29 +0100708struct get_key_cookie {
709 struct sk_buff *msg;
710 int error;
711};
712
713static void get_key_callback(void *c, struct key_params *params)
714{
715 struct get_key_cookie *cookie = c;
716
717 if (params->key)
718 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
719 params->key_len, params->key);
720
721 if (params->seq)
722 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
723 params->seq_len, params->seq);
724
725 if (params->cipher)
726 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
727 params->cipher);
728
729 return;
730 nla_put_failure:
731 cookie->error = 1;
732}
733
734static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
735{
736 struct cfg80211_registered_device *drv;
737 int err;
738 struct net_device *dev;
739 u8 key_idx = 0;
740 u8 *mac_addr = NULL;
741 struct get_key_cookie cookie = {
742 .error = 0,
743 };
744 void *hdr;
745 struct sk_buff *msg;
746
747 if (info->attrs[NL80211_ATTR_KEY_IDX])
748 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
749
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200750 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100751 return -EINVAL;
752
753 if (info->attrs[NL80211_ATTR_MAC])
754 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
755
Johannes Bergbba95fe2008-07-29 13:22:51 +0200756 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100757 if (err)
758 return err;
759
760 if (!drv->ops->get_key) {
761 err = -EOPNOTSUPP;
762 goto out;
763 }
764
765 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
766 if (!msg) {
767 err = -ENOMEM;
768 goto out;
769 }
770
771 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
772 NL80211_CMD_NEW_KEY);
773
774 if (IS_ERR(hdr)) {
775 err = PTR_ERR(hdr);
776 goto out;
777 }
778
779 cookie.msg = msg;
780
781 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
782 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
783 if (mac_addr)
784 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
785
786 rtnl_lock();
787 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
788 &cookie, get_key_callback);
789 rtnl_unlock();
790
791 if (err)
792 goto out;
793
794 if (cookie.error)
795 goto nla_put_failure;
796
797 genlmsg_end(msg, hdr);
798 err = genlmsg_unicast(msg, info->snd_pid);
799 goto out;
800
801 nla_put_failure:
802 err = -ENOBUFS;
803 nlmsg_free(msg);
804 out:
805 cfg80211_put_dev(drv);
806 dev_put(dev);
807 return err;
808}
809
810static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
811{
812 struct cfg80211_registered_device *drv;
813 int err;
814 struct net_device *dev;
815 u8 key_idx;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200816 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
817 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +0100818
819 if (!info->attrs[NL80211_ATTR_KEY_IDX])
820 return -EINVAL;
821
822 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
823
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200824 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
825 if (key_idx < 4 || key_idx > 5)
826 return -EINVAL;
827 } else if (key_idx > 3)
Johannes Berg41ade002007-12-19 02:03:29 +0100828 return -EINVAL;
829
830 /* currently only support setting default key */
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200831 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
832 !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
Johannes Berg41ade002007-12-19 02:03:29 +0100833 return -EINVAL;
834
Johannes Bergbba95fe2008-07-29 13:22:51 +0200835 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100836 if (err)
837 return err;
838
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200839 if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
840 func = drv->ops->set_default_key;
841 else
842 func = drv->ops->set_default_mgmt_key;
843
844 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +0100845 err = -EOPNOTSUPP;
846 goto out;
847 }
848
849 rtnl_lock();
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200850 err = func(&drv->wiphy, dev, key_idx);
Johannes Berg41ade002007-12-19 02:03:29 +0100851 rtnl_unlock();
852
853 out:
854 cfg80211_put_dev(drv);
855 dev_put(dev);
856 return err;
857}
858
859static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
860{
861 struct cfg80211_registered_device *drv;
862 int err;
863 struct net_device *dev;
864 struct key_params params;
865 u8 key_idx = 0;
866 u8 *mac_addr = NULL;
867
868 memset(&params, 0, sizeof(params));
869
870 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
871 return -EINVAL;
872
873 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
874 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
875 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
876 }
877
878 if (info->attrs[NL80211_ATTR_KEY_IDX])
879 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
880
881 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
882
883 if (info->attrs[NL80211_ATTR_MAC])
884 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
885
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200886 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100887 return -EINVAL;
888
889 /*
890 * Disallow pairwise keys with non-zero index unless it's WEP
891 * (because current deployments use pairwise WEP keys with
892 * non-zero indizes but 802.11i clearly specifies to use zero)
893 */
894 if (mac_addr && key_idx &&
895 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
896 params.cipher != WLAN_CIPHER_SUITE_WEP104)
897 return -EINVAL;
898
899 /* TODO: add definitions for the lengths to linux/ieee80211.h */
900 switch (params.cipher) {
901 case WLAN_CIPHER_SUITE_WEP40:
902 if (params.key_len != 5)
903 return -EINVAL;
904 break;
905 case WLAN_CIPHER_SUITE_TKIP:
906 if (params.key_len != 32)
907 return -EINVAL;
908 break;
909 case WLAN_CIPHER_SUITE_CCMP:
910 if (params.key_len != 16)
911 return -EINVAL;
912 break;
913 case WLAN_CIPHER_SUITE_WEP104:
914 if (params.key_len != 13)
915 return -EINVAL;
916 break;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200917 case WLAN_CIPHER_SUITE_AES_CMAC:
918 if (params.key_len != 16)
919 return -EINVAL;
920 break;
Johannes Berg41ade002007-12-19 02:03:29 +0100921 default:
922 return -EINVAL;
923 }
924
Johannes Bergbba95fe2008-07-29 13:22:51 +0200925 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100926 if (err)
927 return err;
928
929 if (!drv->ops->add_key) {
930 err = -EOPNOTSUPP;
931 goto out;
932 }
933
934 rtnl_lock();
935 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, &params);
936 rtnl_unlock();
937
938 out:
939 cfg80211_put_dev(drv);
940 dev_put(dev);
941 return err;
942}
943
944static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
945{
946 struct cfg80211_registered_device *drv;
947 int err;
948 struct net_device *dev;
949 u8 key_idx = 0;
950 u8 *mac_addr = NULL;
951
952 if (info->attrs[NL80211_ATTR_KEY_IDX])
953 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
954
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200955 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100956 return -EINVAL;
957
958 if (info->attrs[NL80211_ATTR_MAC])
959 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
960
Johannes Bergbba95fe2008-07-29 13:22:51 +0200961 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100962 if (err)
963 return err;
964
965 if (!drv->ops->del_key) {
966 err = -EOPNOTSUPP;
967 goto out;
968 }
969
970 rtnl_lock();
971 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
972 rtnl_unlock();
973
974 out:
975 cfg80211_put_dev(drv);
976 dev_put(dev);
977 return err;
978}
979
Johannes Berged1b6cc2007-12-19 02:03:32 +0100980static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
981{
982 int (*call)(struct wiphy *wiphy, struct net_device *dev,
983 struct beacon_parameters *info);
984 struct cfg80211_registered_device *drv;
985 int err;
986 struct net_device *dev;
987 struct beacon_parameters params;
988 int haveinfo = 0;
989
Johannes Bergbba95fe2008-07-29 13:22:51 +0200990 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +0100991 if (err)
992 return err;
993
994 switch (info->genlhdr->cmd) {
995 case NL80211_CMD_NEW_BEACON:
996 /* these are required for NEW_BEACON */
997 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
998 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
999 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1000 err = -EINVAL;
1001 goto out;
1002 }
1003
1004 call = drv->ops->add_beacon;
1005 break;
1006 case NL80211_CMD_SET_BEACON:
1007 call = drv->ops->set_beacon;
1008 break;
1009 default:
1010 WARN_ON(1);
1011 err = -EOPNOTSUPP;
1012 goto out;
1013 }
1014
1015 if (!call) {
1016 err = -EOPNOTSUPP;
1017 goto out;
1018 }
1019
1020 memset(&params, 0, sizeof(params));
1021
1022 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1023 params.interval =
1024 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1025 haveinfo = 1;
1026 }
1027
1028 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1029 params.dtim_period =
1030 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1031 haveinfo = 1;
1032 }
1033
1034 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1035 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1036 params.head_len =
1037 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1038 haveinfo = 1;
1039 }
1040
1041 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1042 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1043 params.tail_len =
1044 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1045 haveinfo = 1;
1046 }
1047
1048 if (!haveinfo) {
1049 err = -EINVAL;
1050 goto out;
1051 }
1052
1053 rtnl_lock();
1054 err = call(&drv->wiphy, dev, &params);
1055 rtnl_unlock();
1056
1057 out:
1058 cfg80211_put_dev(drv);
1059 dev_put(dev);
1060 return err;
1061}
1062
1063static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1064{
1065 struct cfg80211_registered_device *drv;
1066 int err;
1067 struct net_device *dev;
1068
Johannes Bergbba95fe2008-07-29 13:22:51 +02001069 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001070 if (err)
1071 return err;
1072
1073 if (!drv->ops->del_beacon) {
1074 err = -EOPNOTSUPP;
1075 goto out;
1076 }
1077
1078 rtnl_lock();
1079 err = drv->ops->del_beacon(&drv->wiphy, dev);
1080 rtnl_unlock();
1081
1082 out:
1083 cfg80211_put_dev(drv);
1084 dev_put(dev);
1085 return err;
1086}
1087
Johannes Berg5727ef12007-12-19 02:03:34 +01001088static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1089 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1090 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1091 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1092};
1093
1094static int parse_station_flags(struct nlattr *nla, u32 *staflags)
1095{
1096 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1097 int flag;
1098
1099 *staflags = 0;
1100
1101 if (!nla)
1102 return 0;
1103
1104 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1105 nla, sta_flags_policy))
1106 return -EINVAL;
1107
1108 *staflags = STATION_FLAG_CHANGED;
1109
1110 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1111 if (flags[flag])
1112 *staflags |= (1<<flag);
1113
1114 return 0;
1115}
1116
Henning Rogge420e7fa2008-12-11 22:04:19 +01001117static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1118{
1119 int modulation, streams, bitrate;
1120
1121 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1122 return rate->legacy;
1123
1124 /* the formula below does only work for MCS values smaller than 32 */
1125 if (rate->mcs >= 32)
1126 return 0;
1127
1128 modulation = rate->mcs & 7;
1129 streams = (rate->mcs >> 3) + 1;
1130
1131 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1132 13500000 : 6500000;
1133
1134 if (modulation < 4)
1135 bitrate *= (modulation + 1);
1136 else if (modulation == 4)
1137 bitrate *= (modulation + 2);
1138 else
1139 bitrate *= (modulation + 3);
1140
1141 bitrate *= streams;
1142
1143 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1144 bitrate = (bitrate / 9) * 10;
1145
1146 /* do NOT round down here */
1147 return (bitrate + 50000) / 100000;
1148}
1149
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001150static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1151 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001152 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001153{
1154 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001155 struct nlattr *sinfoattr, *txrate;
1156 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001157
1158 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1159 if (!hdr)
1160 return -1;
1161
1162 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1163 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1164
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001165 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1166 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001167 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001168 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1169 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1170 sinfo->inactive_time);
1171 if (sinfo->filled & STATION_INFO_RX_BYTES)
1172 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1173 sinfo->rx_bytes);
1174 if (sinfo->filled & STATION_INFO_TX_BYTES)
1175 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1176 sinfo->tx_bytes);
1177 if (sinfo->filled & STATION_INFO_LLID)
1178 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1179 sinfo->llid);
1180 if (sinfo->filled & STATION_INFO_PLID)
1181 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1182 sinfo->plid);
1183 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1184 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1185 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001186 if (sinfo->filled & STATION_INFO_SIGNAL)
1187 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1188 sinfo->signal);
1189 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1190 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1191 if (!txrate)
1192 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001193
Henning Rogge420e7fa2008-12-11 22:04:19 +01001194 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1195 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1196 if (bitrate > 0)
1197 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1198
1199 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1200 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1201 sinfo->txrate.mcs);
1202 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1203 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1204 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1205 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1206
1207 nla_nest_end(msg, txrate);
1208 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001209 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001210
1211 return genlmsg_end(msg, hdr);
1212
1213 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001214 genlmsg_cancel(msg, hdr);
1215 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001216}
1217
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001218static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001219 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001220{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001221 struct station_info sinfo;
1222 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001223 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001224 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001225 int ifidx = cb->args[0];
1226 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001227 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001228
Johannes Bergbba95fe2008-07-29 13:22:51 +02001229 if (!ifidx) {
1230 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1231 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1232 nl80211_policy);
1233 if (err)
1234 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001235
Johannes Bergbba95fe2008-07-29 13:22:51 +02001236 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1237 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001238
Johannes Bergbba95fe2008-07-29 13:22:51 +02001239 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1240 if (!ifidx)
1241 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001242 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001243
Johannes Bergbba95fe2008-07-29 13:22:51 +02001244 netdev = dev_get_by_index(&init_net, ifidx);
1245 if (!netdev)
1246 return -ENODEV;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001247
Johannes Bergbba95fe2008-07-29 13:22:51 +02001248 dev = cfg80211_get_dev_from_ifindex(ifidx);
1249 if (IS_ERR(dev)) {
1250 err = PTR_ERR(dev);
1251 goto out_put_netdev;
1252 }
1253
1254 if (!dev->ops->dump_station) {
1255 err = -ENOSYS;
1256 goto out_err;
1257 }
1258
1259 rtnl_lock();
1260
1261 while (1) {
1262 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1263 mac_addr, &sinfo);
1264 if (err == -ENOENT)
1265 break;
1266 if (err)
1267 goto out_err_rtnl;
1268
1269 if (nl80211_send_station(skb,
1270 NETLINK_CB(cb->skb).pid,
1271 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1272 netdev, mac_addr,
1273 &sinfo) < 0)
1274 goto out;
1275
1276 sta_idx++;
1277 }
1278
1279
1280 out:
1281 cb->args[1] = sta_idx;
1282 err = skb->len;
1283 out_err_rtnl:
1284 rtnl_unlock();
1285 out_err:
1286 cfg80211_put_dev(dev);
1287 out_put_netdev:
1288 dev_put(netdev);
1289
1290 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001291}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001292
Johannes Berg5727ef12007-12-19 02:03:34 +01001293static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1294{
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001295 struct cfg80211_registered_device *drv;
1296 int err;
1297 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001298 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001299 struct sk_buff *msg;
1300 u8 *mac_addr = NULL;
1301
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001302 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001303
1304 if (!info->attrs[NL80211_ATTR_MAC])
1305 return -EINVAL;
1306
1307 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1308
Johannes Bergbba95fe2008-07-29 13:22:51 +02001309 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001310 if (err)
1311 return err;
1312
1313 if (!drv->ops->get_station) {
1314 err = -EOPNOTSUPP;
1315 goto out;
1316 }
1317
1318 rtnl_lock();
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001319 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001320 rtnl_unlock();
1321
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001322 if (err)
1323 goto out;
1324
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001325 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1326 if (!msg)
1327 goto out;
1328
1329 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001330 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001331 goto out_free;
1332
1333 err = genlmsg_unicast(msg, info->snd_pid);
1334 goto out;
1335
1336 out_free:
1337 nlmsg_free(msg);
1338
1339 out:
1340 cfg80211_put_dev(drv);
1341 dev_put(dev);
1342 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001343}
1344
1345/*
1346 * Get vlan interface making sure it is on the right wiphy.
1347 */
1348static int get_vlan(struct nlattr *vlanattr,
1349 struct cfg80211_registered_device *rdev,
1350 struct net_device **vlan)
1351{
1352 *vlan = NULL;
1353
1354 if (vlanattr) {
1355 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1356 if (!*vlan)
1357 return -ENODEV;
1358 if (!(*vlan)->ieee80211_ptr)
1359 return -EINVAL;
1360 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1361 return -EINVAL;
1362 }
1363 return 0;
1364}
1365
1366static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1367{
1368 struct cfg80211_registered_device *drv;
1369 int err;
1370 struct net_device *dev;
1371 struct station_parameters params;
1372 u8 *mac_addr = NULL;
1373
1374 memset(&params, 0, sizeof(params));
1375
1376 params.listen_interval = -1;
1377
1378 if (info->attrs[NL80211_ATTR_STA_AID])
1379 return -EINVAL;
1380
1381 if (!info->attrs[NL80211_ATTR_MAC])
1382 return -EINVAL;
1383
1384 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1385
1386 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1387 params.supported_rates =
1388 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1389 params.supported_rates_len =
1390 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1391 }
1392
1393 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1394 params.listen_interval =
1395 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1396
Jouni Malinen36aedc92008-08-25 11:58:58 +03001397 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1398 params.ht_capa =
1399 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1400
Johannes Berg5727ef12007-12-19 02:03:34 +01001401 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1402 &params.station_flags))
1403 return -EINVAL;
1404
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001405 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1406 params.plink_action =
1407 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1408
Johannes Bergbba95fe2008-07-29 13:22:51 +02001409 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001410 if (err)
1411 return err;
1412
1413 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1414 if (err)
1415 goto out;
1416
1417 if (!drv->ops->change_station) {
1418 err = -EOPNOTSUPP;
1419 goto out;
1420 }
1421
1422 rtnl_lock();
1423 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, &params);
1424 rtnl_unlock();
1425
1426 out:
1427 if (params.vlan)
1428 dev_put(params.vlan);
1429 cfg80211_put_dev(drv);
1430 dev_put(dev);
1431 return err;
1432}
1433
1434static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1435{
1436 struct cfg80211_registered_device *drv;
1437 int err;
1438 struct net_device *dev;
1439 struct station_parameters params;
1440 u8 *mac_addr = NULL;
1441
1442 memset(&params, 0, sizeof(params));
1443
1444 if (!info->attrs[NL80211_ATTR_MAC])
1445 return -EINVAL;
1446
1447 if (!info->attrs[NL80211_ATTR_STA_AID])
1448 return -EINVAL;
1449
1450 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1451 return -EINVAL;
1452
1453 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1454 return -EINVAL;
1455
1456 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1457 params.supported_rates =
1458 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1459 params.supported_rates_len =
1460 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1461 params.listen_interval =
1462 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg16f2e852008-04-07 14:35:46 +02001463 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Jouni Malinen36aedc92008-08-25 11:58:58 +03001464 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1465 params.ht_capa =
1466 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01001467
1468 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1469 &params.station_flags))
1470 return -EINVAL;
1471
Johannes Bergbba95fe2008-07-29 13:22:51 +02001472 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001473 if (err)
1474 return err;
1475
1476 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1477 if (err)
1478 goto out;
1479
1480 if (!drv->ops->add_station) {
1481 err = -EOPNOTSUPP;
1482 goto out;
1483 }
1484
1485 rtnl_lock();
1486 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
1487 rtnl_unlock();
1488
1489 out:
1490 if (params.vlan)
1491 dev_put(params.vlan);
1492 cfg80211_put_dev(drv);
1493 dev_put(dev);
1494 return err;
1495}
1496
1497static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1498{
1499 struct cfg80211_registered_device *drv;
1500 int err;
1501 struct net_device *dev;
1502 u8 *mac_addr = NULL;
1503
1504 if (info->attrs[NL80211_ATTR_MAC])
1505 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1506
Johannes Bergbba95fe2008-07-29 13:22:51 +02001507 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001508 if (err)
1509 return err;
1510
1511 if (!drv->ops->del_station) {
1512 err = -EOPNOTSUPP;
1513 goto out;
1514 }
1515
1516 rtnl_lock();
1517 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
1518 rtnl_unlock();
1519
1520 out:
1521 cfg80211_put_dev(drv);
1522 dev_put(dev);
1523 return err;
1524}
1525
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001526static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1527 int flags, struct net_device *dev,
1528 u8 *dst, u8 *next_hop,
1529 struct mpath_info *pinfo)
1530{
1531 void *hdr;
1532 struct nlattr *pinfoattr;
1533
1534 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1535 if (!hdr)
1536 return -1;
1537
1538 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1539 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1540 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1541
1542 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1543 if (!pinfoattr)
1544 goto nla_put_failure;
1545 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1546 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1547 pinfo->frame_qlen);
1548 if (pinfo->filled & MPATH_INFO_DSN)
1549 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1550 pinfo->dsn);
1551 if (pinfo->filled & MPATH_INFO_METRIC)
1552 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1553 pinfo->metric);
1554 if (pinfo->filled & MPATH_INFO_EXPTIME)
1555 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1556 pinfo->exptime);
1557 if (pinfo->filled & MPATH_INFO_FLAGS)
1558 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1559 pinfo->flags);
1560 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1561 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1562 pinfo->discovery_timeout);
1563 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1564 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1565 pinfo->discovery_retries);
1566
1567 nla_nest_end(msg, pinfoattr);
1568
1569 return genlmsg_end(msg, hdr);
1570
1571 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001572 genlmsg_cancel(msg, hdr);
1573 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001574}
1575
1576static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001577 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001578{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001579 struct mpath_info pinfo;
1580 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001581 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001582 u8 dst[ETH_ALEN];
1583 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001584 int ifidx = cb->args[0];
1585 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001586 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001587
Johannes Bergbba95fe2008-07-29 13:22:51 +02001588 if (!ifidx) {
1589 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1590 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1591 nl80211_policy);
1592 if (err)
1593 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001594
Johannes Bergbba95fe2008-07-29 13:22:51 +02001595 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1596 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001597
Johannes Bergbba95fe2008-07-29 13:22:51 +02001598 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1599 if (!ifidx)
1600 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001601 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001602
Johannes Bergbba95fe2008-07-29 13:22:51 +02001603 netdev = dev_get_by_index(&init_net, ifidx);
1604 if (!netdev)
1605 return -ENODEV;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001606
Johannes Bergbba95fe2008-07-29 13:22:51 +02001607 dev = cfg80211_get_dev_from_ifindex(ifidx);
1608 if (IS_ERR(dev)) {
1609 err = PTR_ERR(dev);
1610 goto out_put_netdev;
1611 }
1612
1613 if (!dev->ops->dump_mpath) {
1614 err = -ENOSYS;
1615 goto out_err;
1616 }
1617
1618 rtnl_lock();
1619
1620 while (1) {
1621 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1622 dst, next_hop, &pinfo);
1623 if (err == -ENOENT)
1624 break;
1625 if (err)
1626 goto out_err_rtnl;
1627
1628 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1629 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1630 netdev, dst, next_hop,
1631 &pinfo) < 0)
1632 goto out;
1633
1634 path_idx++;
1635 }
1636
1637
1638 out:
1639 cb->args[1] = path_idx;
1640 err = skb->len;
1641 out_err_rtnl:
1642 rtnl_unlock();
1643 out_err:
1644 cfg80211_put_dev(dev);
1645 out_put_netdev:
1646 dev_put(netdev);
1647
1648 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001649}
1650
1651static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1652{
1653 struct cfg80211_registered_device *drv;
1654 int err;
1655 struct net_device *dev;
1656 struct mpath_info pinfo;
1657 struct sk_buff *msg;
1658 u8 *dst = NULL;
1659 u8 next_hop[ETH_ALEN];
1660
1661 memset(&pinfo, 0, sizeof(pinfo));
1662
1663 if (!info->attrs[NL80211_ATTR_MAC])
1664 return -EINVAL;
1665
1666 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1667
Johannes Bergbba95fe2008-07-29 13:22:51 +02001668 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001669 if (err)
1670 return err;
1671
1672 if (!drv->ops->get_mpath) {
1673 err = -EOPNOTSUPP;
1674 goto out;
1675 }
1676
1677 rtnl_lock();
1678 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
1679 rtnl_unlock();
1680
1681 if (err)
1682 goto out;
1683
1684 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1685 if (!msg)
1686 goto out;
1687
1688 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1689 dev, dst, next_hop, &pinfo) < 0)
1690 goto out_free;
1691
1692 err = genlmsg_unicast(msg, info->snd_pid);
1693 goto out;
1694
1695 out_free:
1696 nlmsg_free(msg);
1697
1698 out:
1699 cfg80211_put_dev(drv);
1700 dev_put(dev);
1701 return err;
1702}
1703
1704static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1705{
1706 struct cfg80211_registered_device *drv;
1707 int err;
1708 struct net_device *dev;
1709 u8 *dst = NULL;
1710 u8 *next_hop = NULL;
1711
1712 if (!info->attrs[NL80211_ATTR_MAC])
1713 return -EINVAL;
1714
1715 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1716 return -EINVAL;
1717
1718 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1719 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1720
Johannes Bergbba95fe2008-07-29 13:22:51 +02001721 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001722 if (err)
1723 return err;
1724
1725 if (!drv->ops->change_mpath) {
1726 err = -EOPNOTSUPP;
1727 goto out;
1728 }
1729
1730 rtnl_lock();
1731 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
1732 rtnl_unlock();
1733
1734 out:
1735 cfg80211_put_dev(drv);
1736 dev_put(dev);
1737 return err;
1738}
1739static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1740{
1741 struct cfg80211_registered_device *drv;
1742 int err;
1743 struct net_device *dev;
1744 u8 *dst = NULL;
1745 u8 *next_hop = NULL;
1746
1747 if (!info->attrs[NL80211_ATTR_MAC])
1748 return -EINVAL;
1749
1750 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1751 return -EINVAL;
1752
1753 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1754 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1755
Johannes Bergbba95fe2008-07-29 13:22:51 +02001756 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001757 if (err)
1758 return err;
1759
1760 if (!drv->ops->add_mpath) {
1761 err = -EOPNOTSUPP;
1762 goto out;
1763 }
1764
1765 rtnl_lock();
1766 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
1767 rtnl_unlock();
1768
1769 out:
1770 cfg80211_put_dev(drv);
1771 dev_put(dev);
1772 return err;
1773}
1774
1775static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1776{
1777 struct cfg80211_registered_device *drv;
1778 int err;
1779 struct net_device *dev;
1780 u8 *dst = NULL;
1781
1782 if (info->attrs[NL80211_ATTR_MAC])
1783 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1784
Johannes Bergbba95fe2008-07-29 13:22:51 +02001785 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001786 if (err)
1787 return err;
1788
1789 if (!drv->ops->del_mpath) {
1790 err = -EOPNOTSUPP;
1791 goto out;
1792 }
1793
1794 rtnl_lock();
1795 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
1796 rtnl_unlock();
1797
1798 out:
1799 cfg80211_put_dev(drv);
1800 dev_put(dev);
1801 return err;
1802}
1803
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001804static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
1805{
1806 struct cfg80211_registered_device *drv;
1807 int err;
1808 struct net_device *dev;
1809 struct bss_parameters params;
1810
1811 memset(&params, 0, sizeof(params));
1812 /* default to not changing parameters */
1813 params.use_cts_prot = -1;
1814 params.use_short_preamble = -1;
1815 params.use_short_slot_time = -1;
1816
1817 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
1818 params.use_cts_prot =
1819 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
1820 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
1821 params.use_short_preamble =
1822 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
1823 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
1824 params.use_short_slot_time =
1825 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02001826 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
1827 params.basic_rates =
1828 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1829 params.basic_rates_len =
1830 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1831 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001832
1833 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1834 if (err)
1835 return err;
1836
1837 if (!drv->ops->change_bss) {
1838 err = -EOPNOTSUPP;
1839 goto out;
1840 }
1841
1842 rtnl_lock();
1843 err = drv->ops->change_bss(&drv->wiphy, dev, &params);
1844 rtnl_unlock();
1845
1846 out:
1847 cfg80211_put_dev(drv);
1848 dev_put(dev);
1849 return err;
1850}
1851
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001852static const struct nla_policy
1853 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
1854 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1855 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1856 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1857 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1858 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1859 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1860};
1861
1862static int parse_reg_rule(struct nlattr *tb[],
1863 struct ieee80211_reg_rule *reg_rule)
1864{
1865 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
1866 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
1867
1868 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
1869 return -EINVAL;
1870 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
1871 return -EINVAL;
1872 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
1873 return -EINVAL;
1874 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1875 return -EINVAL;
1876 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1877 return -EINVAL;
1878
1879 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
1880
1881 freq_range->start_freq_khz =
1882 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
1883 freq_range->end_freq_khz =
1884 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
1885 freq_range->max_bandwidth_khz =
1886 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
1887
1888 power_rule->max_eirp =
1889 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
1890
1891 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
1892 power_rule->max_antenna_gain =
1893 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
1894
1895 return 0;
1896}
1897
1898static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
1899{
1900 int r;
1901 char *data = NULL;
1902
1903 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
1904 return -EINVAL;
1905
1906 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
1907
1908#ifdef CONFIG_WIRELESS_OLD_REGULATORY
1909 /* We ignore world regdom requests with the old regdom setup */
1910 if (is_world_regdom(data))
1911 return -EINVAL;
1912#endif
1913 mutex_lock(&cfg80211_drv_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001914 r = __regulatory_hint(NULL, REGDOM_SET_BY_USER, data, 0, ENVIRON_ANY);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001915 mutex_unlock(&cfg80211_drv_mutex);
Luis R. Rodriguezd81c2d92009-01-26 09:00:51 -08001916 /* This means the regulatory domain was already set, however
1917 * we don't want to confuse userspace with a "successful error"
1918 * message so lets just treat it as a success */
1919 if (r == -EALREADY)
1920 r = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001921 return r;
1922}
1923
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07001924static int nl80211_get_mesh_params(struct sk_buff *skb,
1925 struct genl_info *info)
1926{
1927 struct cfg80211_registered_device *drv;
1928 struct mesh_config cur_params;
1929 int err;
1930 struct net_device *dev;
1931 void *hdr;
1932 struct nlattr *pinfoattr;
1933 struct sk_buff *msg;
1934
1935 /* Look up our device */
1936 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1937 if (err)
1938 return err;
1939
1940 /* Get the mesh params */
1941 rtnl_lock();
1942 err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
1943 rtnl_unlock();
1944 if (err)
1945 goto out;
1946
1947 /* Draw up a netlink message to send back */
1948 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1949 if (!msg) {
1950 err = -ENOBUFS;
1951 goto out;
1952 }
1953 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1954 NL80211_CMD_GET_MESH_PARAMS);
1955 if (!hdr)
1956 goto nla_put_failure;
1957 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
1958 if (!pinfoattr)
1959 goto nla_put_failure;
1960 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1961 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
1962 cur_params.dot11MeshRetryTimeout);
1963 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
1964 cur_params.dot11MeshConfirmTimeout);
1965 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
1966 cur_params.dot11MeshHoldingTimeout);
1967 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
1968 cur_params.dot11MeshMaxPeerLinks);
1969 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
1970 cur_params.dot11MeshMaxRetries);
1971 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
1972 cur_params.dot11MeshTTL);
1973 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
1974 cur_params.auto_open_plinks);
1975 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
1976 cur_params.dot11MeshHWMPmaxPREQretries);
1977 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
1978 cur_params.path_refresh_time);
1979 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
1980 cur_params.min_discovery_timeout);
1981 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
1982 cur_params.dot11MeshHWMPactivePathTimeout);
1983 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
1984 cur_params.dot11MeshHWMPpreqMinInterval);
1985 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1986 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
1987 nla_nest_end(msg, pinfoattr);
1988 genlmsg_end(msg, hdr);
1989 err = genlmsg_unicast(msg, info->snd_pid);
1990 goto out;
1991
1992nla_put_failure:
1993 genlmsg_cancel(msg, hdr);
1994 err = -EMSGSIZE;
1995out:
1996 /* Cleanup */
1997 cfg80211_put_dev(drv);
1998 dev_put(dev);
1999 return err;
2000}
2001
2002#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2003do {\
2004 if (table[attr_num]) {\
2005 cfg.param = nla_fn(table[attr_num]); \
2006 mask |= (1 << (attr_num - 1)); \
2007 } \
2008} while (0);\
2009
2010static struct nla_policy
2011nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2012 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2013 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2014 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2015 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2016 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2017 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2018 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2019
2020 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2021 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2022 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2023 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2024 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2025 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2026};
2027
2028static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2029{
2030 int err;
2031 u32 mask;
2032 struct cfg80211_registered_device *drv;
2033 struct net_device *dev;
2034 struct mesh_config cfg;
2035 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2036 struct nlattr *parent_attr;
2037
2038 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2039 if (!parent_attr)
2040 return -EINVAL;
2041 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2042 parent_attr, nl80211_meshconf_params_policy))
2043 return -EINVAL;
2044
2045 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2046 if (err)
2047 return err;
2048
2049 /* This makes sure that there aren't more than 32 mesh config
2050 * parameters (otherwise our bitfield scheme would not work.) */
2051 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2052
2053 /* Fill in the params struct */
2054 mask = 0;
2055 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2056 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2057 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2058 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2059 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2060 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2061 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2062 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2063 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2064 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2065 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2066 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2067 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2068 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2069 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2070 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2071 nla_get_u8);
2072 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2073 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2074 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2075 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2076 nla_get_u16);
2077 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2078 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2079 nla_get_u32);
2080 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2081 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2082 nla_get_u16);
2083 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2084 dot11MeshHWMPnetDiameterTraversalTime,
2085 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2086 nla_get_u16);
2087
2088 /* Apply changes */
2089 rtnl_lock();
2090 err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
2091 rtnl_unlock();
2092
2093 /* cleanup */
2094 cfg80211_put_dev(drv);
2095 dev_put(dev);
2096 return err;
2097}
2098
2099#undef FILL_IN_MESH_PARAM_IF_SET
2100
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002101static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2102{
2103 struct sk_buff *msg;
2104 void *hdr = NULL;
2105 struct nlattr *nl_reg_rules;
2106 unsigned int i;
2107 int err = -EINVAL;
2108
2109 mutex_lock(&cfg80211_drv_mutex);
2110
2111 if (!cfg80211_regdomain)
2112 goto out;
2113
2114 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2115 if (!msg) {
2116 err = -ENOBUFS;
2117 goto out;
2118 }
2119
2120 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2121 NL80211_CMD_GET_REG);
2122 if (!hdr)
2123 goto nla_put_failure;
2124
2125 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2126 cfg80211_regdomain->alpha2);
2127
2128 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2129 if (!nl_reg_rules)
2130 goto nla_put_failure;
2131
2132 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2133 struct nlattr *nl_reg_rule;
2134 const struct ieee80211_reg_rule *reg_rule;
2135 const struct ieee80211_freq_range *freq_range;
2136 const struct ieee80211_power_rule *power_rule;
2137
2138 reg_rule = &cfg80211_regdomain->reg_rules[i];
2139 freq_range = &reg_rule->freq_range;
2140 power_rule = &reg_rule->power_rule;
2141
2142 nl_reg_rule = nla_nest_start(msg, i);
2143 if (!nl_reg_rule)
2144 goto nla_put_failure;
2145
2146 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2147 reg_rule->flags);
2148 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2149 freq_range->start_freq_khz);
2150 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2151 freq_range->end_freq_khz);
2152 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2153 freq_range->max_bandwidth_khz);
2154 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2155 power_rule->max_antenna_gain);
2156 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2157 power_rule->max_eirp);
2158
2159 nla_nest_end(msg, nl_reg_rule);
2160 }
2161
2162 nla_nest_end(msg, nl_reg_rules);
2163
2164 genlmsg_end(msg, hdr);
2165 err = genlmsg_unicast(msg, info->snd_pid);
2166 goto out;
2167
2168nla_put_failure:
2169 genlmsg_cancel(msg, hdr);
2170 err = -EMSGSIZE;
2171out:
2172 mutex_unlock(&cfg80211_drv_mutex);
2173 return err;
2174}
2175
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002176static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2177{
2178 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2179 struct nlattr *nl_reg_rule;
2180 char *alpha2 = NULL;
2181 int rem_reg_rules = 0, r = 0;
2182 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2183 struct ieee80211_regdomain *rd = NULL;
2184
2185 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2186 return -EINVAL;
2187
2188 if (!info->attrs[NL80211_ATTR_REG_RULES])
2189 return -EINVAL;
2190
2191 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2192
2193 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2194 rem_reg_rules) {
2195 num_rules++;
2196 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2197 goto bad_reg;
2198 }
2199
2200 if (!reg_is_valid_request(alpha2))
2201 return -EINVAL;
2202
2203 size_of_regd = sizeof(struct ieee80211_regdomain) +
2204 (num_rules * sizeof(struct ieee80211_reg_rule));
2205
2206 rd = kzalloc(size_of_regd, GFP_KERNEL);
2207 if (!rd)
2208 return -ENOMEM;
2209
2210 rd->n_reg_rules = num_rules;
2211 rd->alpha2[0] = alpha2[0];
2212 rd->alpha2[1] = alpha2[1];
2213
2214 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2215 rem_reg_rules) {
2216 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2217 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2218 reg_rule_policy);
2219 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2220 if (r)
2221 goto bad_reg;
2222
2223 rule_idx++;
2224
2225 if (rule_idx > NL80211_MAX_SUPP_REG_RULES)
2226 goto bad_reg;
2227 }
2228
2229 BUG_ON(rule_idx != num_rules);
2230
2231 mutex_lock(&cfg80211_drv_mutex);
2232 r = set_regdom(rd);
2233 mutex_unlock(&cfg80211_drv_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002234 return r;
2235
Johannes Bergd2372b32008-10-24 20:32:20 +02002236 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002237 kfree(rd);
2238 return -EINVAL;
2239}
2240
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02002241static int nl80211_set_mgmt_extra_ie(struct sk_buff *skb,
2242 struct genl_info *info)
2243{
2244 struct cfg80211_registered_device *drv;
2245 int err;
2246 struct net_device *dev;
2247 struct mgmt_extra_ie_params params;
2248
2249 memset(&params, 0, sizeof(params));
2250
2251 if (!info->attrs[NL80211_ATTR_MGMT_SUBTYPE])
2252 return -EINVAL;
2253 params.subtype = nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
2254 if (params.subtype > 15)
2255 return -EINVAL; /* FC Subtype field is 4 bits (0..15) */
2256
2257 if (info->attrs[NL80211_ATTR_IE]) {
2258 params.ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2259 params.ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2260 }
2261
2262 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2263 if (err)
2264 return err;
2265
2266 if (drv->ops->set_mgmt_extra_ie) {
2267 rtnl_lock();
2268 err = drv->ops->set_mgmt_extra_ie(&drv->wiphy, dev, &params);
2269 rtnl_unlock();
2270 } else
2271 err = -EOPNOTSUPP;
2272
2273 cfg80211_put_dev(drv);
2274 dev_put(dev);
2275 return err;
2276}
2277
Johannes Berg2a519312009-02-10 21:25:55 +01002278static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2279{
2280 struct cfg80211_registered_device *drv;
2281 struct net_device *dev;
2282 struct cfg80211_scan_request *request;
2283 struct cfg80211_ssid *ssid;
2284 struct ieee80211_channel *channel;
2285 struct nlattr *attr;
2286 struct wiphy *wiphy;
2287 int err, tmp, n_ssids = 0, n_channels = 0, i;
2288 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002289 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002290
2291 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2292 if (err)
2293 return err;
2294
2295 wiphy = &drv->wiphy;
2296
2297 if (!drv->ops->scan) {
2298 err = -EOPNOTSUPP;
2299 goto out;
2300 }
2301
2302 rtnl_lock();
2303
2304 if (drv->scan_req) {
2305 err = -EBUSY;
2306 goto out_unlock;
2307 }
2308
2309 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2310 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp)
2311 n_channels++;
2312 if (!n_channels) {
2313 err = -EINVAL;
2314 goto out_unlock;
2315 }
2316 } else {
2317 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2318 if (wiphy->bands[band])
2319 n_channels += wiphy->bands[band]->n_channels;
2320 }
2321
2322 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2323 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2324 n_ssids++;
2325
2326 if (n_ssids > wiphy->max_scan_ssids) {
2327 err = -EINVAL;
2328 goto out_unlock;
2329 }
2330
Jouni Malinen70692ad2009-02-16 19:39:13 +02002331 if (info->attrs[NL80211_ATTR_IE])
2332 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2333 else
2334 ie_len = 0;
2335
Johannes Berg2a519312009-02-10 21:25:55 +01002336 request = kzalloc(sizeof(*request)
2337 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002338 + sizeof(channel) * n_channels
2339 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01002340 if (!request) {
2341 err = -ENOMEM;
2342 goto out_unlock;
2343 }
2344
2345 request->channels = (void *)((char *)request + sizeof(*request));
2346 request->n_channels = n_channels;
2347 if (n_ssids)
2348 request->ssids = (void *)(request->channels + n_channels);
2349 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002350 if (ie_len) {
2351 if (request->ssids)
2352 request->ie = (void *)(request->ssids + n_ssids);
2353 else
2354 request->ie = (void *)(request->channels + n_channels);
2355 }
Johannes Berg2a519312009-02-10 21:25:55 +01002356
2357 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2358 /* user specified, bail out if channel not found */
2359 request->n_channels = n_channels;
2360 i = 0;
2361 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2362 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2363 if (!request->channels[i]) {
2364 err = -EINVAL;
2365 goto out_free;
2366 }
2367 i++;
2368 }
2369 } else {
2370 /* all channels */
2371 i = 0;
2372 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2373 int j;
2374 if (!wiphy->bands[band])
2375 continue;
2376 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2377 request->channels[i] = &wiphy->bands[band]->channels[j];
2378 i++;
2379 }
2380 }
2381 }
2382
2383 i = 0;
2384 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2385 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2386 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2387 err = -EINVAL;
2388 goto out_free;
2389 }
2390 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2391 request->ssids[i].ssid_len = nla_len(attr);
2392 i++;
2393 }
2394 }
2395
Jouni Malinen70692ad2009-02-16 19:39:13 +02002396 if (info->attrs[NL80211_ATTR_IE]) {
2397 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2398 memcpy(request->ie, nla_data(info->attrs[NL80211_ATTR_IE]),
2399 request->ie_len);
2400 }
2401
Johannes Berg2a519312009-02-10 21:25:55 +01002402 request->ifidx = dev->ifindex;
2403 request->wiphy = &drv->wiphy;
2404
2405 drv->scan_req = request;
2406 err = drv->ops->scan(&drv->wiphy, dev, request);
2407
2408 out_free:
2409 if (err) {
2410 drv->scan_req = NULL;
2411 kfree(request);
2412 }
2413 out_unlock:
2414 rtnl_unlock();
2415 out:
2416 cfg80211_put_dev(drv);
2417 dev_put(dev);
2418 return err;
2419}
2420
2421static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2422 struct cfg80211_registered_device *rdev,
2423 struct net_device *dev,
2424 struct cfg80211_bss *res)
2425{
2426 void *hdr;
2427 struct nlattr *bss;
2428
2429 hdr = nl80211hdr_put(msg, pid, seq, flags,
2430 NL80211_CMD_NEW_SCAN_RESULTS);
2431 if (!hdr)
2432 return -1;
2433
2434 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2435 rdev->bss_generation);
2436 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2437
2438 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2439 if (!bss)
2440 goto nla_put_failure;
2441 if (!is_zero_ether_addr(res->bssid))
2442 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2443 if (res->information_elements && res->len_information_elements)
2444 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2445 res->len_information_elements,
2446 res->information_elements);
2447 if (res->tsf)
2448 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2449 if (res->beacon_interval)
2450 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2451 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2452 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2453
2454 switch (res->signal_type) {
2455 case CFG80211_SIGNAL_TYPE_MBM:
2456 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2457 break;
2458 case CFG80211_SIGNAL_TYPE_UNSPEC:
2459 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2460 break;
2461 default:
2462 break;
2463 }
2464
2465 nla_nest_end(msg, bss);
2466
2467 return genlmsg_end(msg, hdr);
2468
2469 nla_put_failure:
2470 genlmsg_cancel(msg, hdr);
2471 return -EMSGSIZE;
2472}
2473
2474static int nl80211_dump_scan(struct sk_buff *skb,
2475 struct netlink_callback *cb)
2476{
2477 struct cfg80211_registered_device *dev;
2478 struct net_device *netdev;
2479 struct cfg80211_internal_bss *scan;
2480 int ifidx = cb->args[0];
2481 int start = cb->args[1], idx = 0;
2482 int err;
2483
2484 if (!ifidx) {
2485 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2486 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2487 nl80211_policy);
2488 if (err)
2489 return err;
2490
2491 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2492 return -EINVAL;
2493
2494 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2495 if (!ifidx)
2496 return -EINVAL;
2497 cb->args[0] = ifidx;
2498 }
2499
2500 netdev = dev_get_by_index(&init_net, ifidx);
2501 if (!netdev)
2502 return -ENODEV;
2503
2504 dev = cfg80211_get_dev_from_ifindex(ifidx);
2505 if (IS_ERR(dev)) {
2506 err = PTR_ERR(dev);
2507 goto out_put_netdev;
2508 }
2509
2510 spin_lock_bh(&dev->bss_lock);
2511 cfg80211_bss_expire(dev);
2512
2513 list_for_each_entry(scan, &dev->bss_list, list) {
2514 if (++idx <= start)
2515 continue;
2516 if (nl80211_send_bss(skb,
2517 NETLINK_CB(cb->skb).pid,
2518 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2519 dev, netdev, &scan->pub) < 0) {
2520 idx--;
2521 goto out;
2522 }
2523 }
2524
2525 out:
2526 spin_unlock_bh(&dev->bss_lock);
2527
2528 cb->args[1] = idx;
2529 err = skb->len;
2530 cfg80211_put_dev(dev);
2531 out_put_netdev:
2532 dev_put(netdev);
2533
2534 return err;
2535}
2536
Johannes Berg55682962007-09-20 13:09:35 -04002537static struct genl_ops nl80211_ops[] = {
2538 {
2539 .cmd = NL80211_CMD_GET_WIPHY,
2540 .doit = nl80211_get_wiphy,
2541 .dumpit = nl80211_dump_wiphy,
2542 .policy = nl80211_policy,
2543 /* can be retrieved by unprivileged users */
2544 },
2545 {
2546 .cmd = NL80211_CMD_SET_WIPHY,
2547 .doit = nl80211_set_wiphy,
2548 .policy = nl80211_policy,
2549 .flags = GENL_ADMIN_PERM,
2550 },
2551 {
2552 .cmd = NL80211_CMD_GET_INTERFACE,
2553 .doit = nl80211_get_interface,
2554 .dumpit = nl80211_dump_interface,
2555 .policy = nl80211_policy,
2556 /* can be retrieved by unprivileged users */
2557 },
2558 {
2559 .cmd = NL80211_CMD_SET_INTERFACE,
2560 .doit = nl80211_set_interface,
2561 .policy = nl80211_policy,
2562 .flags = GENL_ADMIN_PERM,
2563 },
2564 {
2565 .cmd = NL80211_CMD_NEW_INTERFACE,
2566 .doit = nl80211_new_interface,
2567 .policy = nl80211_policy,
2568 .flags = GENL_ADMIN_PERM,
2569 },
2570 {
2571 .cmd = NL80211_CMD_DEL_INTERFACE,
2572 .doit = nl80211_del_interface,
2573 .policy = nl80211_policy,
2574 .flags = GENL_ADMIN_PERM,
2575 },
Johannes Berg41ade002007-12-19 02:03:29 +01002576 {
2577 .cmd = NL80211_CMD_GET_KEY,
2578 .doit = nl80211_get_key,
2579 .policy = nl80211_policy,
2580 .flags = GENL_ADMIN_PERM,
2581 },
2582 {
2583 .cmd = NL80211_CMD_SET_KEY,
2584 .doit = nl80211_set_key,
2585 .policy = nl80211_policy,
2586 .flags = GENL_ADMIN_PERM,
2587 },
2588 {
2589 .cmd = NL80211_CMD_NEW_KEY,
2590 .doit = nl80211_new_key,
2591 .policy = nl80211_policy,
2592 .flags = GENL_ADMIN_PERM,
2593 },
2594 {
2595 .cmd = NL80211_CMD_DEL_KEY,
2596 .doit = nl80211_del_key,
2597 .policy = nl80211_policy,
2598 .flags = GENL_ADMIN_PERM,
2599 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01002600 {
2601 .cmd = NL80211_CMD_SET_BEACON,
2602 .policy = nl80211_policy,
2603 .flags = GENL_ADMIN_PERM,
2604 .doit = nl80211_addset_beacon,
2605 },
2606 {
2607 .cmd = NL80211_CMD_NEW_BEACON,
2608 .policy = nl80211_policy,
2609 .flags = GENL_ADMIN_PERM,
2610 .doit = nl80211_addset_beacon,
2611 },
2612 {
2613 .cmd = NL80211_CMD_DEL_BEACON,
2614 .policy = nl80211_policy,
2615 .flags = GENL_ADMIN_PERM,
2616 .doit = nl80211_del_beacon,
2617 },
Johannes Berg5727ef12007-12-19 02:03:34 +01002618 {
2619 .cmd = NL80211_CMD_GET_STATION,
2620 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002621 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01002622 .policy = nl80211_policy,
2623 .flags = GENL_ADMIN_PERM,
2624 },
2625 {
2626 .cmd = NL80211_CMD_SET_STATION,
2627 .doit = nl80211_set_station,
2628 .policy = nl80211_policy,
2629 .flags = GENL_ADMIN_PERM,
2630 },
2631 {
2632 .cmd = NL80211_CMD_NEW_STATION,
2633 .doit = nl80211_new_station,
2634 .policy = nl80211_policy,
2635 .flags = GENL_ADMIN_PERM,
2636 },
2637 {
2638 .cmd = NL80211_CMD_DEL_STATION,
2639 .doit = nl80211_del_station,
2640 .policy = nl80211_policy,
2641 .flags = GENL_ADMIN_PERM,
2642 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002643 {
2644 .cmd = NL80211_CMD_GET_MPATH,
2645 .doit = nl80211_get_mpath,
2646 .dumpit = nl80211_dump_mpath,
2647 .policy = nl80211_policy,
2648 .flags = GENL_ADMIN_PERM,
2649 },
2650 {
2651 .cmd = NL80211_CMD_SET_MPATH,
2652 .doit = nl80211_set_mpath,
2653 .policy = nl80211_policy,
2654 .flags = GENL_ADMIN_PERM,
2655 },
2656 {
2657 .cmd = NL80211_CMD_NEW_MPATH,
2658 .doit = nl80211_new_mpath,
2659 .policy = nl80211_policy,
2660 .flags = GENL_ADMIN_PERM,
2661 },
2662 {
2663 .cmd = NL80211_CMD_DEL_MPATH,
2664 .doit = nl80211_del_mpath,
2665 .policy = nl80211_policy,
2666 .flags = GENL_ADMIN_PERM,
2667 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002668 {
2669 .cmd = NL80211_CMD_SET_BSS,
2670 .doit = nl80211_set_bss,
2671 .policy = nl80211_policy,
2672 .flags = GENL_ADMIN_PERM,
2673 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002674 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002675 .cmd = NL80211_CMD_GET_REG,
2676 .doit = nl80211_get_reg,
2677 .policy = nl80211_policy,
2678 /* can be retrieved by unprivileged users */
2679 },
2680 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002681 .cmd = NL80211_CMD_SET_REG,
2682 .doit = nl80211_set_reg,
2683 .policy = nl80211_policy,
2684 .flags = GENL_ADMIN_PERM,
2685 },
2686 {
2687 .cmd = NL80211_CMD_REQ_SET_REG,
2688 .doit = nl80211_req_set_reg,
2689 .policy = nl80211_policy,
2690 .flags = GENL_ADMIN_PERM,
2691 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002692 {
2693 .cmd = NL80211_CMD_GET_MESH_PARAMS,
2694 .doit = nl80211_get_mesh_params,
2695 .policy = nl80211_policy,
2696 /* can be retrieved by unprivileged users */
2697 },
2698 {
2699 .cmd = NL80211_CMD_SET_MESH_PARAMS,
2700 .doit = nl80211_set_mesh_params,
2701 .policy = nl80211_policy,
2702 .flags = GENL_ADMIN_PERM,
2703 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02002704 {
2705 .cmd = NL80211_CMD_SET_MGMT_EXTRA_IE,
2706 .doit = nl80211_set_mgmt_extra_ie,
2707 .policy = nl80211_policy,
2708 .flags = GENL_ADMIN_PERM,
2709 },
Johannes Berg2a519312009-02-10 21:25:55 +01002710 {
2711 .cmd = NL80211_CMD_TRIGGER_SCAN,
2712 .doit = nl80211_trigger_scan,
2713 .policy = nl80211_policy,
2714 .flags = GENL_ADMIN_PERM,
2715 },
2716 {
2717 .cmd = NL80211_CMD_GET_SCAN,
2718 .policy = nl80211_policy,
2719 .dumpit = nl80211_dump_scan,
2720 },
Johannes Berg55682962007-09-20 13:09:35 -04002721};
2722
2723/* multicast groups */
2724static struct genl_multicast_group nl80211_config_mcgrp = {
2725 .name = "config",
2726};
Johannes Berg2a519312009-02-10 21:25:55 +01002727static struct genl_multicast_group nl80211_scan_mcgrp = {
2728 .name = "scan",
2729};
Johannes Berg55682962007-09-20 13:09:35 -04002730
2731/* notification functions */
2732
2733void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
2734{
2735 struct sk_buff *msg;
2736
2737 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2738 if (!msg)
2739 return;
2740
2741 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
2742 nlmsg_free(msg);
2743 return;
2744 }
2745
2746 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
2747}
2748
Johannes Berg2a519312009-02-10 21:25:55 +01002749static int nl80211_send_scan_donemsg(struct sk_buff *msg,
2750 struct cfg80211_registered_device *rdev,
2751 struct net_device *netdev,
2752 u32 pid, u32 seq, int flags,
2753 u32 cmd)
2754{
2755 void *hdr;
2756
2757 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
2758 if (!hdr)
2759 return -1;
2760
2761 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->idx);
2762 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
2763
2764 /* XXX: we should probably bounce back the request? */
2765
2766 return genlmsg_end(msg, hdr);
2767
2768 nla_put_failure:
2769 genlmsg_cancel(msg, hdr);
2770 return -EMSGSIZE;
2771}
2772
2773void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
2774 struct net_device *netdev)
2775{
2776 struct sk_buff *msg;
2777
2778 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2779 if (!msg)
2780 return;
2781
2782 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
2783 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
2784 nlmsg_free(msg);
2785 return;
2786 }
2787
2788 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
2789}
2790
2791void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
2792 struct net_device *netdev)
2793{
2794 struct sk_buff *msg;
2795
2796 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2797 if (!msg)
2798 return;
2799
2800 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
2801 NL80211_CMD_SCAN_ABORTED) < 0) {
2802 nlmsg_free(msg);
2803 return;
2804 }
2805
2806 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
2807}
2808
Johannes Berg55682962007-09-20 13:09:35 -04002809/* initialisation/exit functions */
2810
2811int nl80211_init(void)
2812{
2813 int err, i;
2814
2815 err = genl_register_family(&nl80211_fam);
2816 if (err)
2817 return err;
2818
2819 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
2820 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
2821 if (err)
2822 goto err_out;
2823 }
2824
2825 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
2826 if (err)
2827 goto err_out;
2828
Johannes Berg2a519312009-02-10 21:25:55 +01002829 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
2830 if (err)
2831 goto err_out;
2832
Johannes Berg55682962007-09-20 13:09:35 -04002833 return 0;
2834 err_out:
2835 genl_unregister_family(&nl80211_fam);
2836 return err;
2837}
2838
2839void nl80211_exit(void)
2840{
2841 genl_unregister_family(&nl80211_fam);
2842}