blob: 6bb73a3a3391f6e15b3a7baa6d8e30e459e63403 [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 Bergee688b02008-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 Bergee688b02008-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 Bergee688b02008-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 Bergee688b02008-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 Bergee688b02008-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 Bergee688b02008-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 Malinen3cfcf6a2009-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 Malinen3cfcf6a2009-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 Malinen3cfcf6a2009-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 Malinen3cfcf6a2009-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 Malinen3cfcf6a2009-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 Malinen3cfcf6a2009-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 Malinen3cfcf6a2009-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 Malinen3cfcf6a2009-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 Malinen3cfcf6a2009-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
Jouni Malinen35a8efe2009-03-20 21:21:18 +02001559 if (!netif_running(dev)) {
1560 err = -ENETDOWN;
1561 goto out;
1562 }
1563
Johannes Berg5727ef12007-12-19 02:03:34 +01001564 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001565
1566 out:
1567 if (params.vlan)
1568 dev_put(params.vlan);
1569 cfg80211_put_dev(drv);
1570 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001571 out_rtnl:
1572 rtnl_unlock();
1573
Johannes Berg5727ef12007-12-19 02:03:34 +01001574 return err;
1575}
1576
1577static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1578{
1579 struct cfg80211_registered_device *drv;
1580 int err;
1581 struct net_device *dev;
1582 u8 *mac_addr = NULL;
1583
1584 if (info->attrs[NL80211_ATTR_MAC])
1585 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1586
Johannes Berg3b858752009-03-12 09:55:09 +01001587 rtnl_lock();
1588
Johannes Bergbba95fe2008-07-29 13:22:51 +02001589 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001590 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001591 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001592
1593 if (!drv->ops->del_station) {
1594 err = -EOPNOTSUPP;
1595 goto out;
1596 }
1597
Johannes Berg5727ef12007-12-19 02:03:34 +01001598 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01001599
1600 out:
1601 cfg80211_put_dev(drv);
1602 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001603 out_rtnl:
1604 rtnl_unlock();
1605
Johannes Berg5727ef12007-12-19 02:03:34 +01001606 return err;
1607}
1608
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001609static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1610 int flags, struct net_device *dev,
1611 u8 *dst, u8 *next_hop,
1612 struct mpath_info *pinfo)
1613{
1614 void *hdr;
1615 struct nlattr *pinfoattr;
1616
1617 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1618 if (!hdr)
1619 return -1;
1620
1621 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1622 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1623 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1624
1625 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1626 if (!pinfoattr)
1627 goto nla_put_failure;
1628 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1629 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1630 pinfo->frame_qlen);
1631 if (pinfo->filled & MPATH_INFO_DSN)
1632 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1633 pinfo->dsn);
1634 if (pinfo->filled & MPATH_INFO_METRIC)
1635 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1636 pinfo->metric);
1637 if (pinfo->filled & MPATH_INFO_EXPTIME)
1638 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1639 pinfo->exptime);
1640 if (pinfo->filled & MPATH_INFO_FLAGS)
1641 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1642 pinfo->flags);
1643 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1644 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1645 pinfo->discovery_timeout);
1646 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1647 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1648 pinfo->discovery_retries);
1649
1650 nla_nest_end(msg, pinfoattr);
1651
1652 return genlmsg_end(msg, hdr);
1653
1654 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001655 genlmsg_cancel(msg, hdr);
1656 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001657}
1658
1659static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001660 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001661{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001662 struct mpath_info pinfo;
1663 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001664 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001665 u8 dst[ETH_ALEN];
1666 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001667 int ifidx = cb->args[0];
1668 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001669 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001670
Johannes Bergbba95fe2008-07-29 13:22:51 +02001671 if (!ifidx) {
1672 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1673 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1674 nl80211_policy);
1675 if (err)
1676 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001677
Johannes Bergbba95fe2008-07-29 13:22:51 +02001678 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1679 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001680
Johannes Bergbba95fe2008-07-29 13:22:51 +02001681 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1682 if (!ifidx)
1683 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001684 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001685
Johannes Berg3b858752009-03-12 09:55:09 +01001686 rtnl_lock();
1687
1688 netdev = __dev_get_by_index(&init_net, ifidx);
1689 if (!netdev) {
1690 err = -ENODEV;
1691 goto out_rtnl;
1692 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001693
Johannes Bergbba95fe2008-07-29 13:22:51 +02001694 dev = cfg80211_get_dev_from_ifindex(ifidx);
1695 if (IS_ERR(dev)) {
1696 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001697 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001698 }
1699
1700 if (!dev->ops->dump_mpath) {
1701 err = -ENOSYS;
1702 goto out_err;
1703 }
1704
Johannes Bergbba95fe2008-07-29 13:22:51 +02001705 while (1) {
1706 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1707 dst, next_hop, &pinfo);
1708 if (err == -ENOENT)
1709 break;
1710 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001711 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001712
1713 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1714 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1715 netdev, dst, next_hop,
1716 &pinfo) < 0)
1717 goto out;
1718
1719 path_idx++;
1720 }
1721
1722
1723 out:
1724 cb->args[1] = path_idx;
1725 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001726 out_err:
1727 cfg80211_put_dev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001728 out_rtnl:
1729 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001730
1731 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001732}
1733
1734static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1735{
1736 struct cfg80211_registered_device *drv;
1737 int err;
1738 struct net_device *dev;
1739 struct mpath_info pinfo;
1740 struct sk_buff *msg;
1741 u8 *dst = NULL;
1742 u8 next_hop[ETH_ALEN];
1743
1744 memset(&pinfo, 0, sizeof(pinfo));
1745
1746 if (!info->attrs[NL80211_ATTR_MAC])
1747 return -EINVAL;
1748
1749 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1750
Johannes Berg3b858752009-03-12 09:55:09 +01001751 rtnl_lock();
1752
Johannes Bergbba95fe2008-07-29 13:22:51 +02001753 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001754 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001755 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001756
1757 if (!drv->ops->get_mpath) {
1758 err = -EOPNOTSUPP;
1759 goto out;
1760 }
1761
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001762 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001763 if (err)
1764 goto out;
1765
1766 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1767 if (!msg)
1768 goto out;
1769
1770 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1771 dev, dst, next_hop, &pinfo) < 0)
1772 goto out_free;
1773
1774 err = genlmsg_unicast(msg, info->snd_pid);
1775 goto out;
1776
1777 out_free:
1778 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001779 out:
1780 cfg80211_put_dev(drv);
1781 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001782 out_rtnl:
1783 rtnl_unlock();
1784
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001785 return err;
1786}
1787
1788static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1789{
1790 struct cfg80211_registered_device *drv;
1791 int err;
1792 struct net_device *dev;
1793 u8 *dst = NULL;
1794 u8 *next_hop = NULL;
1795
1796 if (!info->attrs[NL80211_ATTR_MAC])
1797 return -EINVAL;
1798
1799 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1800 return -EINVAL;
1801
1802 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1803 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1804
Johannes Berg3b858752009-03-12 09:55:09 +01001805 rtnl_lock();
1806
Johannes Bergbba95fe2008-07-29 13:22:51 +02001807 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001808 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001809 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001810
1811 if (!drv->ops->change_mpath) {
1812 err = -EOPNOTSUPP;
1813 goto out;
1814 }
1815
Jouni Malinen35a8efe2009-03-20 21:21:18 +02001816 if (!netif_running(dev)) {
1817 err = -ENETDOWN;
1818 goto out;
1819 }
1820
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001821 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001822
1823 out:
1824 cfg80211_put_dev(drv);
1825 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001826 out_rtnl:
1827 rtnl_unlock();
1828
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001829 return err;
1830}
1831static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1832{
1833 struct cfg80211_registered_device *drv;
1834 int err;
1835 struct net_device *dev;
1836 u8 *dst = NULL;
1837 u8 *next_hop = NULL;
1838
1839 if (!info->attrs[NL80211_ATTR_MAC])
1840 return -EINVAL;
1841
1842 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1843 return -EINVAL;
1844
1845 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1846 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1847
Johannes Berg3b858752009-03-12 09:55:09 +01001848 rtnl_lock();
1849
Johannes Bergbba95fe2008-07-29 13:22:51 +02001850 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001851 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001852 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001853
1854 if (!drv->ops->add_mpath) {
1855 err = -EOPNOTSUPP;
1856 goto out;
1857 }
1858
Jouni Malinen35a8efe2009-03-20 21:21:18 +02001859 if (!netif_running(dev)) {
1860 err = -ENETDOWN;
1861 goto out;
1862 }
1863
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001864 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001865
1866 out:
1867 cfg80211_put_dev(drv);
1868 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001869 out_rtnl:
1870 rtnl_unlock();
1871
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001872 return err;
1873}
1874
1875static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1876{
1877 struct cfg80211_registered_device *drv;
1878 int err;
1879 struct net_device *dev;
1880 u8 *dst = NULL;
1881
1882 if (info->attrs[NL80211_ATTR_MAC])
1883 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1884
Johannes Berg3b858752009-03-12 09:55:09 +01001885 rtnl_lock();
1886
Johannes Bergbba95fe2008-07-29 13:22:51 +02001887 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001888 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001889 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001890
1891 if (!drv->ops->del_mpath) {
1892 err = -EOPNOTSUPP;
1893 goto out;
1894 }
1895
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001896 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001897
1898 out:
1899 cfg80211_put_dev(drv);
1900 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001901 out_rtnl:
1902 rtnl_unlock();
1903
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001904 return err;
1905}
1906
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001907static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
1908{
1909 struct cfg80211_registered_device *drv;
1910 int err;
1911 struct net_device *dev;
1912 struct bss_parameters params;
1913
1914 memset(&params, 0, sizeof(params));
1915 /* default to not changing parameters */
1916 params.use_cts_prot = -1;
1917 params.use_short_preamble = -1;
1918 params.use_short_slot_time = -1;
1919
1920 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
1921 params.use_cts_prot =
1922 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
1923 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
1924 params.use_short_preamble =
1925 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
1926 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
1927 params.use_short_slot_time =
1928 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02001929 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
1930 params.basic_rates =
1931 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1932 params.basic_rates_len =
1933 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1934 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001935
Johannes Berg3b858752009-03-12 09:55:09 +01001936 rtnl_lock();
1937
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001938 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1939 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001940 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001941
1942 if (!drv->ops->change_bss) {
1943 err = -EOPNOTSUPP;
1944 goto out;
1945 }
1946
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001947 err = drv->ops->change_bss(&drv->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001948
1949 out:
1950 cfg80211_put_dev(drv);
1951 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001952 out_rtnl:
1953 rtnl_unlock();
1954
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001955 return err;
1956}
1957
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001958static const struct nla_policy
1959 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
1960 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1961 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1962 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1963 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1964 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1965 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1966};
1967
1968static int parse_reg_rule(struct nlattr *tb[],
1969 struct ieee80211_reg_rule *reg_rule)
1970{
1971 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
1972 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
1973
1974 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
1975 return -EINVAL;
1976 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
1977 return -EINVAL;
1978 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
1979 return -EINVAL;
1980 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1981 return -EINVAL;
1982 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1983 return -EINVAL;
1984
1985 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
1986
1987 freq_range->start_freq_khz =
1988 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
1989 freq_range->end_freq_khz =
1990 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
1991 freq_range->max_bandwidth_khz =
1992 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
1993
1994 power_rule->max_eirp =
1995 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
1996
1997 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
1998 power_rule->max_antenna_gain =
1999 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2000
2001 return 0;
2002}
2003
2004static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2005{
2006 int r;
2007 char *data = NULL;
2008
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002009 /*
2010 * You should only get this when cfg80211 hasn't yet initialized
2011 * completely when built-in to the kernel right between the time
2012 * window between nl80211_init() and regulatory_init(), if that is
2013 * even possible.
2014 */
2015 mutex_lock(&cfg80211_mutex);
2016 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002017 mutex_unlock(&cfg80211_mutex);
2018 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002019 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002020 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002021
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002022 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2023 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002024
2025 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2026
2027#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2028 /* We ignore world regdom requests with the old regdom setup */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002029 if (is_world_regdom(data))
2030 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002031#endif
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002032
2033 r = regulatory_hint_user(data);
2034
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002035 return r;
2036}
2037
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002038static int nl80211_get_mesh_params(struct sk_buff *skb,
2039 struct genl_info *info)
2040{
2041 struct cfg80211_registered_device *drv;
2042 struct mesh_config cur_params;
2043 int err;
2044 struct net_device *dev;
2045 void *hdr;
2046 struct nlattr *pinfoattr;
2047 struct sk_buff *msg;
2048
Johannes Berg3b858752009-03-12 09:55:09 +01002049 rtnl_lock();
2050
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002051 /* Look up our device */
2052 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2053 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002054 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002055
Jouni Malinenf3f92582009-03-20 17:57:36 +02002056 if (!drv->ops->get_mesh_params) {
2057 err = -EOPNOTSUPP;
2058 goto out;
2059 }
2060
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002061 /* Get the mesh params */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002062 err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002063 if (err)
2064 goto out;
2065
2066 /* Draw up a netlink message to send back */
2067 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2068 if (!msg) {
2069 err = -ENOBUFS;
2070 goto out;
2071 }
2072 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2073 NL80211_CMD_GET_MESH_PARAMS);
2074 if (!hdr)
2075 goto nla_put_failure;
2076 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2077 if (!pinfoattr)
2078 goto nla_put_failure;
2079 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2080 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2081 cur_params.dot11MeshRetryTimeout);
2082 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2083 cur_params.dot11MeshConfirmTimeout);
2084 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2085 cur_params.dot11MeshHoldingTimeout);
2086 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2087 cur_params.dot11MeshMaxPeerLinks);
2088 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2089 cur_params.dot11MeshMaxRetries);
2090 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2091 cur_params.dot11MeshTTL);
2092 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2093 cur_params.auto_open_plinks);
2094 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2095 cur_params.dot11MeshHWMPmaxPREQretries);
2096 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2097 cur_params.path_refresh_time);
2098 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2099 cur_params.min_discovery_timeout);
2100 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2101 cur_params.dot11MeshHWMPactivePathTimeout);
2102 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2103 cur_params.dot11MeshHWMPpreqMinInterval);
2104 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2105 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2106 nla_nest_end(msg, pinfoattr);
2107 genlmsg_end(msg, hdr);
2108 err = genlmsg_unicast(msg, info->snd_pid);
2109 goto out;
2110
Johannes Berg3b858752009-03-12 09:55:09 +01002111 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002112 genlmsg_cancel(msg, hdr);
2113 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002114 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002115 /* Cleanup */
2116 cfg80211_put_dev(drv);
2117 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002118 out_rtnl:
2119 rtnl_unlock();
2120
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002121 return err;
2122}
2123
2124#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2125do {\
2126 if (table[attr_num]) {\
2127 cfg.param = nla_fn(table[attr_num]); \
2128 mask |= (1 << (attr_num - 1)); \
2129 } \
2130} while (0);\
2131
2132static struct nla_policy
2133nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2134 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2135 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2136 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2137 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2138 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2139 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2140 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2141
2142 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2143 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2144 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2145 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2146 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2147 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2148};
2149
2150static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2151{
2152 int err;
2153 u32 mask;
2154 struct cfg80211_registered_device *drv;
2155 struct net_device *dev;
2156 struct mesh_config cfg;
2157 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2158 struct nlattr *parent_attr;
2159
2160 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2161 if (!parent_attr)
2162 return -EINVAL;
2163 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2164 parent_attr, nl80211_meshconf_params_policy))
2165 return -EINVAL;
2166
Johannes Berg3b858752009-03-12 09:55:09 +01002167 rtnl_lock();
2168
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002169 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2170 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002171 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002172
Jouni Malinenf3f92582009-03-20 17:57:36 +02002173 if (!drv->ops->set_mesh_params) {
2174 err = -EOPNOTSUPP;
2175 goto out;
2176 }
2177
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002178 /* This makes sure that there aren't more than 32 mesh config
2179 * parameters (otherwise our bitfield scheme would not work.) */
2180 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2181
2182 /* Fill in the params struct */
2183 mask = 0;
2184 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2185 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2186 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2187 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2188 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2189 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2190 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2191 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2192 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2193 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2194 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2195 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2196 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2197 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2198 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2199 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2200 nla_get_u8);
2201 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2202 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2203 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2204 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2205 nla_get_u16);
2206 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2207 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2208 nla_get_u32);
2209 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2210 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2211 nla_get_u16);
2212 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2213 dot11MeshHWMPnetDiameterTraversalTime,
2214 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2215 nla_get_u16);
2216
2217 /* Apply changes */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002218 err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002219
Jouni Malinenf3f92582009-03-20 17:57:36 +02002220 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002221 /* cleanup */
2222 cfg80211_put_dev(drv);
2223 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002224 out_rtnl:
2225 rtnl_unlock();
2226
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002227 return err;
2228}
2229
2230#undef FILL_IN_MESH_PARAM_IF_SET
2231
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002232static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2233{
2234 struct sk_buff *msg;
2235 void *hdr = NULL;
2236 struct nlattr *nl_reg_rules;
2237 unsigned int i;
2238 int err = -EINVAL;
2239
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002240 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002241
2242 if (!cfg80211_regdomain)
2243 goto out;
2244
2245 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2246 if (!msg) {
2247 err = -ENOBUFS;
2248 goto out;
2249 }
2250
2251 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2252 NL80211_CMD_GET_REG);
2253 if (!hdr)
2254 goto nla_put_failure;
2255
2256 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2257 cfg80211_regdomain->alpha2);
2258
2259 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2260 if (!nl_reg_rules)
2261 goto nla_put_failure;
2262
2263 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2264 struct nlattr *nl_reg_rule;
2265 const struct ieee80211_reg_rule *reg_rule;
2266 const struct ieee80211_freq_range *freq_range;
2267 const struct ieee80211_power_rule *power_rule;
2268
2269 reg_rule = &cfg80211_regdomain->reg_rules[i];
2270 freq_range = &reg_rule->freq_range;
2271 power_rule = &reg_rule->power_rule;
2272
2273 nl_reg_rule = nla_nest_start(msg, i);
2274 if (!nl_reg_rule)
2275 goto nla_put_failure;
2276
2277 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2278 reg_rule->flags);
2279 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2280 freq_range->start_freq_khz);
2281 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2282 freq_range->end_freq_khz);
2283 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2284 freq_range->max_bandwidth_khz);
2285 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2286 power_rule->max_antenna_gain);
2287 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2288 power_rule->max_eirp);
2289
2290 nla_nest_end(msg, nl_reg_rule);
2291 }
2292
2293 nla_nest_end(msg, nl_reg_rules);
2294
2295 genlmsg_end(msg, hdr);
2296 err = genlmsg_unicast(msg, info->snd_pid);
2297 goto out;
2298
2299nla_put_failure:
2300 genlmsg_cancel(msg, hdr);
2301 err = -EMSGSIZE;
2302out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002303 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002304 return err;
2305}
2306
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002307static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2308{
2309 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2310 struct nlattr *nl_reg_rule;
2311 char *alpha2 = NULL;
2312 int rem_reg_rules = 0, r = 0;
2313 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2314 struct ieee80211_regdomain *rd = NULL;
2315
2316 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2317 return -EINVAL;
2318
2319 if (!info->attrs[NL80211_ATTR_REG_RULES])
2320 return -EINVAL;
2321
2322 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2323
2324 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2325 rem_reg_rules) {
2326 num_rules++;
2327 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2328 goto bad_reg;
2329 }
2330
2331 if (!reg_is_valid_request(alpha2))
2332 return -EINVAL;
2333
2334 size_of_regd = sizeof(struct ieee80211_regdomain) +
2335 (num_rules * sizeof(struct ieee80211_reg_rule));
2336
2337 rd = kzalloc(size_of_regd, GFP_KERNEL);
2338 if (!rd)
2339 return -ENOMEM;
2340
2341 rd->n_reg_rules = num_rules;
2342 rd->alpha2[0] = alpha2[0];
2343 rd->alpha2[1] = alpha2[1];
2344
2345 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2346 rem_reg_rules) {
2347 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2348 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2349 reg_rule_policy);
2350 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2351 if (r)
2352 goto bad_reg;
2353
2354 rule_idx++;
2355
2356 if (rule_idx > NL80211_MAX_SUPP_REG_RULES)
2357 goto bad_reg;
2358 }
2359
2360 BUG_ON(rule_idx != num_rules);
2361
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002362 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002363 r = set_regdom(rd);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002364 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002365 return r;
2366
Johannes Bergd2372b32008-10-24 20:32:20 +02002367 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002368 kfree(rd);
2369 return -EINVAL;
2370}
2371
Johannes Berg2a519312009-02-10 21:25:55 +01002372static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2373{
2374 struct cfg80211_registered_device *drv;
2375 struct net_device *dev;
2376 struct cfg80211_scan_request *request;
2377 struct cfg80211_ssid *ssid;
2378 struct ieee80211_channel *channel;
2379 struct nlattr *attr;
2380 struct wiphy *wiphy;
2381 int err, tmp, n_ssids = 0, n_channels = 0, i;
2382 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002383 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002384
Johannes Berg3b858752009-03-12 09:55:09 +01002385 rtnl_lock();
2386
Johannes Berg2a519312009-02-10 21:25:55 +01002387 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2388 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002389 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002390
2391 wiphy = &drv->wiphy;
2392
2393 if (!drv->ops->scan) {
2394 err = -EOPNOTSUPP;
2395 goto out;
2396 }
2397
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002398 if (!netif_running(dev)) {
2399 err = -ENETDOWN;
2400 goto out;
2401 }
2402
Johannes Berg2a519312009-02-10 21:25:55 +01002403 if (drv->scan_req) {
2404 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002405 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002406 }
2407
2408 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2409 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp)
2410 n_channels++;
2411 if (!n_channels) {
2412 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002413 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002414 }
2415 } else {
2416 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2417 if (wiphy->bands[band])
2418 n_channels += wiphy->bands[band]->n_channels;
2419 }
2420
2421 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2422 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2423 n_ssids++;
2424
2425 if (n_ssids > wiphy->max_scan_ssids) {
2426 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002427 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002428 }
2429
Jouni Malinen70692ad2009-02-16 19:39:13 +02002430 if (info->attrs[NL80211_ATTR_IE])
2431 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2432 else
2433 ie_len = 0;
2434
Johannes Berg2a519312009-02-10 21:25:55 +01002435 request = kzalloc(sizeof(*request)
2436 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002437 + sizeof(channel) * n_channels
2438 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01002439 if (!request) {
2440 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01002441 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002442 }
2443
2444 request->channels = (void *)((char *)request + sizeof(*request));
2445 request->n_channels = n_channels;
2446 if (n_ssids)
2447 request->ssids = (void *)(request->channels + n_channels);
2448 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002449 if (ie_len) {
2450 if (request->ssids)
2451 request->ie = (void *)(request->ssids + n_ssids);
2452 else
2453 request->ie = (void *)(request->channels + n_channels);
2454 }
Johannes Berg2a519312009-02-10 21:25:55 +01002455
2456 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2457 /* user specified, bail out if channel not found */
2458 request->n_channels = n_channels;
2459 i = 0;
2460 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2461 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2462 if (!request->channels[i]) {
2463 err = -EINVAL;
2464 goto out_free;
2465 }
2466 i++;
2467 }
2468 } else {
2469 /* all channels */
2470 i = 0;
2471 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2472 int j;
2473 if (!wiphy->bands[band])
2474 continue;
2475 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2476 request->channels[i] = &wiphy->bands[band]->channels[j];
2477 i++;
2478 }
2479 }
2480 }
2481
2482 i = 0;
2483 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2484 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2485 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2486 err = -EINVAL;
2487 goto out_free;
2488 }
2489 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2490 request->ssids[i].ssid_len = nla_len(attr);
2491 i++;
2492 }
2493 }
2494
Jouni Malinen70692ad2009-02-16 19:39:13 +02002495 if (info->attrs[NL80211_ATTR_IE]) {
2496 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2497 memcpy(request->ie, nla_data(info->attrs[NL80211_ATTR_IE]),
2498 request->ie_len);
2499 }
2500
Johannes Berg2a519312009-02-10 21:25:55 +01002501 request->ifidx = dev->ifindex;
2502 request->wiphy = &drv->wiphy;
2503
2504 drv->scan_req = request;
2505 err = drv->ops->scan(&drv->wiphy, dev, request);
2506
2507 out_free:
2508 if (err) {
2509 drv->scan_req = NULL;
2510 kfree(request);
2511 }
Johannes Berg2a519312009-02-10 21:25:55 +01002512 out:
2513 cfg80211_put_dev(drv);
2514 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002515 out_rtnl:
2516 rtnl_unlock();
2517
Johannes Berg2a519312009-02-10 21:25:55 +01002518 return err;
2519}
2520
2521static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2522 struct cfg80211_registered_device *rdev,
2523 struct net_device *dev,
2524 struct cfg80211_bss *res)
2525{
2526 void *hdr;
2527 struct nlattr *bss;
2528
2529 hdr = nl80211hdr_put(msg, pid, seq, flags,
2530 NL80211_CMD_NEW_SCAN_RESULTS);
2531 if (!hdr)
2532 return -1;
2533
2534 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2535 rdev->bss_generation);
2536 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2537
2538 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2539 if (!bss)
2540 goto nla_put_failure;
2541 if (!is_zero_ether_addr(res->bssid))
2542 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2543 if (res->information_elements && res->len_information_elements)
2544 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2545 res->len_information_elements,
2546 res->information_elements);
2547 if (res->tsf)
2548 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2549 if (res->beacon_interval)
2550 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2551 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2552 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2553
Johannes Berg77965c92009-02-18 18:45:06 +01002554 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002555 case CFG80211_SIGNAL_TYPE_MBM:
2556 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2557 break;
2558 case CFG80211_SIGNAL_TYPE_UNSPEC:
2559 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2560 break;
2561 default:
2562 break;
2563 }
2564
2565 nla_nest_end(msg, bss);
2566
2567 return genlmsg_end(msg, hdr);
2568
2569 nla_put_failure:
2570 genlmsg_cancel(msg, hdr);
2571 return -EMSGSIZE;
2572}
2573
2574static int nl80211_dump_scan(struct sk_buff *skb,
2575 struct netlink_callback *cb)
2576{
2577 struct cfg80211_registered_device *dev;
2578 struct net_device *netdev;
2579 struct cfg80211_internal_bss *scan;
2580 int ifidx = cb->args[0];
2581 int start = cb->args[1], idx = 0;
2582 int err;
2583
2584 if (!ifidx) {
2585 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2586 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2587 nl80211_policy);
2588 if (err)
2589 return err;
2590
2591 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2592 return -EINVAL;
2593
2594 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2595 if (!ifidx)
2596 return -EINVAL;
2597 cb->args[0] = ifidx;
2598 }
2599
2600 netdev = dev_get_by_index(&init_net, ifidx);
2601 if (!netdev)
2602 return -ENODEV;
2603
2604 dev = cfg80211_get_dev_from_ifindex(ifidx);
2605 if (IS_ERR(dev)) {
2606 err = PTR_ERR(dev);
2607 goto out_put_netdev;
2608 }
2609
2610 spin_lock_bh(&dev->bss_lock);
2611 cfg80211_bss_expire(dev);
2612
2613 list_for_each_entry(scan, &dev->bss_list, list) {
2614 if (++idx <= start)
2615 continue;
2616 if (nl80211_send_bss(skb,
2617 NETLINK_CB(cb->skb).pid,
2618 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2619 dev, netdev, &scan->pub) < 0) {
2620 idx--;
2621 goto out;
2622 }
2623 }
2624
2625 out:
2626 spin_unlock_bh(&dev->bss_lock);
2627
2628 cb->args[1] = idx;
2629 err = skb->len;
2630 cfg80211_put_dev(dev);
2631 out_put_netdev:
2632 dev_put(netdev);
2633
2634 return err;
2635}
2636
Jouni Malinen255e7372009-03-20 21:21:17 +02002637static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
2638{
2639 return auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM ||
2640 auth_type == NL80211_AUTHTYPE_SHARED_KEY ||
2641 auth_type == NL80211_AUTHTYPE_FT ||
2642 auth_type == NL80211_AUTHTYPE_NETWORK_EAP;
2643}
2644
Jouni Malinen636a5d32009-03-19 13:39:22 +02002645static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
2646{
2647 struct cfg80211_registered_device *drv;
2648 struct net_device *dev;
2649 struct cfg80211_auth_request req;
2650 struct wiphy *wiphy;
2651 int err;
2652
2653 rtnl_lock();
2654
2655 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2656 if (err)
2657 goto unlock_rtnl;
2658
2659 if (!drv->ops->auth) {
2660 err = -EOPNOTSUPP;
2661 goto out;
2662 }
2663
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002664 if (!netif_running(dev)) {
2665 err = -ENETDOWN;
2666 goto out;
2667 }
2668
Jouni Malinen636a5d32009-03-19 13:39:22 +02002669 if (!info->attrs[NL80211_ATTR_MAC]) {
2670 err = -EINVAL;
2671 goto out;
2672 }
2673
2674 wiphy = &drv->wiphy;
2675 memset(&req, 0, sizeof(req));
2676
2677 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2678
2679 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2680 req.chan = ieee80211_get_channel(
2681 wiphy,
2682 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2683 if (!req.chan) {
2684 err = -EINVAL;
2685 goto out;
2686 }
2687 }
2688
2689 if (info->attrs[NL80211_ATTR_SSID]) {
2690 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2691 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
2692 }
2693
2694 if (info->attrs[NL80211_ATTR_IE]) {
2695 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2696 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2697 }
2698
2699 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2700 req.auth_type =
2701 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinen255e7372009-03-20 21:21:17 +02002702 if (!nl80211_valid_auth_type(req.auth_type)) {
2703 err = -EINVAL;
2704 goto out;
2705 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02002706 }
2707
2708 err = drv->ops->auth(&drv->wiphy, dev, &req);
2709
2710out:
2711 cfg80211_put_dev(drv);
2712 dev_put(dev);
2713unlock_rtnl:
2714 rtnl_unlock();
2715 return err;
2716}
2717
2718static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
2719{
2720 struct cfg80211_registered_device *drv;
2721 struct net_device *dev;
2722 struct cfg80211_assoc_request req;
2723 struct wiphy *wiphy;
2724 int err;
2725
2726 rtnl_lock();
2727
2728 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2729 if (err)
2730 goto unlock_rtnl;
2731
2732 if (!drv->ops->assoc) {
2733 err = -EOPNOTSUPP;
2734 goto out;
2735 }
2736
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002737 if (!netif_running(dev)) {
2738 err = -ENETDOWN;
2739 goto out;
2740 }
2741
Jouni Malinen636a5d32009-03-19 13:39:22 +02002742 if (!info->attrs[NL80211_ATTR_MAC] ||
2743 !info->attrs[NL80211_ATTR_SSID]) {
2744 err = -EINVAL;
2745 goto out;
2746 }
2747
2748 wiphy = &drv->wiphy;
2749 memset(&req, 0, sizeof(req));
2750
2751 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2752
2753 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2754 req.chan = ieee80211_get_channel(
2755 wiphy,
2756 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2757 if (!req.chan) {
2758 err = -EINVAL;
2759 goto out;
2760 }
2761 }
2762
Jouni Malinen636a5d32009-03-19 13:39:22 +02002763 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2764 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
2765
2766 if (info->attrs[NL80211_ATTR_IE]) {
2767 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2768 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2769 }
2770
2771 err = drv->ops->assoc(&drv->wiphy, dev, &req);
2772
2773out:
2774 cfg80211_put_dev(drv);
2775 dev_put(dev);
2776unlock_rtnl:
2777 rtnl_unlock();
2778 return err;
2779}
2780
2781static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
2782{
2783 struct cfg80211_registered_device *drv;
2784 struct net_device *dev;
2785 struct cfg80211_deauth_request req;
2786 struct wiphy *wiphy;
2787 int err;
2788
2789 rtnl_lock();
2790
2791 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2792 if (err)
2793 goto unlock_rtnl;
2794
2795 if (!drv->ops->deauth) {
2796 err = -EOPNOTSUPP;
2797 goto out;
2798 }
2799
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002800 if (!netif_running(dev)) {
2801 err = -ENETDOWN;
2802 goto out;
2803 }
2804
Jouni Malinen636a5d32009-03-19 13:39:22 +02002805 if (!info->attrs[NL80211_ATTR_MAC]) {
2806 err = -EINVAL;
2807 goto out;
2808 }
2809
2810 wiphy = &drv->wiphy;
2811 memset(&req, 0, sizeof(req));
2812
2813 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2814
Jouni Malinen255e7372009-03-20 21:21:17 +02002815 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02002816 req.reason_code =
2817 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
Jouni Malinen255e7372009-03-20 21:21:17 +02002818 if (req.reason_code == 0) {
2819 /* Reason Code 0 is reserved */
2820 err = -EINVAL;
2821 goto out;
2822 }
2823 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02002824
2825 if (info->attrs[NL80211_ATTR_IE]) {
2826 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2827 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2828 }
2829
2830 err = drv->ops->deauth(&drv->wiphy, dev, &req);
2831
2832out:
2833 cfg80211_put_dev(drv);
2834 dev_put(dev);
2835unlock_rtnl:
2836 rtnl_unlock();
2837 return err;
2838}
2839
2840static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
2841{
2842 struct cfg80211_registered_device *drv;
2843 struct net_device *dev;
2844 struct cfg80211_disassoc_request req;
2845 struct wiphy *wiphy;
2846 int err;
2847
2848 rtnl_lock();
2849
2850 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2851 if (err)
2852 goto unlock_rtnl;
2853
2854 if (!drv->ops->disassoc) {
2855 err = -EOPNOTSUPP;
2856 goto out;
2857 }
2858
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002859 if (!netif_running(dev)) {
2860 err = -ENETDOWN;
2861 goto out;
2862 }
2863
Jouni Malinen636a5d32009-03-19 13:39:22 +02002864 if (!info->attrs[NL80211_ATTR_MAC]) {
2865 err = -EINVAL;
2866 goto out;
2867 }
2868
2869 wiphy = &drv->wiphy;
2870 memset(&req, 0, sizeof(req));
2871
2872 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2873
Jouni Malinen255e7372009-03-20 21:21:17 +02002874 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02002875 req.reason_code =
2876 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
Jouni Malinen255e7372009-03-20 21:21:17 +02002877 if (req.reason_code == 0) {
2878 /* Reason Code 0 is reserved */
2879 err = -EINVAL;
2880 goto out;
2881 }
2882 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02002883
2884 if (info->attrs[NL80211_ATTR_IE]) {
2885 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2886 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2887 }
2888
2889 err = drv->ops->disassoc(&drv->wiphy, dev, &req);
2890
2891out:
2892 cfg80211_put_dev(drv);
2893 dev_put(dev);
2894unlock_rtnl:
2895 rtnl_unlock();
2896 return err;
2897}
2898
Johannes Berg55682962007-09-20 13:09:35 -04002899static struct genl_ops nl80211_ops[] = {
2900 {
2901 .cmd = NL80211_CMD_GET_WIPHY,
2902 .doit = nl80211_get_wiphy,
2903 .dumpit = nl80211_dump_wiphy,
2904 .policy = nl80211_policy,
2905 /* can be retrieved by unprivileged users */
2906 },
2907 {
2908 .cmd = NL80211_CMD_SET_WIPHY,
2909 .doit = nl80211_set_wiphy,
2910 .policy = nl80211_policy,
2911 .flags = GENL_ADMIN_PERM,
2912 },
2913 {
2914 .cmd = NL80211_CMD_GET_INTERFACE,
2915 .doit = nl80211_get_interface,
2916 .dumpit = nl80211_dump_interface,
2917 .policy = nl80211_policy,
2918 /* can be retrieved by unprivileged users */
2919 },
2920 {
2921 .cmd = NL80211_CMD_SET_INTERFACE,
2922 .doit = nl80211_set_interface,
2923 .policy = nl80211_policy,
2924 .flags = GENL_ADMIN_PERM,
2925 },
2926 {
2927 .cmd = NL80211_CMD_NEW_INTERFACE,
2928 .doit = nl80211_new_interface,
2929 .policy = nl80211_policy,
2930 .flags = GENL_ADMIN_PERM,
2931 },
2932 {
2933 .cmd = NL80211_CMD_DEL_INTERFACE,
2934 .doit = nl80211_del_interface,
2935 .policy = nl80211_policy,
2936 .flags = GENL_ADMIN_PERM,
2937 },
Johannes Berg41ade002007-12-19 02:03:29 +01002938 {
2939 .cmd = NL80211_CMD_GET_KEY,
2940 .doit = nl80211_get_key,
2941 .policy = nl80211_policy,
2942 .flags = GENL_ADMIN_PERM,
2943 },
2944 {
2945 .cmd = NL80211_CMD_SET_KEY,
2946 .doit = nl80211_set_key,
2947 .policy = nl80211_policy,
2948 .flags = GENL_ADMIN_PERM,
2949 },
2950 {
2951 .cmd = NL80211_CMD_NEW_KEY,
2952 .doit = nl80211_new_key,
2953 .policy = nl80211_policy,
2954 .flags = GENL_ADMIN_PERM,
2955 },
2956 {
2957 .cmd = NL80211_CMD_DEL_KEY,
2958 .doit = nl80211_del_key,
2959 .policy = nl80211_policy,
2960 .flags = GENL_ADMIN_PERM,
2961 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01002962 {
2963 .cmd = NL80211_CMD_SET_BEACON,
2964 .policy = nl80211_policy,
2965 .flags = GENL_ADMIN_PERM,
2966 .doit = nl80211_addset_beacon,
2967 },
2968 {
2969 .cmd = NL80211_CMD_NEW_BEACON,
2970 .policy = nl80211_policy,
2971 .flags = GENL_ADMIN_PERM,
2972 .doit = nl80211_addset_beacon,
2973 },
2974 {
2975 .cmd = NL80211_CMD_DEL_BEACON,
2976 .policy = nl80211_policy,
2977 .flags = GENL_ADMIN_PERM,
2978 .doit = nl80211_del_beacon,
2979 },
Johannes Berg5727ef12007-12-19 02:03:34 +01002980 {
2981 .cmd = NL80211_CMD_GET_STATION,
2982 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002983 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01002984 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01002985 },
2986 {
2987 .cmd = NL80211_CMD_SET_STATION,
2988 .doit = nl80211_set_station,
2989 .policy = nl80211_policy,
2990 .flags = GENL_ADMIN_PERM,
2991 },
2992 {
2993 .cmd = NL80211_CMD_NEW_STATION,
2994 .doit = nl80211_new_station,
2995 .policy = nl80211_policy,
2996 .flags = GENL_ADMIN_PERM,
2997 },
2998 {
2999 .cmd = NL80211_CMD_DEL_STATION,
3000 .doit = nl80211_del_station,
3001 .policy = nl80211_policy,
3002 .flags = GENL_ADMIN_PERM,
3003 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003004 {
3005 .cmd = NL80211_CMD_GET_MPATH,
3006 .doit = nl80211_get_mpath,
3007 .dumpit = nl80211_dump_mpath,
3008 .policy = nl80211_policy,
3009 .flags = GENL_ADMIN_PERM,
3010 },
3011 {
3012 .cmd = NL80211_CMD_SET_MPATH,
3013 .doit = nl80211_set_mpath,
3014 .policy = nl80211_policy,
3015 .flags = GENL_ADMIN_PERM,
3016 },
3017 {
3018 .cmd = NL80211_CMD_NEW_MPATH,
3019 .doit = nl80211_new_mpath,
3020 .policy = nl80211_policy,
3021 .flags = GENL_ADMIN_PERM,
3022 },
3023 {
3024 .cmd = NL80211_CMD_DEL_MPATH,
3025 .doit = nl80211_del_mpath,
3026 .policy = nl80211_policy,
3027 .flags = GENL_ADMIN_PERM,
3028 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003029 {
3030 .cmd = NL80211_CMD_SET_BSS,
3031 .doit = nl80211_set_bss,
3032 .policy = nl80211_policy,
3033 .flags = GENL_ADMIN_PERM,
3034 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003035 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003036 .cmd = NL80211_CMD_GET_REG,
3037 .doit = nl80211_get_reg,
3038 .policy = nl80211_policy,
3039 /* can be retrieved by unprivileged users */
3040 },
3041 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003042 .cmd = NL80211_CMD_SET_REG,
3043 .doit = nl80211_set_reg,
3044 .policy = nl80211_policy,
3045 .flags = GENL_ADMIN_PERM,
3046 },
3047 {
3048 .cmd = NL80211_CMD_REQ_SET_REG,
3049 .doit = nl80211_req_set_reg,
3050 .policy = nl80211_policy,
3051 .flags = GENL_ADMIN_PERM,
3052 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003053 {
3054 .cmd = NL80211_CMD_GET_MESH_PARAMS,
3055 .doit = nl80211_get_mesh_params,
3056 .policy = nl80211_policy,
3057 /* can be retrieved by unprivileged users */
3058 },
3059 {
3060 .cmd = NL80211_CMD_SET_MESH_PARAMS,
3061 .doit = nl80211_set_mesh_params,
3062 .policy = nl80211_policy,
3063 .flags = GENL_ADMIN_PERM,
3064 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02003065 {
Johannes Berg2a519312009-02-10 21:25:55 +01003066 .cmd = NL80211_CMD_TRIGGER_SCAN,
3067 .doit = nl80211_trigger_scan,
3068 .policy = nl80211_policy,
3069 .flags = GENL_ADMIN_PERM,
3070 },
3071 {
3072 .cmd = NL80211_CMD_GET_SCAN,
3073 .policy = nl80211_policy,
3074 .dumpit = nl80211_dump_scan,
3075 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02003076 {
3077 .cmd = NL80211_CMD_AUTHENTICATE,
3078 .doit = nl80211_authenticate,
3079 .policy = nl80211_policy,
3080 .flags = GENL_ADMIN_PERM,
3081 },
3082 {
3083 .cmd = NL80211_CMD_ASSOCIATE,
3084 .doit = nl80211_associate,
3085 .policy = nl80211_policy,
3086 .flags = GENL_ADMIN_PERM,
3087 },
3088 {
3089 .cmd = NL80211_CMD_DEAUTHENTICATE,
3090 .doit = nl80211_deauthenticate,
3091 .policy = nl80211_policy,
3092 .flags = GENL_ADMIN_PERM,
3093 },
3094 {
3095 .cmd = NL80211_CMD_DISASSOCIATE,
3096 .doit = nl80211_disassociate,
3097 .policy = nl80211_policy,
3098 .flags = GENL_ADMIN_PERM,
3099 },
Johannes Berg55682962007-09-20 13:09:35 -04003100};
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003101static struct genl_multicast_group nl80211_mlme_mcgrp = {
3102 .name = "mlme",
3103};
Johannes Berg55682962007-09-20 13:09:35 -04003104
3105/* multicast groups */
3106static struct genl_multicast_group nl80211_config_mcgrp = {
3107 .name = "config",
3108};
Johannes Berg2a519312009-02-10 21:25:55 +01003109static struct genl_multicast_group nl80211_scan_mcgrp = {
3110 .name = "scan",
3111};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003112static struct genl_multicast_group nl80211_regulatory_mcgrp = {
3113 .name = "regulatory",
3114};
Johannes Berg55682962007-09-20 13:09:35 -04003115
3116/* notification functions */
3117
3118void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
3119{
3120 struct sk_buff *msg;
3121
3122 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3123 if (!msg)
3124 return;
3125
3126 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
3127 nlmsg_free(msg);
3128 return;
3129 }
3130
3131 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
3132}
3133
Johannes Berg2a519312009-02-10 21:25:55 +01003134static int nl80211_send_scan_donemsg(struct sk_buff *msg,
3135 struct cfg80211_registered_device *rdev,
3136 struct net_device *netdev,
3137 u32 pid, u32 seq, int flags,
3138 u32 cmd)
3139{
3140 void *hdr;
3141
3142 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
3143 if (!hdr)
3144 return -1;
3145
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05003146 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01003147 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3148
3149 /* XXX: we should probably bounce back the request? */
3150
3151 return genlmsg_end(msg, hdr);
3152
3153 nla_put_failure:
3154 genlmsg_cancel(msg, hdr);
3155 return -EMSGSIZE;
3156}
3157
3158void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
3159 struct net_device *netdev)
3160{
3161 struct sk_buff *msg;
3162
3163 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3164 if (!msg)
3165 return;
3166
3167 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3168 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
3169 nlmsg_free(msg);
3170 return;
3171 }
3172
3173 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3174}
3175
3176void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
3177 struct net_device *netdev)
3178{
3179 struct sk_buff *msg;
3180
3181 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3182 if (!msg)
3183 return;
3184
3185 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3186 NL80211_CMD_SCAN_ABORTED) < 0) {
3187 nlmsg_free(msg);
3188 return;
3189 }
3190
3191 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3192}
3193
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003194/*
3195 * This can happen on global regulatory changes or device specific settings
3196 * based on custom world regulatory domains.
3197 */
3198void nl80211_send_reg_change_event(struct regulatory_request *request)
3199{
3200 struct sk_buff *msg;
3201 void *hdr;
3202
3203 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3204 if (!msg)
3205 return;
3206
3207 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
3208 if (!hdr) {
3209 nlmsg_free(msg);
3210 return;
3211 }
3212
3213 /* Userspace can always count this one always being set */
3214 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
3215
3216 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
3217 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3218 NL80211_REGDOM_TYPE_WORLD);
3219 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
3220 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3221 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
3222 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
3223 request->intersect)
3224 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3225 NL80211_REGDOM_TYPE_INTERSECTION);
3226 else {
3227 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3228 NL80211_REGDOM_TYPE_COUNTRY);
3229 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
3230 }
3231
3232 if (wiphy_idx_valid(request->wiphy_idx))
3233 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
3234
3235 if (genlmsg_end(msg, hdr) < 0) {
3236 nlmsg_free(msg);
3237 return;
3238 }
3239
3240 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL);
3241
3242 return;
3243
3244nla_put_failure:
3245 genlmsg_cancel(msg, hdr);
3246 nlmsg_free(msg);
3247}
3248
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003249static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
3250 struct net_device *netdev,
3251 const u8 *buf, size_t len,
3252 enum nl80211_commands cmd)
3253{
3254 struct sk_buff *msg;
3255 void *hdr;
3256
3257 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3258 if (!msg)
3259 return;
3260
3261 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
3262 if (!hdr) {
3263 nlmsg_free(msg);
3264 return;
3265 }
3266
3267 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3268 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3269 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
3270
3271 if (genlmsg_end(msg, hdr) < 0) {
3272 nlmsg_free(msg);
3273 return;
3274 }
3275
3276 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_KERNEL);
3277 return;
3278
3279 nla_put_failure:
3280 genlmsg_cancel(msg, hdr);
3281 nlmsg_free(msg);
3282}
3283
3284void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
3285 struct net_device *netdev, const u8 *buf, size_t len)
3286{
3287 nl80211_send_mlme_event(rdev, netdev, buf, len,
3288 NL80211_CMD_AUTHENTICATE);
3289}
3290
3291void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
3292 struct net_device *netdev, const u8 *buf,
3293 size_t len)
3294{
3295 nl80211_send_mlme_event(rdev, netdev, buf, len, NL80211_CMD_ASSOCIATE);
3296}
3297
3298void nl80211_send_rx_deauth(struct cfg80211_registered_device *rdev,
3299 struct net_device *netdev, const u8 *buf,
3300 size_t len)
3301{
3302 nl80211_send_mlme_event(rdev, netdev, buf, len,
3303 NL80211_CMD_DEAUTHENTICATE);
3304}
3305
3306void nl80211_send_rx_disassoc(struct cfg80211_registered_device *rdev,
3307 struct net_device *netdev, const u8 *buf,
3308 size_t len)
3309{
3310 nl80211_send_mlme_event(rdev, netdev, buf, len,
3311 NL80211_CMD_DISASSOCIATE);
3312}
3313
Johannes Berg55682962007-09-20 13:09:35 -04003314/* initialisation/exit functions */
3315
3316int nl80211_init(void)
3317{
3318 int err, i;
3319
3320 err = genl_register_family(&nl80211_fam);
3321 if (err)
3322 return err;
3323
3324 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
3325 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
3326 if (err)
3327 goto err_out;
3328 }
3329
3330 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
3331 if (err)
3332 goto err_out;
3333
Johannes Berg2a519312009-02-10 21:25:55 +01003334 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
3335 if (err)
3336 goto err_out;
3337
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003338 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
3339 if (err)
3340 goto err_out;
3341
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003342 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
3343 if (err)
3344 goto err_out;
3345
Johannes Berg55682962007-09-20 13:09:35 -04003346 return 0;
3347 err_out:
3348 genl_unregister_family(&nl80211_fam);
3349 return err;
3350}
3351
3352void nl80211_exit(void)
3353{
3354 genl_unregister_family(&nl80211_fam);
3355}