blob: 6f38ee7a3c92656c6576bdf944db08b0ff465a24 [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>
Johannes Berg55682962007-09-20 13:09:35 -040010#include <linux/list.h>
11#include <linux/if_ether.h>
12#include <linux/ieee80211.h>
13#include <linux/nl80211.h>
14#include <linux/rtnetlink.h>
15#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010016#include <linux/etherdevice.h>
Johannes Berg55682962007-09-20 13:09:35 -040017#include <net/genetlink.h>
18#include <net/cfg80211.h>
19#include "core.h"
20#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070021#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040022
23/* the netlink family */
24static struct genl_family nl80211_fam = {
25 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
26 .name = "nl80211", /* have users key off the name instead */
27 .hdrsize = 0, /* no private header */
28 .version = 1, /* no particular meaning now */
29 .maxattr = NL80211_ATTR_MAX,
30};
31
32/* internal helper: get drv and dev */
Johannes Bergbba95fe2008-07-29 13:22:51 +020033static int get_drv_dev_by_info_ifindex(struct nlattr **attrs,
Johannes Berg55682962007-09-20 13:09:35 -040034 struct cfg80211_registered_device **drv,
35 struct net_device **dev)
36{
37 int ifindex;
38
Johannes Bergbba95fe2008-07-29 13:22:51 +020039 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040040 return -EINVAL;
41
Johannes Bergbba95fe2008-07-29 13:22:51 +020042 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg55682962007-09-20 13:09:35 -040043 *dev = dev_get_by_index(&init_net, ifindex);
44 if (!*dev)
45 return -ENODEV;
46
47 *drv = cfg80211_get_dev_from_ifindex(ifindex);
48 if (IS_ERR(*drv)) {
49 dev_put(*dev);
50 return PTR_ERR(*drv);
51 }
52
53 return 0;
54}
55
56/* policy for the attributes */
57static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
58 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
59 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
60 .len = BUS_ID_SIZE-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020061 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020062 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053063 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -040064
65 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
66 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
67 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010068
69 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
70
71 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
72 .len = WLAN_MAX_KEY_LEN },
73 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
74 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
75 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Johannes Berged1b6cc2007-12-19 02:03:32 +010076
77 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
78 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
79 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
80 .len = IEEE80211_MAX_DATA_LEN },
81 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
82 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010083 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
84 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
85 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
86 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
87 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010088 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +010089 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +020090 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010091 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
92 .len = IEEE80211_MAX_MESH_ID_LEN },
93 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +030094
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070095 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
96 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
97
Jouni Malinen9f1ba902008-08-07 20:07:01 +030098 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
99 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
100 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200101 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
102 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300103
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700104 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
105
Jouni Malinen36aedc92008-08-25 11:58:58 +0300106 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
107 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200108
109 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
110 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
111 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100112 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
113 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200114
115 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
116 .len = IEEE80211_MAX_SSID_LEN },
117 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
118 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400119};
120
121/* message building helper */
122static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
123 int flags, u8 cmd)
124{
125 /* since there is no private header just add the generic one */
126 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
127}
128
129/* netlink command implementations */
130
131static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
132 struct cfg80211_registered_device *dev)
133{
134 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100135 struct nlattr *nl_bands, *nl_band;
136 struct nlattr *nl_freqs, *nl_freq;
137 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700138 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100139 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100140 enum ieee80211_band band;
141 struct ieee80211_channel *chan;
142 struct ieee80211_rate *rate;
143 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700144 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400145
146 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
147 if (!hdr)
148 return -1;
149
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500150 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400151 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Johannes Berg2a519312009-02-10 21:25:55 +0100152 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
153 dev->wiphy.max_scan_ssids);
Johannes Bergee688b002008-01-24 19:38:39 +0100154
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700155 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
156 if (!nl_modes)
157 goto nla_put_failure;
158
159 i = 0;
160 while (ifmodes) {
161 if (ifmodes & 1)
162 NLA_PUT_FLAG(msg, i);
163 ifmodes >>= 1;
164 i++;
165 }
166
167 nla_nest_end(msg, nl_modes);
168
Johannes Bergee688b002008-01-24 19:38:39 +0100169 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
170 if (!nl_bands)
171 goto nla_put_failure;
172
173 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
174 if (!dev->wiphy.bands[band])
175 continue;
176
177 nl_band = nla_nest_start(msg, band);
178 if (!nl_band)
179 goto nla_put_failure;
180
Johannes Bergd51626d2008-10-09 12:20:13 +0200181 /* add HT info */
182 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
183 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
184 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
185 &dev->wiphy.bands[band]->ht_cap.mcs);
186 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
187 dev->wiphy.bands[band]->ht_cap.cap);
188 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
189 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
190 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
191 dev->wiphy.bands[band]->ht_cap.ampdu_density);
192 }
193
Johannes Bergee688b002008-01-24 19:38:39 +0100194 /* add frequencies */
195 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
196 if (!nl_freqs)
197 goto nla_put_failure;
198
199 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
200 nl_freq = nla_nest_start(msg, i);
201 if (!nl_freq)
202 goto nla_put_failure;
203
204 chan = &dev->wiphy.bands[band]->channels[i];
205 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
206 chan->center_freq);
207
208 if (chan->flags & IEEE80211_CHAN_DISABLED)
209 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
210 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
211 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
212 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
213 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
214 if (chan->flags & IEEE80211_CHAN_RADAR)
215 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
216
Jouni Malinenbf8c1ac2008-11-22 22:00:31 +0200217 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
218 DBM_TO_MBM(chan->max_power));
Jouni Malinene2f367f262008-11-21 19:01:30 +0200219
Johannes Bergee688b002008-01-24 19:38:39 +0100220 nla_nest_end(msg, nl_freq);
221 }
222
223 nla_nest_end(msg, nl_freqs);
224
225 /* add bitrates */
226 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
227 if (!nl_rates)
228 goto nla_put_failure;
229
230 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
231 nl_rate = nla_nest_start(msg, i);
232 if (!nl_rate)
233 goto nla_put_failure;
234
235 rate = &dev->wiphy.bands[band]->bitrates[i];
236 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
237 rate->bitrate);
238 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
239 NLA_PUT_FLAG(msg,
240 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
241
242 nla_nest_end(msg, nl_rate);
243 }
244
245 nla_nest_end(msg, nl_rates);
246
247 nla_nest_end(msg, nl_band);
248 }
249 nla_nest_end(msg, nl_bands);
250
Johannes Berg8fdc6212009-03-14 09:34:01 +0100251 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
252 if (!nl_cmds)
253 goto nla_put_failure;
254
255 i = 0;
256#define CMD(op, n) \
257 do { \
258 if (dev->ops->op) { \
259 i++; \
260 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
261 } \
262 } while (0)
263
264 CMD(add_virtual_intf, NEW_INTERFACE);
265 CMD(change_virtual_intf, SET_INTERFACE);
266 CMD(add_key, NEW_KEY);
267 CMD(add_beacon, NEW_BEACON);
268 CMD(add_station, NEW_STATION);
269 CMD(add_mpath, NEW_MPATH);
270 CMD(set_mesh_params, SET_MESH_PARAMS);
271 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200272 CMD(auth, AUTHENTICATE);
273 CMD(assoc, ASSOCIATE);
274 CMD(deauth, DEAUTHENTICATE);
275 CMD(disassoc, DISASSOCIATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100276
277#undef CMD
278 nla_nest_end(msg, nl_cmds);
279
Johannes Berg55682962007-09-20 13:09:35 -0400280 return genlmsg_end(msg, hdr);
281
282 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700283 genlmsg_cancel(msg, hdr);
284 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400285}
286
287static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
288{
289 int idx = 0;
290 int start = cb->args[0];
291 struct cfg80211_registered_device *dev;
292
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500293 mutex_lock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400294 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Julius Volzb4637272008-07-08 14:02:19 +0200295 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400296 continue;
297 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
298 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200299 dev) < 0) {
300 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400301 break;
Julius Volzb4637272008-07-08 14:02:19 +0200302 }
Johannes Berg55682962007-09-20 13:09:35 -0400303 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500304 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400305
306 cb->args[0] = idx;
307
308 return skb->len;
309}
310
311static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
312{
313 struct sk_buff *msg;
314 struct cfg80211_registered_device *dev;
315
316 dev = cfg80211_get_dev_from_info(info);
317 if (IS_ERR(dev))
318 return PTR_ERR(dev);
319
320 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
321 if (!msg)
322 goto out_err;
323
324 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
325 goto out_free;
326
327 cfg80211_put_dev(dev);
328
329 return genlmsg_unicast(msg, info->snd_pid);
330
331 out_free:
332 nlmsg_free(msg);
333 out_err:
334 cfg80211_put_dev(dev);
335 return -ENOBUFS;
336}
337
Jouni Malinen31888482008-10-30 16:59:24 +0200338static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
339 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
340 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
341 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
342 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
343 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
344};
345
346static int parse_txq_params(struct nlattr *tb[],
347 struct ieee80211_txq_params *txq_params)
348{
349 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
350 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
351 !tb[NL80211_TXQ_ATTR_AIFS])
352 return -EINVAL;
353
354 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
355 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
356 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
357 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
358 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
359
360 return 0;
361}
362
Johannes Berg55682962007-09-20 13:09:35 -0400363static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
364{
365 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200366 int result = 0, rem_txq_params = 0;
367 struct nlattr *nl_txq_params;
Johannes Berg55682962007-09-20 13:09:35 -0400368
369 rdev = cfg80211_get_dev_from_info(info);
370 if (IS_ERR(rdev))
371 return PTR_ERR(rdev);
372
Jouni Malinen31888482008-10-30 16:59:24 +0200373 if (info->attrs[NL80211_ATTR_WIPHY_NAME]) {
374 result = cfg80211_dev_rename(
375 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
376 if (result)
377 goto bad_res;
378 }
Johannes Berg55682962007-09-20 13:09:35 -0400379
Jouni Malinen31888482008-10-30 16:59:24 +0200380 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
381 struct ieee80211_txq_params txq_params;
382 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
383
384 if (!rdev->ops->set_txq_params) {
385 result = -EOPNOTSUPP;
386 goto bad_res;
387 }
388
389 nla_for_each_nested(nl_txq_params,
390 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
391 rem_txq_params) {
392 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
393 nla_data(nl_txq_params),
394 nla_len(nl_txq_params),
395 txq_params_policy);
396 result = parse_txq_params(tb, &txq_params);
397 if (result)
398 goto bad_res;
399
400 result = rdev->ops->set_txq_params(&rdev->wiphy,
401 &txq_params);
402 if (result)
403 goto bad_res;
404 }
405 }
406
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200407 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530408 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200409 struct ieee80211_channel *chan;
Johannes Berg306d6112008-12-08 12:39:04 +0100410 struct ieee80211_sta_ht_cap *ht_cap;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200411 u32 freq, sec_freq;
412
413 if (!rdev->ops->set_channel) {
414 result = -EOPNOTSUPP;
415 goto bad_res;
416 }
417
Johannes Berg306d6112008-12-08 12:39:04 +0100418 result = -EINVAL;
419
Sujith094d05d2008-12-12 11:57:43 +0530420 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
421 channel_type = nla_get_u32(info->attrs[
422 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
423 if (channel_type != NL80211_CHAN_NO_HT &&
424 channel_type != NL80211_CHAN_HT20 &&
425 channel_type != NL80211_CHAN_HT40PLUS &&
426 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200427 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200428 }
429
430 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
431 chan = ieee80211_get_channel(&rdev->wiphy, freq);
Johannes Berg306d6112008-12-08 12:39:04 +0100432
433 /* Primary channel not allowed */
434 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200435 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100436
Sujith094d05d2008-12-12 11:57:43 +0530437 if (channel_type == NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200438 sec_freq = freq - 20;
Sujith094d05d2008-12-12 11:57:43 +0530439 else if (channel_type == NL80211_CHAN_HT40PLUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200440 sec_freq = freq + 20;
441 else
442 sec_freq = 0;
443
Johannes Berg306d6112008-12-08 12:39:04 +0100444 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
445
446 /* no HT capabilities */
Sujith094d05d2008-12-12 11:57:43 +0530447 if (channel_type != NL80211_CHAN_NO_HT &&
Johannes Berg306d6112008-12-08 12:39:04 +0100448 !ht_cap->ht_supported)
449 goto bad_res;
450
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200451 if (sec_freq) {
452 struct ieee80211_channel *schan;
Johannes Berg306d6112008-12-08 12:39:04 +0100453
454 /* no 40 MHz capabilities */
455 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
456 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200457 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100458
459 schan = ieee80211_get_channel(&rdev->wiphy, sec_freq);
460
461 /* Secondary channel not allowed */
462 if (!schan || schan->flags & IEEE80211_CHAN_DISABLED)
463 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200464 }
465
466 result = rdev->ops->set_channel(&rdev->wiphy, chan,
Sujith094d05d2008-12-12 11:57:43 +0530467 channel_type);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200468 if (result)
469 goto bad_res;
470 }
471
472
Johannes Berg306d6112008-12-08 12:39:04 +0100473 bad_res:
Johannes Berg55682962007-09-20 13:09:35 -0400474 cfg80211_put_dev(rdev);
475 return result;
476}
477
478
479static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
480 struct net_device *dev)
481{
482 void *hdr;
483
484 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
485 if (!hdr)
486 return -1;
487
488 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
489 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200490 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Berg55682962007-09-20 13:09:35 -0400491 return genlmsg_end(msg, hdr);
492
493 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700494 genlmsg_cancel(msg, hdr);
495 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400496}
497
498static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
499{
500 int wp_idx = 0;
501 int if_idx = 0;
502 int wp_start = cb->args[0];
503 int if_start = cb->args[1];
504 struct cfg80211_registered_device *dev;
505 struct wireless_dev *wdev;
506
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500507 mutex_lock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400508 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200509 if (wp_idx < wp_start) {
510 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400511 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200512 }
Johannes Berg55682962007-09-20 13:09:35 -0400513 if_idx = 0;
514
515 mutex_lock(&dev->devlist_mtx);
516 list_for_each_entry(wdev, &dev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200517 if (if_idx < if_start) {
518 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400519 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200520 }
Johannes Berg55682962007-09-20 13:09:35 -0400521 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
522 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergbba95fe2008-07-29 13:22:51 +0200523 wdev->netdev) < 0) {
524 mutex_unlock(&dev->devlist_mtx);
525 goto out;
526 }
527 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400528 }
529 mutex_unlock(&dev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200530
531 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400532 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200533 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500534 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400535
536 cb->args[0] = wp_idx;
537 cb->args[1] = if_idx;
538
539 return skb->len;
540}
541
542static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
543{
544 struct sk_buff *msg;
545 struct cfg80211_registered_device *dev;
546 struct net_device *netdev;
547 int err;
548
Johannes Bergbba95fe2008-07-29 13:22:51 +0200549 err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400550 if (err)
551 return err;
552
553 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
554 if (!msg)
555 goto out_err;
556
557 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
558 goto out_free;
559
560 dev_put(netdev);
561 cfg80211_put_dev(dev);
562
563 return genlmsg_unicast(msg, info->snd_pid);
564
565 out_free:
566 nlmsg_free(msg);
567 out_err:
568 dev_put(netdev);
569 cfg80211_put_dev(dev);
570 return -ENOBUFS;
571}
572
Michael Wu66f7ac52008-01-31 19:48:22 +0100573static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
574 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
575 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
576 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
577 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
578 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
579};
580
581static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
582{
583 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
584 int flag;
585
586 *mntrflags = 0;
587
588 if (!nla)
589 return -EINVAL;
590
591 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
592 nla, mntr_flags_policy))
593 return -EINVAL;
594
595 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
596 if (flags[flag])
597 *mntrflags |= (1<<flag);
598
599 return 0;
600}
601
Johannes Berg55682962007-09-20 13:09:35 -0400602static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
603{
604 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100605 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400606 int err, ifindex;
607 enum nl80211_iftype type;
608 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +0200609 u32 _flags, *flags = NULL;
Johannes Berg55682962007-09-20 13:09:35 -0400610
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100611 memset(&params, 0, sizeof(params));
612
Johannes Berg3b858752009-03-12 09:55:09 +0100613 rtnl_lock();
614
Johannes Bergbba95fe2008-07-29 13:22:51 +0200615 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400616 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100617 goto unlock_rtnl;
618
Johannes Berg55682962007-09-20 13:09:35 -0400619 ifindex = dev->ifindex;
Johannes Berg723b0382008-09-16 20:22:09 +0200620 type = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -0400621 dev_put(dev);
622
Johannes Berg723b0382008-09-16 20:22:09 +0200623 err = -EINVAL;
624 if (info->attrs[NL80211_ATTR_IFTYPE]) {
625 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
626 if (type > NL80211_IFTYPE_MAX)
627 goto unlock;
628 }
629
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700630 if (!drv->ops->change_virtual_intf ||
631 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400632 err = -EOPNOTSUPP;
633 goto unlock;
634 }
635
Johannes Berg92ffe052008-09-16 20:39:36 +0200636 if (info->attrs[NL80211_ATTR_MESH_ID]) {
637 if (type != NL80211_IFTYPE_MESH_POINT) {
638 err = -EINVAL;
639 goto unlock;
640 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100641 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
642 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
643 }
644
Johannes Berg92ffe052008-09-16 20:39:36 +0200645 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
646 if (type != NL80211_IFTYPE_MONITOR) {
647 err = -EINVAL;
648 goto unlock;
649 }
650 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
651 &_flags);
652 if (!err)
653 flags = &_flags;
654 }
Johannes Berg3b858752009-03-12 09:55:09 +0100655
Michael Wu66f7ac52008-01-31 19:48:22 +0100656 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
Johannes Berg92ffe052008-09-16 20:39:36 +0200657 type, flags, &params);
Johannes Berg60719ff2008-09-16 14:55:09 +0200658
659 dev = __dev_get_by_index(&init_net, ifindex);
660 WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != type));
661
Johannes Berg55682962007-09-20 13:09:35 -0400662 unlock:
663 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100664 unlock_rtnl:
665 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400666 return err;
667}
668
669static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
670{
671 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100672 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400673 int err;
674 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +0100675 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -0400676
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100677 memset(&params, 0, sizeof(params));
678
Johannes Berg55682962007-09-20 13:09:35 -0400679 if (!info->attrs[NL80211_ATTR_IFNAME])
680 return -EINVAL;
681
682 if (info->attrs[NL80211_ATTR_IFTYPE]) {
683 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
684 if (type > NL80211_IFTYPE_MAX)
685 return -EINVAL;
686 }
687
Johannes Berg3b858752009-03-12 09:55:09 +0100688 rtnl_lock();
689
Johannes Berg55682962007-09-20 13:09:35 -0400690 drv = cfg80211_get_dev_from_info(info);
Johannes Berg3b858752009-03-12 09:55:09 +0100691 if (IS_ERR(drv)) {
692 err = PTR_ERR(drv);
693 goto unlock_rtnl;
694 }
Johannes Berg55682962007-09-20 13:09:35 -0400695
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700696 if (!drv->ops->add_virtual_intf ||
697 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400698 err = -EOPNOTSUPP;
699 goto unlock;
700 }
701
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100702 if (type == NL80211_IFTYPE_MESH_POINT &&
703 info->attrs[NL80211_ATTR_MESH_ID]) {
704 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
705 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
706 }
707
Michael Wu66f7ac52008-01-31 19:48:22 +0100708 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
709 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
710 &flags);
Johannes Berg55682962007-09-20 13:09:35 -0400711 err = drv->ops->add_virtual_intf(&drv->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +0100712 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100713 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100714
Johannes Berg55682962007-09-20 13:09:35 -0400715 unlock:
716 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100717 unlock_rtnl:
718 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400719 return err;
720}
721
722static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
723{
724 struct cfg80211_registered_device *drv;
725 int ifindex, err;
726 struct net_device *dev;
727
Johannes Berg3b858752009-03-12 09:55:09 +0100728 rtnl_lock();
729
Johannes Bergbba95fe2008-07-29 13:22:51 +0200730 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400731 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100732 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -0400733 ifindex = dev->ifindex;
734 dev_put(dev);
735
736 if (!drv->ops->del_virtual_intf) {
737 err = -EOPNOTSUPP;
738 goto out;
739 }
740
Johannes Berg55682962007-09-20 13:09:35 -0400741 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -0400742
743 out:
744 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100745 unlock_rtnl:
746 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400747 return err;
748}
749
Johannes Berg41ade002007-12-19 02:03:29 +0100750struct get_key_cookie {
751 struct sk_buff *msg;
752 int error;
753};
754
755static void get_key_callback(void *c, struct key_params *params)
756{
757 struct get_key_cookie *cookie = c;
758
759 if (params->key)
760 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
761 params->key_len, params->key);
762
763 if (params->seq)
764 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
765 params->seq_len, params->seq);
766
767 if (params->cipher)
768 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
769 params->cipher);
770
771 return;
772 nla_put_failure:
773 cookie->error = 1;
774}
775
776static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
777{
778 struct cfg80211_registered_device *drv;
779 int err;
780 struct net_device *dev;
781 u8 key_idx = 0;
782 u8 *mac_addr = NULL;
783 struct get_key_cookie cookie = {
784 .error = 0,
785 };
786 void *hdr;
787 struct sk_buff *msg;
788
789 if (info->attrs[NL80211_ATTR_KEY_IDX])
790 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
791
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200792 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100793 return -EINVAL;
794
795 if (info->attrs[NL80211_ATTR_MAC])
796 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
797
Johannes Berg3b858752009-03-12 09:55:09 +0100798 rtnl_lock();
799
Johannes Bergbba95fe2008-07-29 13:22:51 +0200800 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100801 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100802 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +0100803
804 if (!drv->ops->get_key) {
805 err = -EOPNOTSUPP;
806 goto out;
807 }
808
809 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
810 if (!msg) {
811 err = -ENOMEM;
812 goto out;
813 }
814
815 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
816 NL80211_CMD_NEW_KEY);
817
818 if (IS_ERR(hdr)) {
819 err = PTR_ERR(hdr);
820 goto out;
821 }
822
823 cookie.msg = msg;
824
825 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
826 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
827 if (mac_addr)
828 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
829
Johannes Berg41ade002007-12-19 02:03:29 +0100830 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
831 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +0100832
833 if (err)
834 goto out;
835
836 if (cookie.error)
837 goto nla_put_failure;
838
839 genlmsg_end(msg, hdr);
840 err = genlmsg_unicast(msg, info->snd_pid);
841 goto out;
842
843 nla_put_failure:
844 err = -ENOBUFS;
845 nlmsg_free(msg);
846 out:
847 cfg80211_put_dev(drv);
848 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +0100849 unlock_rtnl:
850 rtnl_unlock();
851
Johannes Berg41ade002007-12-19 02:03:29 +0100852 return err;
853}
854
855static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
856{
857 struct cfg80211_registered_device *drv;
858 int err;
859 struct net_device *dev;
860 u8 key_idx;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200861 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
862 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +0100863
864 if (!info->attrs[NL80211_ATTR_KEY_IDX])
865 return -EINVAL;
866
867 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
868
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200869 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
870 if (key_idx < 4 || key_idx > 5)
871 return -EINVAL;
872 } else if (key_idx > 3)
Johannes Berg41ade002007-12-19 02:03:29 +0100873 return -EINVAL;
874
875 /* currently only support setting default key */
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200876 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
877 !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
Johannes Berg41ade002007-12-19 02:03:29 +0100878 return -EINVAL;
879
Johannes Berg3b858752009-03-12 09:55:09 +0100880 rtnl_lock();
881
Johannes Bergbba95fe2008-07-29 13:22:51 +0200882 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100883 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100884 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +0100885
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200886 if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
887 func = drv->ops->set_default_key;
888 else
889 func = drv->ops->set_default_mgmt_key;
890
891 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +0100892 err = -EOPNOTSUPP;
893 goto out;
894 }
895
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200896 err = func(&drv->wiphy, dev, key_idx);
Johannes Berg41ade002007-12-19 02:03:29 +0100897
898 out:
899 cfg80211_put_dev(drv);
900 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +0100901
902 unlock_rtnl:
903 rtnl_unlock();
904
Johannes Berg41ade002007-12-19 02:03:29 +0100905 return err;
906}
907
908static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
909{
910 struct cfg80211_registered_device *drv;
911 int err;
912 struct net_device *dev;
913 struct key_params params;
914 u8 key_idx = 0;
915 u8 *mac_addr = NULL;
916
917 memset(&params, 0, sizeof(params));
918
919 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
920 return -EINVAL;
921
922 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
923 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
924 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
925 }
926
927 if (info->attrs[NL80211_ATTR_KEY_IDX])
928 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
929
930 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
931
932 if (info->attrs[NL80211_ATTR_MAC])
933 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
934
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200935 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100936 return -EINVAL;
937
938 /*
939 * Disallow pairwise keys with non-zero index unless it's WEP
940 * (because current deployments use pairwise WEP keys with
941 * non-zero indizes but 802.11i clearly specifies to use zero)
942 */
943 if (mac_addr && key_idx &&
944 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
945 params.cipher != WLAN_CIPHER_SUITE_WEP104)
946 return -EINVAL;
947
948 /* TODO: add definitions for the lengths to linux/ieee80211.h */
949 switch (params.cipher) {
950 case WLAN_CIPHER_SUITE_WEP40:
951 if (params.key_len != 5)
952 return -EINVAL;
953 break;
954 case WLAN_CIPHER_SUITE_TKIP:
955 if (params.key_len != 32)
956 return -EINVAL;
957 break;
958 case WLAN_CIPHER_SUITE_CCMP:
959 if (params.key_len != 16)
960 return -EINVAL;
961 break;
962 case WLAN_CIPHER_SUITE_WEP104:
963 if (params.key_len != 13)
964 return -EINVAL;
965 break;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200966 case WLAN_CIPHER_SUITE_AES_CMAC:
967 if (params.key_len != 16)
968 return -EINVAL;
969 break;
Johannes Berg41ade002007-12-19 02:03:29 +0100970 default:
971 return -EINVAL;
972 }
973
Johannes Berg3b858752009-03-12 09:55:09 +0100974 rtnl_lock();
975
Johannes Bergbba95fe2008-07-29 13:22:51 +0200976 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100977 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100978 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +0100979
980 if (!drv->ops->add_key) {
981 err = -EOPNOTSUPP;
982 goto out;
983 }
984
Johannes Berg41ade002007-12-19 02:03:29 +0100985 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, &params);
Johannes Berg41ade002007-12-19 02:03:29 +0100986
987 out:
988 cfg80211_put_dev(drv);
989 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +0100990 unlock_rtnl:
991 rtnl_unlock();
992
Johannes Berg41ade002007-12-19 02:03:29 +0100993 return err;
994}
995
996static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
997{
998 struct cfg80211_registered_device *drv;
999 int err;
1000 struct net_device *dev;
1001 u8 key_idx = 0;
1002 u8 *mac_addr = NULL;
1003
1004 if (info->attrs[NL80211_ATTR_KEY_IDX])
1005 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1006
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001007 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001008 return -EINVAL;
1009
1010 if (info->attrs[NL80211_ATTR_MAC])
1011 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1012
Johannes Berg3b858752009-03-12 09:55:09 +01001013 rtnl_lock();
1014
Johannes Bergbba95fe2008-07-29 13:22:51 +02001015 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001016 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001017 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001018
1019 if (!drv->ops->del_key) {
1020 err = -EOPNOTSUPP;
1021 goto out;
1022 }
1023
Johannes Berg41ade002007-12-19 02:03:29 +01001024 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001025
1026 out:
1027 cfg80211_put_dev(drv);
1028 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001029
1030 unlock_rtnl:
1031 rtnl_unlock();
1032
Johannes Berg41ade002007-12-19 02:03:29 +01001033 return err;
1034}
1035
Johannes Berged1b6cc2007-12-19 02:03:32 +01001036static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1037{
1038 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1039 struct beacon_parameters *info);
1040 struct cfg80211_registered_device *drv;
1041 int err;
1042 struct net_device *dev;
1043 struct beacon_parameters params;
1044 int haveinfo = 0;
1045
Johannes Berg3b858752009-03-12 09:55:09 +01001046 rtnl_lock();
1047
Johannes Bergbba95fe2008-07-29 13:22:51 +02001048 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001049 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001050 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001051
1052 switch (info->genlhdr->cmd) {
1053 case NL80211_CMD_NEW_BEACON:
1054 /* these are required for NEW_BEACON */
1055 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1056 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1057 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1058 err = -EINVAL;
1059 goto out;
1060 }
1061
1062 call = drv->ops->add_beacon;
1063 break;
1064 case NL80211_CMD_SET_BEACON:
1065 call = drv->ops->set_beacon;
1066 break;
1067 default:
1068 WARN_ON(1);
1069 err = -EOPNOTSUPP;
1070 goto out;
1071 }
1072
1073 if (!call) {
1074 err = -EOPNOTSUPP;
1075 goto out;
1076 }
1077
1078 memset(&params, 0, sizeof(params));
1079
1080 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1081 params.interval =
1082 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1083 haveinfo = 1;
1084 }
1085
1086 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1087 params.dtim_period =
1088 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1089 haveinfo = 1;
1090 }
1091
1092 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1093 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1094 params.head_len =
1095 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1096 haveinfo = 1;
1097 }
1098
1099 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1100 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1101 params.tail_len =
1102 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1103 haveinfo = 1;
1104 }
1105
1106 if (!haveinfo) {
1107 err = -EINVAL;
1108 goto out;
1109 }
1110
Johannes Berged1b6cc2007-12-19 02:03:32 +01001111 err = call(&drv->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001112
1113 out:
1114 cfg80211_put_dev(drv);
1115 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001116 unlock_rtnl:
1117 rtnl_unlock();
1118
Johannes Berged1b6cc2007-12-19 02:03:32 +01001119 return err;
1120}
1121
1122static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1123{
1124 struct cfg80211_registered_device *drv;
1125 int err;
1126 struct net_device *dev;
1127
Johannes Berg3b858752009-03-12 09:55:09 +01001128 rtnl_lock();
1129
Johannes Bergbba95fe2008-07-29 13:22:51 +02001130 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001131 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001132 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001133
1134 if (!drv->ops->del_beacon) {
1135 err = -EOPNOTSUPP;
1136 goto out;
1137 }
1138
Johannes Berged1b6cc2007-12-19 02:03:32 +01001139 err = drv->ops->del_beacon(&drv->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001140
1141 out:
1142 cfg80211_put_dev(drv);
1143 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001144 unlock_rtnl:
1145 rtnl_unlock();
1146
Johannes Berged1b6cc2007-12-19 02:03:32 +01001147 return err;
1148}
1149
Johannes Berg5727ef12007-12-19 02:03:34 +01001150static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1151 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1152 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1153 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1154};
1155
1156static int parse_station_flags(struct nlattr *nla, u32 *staflags)
1157{
1158 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1159 int flag;
1160
1161 *staflags = 0;
1162
1163 if (!nla)
1164 return 0;
1165
1166 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1167 nla, sta_flags_policy))
1168 return -EINVAL;
1169
1170 *staflags = STATION_FLAG_CHANGED;
1171
1172 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1173 if (flags[flag])
1174 *staflags |= (1<<flag);
1175
1176 return 0;
1177}
1178
Henning Rogge420e7fa2008-12-11 22:04:19 +01001179static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1180{
1181 int modulation, streams, bitrate;
1182
1183 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1184 return rate->legacy;
1185
1186 /* the formula below does only work for MCS values smaller than 32 */
1187 if (rate->mcs >= 32)
1188 return 0;
1189
1190 modulation = rate->mcs & 7;
1191 streams = (rate->mcs >> 3) + 1;
1192
1193 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1194 13500000 : 6500000;
1195
1196 if (modulation < 4)
1197 bitrate *= (modulation + 1);
1198 else if (modulation == 4)
1199 bitrate *= (modulation + 2);
1200 else
1201 bitrate *= (modulation + 3);
1202
1203 bitrate *= streams;
1204
1205 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1206 bitrate = (bitrate / 9) * 10;
1207
1208 /* do NOT round down here */
1209 return (bitrate + 50000) / 100000;
1210}
1211
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001212static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1213 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001214 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001215{
1216 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001217 struct nlattr *sinfoattr, *txrate;
1218 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001219
1220 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1221 if (!hdr)
1222 return -1;
1223
1224 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1225 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1226
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001227 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1228 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001229 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001230 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1231 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1232 sinfo->inactive_time);
1233 if (sinfo->filled & STATION_INFO_RX_BYTES)
1234 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1235 sinfo->rx_bytes);
1236 if (sinfo->filled & STATION_INFO_TX_BYTES)
1237 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1238 sinfo->tx_bytes);
1239 if (sinfo->filled & STATION_INFO_LLID)
1240 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1241 sinfo->llid);
1242 if (sinfo->filled & STATION_INFO_PLID)
1243 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1244 sinfo->plid);
1245 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1246 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1247 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001248 if (sinfo->filled & STATION_INFO_SIGNAL)
1249 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1250 sinfo->signal);
1251 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1252 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1253 if (!txrate)
1254 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001255
Henning Rogge420e7fa2008-12-11 22:04:19 +01001256 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1257 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1258 if (bitrate > 0)
1259 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1260
1261 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1262 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1263 sinfo->txrate.mcs);
1264 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1265 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1266 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1267 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1268
1269 nla_nest_end(msg, txrate);
1270 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001271 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1272 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1273 sinfo->rx_packets);
1274 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1275 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1276 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001277 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001278
1279 return genlmsg_end(msg, hdr);
1280
1281 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001282 genlmsg_cancel(msg, hdr);
1283 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001284}
1285
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001286static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001287 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001288{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001289 struct station_info sinfo;
1290 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001291 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001292 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001293 int ifidx = cb->args[0];
1294 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001295 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001296
Johannes Bergbba95fe2008-07-29 13:22:51 +02001297 if (!ifidx) {
1298 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1299 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1300 nl80211_policy);
1301 if (err)
1302 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001303
Johannes Bergbba95fe2008-07-29 13:22:51 +02001304 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1305 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001306
Johannes Bergbba95fe2008-07-29 13:22:51 +02001307 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1308 if (!ifidx)
1309 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001310 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001311
Johannes Berg3b858752009-03-12 09:55:09 +01001312 rtnl_lock();
1313
1314 netdev = __dev_get_by_index(&init_net, ifidx);
1315 if (!netdev) {
1316 err = -ENODEV;
1317 goto out_rtnl;
1318 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001319
Johannes Bergbba95fe2008-07-29 13:22:51 +02001320 dev = cfg80211_get_dev_from_ifindex(ifidx);
1321 if (IS_ERR(dev)) {
1322 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001323 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001324 }
1325
1326 if (!dev->ops->dump_station) {
1327 err = -ENOSYS;
1328 goto out_err;
1329 }
1330
Johannes Bergbba95fe2008-07-29 13:22:51 +02001331 while (1) {
1332 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1333 mac_addr, &sinfo);
1334 if (err == -ENOENT)
1335 break;
1336 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001337 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001338
1339 if (nl80211_send_station(skb,
1340 NETLINK_CB(cb->skb).pid,
1341 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1342 netdev, mac_addr,
1343 &sinfo) < 0)
1344 goto out;
1345
1346 sta_idx++;
1347 }
1348
1349
1350 out:
1351 cb->args[1] = sta_idx;
1352 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001353 out_err:
1354 cfg80211_put_dev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001355 out_rtnl:
1356 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001357
1358 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001359}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001360
Johannes Berg5727ef12007-12-19 02:03:34 +01001361static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1362{
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001363 struct cfg80211_registered_device *drv;
1364 int err;
1365 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001366 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001367 struct sk_buff *msg;
1368 u8 *mac_addr = NULL;
1369
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001370 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001371
1372 if (!info->attrs[NL80211_ATTR_MAC])
1373 return -EINVAL;
1374
1375 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1376
Johannes Berg3b858752009-03-12 09:55:09 +01001377 rtnl_lock();
1378
Johannes Bergbba95fe2008-07-29 13:22:51 +02001379 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001380 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001381 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001382
1383 if (!drv->ops->get_station) {
1384 err = -EOPNOTSUPP;
1385 goto out;
1386 }
1387
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001388 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001389 if (err)
1390 goto out;
1391
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001392 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1393 if (!msg)
1394 goto out;
1395
1396 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001397 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001398 goto out_free;
1399
1400 err = genlmsg_unicast(msg, info->snd_pid);
1401 goto out;
1402
1403 out_free:
1404 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001405 out:
1406 cfg80211_put_dev(drv);
1407 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001408 out_rtnl:
1409 rtnl_unlock();
1410
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001411 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001412}
1413
1414/*
1415 * Get vlan interface making sure it is on the right wiphy.
1416 */
1417static int get_vlan(struct nlattr *vlanattr,
1418 struct cfg80211_registered_device *rdev,
1419 struct net_device **vlan)
1420{
1421 *vlan = NULL;
1422
1423 if (vlanattr) {
1424 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1425 if (!*vlan)
1426 return -ENODEV;
1427 if (!(*vlan)->ieee80211_ptr)
1428 return -EINVAL;
1429 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1430 return -EINVAL;
1431 }
1432 return 0;
1433}
1434
1435static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1436{
1437 struct cfg80211_registered_device *drv;
1438 int err;
1439 struct net_device *dev;
1440 struct station_parameters params;
1441 u8 *mac_addr = NULL;
1442
1443 memset(&params, 0, sizeof(params));
1444
1445 params.listen_interval = -1;
1446
1447 if (info->attrs[NL80211_ATTR_STA_AID])
1448 return -EINVAL;
1449
1450 if (!info->attrs[NL80211_ATTR_MAC])
1451 return -EINVAL;
1452
1453 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1454
1455 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1456 params.supported_rates =
1457 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1458 params.supported_rates_len =
1459 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1460 }
1461
1462 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1463 params.listen_interval =
1464 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1465
Jouni Malinen36aedc92008-08-25 11:58:58 +03001466 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1467 params.ht_capa =
1468 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1469
Johannes Berg5727ef12007-12-19 02:03:34 +01001470 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1471 &params.station_flags))
1472 return -EINVAL;
1473
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001474 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1475 params.plink_action =
1476 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1477
Johannes Berg3b858752009-03-12 09:55:09 +01001478 rtnl_lock();
1479
Johannes Bergbba95fe2008-07-29 13:22:51 +02001480 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001481 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001482 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001483
1484 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1485 if (err)
1486 goto out;
1487
1488 if (!drv->ops->change_station) {
1489 err = -EOPNOTSUPP;
1490 goto out;
1491 }
1492
Johannes Berg5727ef12007-12-19 02:03:34 +01001493 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001494
1495 out:
1496 if (params.vlan)
1497 dev_put(params.vlan);
1498 cfg80211_put_dev(drv);
1499 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001500 out_rtnl:
1501 rtnl_unlock();
1502
Johannes Berg5727ef12007-12-19 02:03:34 +01001503 return err;
1504}
1505
1506static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1507{
1508 struct cfg80211_registered_device *drv;
1509 int err;
1510 struct net_device *dev;
1511 struct station_parameters params;
1512 u8 *mac_addr = NULL;
1513
1514 memset(&params, 0, sizeof(params));
1515
1516 if (!info->attrs[NL80211_ATTR_MAC])
1517 return -EINVAL;
1518
1519 if (!info->attrs[NL80211_ATTR_STA_AID])
1520 return -EINVAL;
1521
1522 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1523 return -EINVAL;
1524
1525 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1526 return -EINVAL;
1527
1528 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1529 params.supported_rates =
1530 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1531 params.supported_rates_len =
1532 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1533 params.listen_interval =
1534 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg16f2e852008-04-07 14:35:46 +02001535 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Jouni Malinen36aedc92008-08-25 11:58:58 +03001536 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1537 params.ht_capa =
1538 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01001539
1540 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1541 &params.station_flags))
1542 return -EINVAL;
1543
Johannes Berg3b858752009-03-12 09:55:09 +01001544 rtnl_lock();
1545
Johannes Bergbba95fe2008-07-29 13:22:51 +02001546 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001547 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001548 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001549
1550 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1551 if (err)
1552 goto out;
1553
1554 if (!drv->ops->add_station) {
1555 err = -EOPNOTSUPP;
1556 goto out;
1557 }
1558
Johannes Berg5727ef12007-12-19 02:03:34 +01001559 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001560
1561 out:
1562 if (params.vlan)
1563 dev_put(params.vlan);
1564 cfg80211_put_dev(drv);
1565 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001566 out_rtnl:
1567 rtnl_unlock();
1568
Johannes Berg5727ef12007-12-19 02:03:34 +01001569 return err;
1570}
1571
1572static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1573{
1574 struct cfg80211_registered_device *drv;
1575 int err;
1576 struct net_device *dev;
1577 u8 *mac_addr = NULL;
1578
1579 if (info->attrs[NL80211_ATTR_MAC])
1580 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1581
Johannes Berg3b858752009-03-12 09:55:09 +01001582 rtnl_lock();
1583
Johannes Bergbba95fe2008-07-29 13:22:51 +02001584 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001585 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001586 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001587
1588 if (!drv->ops->del_station) {
1589 err = -EOPNOTSUPP;
1590 goto out;
1591 }
1592
Johannes Berg5727ef12007-12-19 02:03:34 +01001593 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01001594
1595 out:
1596 cfg80211_put_dev(drv);
1597 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001598 out_rtnl:
1599 rtnl_unlock();
1600
Johannes Berg5727ef12007-12-19 02:03:34 +01001601 return err;
1602}
1603
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001604static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1605 int flags, struct net_device *dev,
1606 u8 *dst, u8 *next_hop,
1607 struct mpath_info *pinfo)
1608{
1609 void *hdr;
1610 struct nlattr *pinfoattr;
1611
1612 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1613 if (!hdr)
1614 return -1;
1615
1616 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1617 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1618 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1619
1620 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1621 if (!pinfoattr)
1622 goto nla_put_failure;
1623 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1624 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1625 pinfo->frame_qlen);
1626 if (pinfo->filled & MPATH_INFO_DSN)
1627 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1628 pinfo->dsn);
1629 if (pinfo->filled & MPATH_INFO_METRIC)
1630 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1631 pinfo->metric);
1632 if (pinfo->filled & MPATH_INFO_EXPTIME)
1633 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1634 pinfo->exptime);
1635 if (pinfo->filled & MPATH_INFO_FLAGS)
1636 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1637 pinfo->flags);
1638 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1639 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1640 pinfo->discovery_timeout);
1641 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1642 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1643 pinfo->discovery_retries);
1644
1645 nla_nest_end(msg, pinfoattr);
1646
1647 return genlmsg_end(msg, hdr);
1648
1649 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001650 genlmsg_cancel(msg, hdr);
1651 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001652}
1653
1654static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001655 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001656{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001657 struct mpath_info pinfo;
1658 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001659 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001660 u8 dst[ETH_ALEN];
1661 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001662 int ifidx = cb->args[0];
1663 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001664 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001665
Johannes Bergbba95fe2008-07-29 13:22:51 +02001666 if (!ifidx) {
1667 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1668 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1669 nl80211_policy);
1670 if (err)
1671 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001672
Johannes Bergbba95fe2008-07-29 13:22:51 +02001673 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1674 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001675
Johannes Bergbba95fe2008-07-29 13:22:51 +02001676 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1677 if (!ifidx)
1678 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001679 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001680
Johannes Berg3b858752009-03-12 09:55:09 +01001681 rtnl_lock();
1682
1683 netdev = __dev_get_by_index(&init_net, ifidx);
1684 if (!netdev) {
1685 err = -ENODEV;
1686 goto out_rtnl;
1687 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001688
Johannes Bergbba95fe2008-07-29 13:22:51 +02001689 dev = cfg80211_get_dev_from_ifindex(ifidx);
1690 if (IS_ERR(dev)) {
1691 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001692 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001693 }
1694
1695 if (!dev->ops->dump_mpath) {
1696 err = -ENOSYS;
1697 goto out_err;
1698 }
1699
Johannes Bergbba95fe2008-07-29 13:22:51 +02001700 while (1) {
1701 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1702 dst, next_hop, &pinfo);
1703 if (err == -ENOENT)
1704 break;
1705 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001706 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001707
1708 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1709 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1710 netdev, dst, next_hop,
1711 &pinfo) < 0)
1712 goto out;
1713
1714 path_idx++;
1715 }
1716
1717
1718 out:
1719 cb->args[1] = path_idx;
1720 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001721 out_err:
1722 cfg80211_put_dev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001723 out_rtnl:
1724 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001725
1726 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001727}
1728
1729static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1730{
1731 struct cfg80211_registered_device *drv;
1732 int err;
1733 struct net_device *dev;
1734 struct mpath_info pinfo;
1735 struct sk_buff *msg;
1736 u8 *dst = NULL;
1737 u8 next_hop[ETH_ALEN];
1738
1739 memset(&pinfo, 0, sizeof(pinfo));
1740
1741 if (!info->attrs[NL80211_ATTR_MAC])
1742 return -EINVAL;
1743
1744 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1745
Johannes Berg3b858752009-03-12 09:55:09 +01001746 rtnl_lock();
1747
Johannes Bergbba95fe2008-07-29 13:22:51 +02001748 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001749 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001750 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001751
1752 if (!drv->ops->get_mpath) {
1753 err = -EOPNOTSUPP;
1754 goto out;
1755 }
1756
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001757 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001758 if (err)
1759 goto out;
1760
1761 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1762 if (!msg)
1763 goto out;
1764
1765 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1766 dev, dst, next_hop, &pinfo) < 0)
1767 goto out_free;
1768
1769 err = genlmsg_unicast(msg, info->snd_pid);
1770 goto out;
1771
1772 out_free:
1773 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001774 out:
1775 cfg80211_put_dev(drv);
1776 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001777 out_rtnl:
1778 rtnl_unlock();
1779
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001780 return err;
1781}
1782
1783static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1784{
1785 struct cfg80211_registered_device *drv;
1786 int err;
1787 struct net_device *dev;
1788 u8 *dst = NULL;
1789 u8 *next_hop = NULL;
1790
1791 if (!info->attrs[NL80211_ATTR_MAC])
1792 return -EINVAL;
1793
1794 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1795 return -EINVAL;
1796
1797 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1798 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1799
Johannes Berg3b858752009-03-12 09:55:09 +01001800 rtnl_lock();
1801
Johannes Bergbba95fe2008-07-29 13:22:51 +02001802 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001803 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001804 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001805
1806 if (!drv->ops->change_mpath) {
1807 err = -EOPNOTSUPP;
1808 goto out;
1809 }
1810
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001811 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001812
1813 out:
1814 cfg80211_put_dev(drv);
1815 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001816 out_rtnl:
1817 rtnl_unlock();
1818
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001819 return err;
1820}
1821static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1822{
1823 struct cfg80211_registered_device *drv;
1824 int err;
1825 struct net_device *dev;
1826 u8 *dst = NULL;
1827 u8 *next_hop = NULL;
1828
1829 if (!info->attrs[NL80211_ATTR_MAC])
1830 return -EINVAL;
1831
1832 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1833 return -EINVAL;
1834
1835 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1836 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1837
Johannes Berg3b858752009-03-12 09:55:09 +01001838 rtnl_lock();
1839
Johannes Bergbba95fe2008-07-29 13:22:51 +02001840 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001841 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001842 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001843
1844 if (!drv->ops->add_mpath) {
1845 err = -EOPNOTSUPP;
1846 goto out;
1847 }
1848
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001849 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001850
1851 out:
1852 cfg80211_put_dev(drv);
1853 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001854 out_rtnl:
1855 rtnl_unlock();
1856
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001857 return err;
1858}
1859
1860static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1861{
1862 struct cfg80211_registered_device *drv;
1863 int err;
1864 struct net_device *dev;
1865 u8 *dst = NULL;
1866
1867 if (info->attrs[NL80211_ATTR_MAC])
1868 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1869
Johannes Berg3b858752009-03-12 09:55:09 +01001870 rtnl_lock();
1871
Johannes Bergbba95fe2008-07-29 13:22:51 +02001872 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001873 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001874 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001875
1876 if (!drv->ops->del_mpath) {
1877 err = -EOPNOTSUPP;
1878 goto out;
1879 }
1880
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001881 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001882
1883 out:
1884 cfg80211_put_dev(drv);
1885 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001886 out_rtnl:
1887 rtnl_unlock();
1888
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001889 return err;
1890}
1891
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001892static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
1893{
1894 struct cfg80211_registered_device *drv;
1895 int err;
1896 struct net_device *dev;
1897 struct bss_parameters params;
1898
1899 memset(&params, 0, sizeof(params));
1900 /* default to not changing parameters */
1901 params.use_cts_prot = -1;
1902 params.use_short_preamble = -1;
1903 params.use_short_slot_time = -1;
1904
1905 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
1906 params.use_cts_prot =
1907 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
1908 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
1909 params.use_short_preamble =
1910 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
1911 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
1912 params.use_short_slot_time =
1913 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02001914 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
1915 params.basic_rates =
1916 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1917 params.basic_rates_len =
1918 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1919 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001920
Johannes Berg3b858752009-03-12 09:55:09 +01001921 rtnl_lock();
1922
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001923 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1924 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001925 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001926
1927 if (!drv->ops->change_bss) {
1928 err = -EOPNOTSUPP;
1929 goto out;
1930 }
1931
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001932 err = drv->ops->change_bss(&drv->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001933
1934 out:
1935 cfg80211_put_dev(drv);
1936 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001937 out_rtnl:
1938 rtnl_unlock();
1939
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001940 return err;
1941}
1942
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001943static const struct nla_policy
1944 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
1945 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1946 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1947 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1948 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1949 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1950 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1951};
1952
1953static int parse_reg_rule(struct nlattr *tb[],
1954 struct ieee80211_reg_rule *reg_rule)
1955{
1956 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
1957 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
1958
1959 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
1960 return -EINVAL;
1961 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
1962 return -EINVAL;
1963 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
1964 return -EINVAL;
1965 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1966 return -EINVAL;
1967 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1968 return -EINVAL;
1969
1970 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
1971
1972 freq_range->start_freq_khz =
1973 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
1974 freq_range->end_freq_khz =
1975 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
1976 freq_range->max_bandwidth_khz =
1977 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
1978
1979 power_rule->max_eirp =
1980 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
1981
1982 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
1983 power_rule->max_antenna_gain =
1984 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
1985
1986 return 0;
1987}
1988
1989static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
1990{
1991 int r;
1992 char *data = NULL;
1993
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05001994 /*
1995 * You should only get this when cfg80211 hasn't yet initialized
1996 * completely when built-in to the kernel right between the time
1997 * window between nl80211_init() and regulatory_init(), if that is
1998 * even possible.
1999 */
2000 mutex_lock(&cfg80211_mutex);
2001 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002002 mutex_unlock(&cfg80211_mutex);
2003 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002004 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002005 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002006
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002007 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2008 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002009
2010 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2011
2012#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2013 /* We ignore world regdom requests with the old regdom setup */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002014 if (is_world_regdom(data))
2015 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002016#endif
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002017
2018 r = regulatory_hint_user(data);
2019
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002020 return r;
2021}
2022
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002023static int nl80211_get_mesh_params(struct sk_buff *skb,
2024 struct genl_info *info)
2025{
2026 struct cfg80211_registered_device *drv;
2027 struct mesh_config cur_params;
2028 int err;
2029 struct net_device *dev;
2030 void *hdr;
2031 struct nlattr *pinfoattr;
2032 struct sk_buff *msg;
2033
Johannes Berg3b858752009-03-12 09:55:09 +01002034 rtnl_lock();
2035
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002036 /* Look up our device */
2037 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2038 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002039 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002040
Jouni Malinenf3f92582009-03-20 17:57:36 +02002041 if (!drv->ops->get_mesh_params) {
2042 err = -EOPNOTSUPP;
2043 goto out;
2044 }
2045
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002046 /* Get the mesh params */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002047 err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002048 if (err)
2049 goto out;
2050
2051 /* Draw up a netlink message to send back */
2052 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2053 if (!msg) {
2054 err = -ENOBUFS;
2055 goto out;
2056 }
2057 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2058 NL80211_CMD_GET_MESH_PARAMS);
2059 if (!hdr)
2060 goto nla_put_failure;
2061 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2062 if (!pinfoattr)
2063 goto nla_put_failure;
2064 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2065 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2066 cur_params.dot11MeshRetryTimeout);
2067 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2068 cur_params.dot11MeshConfirmTimeout);
2069 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2070 cur_params.dot11MeshHoldingTimeout);
2071 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2072 cur_params.dot11MeshMaxPeerLinks);
2073 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2074 cur_params.dot11MeshMaxRetries);
2075 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2076 cur_params.dot11MeshTTL);
2077 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2078 cur_params.auto_open_plinks);
2079 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2080 cur_params.dot11MeshHWMPmaxPREQretries);
2081 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2082 cur_params.path_refresh_time);
2083 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2084 cur_params.min_discovery_timeout);
2085 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2086 cur_params.dot11MeshHWMPactivePathTimeout);
2087 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2088 cur_params.dot11MeshHWMPpreqMinInterval);
2089 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2090 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2091 nla_nest_end(msg, pinfoattr);
2092 genlmsg_end(msg, hdr);
2093 err = genlmsg_unicast(msg, info->snd_pid);
2094 goto out;
2095
Johannes Berg3b858752009-03-12 09:55:09 +01002096 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002097 genlmsg_cancel(msg, hdr);
2098 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002099 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002100 /* Cleanup */
2101 cfg80211_put_dev(drv);
2102 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002103 out_rtnl:
2104 rtnl_unlock();
2105
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002106 return err;
2107}
2108
2109#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2110do {\
2111 if (table[attr_num]) {\
2112 cfg.param = nla_fn(table[attr_num]); \
2113 mask |= (1 << (attr_num - 1)); \
2114 } \
2115} while (0);\
2116
2117static struct nla_policy
2118nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2119 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2120 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2121 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2122 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2123 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2124 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2125 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2126
2127 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2128 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2129 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2130 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2131 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2132 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2133};
2134
2135static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2136{
2137 int err;
2138 u32 mask;
2139 struct cfg80211_registered_device *drv;
2140 struct net_device *dev;
2141 struct mesh_config cfg;
2142 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2143 struct nlattr *parent_attr;
2144
2145 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2146 if (!parent_attr)
2147 return -EINVAL;
2148 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2149 parent_attr, nl80211_meshconf_params_policy))
2150 return -EINVAL;
2151
Johannes Berg3b858752009-03-12 09:55:09 +01002152 rtnl_lock();
2153
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002154 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2155 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002156 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002157
Jouni Malinenf3f92582009-03-20 17:57:36 +02002158 if (!drv->ops->set_mesh_params) {
2159 err = -EOPNOTSUPP;
2160 goto out;
2161 }
2162
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002163 /* This makes sure that there aren't more than 32 mesh config
2164 * parameters (otherwise our bitfield scheme would not work.) */
2165 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2166
2167 /* Fill in the params struct */
2168 mask = 0;
2169 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2170 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2171 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2172 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2173 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2174 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2175 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2176 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2177 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2178 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2179 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2180 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2181 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2182 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2183 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2184 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2185 nla_get_u8);
2186 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2187 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2188 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2189 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2190 nla_get_u16);
2191 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2192 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2193 nla_get_u32);
2194 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2195 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2196 nla_get_u16);
2197 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2198 dot11MeshHWMPnetDiameterTraversalTime,
2199 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2200 nla_get_u16);
2201
2202 /* Apply changes */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002203 err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002204
Jouni Malinenf3f92582009-03-20 17:57:36 +02002205 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002206 /* cleanup */
2207 cfg80211_put_dev(drv);
2208 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002209 out_rtnl:
2210 rtnl_unlock();
2211
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002212 return err;
2213}
2214
2215#undef FILL_IN_MESH_PARAM_IF_SET
2216
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002217static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2218{
2219 struct sk_buff *msg;
2220 void *hdr = NULL;
2221 struct nlattr *nl_reg_rules;
2222 unsigned int i;
2223 int err = -EINVAL;
2224
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002225 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002226
2227 if (!cfg80211_regdomain)
2228 goto out;
2229
2230 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2231 if (!msg) {
2232 err = -ENOBUFS;
2233 goto out;
2234 }
2235
2236 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2237 NL80211_CMD_GET_REG);
2238 if (!hdr)
2239 goto nla_put_failure;
2240
2241 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2242 cfg80211_regdomain->alpha2);
2243
2244 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2245 if (!nl_reg_rules)
2246 goto nla_put_failure;
2247
2248 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2249 struct nlattr *nl_reg_rule;
2250 const struct ieee80211_reg_rule *reg_rule;
2251 const struct ieee80211_freq_range *freq_range;
2252 const struct ieee80211_power_rule *power_rule;
2253
2254 reg_rule = &cfg80211_regdomain->reg_rules[i];
2255 freq_range = &reg_rule->freq_range;
2256 power_rule = &reg_rule->power_rule;
2257
2258 nl_reg_rule = nla_nest_start(msg, i);
2259 if (!nl_reg_rule)
2260 goto nla_put_failure;
2261
2262 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2263 reg_rule->flags);
2264 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2265 freq_range->start_freq_khz);
2266 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2267 freq_range->end_freq_khz);
2268 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2269 freq_range->max_bandwidth_khz);
2270 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2271 power_rule->max_antenna_gain);
2272 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2273 power_rule->max_eirp);
2274
2275 nla_nest_end(msg, nl_reg_rule);
2276 }
2277
2278 nla_nest_end(msg, nl_reg_rules);
2279
2280 genlmsg_end(msg, hdr);
2281 err = genlmsg_unicast(msg, info->snd_pid);
2282 goto out;
2283
2284nla_put_failure:
2285 genlmsg_cancel(msg, hdr);
2286 err = -EMSGSIZE;
2287out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002288 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002289 return err;
2290}
2291
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002292static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2293{
2294 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2295 struct nlattr *nl_reg_rule;
2296 char *alpha2 = NULL;
2297 int rem_reg_rules = 0, r = 0;
2298 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2299 struct ieee80211_regdomain *rd = NULL;
2300
2301 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2302 return -EINVAL;
2303
2304 if (!info->attrs[NL80211_ATTR_REG_RULES])
2305 return -EINVAL;
2306
2307 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2308
2309 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2310 rem_reg_rules) {
2311 num_rules++;
2312 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2313 goto bad_reg;
2314 }
2315
2316 if (!reg_is_valid_request(alpha2))
2317 return -EINVAL;
2318
2319 size_of_regd = sizeof(struct ieee80211_regdomain) +
2320 (num_rules * sizeof(struct ieee80211_reg_rule));
2321
2322 rd = kzalloc(size_of_regd, GFP_KERNEL);
2323 if (!rd)
2324 return -ENOMEM;
2325
2326 rd->n_reg_rules = num_rules;
2327 rd->alpha2[0] = alpha2[0];
2328 rd->alpha2[1] = alpha2[1];
2329
2330 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2331 rem_reg_rules) {
2332 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2333 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2334 reg_rule_policy);
2335 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2336 if (r)
2337 goto bad_reg;
2338
2339 rule_idx++;
2340
2341 if (rule_idx > NL80211_MAX_SUPP_REG_RULES)
2342 goto bad_reg;
2343 }
2344
2345 BUG_ON(rule_idx != num_rules);
2346
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002347 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002348 r = set_regdom(rd);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002349 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002350 return r;
2351
Johannes Bergd2372b32008-10-24 20:32:20 +02002352 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002353 kfree(rd);
2354 return -EINVAL;
2355}
2356
Johannes Berg2a519312009-02-10 21:25:55 +01002357static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2358{
2359 struct cfg80211_registered_device *drv;
2360 struct net_device *dev;
2361 struct cfg80211_scan_request *request;
2362 struct cfg80211_ssid *ssid;
2363 struct ieee80211_channel *channel;
2364 struct nlattr *attr;
2365 struct wiphy *wiphy;
2366 int err, tmp, n_ssids = 0, n_channels = 0, i;
2367 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002368 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002369
Johannes Berg3b858752009-03-12 09:55:09 +01002370 rtnl_lock();
2371
Johannes Berg2a519312009-02-10 21:25:55 +01002372 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2373 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002374 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002375
2376 wiphy = &drv->wiphy;
2377
2378 if (!drv->ops->scan) {
2379 err = -EOPNOTSUPP;
2380 goto out;
2381 }
2382
Johannes Berg2a519312009-02-10 21:25:55 +01002383 if (drv->scan_req) {
2384 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002385 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002386 }
2387
2388 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2389 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp)
2390 n_channels++;
2391 if (!n_channels) {
2392 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002393 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002394 }
2395 } else {
2396 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2397 if (wiphy->bands[band])
2398 n_channels += wiphy->bands[band]->n_channels;
2399 }
2400
2401 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2402 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2403 n_ssids++;
2404
2405 if (n_ssids > wiphy->max_scan_ssids) {
2406 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002407 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002408 }
2409
Jouni Malinen70692ad2009-02-16 19:39:13 +02002410 if (info->attrs[NL80211_ATTR_IE])
2411 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2412 else
2413 ie_len = 0;
2414
Johannes Berg2a519312009-02-10 21:25:55 +01002415 request = kzalloc(sizeof(*request)
2416 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002417 + sizeof(channel) * n_channels
2418 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01002419 if (!request) {
2420 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01002421 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002422 }
2423
2424 request->channels = (void *)((char *)request + sizeof(*request));
2425 request->n_channels = n_channels;
2426 if (n_ssids)
2427 request->ssids = (void *)(request->channels + n_channels);
2428 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002429 if (ie_len) {
2430 if (request->ssids)
2431 request->ie = (void *)(request->ssids + n_ssids);
2432 else
2433 request->ie = (void *)(request->channels + n_channels);
2434 }
Johannes Berg2a519312009-02-10 21:25:55 +01002435
2436 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2437 /* user specified, bail out if channel not found */
2438 request->n_channels = n_channels;
2439 i = 0;
2440 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2441 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2442 if (!request->channels[i]) {
2443 err = -EINVAL;
2444 goto out_free;
2445 }
2446 i++;
2447 }
2448 } else {
2449 /* all channels */
2450 i = 0;
2451 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2452 int j;
2453 if (!wiphy->bands[band])
2454 continue;
2455 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2456 request->channels[i] = &wiphy->bands[band]->channels[j];
2457 i++;
2458 }
2459 }
2460 }
2461
2462 i = 0;
2463 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2464 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2465 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2466 err = -EINVAL;
2467 goto out_free;
2468 }
2469 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2470 request->ssids[i].ssid_len = nla_len(attr);
2471 i++;
2472 }
2473 }
2474
Jouni Malinen70692ad2009-02-16 19:39:13 +02002475 if (info->attrs[NL80211_ATTR_IE]) {
2476 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2477 memcpy(request->ie, nla_data(info->attrs[NL80211_ATTR_IE]),
2478 request->ie_len);
2479 }
2480
Johannes Berg2a519312009-02-10 21:25:55 +01002481 request->ifidx = dev->ifindex;
2482 request->wiphy = &drv->wiphy;
2483
2484 drv->scan_req = request;
2485 err = drv->ops->scan(&drv->wiphy, dev, request);
2486
2487 out_free:
2488 if (err) {
2489 drv->scan_req = NULL;
2490 kfree(request);
2491 }
Johannes Berg2a519312009-02-10 21:25:55 +01002492 out:
2493 cfg80211_put_dev(drv);
2494 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002495 out_rtnl:
2496 rtnl_unlock();
2497
Johannes Berg2a519312009-02-10 21:25:55 +01002498 return err;
2499}
2500
2501static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2502 struct cfg80211_registered_device *rdev,
2503 struct net_device *dev,
2504 struct cfg80211_bss *res)
2505{
2506 void *hdr;
2507 struct nlattr *bss;
2508
2509 hdr = nl80211hdr_put(msg, pid, seq, flags,
2510 NL80211_CMD_NEW_SCAN_RESULTS);
2511 if (!hdr)
2512 return -1;
2513
2514 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2515 rdev->bss_generation);
2516 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2517
2518 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2519 if (!bss)
2520 goto nla_put_failure;
2521 if (!is_zero_ether_addr(res->bssid))
2522 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2523 if (res->information_elements && res->len_information_elements)
2524 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2525 res->len_information_elements,
2526 res->information_elements);
2527 if (res->tsf)
2528 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2529 if (res->beacon_interval)
2530 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2531 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2532 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2533
Johannes Berg77965c92009-02-18 18:45:06 +01002534 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002535 case CFG80211_SIGNAL_TYPE_MBM:
2536 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2537 break;
2538 case CFG80211_SIGNAL_TYPE_UNSPEC:
2539 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2540 break;
2541 default:
2542 break;
2543 }
2544
2545 nla_nest_end(msg, bss);
2546
2547 return genlmsg_end(msg, hdr);
2548
2549 nla_put_failure:
2550 genlmsg_cancel(msg, hdr);
2551 return -EMSGSIZE;
2552}
2553
2554static int nl80211_dump_scan(struct sk_buff *skb,
2555 struct netlink_callback *cb)
2556{
2557 struct cfg80211_registered_device *dev;
2558 struct net_device *netdev;
2559 struct cfg80211_internal_bss *scan;
2560 int ifidx = cb->args[0];
2561 int start = cb->args[1], idx = 0;
2562 int err;
2563
2564 if (!ifidx) {
2565 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2566 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2567 nl80211_policy);
2568 if (err)
2569 return err;
2570
2571 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2572 return -EINVAL;
2573
2574 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2575 if (!ifidx)
2576 return -EINVAL;
2577 cb->args[0] = ifidx;
2578 }
2579
2580 netdev = dev_get_by_index(&init_net, ifidx);
2581 if (!netdev)
2582 return -ENODEV;
2583
2584 dev = cfg80211_get_dev_from_ifindex(ifidx);
2585 if (IS_ERR(dev)) {
2586 err = PTR_ERR(dev);
2587 goto out_put_netdev;
2588 }
2589
2590 spin_lock_bh(&dev->bss_lock);
2591 cfg80211_bss_expire(dev);
2592
2593 list_for_each_entry(scan, &dev->bss_list, list) {
2594 if (++idx <= start)
2595 continue;
2596 if (nl80211_send_bss(skb,
2597 NETLINK_CB(cb->skb).pid,
2598 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2599 dev, netdev, &scan->pub) < 0) {
2600 idx--;
2601 goto out;
2602 }
2603 }
2604
2605 out:
2606 spin_unlock_bh(&dev->bss_lock);
2607
2608 cb->args[1] = idx;
2609 err = skb->len;
2610 cfg80211_put_dev(dev);
2611 out_put_netdev:
2612 dev_put(netdev);
2613
2614 return err;
2615}
2616
Jouni Malinen255e7372009-03-20 21:21:17 +02002617static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
2618{
2619 return auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM ||
2620 auth_type == NL80211_AUTHTYPE_SHARED_KEY ||
2621 auth_type == NL80211_AUTHTYPE_FT ||
2622 auth_type == NL80211_AUTHTYPE_NETWORK_EAP;
2623}
2624
Jouni Malinen636a5d32009-03-19 13:39:22 +02002625static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
2626{
2627 struct cfg80211_registered_device *drv;
2628 struct net_device *dev;
2629 struct cfg80211_auth_request req;
2630 struct wiphy *wiphy;
2631 int err;
2632
2633 rtnl_lock();
2634
2635 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2636 if (err)
2637 goto unlock_rtnl;
2638
2639 if (!drv->ops->auth) {
2640 err = -EOPNOTSUPP;
2641 goto out;
2642 }
2643
2644 if (!info->attrs[NL80211_ATTR_MAC]) {
2645 err = -EINVAL;
2646 goto out;
2647 }
2648
2649 wiphy = &drv->wiphy;
2650 memset(&req, 0, sizeof(req));
2651
2652 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2653
2654 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2655 req.chan = ieee80211_get_channel(
2656 wiphy,
2657 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2658 if (!req.chan) {
2659 err = -EINVAL;
2660 goto out;
2661 }
2662 }
2663
2664 if (info->attrs[NL80211_ATTR_SSID]) {
2665 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2666 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
2667 }
2668
2669 if (info->attrs[NL80211_ATTR_IE]) {
2670 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2671 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2672 }
2673
2674 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2675 req.auth_type =
2676 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinen255e7372009-03-20 21:21:17 +02002677 if (!nl80211_valid_auth_type(req.auth_type)) {
2678 err = -EINVAL;
2679 goto out;
2680 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02002681 }
2682
2683 err = drv->ops->auth(&drv->wiphy, dev, &req);
2684
2685out:
2686 cfg80211_put_dev(drv);
2687 dev_put(dev);
2688unlock_rtnl:
2689 rtnl_unlock();
2690 return err;
2691}
2692
2693static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
2694{
2695 struct cfg80211_registered_device *drv;
2696 struct net_device *dev;
2697 struct cfg80211_assoc_request req;
2698 struct wiphy *wiphy;
2699 int err;
2700
2701 rtnl_lock();
2702
2703 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2704 if (err)
2705 goto unlock_rtnl;
2706
2707 if (!drv->ops->assoc) {
2708 err = -EOPNOTSUPP;
2709 goto out;
2710 }
2711
2712 if (!info->attrs[NL80211_ATTR_MAC] ||
2713 !info->attrs[NL80211_ATTR_SSID]) {
2714 err = -EINVAL;
2715 goto out;
2716 }
2717
2718 wiphy = &drv->wiphy;
2719 memset(&req, 0, sizeof(req));
2720
2721 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2722
2723 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2724 req.chan = ieee80211_get_channel(
2725 wiphy,
2726 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2727 if (!req.chan) {
2728 err = -EINVAL;
2729 goto out;
2730 }
2731 }
2732
Jouni Malinen636a5d32009-03-19 13:39:22 +02002733 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2734 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
2735
2736 if (info->attrs[NL80211_ATTR_IE]) {
2737 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2738 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2739 }
2740
2741 err = drv->ops->assoc(&drv->wiphy, dev, &req);
2742
2743out:
2744 cfg80211_put_dev(drv);
2745 dev_put(dev);
2746unlock_rtnl:
2747 rtnl_unlock();
2748 return err;
2749}
2750
2751static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
2752{
2753 struct cfg80211_registered_device *drv;
2754 struct net_device *dev;
2755 struct cfg80211_deauth_request req;
2756 struct wiphy *wiphy;
2757 int err;
2758
2759 rtnl_lock();
2760
2761 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2762 if (err)
2763 goto unlock_rtnl;
2764
2765 if (!drv->ops->deauth) {
2766 err = -EOPNOTSUPP;
2767 goto out;
2768 }
2769
2770 if (!info->attrs[NL80211_ATTR_MAC]) {
2771 err = -EINVAL;
2772 goto out;
2773 }
2774
2775 wiphy = &drv->wiphy;
2776 memset(&req, 0, sizeof(req));
2777
2778 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2779
Jouni Malinen255e7372009-03-20 21:21:17 +02002780 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02002781 req.reason_code =
2782 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
Jouni Malinen255e7372009-03-20 21:21:17 +02002783 if (req.reason_code == 0) {
2784 /* Reason Code 0 is reserved */
2785 err = -EINVAL;
2786 goto out;
2787 }
2788 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02002789
2790 if (info->attrs[NL80211_ATTR_IE]) {
2791 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2792 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2793 }
2794
2795 err = drv->ops->deauth(&drv->wiphy, dev, &req);
2796
2797out:
2798 cfg80211_put_dev(drv);
2799 dev_put(dev);
2800unlock_rtnl:
2801 rtnl_unlock();
2802 return err;
2803}
2804
2805static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
2806{
2807 struct cfg80211_registered_device *drv;
2808 struct net_device *dev;
2809 struct cfg80211_disassoc_request req;
2810 struct wiphy *wiphy;
2811 int err;
2812
2813 rtnl_lock();
2814
2815 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2816 if (err)
2817 goto unlock_rtnl;
2818
2819 if (!drv->ops->disassoc) {
2820 err = -EOPNOTSUPP;
2821 goto out;
2822 }
2823
2824 if (!info->attrs[NL80211_ATTR_MAC]) {
2825 err = -EINVAL;
2826 goto out;
2827 }
2828
2829 wiphy = &drv->wiphy;
2830 memset(&req, 0, sizeof(req));
2831
2832 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2833
Jouni Malinen255e7372009-03-20 21:21:17 +02002834 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02002835 req.reason_code =
2836 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
Jouni Malinen255e7372009-03-20 21:21:17 +02002837 if (req.reason_code == 0) {
2838 /* Reason Code 0 is reserved */
2839 err = -EINVAL;
2840 goto out;
2841 }
2842 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02002843
2844 if (info->attrs[NL80211_ATTR_IE]) {
2845 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2846 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2847 }
2848
2849 err = drv->ops->disassoc(&drv->wiphy, dev, &req);
2850
2851out:
2852 cfg80211_put_dev(drv);
2853 dev_put(dev);
2854unlock_rtnl:
2855 rtnl_unlock();
2856 return err;
2857}
2858
Johannes Berg55682962007-09-20 13:09:35 -04002859static struct genl_ops nl80211_ops[] = {
2860 {
2861 .cmd = NL80211_CMD_GET_WIPHY,
2862 .doit = nl80211_get_wiphy,
2863 .dumpit = nl80211_dump_wiphy,
2864 .policy = nl80211_policy,
2865 /* can be retrieved by unprivileged users */
2866 },
2867 {
2868 .cmd = NL80211_CMD_SET_WIPHY,
2869 .doit = nl80211_set_wiphy,
2870 .policy = nl80211_policy,
2871 .flags = GENL_ADMIN_PERM,
2872 },
2873 {
2874 .cmd = NL80211_CMD_GET_INTERFACE,
2875 .doit = nl80211_get_interface,
2876 .dumpit = nl80211_dump_interface,
2877 .policy = nl80211_policy,
2878 /* can be retrieved by unprivileged users */
2879 },
2880 {
2881 .cmd = NL80211_CMD_SET_INTERFACE,
2882 .doit = nl80211_set_interface,
2883 .policy = nl80211_policy,
2884 .flags = GENL_ADMIN_PERM,
2885 },
2886 {
2887 .cmd = NL80211_CMD_NEW_INTERFACE,
2888 .doit = nl80211_new_interface,
2889 .policy = nl80211_policy,
2890 .flags = GENL_ADMIN_PERM,
2891 },
2892 {
2893 .cmd = NL80211_CMD_DEL_INTERFACE,
2894 .doit = nl80211_del_interface,
2895 .policy = nl80211_policy,
2896 .flags = GENL_ADMIN_PERM,
2897 },
Johannes Berg41ade002007-12-19 02:03:29 +01002898 {
2899 .cmd = NL80211_CMD_GET_KEY,
2900 .doit = nl80211_get_key,
2901 .policy = nl80211_policy,
2902 .flags = GENL_ADMIN_PERM,
2903 },
2904 {
2905 .cmd = NL80211_CMD_SET_KEY,
2906 .doit = nl80211_set_key,
2907 .policy = nl80211_policy,
2908 .flags = GENL_ADMIN_PERM,
2909 },
2910 {
2911 .cmd = NL80211_CMD_NEW_KEY,
2912 .doit = nl80211_new_key,
2913 .policy = nl80211_policy,
2914 .flags = GENL_ADMIN_PERM,
2915 },
2916 {
2917 .cmd = NL80211_CMD_DEL_KEY,
2918 .doit = nl80211_del_key,
2919 .policy = nl80211_policy,
2920 .flags = GENL_ADMIN_PERM,
2921 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01002922 {
2923 .cmd = NL80211_CMD_SET_BEACON,
2924 .policy = nl80211_policy,
2925 .flags = GENL_ADMIN_PERM,
2926 .doit = nl80211_addset_beacon,
2927 },
2928 {
2929 .cmd = NL80211_CMD_NEW_BEACON,
2930 .policy = nl80211_policy,
2931 .flags = GENL_ADMIN_PERM,
2932 .doit = nl80211_addset_beacon,
2933 },
2934 {
2935 .cmd = NL80211_CMD_DEL_BEACON,
2936 .policy = nl80211_policy,
2937 .flags = GENL_ADMIN_PERM,
2938 .doit = nl80211_del_beacon,
2939 },
Johannes Berg5727ef12007-12-19 02:03:34 +01002940 {
2941 .cmd = NL80211_CMD_GET_STATION,
2942 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002943 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01002944 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01002945 },
2946 {
2947 .cmd = NL80211_CMD_SET_STATION,
2948 .doit = nl80211_set_station,
2949 .policy = nl80211_policy,
2950 .flags = GENL_ADMIN_PERM,
2951 },
2952 {
2953 .cmd = NL80211_CMD_NEW_STATION,
2954 .doit = nl80211_new_station,
2955 .policy = nl80211_policy,
2956 .flags = GENL_ADMIN_PERM,
2957 },
2958 {
2959 .cmd = NL80211_CMD_DEL_STATION,
2960 .doit = nl80211_del_station,
2961 .policy = nl80211_policy,
2962 .flags = GENL_ADMIN_PERM,
2963 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002964 {
2965 .cmd = NL80211_CMD_GET_MPATH,
2966 .doit = nl80211_get_mpath,
2967 .dumpit = nl80211_dump_mpath,
2968 .policy = nl80211_policy,
2969 .flags = GENL_ADMIN_PERM,
2970 },
2971 {
2972 .cmd = NL80211_CMD_SET_MPATH,
2973 .doit = nl80211_set_mpath,
2974 .policy = nl80211_policy,
2975 .flags = GENL_ADMIN_PERM,
2976 },
2977 {
2978 .cmd = NL80211_CMD_NEW_MPATH,
2979 .doit = nl80211_new_mpath,
2980 .policy = nl80211_policy,
2981 .flags = GENL_ADMIN_PERM,
2982 },
2983 {
2984 .cmd = NL80211_CMD_DEL_MPATH,
2985 .doit = nl80211_del_mpath,
2986 .policy = nl80211_policy,
2987 .flags = GENL_ADMIN_PERM,
2988 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002989 {
2990 .cmd = NL80211_CMD_SET_BSS,
2991 .doit = nl80211_set_bss,
2992 .policy = nl80211_policy,
2993 .flags = GENL_ADMIN_PERM,
2994 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002995 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002996 .cmd = NL80211_CMD_GET_REG,
2997 .doit = nl80211_get_reg,
2998 .policy = nl80211_policy,
2999 /* can be retrieved by unprivileged users */
3000 },
3001 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003002 .cmd = NL80211_CMD_SET_REG,
3003 .doit = nl80211_set_reg,
3004 .policy = nl80211_policy,
3005 .flags = GENL_ADMIN_PERM,
3006 },
3007 {
3008 .cmd = NL80211_CMD_REQ_SET_REG,
3009 .doit = nl80211_req_set_reg,
3010 .policy = nl80211_policy,
3011 .flags = GENL_ADMIN_PERM,
3012 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003013 {
3014 .cmd = NL80211_CMD_GET_MESH_PARAMS,
3015 .doit = nl80211_get_mesh_params,
3016 .policy = nl80211_policy,
3017 /* can be retrieved by unprivileged users */
3018 },
3019 {
3020 .cmd = NL80211_CMD_SET_MESH_PARAMS,
3021 .doit = nl80211_set_mesh_params,
3022 .policy = nl80211_policy,
3023 .flags = GENL_ADMIN_PERM,
3024 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02003025 {
Johannes Berg2a519312009-02-10 21:25:55 +01003026 .cmd = NL80211_CMD_TRIGGER_SCAN,
3027 .doit = nl80211_trigger_scan,
3028 .policy = nl80211_policy,
3029 .flags = GENL_ADMIN_PERM,
3030 },
3031 {
3032 .cmd = NL80211_CMD_GET_SCAN,
3033 .policy = nl80211_policy,
3034 .dumpit = nl80211_dump_scan,
3035 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02003036 {
3037 .cmd = NL80211_CMD_AUTHENTICATE,
3038 .doit = nl80211_authenticate,
3039 .policy = nl80211_policy,
3040 .flags = GENL_ADMIN_PERM,
3041 },
3042 {
3043 .cmd = NL80211_CMD_ASSOCIATE,
3044 .doit = nl80211_associate,
3045 .policy = nl80211_policy,
3046 .flags = GENL_ADMIN_PERM,
3047 },
3048 {
3049 .cmd = NL80211_CMD_DEAUTHENTICATE,
3050 .doit = nl80211_deauthenticate,
3051 .policy = nl80211_policy,
3052 .flags = GENL_ADMIN_PERM,
3053 },
3054 {
3055 .cmd = NL80211_CMD_DISASSOCIATE,
3056 .doit = nl80211_disassociate,
3057 .policy = nl80211_policy,
3058 .flags = GENL_ADMIN_PERM,
3059 },
Johannes Berg55682962007-09-20 13:09:35 -04003060};
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003061static struct genl_multicast_group nl80211_mlme_mcgrp = {
3062 .name = "mlme",
3063};
Johannes Berg55682962007-09-20 13:09:35 -04003064
3065/* multicast groups */
3066static struct genl_multicast_group nl80211_config_mcgrp = {
3067 .name = "config",
3068};
Johannes Berg2a519312009-02-10 21:25:55 +01003069static struct genl_multicast_group nl80211_scan_mcgrp = {
3070 .name = "scan",
3071};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003072static struct genl_multicast_group nl80211_regulatory_mcgrp = {
3073 .name = "regulatory",
3074};
Johannes Berg55682962007-09-20 13:09:35 -04003075
3076/* notification functions */
3077
3078void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
3079{
3080 struct sk_buff *msg;
3081
3082 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3083 if (!msg)
3084 return;
3085
3086 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
3087 nlmsg_free(msg);
3088 return;
3089 }
3090
3091 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
3092}
3093
Johannes Berg2a519312009-02-10 21:25:55 +01003094static int nl80211_send_scan_donemsg(struct sk_buff *msg,
3095 struct cfg80211_registered_device *rdev,
3096 struct net_device *netdev,
3097 u32 pid, u32 seq, int flags,
3098 u32 cmd)
3099{
3100 void *hdr;
3101
3102 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
3103 if (!hdr)
3104 return -1;
3105
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05003106 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01003107 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3108
3109 /* XXX: we should probably bounce back the request? */
3110
3111 return genlmsg_end(msg, hdr);
3112
3113 nla_put_failure:
3114 genlmsg_cancel(msg, hdr);
3115 return -EMSGSIZE;
3116}
3117
3118void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
3119 struct net_device *netdev)
3120{
3121 struct sk_buff *msg;
3122
3123 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3124 if (!msg)
3125 return;
3126
3127 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3128 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
3129 nlmsg_free(msg);
3130 return;
3131 }
3132
3133 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3134}
3135
3136void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
3137 struct net_device *netdev)
3138{
3139 struct sk_buff *msg;
3140
3141 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3142 if (!msg)
3143 return;
3144
3145 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3146 NL80211_CMD_SCAN_ABORTED) < 0) {
3147 nlmsg_free(msg);
3148 return;
3149 }
3150
3151 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3152}
3153
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003154/*
3155 * This can happen on global regulatory changes or device specific settings
3156 * based on custom world regulatory domains.
3157 */
3158void nl80211_send_reg_change_event(struct regulatory_request *request)
3159{
3160 struct sk_buff *msg;
3161 void *hdr;
3162
3163 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3164 if (!msg)
3165 return;
3166
3167 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
3168 if (!hdr) {
3169 nlmsg_free(msg);
3170 return;
3171 }
3172
3173 /* Userspace can always count this one always being set */
3174 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
3175
3176 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
3177 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3178 NL80211_REGDOM_TYPE_WORLD);
3179 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
3180 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3181 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
3182 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
3183 request->intersect)
3184 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3185 NL80211_REGDOM_TYPE_INTERSECTION);
3186 else {
3187 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3188 NL80211_REGDOM_TYPE_COUNTRY);
3189 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
3190 }
3191
3192 if (wiphy_idx_valid(request->wiphy_idx))
3193 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
3194
3195 if (genlmsg_end(msg, hdr) < 0) {
3196 nlmsg_free(msg);
3197 return;
3198 }
3199
3200 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL);
3201
3202 return;
3203
3204nla_put_failure:
3205 genlmsg_cancel(msg, hdr);
3206 nlmsg_free(msg);
3207}
3208
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003209static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
3210 struct net_device *netdev,
3211 const u8 *buf, size_t len,
3212 enum nl80211_commands cmd)
3213{
3214 struct sk_buff *msg;
3215 void *hdr;
3216
3217 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3218 if (!msg)
3219 return;
3220
3221 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
3222 if (!hdr) {
3223 nlmsg_free(msg);
3224 return;
3225 }
3226
3227 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3228 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3229 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
3230
3231 if (genlmsg_end(msg, hdr) < 0) {
3232 nlmsg_free(msg);
3233 return;
3234 }
3235
3236 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_KERNEL);
3237 return;
3238
3239 nla_put_failure:
3240 genlmsg_cancel(msg, hdr);
3241 nlmsg_free(msg);
3242}
3243
3244void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
3245 struct net_device *netdev, const u8 *buf, size_t len)
3246{
3247 nl80211_send_mlme_event(rdev, netdev, buf, len,
3248 NL80211_CMD_AUTHENTICATE);
3249}
3250
3251void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
3252 struct net_device *netdev, const u8 *buf,
3253 size_t len)
3254{
3255 nl80211_send_mlme_event(rdev, netdev, buf, len, NL80211_CMD_ASSOCIATE);
3256}
3257
3258void nl80211_send_rx_deauth(struct cfg80211_registered_device *rdev,
3259 struct net_device *netdev, const u8 *buf,
3260 size_t len)
3261{
3262 nl80211_send_mlme_event(rdev, netdev, buf, len,
3263 NL80211_CMD_DEAUTHENTICATE);
3264}
3265
3266void nl80211_send_rx_disassoc(struct cfg80211_registered_device *rdev,
3267 struct net_device *netdev, const u8 *buf,
3268 size_t len)
3269{
3270 nl80211_send_mlme_event(rdev, netdev, buf, len,
3271 NL80211_CMD_DISASSOCIATE);
3272}
3273
Johannes Berg55682962007-09-20 13:09:35 -04003274/* initialisation/exit functions */
3275
3276int nl80211_init(void)
3277{
3278 int err, i;
3279
3280 err = genl_register_family(&nl80211_fam);
3281 if (err)
3282 return err;
3283
3284 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
3285 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
3286 if (err)
3287 goto err_out;
3288 }
3289
3290 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
3291 if (err)
3292 goto err_out;
3293
Johannes Berg2a519312009-02-10 21:25:55 +01003294 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
3295 if (err)
3296 goto err_out;
3297
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003298 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
3299 if (err)
3300 goto err_out;
3301
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003302 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
3303 if (err)
3304 goto err_out;
3305
Johannes Berg55682962007-09-20 13:09:35 -04003306 return 0;
3307 err_out:
3308 genl_unregister_family(&nl80211_fam);
3309 return err;
3310}
3311
3312void nl80211_exit(void)
3313{
3314 genl_unregister_family(&nl80211_fam);
3315}