blob: a95ab9e4c19e40f233afd0cc59f62fcee8cf159e [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Johannes Berg08645122009-05-11 13:54:58 +02004 * Copyright 2006-2009 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
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 Berg463d0182009-07-14 00:33:35 +020017#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040018#include <net/genetlink.h>
19#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020020#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040021#include "core.h"
22#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070023#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040024
25/* the netlink family */
26static struct genl_family nl80211_fam = {
27 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
28 .name = "nl80211", /* have users key off the name instead */
29 .hdrsize = 0, /* no private header */
30 .version = 1, /* no particular meaning now */
31 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020032 .netnsok = true,
Johannes Berg55682962007-09-20 13:09:35 -040033};
34
Johannes Berg79c97e92009-07-07 03:56:12 +020035/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020036static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020037 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040038 struct net_device **dev)
39{
Johannes Berg463d0182009-07-14 00:33:35 +020040 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040041 int ifindex;
42
Johannes Bergbba95fe2008-07-29 13:22:51 +020043 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040044 return -EINVAL;
45
Johannes Bergbba95fe2008-07-29 13:22:51 +020046 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020047 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040048 if (!*dev)
49 return -ENODEV;
50
Johannes Berg463d0182009-07-14 00:33:35 +020051 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020052 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040053 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020054 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040055 }
56
57 return 0;
58}
59
60/* policy for the attributes */
61static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
62 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
63 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070064 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020065 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020066 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053067 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020068 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
69 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
70 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010072 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040073
74 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
75 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
76 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010077
78 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020079 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010080
Johannes Bergb9454e82009-07-08 13:29:08 +020081 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010082 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
83 .len = WLAN_MAX_KEY_LEN },
84 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
85 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
86 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030087 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010088
89 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
90 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
91 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
92 .len = IEEE80211_MAX_DATA_LEN },
93 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
94 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010095 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
96 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
97 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
98 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
99 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100100 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100101 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200102 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100103 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
104 .len = IEEE80211_MAX_MESH_ID_LEN },
105 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300106
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700107 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
108 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
109
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300110 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
111 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
112 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200113 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
114 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300115
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700116 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
117
Jouni Malinen36aedc92008-08-25 11:58:58 +0300118 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
119 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200120
121 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
122 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
123 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100124 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
125 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200126
127 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
128 .len = IEEE80211_MAX_SSID_LEN },
129 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
130 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200131 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300132 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300133 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300134 [NL80211_ATTR_STA_FLAGS2] = {
135 .len = sizeof(struct nl80211_sta_flag_update),
136 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300137 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200138 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
139 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
140 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200141 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100142 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100143 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
144 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100145 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
146 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200147 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Johannes Berg55682962007-09-20 13:09:35 -0400148};
149
Johannes Bergb9454e82009-07-08 13:29:08 +0200150/* policy for the attributes */
151static struct nla_policy
152nl80211_key_policy[NL80211_KEY_MAX + 1] __read_mostly = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200153 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200154 [NL80211_KEY_IDX] = { .type = NLA_U8 },
155 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
156 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
157 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
158 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
159};
160
Holger Schuriga0438972009-11-11 11:30:02 +0100161/* ifidx get helper */
162static int nl80211_get_ifidx(struct netlink_callback *cb)
163{
164 int res;
165
166 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
167 nl80211_fam.attrbuf, nl80211_fam.maxattr,
168 nl80211_policy);
169 if (res)
170 return res;
171
172 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
173 return -EINVAL;
174
175 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
176 if (!res)
177 return -EINVAL;
178 return res;
179}
180
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100181/* IE validation */
182static bool is_valid_ie_attr(const struct nlattr *attr)
183{
184 const u8 *pos;
185 int len;
186
187 if (!attr)
188 return true;
189
190 pos = nla_data(attr);
191 len = nla_len(attr);
192
193 while (len) {
194 u8 elemlen;
195
196 if (len < 2)
197 return false;
198 len -= 2;
199
200 elemlen = pos[1];
201 if (elemlen > len)
202 return false;
203
204 len -= elemlen;
205 pos += 2 + elemlen;
206 }
207
208 return true;
209}
210
Johannes Berg55682962007-09-20 13:09:35 -0400211/* message building helper */
212static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
213 int flags, u8 cmd)
214{
215 /* since there is no private header just add the generic one */
216 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
217}
218
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400219static int nl80211_msg_put_channel(struct sk_buff *msg,
220 struct ieee80211_channel *chan)
221{
222 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
223 chan->center_freq);
224
225 if (chan->flags & IEEE80211_CHAN_DISABLED)
226 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
227 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
228 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
229 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
230 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
231 if (chan->flags & IEEE80211_CHAN_RADAR)
232 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
233
234 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
235 DBM_TO_MBM(chan->max_power));
236
237 return 0;
238
239 nla_put_failure:
240 return -ENOBUFS;
241}
242
Johannes Berg55682962007-09-20 13:09:35 -0400243/* netlink command implementations */
244
Johannes Bergb9454e82009-07-08 13:29:08 +0200245struct key_parse {
246 struct key_params p;
247 int idx;
248 bool def, defmgmt;
249};
250
251static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
252{
253 struct nlattr *tb[NL80211_KEY_MAX + 1];
254 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
255 nl80211_key_policy);
256 if (err)
257 return err;
258
259 k->def = !!tb[NL80211_KEY_DEFAULT];
260 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
261
262 if (tb[NL80211_KEY_IDX])
263 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
264
265 if (tb[NL80211_KEY_DATA]) {
266 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
267 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
268 }
269
270 if (tb[NL80211_KEY_SEQ]) {
271 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
272 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
273 }
274
275 if (tb[NL80211_KEY_CIPHER])
276 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
277
278 return 0;
279}
280
281static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
282{
283 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
284 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
285 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
286 }
287
288 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
289 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
290 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
291 }
292
293 if (info->attrs[NL80211_ATTR_KEY_IDX])
294 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
295
296 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
297 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
298
299 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
300 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
301
302 return 0;
303}
304
305static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
306{
307 int err;
308
309 memset(k, 0, sizeof(*k));
310 k->idx = -1;
311
312 if (info->attrs[NL80211_ATTR_KEY])
313 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
314 else
315 err = nl80211_parse_key_old(info, k);
316
317 if (err)
318 return err;
319
320 if (k->def && k->defmgmt)
321 return -EINVAL;
322
323 if (k->idx != -1) {
324 if (k->defmgmt) {
325 if (k->idx < 4 || k->idx > 5)
326 return -EINVAL;
327 } else if (k->def) {
328 if (k->idx < 0 || k->idx > 3)
329 return -EINVAL;
330 } else {
331 if (k->idx < 0 || k->idx > 5)
332 return -EINVAL;
333 }
334 }
335
336 return 0;
337}
338
Johannes Bergfffd0932009-07-08 14:22:54 +0200339static struct cfg80211_cached_keys *
340nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
341 struct nlattr *keys)
342{
343 struct key_parse parse;
344 struct nlattr *key;
345 struct cfg80211_cached_keys *result;
346 int rem, err, def = 0;
347
348 result = kzalloc(sizeof(*result), GFP_KERNEL);
349 if (!result)
350 return ERR_PTR(-ENOMEM);
351
352 result->def = -1;
353 result->defmgmt = -1;
354
355 nla_for_each_nested(key, keys, rem) {
356 memset(&parse, 0, sizeof(parse));
357 parse.idx = -1;
358
359 err = nl80211_parse_key_new(key, &parse);
360 if (err)
361 goto error;
362 err = -EINVAL;
363 if (!parse.p.key)
364 goto error;
365 if (parse.idx < 0 || parse.idx > 4)
366 goto error;
367 if (parse.def) {
368 if (def)
369 goto error;
370 def = 1;
371 result->def = parse.idx;
372 } else if (parse.defmgmt)
373 goto error;
374 err = cfg80211_validate_key_settings(rdev, &parse.p,
375 parse.idx, NULL);
376 if (err)
377 goto error;
378 result->params[parse.idx].cipher = parse.p.cipher;
379 result->params[parse.idx].key_len = parse.p.key_len;
380 result->params[parse.idx].key = result->data[parse.idx];
381 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
382 }
383
384 return result;
385 error:
386 kfree(result);
387 return ERR_PTR(err);
388}
389
390static int nl80211_key_allowed(struct wireless_dev *wdev)
391{
392 ASSERT_WDEV_LOCK(wdev);
393
394 if (!netif_running(wdev->netdev))
395 return -ENETDOWN;
396
397 switch (wdev->iftype) {
398 case NL80211_IFTYPE_AP:
399 case NL80211_IFTYPE_AP_VLAN:
400 break;
401 case NL80211_IFTYPE_ADHOC:
402 if (!wdev->current_bss)
403 return -ENOLINK;
404 break;
405 case NL80211_IFTYPE_STATION:
406 if (wdev->sme_state != CFG80211_SME_CONNECTED)
407 return -ENOLINK;
408 break;
409 default:
410 return -EINVAL;
411 }
412
413 return 0;
414}
415
Johannes Berg55682962007-09-20 13:09:35 -0400416static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
417 struct cfg80211_registered_device *dev)
418{
419 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100420 struct nlattr *nl_bands, *nl_band;
421 struct nlattr *nl_freqs, *nl_freq;
422 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700423 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100424 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100425 enum ieee80211_band band;
426 struct ieee80211_channel *chan;
427 struct ieee80211_rate *rate;
428 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700429 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400430
431 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
432 if (!hdr)
433 return -1;
434
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500435 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400436 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200437
Johannes Bergf5ea9122009-08-07 16:17:38 +0200438 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
439 cfg80211_rdev_list_generation);
440
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200441 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
442 dev->wiphy.retry_short);
443 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
444 dev->wiphy.retry_long);
445 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
446 dev->wiphy.frag_threshold);
447 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
448 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100449 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
450 dev->wiphy.coverage_class);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200451
Johannes Berg2a519312009-02-10 21:25:55 +0100452 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
453 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200454 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
455 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100456
Johannes Berg25e47c182009-04-02 20:14:06 +0200457 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
458 sizeof(u32) * dev->wiphy.n_cipher_suites,
459 dev->wiphy.cipher_suites);
460
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100461 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
462 dev->wiphy.max_num_pmkids);
463
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700464 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
465 if (!nl_modes)
466 goto nla_put_failure;
467
468 i = 0;
469 while (ifmodes) {
470 if (ifmodes & 1)
471 NLA_PUT_FLAG(msg, i);
472 ifmodes >>= 1;
473 i++;
474 }
475
476 nla_nest_end(msg, nl_modes);
477
Johannes Bergee688b002008-01-24 19:38:39 +0100478 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
479 if (!nl_bands)
480 goto nla_put_failure;
481
482 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
483 if (!dev->wiphy.bands[band])
484 continue;
485
486 nl_band = nla_nest_start(msg, band);
487 if (!nl_band)
488 goto nla_put_failure;
489
Johannes Bergd51626d2008-10-09 12:20:13 +0200490 /* add HT info */
491 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
492 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
493 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
494 &dev->wiphy.bands[band]->ht_cap.mcs);
495 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
496 dev->wiphy.bands[band]->ht_cap.cap);
497 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
498 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
499 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
500 dev->wiphy.bands[band]->ht_cap.ampdu_density);
501 }
502
Johannes Bergee688b002008-01-24 19:38:39 +0100503 /* add frequencies */
504 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
505 if (!nl_freqs)
506 goto nla_put_failure;
507
508 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
509 nl_freq = nla_nest_start(msg, i);
510 if (!nl_freq)
511 goto nla_put_failure;
512
513 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100514
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400515 if (nl80211_msg_put_channel(msg, chan))
516 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200517
Johannes Bergee688b002008-01-24 19:38:39 +0100518 nla_nest_end(msg, nl_freq);
519 }
520
521 nla_nest_end(msg, nl_freqs);
522
523 /* add bitrates */
524 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
525 if (!nl_rates)
526 goto nla_put_failure;
527
528 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
529 nl_rate = nla_nest_start(msg, i);
530 if (!nl_rate)
531 goto nla_put_failure;
532
533 rate = &dev->wiphy.bands[band]->bitrates[i];
534 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
535 rate->bitrate);
536 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
537 NLA_PUT_FLAG(msg,
538 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
539
540 nla_nest_end(msg, nl_rate);
541 }
542
543 nla_nest_end(msg, nl_rates);
544
545 nla_nest_end(msg, nl_band);
546 }
547 nla_nest_end(msg, nl_bands);
548
Johannes Berg8fdc6212009-03-14 09:34:01 +0100549 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
550 if (!nl_cmds)
551 goto nla_put_failure;
552
553 i = 0;
554#define CMD(op, n) \
555 do { \
556 if (dev->ops->op) { \
557 i++; \
558 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
559 } \
560 } while (0)
561
562 CMD(add_virtual_intf, NEW_INTERFACE);
563 CMD(change_virtual_intf, SET_INTERFACE);
564 CMD(add_key, NEW_KEY);
565 CMD(add_beacon, NEW_BEACON);
566 CMD(add_station, NEW_STATION);
567 CMD(add_mpath, NEW_MPATH);
568 CMD(set_mesh_params, SET_MESH_PARAMS);
569 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200570 CMD(auth, AUTHENTICATE);
571 CMD(assoc, ASSOCIATE);
572 CMD(deauth, DEAUTHENTICATE);
573 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200574 CMD(join_ibss, JOIN_IBSS);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100575 CMD(set_pmksa, SET_PMKSA);
576 CMD(del_pmksa, DEL_PMKSA);
577 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100578 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200579 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg5be83de2009-11-19 00:56:28 +0100580 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200581 i++;
582 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
583 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100584
585#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200586
Johannes Berg6829c872009-07-02 09:13:27 +0200587 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200588 i++;
589 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
590 }
591
Johannes Berg6829c872009-07-02 09:13:27 +0200592 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200593 i++;
594 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
595 }
596
Johannes Berg8fdc6212009-03-14 09:34:01 +0100597 nla_nest_end(msg, nl_cmds);
598
Johannes Berg55682962007-09-20 13:09:35 -0400599 return genlmsg_end(msg, hdr);
600
601 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700602 genlmsg_cancel(msg, hdr);
603 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400604}
605
606static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
607{
608 int idx = 0;
609 int start = cb->args[0];
610 struct cfg80211_registered_device *dev;
611
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500612 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200613 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200614 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
615 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200616 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400617 continue;
618 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
619 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200620 dev) < 0) {
621 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400622 break;
Julius Volzb4637272008-07-08 14:02:19 +0200623 }
Johannes Berg55682962007-09-20 13:09:35 -0400624 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500625 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400626
627 cb->args[0] = idx;
628
629 return skb->len;
630}
631
632static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
633{
634 struct sk_buff *msg;
635 struct cfg80211_registered_device *dev;
636
637 dev = cfg80211_get_dev_from_info(info);
638 if (IS_ERR(dev))
639 return PTR_ERR(dev);
640
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700641 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400642 if (!msg)
643 goto out_err;
644
645 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
646 goto out_free;
647
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200648 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400649
Johannes Berg134e6372009-07-10 09:51:34 +0000650 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400651
652 out_free:
653 nlmsg_free(msg);
654 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200655 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400656 return -ENOBUFS;
657}
658
Jouni Malinen31888482008-10-30 16:59:24 +0200659static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
660 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
661 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
662 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
663 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
664 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
665};
666
667static int parse_txq_params(struct nlattr *tb[],
668 struct ieee80211_txq_params *txq_params)
669{
670 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
671 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
672 !tb[NL80211_TXQ_ATTR_AIFS])
673 return -EINVAL;
674
675 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
676 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
677 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
678 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
679 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
680
681 return 0;
682}
683
Johannes Berg55682962007-09-20 13:09:35 -0400684static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
685{
686 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200687 int result = 0, rem_txq_params = 0;
688 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200689 u32 changed;
690 u8 retry_short = 0, retry_long = 0;
691 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100692 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400693
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100694 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400695
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100696 mutex_lock(&cfg80211_mutex);
697
Johannes Berg79c97e92009-07-07 03:56:12 +0200698 rdev = __cfg80211_rdev_from_info(info);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100699 if (IS_ERR(rdev)) {
Jiri Slaby1f5fc702009-06-20 12:31:11 +0200700 mutex_unlock(&cfg80211_mutex);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100701 result = PTR_ERR(rdev);
702 goto unlock;
703 }
704
705 mutex_lock(&rdev->mtx);
706
707 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200708 result = cfg80211_dev_rename(
709 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100710
711 mutex_unlock(&cfg80211_mutex);
712
713 if (result)
714 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400715
Jouni Malinen31888482008-10-30 16:59:24 +0200716 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
717 struct ieee80211_txq_params txq_params;
718 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
719
720 if (!rdev->ops->set_txq_params) {
721 result = -EOPNOTSUPP;
722 goto bad_res;
723 }
724
725 nla_for_each_nested(nl_txq_params,
726 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
727 rem_txq_params) {
728 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
729 nla_data(nl_txq_params),
730 nla_len(nl_txq_params),
731 txq_params_policy);
732 result = parse_txq_params(tb, &txq_params);
733 if (result)
734 goto bad_res;
735
736 result = rdev->ops->set_txq_params(&rdev->wiphy,
737 &txq_params);
738 if (result)
739 goto bad_res;
740 }
741 }
742
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200743 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530744 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400745 u32 freq;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200746
Johannes Berg306d6112008-12-08 12:39:04 +0100747 result = -EINVAL;
748
Sujith094d05d2008-12-12 11:57:43 +0530749 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
750 channel_type = nla_get_u32(info->attrs[
751 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
752 if (channel_type != NL80211_CHAN_NO_HT &&
753 channel_type != NL80211_CHAN_HT20 &&
754 channel_type != NL80211_CHAN_HT40PLUS &&
755 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200756 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200757 }
758
759 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
Johannes Berg306d6112008-12-08 12:39:04 +0100760
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200761 mutex_lock(&rdev->devlist_mtx);
Johannes Berg4b181142009-08-08 11:03:58 +0200762 result = rdev_set_freq(rdev, NULL, freq, channel_type);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200763 mutex_unlock(&rdev->devlist_mtx);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200764 if (result)
765 goto bad_res;
766 }
767
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200768 changed = 0;
769
770 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
771 retry_short = nla_get_u8(
772 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
773 if (retry_short == 0) {
774 result = -EINVAL;
775 goto bad_res;
776 }
777 changed |= WIPHY_PARAM_RETRY_SHORT;
778 }
779
780 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
781 retry_long = nla_get_u8(
782 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
783 if (retry_long == 0) {
784 result = -EINVAL;
785 goto bad_res;
786 }
787 changed |= WIPHY_PARAM_RETRY_LONG;
788 }
789
790 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
791 frag_threshold = nla_get_u32(
792 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
793 if (frag_threshold < 256) {
794 result = -EINVAL;
795 goto bad_res;
796 }
797 if (frag_threshold != (u32) -1) {
798 /*
799 * Fragments (apart from the last one) are required to
800 * have even length. Make the fragmentation code
801 * simpler by stripping LSB should someone try to use
802 * odd threshold value.
803 */
804 frag_threshold &= ~0x1;
805 }
806 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
807 }
808
809 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
810 rts_threshold = nla_get_u32(
811 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
812 changed |= WIPHY_PARAM_RTS_THRESHOLD;
813 }
814
Lukáš Turek81077e82009-12-21 22:50:47 +0100815 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
816 coverage_class = nla_get_u8(
817 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
818 changed |= WIPHY_PARAM_COVERAGE_CLASS;
819 }
820
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200821 if (changed) {
822 u8 old_retry_short, old_retry_long;
823 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100824 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200825
826 if (!rdev->ops->set_wiphy_params) {
827 result = -EOPNOTSUPP;
828 goto bad_res;
829 }
830
831 old_retry_short = rdev->wiphy.retry_short;
832 old_retry_long = rdev->wiphy.retry_long;
833 old_frag_threshold = rdev->wiphy.frag_threshold;
834 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100835 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200836
837 if (changed & WIPHY_PARAM_RETRY_SHORT)
838 rdev->wiphy.retry_short = retry_short;
839 if (changed & WIPHY_PARAM_RETRY_LONG)
840 rdev->wiphy.retry_long = retry_long;
841 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
842 rdev->wiphy.frag_threshold = frag_threshold;
843 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
844 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100845 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
846 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200847
848 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
849 if (result) {
850 rdev->wiphy.retry_short = old_retry_short;
851 rdev->wiphy.retry_long = old_retry_long;
852 rdev->wiphy.frag_threshold = old_frag_threshold;
853 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100854 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200855 }
856 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200857
Johannes Berg306d6112008-12-08 12:39:04 +0100858 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100859 mutex_unlock(&rdev->mtx);
860 unlock:
861 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400862 return result;
863}
864
865
866static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200867 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400868 struct net_device *dev)
869{
870 void *hdr;
871
872 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
873 if (!hdr)
874 return -1;
875
876 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200877 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400878 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200879 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200880
881 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
882 rdev->devlist_generation ^
883 (cfg80211_rdev_list_generation << 2));
884
Johannes Berg55682962007-09-20 13:09:35 -0400885 return genlmsg_end(msg, hdr);
886
887 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700888 genlmsg_cancel(msg, hdr);
889 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400890}
891
892static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
893{
894 int wp_idx = 0;
895 int if_idx = 0;
896 int wp_start = cb->args[0];
897 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +0200898 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -0400899 struct wireless_dev *wdev;
900
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500901 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200902 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
903 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +0200904 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200905 if (wp_idx < wp_start) {
906 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400907 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200908 }
Johannes Berg55682962007-09-20 13:09:35 -0400909 if_idx = 0;
910
Johannes Bergf5ea9122009-08-07 16:17:38 +0200911 mutex_lock(&rdev->devlist_mtx);
912 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200913 if (if_idx < if_start) {
914 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400915 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200916 }
Johannes Berg55682962007-09-20 13:09:35 -0400917 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
918 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +0200919 rdev, wdev->netdev) < 0) {
920 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200921 goto out;
922 }
923 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400924 }
Johannes Bergf5ea9122009-08-07 16:17:38 +0200925 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200926
927 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400928 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200929 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500930 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400931
932 cb->args[0] = wp_idx;
933 cb->args[1] = if_idx;
934
935 return skb->len;
936}
937
938static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
939{
940 struct sk_buff *msg;
941 struct cfg80211_registered_device *dev;
942 struct net_device *netdev;
943 int err;
944
Johannes Berg463d0182009-07-14 00:33:35 +0200945 err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400946 if (err)
947 return err;
948
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700949 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400950 if (!msg)
951 goto out_err;
952
Johannes Bergd7264052009-04-19 16:23:20 +0200953 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
954 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -0400955 goto out_free;
956
957 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200958 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400959
Johannes Berg134e6372009-07-10 09:51:34 +0000960 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400961
962 out_free:
963 nlmsg_free(msg);
964 out_err:
965 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200966 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400967 return -ENOBUFS;
968}
969
Michael Wu66f7ac52008-01-31 19:48:22 +0100970static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
971 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
972 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
973 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
974 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
975 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
976};
977
978static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
979{
980 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
981 int flag;
982
983 *mntrflags = 0;
984
985 if (!nla)
986 return -EINVAL;
987
988 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
989 nla, mntr_flags_policy))
990 return -EINVAL;
991
992 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
993 if (flags[flag])
994 *mntrflags |= (1<<flag);
995
996 return 0;
997}
998
Johannes Berg9bc383d2009-11-19 11:55:19 +0100999static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001000 struct net_device *netdev, u8 use_4addr,
1001 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001002{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001003 if (!use_4addr) {
1004 if (netdev && netdev->br_port)
1005 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001006 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001007 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001008
1009 switch (iftype) {
1010 case NL80211_IFTYPE_AP_VLAN:
1011 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1012 return 0;
1013 break;
1014 case NL80211_IFTYPE_STATION:
1015 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1016 return 0;
1017 break;
1018 default:
1019 break;
1020 }
1021
1022 return -EOPNOTSUPP;
1023}
1024
Johannes Berg55682962007-09-20 13:09:35 -04001025static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1026{
Johannes Berg79c97e92009-07-07 03:56:12 +02001027 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001028 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001029 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001030 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -04001031 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +02001032 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001033 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001034
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001035 memset(&params, 0, sizeof(params));
1036
Johannes Berg3b858752009-03-12 09:55:09 +01001037 rtnl_lock();
1038
Johannes Berg463d0182009-07-14 00:33:35 +02001039 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001040 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001041 goto unlock_rtnl;
1042
Johannes Berg04a773a2009-04-19 21:24:32 +02001043 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001044
Johannes Berg723b0382008-09-16 20:22:09 +02001045 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001046 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001047 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001048 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +02001049 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001050 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001051 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001052 }
Johannes Berg723b0382008-09-16 20:22:09 +02001053 }
1054
Johannes Berg92ffe052008-09-16 20:39:36 +02001055 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001056 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001057 err = -EINVAL;
1058 goto unlock;
1059 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001060 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1061 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001062 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001063 }
1064
Felix Fietkau8b787642009-11-10 18:53:10 +01001065 if (info->attrs[NL80211_ATTR_4ADDR]) {
1066 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1067 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001068 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001069 if (err)
1070 goto unlock;
Felix Fietkau8b787642009-11-10 18:53:10 +01001071 } else {
1072 params.use_4addr = -1;
1073 }
1074
Johannes Berg92ffe052008-09-16 20:39:36 +02001075 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001076 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001077 err = -EINVAL;
1078 goto unlock;
1079 }
1080 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1081 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001082 if (err)
1083 goto unlock;
1084
1085 flags = &_flags;
1086 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001087 }
Johannes Berg3b858752009-03-12 09:55:09 +01001088
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001089 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001090 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001091 else
1092 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001093
Johannes Berg9bc383d2009-11-19 11:55:19 +01001094 if (!err && params.use_4addr != -1)
1095 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1096
Johannes Berg55682962007-09-20 13:09:35 -04001097 unlock:
Johannes Berge36d56b2009-06-09 21:04:43 +02001098 dev_put(dev);
Johannes Berg79c97e92009-07-07 03:56:12 +02001099 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001100 unlock_rtnl:
1101 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001102 return err;
1103}
1104
1105static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1106{
Johannes Berg79c97e92009-07-07 03:56:12 +02001107 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001108 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001109 int err;
1110 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001111 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001112
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001113 memset(&params, 0, sizeof(params));
1114
Johannes Berg55682962007-09-20 13:09:35 -04001115 if (!info->attrs[NL80211_ATTR_IFNAME])
1116 return -EINVAL;
1117
1118 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1119 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1120 if (type > NL80211_IFTYPE_MAX)
1121 return -EINVAL;
1122 }
1123
Johannes Berg3b858752009-03-12 09:55:09 +01001124 rtnl_lock();
1125
Johannes Berg79c97e92009-07-07 03:56:12 +02001126 rdev = cfg80211_get_dev_from_info(info);
1127 if (IS_ERR(rdev)) {
1128 err = PTR_ERR(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001129 goto unlock_rtnl;
1130 }
Johannes Berg55682962007-09-20 13:09:35 -04001131
Johannes Berg79c97e92009-07-07 03:56:12 +02001132 if (!rdev->ops->add_virtual_intf ||
1133 !(rdev->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -04001134 err = -EOPNOTSUPP;
1135 goto unlock;
1136 }
1137
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001138 if (type == NL80211_IFTYPE_MESH_POINT &&
1139 info->attrs[NL80211_ATTR_MESH_ID]) {
1140 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1141 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1142 }
1143
Johannes Berg9bc383d2009-11-19 11:55:19 +01001144 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001145 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001146 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001147 if (err)
1148 goto unlock;
1149 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001150
Michael Wu66f7ac52008-01-31 19:48:22 +01001151 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1152 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1153 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001154 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001155 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001156 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001157
Johannes Berg55682962007-09-20 13:09:35 -04001158 unlock:
Johannes Berg79c97e92009-07-07 03:56:12 +02001159 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001160 unlock_rtnl:
1161 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001162 return err;
1163}
1164
1165static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1166{
Johannes Berg79c97e92009-07-07 03:56:12 +02001167 struct cfg80211_registered_device *rdev;
Johannes Berg463d0182009-07-14 00:33:35 +02001168 int err;
Johannes Berg55682962007-09-20 13:09:35 -04001169 struct net_device *dev;
1170
Johannes Berg3b858752009-03-12 09:55:09 +01001171 rtnl_lock();
1172
Johannes Berg463d0182009-07-14 00:33:35 +02001173 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001174 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001175 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -04001176
Johannes Berg79c97e92009-07-07 03:56:12 +02001177 if (!rdev->ops->del_virtual_intf) {
Johannes Berg55682962007-09-20 13:09:35 -04001178 err = -EOPNOTSUPP;
1179 goto out;
1180 }
1181
Johannes Berg463d0182009-07-14 00:33:35 +02001182 err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001183
1184 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001185 cfg80211_unlock_rdev(rdev);
Johannes Berg463d0182009-07-14 00:33:35 +02001186 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001187 unlock_rtnl:
1188 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001189 return err;
1190}
1191
Johannes Berg41ade002007-12-19 02:03:29 +01001192struct get_key_cookie {
1193 struct sk_buff *msg;
1194 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001195 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001196};
1197
1198static void get_key_callback(void *c, struct key_params *params)
1199{
Johannes Bergb9454e82009-07-08 13:29:08 +02001200 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001201 struct get_key_cookie *cookie = c;
1202
1203 if (params->key)
1204 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1205 params->key_len, params->key);
1206
1207 if (params->seq)
1208 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1209 params->seq_len, params->seq);
1210
1211 if (params->cipher)
1212 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1213 params->cipher);
1214
Johannes Bergb9454e82009-07-08 13:29:08 +02001215 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1216 if (!key)
1217 goto nla_put_failure;
1218
1219 if (params->key)
1220 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1221 params->key_len, params->key);
1222
1223 if (params->seq)
1224 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1225 params->seq_len, params->seq);
1226
1227 if (params->cipher)
1228 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1229 params->cipher);
1230
1231 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1232
1233 nla_nest_end(cookie->msg, key);
1234
Johannes Berg41ade002007-12-19 02:03:29 +01001235 return;
1236 nla_put_failure:
1237 cookie->error = 1;
1238}
1239
1240static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1241{
Johannes Berg79c97e92009-07-07 03:56:12 +02001242 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001243 int err;
1244 struct net_device *dev;
1245 u8 key_idx = 0;
1246 u8 *mac_addr = NULL;
1247 struct get_key_cookie cookie = {
1248 .error = 0,
1249 };
1250 void *hdr;
1251 struct sk_buff *msg;
1252
1253 if (info->attrs[NL80211_ATTR_KEY_IDX])
1254 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1255
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001256 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001257 return -EINVAL;
1258
1259 if (info->attrs[NL80211_ATTR_MAC])
1260 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1261
Johannes Berg3b858752009-03-12 09:55:09 +01001262 rtnl_lock();
1263
Johannes Berg463d0182009-07-14 00:33:35 +02001264 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001265 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001266 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001267
Johannes Berg79c97e92009-07-07 03:56:12 +02001268 if (!rdev->ops->get_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001269 err = -EOPNOTSUPP;
1270 goto out;
1271 }
1272
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001273 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +01001274 if (!msg) {
1275 err = -ENOMEM;
1276 goto out;
1277 }
1278
1279 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1280 NL80211_CMD_NEW_KEY);
1281
1282 if (IS_ERR(hdr)) {
1283 err = PTR_ERR(hdr);
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001284 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001285 }
1286
1287 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001288 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001289
1290 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1291 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1292 if (mac_addr)
1293 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1294
Johannes Berg79c97e92009-07-07 03:56:12 +02001295 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001296 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001297
1298 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001299 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001300
1301 if (cookie.error)
1302 goto nla_put_failure;
1303
1304 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00001305 err = genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001306 goto out;
1307
1308 nla_put_failure:
1309 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001310 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001311 nlmsg_free(msg);
1312 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001313 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001314 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001315 unlock_rtnl:
1316 rtnl_unlock();
1317
Johannes Berg41ade002007-12-19 02:03:29 +01001318 return err;
1319}
1320
1321static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1322{
Johannes Berg79c97e92009-07-07 03:56:12 +02001323 struct cfg80211_registered_device *rdev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001324 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001325 int err;
1326 struct net_device *dev;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001327 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1328 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001329
Johannes Bergb9454e82009-07-08 13:29:08 +02001330 err = nl80211_parse_key(info, &key);
1331 if (err)
1332 return err;
1333
1334 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001335 return -EINVAL;
1336
Johannes Bergb9454e82009-07-08 13:29:08 +02001337 /* only support setting default key */
1338 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001339 return -EINVAL;
1340
Johannes Berg3b858752009-03-12 09:55:09 +01001341 rtnl_lock();
1342
Johannes Berg463d0182009-07-14 00:33:35 +02001343 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001344 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001345 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001346
Johannes Bergb9454e82009-07-08 13:29:08 +02001347 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001348 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001349 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001350 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001351
1352 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001353 err = -EOPNOTSUPP;
1354 goto out;
1355 }
1356
Johannes Bergfffd0932009-07-08 14:22:54 +02001357 wdev_lock(dev->ieee80211_ptr);
1358 err = nl80211_key_allowed(dev->ieee80211_ptr);
1359 if (!err)
1360 err = func(&rdev->wiphy, dev, key.idx);
1361
Johannes Berg3d23e342009-09-29 23:27:28 +02001362#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001363 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001364 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001365 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001366 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001367 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001368 }
1369#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001370 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001371
1372 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001373 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001374 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001375
1376 unlock_rtnl:
1377 rtnl_unlock();
1378
Johannes Berg41ade002007-12-19 02:03:29 +01001379 return err;
1380}
1381
1382static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1383{
Johannes Berg79c97e92009-07-07 03:56:12 +02001384 struct cfg80211_registered_device *rdev;
Johannes Bergfffd0932009-07-08 14:22:54 +02001385 int err;
Johannes Berg41ade002007-12-19 02:03:29 +01001386 struct net_device *dev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001387 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001388 u8 *mac_addr = NULL;
1389
Johannes Bergb9454e82009-07-08 13:29:08 +02001390 err = nl80211_parse_key(info, &key);
1391 if (err)
1392 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001393
Johannes Bergb9454e82009-07-08 13:29:08 +02001394 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001395 return -EINVAL;
1396
Johannes Berg41ade002007-12-19 02:03:29 +01001397 if (info->attrs[NL80211_ATTR_MAC])
1398 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1399
Johannes Berg3b858752009-03-12 09:55:09 +01001400 rtnl_lock();
1401
Johannes Berg463d0182009-07-14 00:33:35 +02001402 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001403 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001404 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001405
Johannes Berg79c97e92009-07-07 03:56:12 +02001406 if (!rdev->ops->add_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001407 err = -EOPNOTSUPP;
1408 goto out;
1409 }
1410
Johannes Bergfffd0932009-07-08 14:22:54 +02001411 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1412 err = -EINVAL;
1413 goto out;
1414 }
1415
1416 wdev_lock(dev->ieee80211_ptr);
1417 err = nl80211_key_allowed(dev->ieee80211_ptr);
1418 if (!err)
1419 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1420 mac_addr, &key.p);
1421 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001422
1423 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001424 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001425 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001426 unlock_rtnl:
1427 rtnl_unlock();
1428
Johannes Berg41ade002007-12-19 02:03:29 +01001429 return err;
1430}
1431
1432static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1433{
Johannes Berg79c97e92009-07-07 03:56:12 +02001434 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001435 int err;
1436 struct net_device *dev;
Johannes Berg41ade002007-12-19 02:03:29 +01001437 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001438 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001439
Johannes Bergb9454e82009-07-08 13:29:08 +02001440 err = nl80211_parse_key(info, &key);
1441 if (err)
1442 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001443
1444 if (info->attrs[NL80211_ATTR_MAC])
1445 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1446
Johannes Berg3b858752009-03-12 09:55:09 +01001447 rtnl_lock();
1448
Johannes Berg463d0182009-07-14 00:33:35 +02001449 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001450 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001451 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001452
Johannes Berg79c97e92009-07-07 03:56:12 +02001453 if (!rdev->ops->del_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001454 err = -EOPNOTSUPP;
1455 goto out;
1456 }
1457
Johannes Bergfffd0932009-07-08 14:22:54 +02001458 wdev_lock(dev->ieee80211_ptr);
1459 err = nl80211_key_allowed(dev->ieee80211_ptr);
1460 if (!err)
1461 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001462
Johannes Berg3d23e342009-09-29 23:27:28 +02001463#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001464 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001465 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001466 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001467 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001468 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1469 }
1470#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001471 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001472
Johannes Berg41ade002007-12-19 02:03:29 +01001473 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001474 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001475 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001476
1477 unlock_rtnl:
1478 rtnl_unlock();
1479
Johannes Berg41ade002007-12-19 02:03:29 +01001480 return err;
1481}
1482
Johannes Berged1b6cc2007-12-19 02:03:32 +01001483static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1484{
1485 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1486 struct beacon_parameters *info);
Johannes Berg79c97e92009-07-07 03:56:12 +02001487 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001488 int err;
1489 struct net_device *dev;
1490 struct beacon_parameters params;
1491 int haveinfo = 0;
1492
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001493 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1494 return -EINVAL;
1495
Johannes Berg3b858752009-03-12 09:55:09 +01001496 rtnl_lock();
1497
Johannes Berg463d0182009-07-14 00:33:35 +02001498 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001499 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001500 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001501
Jouni Malineneec60b02009-03-20 21:21:19 +02001502 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1503 err = -EOPNOTSUPP;
1504 goto out;
1505 }
1506
Johannes Berged1b6cc2007-12-19 02:03:32 +01001507 switch (info->genlhdr->cmd) {
1508 case NL80211_CMD_NEW_BEACON:
1509 /* these are required for NEW_BEACON */
1510 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1511 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1512 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1513 err = -EINVAL;
1514 goto out;
1515 }
1516
Johannes Berg79c97e92009-07-07 03:56:12 +02001517 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001518 break;
1519 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001520 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001521 break;
1522 default:
1523 WARN_ON(1);
1524 err = -EOPNOTSUPP;
1525 goto out;
1526 }
1527
1528 if (!call) {
1529 err = -EOPNOTSUPP;
1530 goto out;
1531 }
1532
1533 memset(&params, 0, sizeof(params));
1534
1535 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1536 params.interval =
1537 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1538 haveinfo = 1;
1539 }
1540
1541 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1542 params.dtim_period =
1543 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1544 haveinfo = 1;
1545 }
1546
1547 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1548 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1549 params.head_len =
1550 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1551 haveinfo = 1;
1552 }
1553
1554 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1555 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1556 params.tail_len =
1557 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1558 haveinfo = 1;
1559 }
1560
1561 if (!haveinfo) {
1562 err = -EINVAL;
1563 goto out;
1564 }
1565
Johannes Berg79c97e92009-07-07 03:56:12 +02001566 err = call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001567
1568 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001569 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001570 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001571 unlock_rtnl:
1572 rtnl_unlock();
1573
Johannes Berged1b6cc2007-12-19 02:03:32 +01001574 return err;
1575}
1576
1577static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1578{
Johannes Berg79c97e92009-07-07 03:56:12 +02001579 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001580 int err;
1581 struct net_device *dev;
1582
Johannes Berg3b858752009-03-12 09:55:09 +01001583 rtnl_lock();
1584
Johannes Berg463d0182009-07-14 00:33:35 +02001585 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001586 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001587 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001588
Johannes Berg79c97e92009-07-07 03:56:12 +02001589 if (!rdev->ops->del_beacon) {
Johannes Berged1b6cc2007-12-19 02:03:32 +01001590 err = -EOPNOTSUPP;
1591 goto out;
1592 }
1593
Jouni Malineneec60b02009-03-20 21:21:19 +02001594 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1595 err = -EOPNOTSUPP;
1596 goto out;
1597 }
Johannes Berg79c97e92009-07-07 03:56:12 +02001598 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001599
1600 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001601 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001602 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001603 unlock_rtnl:
1604 rtnl_unlock();
1605
Johannes Berged1b6cc2007-12-19 02:03:32 +01001606 return err;
1607}
1608
Johannes Berg5727ef12007-12-19 02:03:34 +01001609static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1610 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1611 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1612 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001613 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001614};
1615
Johannes Bergeccb8e82009-05-11 21:57:56 +03001616static int parse_station_flags(struct genl_info *info,
1617 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001618{
1619 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001620 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001621 int flag;
1622
Johannes Bergeccb8e82009-05-11 21:57:56 +03001623 /*
1624 * Try parsing the new attribute first so userspace
1625 * can specify both for older kernels.
1626 */
1627 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1628 if (nla) {
1629 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001630
Johannes Bergeccb8e82009-05-11 21:57:56 +03001631 sta_flags = nla_data(nla);
1632 params->sta_flags_mask = sta_flags->mask;
1633 params->sta_flags_set = sta_flags->set;
1634 if ((params->sta_flags_mask |
1635 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1636 return -EINVAL;
1637 return 0;
1638 }
1639
1640 /* if present, parse the old attribute */
1641
1642 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001643 if (!nla)
1644 return 0;
1645
1646 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1647 nla, sta_flags_policy))
1648 return -EINVAL;
1649
Johannes Bergeccb8e82009-05-11 21:57:56 +03001650 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1651 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001652
1653 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1654 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001655 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001656
1657 return 0;
1658}
1659
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001660static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1661 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001662 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001663{
1664 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001665 struct nlattr *sinfoattr, *txrate;
1666 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001667
1668 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1669 if (!hdr)
1670 return -1;
1671
1672 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1673 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1674
Johannes Bergf5ea9122009-08-07 16:17:38 +02001675 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1676
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001677 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1678 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001679 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001680 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1681 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1682 sinfo->inactive_time);
1683 if (sinfo->filled & STATION_INFO_RX_BYTES)
1684 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1685 sinfo->rx_bytes);
1686 if (sinfo->filled & STATION_INFO_TX_BYTES)
1687 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1688 sinfo->tx_bytes);
1689 if (sinfo->filled & STATION_INFO_LLID)
1690 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1691 sinfo->llid);
1692 if (sinfo->filled & STATION_INFO_PLID)
1693 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1694 sinfo->plid);
1695 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1696 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1697 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001698 if (sinfo->filled & STATION_INFO_SIGNAL)
1699 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1700 sinfo->signal);
1701 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1702 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1703 if (!txrate)
1704 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001705
John W. Linville254416a2009-12-09 16:43:52 -05001706 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1707 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001708 if (bitrate > 0)
1709 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1710
1711 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1712 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1713 sinfo->txrate.mcs);
1714 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1715 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1716 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1717 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1718
1719 nla_nest_end(msg, txrate);
1720 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001721 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1722 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1723 sinfo->rx_packets);
1724 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1725 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1726 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001727 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001728
1729 return genlmsg_end(msg, hdr);
1730
1731 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001732 genlmsg_cancel(msg, hdr);
1733 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001734}
1735
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001736static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001737 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001738{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001739 struct station_info sinfo;
1740 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001741 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001742 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001743 int ifidx = cb->args[0];
1744 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001745 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001746
Holger Schuriga0438972009-11-11 11:30:02 +01001747 if (!ifidx)
1748 ifidx = nl80211_get_ifidx(cb);
1749 if (ifidx < 0)
1750 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001751
Johannes Berg3b858752009-03-12 09:55:09 +01001752 rtnl_lock();
1753
Johannes Berg463d0182009-07-14 00:33:35 +02001754 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01001755 if (!netdev) {
1756 err = -ENODEV;
1757 goto out_rtnl;
1758 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001759
Johannes Berg463d0182009-07-14 00:33:35 +02001760 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001761 if (IS_ERR(dev)) {
1762 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001763 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001764 }
1765
1766 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001767 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001768 goto out_err;
1769 }
1770
Johannes Bergbba95fe2008-07-29 13:22:51 +02001771 while (1) {
1772 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1773 mac_addr, &sinfo);
1774 if (err == -ENOENT)
1775 break;
1776 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001777 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001778
1779 if (nl80211_send_station(skb,
1780 NETLINK_CB(cb->skb).pid,
1781 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1782 netdev, mac_addr,
1783 &sinfo) < 0)
1784 goto out;
1785
1786 sta_idx++;
1787 }
1788
1789
1790 out:
1791 cb->args[1] = sta_idx;
1792 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001793 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001794 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001795 out_rtnl:
1796 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001797
1798 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001799}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001800
Johannes Berg5727ef12007-12-19 02:03:34 +01001801static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1802{
Johannes Berg79c97e92009-07-07 03:56:12 +02001803 struct cfg80211_registered_device *rdev;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001804 int err;
1805 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001806 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001807 struct sk_buff *msg;
1808 u8 *mac_addr = NULL;
1809
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001810 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001811
1812 if (!info->attrs[NL80211_ATTR_MAC])
1813 return -EINVAL;
1814
1815 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1816
Johannes Berg3b858752009-03-12 09:55:09 +01001817 rtnl_lock();
1818
Johannes Berg463d0182009-07-14 00:33:35 +02001819 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001820 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001821 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001822
Johannes Berg79c97e92009-07-07 03:56:12 +02001823 if (!rdev->ops->get_station) {
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001824 err = -EOPNOTSUPP;
1825 goto out;
1826 }
1827
Johannes Berg79c97e92009-07-07 03:56:12 +02001828 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001829 if (err)
1830 goto out;
1831
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001832 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001833 if (!msg)
1834 goto out;
1835
1836 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001837 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001838 goto out_free;
1839
Johannes Berg134e6372009-07-10 09:51:34 +00001840 err = genlmsg_reply(msg, info);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001841 goto out;
1842
1843 out_free:
1844 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001845 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001846 cfg80211_unlock_rdev(rdev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001847 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001848 out_rtnl:
1849 rtnl_unlock();
1850
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001851 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001852}
1853
1854/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001855 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01001856 */
Johannes Berg463d0182009-07-14 00:33:35 +02001857static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01001858 struct cfg80211_registered_device *rdev,
1859 struct net_device **vlan)
1860{
Johannes Berg463d0182009-07-14 00:33:35 +02001861 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01001862 *vlan = NULL;
1863
1864 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02001865 *vlan = dev_get_by_index(genl_info_net(info),
1866 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01001867 if (!*vlan)
1868 return -ENODEV;
1869 if (!(*vlan)->ieee80211_ptr)
1870 return -EINVAL;
1871 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1872 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001873 if (!netif_running(*vlan))
1874 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01001875 }
1876 return 0;
1877}
1878
1879static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1880{
Johannes Berg79c97e92009-07-07 03:56:12 +02001881 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001882 int err;
1883 struct net_device *dev;
1884 struct station_parameters params;
1885 u8 *mac_addr = NULL;
1886
1887 memset(&params, 0, sizeof(params));
1888
1889 params.listen_interval = -1;
1890
1891 if (info->attrs[NL80211_ATTR_STA_AID])
1892 return -EINVAL;
1893
1894 if (!info->attrs[NL80211_ATTR_MAC])
1895 return -EINVAL;
1896
1897 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1898
1899 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1900 params.supported_rates =
1901 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1902 params.supported_rates_len =
1903 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1904 }
1905
1906 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1907 params.listen_interval =
1908 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1909
Jouni Malinen36aedc92008-08-25 11:58:58 +03001910 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1911 params.ht_capa =
1912 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1913
Johannes Bergeccb8e82009-05-11 21:57:56 +03001914 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001915 return -EINVAL;
1916
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001917 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1918 params.plink_action =
1919 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1920
Johannes Berg3b858752009-03-12 09:55:09 +01001921 rtnl_lock();
1922
Johannes Berg463d0182009-07-14 00:33:35 +02001923 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001924 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001925 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001926
Johannes Berg463d0182009-07-14 00:33:35 +02001927 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001928 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02001929 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001930
1931 /* validate settings */
1932 err = 0;
1933
1934 switch (dev->ieee80211_ptr->iftype) {
1935 case NL80211_IFTYPE_AP:
1936 case NL80211_IFTYPE_AP_VLAN:
1937 /* disallow mesh-specific things */
1938 if (params.plink_action)
1939 err = -EINVAL;
1940 break;
1941 case NL80211_IFTYPE_STATION:
1942 /* disallow everything but AUTHORIZED flag */
1943 if (params.plink_action)
1944 err = -EINVAL;
1945 if (params.vlan)
1946 err = -EINVAL;
1947 if (params.supported_rates)
1948 err = -EINVAL;
1949 if (params.ht_capa)
1950 err = -EINVAL;
1951 if (params.listen_interval >= 0)
1952 err = -EINVAL;
1953 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
1954 err = -EINVAL;
1955 break;
1956 case NL80211_IFTYPE_MESH_POINT:
1957 /* disallow things mesh doesn't support */
1958 if (params.vlan)
1959 err = -EINVAL;
1960 if (params.ht_capa)
1961 err = -EINVAL;
1962 if (params.listen_interval >= 0)
1963 err = -EINVAL;
1964 if (params.supported_rates)
1965 err = -EINVAL;
1966 if (params.sta_flags_mask)
1967 err = -EINVAL;
1968 break;
1969 default:
1970 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02001971 }
1972
Johannes Berg5727ef12007-12-19 02:03:34 +01001973 if (err)
1974 goto out;
1975
Johannes Berg79c97e92009-07-07 03:56:12 +02001976 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01001977 err = -EOPNOTSUPP;
1978 goto out;
1979 }
1980
Johannes Berg79c97e92009-07-07 03:56:12 +02001981 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001982
1983 out:
1984 if (params.vlan)
1985 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02001986 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001987 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001988 out_rtnl:
1989 rtnl_unlock();
1990
Johannes Berg5727ef12007-12-19 02:03:34 +01001991 return err;
1992}
1993
1994static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1995{
Johannes Berg79c97e92009-07-07 03:56:12 +02001996 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001997 int err;
1998 struct net_device *dev;
1999 struct station_parameters params;
2000 u8 *mac_addr = NULL;
2001
2002 memset(&params, 0, sizeof(params));
2003
2004 if (!info->attrs[NL80211_ATTR_MAC])
2005 return -EINVAL;
2006
Johannes Berg5727ef12007-12-19 02:03:34 +01002007 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2008 return -EINVAL;
2009
2010 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2011 return -EINVAL;
2012
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002013 if (!info->attrs[NL80211_ATTR_STA_AID])
2014 return -EINVAL;
2015
Johannes Berg5727ef12007-12-19 02:03:34 +01002016 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2017 params.supported_rates =
2018 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2019 params.supported_rates_len =
2020 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2021 params.listen_interval =
2022 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002023
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002024 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2025 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2026 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002027
Jouni Malinen36aedc92008-08-25 11:58:58 +03002028 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2029 params.ht_capa =
2030 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002031
Johannes Bergeccb8e82009-05-11 21:57:56 +03002032 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002033 return -EINVAL;
2034
Johannes Berg3b858752009-03-12 09:55:09 +01002035 rtnl_lock();
2036
Johannes Berg463d0182009-07-14 00:33:35 +02002037 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002038 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002039 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002040
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002041 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2042 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN) {
2043 err = -EINVAL;
2044 goto out;
2045 }
2046
Johannes Berg463d0182009-07-14 00:33:35 +02002047 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002048 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002049 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002050
2051 /* validate settings */
2052 err = 0;
2053
Johannes Berg79c97e92009-07-07 03:56:12 +02002054 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002055 err = -EOPNOTSUPP;
2056 goto out;
2057 }
2058
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002059 if (!netif_running(dev)) {
2060 err = -ENETDOWN;
2061 goto out;
2062 }
2063
Johannes Berg79c97e92009-07-07 03:56:12 +02002064 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002065
2066 out:
2067 if (params.vlan)
2068 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002069 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002070 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002071 out_rtnl:
2072 rtnl_unlock();
2073
Johannes Berg5727ef12007-12-19 02:03:34 +01002074 return err;
2075}
2076
2077static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2078{
Johannes Berg79c97e92009-07-07 03:56:12 +02002079 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002080 int err;
2081 struct net_device *dev;
2082 u8 *mac_addr = NULL;
2083
2084 if (info->attrs[NL80211_ATTR_MAC])
2085 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2086
Johannes Berg3b858752009-03-12 09:55:09 +01002087 rtnl_lock();
2088
Johannes Berg463d0182009-07-14 00:33:35 +02002089 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002090 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002091 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002092
Johannes Berge80cf852009-05-11 14:43:13 +02002093 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002094 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN) {
Johannes Berge80cf852009-05-11 14:43:13 +02002095 err = -EINVAL;
2096 goto out;
2097 }
2098
Johannes Berg79c97e92009-07-07 03:56:12 +02002099 if (!rdev->ops->del_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002100 err = -EOPNOTSUPP;
2101 goto out;
2102 }
2103
Johannes Berg79c97e92009-07-07 03:56:12 +02002104 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002105
2106 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002107 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002108 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002109 out_rtnl:
2110 rtnl_unlock();
2111
Johannes Berg5727ef12007-12-19 02:03:34 +01002112 return err;
2113}
2114
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002115static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2116 int flags, struct net_device *dev,
2117 u8 *dst, u8 *next_hop,
2118 struct mpath_info *pinfo)
2119{
2120 void *hdr;
2121 struct nlattr *pinfoattr;
2122
2123 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2124 if (!hdr)
2125 return -1;
2126
2127 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2128 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2129 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2130
Johannes Bergf5ea9122009-08-07 16:17:38 +02002131 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2132
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002133 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2134 if (!pinfoattr)
2135 goto nla_put_failure;
2136 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2137 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2138 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002139 if (pinfo->filled & MPATH_INFO_SN)
2140 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2141 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002142 if (pinfo->filled & MPATH_INFO_METRIC)
2143 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2144 pinfo->metric);
2145 if (pinfo->filled & MPATH_INFO_EXPTIME)
2146 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2147 pinfo->exptime);
2148 if (pinfo->filled & MPATH_INFO_FLAGS)
2149 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2150 pinfo->flags);
2151 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2152 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2153 pinfo->discovery_timeout);
2154 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2155 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2156 pinfo->discovery_retries);
2157
2158 nla_nest_end(msg, pinfoattr);
2159
2160 return genlmsg_end(msg, hdr);
2161
2162 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002163 genlmsg_cancel(msg, hdr);
2164 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002165}
2166
2167static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002168 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002169{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002170 struct mpath_info pinfo;
2171 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002172 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002173 u8 dst[ETH_ALEN];
2174 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002175 int ifidx = cb->args[0];
2176 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002177 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002178
Holger Schuriga0438972009-11-11 11:30:02 +01002179 if (!ifidx)
2180 ifidx = nl80211_get_ifidx(cb);
2181 if (ifidx < 0)
2182 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002183
Johannes Berg3b858752009-03-12 09:55:09 +01002184 rtnl_lock();
2185
Johannes Berg463d0182009-07-14 00:33:35 +02002186 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01002187 if (!netdev) {
2188 err = -ENODEV;
2189 goto out_rtnl;
2190 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002191
Johannes Berg463d0182009-07-14 00:33:35 +02002192 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002193 if (IS_ERR(dev)) {
2194 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002195 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002196 }
2197
2198 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002199 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002200 goto out_err;
2201 }
2202
Jouni Malineneec60b02009-03-20 21:21:19 +02002203 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2204 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002205 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002206 }
2207
Johannes Bergbba95fe2008-07-29 13:22:51 +02002208 while (1) {
2209 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2210 dst, next_hop, &pinfo);
2211 if (err == -ENOENT)
2212 break;
2213 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002214 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002215
2216 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2217 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2218 netdev, dst, next_hop,
2219 &pinfo) < 0)
2220 goto out;
2221
2222 path_idx++;
2223 }
2224
2225
2226 out:
2227 cb->args[1] = path_idx;
2228 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002229 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02002230 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002231 out_rtnl:
2232 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002233
2234 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002235}
2236
2237static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2238{
Johannes Berg79c97e92009-07-07 03:56:12 +02002239 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002240 int err;
2241 struct net_device *dev;
2242 struct mpath_info pinfo;
2243 struct sk_buff *msg;
2244 u8 *dst = NULL;
2245 u8 next_hop[ETH_ALEN];
2246
2247 memset(&pinfo, 0, sizeof(pinfo));
2248
2249 if (!info->attrs[NL80211_ATTR_MAC])
2250 return -EINVAL;
2251
2252 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2253
Johannes Berg3b858752009-03-12 09:55:09 +01002254 rtnl_lock();
2255
Johannes Berg463d0182009-07-14 00:33:35 +02002256 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002257 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002258 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002259
Johannes Berg79c97e92009-07-07 03:56:12 +02002260 if (!rdev->ops->get_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002261 err = -EOPNOTSUPP;
2262 goto out;
2263 }
2264
Jouni Malineneec60b02009-03-20 21:21:19 +02002265 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2266 err = -EOPNOTSUPP;
2267 goto out;
2268 }
2269
Johannes Berg79c97e92009-07-07 03:56:12 +02002270 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002271 if (err)
2272 goto out;
2273
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002274 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002275 if (!msg)
2276 goto out;
2277
2278 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2279 dev, dst, next_hop, &pinfo) < 0)
2280 goto out_free;
2281
Johannes Berg134e6372009-07-10 09:51:34 +00002282 err = genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002283 goto out;
2284
2285 out_free:
2286 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002287 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002288 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002289 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002290 out_rtnl:
2291 rtnl_unlock();
2292
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002293 return err;
2294}
2295
2296static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2297{
Johannes Berg79c97e92009-07-07 03:56:12 +02002298 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002299 int err;
2300 struct net_device *dev;
2301 u8 *dst = NULL;
2302 u8 *next_hop = NULL;
2303
2304 if (!info->attrs[NL80211_ATTR_MAC])
2305 return -EINVAL;
2306
2307 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2308 return -EINVAL;
2309
2310 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2311 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2312
Johannes Berg3b858752009-03-12 09:55:09 +01002313 rtnl_lock();
2314
Johannes Berg463d0182009-07-14 00:33:35 +02002315 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002316 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002317 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002318
Johannes Berg79c97e92009-07-07 03:56:12 +02002319 if (!rdev->ops->change_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002320 err = -EOPNOTSUPP;
2321 goto out;
2322 }
2323
Jouni Malineneec60b02009-03-20 21:21:19 +02002324 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2325 err = -EOPNOTSUPP;
2326 goto out;
2327 }
2328
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002329 if (!netif_running(dev)) {
2330 err = -ENETDOWN;
2331 goto out;
2332 }
2333
Johannes Berg79c97e92009-07-07 03:56:12 +02002334 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002335
2336 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002337 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002338 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002339 out_rtnl:
2340 rtnl_unlock();
2341
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002342 return err;
2343}
2344static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2345{
Johannes Berg79c97e92009-07-07 03:56:12 +02002346 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002347 int err;
2348 struct net_device *dev;
2349 u8 *dst = NULL;
2350 u8 *next_hop = NULL;
2351
2352 if (!info->attrs[NL80211_ATTR_MAC])
2353 return -EINVAL;
2354
2355 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2356 return -EINVAL;
2357
2358 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2359 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2360
Johannes Berg3b858752009-03-12 09:55:09 +01002361 rtnl_lock();
2362
Johannes Berg463d0182009-07-14 00:33:35 +02002363 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002364 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002365 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002366
Johannes Berg79c97e92009-07-07 03:56:12 +02002367 if (!rdev->ops->add_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002368 err = -EOPNOTSUPP;
2369 goto out;
2370 }
2371
Jouni Malineneec60b02009-03-20 21:21:19 +02002372 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2373 err = -EOPNOTSUPP;
2374 goto out;
2375 }
2376
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002377 if (!netif_running(dev)) {
2378 err = -ENETDOWN;
2379 goto out;
2380 }
2381
Johannes Berg79c97e92009-07-07 03:56:12 +02002382 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002383
2384 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002385 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002386 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002387 out_rtnl:
2388 rtnl_unlock();
2389
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002390 return err;
2391}
2392
2393static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2394{
Johannes Berg79c97e92009-07-07 03:56:12 +02002395 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002396 int err;
2397 struct net_device *dev;
2398 u8 *dst = NULL;
2399
2400 if (info->attrs[NL80211_ATTR_MAC])
2401 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2402
Johannes Berg3b858752009-03-12 09:55:09 +01002403 rtnl_lock();
2404
Johannes Berg463d0182009-07-14 00:33:35 +02002405 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002406 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002407 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002408
Johannes Berg79c97e92009-07-07 03:56:12 +02002409 if (!rdev->ops->del_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002410 err = -EOPNOTSUPP;
2411 goto out;
2412 }
2413
Johannes Berg79c97e92009-07-07 03:56:12 +02002414 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002415
2416 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002417 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002418 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002419 out_rtnl:
2420 rtnl_unlock();
2421
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002422 return err;
2423}
2424
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002425static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2426{
Johannes Berg79c97e92009-07-07 03:56:12 +02002427 struct cfg80211_registered_device *rdev;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002428 int err;
2429 struct net_device *dev;
2430 struct bss_parameters params;
2431
2432 memset(&params, 0, sizeof(params));
2433 /* default to not changing parameters */
2434 params.use_cts_prot = -1;
2435 params.use_short_preamble = -1;
2436 params.use_short_slot_time = -1;
2437
2438 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2439 params.use_cts_prot =
2440 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2441 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2442 params.use_short_preamble =
2443 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2444 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2445 params.use_short_slot_time =
2446 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002447 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2448 params.basic_rates =
2449 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2450 params.basic_rates_len =
2451 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2452 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002453
Johannes Berg3b858752009-03-12 09:55:09 +01002454 rtnl_lock();
2455
Johannes Berg463d0182009-07-14 00:33:35 +02002456 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002457 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002458 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002459
Johannes Berg79c97e92009-07-07 03:56:12 +02002460 if (!rdev->ops->change_bss) {
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002461 err = -EOPNOTSUPP;
2462 goto out;
2463 }
2464
Jouni Malineneec60b02009-03-20 21:21:19 +02002465 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2466 err = -EOPNOTSUPP;
2467 goto out;
2468 }
2469
Johannes Berg79c97e92009-07-07 03:56:12 +02002470 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002471
2472 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002473 cfg80211_unlock_rdev(rdev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002474 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002475 out_rtnl:
2476 rtnl_unlock();
2477
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002478 return err;
2479}
2480
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002481static const struct nla_policy
2482 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2483 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2484 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2485 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2486 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2487 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2488 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2489};
2490
2491static int parse_reg_rule(struct nlattr *tb[],
2492 struct ieee80211_reg_rule *reg_rule)
2493{
2494 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2495 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2496
2497 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2498 return -EINVAL;
2499 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2500 return -EINVAL;
2501 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2502 return -EINVAL;
2503 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2504 return -EINVAL;
2505 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2506 return -EINVAL;
2507
2508 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2509
2510 freq_range->start_freq_khz =
2511 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2512 freq_range->end_freq_khz =
2513 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2514 freq_range->max_bandwidth_khz =
2515 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2516
2517 power_rule->max_eirp =
2518 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2519
2520 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2521 power_rule->max_antenna_gain =
2522 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2523
2524 return 0;
2525}
2526
2527static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2528{
2529 int r;
2530 char *data = NULL;
2531
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002532 /*
2533 * You should only get this when cfg80211 hasn't yet initialized
2534 * completely when built-in to the kernel right between the time
2535 * window between nl80211_init() and regulatory_init(), if that is
2536 * even possible.
2537 */
2538 mutex_lock(&cfg80211_mutex);
2539 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002540 mutex_unlock(&cfg80211_mutex);
2541 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002542 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002543 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002544
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002545 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2546 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002547
2548 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2549
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002550 r = regulatory_hint_user(data);
2551
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002552 return r;
2553}
2554
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002555static int nl80211_get_mesh_params(struct sk_buff *skb,
2556 struct genl_info *info)
2557{
Johannes Berg79c97e92009-07-07 03:56:12 +02002558 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002559 struct mesh_config cur_params;
2560 int err;
2561 struct net_device *dev;
2562 void *hdr;
2563 struct nlattr *pinfoattr;
2564 struct sk_buff *msg;
2565
Johannes Berg3b858752009-03-12 09:55:09 +01002566 rtnl_lock();
2567
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002568 /* Look up our device */
Johannes Berg463d0182009-07-14 00:33:35 +02002569 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002570 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002571 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002572
Johannes Berg79c97e92009-07-07 03:56:12 +02002573 if (!rdev->ops->get_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002574 err = -EOPNOTSUPP;
2575 goto out;
2576 }
2577
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002578 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002579 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002580 if (err)
2581 goto out;
2582
2583 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002584 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002585 if (!msg) {
2586 err = -ENOBUFS;
2587 goto out;
2588 }
2589 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2590 NL80211_CMD_GET_MESH_PARAMS);
2591 if (!hdr)
2592 goto nla_put_failure;
2593 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2594 if (!pinfoattr)
2595 goto nla_put_failure;
2596 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2597 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2598 cur_params.dot11MeshRetryTimeout);
2599 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2600 cur_params.dot11MeshConfirmTimeout);
2601 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2602 cur_params.dot11MeshHoldingTimeout);
2603 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2604 cur_params.dot11MeshMaxPeerLinks);
2605 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2606 cur_params.dot11MeshMaxRetries);
2607 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2608 cur_params.dot11MeshTTL);
2609 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2610 cur_params.auto_open_plinks);
2611 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2612 cur_params.dot11MeshHWMPmaxPREQretries);
2613 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2614 cur_params.path_refresh_time);
2615 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2616 cur_params.min_discovery_timeout);
2617 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2618 cur_params.dot11MeshHWMPactivePathTimeout);
2619 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2620 cur_params.dot11MeshHWMPpreqMinInterval);
2621 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2622 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002623 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2624 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002625 nla_nest_end(msg, pinfoattr);
2626 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002627 err = genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002628 goto out;
2629
Johannes Berg3b858752009-03-12 09:55:09 +01002630 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002631 genlmsg_cancel(msg, hdr);
2632 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002633 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002634 /* Cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002635 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002636 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002637 out_rtnl:
2638 rtnl_unlock();
2639
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002640 return err;
2641}
2642
2643#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2644do {\
2645 if (table[attr_num]) {\
2646 cfg.param = nla_fn(table[attr_num]); \
2647 mask |= (1 << (attr_num - 1)); \
2648 } \
2649} while (0);\
2650
2651static struct nla_policy
2652nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2653 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2654 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2655 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2656 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2657 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2658 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2659 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2660
2661 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2662 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2663 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2664 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2665 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2666 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2667};
2668
2669static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2670{
2671 int err;
2672 u32 mask;
Johannes Berg79c97e92009-07-07 03:56:12 +02002673 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002674 struct net_device *dev;
2675 struct mesh_config cfg;
2676 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2677 struct nlattr *parent_attr;
2678
2679 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2680 if (!parent_attr)
2681 return -EINVAL;
2682 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2683 parent_attr, nl80211_meshconf_params_policy))
2684 return -EINVAL;
2685
Johannes Berg3b858752009-03-12 09:55:09 +01002686 rtnl_lock();
2687
Johannes Berg463d0182009-07-14 00:33:35 +02002688 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002689 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002690 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002691
Johannes Berg79c97e92009-07-07 03:56:12 +02002692 if (!rdev->ops->set_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002693 err = -EOPNOTSUPP;
2694 goto out;
2695 }
2696
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002697 /* This makes sure that there aren't more than 32 mesh config
2698 * parameters (otherwise our bitfield scheme would not work.) */
2699 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2700
2701 /* Fill in the params struct */
2702 mask = 0;
2703 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2704 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2705 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2706 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2707 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2708 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2709 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2710 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2712 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2713 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2714 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2715 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2716 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2717 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2718 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2719 nla_get_u8);
2720 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2721 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2722 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2723 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2724 nla_get_u16);
2725 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2726 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2727 nla_get_u32);
2728 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2729 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2730 nla_get_u16);
2731 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2732 dot11MeshHWMPnetDiameterTraversalTime,
2733 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2734 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002735 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2736 dot11MeshHWMPRootMode, mask,
2737 NL80211_MESHCONF_HWMP_ROOTMODE,
2738 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002739
2740 /* Apply changes */
Johannes Berg79c97e92009-07-07 03:56:12 +02002741 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002742
Jouni Malinenf3f92582009-03-20 17:57:36 +02002743 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002744 /* cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002745 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002746 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002747 out_rtnl:
2748 rtnl_unlock();
2749
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002750 return err;
2751}
2752
2753#undef FILL_IN_MESH_PARAM_IF_SET
2754
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002755static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2756{
2757 struct sk_buff *msg;
2758 void *hdr = NULL;
2759 struct nlattr *nl_reg_rules;
2760 unsigned int i;
2761 int err = -EINVAL;
2762
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002763 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002764
2765 if (!cfg80211_regdomain)
2766 goto out;
2767
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002768 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002769 if (!msg) {
2770 err = -ENOBUFS;
2771 goto out;
2772 }
2773
2774 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2775 NL80211_CMD_GET_REG);
2776 if (!hdr)
2777 goto nla_put_failure;
2778
2779 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2780 cfg80211_regdomain->alpha2);
2781
2782 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2783 if (!nl_reg_rules)
2784 goto nla_put_failure;
2785
2786 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2787 struct nlattr *nl_reg_rule;
2788 const struct ieee80211_reg_rule *reg_rule;
2789 const struct ieee80211_freq_range *freq_range;
2790 const struct ieee80211_power_rule *power_rule;
2791
2792 reg_rule = &cfg80211_regdomain->reg_rules[i];
2793 freq_range = &reg_rule->freq_range;
2794 power_rule = &reg_rule->power_rule;
2795
2796 nl_reg_rule = nla_nest_start(msg, i);
2797 if (!nl_reg_rule)
2798 goto nla_put_failure;
2799
2800 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2801 reg_rule->flags);
2802 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2803 freq_range->start_freq_khz);
2804 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2805 freq_range->end_freq_khz);
2806 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2807 freq_range->max_bandwidth_khz);
2808 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2809 power_rule->max_antenna_gain);
2810 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2811 power_rule->max_eirp);
2812
2813 nla_nest_end(msg, nl_reg_rule);
2814 }
2815
2816 nla_nest_end(msg, nl_reg_rules);
2817
2818 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002819 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002820 goto out;
2821
2822nla_put_failure:
2823 genlmsg_cancel(msg, hdr);
2824 err = -EMSGSIZE;
2825out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002826 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002827 return err;
2828}
2829
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002830static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2831{
2832 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2833 struct nlattr *nl_reg_rule;
2834 char *alpha2 = NULL;
2835 int rem_reg_rules = 0, r = 0;
2836 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2837 struct ieee80211_regdomain *rd = NULL;
2838
2839 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2840 return -EINVAL;
2841
2842 if (!info->attrs[NL80211_ATTR_REG_RULES])
2843 return -EINVAL;
2844
2845 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2846
2847 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2848 rem_reg_rules) {
2849 num_rules++;
2850 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002851 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002852 }
2853
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002854 mutex_lock(&cfg80211_mutex);
2855
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002856 if (!reg_is_valid_request(alpha2)) {
2857 r = -EINVAL;
2858 goto bad_reg;
2859 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002860
2861 size_of_regd = sizeof(struct ieee80211_regdomain) +
2862 (num_rules * sizeof(struct ieee80211_reg_rule));
2863
2864 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002865 if (!rd) {
2866 r = -ENOMEM;
2867 goto bad_reg;
2868 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002869
2870 rd->n_reg_rules = num_rules;
2871 rd->alpha2[0] = alpha2[0];
2872 rd->alpha2[1] = alpha2[1];
2873
2874 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2875 rem_reg_rules) {
2876 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2877 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2878 reg_rule_policy);
2879 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2880 if (r)
2881 goto bad_reg;
2882
2883 rule_idx++;
2884
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002885 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2886 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002887 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002888 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002889 }
2890
2891 BUG_ON(rule_idx != num_rules);
2892
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002893 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002894
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002895 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002896
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002897 return r;
2898
Johannes Bergd2372b32008-10-24 20:32:20 +02002899 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002900 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002901 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002902 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002903}
2904
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002905static int validate_scan_freqs(struct nlattr *freqs)
2906{
2907 struct nlattr *attr1, *attr2;
2908 int n_channels = 0, tmp1, tmp2;
2909
2910 nla_for_each_nested(attr1, freqs, tmp1) {
2911 n_channels++;
2912 /*
2913 * Some hardware has a limited channel list for
2914 * scanning, and it is pretty much nonsensical
2915 * to scan for a channel twice, so disallow that
2916 * and don't require drivers to check that the
2917 * channel list they get isn't longer than what
2918 * they can scan, as long as they can scan all
2919 * the channels they registered at once.
2920 */
2921 nla_for_each_nested(attr2, freqs, tmp2)
2922 if (attr1 != attr2 &&
2923 nla_get_u32(attr1) == nla_get_u32(attr2))
2924 return 0;
2925 }
2926
2927 return n_channels;
2928}
2929
Johannes Berg2a519312009-02-10 21:25:55 +01002930static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2931{
Johannes Berg79c97e92009-07-07 03:56:12 +02002932 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01002933 struct net_device *dev;
2934 struct cfg80211_scan_request *request;
2935 struct cfg80211_ssid *ssid;
2936 struct ieee80211_channel *channel;
2937 struct nlattr *attr;
2938 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002939 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002940 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002941 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002942
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002943 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2944 return -EINVAL;
2945
Johannes Berg3b858752009-03-12 09:55:09 +01002946 rtnl_lock();
2947
Johannes Berg463d0182009-07-14 00:33:35 +02002948 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg2a519312009-02-10 21:25:55 +01002949 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002950 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002951
Johannes Berg79c97e92009-07-07 03:56:12 +02002952 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002953
Johannes Berg79c97e92009-07-07 03:56:12 +02002954 if (!rdev->ops->scan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002955 err = -EOPNOTSUPP;
2956 goto out;
2957 }
2958
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002959 if (!netif_running(dev)) {
2960 err = -ENETDOWN;
2961 goto out;
2962 }
2963
Johannes Berg79c97e92009-07-07 03:56:12 +02002964 if (rdev->scan_req) {
Johannes Berg2a519312009-02-10 21:25:55 +01002965 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002966 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002967 }
2968
2969 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002970 n_channels = validate_scan_freqs(
2971 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg2a519312009-02-10 21:25:55 +01002972 if (!n_channels) {
2973 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002974 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002975 }
2976 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002977 n_channels = 0;
2978
Johannes Berg2a519312009-02-10 21:25:55 +01002979 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2980 if (wiphy->bands[band])
2981 n_channels += wiphy->bands[band]->n_channels;
2982 }
2983
2984 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2985 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2986 n_ssids++;
2987
2988 if (n_ssids > wiphy->max_scan_ssids) {
2989 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002990 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002991 }
2992
Jouni Malinen70692ad2009-02-16 19:39:13 +02002993 if (info->attrs[NL80211_ATTR_IE])
2994 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2995 else
2996 ie_len = 0;
2997
Johannes Berg18a83652009-03-31 12:12:05 +02002998 if (ie_len > wiphy->max_scan_ie_len) {
2999 err = -EINVAL;
3000 goto out;
3001 }
3002
Johannes Berg2a519312009-02-10 21:25:55 +01003003 request = kzalloc(sizeof(*request)
3004 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003005 + sizeof(channel) * n_channels
3006 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003007 if (!request) {
3008 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01003009 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003010 }
3011
Johannes Berg2a519312009-02-10 21:25:55 +01003012 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003013 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003014 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003015 if (ie_len) {
3016 if (request->ssids)
3017 request->ie = (void *)(request->ssids + n_ssids);
3018 else
3019 request->ie = (void *)(request->channels + n_channels);
3020 }
Johannes Berg2a519312009-02-10 21:25:55 +01003021
Johannes Berg584991d2009-11-02 13:32:03 +01003022 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003023 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3024 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003025 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003026 struct ieee80211_channel *chan;
3027
3028 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3029
3030 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003031 err = -EINVAL;
3032 goto out_free;
3033 }
Johannes Berg584991d2009-11-02 13:32:03 +01003034
3035 /* ignore disabled channels */
3036 if (chan->flags & IEEE80211_CHAN_DISABLED)
3037 continue;
3038
3039 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003040 i++;
3041 }
3042 } else {
3043 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003044 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3045 int j;
3046 if (!wiphy->bands[band])
3047 continue;
3048 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003049 struct ieee80211_channel *chan;
3050
3051 chan = &wiphy->bands[band]->channels[j];
3052
3053 if (chan->flags & IEEE80211_CHAN_DISABLED)
3054 continue;
3055
3056 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003057 i++;
3058 }
3059 }
3060 }
3061
Johannes Berg584991d2009-11-02 13:32:03 +01003062 if (!i) {
3063 err = -EINVAL;
3064 goto out_free;
3065 }
3066
3067 request->n_channels = i;
3068
Johannes Berg2a519312009-02-10 21:25:55 +01003069 i = 0;
3070 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3071 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3072 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3073 err = -EINVAL;
3074 goto out_free;
3075 }
3076 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3077 request->ssids[i].ssid_len = nla_len(attr);
3078 i++;
3079 }
3080 }
3081
Jouni Malinen70692ad2009-02-16 19:39:13 +02003082 if (info->attrs[NL80211_ATTR_IE]) {
3083 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003084 memcpy((void *)request->ie,
3085 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003086 request->ie_len);
3087 }
3088
Johannes Berg463d0182009-07-14 00:33:35 +02003089 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003090 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003091
Johannes Berg79c97e92009-07-07 03:56:12 +02003092 rdev->scan_req = request;
3093 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003094
Johannes Berg463d0182009-07-14 00:33:35 +02003095 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003096 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003097 dev_hold(dev);
3098 }
Johannes Berga538e2d2009-06-16 19:56:42 +02003099
Johannes Berg2a519312009-02-10 21:25:55 +01003100 out_free:
3101 if (err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003102 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003103 kfree(request);
3104 }
Johannes Berg2a519312009-02-10 21:25:55 +01003105 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003106 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003107 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01003108 out_rtnl:
3109 rtnl_unlock();
3110
Johannes Berg2a519312009-02-10 21:25:55 +01003111 return err;
3112}
3113
3114static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3115 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003116 struct wireless_dev *wdev,
3117 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003118{
Johannes Berg48ab9052009-07-10 18:42:31 +02003119 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003120 void *hdr;
3121 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003122 int i;
3123
3124 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003125
3126 hdr = nl80211hdr_put(msg, pid, seq, flags,
3127 NL80211_CMD_NEW_SCAN_RESULTS);
3128 if (!hdr)
3129 return -1;
3130
Johannes Bergf5ea9122009-08-07 16:17:38 +02003131 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003132 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003133
3134 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3135 if (!bss)
3136 goto nla_put_failure;
3137 if (!is_zero_ether_addr(res->bssid))
3138 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3139 if (res->information_elements && res->len_information_elements)
3140 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3141 res->len_information_elements,
3142 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003143 if (res->beacon_ies && res->len_beacon_ies &&
3144 res->beacon_ies != res->information_elements)
3145 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3146 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003147 if (res->tsf)
3148 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3149 if (res->beacon_interval)
3150 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3151 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3152 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003153 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3154 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003155
Johannes Berg77965c92009-02-18 18:45:06 +01003156 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003157 case CFG80211_SIGNAL_TYPE_MBM:
3158 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3159 break;
3160 case CFG80211_SIGNAL_TYPE_UNSPEC:
3161 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3162 break;
3163 default:
3164 break;
3165 }
3166
Johannes Berg48ab9052009-07-10 18:42:31 +02003167 switch (wdev->iftype) {
3168 case NL80211_IFTYPE_STATION:
3169 if (intbss == wdev->current_bss)
3170 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3171 NL80211_BSS_STATUS_ASSOCIATED);
3172 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3173 if (intbss != wdev->auth_bsses[i])
3174 continue;
3175 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3176 NL80211_BSS_STATUS_AUTHENTICATED);
3177 break;
3178 }
3179 break;
3180 case NL80211_IFTYPE_ADHOC:
3181 if (intbss == wdev->current_bss)
3182 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3183 NL80211_BSS_STATUS_IBSS_JOINED);
3184 break;
3185 default:
3186 break;
3187 }
3188
Johannes Berg2a519312009-02-10 21:25:55 +01003189 nla_nest_end(msg, bss);
3190
3191 return genlmsg_end(msg, hdr);
3192
3193 nla_put_failure:
3194 genlmsg_cancel(msg, hdr);
3195 return -EMSGSIZE;
3196}
3197
3198static int nl80211_dump_scan(struct sk_buff *skb,
3199 struct netlink_callback *cb)
3200{
Johannes Berg48ab9052009-07-10 18:42:31 +02003201 struct cfg80211_registered_device *rdev;
3202 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003203 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003204 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003205 int ifidx = cb->args[0];
3206 int start = cb->args[1], idx = 0;
3207 int err;
3208
Holger Schuriga0438972009-11-11 11:30:02 +01003209 if (!ifidx)
3210 ifidx = nl80211_get_ifidx(cb);
3211 if (ifidx < 0)
3212 return ifidx;
3213 cb->args[0] = ifidx;
Johannes Berg2a519312009-02-10 21:25:55 +01003214
Johannes Berg463d0182009-07-14 00:33:35 +02003215 dev = dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003216 if (!dev)
Johannes Berg2a519312009-02-10 21:25:55 +01003217 return -ENODEV;
3218
Johannes Berg463d0182009-07-14 00:33:35 +02003219 rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003220 if (IS_ERR(rdev)) {
3221 err = PTR_ERR(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003222 goto out_put_netdev;
3223 }
3224
Johannes Berg48ab9052009-07-10 18:42:31 +02003225 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003226
Johannes Berg48ab9052009-07-10 18:42:31 +02003227 wdev_lock(wdev);
3228 spin_lock_bh(&rdev->bss_lock);
3229 cfg80211_bss_expire(rdev);
3230
3231 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003232 if (++idx <= start)
3233 continue;
3234 if (nl80211_send_bss(skb,
3235 NETLINK_CB(cb->skb).pid,
3236 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003237 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003238 idx--;
3239 goto out;
3240 }
3241 }
3242
3243 out:
Johannes Berg48ab9052009-07-10 18:42:31 +02003244 spin_unlock_bh(&rdev->bss_lock);
3245 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003246
3247 cb->args[1] = idx;
3248 err = skb->len;
Johannes Berg48ab9052009-07-10 18:42:31 +02003249 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003250 out_put_netdev:
Johannes Berg48ab9052009-07-10 18:42:31 +02003251 dev_put(dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003252
3253 return err;
3254}
3255
Holger Schurig61fa7132009-11-11 12:25:40 +01003256static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3257 int flags, struct net_device *dev,
3258 struct survey_info *survey)
3259{
3260 void *hdr;
3261 struct nlattr *infoattr;
3262
3263 /* Survey without a channel doesn't make sense */
3264 if (!survey->channel)
3265 return -EINVAL;
3266
3267 hdr = nl80211hdr_put(msg, pid, seq, flags,
3268 NL80211_CMD_NEW_SURVEY_RESULTS);
3269 if (!hdr)
3270 return -ENOMEM;
3271
3272 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3273
3274 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3275 if (!infoattr)
3276 goto nla_put_failure;
3277
3278 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3279 survey->channel->center_freq);
3280 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3281 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3282 survey->noise);
3283
3284 nla_nest_end(msg, infoattr);
3285
3286 return genlmsg_end(msg, hdr);
3287
3288 nla_put_failure:
3289 genlmsg_cancel(msg, hdr);
3290 return -EMSGSIZE;
3291}
3292
3293static int nl80211_dump_survey(struct sk_buff *skb,
3294 struct netlink_callback *cb)
3295{
3296 struct survey_info survey;
3297 struct cfg80211_registered_device *dev;
3298 struct net_device *netdev;
3299 int ifidx = cb->args[0];
3300 int survey_idx = cb->args[1];
3301 int res;
3302
3303 if (!ifidx)
3304 ifidx = nl80211_get_ifidx(cb);
3305 if (ifidx < 0)
3306 return ifidx;
3307 cb->args[0] = ifidx;
3308
3309 rtnl_lock();
3310
3311 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3312 if (!netdev) {
3313 res = -ENODEV;
3314 goto out_rtnl;
3315 }
3316
3317 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
3318 if (IS_ERR(dev)) {
3319 res = PTR_ERR(dev);
3320 goto out_rtnl;
3321 }
3322
3323 if (!dev->ops->dump_survey) {
3324 res = -EOPNOTSUPP;
3325 goto out_err;
3326 }
3327
3328 while (1) {
3329 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3330 &survey);
3331 if (res == -ENOENT)
3332 break;
3333 if (res)
3334 goto out_err;
3335
3336 if (nl80211_send_survey(skb,
3337 NETLINK_CB(cb->skb).pid,
3338 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3339 netdev,
3340 &survey) < 0)
3341 goto out;
3342 survey_idx++;
3343 }
3344
3345 out:
3346 cb->args[1] = survey_idx;
3347 res = skb->len;
3348 out_err:
3349 cfg80211_unlock_rdev(dev);
3350 out_rtnl:
3351 rtnl_unlock();
3352
3353 return res;
3354}
3355
Jouni Malinen255e7372009-03-20 21:21:17 +02003356static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3357{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003358 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003359}
3360
Samuel Ortizb23aa672009-07-01 21:26:54 +02003361static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3362{
3363 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3364 NL80211_WPA_VERSION_2));
3365}
3366
3367static bool nl80211_valid_akm_suite(u32 akm)
3368{
3369 return akm == WLAN_AKM_SUITE_8021X ||
3370 akm == WLAN_AKM_SUITE_PSK;
3371}
3372
3373static bool nl80211_valid_cipher_suite(u32 cipher)
3374{
3375 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3376 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3377 cipher == WLAN_CIPHER_SUITE_TKIP ||
3378 cipher == WLAN_CIPHER_SUITE_CCMP ||
3379 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3380}
3381
3382
Jouni Malinen636a5d32009-03-19 13:39:22 +02003383static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3384{
Johannes Berg79c97e92009-07-07 03:56:12 +02003385 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003386 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003387 struct ieee80211_channel *chan;
3388 const u8 *bssid, *ssid, *ie = NULL;
3389 int err, ssid_len, ie_len = 0;
3390 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003391 struct key_parse key;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003392
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003393 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3394 return -EINVAL;
3395
3396 if (!info->attrs[NL80211_ATTR_MAC])
3397 return -EINVAL;
3398
Jouni Malinen17780922009-03-27 20:52:47 +02003399 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3400 return -EINVAL;
3401
Johannes Berg19957bb2009-07-02 17:20:43 +02003402 if (!info->attrs[NL80211_ATTR_SSID])
3403 return -EINVAL;
3404
3405 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3406 return -EINVAL;
3407
Johannes Bergfffd0932009-07-08 14:22:54 +02003408 err = nl80211_parse_key(info, &key);
3409 if (err)
3410 return err;
3411
3412 if (key.idx >= 0) {
3413 if (!key.p.key || !key.p.key_len)
3414 return -EINVAL;
3415 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3416 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3417 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3418 key.p.key_len != WLAN_KEY_LEN_WEP104))
3419 return -EINVAL;
3420 if (key.idx > 4)
3421 return -EINVAL;
3422 } else {
3423 key.p.key_len = 0;
3424 key.p.key = NULL;
3425 }
3426
Jouni Malinen636a5d32009-03-19 13:39:22 +02003427 rtnl_lock();
3428
Johannes Berg463d0182009-07-14 00:33:35 +02003429 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003430 if (err)
3431 goto unlock_rtnl;
3432
Johannes Berg79c97e92009-07-07 03:56:12 +02003433 if (!rdev->ops->auth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003434 err = -EOPNOTSUPP;
3435 goto out;
3436 }
3437
Jouni Malineneec60b02009-03-20 21:21:19 +02003438 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3439 err = -EOPNOTSUPP;
3440 goto out;
3441 }
3442
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003443 if (!netif_running(dev)) {
3444 err = -ENETDOWN;
3445 goto out;
3446 }
3447
Johannes Berg19957bb2009-07-02 17:20:43 +02003448 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003449 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003450 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3451 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
Jouni Malinen17780922009-03-27 20:52:47 +02003452 err = -EINVAL;
3453 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003454 }
3455
Johannes Berg19957bb2009-07-02 17:20:43 +02003456 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3457 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3458
3459 if (info->attrs[NL80211_ATTR_IE]) {
3460 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3461 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3462 }
3463
3464 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3465 if (!nl80211_valid_auth_type(auth_type)) {
3466 err = -EINVAL;
3467 goto out;
3468 }
3469
Johannes Berg79c97e92009-07-07 03:56:12 +02003470 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +02003471 ssid, ssid_len, ie, ie_len,
3472 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003473
3474out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003475 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003476 dev_put(dev);
3477unlock_rtnl:
3478 rtnl_unlock();
3479 return err;
3480}
3481
Samuel Ortizb23aa672009-07-01 21:26:54 +02003482static int nl80211_crypto_settings(struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003483 struct cfg80211_crypto_settings *settings,
3484 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003485{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003486 memset(settings, 0, sizeof(*settings));
3487
Samuel Ortizb23aa672009-07-01 21:26:54 +02003488 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3489
3490 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3491 void *data;
3492 int len, i;
3493
3494 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3495 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3496 settings->n_ciphers_pairwise = len / sizeof(u32);
3497
3498 if (len % sizeof(u32))
3499 return -EINVAL;
3500
Johannes Berg3dc27d22009-07-02 21:36:37 +02003501 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003502 return -EINVAL;
3503
3504 memcpy(settings->ciphers_pairwise, data, len);
3505
3506 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3507 if (!nl80211_valid_cipher_suite(
3508 settings->ciphers_pairwise[i]))
3509 return -EINVAL;
3510 }
3511
3512 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3513 settings->cipher_group =
3514 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3515 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3516 return -EINVAL;
3517 }
3518
3519 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3520 settings->wpa_versions =
3521 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3522 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3523 return -EINVAL;
3524 }
3525
3526 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3527 void *data;
3528 int len, i;
3529
3530 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3531 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3532 settings->n_akm_suites = len / sizeof(u32);
3533
3534 if (len % sizeof(u32))
3535 return -EINVAL;
3536
3537 memcpy(settings->akm_suites, data, len);
3538
3539 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3540 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3541 return -EINVAL;
3542 }
3543
3544 return 0;
3545}
3546
Jouni Malinen636a5d32009-03-19 13:39:22 +02003547static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3548{
Johannes Berg19957bb2009-07-02 17:20:43 +02003549 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003550 struct net_device *dev;
Jouni Malinen27e310c2010-01-19 10:53:30 -08003551 struct wireless_dev *wdev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003552 struct cfg80211_crypto_settings crypto;
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003553 struct ieee80211_channel *chan, *fixedchan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003554 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003555 int err, ssid_len, ie_len = 0;
3556 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003557
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003558 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3559 return -EINVAL;
3560
3561 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003562 !info->attrs[NL80211_ATTR_SSID] ||
3563 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003564 return -EINVAL;
3565
Jouni Malinen636a5d32009-03-19 13:39:22 +02003566 rtnl_lock();
3567
Johannes Berg463d0182009-07-14 00:33:35 +02003568 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003569 if (err)
3570 goto unlock_rtnl;
3571
Johannes Berg19957bb2009-07-02 17:20:43 +02003572 if (!rdev->ops->assoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003573 err = -EOPNOTSUPP;
3574 goto out;
3575 }
3576
Jouni Malineneec60b02009-03-20 21:21:19 +02003577 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3578 err = -EOPNOTSUPP;
3579 goto out;
3580 }
3581
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003582 if (!netif_running(dev)) {
3583 err = -ENETDOWN;
3584 goto out;
3585 }
3586
Johannes Berg19957bb2009-07-02 17:20:43 +02003587 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003588
Johannes Berg19957bb2009-07-02 17:20:43 +02003589 chan = ieee80211_get_channel(&rdev->wiphy,
3590 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3591 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3592 err = -EINVAL;
3593 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003594 }
3595
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003596 mutex_lock(&rdev->devlist_mtx);
Jouni Malinen27e310c2010-01-19 10:53:30 -08003597 wdev = dev->ieee80211_ptr;
3598 fixedchan = rdev_fixed_channel(rdev, wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003599 if (fixedchan && chan != fixedchan) {
3600 err = -EBUSY;
3601 mutex_unlock(&rdev->devlist_mtx);
3602 goto out;
3603 }
3604 mutex_unlock(&rdev->devlist_mtx);
3605
Johannes Berg19957bb2009-07-02 17:20:43 +02003606 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3607 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003608
3609 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003610 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3611 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003612 }
3613
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003614 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003615 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003616 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003617 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003618 use_mfp = true;
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003619 else if (mfp != NL80211_MFP_NO) {
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003620 err = -EINVAL;
3621 goto out;
3622 }
3623 }
3624
Johannes Berg3e5d7642009-07-07 14:37:26 +02003625 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3626 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3627
Johannes Berg3dc27d22009-07-02 21:36:37 +02003628 err = nl80211_crypto_settings(info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003629 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003630 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3631 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003632 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003633
3634out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003635 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003636 dev_put(dev);
3637unlock_rtnl:
3638 rtnl_unlock();
3639 return err;
3640}
3641
3642static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3643{
Johannes Berg79c97e92009-07-07 03:56:12 +02003644 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003645 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003646 const u8 *ie = NULL, *bssid;
3647 int err, ie_len = 0;
3648 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003649
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003650 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3651 return -EINVAL;
3652
3653 if (!info->attrs[NL80211_ATTR_MAC])
3654 return -EINVAL;
3655
3656 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3657 return -EINVAL;
3658
Jouni Malinen636a5d32009-03-19 13:39:22 +02003659 rtnl_lock();
3660
Johannes Berg463d0182009-07-14 00:33:35 +02003661 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003662 if (err)
3663 goto unlock_rtnl;
3664
Johannes Berg79c97e92009-07-07 03:56:12 +02003665 if (!rdev->ops->deauth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003666 err = -EOPNOTSUPP;
3667 goto out;
3668 }
3669
Jouni Malineneec60b02009-03-20 21:21:19 +02003670 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3671 err = -EOPNOTSUPP;
3672 goto out;
3673 }
3674
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003675 if (!netif_running(dev)) {
3676 err = -ENETDOWN;
3677 goto out;
3678 }
3679
Johannes Berg19957bb2009-07-02 17:20:43 +02003680 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003681
Johannes Berg19957bb2009-07-02 17:20:43 +02003682 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3683 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003684 /* Reason Code 0 is reserved */
3685 err = -EINVAL;
3686 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003687 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003688
3689 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003690 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3691 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003692 }
3693
Johannes Berg79c97e92009-07-07 03:56:12 +02003694 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003695
3696out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003697 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003698 dev_put(dev);
3699unlock_rtnl:
3700 rtnl_unlock();
3701 return err;
3702}
3703
3704static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3705{
Johannes Berg79c97e92009-07-07 03:56:12 +02003706 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003707 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003708 const u8 *ie = NULL, *bssid;
3709 int err, ie_len = 0;
3710 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003711
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003712 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3713 return -EINVAL;
3714
3715 if (!info->attrs[NL80211_ATTR_MAC])
3716 return -EINVAL;
3717
3718 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3719 return -EINVAL;
3720
Jouni Malinen636a5d32009-03-19 13:39:22 +02003721 rtnl_lock();
3722
Johannes Berg463d0182009-07-14 00:33:35 +02003723 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003724 if (err)
3725 goto unlock_rtnl;
3726
Johannes Berg79c97e92009-07-07 03:56:12 +02003727 if (!rdev->ops->disassoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003728 err = -EOPNOTSUPP;
3729 goto out;
3730 }
3731
Jouni Malineneec60b02009-03-20 21:21:19 +02003732 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3733 err = -EOPNOTSUPP;
3734 goto out;
3735 }
3736
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003737 if (!netif_running(dev)) {
3738 err = -ENETDOWN;
3739 goto out;
3740 }
3741
Johannes Berg19957bb2009-07-02 17:20:43 +02003742 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003743
Johannes Berg19957bb2009-07-02 17:20:43 +02003744 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3745 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003746 /* Reason Code 0 is reserved */
3747 err = -EINVAL;
3748 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003749 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003750
3751 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003752 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3753 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003754 }
3755
Johannes Berg79c97e92009-07-07 03:56:12 +02003756 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003757
3758out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003759 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003760 dev_put(dev);
3761unlock_rtnl:
3762 rtnl_unlock();
3763 return err;
3764}
3765
Johannes Berg04a773a2009-04-19 21:24:32 +02003766static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3767{
Johannes Berg79c97e92009-07-07 03:56:12 +02003768 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003769 struct net_device *dev;
3770 struct cfg80211_ibss_params ibss;
3771 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003772 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003773 int err;
3774
Johannes Berg8e30bc52009-04-22 17:45:38 +02003775 memset(&ibss, 0, sizeof(ibss));
3776
Johannes Berg04a773a2009-04-19 21:24:32 +02003777 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3778 return -EINVAL;
3779
3780 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3781 !info->attrs[NL80211_ATTR_SSID] ||
3782 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3783 return -EINVAL;
3784
Johannes Berg8e30bc52009-04-22 17:45:38 +02003785 ibss.beacon_interval = 100;
3786
3787 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3788 ibss.beacon_interval =
3789 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3790 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3791 return -EINVAL;
3792 }
3793
Johannes Berg04a773a2009-04-19 21:24:32 +02003794 rtnl_lock();
3795
Johannes Berg463d0182009-07-14 00:33:35 +02003796 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003797 if (err)
3798 goto unlock_rtnl;
3799
Johannes Berg79c97e92009-07-07 03:56:12 +02003800 if (!rdev->ops->join_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003801 err = -EOPNOTSUPP;
3802 goto out;
3803 }
3804
3805 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3806 err = -EOPNOTSUPP;
3807 goto out;
3808 }
3809
3810 if (!netif_running(dev)) {
3811 err = -ENETDOWN;
3812 goto out;
3813 }
3814
Johannes Berg79c97e92009-07-07 03:56:12 +02003815 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003816
3817 if (info->attrs[NL80211_ATTR_MAC])
3818 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3819 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3820 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3821
3822 if (info->attrs[NL80211_ATTR_IE]) {
3823 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3824 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3825 }
3826
3827 ibss.channel = ieee80211_get_channel(wiphy,
3828 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3829 if (!ibss.channel ||
3830 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3831 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3832 err = -EINVAL;
3833 goto out;
3834 }
3835
3836 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003837 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003838
Johannes Bergfffd0932009-07-08 14:22:54 +02003839 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3840 connkeys = nl80211_parse_connkeys(rdev,
3841 info->attrs[NL80211_ATTR_KEYS]);
3842 if (IS_ERR(connkeys)) {
3843 err = PTR_ERR(connkeys);
3844 connkeys = NULL;
3845 goto out;
3846 }
3847 }
3848
3849 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003850
3851out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003852 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003853 dev_put(dev);
3854unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02003855 if (err)
3856 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003857 rtnl_unlock();
3858 return err;
3859}
3860
3861static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3862{
Johannes Berg79c97e92009-07-07 03:56:12 +02003863 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003864 struct net_device *dev;
3865 int err;
3866
3867 rtnl_lock();
3868
Johannes Berg463d0182009-07-14 00:33:35 +02003869 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003870 if (err)
3871 goto unlock_rtnl;
3872
Johannes Berg79c97e92009-07-07 03:56:12 +02003873 if (!rdev->ops->leave_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003874 err = -EOPNOTSUPP;
3875 goto out;
3876 }
3877
3878 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3879 err = -EOPNOTSUPP;
3880 goto out;
3881 }
3882
3883 if (!netif_running(dev)) {
3884 err = -ENETDOWN;
3885 goto out;
3886 }
3887
Johannes Berg79c97e92009-07-07 03:56:12 +02003888 err = cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003889
3890out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003891 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003892 dev_put(dev);
3893unlock_rtnl:
3894 rtnl_unlock();
3895 return err;
3896}
3897
Johannes Bergaff89a92009-07-01 21:26:51 +02003898#ifdef CONFIG_NL80211_TESTMODE
3899static struct genl_multicast_group nl80211_testmode_mcgrp = {
3900 .name = "testmode",
3901};
3902
3903static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3904{
3905 struct cfg80211_registered_device *rdev;
3906 int err;
3907
3908 if (!info->attrs[NL80211_ATTR_TESTDATA])
3909 return -EINVAL;
3910
3911 rtnl_lock();
3912
3913 rdev = cfg80211_get_dev_from_info(info);
3914 if (IS_ERR(rdev)) {
3915 err = PTR_ERR(rdev);
3916 goto unlock_rtnl;
3917 }
3918
3919 err = -EOPNOTSUPP;
3920 if (rdev->ops->testmode_cmd) {
3921 rdev->testmode_info = info;
3922 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3923 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3924 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3925 rdev->testmode_info = NULL;
3926 }
3927
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003928 cfg80211_unlock_rdev(rdev);
Johannes Bergaff89a92009-07-01 21:26:51 +02003929
3930 unlock_rtnl:
3931 rtnl_unlock();
3932 return err;
3933}
3934
3935static struct sk_buff *
3936__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3937 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3938{
3939 struct sk_buff *skb;
3940 void *hdr;
3941 struct nlattr *data;
3942
3943 skb = nlmsg_new(approxlen + 100, gfp);
3944 if (!skb)
3945 return NULL;
3946
3947 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3948 if (!hdr) {
3949 kfree_skb(skb);
3950 return NULL;
3951 }
3952
3953 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3954 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3955
3956 ((void **)skb->cb)[0] = rdev;
3957 ((void **)skb->cb)[1] = hdr;
3958 ((void **)skb->cb)[2] = data;
3959
3960 return skb;
3961
3962 nla_put_failure:
3963 kfree_skb(skb);
3964 return NULL;
3965}
3966
3967struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3968 int approxlen)
3969{
3970 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3971
3972 if (WARN_ON(!rdev->testmode_info))
3973 return NULL;
3974
3975 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3976 rdev->testmode_info->snd_pid,
3977 rdev->testmode_info->snd_seq,
3978 GFP_KERNEL);
3979}
3980EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3981
3982int cfg80211_testmode_reply(struct sk_buff *skb)
3983{
3984 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3985 void *hdr = ((void **)skb->cb)[1];
3986 struct nlattr *data = ((void **)skb->cb)[2];
3987
3988 if (WARN_ON(!rdev->testmode_info)) {
3989 kfree_skb(skb);
3990 return -EINVAL;
3991 }
3992
3993 nla_nest_end(skb, data);
3994 genlmsg_end(skb, hdr);
3995 return genlmsg_reply(skb, rdev->testmode_info);
3996}
3997EXPORT_SYMBOL(cfg80211_testmode_reply);
3998
3999struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4000 int approxlen, gfp_t gfp)
4001{
4002 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4003
4004 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4005}
4006EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4007
4008void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4009{
4010 void *hdr = ((void **)skb->cb)[1];
4011 struct nlattr *data = ((void **)skb->cb)[2];
4012
4013 nla_nest_end(skb, data);
4014 genlmsg_end(skb, hdr);
4015 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4016}
4017EXPORT_SYMBOL(cfg80211_testmode_event);
4018#endif
4019
Samuel Ortizb23aa672009-07-01 21:26:54 +02004020static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4021{
Johannes Berg79c97e92009-07-07 03:56:12 +02004022 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004023 struct net_device *dev;
4024 struct cfg80211_connect_params connect;
4025 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004026 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004027 int err;
4028
4029 memset(&connect, 0, sizeof(connect));
4030
4031 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4032 return -EINVAL;
4033
4034 if (!info->attrs[NL80211_ATTR_SSID] ||
4035 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4036 return -EINVAL;
4037
4038 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4039 connect.auth_type =
4040 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4041 if (!nl80211_valid_auth_type(connect.auth_type))
4042 return -EINVAL;
4043 } else
4044 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4045
4046 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4047
Johannes Berg3dc27d22009-07-02 21:36:37 +02004048 err = nl80211_crypto_settings(info, &connect.crypto,
4049 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004050 if (err)
4051 return err;
4052 rtnl_lock();
4053
Johannes Berg463d0182009-07-14 00:33:35 +02004054 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004055 if (err)
4056 goto unlock_rtnl;
4057
4058 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4059 err = -EOPNOTSUPP;
4060 goto out;
4061 }
4062
4063 if (!netif_running(dev)) {
4064 err = -ENETDOWN;
4065 goto out;
4066 }
4067
Johannes Berg79c97e92009-07-07 03:56:12 +02004068 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004069
Samuel Ortizb23aa672009-07-01 21:26:54 +02004070 if (info->attrs[NL80211_ATTR_MAC])
4071 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4072 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4073 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4074
4075 if (info->attrs[NL80211_ATTR_IE]) {
4076 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4077 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4078 }
4079
4080 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4081 connect.channel =
4082 ieee80211_get_channel(wiphy,
4083 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4084 if (!connect.channel ||
4085 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
4086 err = -EINVAL;
4087 goto out;
4088 }
4089 }
4090
Johannes Bergfffd0932009-07-08 14:22:54 +02004091 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4092 connkeys = nl80211_parse_connkeys(rdev,
4093 info->attrs[NL80211_ATTR_KEYS]);
4094 if (IS_ERR(connkeys)) {
4095 err = PTR_ERR(connkeys);
4096 connkeys = NULL;
4097 goto out;
4098 }
4099 }
4100
4101 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004102
4103out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004104 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004105 dev_put(dev);
4106unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02004107 if (err)
4108 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004109 rtnl_unlock();
4110 return err;
4111}
4112
4113static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4114{
Johannes Berg79c97e92009-07-07 03:56:12 +02004115 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004116 struct net_device *dev;
4117 int err;
4118 u16 reason;
4119
4120 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4121 reason = WLAN_REASON_DEAUTH_LEAVING;
4122 else
4123 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4124
4125 if (reason == 0)
4126 return -EINVAL;
4127
4128 rtnl_lock();
4129
Johannes Berg463d0182009-07-14 00:33:35 +02004130 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004131 if (err)
4132 goto unlock_rtnl;
4133
4134 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4135 err = -EOPNOTSUPP;
4136 goto out;
4137 }
4138
4139 if (!netif_running(dev)) {
4140 err = -ENETDOWN;
4141 goto out;
4142 }
4143
Johannes Berg79c97e92009-07-07 03:56:12 +02004144 err = cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004145
4146out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004147 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004148 dev_put(dev);
4149unlock_rtnl:
4150 rtnl_unlock();
4151 return err;
4152}
4153
Johannes Berg463d0182009-07-14 00:33:35 +02004154static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4155{
4156 struct cfg80211_registered_device *rdev;
4157 struct net *net;
4158 int err;
4159 u32 pid;
4160
4161 if (!info->attrs[NL80211_ATTR_PID])
4162 return -EINVAL;
4163
4164 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4165
4166 rtnl_lock();
4167
4168 rdev = cfg80211_get_dev_from_info(info);
4169 if (IS_ERR(rdev)) {
4170 err = PTR_ERR(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004171 goto out_rtnl;
Johannes Berg463d0182009-07-14 00:33:35 +02004172 }
4173
4174 net = get_net_ns_by_pid(pid);
4175 if (IS_ERR(net)) {
4176 err = PTR_ERR(net);
4177 goto out;
4178 }
4179
4180 err = 0;
4181
4182 /* check if anything to do */
4183 if (net_eq(wiphy_net(&rdev->wiphy), net))
4184 goto out_put_net;
4185
4186 err = cfg80211_switch_netns(rdev, net);
4187 out_put_net:
4188 put_net(net);
4189 out:
4190 cfg80211_unlock_rdev(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004191 out_rtnl:
Johannes Berg463d0182009-07-14 00:33:35 +02004192 rtnl_unlock();
4193 return err;
4194}
4195
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004196static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4197{
4198 struct cfg80211_registered_device *rdev;
4199 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4200 struct cfg80211_pmksa *pmksa) = NULL;
4201 int err;
4202 struct net_device *dev;
4203 struct cfg80211_pmksa pmksa;
4204
4205 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4206
4207 if (!info->attrs[NL80211_ATTR_MAC])
4208 return -EINVAL;
4209
4210 if (!info->attrs[NL80211_ATTR_PMKID])
4211 return -EINVAL;
4212
4213 rtnl_lock();
4214
4215 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4216 if (err)
4217 goto out_rtnl;
4218
4219 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4220 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4221
4222 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4223 err = -EOPNOTSUPP;
4224 goto out;
4225 }
4226
4227 switch (info->genlhdr->cmd) {
4228 case NL80211_CMD_SET_PMKSA:
4229 rdev_ops = rdev->ops->set_pmksa;
4230 break;
4231 case NL80211_CMD_DEL_PMKSA:
4232 rdev_ops = rdev->ops->del_pmksa;
4233 break;
4234 default:
4235 WARN_ON(1);
4236 break;
4237 }
4238
4239 if (!rdev_ops) {
4240 err = -EOPNOTSUPP;
4241 goto out;
4242 }
4243
4244 err = rdev_ops(&rdev->wiphy, dev, &pmksa);
4245
4246 out:
4247 cfg80211_unlock_rdev(rdev);
4248 dev_put(dev);
4249 out_rtnl:
4250 rtnl_unlock();
4251
4252 return err;
4253}
4254
4255static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4256{
4257 struct cfg80211_registered_device *rdev;
4258 int err;
4259 struct net_device *dev;
4260
4261 rtnl_lock();
4262
4263 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4264 if (err)
4265 goto out_rtnl;
4266
4267 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4268 err = -EOPNOTSUPP;
4269 goto out;
4270 }
4271
4272 if (!rdev->ops->flush_pmksa) {
4273 err = -EOPNOTSUPP;
4274 goto out;
4275 }
4276
4277 err = rdev->ops->flush_pmksa(&rdev->wiphy, dev);
4278
4279 out:
4280 cfg80211_unlock_rdev(rdev);
4281 dev_put(dev);
4282 out_rtnl:
4283 rtnl_unlock();
4284
4285 return err;
4286
4287}
4288
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004289static int nl80211_remain_on_channel(struct sk_buff *skb,
4290 struct genl_info *info)
4291{
4292 struct cfg80211_registered_device *rdev;
4293 struct net_device *dev;
4294 struct ieee80211_channel *chan;
4295 struct sk_buff *msg;
4296 void *hdr;
4297 u64 cookie;
4298 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4299 u32 freq, duration;
4300 int err;
4301
4302 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4303 !info->attrs[NL80211_ATTR_DURATION])
4304 return -EINVAL;
4305
4306 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4307
4308 /*
4309 * We should be on that channel for at least one jiffie,
4310 * and more than 5 seconds seems excessive.
4311 */
4312 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4313 return -EINVAL;
4314
4315 rtnl_lock();
4316
4317 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4318 if (err)
4319 goto unlock_rtnl;
4320
4321 if (!rdev->ops->remain_on_channel) {
4322 err = -EOPNOTSUPP;
4323 goto out;
4324 }
4325
4326 if (!netif_running(dev)) {
4327 err = -ENETDOWN;
4328 goto out;
4329 }
4330
4331 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4332 channel_type = nla_get_u32(
4333 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4334 if (channel_type != NL80211_CHAN_NO_HT &&
4335 channel_type != NL80211_CHAN_HT20 &&
4336 channel_type != NL80211_CHAN_HT40PLUS &&
4337 channel_type != NL80211_CHAN_HT40MINUS)
4338 err = -EINVAL;
4339 goto out;
4340 }
4341
4342 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4343 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4344 if (chan == NULL) {
4345 err = -EINVAL;
4346 goto out;
4347 }
4348
4349 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4350 if (!msg) {
4351 err = -ENOMEM;
4352 goto out;
4353 }
4354
4355 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4356 NL80211_CMD_REMAIN_ON_CHANNEL);
4357
4358 if (IS_ERR(hdr)) {
4359 err = PTR_ERR(hdr);
4360 goto free_msg;
4361 }
4362
4363 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4364 channel_type, duration, &cookie);
4365
4366 if (err)
4367 goto free_msg;
4368
4369 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4370
4371 genlmsg_end(msg, hdr);
4372 err = genlmsg_reply(msg, info);
4373 goto out;
4374
4375 nla_put_failure:
4376 err = -ENOBUFS;
4377 free_msg:
4378 nlmsg_free(msg);
4379 out:
4380 cfg80211_unlock_rdev(rdev);
4381 dev_put(dev);
4382 unlock_rtnl:
4383 rtnl_unlock();
4384 return err;
4385}
4386
4387static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4388 struct genl_info *info)
4389{
4390 struct cfg80211_registered_device *rdev;
4391 struct net_device *dev;
4392 u64 cookie;
4393 int err;
4394
4395 if (!info->attrs[NL80211_ATTR_COOKIE])
4396 return -EINVAL;
4397
4398 rtnl_lock();
4399
4400 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4401 if (err)
4402 goto unlock_rtnl;
4403
4404 if (!rdev->ops->cancel_remain_on_channel) {
4405 err = -EOPNOTSUPP;
4406 goto out;
4407 }
4408
4409 if (!netif_running(dev)) {
4410 err = -ENETDOWN;
4411 goto out;
4412 }
4413
4414 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4415
4416 err = rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
4417
4418 out:
4419 cfg80211_unlock_rdev(rdev);
4420 dev_put(dev);
4421 unlock_rtnl:
4422 rtnl_unlock();
4423 return err;
4424}
4425
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004426static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4427 u8 *rates, u8 rates_len)
4428{
4429 u8 i;
4430 u32 mask = 0;
4431
4432 for (i = 0; i < rates_len; i++) {
4433 int rate = (rates[i] & 0x7f) * 5;
4434 int ridx;
4435 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4436 struct ieee80211_rate *srate =
4437 &sband->bitrates[ridx];
4438 if (rate == srate->bitrate) {
4439 mask |= 1 << ridx;
4440 break;
4441 }
4442 }
4443 if (ridx == sband->n_bitrates)
4444 return 0; /* rate not found */
4445 }
4446
4447 return mask;
4448}
4449
4450static struct nla_policy
4451nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] __read_mostly = {
4452 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4453 .len = NL80211_MAX_SUPP_RATES },
4454};
4455
4456static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4457 struct genl_info *info)
4458{
4459 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4460 struct cfg80211_registered_device *rdev;
4461 struct cfg80211_bitrate_mask mask;
4462 int err, rem, i;
4463 struct net_device *dev;
4464 struct nlattr *tx_rates;
4465 struct ieee80211_supported_band *sband;
4466
4467 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4468 return -EINVAL;
4469
4470 rtnl_lock();
4471
4472 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4473 if (err)
4474 goto unlock_rtnl;
4475
4476 if (!rdev->ops->set_bitrate_mask) {
4477 err = -EOPNOTSUPP;
4478 goto unlock;
4479 }
4480
4481 memset(&mask, 0, sizeof(mask));
4482 /* Default to all rates enabled */
4483 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4484 sband = rdev->wiphy.bands[i];
4485 mask.control[i].legacy =
4486 sband ? (1 << sband->n_bitrates) - 1 : 0;
4487 }
4488
4489 /*
4490 * The nested attribute uses enum nl80211_band as the index. This maps
4491 * directly to the enum ieee80211_band values used in cfg80211.
4492 */
4493 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4494 {
4495 enum ieee80211_band band = nla_type(tx_rates);
4496 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
4497 err = -EINVAL;
4498 goto unlock;
4499 }
4500 sband = rdev->wiphy.bands[band];
4501 if (sband == NULL) {
4502 err = -EINVAL;
4503 goto unlock;
4504 }
4505 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4506 nla_len(tx_rates), nl80211_txattr_policy);
4507 if (tb[NL80211_TXRATE_LEGACY]) {
4508 mask.control[band].legacy = rateset_to_mask(
4509 sband,
4510 nla_data(tb[NL80211_TXRATE_LEGACY]),
4511 nla_len(tb[NL80211_TXRATE_LEGACY]));
4512 if (mask.control[band].legacy == 0) {
4513 err = -EINVAL;
4514 goto unlock;
4515 }
4516 }
4517 }
4518
4519 err = rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
4520
4521 unlock:
4522 dev_put(dev);
4523 cfg80211_unlock_rdev(rdev);
4524 unlock_rtnl:
4525 rtnl_unlock();
4526 return err;
4527}
4528
Johannes Berg55682962007-09-20 13:09:35 -04004529static struct genl_ops nl80211_ops[] = {
4530 {
4531 .cmd = NL80211_CMD_GET_WIPHY,
4532 .doit = nl80211_get_wiphy,
4533 .dumpit = nl80211_dump_wiphy,
4534 .policy = nl80211_policy,
4535 /* can be retrieved by unprivileged users */
4536 },
4537 {
4538 .cmd = NL80211_CMD_SET_WIPHY,
4539 .doit = nl80211_set_wiphy,
4540 .policy = nl80211_policy,
4541 .flags = GENL_ADMIN_PERM,
4542 },
4543 {
4544 .cmd = NL80211_CMD_GET_INTERFACE,
4545 .doit = nl80211_get_interface,
4546 .dumpit = nl80211_dump_interface,
4547 .policy = nl80211_policy,
4548 /* can be retrieved by unprivileged users */
4549 },
4550 {
4551 .cmd = NL80211_CMD_SET_INTERFACE,
4552 .doit = nl80211_set_interface,
4553 .policy = nl80211_policy,
4554 .flags = GENL_ADMIN_PERM,
4555 },
4556 {
4557 .cmd = NL80211_CMD_NEW_INTERFACE,
4558 .doit = nl80211_new_interface,
4559 .policy = nl80211_policy,
4560 .flags = GENL_ADMIN_PERM,
4561 },
4562 {
4563 .cmd = NL80211_CMD_DEL_INTERFACE,
4564 .doit = nl80211_del_interface,
4565 .policy = nl80211_policy,
4566 .flags = GENL_ADMIN_PERM,
4567 },
Johannes Berg41ade002007-12-19 02:03:29 +01004568 {
4569 .cmd = NL80211_CMD_GET_KEY,
4570 .doit = nl80211_get_key,
4571 .policy = nl80211_policy,
4572 .flags = GENL_ADMIN_PERM,
4573 },
4574 {
4575 .cmd = NL80211_CMD_SET_KEY,
4576 .doit = nl80211_set_key,
4577 .policy = nl80211_policy,
4578 .flags = GENL_ADMIN_PERM,
4579 },
4580 {
4581 .cmd = NL80211_CMD_NEW_KEY,
4582 .doit = nl80211_new_key,
4583 .policy = nl80211_policy,
4584 .flags = GENL_ADMIN_PERM,
4585 },
4586 {
4587 .cmd = NL80211_CMD_DEL_KEY,
4588 .doit = nl80211_del_key,
4589 .policy = nl80211_policy,
4590 .flags = GENL_ADMIN_PERM,
4591 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004592 {
4593 .cmd = NL80211_CMD_SET_BEACON,
4594 .policy = nl80211_policy,
4595 .flags = GENL_ADMIN_PERM,
4596 .doit = nl80211_addset_beacon,
4597 },
4598 {
4599 .cmd = NL80211_CMD_NEW_BEACON,
4600 .policy = nl80211_policy,
4601 .flags = GENL_ADMIN_PERM,
4602 .doit = nl80211_addset_beacon,
4603 },
4604 {
4605 .cmd = NL80211_CMD_DEL_BEACON,
4606 .policy = nl80211_policy,
4607 .flags = GENL_ADMIN_PERM,
4608 .doit = nl80211_del_beacon,
4609 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004610 {
4611 .cmd = NL80211_CMD_GET_STATION,
4612 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004613 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004614 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01004615 },
4616 {
4617 .cmd = NL80211_CMD_SET_STATION,
4618 .doit = nl80211_set_station,
4619 .policy = nl80211_policy,
4620 .flags = GENL_ADMIN_PERM,
4621 },
4622 {
4623 .cmd = NL80211_CMD_NEW_STATION,
4624 .doit = nl80211_new_station,
4625 .policy = nl80211_policy,
4626 .flags = GENL_ADMIN_PERM,
4627 },
4628 {
4629 .cmd = NL80211_CMD_DEL_STATION,
4630 .doit = nl80211_del_station,
4631 .policy = nl80211_policy,
4632 .flags = GENL_ADMIN_PERM,
4633 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004634 {
4635 .cmd = NL80211_CMD_GET_MPATH,
4636 .doit = nl80211_get_mpath,
4637 .dumpit = nl80211_dump_mpath,
4638 .policy = nl80211_policy,
4639 .flags = GENL_ADMIN_PERM,
4640 },
4641 {
4642 .cmd = NL80211_CMD_SET_MPATH,
4643 .doit = nl80211_set_mpath,
4644 .policy = nl80211_policy,
4645 .flags = GENL_ADMIN_PERM,
4646 },
4647 {
4648 .cmd = NL80211_CMD_NEW_MPATH,
4649 .doit = nl80211_new_mpath,
4650 .policy = nl80211_policy,
4651 .flags = GENL_ADMIN_PERM,
4652 },
4653 {
4654 .cmd = NL80211_CMD_DEL_MPATH,
4655 .doit = nl80211_del_mpath,
4656 .policy = nl80211_policy,
4657 .flags = GENL_ADMIN_PERM,
4658 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004659 {
4660 .cmd = NL80211_CMD_SET_BSS,
4661 .doit = nl80211_set_bss,
4662 .policy = nl80211_policy,
4663 .flags = GENL_ADMIN_PERM,
4664 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004665 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004666 .cmd = NL80211_CMD_GET_REG,
4667 .doit = nl80211_get_reg,
4668 .policy = nl80211_policy,
4669 /* can be retrieved by unprivileged users */
4670 },
4671 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004672 .cmd = NL80211_CMD_SET_REG,
4673 .doit = nl80211_set_reg,
4674 .policy = nl80211_policy,
4675 .flags = GENL_ADMIN_PERM,
4676 },
4677 {
4678 .cmd = NL80211_CMD_REQ_SET_REG,
4679 .doit = nl80211_req_set_reg,
4680 .policy = nl80211_policy,
4681 .flags = GENL_ADMIN_PERM,
4682 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004683 {
4684 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4685 .doit = nl80211_get_mesh_params,
4686 .policy = nl80211_policy,
4687 /* can be retrieved by unprivileged users */
4688 },
4689 {
4690 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4691 .doit = nl80211_set_mesh_params,
4692 .policy = nl80211_policy,
4693 .flags = GENL_ADMIN_PERM,
4694 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004695 {
Johannes Berg2a519312009-02-10 21:25:55 +01004696 .cmd = NL80211_CMD_TRIGGER_SCAN,
4697 .doit = nl80211_trigger_scan,
4698 .policy = nl80211_policy,
4699 .flags = GENL_ADMIN_PERM,
4700 },
4701 {
4702 .cmd = NL80211_CMD_GET_SCAN,
4703 .policy = nl80211_policy,
4704 .dumpit = nl80211_dump_scan,
4705 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004706 {
4707 .cmd = NL80211_CMD_AUTHENTICATE,
4708 .doit = nl80211_authenticate,
4709 .policy = nl80211_policy,
4710 .flags = GENL_ADMIN_PERM,
4711 },
4712 {
4713 .cmd = NL80211_CMD_ASSOCIATE,
4714 .doit = nl80211_associate,
4715 .policy = nl80211_policy,
4716 .flags = GENL_ADMIN_PERM,
4717 },
4718 {
4719 .cmd = NL80211_CMD_DEAUTHENTICATE,
4720 .doit = nl80211_deauthenticate,
4721 .policy = nl80211_policy,
4722 .flags = GENL_ADMIN_PERM,
4723 },
4724 {
4725 .cmd = NL80211_CMD_DISASSOCIATE,
4726 .doit = nl80211_disassociate,
4727 .policy = nl80211_policy,
4728 .flags = GENL_ADMIN_PERM,
4729 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004730 {
4731 .cmd = NL80211_CMD_JOIN_IBSS,
4732 .doit = nl80211_join_ibss,
4733 .policy = nl80211_policy,
4734 .flags = GENL_ADMIN_PERM,
4735 },
4736 {
4737 .cmd = NL80211_CMD_LEAVE_IBSS,
4738 .doit = nl80211_leave_ibss,
4739 .policy = nl80211_policy,
4740 .flags = GENL_ADMIN_PERM,
4741 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004742#ifdef CONFIG_NL80211_TESTMODE
4743 {
4744 .cmd = NL80211_CMD_TESTMODE,
4745 .doit = nl80211_testmode_do,
4746 .policy = nl80211_policy,
4747 .flags = GENL_ADMIN_PERM,
4748 },
4749#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004750 {
4751 .cmd = NL80211_CMD_CONNECT,
4752 .doit = nl80211_connect,
4753 .policy = nl80211_policy,
4754 .flags = GENL_ADMIN_PERM,
4755 },
4756 {
4757 .cmd = NL80211_CMD_DISCONNECT,
4758 .doit = nl80211_disconnect,
4759 .policy = nl80211_policy,
4760 .flags = GENL_ADMIN_PERM,
4761 },
Johannes Berg463d0182009-07-14 00:33:35 +02004762 {
4763 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4764 .doit = nl80211_wiphy_netns,
4765 .policy = nl80211_policy,
4766 .flags = GENL_ADMIN_PERM,
4767 },
Holger Schurig61fa7132009-11-11 12:25:40 +01004768 {
4769 .cmd = NL80211_CMD_GET_SURVEY,
4770 .policy = nl80211_policy,
4771 .dumpit = nl80211_dump_survey,
4772 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004773 {
4774 .cmd = NL80211_CMD_SET_PMKSA,
4775 .doit = nl80211_setdel_pmksa,
4776 .policy = nl80211_policy,
4777 .flags = GENL_ADMIN_PERM,
4778 },
4779 {
4780 .cmd = NL80211_CMD_DEL_PMKSA,
4781 .doit = nl80211_setdel_pmksa,
4782 .policy = nl80211_policy,
4783 .flags = GENL_ADMIN_PERM,
4784 },
4785 {
4786 .cmd = NL80211_CMD_FLUSH_PMKSA,
4787 .doit = nl80211_flush_pmksa,
4788 .policy = nl80211_policy,
4789 .flags = GENL_ADMIN_PERM,
4790 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004791 {
4792 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
4793 .doit = nl80211_remain_on_channel,
4794 .policy = nl80211_policy,
4795 .flags = GENL_ADMIN_PERM,
4796 },
4797 {
4798 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
4799 .doit = nl80211_cancel_remain_on_channel,
4800 .policy = nl80211_policy,
4801 .flags = GENL_ADMIN_PERM,
4802 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004803 {
4804 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
4805 .doit = nl80211_set_tx_bitrate_mask,
4806 .policy = nl80211_policy,
4807 .flags = GENL_ADMIN_PERM,
4808 },
Johannes Berg55682962007-09-20 13:09:35 -04004809};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004810
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004811static struct genl_multicast_group nl80211_mlme_mcgrp = {
4812 .name = "mlme",
4813};
Johannes Berg55682962007-09-20 13:09:35 -04004814
4815/* multicast groups */
4816static struct genl_multicast_group nl80211_config_mcgrp = {
4817 .name = "config",
4818};
Johannes Berg2a519312009-02-10 21:25:55 +01004819static struct genl_multicast_group nl80211_scan_mcgrp = {
4820 .name = "scan",
4821};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004822static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4823 .name = "regulatory",
4824};
Johannes Berg55682962007-09-20 13:09:35 -04004825
4826/* notification functions */
4827
4828void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4829{
4830 struct sk_buff *msg;
4831
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004832 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004833 if (!msg)
4834 return;
4835
4836 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4837 nlmsg_free(msg);
4838 return;
4839 }
4840
Johannes Berg463d0182009-07-14 00:33:35 +02004841 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4842 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004843}
4844
Johannes Berg362a4152009-05-24 16:43:15 +02004845static int nl80211_add_scan_req(struct sk_buff *msg,
4846 struct cfg80211_registered_device *rdev)
4847{
4848 struct cfg80211_scan_request *req = rdev->scan_req;
4849 struct nlattr *nest;
4850 int i;
4851
Johannes Berg667503d2009-07-07 03:56:11 +02004852 ASSERT_RDEV_LOCK(rdev);
4853
Johannes Berg362a4152009-05-24 16:43:15 +02004854 if (WARN_ON(!req))
4855 return 0;
4856
4857 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4858 if (!nest)
4859 goto nla_put_failure;
4860 for (i = 0; i < req->n_ssids; i++)
4861 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4862 nla_nest_end(msg, nest);
4863
4864 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4865 if (!nest)
4866 goto nla_put_failure;
4867 for (i = 0; i < req->n_channels; i++)
4868 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4869 nla_nest_end(msg, nest);
4870
4871 if (req->ie)
4872 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4873
4874 return 0;
4875 nla_put_failure:
4876 return -ENOBUFS;
4877}
4878
Johannes Berga538e2d2009-06-16 19:56:42 +02004879static int nl80211_send_scan_msg(struct sk_buff *msg,
4880 struct cfg80211_registered_device *rdev,
4881 struct net_device *netdev,
4882 u32 pid, u32 seq, int flags,
4883 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01004884{
4885 void *hdr;
4886
4887 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4888 if (!hdr)
4889 return -1;
4890
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05004891 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01004892 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4893
Johannes Berg362a4152009-05-24 16:43:15 +02004894 /* ignore errors and send incomplete event anyway */
4895 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004896
4897 return genlmsg_end(msg, hdr);
4898
4899 nla_put_failure:
4900 genlmsg_cancel(msg, hdr);
4901 return -EMSGSIZE;
4902}
4903
Johannes Berga538e2d2009-06-16 19:56:42 +02004904void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4905 struct net_device *netdev)
4906{
4907 struct sk_buff *msg;
4908
4909 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4910 if (!msg)
4911 return;
4912
4913 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4914 NL80211_CMD_TRIGGER_SCAN) < 0) {
4915 nlmsg_free(msg);
4916 return;
4917 }
4918
Johannes Berg463d0182009-07-14 00:33:35 +02004919 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4920 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02004921}
4922
Johannes Berg2a519312009-02-10 21:25:55 +01004923void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4924 struct net_device *netdev)
4925{
4926 struct sk_buff *msg;
4927
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004928 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004929 if (!msg)
4930 return;
4931
Johannes Berga538e2d2009-06-16 19:56:42 +02004932 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4933 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004934 nlmsg_free(msg);
4935 return;
4936 }
4937
Johannes Berg463d0182009-07-14 00:33:35 +02004938 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4939 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004940}
4941
4942void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4943 struct net_device *netdev)
4944{
4945 struct sk_buff *msg;
4946
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004947 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004948 if (!msg)
4949 return;
4950
Johannes Berga538e2d2009-06-16 19:56:42 +02004951 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4952 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004953 nlmsg_free(msg);
4954 return;
4955 }
4956
Johannes Berg463d0182009-07-14 00:33:35 +02004957 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4958 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004959}
4960
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004961/*
4962 * This can happen on global regulatory changes or device specific settings
4963 * based on custom world regulatory domains.
4964 */
4965void nl80211_send_reg_change_event(struct regulatory_request *request)
4966{
4967 struct sk_buff *msg;
4968 void *hdr;
4969
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004970 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004971 if (!msg)
4972 return;
4973
4974 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
4975 if (!hdr) {
4976 nlmsg_free(msg);
4977 return;
4978 }
4979
4980 /* Userspace can always count this one always being set */
4981 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
4982
4983 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
4984 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4985 NL80211_REGDOM_TYPE_WORLD);
4986 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
4987 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4988 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
4989 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
4990 request->intersect)
4991 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4992 NL80211_REGDOM_TYPE_INTERSECTION);
4993 else {
4994 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4995 NL80211_REGDOM_TYPE_COUNTRY);
4996 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
4997 }
4998
4999 if (wiphy_idx_valid(request->wiphy_idx))
5000 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5001
5002 if (genlmsg_end(msg, hdr) < 0) {
5003 nlmsg_free(msg);
5004 return;
5005 }
5006
Johannes Bergbc43b282009-07-25 10:54:13 +02005007 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005008 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005009 GFP_ATOMIC);
5010 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005011
5012 return;
5013
5014nla_put_failure:
5015 genlmsg_cancel(msg, hdr);
5016 nlmsg_free(msg);
5017}
5018
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005019static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5020 struct net_device *netdev,
5021 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005022 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005023{
5024 struct sk_buff *msg;
5025 void *hdr;
5026
Johannes Berge6d6e342009-07-01 21:26:47 +02005027 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005028 if (!msg)
5029 return;
5030
5031 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5032 if (!hdr) {
5033 nlmsg_free(msg);
5034 return;
5035 }
5036
5037 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5038 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5039 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5040
5041 if (genlmsg_end(msg, hdr) < 0) {
5042 nlmsg_free(msg);
5043 return;
5044 }
5045
Johannes Berg463d0182009-07-14 00:33:35 +02005046 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5047 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005048 return;
5049
5050 nla_put_failure:
5051 genlmsg_cancel(msg, hdr);
5052 nlmsg_free(msg);
5053}
5054
5055void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005056 struct net_device *netdev, const u8 *buf,
5057 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005058{
5059 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005060 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005061}
5062
5063void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5064 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005065 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005066{
Johannes Berge6d6e342009-07-01 21:26:47 +02005067 nl80211_send_mlme_event(rdev, netdev, buf, len,
5068 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005069}
5070
Jouni Malinen53b46b82009-03-27 20:53:56 +02005071void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005072 struct net_device *netdev, const u8 *buf,
5073 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005074{
5075 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005076 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005077}
5078
Jouni Malinen53b46b82009-03-27 20:53:56 +02005079void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5080 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005081 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005082{
5083 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005084 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005085}
5086
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005087static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5088 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005089 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005090{
5091 struct sk_buff *msg;
5092 void *hdr;
5093
Johannes Berge6d6e342009-07-01 21:26:47 +02005094 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005095 if (!msg)
5096 return;
5097
5098 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5099 if (!hdr) {
5100 nlmsg_free(msg);
5101 return;
5102 }
5103
5104 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5105 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5106 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5107 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5108
5109 if (genlmsg_end(msg, hdr) < 0) {
5110 nlmsg_free(msg);
5111 return;
5112 }
5113
Johannes Berg463d0182009-07-14 00:33:35 +02005114 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5115 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005116 return;
5117
5118 nla_put_failure:
5119 genlmsg_cancel(msg, hdr);
5120 nlmsg_free(msg);
5121}
5122
5123void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005124 struct net_device *netdev, const u8 *addr,
5125 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005126{
5127 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005128 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005129}
5130
5131void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005132 struct net_device *netdev, const u8 *addr,
5133 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005134{
Johannes Berge6d6e342009-07-01 21:26:47 +02005135 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5136 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005137}
5138
Samuel Ortizb23aa672009-07-01 21:26:54 +02005139void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5140 struct net_device *netdev, const u8 *bssid,
5141 const u8 *req_ie, size_t req_ie_len,
5142 const u8 *resp_ie, size_t resp_ie_len,
5143 u16 status, gfp_t gfp)
5144{
5145 struct sk_buff *msg;
5146 void *hdr;
5147
5148 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5149 if (!msg)
5150 return;
5151
5152 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5153 if (!hdr) {
5154 nlmsg_free(msg);
5155 return;
5156 }
5157
5158 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5159 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5160 if (bssid)
5161 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5162 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5163 if (req_ie)
5164 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5165 if (resp_ie)
5166 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5167
5168 if (genlmsg_end(msg, hdr) < 0) {
5169 nlmsg_free(msg);
5170 return;
5171 }
5172
Johannes Berg463d0182009-07-14 00:33:35 +02005173 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5174 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005175 return;
5176
5177 nla_put_failure:
5178 genlmsg_cancel(msg, hdr);
5179 nlmsg_free(msg);
5180
5181}
5182
5183void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5184 struct net_device *netdev, const u8 *bssid,
5185 const u8 *req_ie, size_t req_ie_len,
5186 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5187{
5188 struct sk_buff *msg;
5189 void *hdr;
5190
5191 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5192 if (!msg)
5193 return;
5194
5195 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5196 if (!hdr) {
5197 nlmsg_free(msg);
5198 return;
5199 }
5200
5201 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5202 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5203 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5204 if (req_ie)
5205 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5206 if (resp_ie)
5207 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5208
5209 if (genlmsg_end(msg, hdr) < 0) {
5210 nlmsg_free(msg);
5211 return;
5212 }
5213
Johannes Berg463d0182009-07-14 00:33:35 +02005214 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5215 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005216 return;
5217
5218 nla_put_failure:
5219 genlmsg_cancel(msg, hdr);
5220 nlmsg_free(msg);
5221
5222}
5223
5224void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5225 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005226 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005227{
5228 struct sk_buff *msg;
5229 void *hdr;
5230
Johannes Berg667503d2009-07-07 03:56:11 +02005231 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005232 if (!msg)
5233 return;
5234
5235 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5236 if (!hdr) {
5237 nlmsg_free(msg);
5238 return;
5239 }
5240
5241 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5242 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5243 if (from_ap && reason)
5244 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5245 if (from_ap)
5246 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5247 if (ie)
5248 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5249
5250 if (genlmsg_end(msg, hdr) < 0) {
5251 nlmsg_free(msg);
5252 return;
5253 }
5254
Johannes Berg463d0182009-07-14 00:33:35 +02005255 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5256 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005257 return;
5258
5259 nla_put_failure:
5260 genlmsg_cancel(msg, hdr);
5261 nlmsg_free(msg);
5262
5263}
5264
Johannes Berg04a773a2009-04-19 21:24:32 +02005265void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5266 struct net_device *netdev, const u8 *bssid,
5267 gfp_t gfp)
5268{
5269 struct sk_buff *msg;
5270 void *hdr;
5271
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005272 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005273 if (!msg)
5274 return;
5275
5276 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5277 if (!hdr) {
5278 nlmsg_free(msg);
5279 return;
5280 }
5281
5282 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5283 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5284 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5285
5286 if (genlmsg_end(msg, hdr) < 0) {
5287 nlmsg_free(msg);
5288 return;
5289 }
5290
Johannes Berg463d0182009-07-14 00:33:35 +02005291 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5292 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005293 return;
5294
5295 nla_put_failure:
5296 genlmsg_cancel(msg, hdr);
5297 nlmsg_free(msg);
5298}
5299
Jouni Malinena3b8b052009-03-27 21:59:49 +02005300void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5301 struct net_device *netdev, const u8 *addr,
5302 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005303 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005304{
5305 struct sk_buff *msg;
5306 void *hdr;
5307
Johannes Berge6d6e342009-07-01 21:26:47 +02005308 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005309 if (!msg)
5310 return;
5311
5312 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5313 if (!hdr) {
5314 nlmsg_free(msg);
5315 return;
5316 }
5317
5318 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5319 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5320 if (addr)
5321 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5322 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5323 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5324 if (tsc)
5325 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5326
5327 if (genlmsg_end(msg, hdr) < 0) {
5328 nlmsg_free(msg);
5329 return;
5330 }
5331
Johannes Berg463d0182009-07-14 00:33:35 +02005332 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5333 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005334 return;
5335
5336 nla_put_failure:
5337 genlmsg_cancel(msg, hdr);
5338 nlmsg_free(msg);
5339}
5340
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005341void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5342 struct ieee80211_channel *channel_before,
5343 struct ieee80211_channel *channel_after)
5344{
5345 struct sk_buff *msg;
5346 void *hdr;
5347 struct nlattr *nl_freq;
5348
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005349 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005350 if (!msg)
5351 return;
5352
5353 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5354 if (!hdr) {
5355 nlmsg_free(msg);
5356 return;
5357 }
5358
5359 /*
5360 * Since we are applying the beacon hint to a wiphy we know its
5361 * wiphy_idx is valid
5362 */
5363 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5364
5365 /* Before */
5366 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5367 if (!nl_freq)
5368 goto nla_put_failure;
5369 if (nl80211_msg_put_channel(msg, channel_before))
5370 goto nla_put_failure;
5371 nla_nest_end(msg, nl_freq);
5372
5373 /* After */
5374 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5375 if (!nl_freq)
5376 goto nla_put_failure;
5377 if (nl80211_msg_put_channel(msg, channel_after))
5378 goto nla_put_failure;
5379 nla_nest_end(msg, nl_freq);
5380
5381 if (genlmsg_end(msg, hdr) < 0) {
5382 nlmsg_free(msg);
5383 return;
5384 }
5385
Johannes Berg463d0182009-07-14 00:33:35 +02005386 rcu_read_lock();
5387 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5388 GFP_ATOMIC);
5389 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005390
5391 return;
5392
5393nla_put_failure:
5394 genlmsg_cancel(msg, hdr);
5395 nlmsg_free(msg);
5396}
5397
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005398static void nl80211_send_remain_on_chan_event(
5399 int cmd, struct cfg80211_registered_device *rdev,
5400 struct net_device *netdev, u64 cookie,
5401 struct ieee80211_channel *chan,
5402 enum nl80211_channel_type channel_type,
5403 unsigned int duration, gfp_t gfp)
5404{
5405 struct sk_buff *msg;
5406 void *hdr;
5407
5408 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5409 if (!msg)
5410 return;
5411
5412 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5413 if (!hdr) {
5414 nlmsg_free(msg);
5415 return;
5416 }
5417
5418 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5419 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5420 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5421 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5422 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5423
5424 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5425 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5426
5427 if (genlmsg_end(msg, hdr) < 0) {
5428 nlmsg_free(msg);
5429 return;
5430 }
5431
5432 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5433 nl80211_mlme_mcgrp.id, gfp);
5434 return;
5435
5436 nla_put_failure:
5437 genlmsg_cancel(msg, hdr);
5438 nlmsg_free(msg);
5439}
5440
5441void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5442 struct net_device *netdev, u64 cookie,
5443 struct ieee80211_channel *chan,
5444 enum nl80211_channel_type channel_type,
5445 unsigned int duration, gfp_t gfp)
5446{
5447 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5448 rdev, netdev, cookie, chan,
5449 channel_type, duration, gfp);
5450}
5451
5452void nl80211_send_remain_on_channel_cancel(
5453 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5454 u64 cookie, struct ieee80211_channel *chan,
5455 enum nl80211_channel_type channel_type, gfp_t gfp)
5456{
5457 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5458 rdev, netdev, cookie, chan,
5459 channel_type, 0, gfp);
5460}
5461
Johannes Berg98b62182009-12-23 13:15:44 +01005462void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5463 struct net_device *dev, const u8 *mac_addr,
5464 struct station_info *sinfo, gfp_t gfp)
5465{
5466 struct sk_buff *msg;
5467
5468 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5469 if (!msg)
5470 return;
5471
5472 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5473 nlmsg_free(msg);
5474 return;
5475 }
5476
5477 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5478 nl80211_mlme_mcgrp.id, gfp);
5479}
5480
Johannes Berg55682962007-09-20 13:09:35 -04005481/* initialisation/exit functions */
5482
5483int nl80211_init(void)
5484{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005485 int err;
Johannes Berg55682962007-09-20 13:09:35 -04005486
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005487 err = genl_register_family_with_ops(&nl80211_fam,
5488 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04005489 if (err)
5490 return err;
5491
Johannes Berg55682962007-09-20 13:09:35 -04005492 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5493 if (err)
5494 goto err_out;
5495
Johannes Berg2a519312009-02-10 21:25:55 +01005496 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5497 if (err)
5498 goto err_out;
5499
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005500 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5501 if (err)
5502 goto err_out;
5503
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005504 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5505 if (err)
5506 goto err_out;
5507
Johannes Bergaff89a92009-07-01 21:26:51 +02005508#ifdef CONFIG_NL80211_TESTMODE
5509 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5510 if (err)
5511 goto err_out;
5512#endif
5513
Johannes Berg55682962007-09-20 13:09:35 -04005514 return 0;
5515 err_out:
5516 genl_unregister_family(&nl80211_fam);
5517 return err;
5518}
5519
5520void nl80211_exit(void)
5521{
5522 genl_unregister_family(&nl80211_fam);
5523}