blob: 8c8e4eae6a17d6366368c93d194bc6a9806064ad [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 },
Johannes Berg55682962007-09-20 13:09:35 -040072
73 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
74 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
75 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010076
77 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020078 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010079
Johannes Bergb9454e82009-07-08 13:29:08 +020080 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010081 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
82 .len = WLAN_MAX_KEY_LEN },
83 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
84 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
85 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030086 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010087
88 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
89 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
90 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
91 .len = IEEE80211_MAX_DATA_LEN },
92 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
93 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010094 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
95 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
96 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
97 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
98 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010099 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100100 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200101 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100102 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
103 .len = IEEE80211_MAX_MESH_ID_LEN },
104 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300105
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700106 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
107 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
108
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300109 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
110 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
111 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200112 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300114
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700115 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
116
Jouni Malinen36aedc92008-08-25 11:58:58 +0300117 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
118 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200119
120 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
121 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
122 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100123 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
124 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200125
126 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
127 .len = IEEE80211_MAX_SSID_LEN },
128 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
129 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200130 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300131 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300132 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300133 [NL80211_ATTR_STA_FLAGS2] = {
134 .len = sizeof(struct nl80211_sta_flag_update),
135 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300136 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200137 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
138 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
139 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200140 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100141 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400142};
143
Johannes Bergb9454e82009-07-08 13:29:08 +0200144/* policy for the attributes */
145static struct nla_policy
146nl80211_key_policy[NL80211_KEY_MAX + 1] __read_mostly = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200147 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200148 [NL80211_KEY_IDX] = { .type = NLA_U8 },
149 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
150 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
151 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
152 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
153};
154
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100155/* IE validation */
156static bool is_valid_ie_attr(const struct nlattr *attr)
157{
158 const u8 *pos;
159 int len;
160
161 if (!attr)
162 return true;
163
164 pos = nla_data(attr);
165 len = nla_len(attr);
166
167 while (len) {
168 u8 elemlen;
169
170 if (len < 2)
171 return false;
172 len -= 2;
173
174 elemlen = pos[1];
175 if (elemlen > len)
176 return false;
177
178 len -= elemlen;
179 pos += 2 + elemlen;
180 }
181
182 return true;
183}
184
Johannes Berg55682962007-09-20 13:09:35 -0400185/* message building helper */
186static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
187 int flags, u8 cmd)
188{
189 /* since there is no private header just add the generic one */
190 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
191}
192
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400193static int nl80211_msg_put_channel(struct sk_buff *msg,
194 struct ieee80211_channel *chan)
195{
196 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
197 chan->center_freq);
198
199 if (chan->flags & IEEE80211_CHAN_DISABLED)
200 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
201 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
202 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
203 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
204 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
205 if (chan->flags & IEEE80211_CHAN_RADAR)
206 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
207
208 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
209 DBM_TO_MBM(chan->max_power));
210
211 return 0;
212
213 nla_put_failure:
214 return -ENOBUFS;
215}
216
Johannes Berg55682962007-09-20 13:09:35 -0400217/* netlink command implementations */
218
Johannes Bergb9454e82009-07-08 13:29:08 +0200219struct key_parse {
220 struct key_params p;
221 int idx;
222 bool def, defmgmt;
223};
224
225static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
226{
227 struct nlattr *tb[NL80211_KEY_MAX + 1];
228 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
229 nl80211_key_policy);
230 if (err)
231 return err;
232
233 k->def = !!tb[NL80211_KEY_DEFAULT];
234 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
235
236 if (tb[NL80211_KEY_IDX])
237 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
238
239 if (tb[NL80211_KEY_DATA]) {
240 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
241 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
242 }
243
244 if (tb[NL80211_KEY_SEQ]) {
245 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
246 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
247 }
248
249 if (tb[NL80211_KEY_CIPHER])
250 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
251
252 return 0;
253}
254
255static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
256{
257 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
258 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
259 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
260 }
261
262 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
263 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
264 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
265 }
266
267 if (info->attrs[NL80211_ATTR_KEY_IDX])
268 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
269
270 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
271 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
272
273 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
274 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
275
276 return 0;
277}
278
279static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
280{
281 int err;
282
283 memset(k, 0, sizeof(*k));
284 k->idx = -1;
285
286 if (info->attrs[NL80211_ATTR_KEY])
287 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
288 else
289 err = nl80211_parse_key_old(info, k);
290
291 if (err)
292 return err;
293
294 if (k->def && k->defmgmt)
295 return -EINVAL;
296
297 if (k->idx != -1) {
298 if (k->defmgmt) {
299 if (k->idx < 4 || k->idx > 5)
300 return -EINVAL;
301 } else if (k->def) {
302 if (k->idx < 0 || k->idx > 3)
303 return -EINVAL;
304 } else {
305 if (k->idx < 0 || k->idx > 5)
306 return -EINVAL;
307 }
308 }
309
310 return 0;
311}
312
Johannes Bergfffd0932009-07-08 14:22:54 +0200313static struct cfg80211_cached_keys *
314nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
315 struct nlattr *keys)
316{
317 struct key_parse parse;
318 struct nlattr *key;
319 struct cfg80211_cached_keys *result;
320 int rem, err, def = 0;
321
322 result = kzalloc(sizeof(*result), GFP_KERNEL);
323 if (!result)
324 return ERR_PTR(-ENOMEM);
325
326 result->def = -1;
327 result->defmgmt = -1;
328
329 nla_for_each_nested(key, keys, rem) {
330 memset(&parse, 0, sizeof(parse));
331 parse.idx = -1;
332
333 err = nl80211_parse_key_new(key, &parse);
334 if (err)
335 goto error;
336 err = -EINVAL;
337 if (!parse.p.key)
338 goto error;
339 if (parse.idx < 0 || parse.idx > 4)
340 goto error;
341 if (parse.def) {
342 if (def)
343 goto error;
344 def = 1;
345 result->def = parse.idx;
346 } else if (parse.defmgmt)
347 goto error;
348 err = cfg80211_validate_key_settings(rdev, &parse.p,
349 parse.idx, NULL);
350 if (err)
351 goto error;
352 result->params[parse.idx].cipher = parse.p.cipher;
353 result->params[parse.idx].key_len = parse.p.key_len;
354 result->params[parse.idx].key = result->data[parse.idx];
355 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
356 }
357
358 return result;
359 error:
360 kfree(result);
361 return ERR_PTR(err);
362}
363
364static int nl80211_key_allowed(struct wireless_dev *wdev)
365{
366 ASSERT_WDEV_LOCK(wdev);
367
368 if (!netif_running(wdev->netdev))
369 return -ENETDOWN;
370
371 switch (wdev->iftype) {
372 case NL80211_IFTYPE_AP:
373 case NL80211_IFTYPE_AP_VLAN:
374 break;
375 case NL80211_IFTYPE_ADHOC:
376 if (!wdev->current_bss)
377 return -ENOLINK;
378 break;
379 case NL80211_IFTYPE_STATION:
380 if (wdev->sme_state != CFG80211_SME_CONNECTED)
381 return -ENOLINK;
382 break;
383 default:
384 return -EINVAL;
385 }
386
387 return 0;
388}
389
Johannes Berg55682962007-09-20 13:09:35 -0400390static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
391 struct cfg80211_registered_device *dev)
392{
393 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100394 struct nlattr *nl_bands, *nl_band;
395 struct nlattr *nl_freqs, *nl_freq;
396 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700397 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100398 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100399 enum ieee80211_band band;
400 struct ieee80211_channel *chan;
401 struct ieee80211_rate *rate;
402 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700403 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400404
405 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
406 if (!hdr)
407 return -1;
408
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500409 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400410 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200411
Johannes Bergf5ea9122009-08-07 16:17:38 +0200412 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
413 cfg80211_rdev_list_generation);
414
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200415 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
416 dev->wiphy.retry_short);
417 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
418 dev->wiphy.retry_long);
419 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
420 dev->wiphy.frag_threshold);
421 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
422 dev->wiphy.rts_threshold);
423
Johannes Berg2a519312009-02-10 21:25:55 +0100424 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
425 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200426 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
427 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100428
Johannes Berg25e47c182009-04-02 20:14:06 +0200429 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
430 sizeof(u32) * dev->wiphy.n_cipher_suites,
431 dev->wiphy.cipher_suites);
432
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700433 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
434 if (!nl_modes)
435 goto nla_put_failure;
436
437 i = 0;
438 while (ifmodes) {
439 if (ifmodes & 1)
440 NLA_PUT_FLAG(msg, i);
441 ifmodes >>= 1;
442 i++;
443 }
444
445 nla_nest_end(msg, nl_modes);
446
Johannes Bergee688b002008-01-24 19:38:39 +0100447 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
448 if (!nl_bands)
449 goto nla_put_failure;
450
451 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
452 if (!dev->wiphy.bands[band])
453 continue;
454
455 nl_band = nla_nest_start(msg, band);
456 if (!nl_band)
457 goto nla_put_failure;
458
Johannes Bergd51626d2008-10-09 12:20:13 +0200459 /* add HT info */
460 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
461 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
462 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
463 &dev->wiphy.bands[band]->ht_cap.mcs);
464 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
465 dev->wiphy.bands[band]->ht_cap.cap);
466 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
467 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
468 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
469 dev->wiphy.bands[band]->ht_cap.ampdu_density);
470 }
471
Johannes Bergee688b002008-01-24 19:38:39 +0100472 /* add frequencies */
473 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
474 if (!nl_freqs)
475 goto nla_put_failure;
476
477 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
478 nl_freq = nla_nest_start(msg, i);
479 if (!nl_freq)
480 goto nla_put_failure;
481
482 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100483
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400484 if (nl80211_msg_put_channel(msg, chan))
485 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200486
Johannes Bergee688b002008-01-24 19:38:39 +0100487 nla_nest_end(msg, nl_freq);
488 }
489
490 nla_nest_end(msg, nl_freqs);
491
492 /* add bitrates */
493 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
494 if (!nl_rates)
495 goto nla_put_failure;
496
497 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
498 nl_rate = nla_nest_start(msg, i);
499 if (!nl_rate)
500 goto nla_put_failure;
501
502 rate = &dev->wiphy.bands[band]->bitrates[i];
503 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
504 rate->bitrate);
505 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
506 NLA_PUT_FLAG(msg,
507 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
508
509 nla_nest_end(msg, nl_rate);
510 }
511
512 nla_nest_end(msg, nl_rates);
513
514 nla_nest_end(msg, nl_band);
515 }
516 nla_nest_end(msg, nl_bands);
517
Johannes Berg8fdc6212009-03-14 09:34:01 +0100518 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
519 if (!nl_cmds)
520 goto nla_put_failure;
521
522 i = 0;
523#define CMD(op, n) \
524 do { \
525 if (dev->ops->op) { \
526 i++; \
527 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
528 } \
529 } while (0)
530
531 CMD(add_virtual_intf, NEW_INTERFACE);
532 CMD(change_virtual_intf, SET_INTERFACE);
533 CMD(add_key, NEW_KEY);
534 CMD(add_beacon, NEW_BEACON);
535 CMD(add_station, NEW_STATION);
536 CMD(add_mpath, NEW_MPATH);
537 CMD(set_mesh_params, SET_MESH_PARAMS);
538 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200539 CMD(auth, AUTHENTICATE);
540 CMD(assoc, ASSOCIATE);
541 CMD(deauth, DEAUTHENTICATE);
542 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200543 CMD(join_ibss, JOIN_IBSS);
Johannes Berg463d0182009-07-14 00:33:35 +0200544 if (dev->wiphy.netnsok) {
545 i++;
546 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
547 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100548
549#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200550
Johannes Berg6829c872009-07-02 09:13:27 +0200551 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200552 i++;
553 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
554 }
555
Johannes Berg6829c872009-07-02 09:13:27 +0200556 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200557 i++;
558 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
559 }
560
Johannes Berg8fdc6212009-03-14 09:34:01 +0100561 nla_nest_end(msg, nl_cmds);
562
Johannes Berg55682962007-09-20 13:09:35 -0400563 return genlmsg_end(msg, hdr);
564
565 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700566 genlmsg_cancel(msg, hdr);
567 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400568}
569
570static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
571{
572 int idx = 0;
573 int start = cb->args[0];
574 struct cfg80211_registered_device *dev;
575
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500576 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200577 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200578 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
579 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200580 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400581 continue;
582 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
583 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200584 dev) < 0) {
585 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400586 break;
Julius Volzb4637272008-07-08 14:02:19 +0200587 }
Johannes Berg55682962007-09-20 13:09:35 -0400588 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500589 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400590
591 cb->args[0] = idx;
592
593 return skb->len;
594}
595
596static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
597{
598 struct sk_buff *msg;
599 struct cfg80211_registered_device *dev;
600
601 dev = cfg80211_get_dev_from_info(info);
602 if (IS_ERR(dev))
603 return PTR_ERR(dev);
604
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700605 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400606 if (!msg)
607 goto out_err;
608
609 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
610 goto out_free;
611
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200612 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400613
Johannes Berg134e6372009-07-10 09:51:34 +0000614 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400615
616 out_free:
617 nlmsg_free(msg);
618 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200619 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400620 return -ENOBUFS;
621}
622
Jouni Malinen31888482008-10-30 16:59:24 +0200623static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
624 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
625 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
626 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
627 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
628 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
629};
630
631static int parse_txq_params(struct nlattr *tb[],
632 struct ieee80211_txq_params *txq_params)
633{
634 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
635 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
636 !tb[NL80211_TXQ_ATTR_AIFS])
637 return -EINVAL;
638
639 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
640 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
641 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
642 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
643 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
644
645 return 0;
646}
647
Johannes Berg55682962007-09-20 13:09:35 -0400648static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
649{
650 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200651 int result = 0, rem_txq_params = 0;
652 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200653 u32 changed;
654 u8 retry_short = 0, retry_long = 0;
655 u32 frag_threshold = 0, rts_threshold = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400656
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100657 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400658
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100659 mutex_lock(&cfg80211_mutex);
660
Johannes Berg79c97e92009-07-07 03:56:12 +0200661 rdev = __cfg80211_rdev_from_info(info);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100662 if (IS_ERR(rdev)) {
Jiri Slaby1f5fc702009-06-20 12:31:11 +0200663 mutex_unlock(&cfg80211_mutex);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100664 result = PTR_ERR(rdev);
665 goto unlock;
666 }
667
668 mutex_lock(&rdev->mtx);
669
670 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200671 result = cfg80211_dev_rename(
672 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100673
674 mutex_unlock(&cfg80211_mutex);
675
676 if (result)
677 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400678
Jouni Malinen31888482008-10-30 16:59:24 +0200679 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
680 struct ieee80211_txq_params txq_params;
681 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
682
683 if (!rdev->ops->set_txq_params) {
684 result = -EOPNOTSUPP;
685 goto bad_res;
686 }
687
688 nla_for_each_nested(nl_txq_params,
689 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
690 rem_txq_params) {
691 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
692 nla_data(nl_txq_params),
693 nla_len(nl_txq_params),
694 txq_params_policy);
695 result = parse_txq_params(tb, &txq_params);
696 if (result)
697 goto bad_res;
698
699 result = rdev->ops->set_txq_params(&rdev->wiphy,
700 &txq_params);
701 if (result)
702 goto bad_res;
703 }
704 }
705
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200706 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530707 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400708 u32 freq;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200709
Johannes Berg306d6112008-12-08 12:39:04 +0100710 result = -EINVAL;
711
Sujith094d05d2008-12-12 11:57:43 +0530712 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
713 channel_type = nla_get_u32(info->attrs[
714 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
715 if (channel_type != NL80211_CHAN_NO_HT &&
716 channel_type != NL80211_CHAN_HT20 &&
717 channel_type != NL80211_CHAN_HT40PLUS &&
718 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200719 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200720 }
721
722 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
Johannes Berg306d6112008-12-08 12:39:04 +0100723
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200724 mutex_lock(&rdev->devlist_mtx);
Johannes Berg4b181142009-08-08 11:03:58 +0200725 result = rdev_set_freq(rdev, NULL, freq, channel_type);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200726 mutex_unlock(&rdev->devlist_mtx);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200727 if (result)
728 goto bad_res;
729 }
730
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200731 changed = 0;
732
733 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
734 retry_short = nla_get_u8(
735 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
736 if (retry_short == 0) {
737 result = -EINVAL;
738 goto bad_res;
739 }
740 changed |= WIPHY_PARAM_RETRY_SHORT;
741 }
742
743 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
744 retry_long = nla_get_u8(
745 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
746 if (retry_long == 0) {
747 result = -EINVAL;
748 goto bad_res;
749 }
750 changed |= WIPHY_PARAM_RETRY_LONG;
751 }
752
753 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
754 frag_threshold = nla_get_u32(
755 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
756 if (frag_threshold < 256) {
757 result = -EINVAL;
758 goto bad_res;
759 }
760 if (frag_threshold != (u32) -1) {
761 /*
762 * Fragments (apart from the last one) are required to
763 * have even length. Make the fragmentation code
764 * simpler by stripping LSB should someone try to use
765 * odd threshold value.
766 */
767 frag_threshold &= ~0x1;
768 }
769 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
770 }
771
772 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
773 rts_threshold = nla_get_u32(
774 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
775 changed |= WIPHY_PARAM_RTS_THRESHOLD;
776 }
777
778 if (changed) {
779 u8 old_retry_short, old_retry_long;
780 u32 old_frag_threshold, old_rts_threshold;
781
782 if (!rdev->ops->set_wiphy_params) {
783 result = -EOPNOTSUPP;
784 goto bad_res;
785 }
786
787 old_retry_short = rdev->wiphy.retry_short;
788 old_retry_long = rdev->wiphy.retry_long;
789 old_frag_threshold = rdev->wiphy.frag_threshold;
790 old_rts_threshold = rdev->wiphy.rts_threshold;
791
792 if (changed & WIPHY_PARAM_RETRY_SHORT)
793 rdev->wiphy.retry_short = retry_short;
794 if (changed & WIPHY_PARAM_RETRY_LONG)
795 rdev->wiphy.retry_long = retry_long;
796 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
797 rdev->wiphy.frag_threshold = frag_threshold;
798 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
799 rdev->wiphy.rts_threshold = rts_threshold;
800
801 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
802 if (result) {
803 rdev->wiphy.retry_short = old_retry_short;
804 rdev->wiphy.retry_long = old_retry_long;
805 rdev->wiphy.frag_threshold = old_frag_threshold;
806 rdev->wiphy.rts_threshold = old_rts_threshold;
807 }
808 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200809
Johannes Berg306d6112008-12-08 12:39:04 +0100810 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100811 mutex_unlock(&rdev->mtx);
812 unlock:
813 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400814 return result;
815}
816
817
818static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200819 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400820 struct net_device *dev)
821{
822 void *hdr;
823
824 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
825 if (!hdr)
826 return -1;
827
828 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200829 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400830 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200831 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200832
833 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
834 rdev->devlist_generation ^
835 (cfg80211_rdev_list_generation << 2));
836
Johannes Berg55682962007-09-20 13:09:35 -0400837 return genlmsg_end(msg, hdr);
838
839 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700840 genlmsg_cancel(msg, hdr);
841 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400842}
843
844static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
845{
846 int wp_idx = 0;
847 int if_idx = 0;
848 int wp_start = cb->args[0];
849 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +0200850 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -0400851 struct wireless_dev *wdev;
852
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500853 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200854 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
855 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +0200856 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200857 if (wp_idx < wp_start) {
858 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400859 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200860 }
Johannes Berg55682962007-09-20 13:09:35 -0400861 if_idx = 0;
862
Johannes Bergf5ea9122009-08-07 16:17:38 +0200863 mutex_lock(&rdev->devlist_mtx);
864 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200865 if (if_idx < if_start) {
866 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400867 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200868 }
Johannes Berg55682962007-09-20 13:09:35 -0400869 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
870 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +0200871 rdev, wdev->netdev) < 0) {
872 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200873 goto out;
874 }
875 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400876 }
Johannes Bergf5ea9122009-08-07 16:17:38 +0200877 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200878
879 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400880 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200881 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500882 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400883
884 cb->args[0] = wp_idx;
885 cb->args[1] = if_idx;
886
887 return skb->len;
888}
889
890static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
891{
892 struct sk_buff *msg;
893 struct cfg80211_registered_device *dev;
894 struct net_device *netdev;
895 int err;
896
Johannes Berg463d0182009-07-14 00:33:35 +0200897 err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400898 if (err)
899 return err;
900
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700901 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400902 if (!msg)
903 goto out_err;
904
Johannes Bergd7264052009-04-19 16:23:20 +0200905 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
906 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -0400907 goto out_free;
908
909 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200910 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400911
Johannes Berg134e6372009-07-10 09:51:34 +0000912 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400913
914 out_free:
915 nlmsg_free(msg);
916 out_err:
917 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200918 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400919 return -ENOBUFS;
920}
921
Michael Wu66f7ac52008-01-31 19:48:22 +0100922static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
923 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
924 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
925 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
926 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
927 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
928};
929
930static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
931{
932 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
933 int flag;
934
935 *mntrflags = 0;
936
937 if (!nla)
938 return -EINVAL;
939
940 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
941 nla, mntr_flags_policy))
942 return -EINVAL;
943
944 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
945 if (flags[flag])
946 *mntrflags |= (1<<flag);
947
948 return 0;
949}
950
Johannes Berg55682962007-09-20 13:09:35 -0400951static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
952{
Johannes Berg79c97e92009-07-07 03:56:12 +0200953 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100954 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +0200955 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200956 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -0400957 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +0200958 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100959 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -0400960
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100961 memset(&params, 0, sizeof(params));
962
Johannes Berg3b858752009-03-12 09:55:09 +0100963 rtnl_lock();
964
Johannes Berg463d0182009-07-14 00:33:35 +0200965 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400966 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100967 goto unlock_rtnl;
968
Johannes Berg04a773a2009-04-19 21:24:32 +0200969 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -0400970
Johannes Berg723b0382008-09-16 20:22:09 +0200971 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100972 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +0200973 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100974 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200975 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100976 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +0200977 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100978 }
Johannes Berg723b0382008-09-16 20:22:09 +0200979 }
980
Johannes Berg92ffe052008-09-16 20:39:36 +0200981 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200982 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +0200983 err = -EINVAL;
984 goto unlock;
985 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100986 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
987 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100988 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100989 }
990
Felix Fietkau8b787642009-11-10 18:53:10 +0100991 if (info->attrs[NL80211_ATTR_4ADDR]) {
992 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
993 change = true;
994 } else {
995 params.use_4addr = -1;
996 }
997
Johannes Berg92ffe052008-09-16 20:39:36 +0200998 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200999 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001000 err = -EINVAL;
1001 goto unlock;
1002 }
1003 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1004 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001005 if (err)
1006 goto unlock;
1007
1008 flags = &_flags;
1009 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001010 }
Johannes Berg3b858752009-03-12 09:55:09 +01001011
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001012 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001013 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001014 else
1015 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001016
Johannes Berg55682962007-09-20 13:09:35 -04001017 unlock:
Johannes Berge36d56b2009-06-09 21:04:43 +02001018 dev_put(dev);
Johannes Berg79c97e92009-07-07 03:56:12 +02001019 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001020 unlock_rtnl:
1021 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001022 return err;
1023}
1024
1025static int nl80211_new_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 Berg55682962007-09-20 13:09:35 -04001029 int err;
1030 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001031 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001032
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001033 memset(&params, 0, sizeof(params));
1034
Johannes Berg55682962007-09-20 13:09:35 -04001035 if (!info->attrs[NL80211_ATTR_IFNAME])
1036 return -EINVAL;
1037
1038 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1039 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1040 if (type > NL80211_IFTYPE_MAX)
1041 return -EINVAL;
1042 }
1043
Johannes Berg3b858752009-03-12 09:55:09 +01001044 rtnl_lock();
1045
Johannes Berg79c97e92009-07-07 03:56:12 +02001046 rdev = cfg80211_get_dev_from_info(info);
1047 if (IS_ERR(rdev)) {
1048 err = PTR_ERR(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001049 goto unlock_rtnl;
1050 }
Johannes Berg55682962007-09-20 13:09:35 -04001051
Johannes Berg79c97e92009-07-07 03:56:12 +02001052 if (!rdev->ops->add_virtual_intf ||
1053 !(rdev->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -04001054 err = -EOPNOTSUPP;
1055 goto unlock;
1056 }
1057
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001058 if (type == NL80211_IFTYPE_MESH_POINT &&
1059 info->attrs[NL80211_ATTR_MESH_ID]) {
1060 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1061 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1062 }
1063
Felix Fietkau8b787642009-11-10 18:53:10 +01001064 if (info->attrs[NL80211_ATTR_4ADDR])
1065 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1066
Michael Wu66f7ac52008-01-31 19:48:22 +01001067 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1068 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1069 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001070 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001071 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001072 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001073
Johannes Berg55682962007-09-20 13:09:35 -04001074 unlock:
Johannes Berg79c97e92009-07-07 03:56:12 +02001075 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001076 unlock_rtnl:
1077 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001078 return err;
1079}
1080
1081static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1082{
Johannes Berg79c97e92009-07-07 03:56:12 +02001083 struct cfg80211_registered_device *rdev;
Johannes Berg463d0182009-07-14 00:33:35 +02001084 int err;
Johannes Berg55682962007-09-20 13:09:35 -04001085 struct net_device *dev;
1086
Johannes Berg3b858752009-03-12 09:55:09 +01001087 rtnl_lock();
1088
Johannes Berg463d0182009-07-14 00:33:35 +02001089 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001090 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001091 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -04001092
Johannes Berg79c97e92009-07-07 03:56:12 +02001093 if (!rdev->ops->del_virtual_intf) {
Johannes Berg55682962007-09-20 13:09:35 -04001094 err = -EOPNOTSUPP;
1095 goto out;
1096 }
1097
Johannes Berg463d0182009-07-14 00:33:35 +02001098 err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001099
1100 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001101 cfg80211_unlock_rdev(rdev);
Johannes Berg463d0182009-07-14 00:33:35 +02001102 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001103 unlock_rtnl:
1104 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001105 return err;
1106}
1107
Johannes Berg41ade002007-12-19 02:03:29 +01001108struct get_key_cookie {
1109 struct sk_buff *msg;
1110 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001111 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001112};
1113
1114static void get_key_callback(void *c, struct key_params *params)
1115{
Johannes Bergb9454e82009-07-08 13:29:08 +02001116 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001117 struct get_key_cookie *cookie = c;
1118
1119 if (params->key)
1120 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1121 params->key_len, params->key);
1122
1123 if (params->seq)
1124 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1125 params->seq_len, params->seq);
1126
1127 if (params->cipher)
1128 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1129 params->cipher);
1130
Johannes Bergb9454e82009-07-08 13:29:08 +02001131 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1132 if (!key)
1133 goto nla_put_failure;
1134
1135 if (params->key)
1136 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1137 params->key_len, params->key);
1138
1139 if (params->seq)
1140 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1141 params->seq_len, params->seq);
1142
1143 if (params->cipher)
1144 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1145 params->cipher);
1146
1147 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1148
1149 nla_nest_end(cookie->msg, key);
1150
Johannes Berg41ade002007-12-19 02:03:29 +01001151 return;
1152 nla_put_failure:
1153 cookie->error = 1;
1154}
1155
1156static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1157{
Johannes Berg79c97e92009-07-07 03:56:12 +02001158 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001159 int err;
1160 struct net_device *dev;
1161 u8 key_idx = 0;
1162 u8 *mac_addr = NULL;
1163 struct get_key_cookie cookie = {
1164 .error = 0,
1165 };
1166 void *hdr;
1167 struct sk_buff *msg;
1168
1169 if (info->attrs[NL80211_ATTR_KEY_IDX])
1170 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1171
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001172 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001173 return -EINVAL;
1174
1175 if (info->attrs[NL80211_ATTR_MAC])
1176 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1177
Johannes Berg3b858752009-03-12 09:55:09 +01001178 rtnl_lock();
1179
Johannes Berg463d0182009-07-14 00:33:35 +02001180 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001181 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001182 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001183
Johannes Berg79c97e92009-07-07 03:56:12 +02001184 if (!rdev->ops->get_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001185 err = -EOPNOTSUPP;
1186 goto out;
1187 }
1188
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001189 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +01001190 if (!msg) {
1191 err = -ENOMEM;
1192 goto out;
1193 }
1194
1195 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1196 NL80211_CMD_NEW_KEY);
1197
1198 if (IS_ERR(hdr)) {
1199 err = PTR_ERR(hdr);
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001200 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001201 }
1202
1203 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001204 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001205
1206 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1207 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1208 if (mac_addr)
1209 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1210
Johannes Berg79c97e92009-07-07 03:56:12 +02001211 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001212 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001213
1214 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001215 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001216
1217 if (cookie.error)
1218 goto nla_put_failure;
1219
1220 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00001221 err = genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001222 goto out;
1223
1224 nla_put_failure:
1225 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001226 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001227 nlmsg_free(msg);
1228 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001229 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001230 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001231 unlock_rtnl:
1232 rtnl_unlock();
1233
Johannes Berg41ade002007-12-19 02:03:29 +01001234 return err;
1235}
1236
1237static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1238{
Johannes Berg79c97e92009-07-07 03:56:12 +02001239 struct cfg80211_registered_device *rdev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001240 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001241 int err;
1242 struct net_device *dev;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001243 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1244 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001245
Johannes Bergb9454e82009-07-08 13:29:08 +02001246 err = nl80211_parse_key(info, &key);
1247 if (err)
1248 return err;
1249
1250 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001251 return -EINVAL;
1252
Johannes Bergb9454e82009-07-08 13:29:08 +02001253 /* only support setting default key */
1254 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001255 return -EINVAL;
1256
Johannes Berg3b858752009-03-12 09:55:09 +01001257 rtnl_lock();
1258
Johannes Berg463d0182009-07-14 00:33:35 +02001259 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001260 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001261 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001262
Johannes Bergb9454e82009-07-08 13:29:08 +02001263 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001264 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001265 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001266 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001267
1268 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001269 err = -EOPNOTSUPP;
1270 goto out;
1271 }
1272
Johannes Bergfffd0932009-07-08 14:22:54 +02001273 wdev_lock(dev->ieee80211_ptr);
1274 err = nl80211_key_allowed(dev->ieee80211_ptr);
1275 if (!err)
1276 err = func(&rdev->wiphy, dev, key.idx);
1277
Johannes Berg3d23e342009-09-29 23:27:28 +02001278#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001279 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001280 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001281 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001282 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001283 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001284 }
1285#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001286 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001287
1288 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001289 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001290 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001291
1292 unlock_rtnl:
1293 rtnl_unlock();
1294
Johannes Berg41ade002007-12-19 02:03:29 +01001295 return err;
1296}
1297
1298static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1299{
Johannes Berg79c97e92009-07-07 03:56:12 +02001300 struct cfg80211_registered_device *rdev;
Johannes Bergfffd0932009-07-08 14:22:54 +02001301 int err;
Johannes Berg41ade002007-12-19 02:03:29 +01001302 struct net_device *dev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001303 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001304 u8 *mac_addr = NULL;
1305
Johannes Bergb9454e82009-07-08 13:29:08 +02001306 err = nl80211_parse_key(info, &key);
1307 if (err)
1308 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001309
Johannes Bergb9454e82009-07-08 13:29:08 +02001310 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001311 return -EINVAL;
1312
Johannes Berg41ade002007-12-19 02:03:29 +01001313 if (info->attrs[NL80211_ATTR_MAC])
1314 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1315
Johannes Berg3b858752009-03-12 09:55:09 +01001316 rtnl_lock();
1317
Johannes Berg463d0182009-07-14 00:33:35 +02001318 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001319 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001320 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001321
Johannes Berg79c97e92009-07-07 03:56:12 +02001322 if (!rdev->ops->add_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001323 err = -EOPNOTSUPP;
1324 goto out;
1325 }
1326
Johannes Bergfffd0932009-07-08 14:22:54 +02001327 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1328 err = -EINVAL;
1329 goto out;
1330 }
1331
1332 wdev_lock(dev->ieee80211_ptr);
1333 err = nl80211_key_allowed(dev->ieee80211_ptr);
1334 if (!err)
1335 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1336 mac_addr, &key.p);
1337 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001338
1339 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001340 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001341 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001342 unlock_rtnl:
1343 rtnl_unlock();
1344
Johannes Berg41ade002007-12-19 02:03:29 +01001345 return err;
1346}
1347
1348static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1349{
Johannes Berg79c97e92009-07-07 03:56:12 +02001350 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001351 int err;
1352 struct net_device *dev;
Johannes Berg41ade002007-12-19 02:03:29 +01001353 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001354 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001355
Johannes Bergb9454e82009-07-08 13:29:08 +02001356 err = nl80211_parse_key(info, &key);
1357 if (err)
1358 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001359
1360 if (info->attrs[NL80211_ATTR_MAC])
1361 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1362
Johannes Berg3b858752009-03-12 09:55:09 +01001363 rtnl_lock();
1364
Johannes Berg463d0182009-07-14 00:33:35 +02001365 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001366 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001367 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001368
Johannes Berg79c97e92009-07-07 03:56:12 +02001369 if (!rdev->ops->del_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001370 err = -EOPNOTSUPP;
1371 goto out;
1372 }
1373
Johannes Bergfffd0932009-07-08 14:22:54 +02001374 wdev_lock(dev->ieee80211_ptr);
1375 err = nl80211_key_allowed(dev->ieee80211_ptr);
1376 if (!err)
1377 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001378
Johannes Berg3d23e342009-09-29 23:27:28 +02001379#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001380 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001381 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001382 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001383 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001384 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1385 }
1386#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001387 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001388
Johannes Berg41ade002007-12-19 02:03:29 +01001389 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001390 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001391 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001392
1393 unlock_rtnl:
1394 rtnl_unlock();
1395
Johannes Berg41ade002007-12-19 02:03:29 +01001396 return err;
1397}
1398
Johannes Berged1b6cc2007-12-19 02:03:32 +01001399static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1400{
1401 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1402 struct beacon_parameters *info);
Johannes Berg79c97e92009-07-07 03:56:12 +02001403 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001404 int err;
1405 struct net_device *dev;
1406 struct beacon_parameters params;
1407 int haveinfo = 0;
1408
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001409 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1410 return -EINVAL;
1411
Johannes Berg3b858752009-03-12 09:55:09 +01001412 rtnl_lock();
1413
Johannes Berg463d0182009-07-14 00:33:35 +02001414 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001415 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001416 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001417
Jouni Malineneec60b02009-03-20 21:21:19 +02001418 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1419 err = -EOPNOTSUPP;
1420 goto out;
1421 }
1422
Johannes Berged1b6cc2007-12-19 02:03:32 +01001423 switch (info->genlhdr->cmd) {
1424 case NL80211_CMD_NEW_BEACON:
1425 /* these are required for NEW_BEACON */
1426 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1427 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1428 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1429 err = -EINVAL;
1430 goto out;
1431 }
1432
Johannes Berg79c97e92009-07-07 03:56:12 +02001433 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001434 break;
1435 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001436 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001437 break;
1438 default:
1439 WARN_ON(1);
1440 err = -EOPNOTSUPP;
1441 goto out;
1442 }
1443
1444 if (!call) {
1445 err = -EOPNOTSUPP;
1446 goto out;
1447 }
1448
1449 memset(&params, 0, sizeof(params));
1450
1451 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1452 params.interval =
1453 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1454 haveinfo = 1;
1455 }
1456
1457 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1458 params.dtim_period =
1459 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1460 haveinfo = 1;
1461 }
1462
1463 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1464 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1465 params.head_len =
1466 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1467 haveinfo = 1;
1468 }
1469
1470 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1471 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1472 params.tail_len =
1473 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1474 haveinfo = 1;
1475 }
1476
1477 if (!haveinfo) {
1478 err = -EINVAL;
1479 goto out;
1480 }
1481
Johannes Berg79c97e92009-07-07 03:56:12 +02001482 err = call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001483
1484 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001485 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001486 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001487 unlock_rtnl:
1488 rtnl_unlock();
1489
Johannes Berged1b6cc2007-12-19 02:03:32 +01001490 return err;
1491}
1492
1493static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1494{
Johannes Berg79c97e92009-07-07 03:56:12 +02001495 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001496 int err;
1497 struct net_device *dev;
1498
Johannes Berg3b858752009-03-12 09:55:09 +01001499 rtnl_lock();
1500
Johannes Berg463d0182009-07-14 00:33:35 +02001501 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001502 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001503 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001504
Johannes Berg79c97e92009-07-07 03:56:12 +02001505 if (!rdev->ops->del_beacon) {
Johannes Berged1b6cc2007-12-19 02:03:32 +01001506 err = -EOPNOTSUPP;
1507 goto out;
1508 }
1509
Jouni Malineneec60b02009-03-20 21:21:19 +02001510 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1511 err = -EOPNOTSUPP;
1512 goto out;
1513 }
Johannes Berg79c97e92009-07-07 03:56:12 +02001514 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001515
1516 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001517 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001518 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001519 unlock_rtnl:
1520 rtnl_unlock();
1521
Johannes Berged1b6cc2007-12-19 02:03:32 +01001522 return err;
1523}
1524
Johannes Berg5727ef12007-12-19 02:03:34 +01001525static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1526 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1527 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1528 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001529 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001530};
1531
Johannes Bergeccb8e82009-05-11 21:57:56 +03001532static int parse_station_flags(struct genl_info *info,
1533 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001534{
1535 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001536 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001537 int flag;
1538
Johannes Bergeccb8e82009-05-11 21:57:56 +03001539 /*
1540 * Try parsing the new attribute first so userspace
1541 * can specify both for older kernels.
1542 */
1543 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1544 if (nla) {
1545 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001546
Johannes Bergeccb8e82009-05-11 21:57:56 +03001547 sta_flags = nla_data(nla);
1548 params->sta_flags_mask = sta_flags->mask;
1549 params->sta_flags_set = sta_flags->set;
1550 if ((params->sta_flags_mask |
1551 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1552 return -EINVAL;
1553 return 0;
1554 }
1555
1556 /* if present, parse the old attribute */
1557
1558 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001559 if (!nla)
1560 return 0;
1561
1562 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1563 nla, sta_flags_policy))
1564 return -EINVAL;
1565
Johannes Bergeccb8e82009-05-11 21:57:56 +03001566 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1567 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001568
1569 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1570 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001571 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001572
1573 return 0;
1574}
1575
Henning Rogge420e7fa2008-12-11 22:04:19 +01001576static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1577{
1578 int modulation, streams, bitrate;
1579
1580 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1581 return rate->legacy;
1582
1583 /* the formula below does only work for MCS values smaller than 32 */
1584 if (rate->mcs >= 32)
1585 return 0;
1586
1587 modulation = rate->mcs & 7;
1588 streams = (rate->mcs >> 3) + 1;
1589
1590 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1591 13500000 : 6500000;
1592
1593 if (modulation < 4)
1594 bitrate *= (modulation + 1);
1595 else if (modulation == 4)
1596 bitrate *= (modulation + 2);
1597 else
1598 bitrate *= (modulation + 3);
1599
1600 bitrate *= streams;
1601
1602 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1603 bitrate = (bitrate / 9) * 10;
1604
1605 /* do NOT round down here */
1606 return (bitrate + 50000) / 100000;
1607}
1608
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001609static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1610 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001611 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001612{
1613 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001614 struct nlattr *sinfoattr, *txrate;
1615 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001616
1617 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1618 if (!hdr)
1619 return -1;
1620
1621 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1622 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1623
Johannes Bergf5ea9122009-08-07 16:17:38 +02001624 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1625
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001626 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1627 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001628 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001629 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1630 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1631 sinfo->inactive_time);
1632 if (sinfo->filled & STATION_INFO_RX_BYTES)
1633 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1634 sinfo->rx_bytes);
1635 if (sinfo->filled & STATION_INFO_TX_BYTES)
1636 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1637 sinfo->tx_bytes);
1638 if (sinfo->filled & STATION_INFO_LLID)
1639 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1640 sinfo->llid);
1641 if (sinfo->filled & STATION_INFO_PLID)
1642 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1643 sinfo->plid);
1644 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1645 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1646 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001647 if (sinfo->filled & STATION_INFO_SIGNAL)
1648 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1649 sinfo->signal);
1650 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1651 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1652 if (!txrate)
1653 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001654
Henning Rogge420e7fa2008-12-11 22:04:19 +01001655 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1656 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1657 if (bitrate > 0)
1658 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1659
1660 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1661 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1662 sinfo->txrate.mcs);
1663 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1664 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1665 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1666 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1667
1668 nla_nest_end(msg, txrate);
1669 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001670 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1671 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1672 sinfo->rx_packets);
1673 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1674 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1675 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001676 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001677
1678 return genlmsg_end(msg, hdr);
1679
1680 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001681 genlmsg_cancel(msg, hdr);
1682 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001683}
1684
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001685static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001686 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001687{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001688 struct station_info sinfo;
1689 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001690 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001691 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001692 int ifidx = cb->args[0];
1693 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001694 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001695
Johannes Bergbba95fe2008-07-29 13:22:51 +02001696 if (!ifidx) {
1697 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1698 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1699 nl80211_policy);
1700 if (err)
1701 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001702
Johannes Bergbba95fe2008-07-29 13:22:51 +02001703 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1704 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001705
Johannes Bergbba95fe2008-07-29 13:22:51 +02001706 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1707 if (!ifidx)
1708 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001709 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001710
Johannes Berg3b858752009-03-12 09:55:09 +01001711 rtnl_lock();
1712
Johannes Berg463d0182009-07-14 00:33:35 +02001713 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01001714 if (!netdev) {
1715 err = -ENODEV;
1716 goto out_rtnl;
1717 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001718
Johannes Berg463d0182009-07-14 00:33:35 +02001719 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001720 if (IS_ERR(dev)) {
1721 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001722 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001723 }
1724
1725 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001726 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001727 goto out_err;
1728 }
1729
Johannes Bergbba95fe2008-07-29 13:22:51 +02001730 while (1) {
1731 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1732 mac_addr, &sinfo);
1733 if (err == -ENOENT)
1734 break;
1735 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001736 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001737
1738 if (nl80211_send_station(skb,
1739 NETLINK_CB(cb->skb).pid,
1740 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1741 netdev, mac_addr,
1742 &sinfo) < 0)
1743 goto out;
1744
1745 sta_idx++;
1746 }
1747
1748
1749 out:
1750 cb->args[1] = sta_idx;
1751 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001752 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001753 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001754 out_rtnl:
1755 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001756
1757 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001758}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001759
Johannes Berg5727ef12007-12-19 02:03:34 +01001760static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1761{
Johannes Berg79c97e92009-07-07 03:56:12 +02001762 struct cfg80211_registered_device *rdev;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001763 int err;
1764 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001765 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001766 struct sk_buff *msg;
1767 u8 *mac_addr = NULL;
1768
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001769 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001770
1771 if (!info->attrs[NL80211_ATTR_MAC])
1772 return -EINVAL;
1773
1774 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1775
Johannes Berg3b858752009-03-12 09:55:09 +01001776 rtnl_lock();
1777
Johannes Berg463d0182009-07-14 00:33:35 +02001778 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001779 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001780 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001781
Johannes Berg79c97e92009-07-07 03:56:12 +02001782 if (!rdev->ops->get_station) {
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001783 err = -EOPNOTSUPP;
1784 goto out;
1785 }
1786
Johannes Berg79c97e92009-07-07 03:56:12 +02001787 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001788 if (err)
1789 goto out;
1790
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001791 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001792 if (!msg)
1793 goto out;
1794
1795 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001796 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001797 goto out_free;
1798
Johannes Berg134e6372009-07-10 09:51:34 +00001799 err = genlmsg_reply(msg, info);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001800 goto out;
1801
1802 out_free:
1803 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001804 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001805 cfg80211_unlock_rdev(rdev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001806 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001807 out_rtnl:
1808 rtnl_unlock();
1809
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001810 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001811}
1812
1813/*
1814 * Get vlan interface making sure it is on the right wiphy.
1815 */
Johannes Berg463d0182009-07-14 00:33:35 +02001816static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01001817 struct cfg80211_registered_device *rdev,
1818 struct net_device **vlan)
1819{
Johannes Berg463d0182009-07-14 00:33:35 +02001820 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01001821 *vlan = NULL;
1822
1823 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02001824 *vlan = dev_get_by_index(genl_info_net(info),
1825 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01001826 if (!*vlan)
1827 return -ENODEV;
1828 if (!(*vlan)->ieee80211_ptr)
1829 return -EINVAL;
1830 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1831 return -EINVAL;
1832 }
1833 return 0;
1834}
1835
1836static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1837{
Johannes Berg79c97e92009-07-07 03:56:12 +02001838 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001839 int err;
1840 struct net_device *dev;
1841 struct station_parameters params;
1842 u8 *mac_addr = NULL;
1843
1844 memset(&params, 0, sizeof(params));
1845
1846 params.listen_interval = -1;
1847
1848 if (info->attrs[NL80211_ATTR_STA_AID])
1849 return -EINVAL;
1850
1851 if (!info->attrs[NL80211_ATTR_MAC])
1852 return -EINVAL;
1853
1854 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1855
1856 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1857 params.supported_rates =
1858 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1859 params.supported_rates_len =
1860 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1861 }
1862
1863 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1864 params.listen_interval =
1865 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1866
Jouni Malinen36aedc92008-08-25 11:58:58 +03001867 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1868 params.ht_capa =
1869 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1870
Johannes Bergeccb8e82009-05-11 21:57:56 +03001871 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001872 return -EINVAL;
1873
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001874 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1875 params.plink_action =
1876 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1877
Johannes Berg3b858752009-03-12 09:55:09 +01001878 rtnl_lock();
1879
Johannes Berg463d0182009-07-14 00:33:35 +02001880 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001881 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001882 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001883
Johannes Berg463d0182009-07-14 00:33:35 +02001884 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001885 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02001886 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001887
1888 /* validate settings */
1889 err = 0;
1890
1891 switch (dev->ieee80211_ptr->iftype) {
1892 case NL80211_IFTYPE_AP:
1893 case NL80211_IFTYPE_AP_VLAN:
1894 /* disallow mesh-specific things */
1895 if (params.plink_action)
1896 err = -EINVAL;
1897 break;
1898 case NL80211_IFTYPE_STATION:
1899 /* disallow everything but AUTHORIZED flag */
1900 if (params.plink_action)
1901 err = -EINVAL;
1902 if (params.vlan)
1903 err = -EINVAL;
1904 if (params.supported_rates)
1905 err = -EINVAL;
1906 if (params.ht_capa)
1907 err = -EINVAL;
1908 if (params.listen_interval >= 0)
1909 err = -EINVAL;
1910 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
1911 err = -EINVAL;
1912 break;
1913 case NL80211_IFTYPE_MESH_POINT:
1914 /* disallow things mesh doesn't support */
1915 if (params.vlan)
1916 err = -EINVAL;
1917 if (params.ht_capa)
1918 err = -EINVAL;
1919 if (params.listen_interval >= 0)
1920 err = -EINVAL;
1921 if (params.supported_rates)
1922 err = -EINVAL;
1923 if (params.sta_flags_mask)
1924 err = -EINVAL;
1925 break;
1926 default:
1927 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02001928 }
1929
Johannes Berg5727ef12007-12-19 02:03:34 +01001930 if (err)
1931 goto out;
1932
Johannes Berg79c97e92009-07-07 03:56:12 +02001933 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01001934 err = -EOPNOTSUPP;
1935 goto out;
1936 }
1937
Johannes Berg79c97e92009-07-07 03:56:12 +02001938 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001939
1940 out:
1941 if (params.vlan)
1942 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02001943 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001944 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001945 out_rtnl:
1946 rtnl_unlock();
1947
Johannes Berg5727ef12007-12-19 02:03:34 +01001948 return err;
1949}
1950
1951static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1952{
Johannes Berg79c97e92009-07-07 03:56:12 +02001953 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001954 int err;
1955 struct net_device *dev;
1956 struct station_parameters params;
1957 u8 *mac_addr = NULL;
1958
1959 memset(&params, 0, sizeof(params));
1960
1961 if (!info->attrs[NL80211_ATTR_MAC])
1962 return -EINVAL;
1963
Johannes Berg5727ef12007-12-19 02:03:34 +01001964 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1965 return -EINVAL;
1966
1967 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1968 return -EINVAL;
1969
1970 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1971 params.supported_rates =
1972 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1973 params.supported_rates_len =
1974 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1975 params.listen_interval =
1976 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02001977
Johannes Berga97f4422009-06-18 17:23:43 +02001978 if (info->attrs[NL80211_ATTR_STA_AID]) {
1979 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1980 if (!params.aid || params.aid > IEEE80211_MAX_AID)
1981 return -EINVAL;
1982 }
Johannes Berg51b50fb2009-05-24 16:42:30 +02001983
Jouni Malinen36aedc92008-08-25 11:58:58 +03001984 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1985 params.ht_capa =
1986 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01001987
Johannes Bergeccb8e82009-05-11 21:57:56 +03001988 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001989 return -EINVAL;
1990
Johannes Berg3b858752009-03-12 09:55:09 +01001991 rtnl_lock();
1992
Johannes Berg463d0182009-07-14 00:33:35 +02001993 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001994 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001995 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001996
Johannes Berg463d0182009-07-14 00:33:35 +02001997 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001998 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02001999 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002000
2001 /* validate settings */
2002 err = 0;
2003
2004 switch (dev->ieee80211_ptr->iftype) {
2005 case NL80211_IFTYPE_AP:
2006 case NL80211_IFTYPE_AP_VLAN:
2007 /* all ok but must have AID */
2008 if (!params.aid)
2009 err = -EINVAL;
2010 break;
2011 case NL80211_IFTYPE_MESH_POINT:
2012 /* disallow things mesh doesn't support */
2013 if (params.vlan)
2014 err = -EINVAL;
2015 if (params.aid)
2016 err = -EINVAL;
2017 if (params.ht_capa)
2018 err = -EINVAL;
2019 if (params.listen_interval >= 0)
2020 err = -EINVAL;
2021 if (params.supported_rates)
2022 err = -EINVAL;
2023 if (params.sta_flags_mask)
2024 err = -EINVAL;
2025 break;
2026 default:
2027 err = -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002028 }
2029
Johannes Berg5727ef12007-12-19 02:03:34 +01002030 if (err)
2031 goto out;
2032
Johannes Berg79c97e92009-07-07 03:56:12 +02002033 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002034 err = -EOPNOTSUPP;
2035 goto out;
2036 }
2037
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002038 if (!netif_running(dev)) {
2039 err = -ENETDOWN;
2040 goto out;
2041 }
2042
Johannes Berg79c97e92009-07-07 03:56:12 +02002043 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002044
2045 out:
2046 if (params.vlan)
2047 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002048 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002049 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002050 out_rtnl:
2051 rtnl_unlock();
2052
Johannes Berg5727ef12007-12-19 02:03:34 +01002053 return err;
2054}
2055
2056static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2057{
Johannes Berg79c97e92009-07-07 03:56:12 +02002058 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002059 int err;
2060 struct net_device *dev;
2061 u8 *mac_addr = NULL;
2062
2063 if (info->attrs[NL80211_ATTR_MAC])
2064 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2065
Johannes Berg3b858752009-03-12 09:55:09 +01002066 rtnl_lock();
2067
Johannes Berg463d0182009-07-14 00:33:35 +02002068 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002069 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002070 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002071
Johannes Berge80cf852009-05-11 14:43:13 +02002072 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Andrey Yurovsky155cc9e2009-06-16 11:31:04 -07002073 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2074 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berge80cf852009-05-11 14:43:13 +02002075 err = -EINVAL;
2076 goto out;
2077 }
2078
Johannes Berg79c97e92009-07-07 03:56:12 +02002079 if (!rdev->ops->del_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002080 err = -EOPNOTSUPP;
2081 goto out;
2082 }
2083
Johannes Berg79c97e92009-07-07 03:56:12 +02002084 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002085
2086 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002087 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002088 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002089 out_rtnl:
2090 rtnl_unlock();
2091
Johannes Berg5727ef12007-12-19 02:03:34 +01002092 return err;
2093}
2094
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002095static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2096 int flags, struct net_device *dev,
2097 u8 *dst, u8 *next_hop,
2098 struct mpath_info *pinfo)
2099{
2100 void *hdr;
2101 struct nlattr *pinfoattr;
2102
2103 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2104 if (!hdr)
2105 return -1;
2106
2107 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2108 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2109 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2110
Johannes Bergf5ea9122009-08-07 16:17:38 +02002111 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2112
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002113 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2114 if (!pinfoattr)
2115 goto nla_put_failure;
2116 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2117 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2118 pinfo->frame_qlen);
2119 if (pinfo->filled & MPATH_INFO_DSN)
2120 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
2121 pinfo->dsn);
2122 if (pinfo->filled & MPATH_INFO_METRIC)
2123 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2124 pinfo->metric);
2125 if (pinfo->filled & MPATH_INFO_EXPTIME)
2126 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2127 pinfo->exptime);
2128 if (pinfo->filled & MPATH_INFO_FLAGS)
2129 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2130 pinfo->flags);
2131 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2132 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2133 pinfo->discovery_timeout);
2134 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2135 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2136 pinfo->discovery_retries);
2137
2138 nla_nest_end(msg, pinfoattr);
2139
2140 return genlmsg_end(msg, hdr);
2141
2142 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002143 genlmsg_cancel(msg, hdr);
2144 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002145}
2146
2147static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002148 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002149{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002150 struct mpath_info pinfo;
2151 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002152 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002153 u8 dst[ETH_ALEN];
2154 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002155 int ifidx = cb->args[0];
2156 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002157 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002158
Johannes Bergbba95fe2008-07-29 13:22:51 +02002159 if (!ifidx) {
2160 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2161 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2162 nl80211_policy);
2163 if (err)
2164 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002165
Johannes Bergbba95fe2008-07-29 13:22:51 +02002166 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2167 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002168
Johannes Bergbba95fe2008-07-29 13:22:51 +02002169 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2170 if (!ifidx)
2171 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002172 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002173
Johannes Berg3b858752009-03-12 09:55:09 +01002174 rtnl_lock();
2175
Johannes Berg463d0182009-07-14 00:33:35 +02002176 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01002177 if (!netdev) {
2178 err = -ENODEV;
2179 goto out_rtnl;
2180 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002181
Johannes Berg463d0182009-07-14 00:33:35 +02002182 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002183 if (IS_ERR(dev)) {
2184 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002185 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002186 }
2187
2188 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002189 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002190 goto out_err;
2191 }
2192
Jouni Malineneec60b02009-03-20 21:21:19 +02002193 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2194 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002195 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002196 }
2197
Johannes Bergbba95fe2008-07-29 13:22:51 +02002198 while (1) {
2199 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2200 dst, next_hop, &pinfo);
2201 if (err == -ENOENT)
2202 break;
2203 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002204 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002205
2206 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2207 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2208 netdev, dst, next_hop,
2209 &pinfo) < 0)
2210 goto out;
2211
2212 path_idx++;
2213 }
2214
2215
2216 out:
2217 cb->args[1] = path_idx;
2218 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002219 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02002220 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002221 out_rtnl:
2222 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002223
2224 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002225}
2226
2227static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2228{
Johannes Berg79c97e92009-07-07 03:56:12 +02002229 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002230 int err;
2231 struct net_device *dev;
2232 struct mpath_info pinfo;
2233 struct sk_buff *msg;
2234 u8 *dst = NULL;
2235 u8 next_hop[ETH_ALEN];
2236
2237 memset(&pinfo, 0, sizeof(pinfo));
2238
2239 if (!info->attrs[NL80211_ATTR_MAC])
2240 return -EINVAL;
2241
2242 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2243
Johannes Berg3b858752009-03-12 09:55:09 +01002244 rtnl_lock();
2245
Johannes Berg463d0182009-07-14 00:33:35 +02002246 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002247 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002248 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002249
Johannes Berg79c97e92009-07-07 03:56:12 +02002250 if (!rdev->ops->get_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002251 err = -EOPNOTSUPP;
2252 goto out;
2253 }
2254
Jouni Malineneec60b02009-03-20 21:21:19 +02002255 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2256 err = -EOPNOTSUPP;
2257 goto out;
2258 }
2259
Johannes Berg79c97e92009-07-07 03:56:12 +02002260 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002261 if (err)
2262 goto out;
2263
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002264 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002265 if (!msg)
2266 goto out;
2267
2268 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2269 dev, dst, next_hop, &pinfo) < 0)
2270 goto out_free;
2271
Johannes Berg134e6372009-07-10 09:51:34 +00002272 err = genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002273 goto out;
2274
2275 out_free:
2276 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002277 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002278 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002279 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002280 out_rtnl:
2281 rtnl_unlock();
2282
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002283 return err;
2284}
2285
2286static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2287{
Johannes Berg79c97e92009-07-07 03:56:12 +02002288 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002289 int err;
2290 struct net_device *dev;
2291 u8 *dst = NULL;
2292 u8 *next_hop = NULL;
2293
2294 if (!info->attrs[NL80211_ATTR_MAC])
2295 return -EINVAL;
2296
2297 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2298 return -EINVAL;
2299
2300 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2301 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2302
Johannes Berg3b858752009-03-12 09:55:09 +01002303 rtnl_lock();
2304
Johannes Berg463d0182009-07-14 00:33:35 +02002305 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002306 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002307 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002308
Johannes Berg79c97e92009-07-07 03:56:12 +02002309 if (!rdev->ops->change_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002310 err = -EOPNOTSUPP;
2311 goto out;
2312 }
2313
Jouni Malineneec60b02009-03-20 21:21:19 +02002314 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2315 err = -EOPNOTSUPP;
2316 goto out;
2317 }
2318
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002319 if (!netif_running(dev)) {
2320 err = -ENETDOWN;
2321 goto out;
2322 }
2323
Johannes Berg79c97e92009-07-07 03:56:12 +02002324 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002325
2326 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002327 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002328 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002329 out_rtnl:
2330 rtnl_unlock();
2331
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002332 return err;
2333}
2334static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2335{
Johannes Berg79c97e92009-07-07 03:56:12 +02002336 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002337 int err;
2338 struct net_device *dev;
2339 u8 *dst = NULL;
2340 u8 *next_hop = NULL;
2341
2342 if (!info->attrs[NL80211_ATTR_MAC])
2343 return -EINVAL;
2344
2345 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2346 return -EINVAL;
2347
2348 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2349 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2350
Johannes Berg3b858752009-03-12 09:55:09 +01002351 rtnl_lock();
2352
Johannes Berg463d0182009-07-14 00:33:35 +02002353 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002354 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002355 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002356
Johannes Berg79c97e92009-07-07 03:56:12 +02002357 if (!rdev->ops->add_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002358 err = -EOPNOTSUPP;
2359 goto out;
2360 }
2361
Jouni Malineneec60b02009-03-20 21:21:19 +02002362 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2363 err = -EOPNOTSUPP;
2364 goto out;
2365 }
2366
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002367 if (!netif_running(dev)) {
2368 err = -ENETDOWN;
2369 goto out;
2370 }
2371
Johannes Berg79c97e92009-07-07 03:56:12 +02002372 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002373
2374 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002375 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002376 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002377 out_rtnl:
2378 rtnl_unlock();
2379
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002380 return err;
2381}
2382
2383static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2384{
Johannes Berg79c97e92009-07-07 03:56:12 +02002385 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002386 int err;
2387 struct net_device *dev;
2388 u8 *dst = NULL;
2389
2390 if (info->attrs[NL80211_ATTR_MAC])
2391 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2392
Johannes Berg3b858752009-03-12 09:55:09 +01002393 rtnl_lock();
2394
Johannes Berg463d0182009-07-14 00:33:35 +02002395 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002396 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002397 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002398
Johannes Berg79c97e92009-07-07 03:56:12 +02002399 if (!rdev->ops->del_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002400 err = -EOPNOTSUPP;
2401 goto out;
2402 }
2403
Johannes Berg79c97e92009-07-07 03:56:12 +02002404 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002405
2406 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002407 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002408 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002409 out_rtnl:
2410 rtnl_unlock();
2411
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002412 return err;
2413}
2414
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002415static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2416{
Johannes Berg79c97e92009-07-07 03:56:12 +02002417 struct cfg80211_registered_device *rdev;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002418 int err;
2419 struct net_device *dev;
2420 struct bss_parameters params;
2421
2422 memset(&params, 0, sizeof(params));
2423 /* default to not changing parameters */
2424 params.use_cts_prot = -1;
2425 params.use_short_preamble = -1;
2426 params.use_short_slot_time = -1;
2427
2428 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2429 params.use_cts_prot =
2430 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2431 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2432 params.use_short_preamble =
2433 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2434 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2435 params.use_short_slot_time =
2436 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002437 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2438 params.basic_rates =
2439 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2440 params.basic_rates_len =
2441 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2442 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002443
Johannes Berg3b858752009-03-12 09:55:09 +01002444 rtnl_lock();
2445
Johannes Berg463d0182009-07-14 00:33:35 +02002446 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002447 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002448 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002449
Johannes Berg79c97e92009-07-07 03:56:12 +02002450 if (!rdev->ops->change_bss) {
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002451 err = -EOPNOTSUPP;
2452 goto out;
2453 }
2454
Jouni Malineneec60b02009-03-20 21:21:19 +02002455 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2456 err = -EOPNOTSUPP;
2457 goto out;
2458 }
2459
Johannes Berg79c97e92009-07-07 03:56:12 +02002460 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002461
2462 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002463 cfg80211_unlock_rdev(rdev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002464 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002465 out_rtnl:
2466 rtnl_unlock();
2467
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002468 return err;
2469}
2470
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002471static const struct nla_policy
2472 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2473 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2474 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2475 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2476 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2477 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2478 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2479};
2480
2481static int parse_reg_rule(struct nlattr *tb[],
2482 struct ieee80211_reg_rule *reg_rule)
2483{
2484 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2485 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2486
2487 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2488 return -EINVAL;
2489 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2490 return -EINVAL;
2491 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2492 return -EINVAL;
2493 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2494 return -EINVAL;
2495 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2496 return -EINVAL;
2497
2498 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2499
2500 freq_range->start_freq_khz =
2501 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2502 freq_range->end_freq_khz =
2503 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2504 freq_range->max_bandwidth_khz =
2505 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2506
2507 power_rule->max_eirp =
2508 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2509
2510 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2511 power_rule->max_antenna_gain =
2512 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2513
2514 return 0;
2515}
2516
2517static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2518{
2519 int r;
2520 char *data = NULL;
2521
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002522 /*
2523 * You should only get this when cfg80211 hasn't yet initialized
2524 * completely when built-in to the kernel right between the time
2525 * window between nl80211_init() and regulatory_init(), if that is
2526 * even possible.
2527 */
2528 mutex_lock(&cfg80211_mutex);
2529 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002530 mutex_unlock(&cfg80211_mutex);
2531 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002532 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002533 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002534
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002535 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2536 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002537
2538 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2539
2540#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2541 /* We ignore world regdom requests with the old regdom setup */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002542 if (is_world_regdom(data))
2543 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002544#endif
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002545
2546 r = regulatory_hint_user(data);
2547
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002548 return r;
2549}
2550
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002551static int nl80211_get_mesh_params(struct sk_buff *skb,
2552 struct genl_info *info)
2553{
Johannes Berg79c97e92009-07-07 03:56:12 +02002554 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002555 struct mesh_config cur_params;
2556 int err;
2557 struct net_device *dev;
2558 void *hdr;
2559 struct nlattr *pinfoattr;
2560 struct sk_buff *msg;
2561
Johannes Berg3b858752009-03-12 09:55:09 +01002562 rtnl_lock();
2563
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002564 /* Look up our device */
Johannes Berg463d0182009-07-14 00:33:35 +02002565 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002566 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002567 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002568
Johannes Berg79c97e92009-07-07 03:56:12 +02002569 if (!rdev->ops->get_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002570 err = -EOPNOTSUPP;
2571 goto out;
2572 }
2573
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002574 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002575 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002576 if (err)
2577 goto out;
2578
2579 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002580 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002581 if (!msg) {
2582 err = -ENOBUFS;
2583 goto out;
2584 }
2585 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2586 NL80211_CMD_GET_MESH_PARAMS);
2587 if (!hdr)
2588 goto nla_put_failure;
2589 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2590 if (!pinfoattr)
2591 goto nla_put_failure;
2592 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2593 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2594 cur_params.dot11MeshRetryTimeout);
2595 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2596 cur_params.dot11MeshConfirmTimeout);
2597 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2598 cur_params.dot11MeshHoldingTimeout);
2599 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2600 cur_params.dot11MeshMaxPeerLinks);
2601 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2602 cur_params.dot11MeshMaxRetries);
2603 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2604 cur_params.dot11MeshTTL);
2605 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2606 cur_params.auto_open_plinks);
2607 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2608 cur_params.dot11MeshHWMPmaxPREQretries);
2609 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2610 cur_params.path_refresh_time);
2611 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2612 cur_params.min_discovery_timeout);
2613 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2614 cur_params.dot11MeshHWMPactivePathTimeout);
2615 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2616 cur_params.dot11MeshHWMPpreqMinInterval);
2617 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2618 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2619 nla_nest_end(msg, pinfoattr);
2620 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002621 err = genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002622 goto out;
2623
Johannes Berg3b858752009-03-12 09:55:09 +01002624 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002625 genlmsg_cancel(msg, hdr);
2626 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002627 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002628 /* Cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002629 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002630 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002631 out_rtnl:
2632 rtnl_unlock();
2633
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002634 return err;
2635}
2636
2637#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2638do {\
2639 if (table[attr_num]) {\
2640 cfg.param = nla_fn(table[attr_num]); \
2641 mask |= (1 << (attr_num - 1)); \
2642 } \
2643} while (0);\
2644
2645static struct nla_policy
2646nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2647 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2648 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2649 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2650 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2651 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2652 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2653 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2654
2655 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2656 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2657 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2658 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2659 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2660 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2661};
2662
2663static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2664{
2665 int err;
2666 u32 mask;
Johannes Berg79c97e92009-07-07 03:56:12 +02002667 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002668 struct net_device *dev;
2669 struct mesh_config cfg;
2670 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2671 struct nlattr *parent_attr;
2672
2673 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2674 if (!parent_attr)
2675 return -EINVAL;
2676 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2677 parent_attr, nl80211_meshconf_params_policy))
2678 return -EINVAL;
2679
Johannes Berg3b858752009-03-12 09:55:09 +01002680 rtnl_lock();
2681
Johannes Berg463d0182009-07-14 00:33:35 +02002682 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002683 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002684 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002685
Johannes Berg79c97e92009-07-07 03:56:12 +02002686 if (!rdev->ops->set_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002687 err = -EOPNOTSUPP;
2688 goto out;
2689 }
2690
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002691 /* This makes sure that there aren't more than 32 mesh config
2692 * parameters (otherwise our bitfield scheme would not work.) */
2693 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2694
2695 /* Fill in the params struct */
2696 mask = 0;
2697 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2698 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2699 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2700 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2701 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2702 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2703 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2704 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2705 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2706 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2707 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2708 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2709 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2710 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2712 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2713 nla_get_u8);
2714 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2715 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2716 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2717 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2718 nla_get_u16);
2719 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2720 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2721 nla_get_u32);
2722 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2723 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2724 nla_get_u16);
2725 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2726 dot11MeshHWMPnetDiameterTraversalTime,
2727 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2728 nla_get_u16);
2729
2730 /* Apply changes */
Johannes Berg79c97e92009-07-07 03:56:12 +02002731 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002732
Jouni Malinenf3f92582009-03-20 17:57:36 +02002733 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002734 /* cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002735 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002736 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002737 out_rtnl:
2738 rtnl_unlock();
2739
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002740 return err;
2741}
2742
2743#undef FILL_IN_MESH_PARAM_IF_SET
2744
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002745static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2746{
2747 struct sk_buff *msg;
2748 void *hdr = NULL;
2749 struct nlattr *nl_reg_rules;
2750 unsigned int i;
2751 int err = -EINVAL;
2752
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002753 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002754
2755 if (!cfg80211_regdomain)
2756 goto out;
2757
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002758 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002759 if (!msg) {
2760 err = -ENOBUFS;
2761 goto out;
2762 }
2763
2764 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2765 NL80211_CMD_GET_REG);
2766 if (!hdr)
2767 goto nla_put_failure;
2768
2769 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2770 cfg80211_regdomain->alpha2);
2771
2772 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2773 if (!nl_reg_rules)
2774 goto nla_put_failure;
2775
2776 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2777 struct nlattr *nl_reg_rule;
2778 const struct ieee80211_reg_rule *reg_rule;
2779 const struct ieee80211_freq_range *freq_range;
2780 const struct ieee80211_power_rule *power_rule;
2781
2782 reg_rule = &cfg80211_regdomain->reg_rules[i];
2783 freq_range = &reg_rule->freq_range;
2784 power_rule = &reg_rule->power_rule;
2785
2786 nl_reg_rule = nla_nest_start(msg, i);
2787 if (!nl_reg_rule)
2788 goto nla_put_failure;
2789
2790 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2791 reg_rule->flags);
2792 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2793 freq_range->start_freq_khz);
2794 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2795 freq_range->end_freq_khz);
2796 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2797 freq_range->max_bandwidth_khz);
2798 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2799 power_rule->max_antenna_gain);
2800 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2801 power_rule->max_eirp);
2802
2803 nla_nest_end(msg, nl_reg_rule);
2804 }
2805
2806 nla_nest_end(msg, nl_reg_rules);
2807
2808 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002809 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002810 goto out;
2811
2812nla_put_failure:
2813 genlmsg_cancel(msg, hdr);
2814 err = -EMSGSIZE;
2815out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002816 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002817 return err;
2818}
2819
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002820static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2821{
2822 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2823 struct nlattr *nl_reg_rule;
2824 char *alpha2 = NULL;
2825 int rem_reg_rules = 0, r = 0;
2826 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2827 struct ieee80211_regdomain *rd = NULL;
2828
2829 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2830 return -EINVAL;
2831
2832 if (!info->attrs[NL80211_ATTR_REG_RULES])
2833 return -EINVAL;
2834
2835 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2836
2837 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2838 rem_reg_rules) {
2839 num_rules++;
2840 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002841 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002842 }
2843
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002844 mutex_lock(&cfg80211_mutex);
2845
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002846 if (!reg_is_valid_request(alpha2)) {
2847 r = -EINVAL;
2848 goto bad_reg;
2849 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002850
2851 size_of_regd = sizeof(struct ieee80211_regdomain) +
2852 (num_rules * sizeof(struct ieee80211_reg_rule));
2853
2854 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002855 if (!rd) {
2856 r = -ENOMEM;
2857 goto bad_reg;
2858 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002859
2860 rd->n_reg_rules = num_rules;
2861 rd->alpha2[0] = alpha2[0];
2862 rd->alpha2[1] = alpha2[1];
2863
2864 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2865 rem_reg_rules) {
2866 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2867 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2868 reg_rule_policy);
2869 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2870 if (r)
2871 goto bad_reg;
2872
2873 rule_idx++;
2874
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002875 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2876 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002877 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002878 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002879 }
2880
2881 BUG_ON(rule_idx != num_rules);
2882
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002883 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002884
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002885 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002886
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002887 return r;
2888
Johannes Bergd2372b32008-10-24 20:32:20 +02002889 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002890 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002891 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002892 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002893}
2894
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002895static int validate_scan_freqs(struct nlattr *freqs)
2896{
2897 struct nlattr *attr1, *attr2;
2898 int n_channels = 0, tmp1, tmp2;
2899
2900 nla_for_each_nested(attr1, freqs, tmp1) {
2901 n_channels++;
2902 /*
2903 * Some hardware has a limited channel list for
2904 * scanning, and it is pretty much nonsensical
2905 * to scan for a channel twice, so disallow that
2906 * and don't require drivers to check that the
2907 * channel list they get isn't longer than what
2908 * they can scan, as long as they can scan all
2909 * the channels they registered at once.
2910 */
2911 nla_for_each_nested(attr2, freqs, tmp2)
2912 if (attr1 != attr2 &&
2913 nla_get_u32(attr1) == nla_get_u32(attr2))
2914 return 0;
2915 }
2916
2917 return n_channels;
2918}
2919
Johannes Berg2a519312009-02-10 21:25:55 +01002920static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2921{
Johannes Berg79c97e92009-07-07 03:56:12 +02002922 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01002923 struct net_device *dev;
2924 struct cfg80211_scan_request *request;
2925 struct cfg80211_ssid *ssid;
2926 struct ieee80211_channel *channel;
2927 struct nlattr *attr;
2928 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002929 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002930 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002931 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002932
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002933 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2934 return -EINVAL;
2935
Johannes Berg3b858752009-03-12 09:55:09 +01002936 rtnl_lock();
2937
Johannes Berg463d0182009-07-14 00:33:35 +02002938 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg2a519312009-02-10 21:25:55 +01002939 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002940 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002941
Johannes Berg79c97e92009-07-07 03:56:12 +02002942 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002943
Johannes Berg79c97e92009-07-07 03:56:12 +02002944 if (!rdev->ops->scan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002945 err = -EOPNOTSUPP;
2946 goto out;
2947 }
2948
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002949 if (!netif_running(dev)) {
2950 err = -ENETDOWN;
2951 goto out;
2952 }
2953
Johannes Berg79c97e92009-07-07 03:56:12 +02002954 if (rdev->scan_req) {
Johannes Berg2a519312009-02-10 21:25:55 +01002955 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002956 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002957 }
2958
2959 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002960 n_channels = validate_scan_freqs(
2961 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg2a519312009-02-10 21:25:55 +01002962 if (!n_channels) {
2963 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002964 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002965 }
2966 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002967 n_channels = 0;
2968
Johannes Berg2a519312009-02-10 21:25:55 +01002969 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2970 if (wiphy->bands[band])
2971 n_channels += wiphy->bands[band]->n_channels;
2972 }
2973
2974 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2975 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2976 n_ssids++;
2977
2978 if (n_ssids > wiphy->max_scan_ssids) {
2979 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002980 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002981 }
2982
Jouni Malinen70692ad2009-02-16 19:39:13 +02002983 if (info->attrs[NL80211_ATTR_IE])
2984 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2985 else
2986 ie_len = 0;
2987
Johannes Berg18a83652009-03-31 12:12:05 +02002988 if (ie_len > wiphy->max_scan_ie_len) {
2989 err = -EINVAL;
2990 goto out;
2991 }
2992
Johannes Berg2a519312009-02-10 21:25:55 +01002993 request = kzalloc(sizeof(*request)
2994 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002995 + sizeof(channel) * n_channels
2996 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01002997 if (!request) {
2998 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01002999 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003000 }
3001
Johannes Berg2a519312009-02-10 21:25:55 +01003002 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003003 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003004 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003005 if (ie_len) {
3006 if (request->ssids)
3007 request->ie = (void *)(request->ssids + n_ssids);
3008 else
3009 request->ie = (void *)(request->channels + n_channels);
3010 }
Johannes Berg2a519312009-02-10 21:25:55 +01003011
Johannes Berg584991d2009-11-02 13:32:03 +01003012 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003013 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3014 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003015 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003016 struct ieee80211_channel *chan;
3017
3018 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3019
3020 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003021 err = -EINVAL;
3022 goto out_free;
3023 }
Johannes Berg584991d2009-11-02 13:32:03 +01003024
3025 /* ignore disabled channels */
3026 if (chan->flags & IEEE80211_CHAN_DISABLED)
3027 continue;
3028
3029 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003030 i++;
3031 }
3032 } else {
3033 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003034 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3035 int j;
3036 if (!wiphy->bands[band])
3037 continue;
3038 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003039 struct ieee80211_channel *chan;
3040
3041 chan = &wiphy->bands[band]->channels[j];
3042
3043 if (chan->flags & IEEE80211_CHAN_DISABLED)
3044 continue;
3045
3046 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003047 i++;
3048 }
3049 }
3050 }
3051
Johannes Berg584991d2009-11-02 13:32:03 +01003052 if (!i) {
3053 err = -EINVAL;
3054 goto out_free;
3055 }
3056
3057 request->n_channels = i;
3058
Johannes Berg2a519312009-02-10 21:25:55 +01003059 i = 0;
3060 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3061 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3062 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3063 err = -EINVAL;
3064 goto out_free;
3065 }
3066 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3067 request->ssids[i].ssid_len = nla_len(attr);
3068 i++;
3069 }
3070 }
3071
Jouni Malinen70692ad2009-02-16 19:39:13 +02003072 if (info->attrs[NL80211_ATTR_IE]) {
3073 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003074 memcpy((void *)request->ie,
3075 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003076 request->ie_len);
3077 }
3078
Johannes Berg463d0182009-07-14 00:33:35 +02003079 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003080 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003081
Johannes Berg79c97e92009-07-07 03:56:12 +02003082 rdev->scan_req = request;
3083 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003084
Johannes Berg463d0182009-07-14 00:33:35 +02003085 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003086 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003087 dev_hold(dev);
3088 }
Johannes Berga538e2d2009-06-16 19:56:42 +02003089
Johannes Berg2a519312009-02-10 21:25:55 +01003090 out_free:
3091 if (err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003092 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003093 kfree(request);
3094 }
Johannes Berg2a519312009-02-10 21:25:55 +01003095 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003096 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003097 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01003098 out_rtnl:
3099 rtnl_unlock();
3100
Johannes Berg2a519312009-02-10 21:25:55 +01003101 return err;
3102}
3103
3104static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3105 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003106 struct wireless_dev *wdev,
3107 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003108{
Johannes Berg48ab9052009-07-10 18:42:31 +02003109 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003110 void *hdr;
3111 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003112 int i;
3113
3114 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003115
3116 hdr = nl80211hdr_put(msg, pid, seq, flags,
3117 NL80211_CMD_NEW_SCAN_RESULTS);
3118 if (!hdr)
3119 return -1;
3120
Johannes Bergf5ea9122009-08-07 16:17:38 +02003121 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003122 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003123
3124 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3125 if (!bss)
3126 goto nla_put_failure;
3127 if (!is_zero_ether_addr(res->bssid))
3128 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3129 if (res->information_elements && res->len_information_elements)
3130 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3131 res->len_information_elements,
3132 res->information_elements);
3133 if (res->tsf)
3134 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3135 if (res->beacon_interval)
3136 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3137 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3138 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003139 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3140 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003141
Johannes Berg77965c92009-02-18 18:45:06 +01003142 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003143 case CFG80211_SIGNAL_TYPE_MBM:
3144 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3145 break;
3146 case CFG80211_SIGNAL_TYPE_UNSPEC:
3147 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3148 break;
3149 default:
3150 break;
3151 }
3152
Johannes Berg48ab9052009-07-10 18:42:31 +02003153 switch (wdev->iftype) {
3154 case NL80211_IFTYPE_STATION:
3155 if (intbss == wdev->current_bss)
3156 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3157 NL80211_BSS_STATUS_ASSOCIATED);
3158 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3159 if (intbss != wdev->auth_bsses[i])
3160 continue;
3161 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3162 NL80211_BSS_STATUS_AUTHENTICATED);
3163 break;
3164 }
3165 break;
3166 case NL80211_IFTYPE_ADHOC:
3167 if (intbss == wdev->current_bss)
3168 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3169 NL80211_BSS_STATUS_IBSS_JOINED);
3170 break;
3171 default:
3172 break;
3173 }
3174
Johannes Berg2a519312009-02-10 21:25:55 +01003175 nla_nest_end(msg, bss);
3176
3177 return genlmsg_end(msg, hdr);
3178
3179 nla_put_failure:
3180 genlmsg_cancel(msg, hdr);
3181 return -EMSGSIZE;
3182}
3183
3184static int nl80211_dump_scan(struct sk_buff *skb,
3185 struct netlink_callback *cb)
3186{
Johannes Berg48ab9052009-07-10 18:42:31 +02003187 struct cfg80211_registered_device *rdev;
3188 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003189 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003190 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003191 int ifidx = cb->args[0];
3192 int start = cb->args[1], idx = 0;
3193 int err;
3194
3195 if (!ifidx) {
3196 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
3197 nl80211_fam.attrbuf, nl80211_fam.maxattr,
3198 nl80211_policy);
3199 if (err)
3200 return err;
3201
3202 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
3203 return -EINVAL;
3204
3205 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
3206 if (!ifidx)
3207 return -EINVAL;
3208 cb->args[0] = ifidx;
3209 }
3210
Johannes Berg463d0182009-07-14 00:33:35 +02003211 dev = dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003212 if (!dev)
Johannes Berg2a519312009-02-10 21:25:55 +01003213 return -ENODEV;
3214
Johannes Berg463d0182009-07-14 00:33:35 +02003215 rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003216 if (IS_ERR(rdev)) {
3217 err = PTR_ERR(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003218 goto out_put_netdev;
3219 }
3220
Johannes Berg48ab9052009-07-10 18:42:31 +02003221 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003222
Johannes Berg48ab9052009-07-10 18:42:31 +02003223 wdev_lock(wdev);
3224 spin_lock_bh(&rdev->bss_lock);
3225 cfg80211_bss_expire(rdev);
3226
3227 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003228 if (++idx <= start)
3229 continue;
3230 if (nl80211_send_bss(skb,
3231 NETLINK_CB(cb->skb).pid,
3232 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003233 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003234 idx--;
3235 goto out;
3236 }
3237 }
3238
3239 out:
Johannes Berg48ab9052009-07-10 18:42:31 +02003240 spin_unlock_bh(&rdev->bss_lock);
3241 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003242
3243 cb->args[1] = idx;
3244 err = skb->len;
Johannes Berg48ab9052009-07-10 18:42:31 +02003245 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003246 out_put_netdev:
Johannes Berg48ab9052009-07-10 18:42:31 +02003247 dev_put(dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003248
3249 return err;
3250}
3251
Jouni Malinen255e7372009-03-20 21:21:17 +02003252static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3253{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003254 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003255}
3256
Samuel Ortizb23aa672009-07-01 21:26:54 +02003257static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3258{
3259 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3260 NL80211_WPA_VERSION_2));
3261}
3262
3263static bool nl80211_valid_akm_suite(u32 akm)
3264{
3265 return akm == WLAN_AKM_SUITE_8021X ||
3266 akm == WLAN_AKM_SUITE_PSK;
3267}
3268
3269static bool nl80211_valid_cipher_suite(u32 cipher)
3270{
3271 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3272 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3273 cipher == WLAN_CIPHER_SUITE_TKIP ||
3274 cipher == WLAN_CIPHER_SUITE_CCMP ||
3275 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3276}
3277
3278
Jouni Malinen636a5d32009-03-19 13:39:22 +02003279static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3280{
Johannes Berg79c97e92009-07-07 03:56:12 +02003281 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003282 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003283 struct ieee80211_channel *chan;
3284 const u8 *bssid, *ssid, *ie = NULL;
3285 int err, ssid_len, ie_len = 0;
3286 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003287 struct key_parse key;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003288
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003289 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3290 return -EINVAL;
3291
3292 if (!info->attrs[NL80211_ATTR_MAC])
3293 return -EINVAL;
3294
Jouni Malinen17780922009-03-27 20:52:47 +02003295 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3296 return -EINVAL;
3297
Johannes Berg19957bb2009-07-02 17:20:43 +02003298 if (!info->attrs[NL80211_ATTR_SSID])
3299 return -EINVAL;
3300
3301 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3302 return -EINVAL;
3303
Johannes Bergfffd0932009-07-08 14:22:54 +02003304 err = nl80211_parse_key(info, &key);
3305 if (err)
3306 return err;
3307
3308 if (key.idx >= 0) {
3309 if (!key.p.key || !key.p.key_len)
3310 return -EINVAL;
3311 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3312 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3313 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3314 key.p.key_len != WLAN_KEY_LEN_WEP104))
3315 return -EINVAL;
3316 if (key.idx > 4)
3317 return -EINVAL;
3318 } else {
3319 key.p.key_len = 0;
3320 key.p.key = NULL;
3321 }
3322
Jouni Malinen636a5d32009-03-19 13:39:22 +02003323 rtnl_lock();
3324
Johannes Berg463d0182009-07-14 00:33:35 +02003325 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003326 if (err)
3327 goto unlock_rtnl;
3328
Johannes Berg79c97e92009-07-07 03:56:12 +02003329 if (!rdev->ops->auth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003330 err = -EOPNOTSUPP;
3331 goto out;
3332 }
3333
Jouni Malineneec60b02009-03-20 21:21:19 +02003334 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3335 err = -EOPNOTSUPP;
3336 goto out;
3337 }
3338
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003339 if (!netif_running(dev)) {
3340 err = -ENETDOWN;
3341 goto out;
3342 }
3343
Johannes Berg19957bb2009-07-02 17:20:43 +02003344 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003345 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003346 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3347 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
Jouni Malinen17780922009-03-27 20:52:47 +02003348 err = -EINVAL;
3349 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003350 }
3351
Johannes Berg19957bb2009-07-02 17:20:43 +02003352 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3353 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3354
3355 if (info->attrs[NL80211_ATTR_IE]) {
3356 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3357 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3358 }
3359
3360 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3361 if (!nl80211_valid_auth_type(auth_type)) {
3362 err = -EINVAL;
3363 goto out;
3364 }
3365
Johannes Berg79c97e92009-07-07 03:56:12 +02003366 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +02003367 ssid, ssid_len, ie, ie_len,
3368 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003369
3370out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003371 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003372 dev_put(dev);
3373unlock_rtnl:
3374 rtnl_unlock();
3375 return err;
3376}
3377
Samuel Ortizb23aa672009-07-01 21:26:54 +02003378static int nl80211_crypto_settings(struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003379 struct cfg80211_crypto_settings *settings,
3380 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003381{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003382 memset(settings, 0, sizeof(*settings));
3383
Samuel Ortizb23aa672009-07-01 21:26:54 +02003384 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3385
3386 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3387 void *data;
3388 int len, i;
3389
3390 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3391 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3392 settings->n_ciphers_pairwise = len / sizeof(u32);
3393
3394 if (len % sizeof(u32))
3395 return -EINVAL;
3396
Johannes Berg3dc27d22009-07-02 21:36:37 +02003397 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003398 return -EINVAL;
3399
3400 memcpy(settings->ciphers_pairwise, data, len);
3401
3402 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3403 if (!nl80211_valid_cipher_suite(
3404 settings->ciphers_pairwise[i]))
3405 return -EINVAL;
3406 }
3407
3408 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3409 settings->cipher_group =
3410 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3411 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3412 return -EINVAL;
3413 }
3414
3415 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3416 settings->wpa_versions =
3417 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3418 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3419 return -EINVAL;
3420 }
3421
3422 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3423 void *data;
3424 int len, i;
3425
3426 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3427 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3428 settings->n_akm_suites = len / sizeof(u32);
3429
3430 if (len % sizeof(u32))
3431 return -EINVAL;
3432
3433 memcpy(settings->akm_suites, data, len);
3434
3435 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3436 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3437 return -EINVAL;
3438 }
3439
3440 return 0;
3441}
3442
Jouni Malinen636a5d32009-03-19 13:39:22 +02003443static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3444{
Johannes Berg19957bb2009-07-02 17:20:43 +02003445 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003446 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003447 struct cfg80211_crypto_settings crypto;
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003448 struct ieee80211_channel *chan, *fixedchan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003449 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003450 int err, ssid_len, ie_len = 0;
3451 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003452
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003453 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3454 return -EINVAL;
3455
3456 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003457 !info->attrs[NL80211_ATTR_SSID] ||
3458 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003459 return -EINVAL;
3460
Jouni Malinen636a5d32009-03-19 13:39:22 +02003461 rtnl_lock();
3462
Johannes Berg463d0182009-07-14 00:33:35 +02003463 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003464 if (err)
3465 goto unlock_rtnl;
3466
Johannes Berg19957bb2009-07-02 17:20:43 +02003467 if (!rdev->ops->assoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003468 err = -EOPNOTSUPP;
3469 goto out;
3470 }
3471
Jouni Malineneec60b02009-03-20 21:21:19 +02003472 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3473 err = -EOPNOTSUPP;
3474 goto out;
3475 }
3476
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003477 if (!netif_running(dev)) {
3478 err = -ENETDOWN;
3479 goto out;
3480 }
3481
Johannes Berg19957bb2009-07-02 17:20:43 +02003482 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003483
Johannes Berg19957bb2009-07-02 17:20:43 +02003484 chan = ieee80211_get_channel(&rdev->wiphy,
3485 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3486 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3487 err = -EINVAL;
3488 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003489 }
3490
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003491 mutex_lock(&rdev->devlist_mtx);
3492 fixedchan = rdev_fixed_channel(rdev, NULL);
3493 if (fixedchan && chan != fixedchan) {
3494 err = -EBUSY;
3495 mutex_unlock(&rdev->devlist_mtx);
3496 goto out;
3497 }
3498 mutex_unlock(&rdev->devlist_mtx);
3499
Johannes Berg19957bb2009-07-02 17:20:43 +02003500 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3501 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003502
3503 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003504 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3505 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003506 }
3507
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003508 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003509 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003510 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003511 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003512 use_mfp = true;
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003513 else if (mfp != NL80211_MFP_NO) {
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003514 err = -EINVAL;
3515 goto out;
3516 }
3517 }
3518
Johannes Berg3e5d7642009-07-07 14:37:26 +02003519 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3520 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3521
Johannes Berg3dc27d22009-07-02 21:36:37 +02003522 err = nl80211_crypto_settings(info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003523 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003524 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3525 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003526 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003527
3528out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003529 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003530 dev_put(dev);
3531unlock_rtnl:
3532 rtnl_unlock();
3533 return err;
3534}
3535
3536static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3537{
Johannes Berg79c97e92009-07-07 03:56:12 +02003538 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003539 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003540 const u8 *ie = NULL, *bssid;
3541 int err, ie_len = 0;
3542 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003543
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003544 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3545 return -EINVAL;
3546
3547 if (!info->attrs[NL80211_ATTR_MAC])
3548 return -EINVAL;
3549
3550 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3551 return -EINVAL;
3552
Jouni Malinen636a5d32009-03-19 13:39:22 +02003553 rtnl_lock();
3554
Johannes Berg463d0182009-07-14 00:33:35 +02003555 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003556 if (err)
3557 goto unlock_rtnl;
3558
Johannes Berg79c97e92009-07-07 03:56:12 +02003559 if (!rdev->ops->deauth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003560 err = -EOPNOTSUPP;
3561 goto out;
3562 }
3563
Jouni Malineneec60b02009-03-20 21:21:19 +02003564 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3565 err = -EOPNOTSUPP;
3566 goto out;
3567 }
3568
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003569 if (!netif_running(dev)) {
3570 err = -ENETDOWN;
3571 goto out;
3572 }
3573
Johannes Berg19957bb2009-07-02 17:20:43 +02003574 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003575
Johannes Berg19957bb2009-07-02 17:20:43 +02003576 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3577 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003578 /* Reason Code 0 is reserved */
3579 err = -EINVAL;
3580 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003581 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003582
3583 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003584 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3585 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003586 }
3587
Johannes Berg79c97e92009-07-07 03:56:12 +02003588 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003589
3590out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003591 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003592 dev_put(dev);
3593unlock_rtnl:
3594 rtnl_unlock();
3595 return err;
3596}
3597
3598static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3599{
Johannes Berg79c97e92009-07-07 03:56:12 +02003600 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003601 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003602 const u8 *ie = NULL, *bssid;
3603 int err, ie_len = 0;
3604 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003605
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003606 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3607 return -EINVAL;
3608
3609 if (!info->attrs[NL80211_ATTR_MAC])
3610 return -EINVAL;
3611
3612 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3613 return -EINVAL;
3614
Jouni Malinen636a5d32009-03-19 13:39:22 +02003615 rtnl_lock();
3616
Johannes Berg463d0182009-07-14 00:33:35 +02003617 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003618 if (err)
3619 goto unlock_rtnl;
3620
Johannes Berg79c97e92009-07-07 03:56:12 +02003621 if (!rdev->ops->disassoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003622 err = -EOPNOTSUPP;
3623 goto out;
3624 }
3625
Jouni Malineneec60b02009-03-20 21:21:19 +02003626 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3627 err = -EOPNOTSUPP;
3628 goto out;
3629 }
3630
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003631 if (!netif_running(dev)) {
3632 err = -ENETDOWN;
3633 goto out;
3634 }
3635
Johannes Berg19957bb2009-07-02 17:20:43 +02003636 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003637
Johannes Berg19957bb2009-07-02 17:20:43 +02003638 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3639 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003640 /* Reason Code 0 is reserved */
3641 err = -EINVAL;
3642 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003643 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003644
3645 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003646 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3647 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003648 }
3649
Johannes Berg79c97e92009-07-07 03:56:12 +02003650 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003651
3652out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003653 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003654 dev_put(dev);
3655unlock_rtnl:
3656 rtnl_unlock();
3657 return err;
3658}
3659
Johannes Berg04a773a2009-04-19 21:24:32 +02003660static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3661{
Johannes Berg79c97e92009-07-07 03:56:12 +02003662 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003663 struct net_device *dev;
3664 struct cfg80211_ibss_params ibss;
3665 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003666 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003667 int err;
3668
Johannes Berg8e30bc52009-04-22 17:45:38 +02003669 memset(&ibss, 0, sizeof(ibss));
3670
Johannes Berg04a773a2009-04-19 21:24:32 +02003671 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3672 return -EINVAL;
3673
3674 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3675 !info->attrs[NL80211_ATTR_SSID] ||
3676 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3677 return -EINVAL;
3678
Johannes Berg8e30bc52009-04-22 17:45:38 +02003679 ibss.beacon_interval = 100;
3680
3681 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3682 ibss.beacon_interval =
3683 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3684 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3685 return -EINVAL;
3686 }
3687
Johannes Berg04a773a2009-04-19 21:24:32 +02003688 rtnl_lock();
3689
Johannes Berg463d0182009-07-14 00:33:35 +02003690 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003691 if (err)
3692 goto unlock_rtnl;
3693
Johannes Berg79c97e92009-07-07 03:56:12 +02003694 if (!rdev->ops->join_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003695 err = -EOPNOTSUPP;
3696 goto out;
3697 }
3698
3699 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3700 err = -EOPNOTSUPP;
3701 goto out;
3702 }
3703
3704 if (!netif_running(dev)) {
3705 err = -ENETDOWN;
3706 goto out;
3707 }
3708
Johannes Berg79c97e92009-07-07 03:56:12 +02003709 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003710
3711 if (info->attrs[NL80211_ATTR_MAC])
3712 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3713 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3714 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3715
3716 if (info->attrs[NL80211_ATTR_IE]) {
3717 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3718 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3719 }
3720
3721 ibss.channel = ieee80211_get_channel(wiphy,
3722 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3723 if (!ibss.channel ||
3724 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3725 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3726 err = -EINVAL;
3727 goto out;
3728 }
3729
3730 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003731 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003732
Johannes Bergfffd0932009-07-08 14:22:54 +02003733 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3734 connkeys = nl80211_parse_connkeys(rdev,
3735 info->attrs[NL80211_ATTR_KEYS]);
3736 if (IS_ERR(connkeys)) {
3737 err = PTR_ERR(connkeys);
3738 connkeys = NULL;
3739 goto out;
3740 }
3741 }
3742
3743 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003744
3745out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003746 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003747 dev_put(dev);
3748unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02003749 if (err)
3750 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003751 rtnl_unlock();
3752 return err;
3753}
3754
3755static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3756{
Johannes Berg79c97e92009-07-07 03:56:12 +02003757 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003758 struct net_device *dev;
3759 int err;
3760
3761 rtnl_lock();
3762
Johannes Berg463d0182009-07-14 00:33:35 +02003763 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003764 if (err)
3765 goto unlock_rtnl;
3766
Johannes Berg79c97e92009-07-07 03:56:12 +02003767 if (!rdev->ops->leave_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003768 err = -EOPNOTSUPP;
3769 goto out;
3770 }
3771
3772 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3773 err = -EOPNOTSUPP;
3774 goto out;
3775 }
3776
3777 if (!netif_running(dev)) {
3778 err = -ENETDOWN;
3779 goto out;
3780 }
3781
Johannes Berg79c97e92009-07-07 03:56:12 +02003782 err = cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003783
3784out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003785 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003786 dev_put(dev);
3787unlock_rtnl:
3788 rtnl_unlock();
3789 return err;
3790}
3791
Johannes Bergaff89a92009-07-01 21:26:51 +02003792#ifdef CONFIG_NL80211_TESTMODE
3793static struct genl_multicast_group nl80211_testmode_mcgrp = {
3794 .name = "testmode",
3795};
3796
3797static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3798{
3799 struct cfg80211_registered_device *rdev;
3800 int err;
3801
3802 if (!info->attrs[NL80211_ATTR_TESTDATA])
3803 return -EINVAL;
3804
3805 rtnl_lock();
3806
3807 rdev = cfg80211_get_dev_from_info(info);
3808 if (IS_ERR(rdev)) {
3809 err = PTR_ERR(rdev);
3810 goto unlock_rtnl;
3811 }
3812
3813 err = -EOPNOTSUPP;
3814 if (rdev->ops->testmode_cmd) {
3815 rdev->testmode_info = info;
3816 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3817 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3818 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3819 rdev->testmode_info = NULL;
3820 }
3821
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003822 cfg80211_unlock_rdev(rdev);
Johannes Bergaff89a92009-07-01 21:26:51 +02003823
3824 unlock_rtnl:
3825 rtnl_unlock();
3826 return err;
3827}
3828
3829static struct sk_buff *
3830__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3831 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3832{
3833 struct sk_buff *skb;
3834 void *hdr;
3835 struct nlattr *data;
3836
3837 skb = nlmsg_new(approxlen + 100, gfp);
3838 if (!skb)
3839 return NULL;
3840
3841 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3842 if (!hdr) {
3843 kfree_skb(skb);
3844 return NULL;
3845 }
3846
3847 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3848 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3849
3850 ((void **)skb->cb)[0] = rdev;
3851 ((void **)skb->cb)[1] = hdr;
3852 ((void **)skb->cb)[2] = data;
3853
3854 return skb;
3855
3856 nla_put_failure:
3857 kfree_skb(skb);
3858 return NULL;
3859}
3860
3861struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3862 int approxlen)
3863{
3864 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3865
3866 if (WARN_ON(!rdev->testmode_info))
3867 return NULL;
3868
3869 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3870 rdev->testmode_info->snd_pid,
3871 rdev->testmode_info->snd_seq,
3872 GFP_KERNEL);
3873}
3874EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3875
3876int cfg80211_testmode_reply(struct sk_buff *skb)
3877{
3878 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3879 void *hdr = ((void **)skb->cb)[1];
3880 struct nlattr *data = ((void **)skb->cb)[2];
3881
3882 if (WARN_ON(!rdev->testmode_info)) {
3883 kfree_skb(skb);
3884 return -EINVAL;
3885 }
3886
3887 nla_nest_end(skb, data);
3888 genlmsg_end(skb, hdr);
3889 return genlmsg_reply(skb, rdev->testmode_info);
3890}
3891EXPORT_SYMBOL(cfg80211_testmode_reply);
3892
3893struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3894 int approxlen, gfp_t gfp)
3895{
3896 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3897
3898 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3899}
3900EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3901
3902void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3903{
3904 void *hdr = ((void **)skb->cb)[1];
3905 struct nlattr *data = ((void **)skb->cb)[2];
3906
3907 nla_nest_end(skb, data);
3908 genlmsg_end(skb, hdr);
3909 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3910}
3911EXPORT_SYMBOL(cfg80211_testmode_event);
3912#endif
3913
Samuel Ortizb23aa672009-07-01 21:26:54 +02003914static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3915{
Johannes Berg79c97e92009-07-07 03:56:12 +02003916 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003917 struct net_device *dev;
3918 struct cfg80211_connect_params connect;
3919 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003920 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003921 int err;
3922
3923 memset(&connect, 0, sizeof(connect));
3924
3925 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3926 return -EINVAL;
3927
3928 if (!info->attrs[NL80211_ATTR_SSID] ||
3929 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3930 return -EINVAL;
3931
3932 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3933 connect.auth_type =
3934 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3935 if (!nl80211_valid_auth_type(connect.auth_type))
3936 return -EINVAL;
3937 } else
3938 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3939
3940 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3941
Johannes Berg3dc27d22009-07-02 21:36:37 +02003942 err = nl80211_crypto_settings(info, &connect.crypto,
3943 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003944 if (err)
3945 return err;
3946 rtnl_lock();
3947
Johannes Berg463d0182009-07-14 00:33:35 +02003948 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003949 if (err)
3950 goto unlock_rtnl;
3951
3952 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3953 err = -EOPNOTSUPP;
3954 goto out;
3955 }
3956
3957 if (!netif_running(dev)) {
3958 err = -ENETDOWN;
3959 goto out;
3960 }
3961
Johannes Berg79c97e92009-07-07 03:56:12 +02003962 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003963
Samuel Ortizb23aa672009-07-01 21:26:54 +02003964 if (info->attrs[NL80211_ATTR_MAC])
3965 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3966 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3967 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3968
3969 if (info->attrs[NL80211_ATTR_IE]) {
3970 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3971 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3972 }
3973
3974 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3975 connect.channel =
3976 ieee80211_get_channel(wiphy,
3977 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3978 if (!connect.channel ||
3979 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
3980 err = -EINVAL;
3981 goto out;
3982 }
3983 }
3984
Johannes Bergfffd0932009-07-08 14:22:54 +02003985 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3986 connkeys = nl80211_parse_connkeys(rdev,
3987 info->attrs[NL80211_ATTR_KEYS]);
3988 if (IS_ERR(connkeys)) {
3989 err = PTR_ERR(connkeys);
3990 connkeys = NULL;
3991 goto out;
3992 }
3993 }
3994
3995 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003996
3997out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003998 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003999 dev_put(dev);
4000unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02004001 if (err)
4002 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004003 rtnl_unlock();
4004 return err;
4005}
4006
4007static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4008{
Johannes Berg79c97e92009-07-07 03:56:12 +02004009 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004010 struct net_device *dev;
4011 int err;
4012 u16 reason;
4013
4014 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4015 reason = WLAN_REASON_DEAUTH_LEAVING;
4016 else
4017 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4018
4019 if (reason == 0)
4020 return -EINVAL;
4021
4022 rtnl_lock();
4023
Johannes Berg463d0182009-07-14 00:33:35 +02004024 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004025 if (err)
4026 goto unlock_rtnl;
4027
4028 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4029 err = -EOPNOTSUPP;
4030 goto out;
4031 }
4032
4033 if (!netif_running(dev)) {
4034 err = -ENETDOWN;
4035 goto out;
4036 }
4037
Johannes Berg79c97e92009-07-07 03:56:12 +02004038 err = cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004039
4040out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004041 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004042 dev_put(dev);
4043unlock_rtnl:
4044 rtnl_unlock();
4045 return err;
4046}
4047
Johannes Berg463d0182009-07-14 00:33:35 +02004048static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4049{
4050 struct cfg80211_registered_device *rdev;
4051 struct net *net;
4052 int err;
4053 u32 pid;
4054
4055 if (!info->attrs[NL80211_ATTR_PID])
4056 return -EINVAL;
4057
4058 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4059
4060 rtnl_lock();
4061
4062 rdev = cfg80211_get_dev_from_info(info);
4063 if (IS_ERR(rdev)) {
4064 err = PTR_ERR(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004065 goto out_rtnl;
Johannes Berg463d0182009-07-14 00:33:35 +02004066 }
4067
4068 net = get_net_ns_by_pid(pid);
4069 if (IS_ERR(net)) {
4070 err = PTR_ERR(net);
4071 goto out;
4072 }
4073
4074 err = 0;
4075
4076 /* check if anything to do */
4077 if (net_eq(wiphy_net(&rdev->wiphy), net))
4078 goto out_put_net;
4079
4080 err = cfg80211_switch_netns(rdev, net);
4081 out_put_net:
4082 put_net(net);
4083 out:
4084 cfg80211_unlock_rdev(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004085 out_rtnl:
Johannes Berg463d0182009-07-14 00:33:35 +02004086 rtnl_unlock();
4087 return err;
4088}
4089
Johannes Berg55682962007-09-20 13:09:35 -04004090static struct genl_ops nl80211_ops[] = {
4091 {
4092 .cmd = NL80211_CMD_GET_WIPHY,
4093 .doit = nl80211_get_wiphy,
4094 .dumpit = nl80211_dump_wiphy,
4095 .policy = nl80211_policy,
4096 /* can be retrieved by unprivileged users */
4097 },
4098 {
4099 .cmd = NL80211_CMD_SET_WIPHY,
4100 .doit = nl80211_set_wiphy,
4101 .policy = nl80211_policy,
4102 .flags = GENL_ADMIN_PERM,
4103 },
4104 {
4105 .cmd = NL80211_CMD_GET_INTERFACE,
4106 .doit = nl80211_get_interface,
4107 .dumpit = nl80211_dump_interface,
4108 .policy = nl80211_policy,
4109 /* can be retrieved by unprivileged users */
4110 },
4111 {
4112 .cmd = NL80211_CMD_SET_INTERFACE,
4113 .doit = nl80211_set_interface,
4114 .policy = nl80211_policy,
4115 .flags = GENL_ADMIN_PERM,
4116 },
4117 {
4118 .cmd = NL80211_CMD_NEW_INTERFACE,
4119 .doit = nl80211_new_interface,
4120 .policy = nl80211_policy,
4121 .flags = GENL_ADMIN_PERM,
4122 },
4123 {
4124 .cmd = NL80211_CMD_DEL_INTERFACE,
4125 .doit = nl80211_del_interface,
4126 .policy = nl80211_policy,
4127 .flags = GENL_ADMIN_PERM,
4128 },
Johannes Berg41ade002007-12-19 02:03:29 +01004129 {
4130 .cmd = NL80211_CMD_GET_KEY,
4131 .doit = nl80211_get_key,
4132 .policy = nl80211_policy,
4133 .flags = GENL_ADMIN_PERM,
4134 },
4135 {
4136 .cmd = NL80211_CMD_SET_KEY,
4137 .doit = nl80211_set_key,
4138 .policy = nl80211_policy,
4139 .flags = GENL_ADMIN_PERM,
4140 },
4141 {
4142 .cmd = NL80211_CMD_NEW_KEY,
4143 .doit = nl80211_new_key,
4144 .policy = nl80211_policy,
4145 .flags = GENL_ADMIN_PERM,
4146 },
4147 {
4148 .cmd = NL80211_CMD_DEL_KEY,
4149 .doit = nl80211_del_key,
4150 .policy = nl80211_policy,
4151 .flags = GENL_ADMIN_PERM,
4152 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004153 {
4154 .cmd = NL80211_CMD_SET_BEACON,
4155 .policy = nl80211_policy,
4156 .flags = GENL_ADMIN_PERM,
4157 .doit = nl80211_addset_beacon,
4158 },
4159 {
4160 .cmd = NL80211_CMD_NEW_BEACON,
4161 .policy = nl80211_policy,
4162 .flags = GENL_ADMIN_PERM,
4163 .doit = nl80211_addset_beacon,
4164 },
4165 {
4166 .cmd = NL80211_CMD_DEL_BEACON,
4167 .policy = nl80211_policy,
4168 .flags = GENL_ADMIN_PERM,
4169 .doit = nl80211_del_beacon,
4170 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004171 {
4172 .cmd = NL80211_CMD_GET_STATION,
4173 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004174 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004175 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01004176 },
4177 {
4178 .cmd = NL80211_CMD_SET_STATION,
4179 .doit = nl80211_set_station,
4180 .policy = nl80211_policy,
4181 .flags = GENL_ADMIN_PERM,
4182 },
4183 {
4184 .cmd = NL80211_CMD_NEW_STATION,
4185 .doit = nl80211_new_station,
4186 .policy = nl80211_policy,
4187 .flags = GENL_ADMIN_PERM,
4188 },
4189 {
4190 .cmd = NL80211_CMD_DEL_STATION,
4191 .doit = nl80211_del_station,
4192 .policy = nl80211_policy,
4193 .flags = GENL_ADMIN_PERM,
4194 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004195 {
4196 .cmd = NL80211_CMD_GET_MPATH,
4197 .doit = nl80211_get_mpath,
4198 .dumpit = nl80211_dump_mpath,
4199 .policy = nl80211_policy,
4200 .flags = GENL_ADMIN_PERM,
4201 },
4202 {
4203 .cmd = NL80211_CMD_SET_MPATH,
4204 .doit = nl80211_set_mpath,
4205 .policy = nl80211_policy,
4206 .flags = GENL_ADMIN_PERM,
4207 },
4208 {
4209 .cmd = NL80211_CMD_NEW_MPATH,
4210 .doit = nl80211_new_mpath,
4211 .policy = nl80211_policy,
4212 .flags = GENL_ADMIN_PERM,
4213 },
4214 {
4215 .cmd = NL80211_CMD_DEL_MPATH,
4216 .doit = nl80211_del_mpath,
4217 .policy = nl80211_policy,
4218 .flags = GENL_ADMIN_PERM,
4219 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004220 {
4221 .cmd = NL80211_CMD_SET_BSS,
4222 .doit = nl80211_set_bss,
4223 .policy = nl80211_policy,
4224 .flags = GENL_ADMIN_PERM,
4225 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004226 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004227 .cmd = NL80211_CMD_GET_REG,
4228 .doit = nl80211_get_reg,
4229 .policy = nl80211_policy,
4230 /* can be retrieved by unprivileged users */
4231 },
4232 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004233 .cmd = NL80211_CMD_SET_REG,
4234 .doit = nl80211_set_reg,
4235 .policy = nl80211_policy,
4236 .flags = GENL_ADMIN_PERM,
4237 },
4238 {
4239 .cmd = NL80211_CMD_REQ_SET_REG,
4240 .doit = nl80211_req_set_reg,
4241 .policy = nl80211_policy,
4242 .flags = GENL_ADMIN_PERM,
4243 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004244 {
4245 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4246 .doit = nl80211_get_mesh_params,
4247 .policy = nl80211_policy,
4248 /* can be retrieved by unprivileged users */
4249 },
4250 {
4251 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4252 .doit = nl80211_set_mesh_params,
4253 .policy = nl80211_policy,
4254 .flags = GENL_ADMIN_PERM,
4255 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004256 {
Johannes Berg2a519312009-02-10 21:25:55 +01004257 .cmd = NL80211_CMD_TRIGGER_SCAN,
4258 .doit = nl80211_trigger_scan,
4259 .policy = nl80211_policy,
4260 .flags = GENL_ADMIN_PERM,
4261 },
4262 {
4263 .cmd = NL80211_CMD_GET_SCAN,
4264 .policy = nl80211_policy,
4265 .dumpit = nl80211_dump_scan,
4266 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004267 {
4268 .cmd = NL80211_CMD_AUTHENTICATE,
4269 .doit = nl80211_authenticate,
4270 .policy = nl80211_policy,
4271 .flags = GENL_ADMIN_PERM,
4272 },
4273 {
4274 .cmd = NL80211_CMD_ASSOCIATE,
4275 .doit = nl80211_associate,
4276 .policy = nl80211_policy,
4277 .flags = GENL_ADMIN_PERM,
4278 },
4279 {
4280 .cmd = NL80211_CMD_DEAUTHENTICATE,
4281 .doit = nl80211_deauthenticate,
4282 .policy = nl80211_policy,
4283 .flags = GENL_ADMIN_PERM,
4284 },
4285 {
4286 .cmd = NL80211_CMD_DISASSOCIATE,
4287 .doit = nl80211_disassociate,
4288 .policy = nl80211_policy,
4289 .flags = GENL_ADMIN_PERM,
4290 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004291 {
4292 .cmd = NL80211_CMD_JOIN_IBSS,
4293 .doit = nl80211_join_ibss,
4294 .policy = nl80211_policy,
4295 .flags = GENL_ADMIN_PERM,
4296 },
4297 {
4298 .cmd = NL80211_CMD_LEAVE_IBSS,
4299 .doit = nl80211_leave_ibss,
4300 .policy = nl80211_policy,
4301 .flags = GENL_ADMIN_PERM,
4302 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004303#ifdef CONFIG_NL80211_TESTMODE
4304 {
4305 .cmd = NL80211_CMD_TESTMODE,
4306 .doit = nl80211_testmode_do,
4307 .policy = nl80211_policy,
4308 .flags = GENL_ADMIN_PERM,
4309 },
4310#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004311 {
4312 .cmd = NL80211_CMD_CONNECT,
4313 .doit = nl80211_connect,
4314 .policy = nl80211_policy,
4315 .flags = GENL_ADMIN_PERM,
4316 },
4317 {
4318 .cmd = NL80211_CMD_DISCONNECT,
4319 .doit = nl80211_disconnect,
4320 .policy = nl80211_policy,
4321 .flags = GENL_ADMIN_PERM,
4322 },
Johannes Berg463d0182009-07-14 00:33:35 +02004323 {
4324 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4325 .doit = nl80211_wiphy_netns,
4326 .policy = nl80211_policy,
4327 .flags = GENL_ADMIN_PERM,
4328 },
Johannes Berg55682962007-09-20 13:09:35 -04004329};
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004330static struct genl_multicast_group nl80211_mlme_mcgrp = {
4331 .name = "mlme",
4332};
Johannes Berg55682962007-09-20 13:09:35 -04004333
4334/* multicast groups */
4335static struct genl_multicast_group nl80211_config_mcgrp = {
4336 .name = "config",
4337};
Johannes Berg2a519312009-02-10 21:25:55 +01004338static struct genl_multicast_group nl80211_scan_mcgrp = {
4339 .name = "scan",
4340};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004341static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4342 .name = "regulatory",
4343};
Johannes Berg55682962007-09-20 13:09:35 -04004344
4345/* notification functions */
4346
4347void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4348{
4349 struct sk_buff *msg;
4350
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004351 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004352 if (!msg)
4353 return;
4354
4355 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4356 nlmsg_free(msg);
4357 return;
4358 }
4359
Johannes Berg463d0182009-07-14 00:33:35 +02004360 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4361 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004362}
4363
Johannes Berg362a4152009-05-24 16:43:15 +02004364static int nl80211_add_scan_req(struct sk_buff *msg,
4365 struct cfg80211_registered_device *rdev)
4366{
4367 struct cfg80211_scan_request *req = rdev->scan_req;
4368 struct nlattr *nest;
4369 int i;
4370
Johannes Berg667503d2009-07-07 03:56:11 +02004371 ASSERT_RDEV_LOCK(rdev);
4372
Johannes Berg362a4152009-05-24 16:43:15 +02004373 if (WARN_ON(!req))
4374 return 0;
4375
4376 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4377 if (!nest)
4378 goto nla_put_failure;
4379 for (i = 0; i < req->n_ssids; i++)
4380 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4381 nla_nest_end(msg, nest);
4382
4383 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4384 if (!nest)
4385 goto nla_put_failure;
4386 for (i = 0; i < req->n_channels; i++)
4387 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4388 nla_nest_end(msg, nest);
4389
4390 if (req->ie)
4391 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4392
4393 return 0;
4394 nla_put_failure:
4395 return -ENOBUFS;
4396}
4397
Johannes Berga538e2d2009-06-16 19:56:42 +02004398static int nl80211_send_scan_msg(struct sk_buff *msg,
4399 struct cfg80211_registered_device *rdev,
4400 struct net_device *netdev,
4401 u32 pid, u32 seq, int flags,
4402 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01004403{
4404 void *hdr;
4405
4406 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4407 if (!hdr)
4408 return -1;
4409
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05004410 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01004411 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4412
Johannes Berg362a4152009-05-24 16:43:15 +02004413 /* ignore errors and send incomplete event anyway */
4414 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004415
4416 return genlmsg_end(msg, hdr);
4417
4418 nla_put_failure:
4419 genlmsg_cancel(msg, hdr);
4420 return -EMSGSIZE;
4421}
4422
Johannes Berga538e2d2009-06-16 19:56:42 +02004423void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4424 struct net_device *netdev)
4425{
4426 struct sk_buff *msg;
4427
4428 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4429 if (!msg)
4430 return;
4431
4432 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4433 NL80211_CMD_TRIGGER_SCAN) < 0) {
4434 nlmsg_free(msg);
4435 return;
4436 }
4437
Johannes Berg463d0182009-07-14 00:33:35 +02004438 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4439 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02004440}
4441
Johannes Berg2a519312009-02-10 21:25:55 +01004442void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4443 struct net_device *netdev)
4444{
4445 struct sk_buff *msg;
4446
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004447 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004448 if (!msg)
4449 return;
4450
Johannes Berga538e2d2009-06-16 19:56:42 +02004451 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4452 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004453 nlmsg_free(msg);
4454 return;
4455 }
4456
Johannes Berg463d0182009-07-14 00:33:35 +02004457 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4458 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004459}
4460
4461void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4462 struct net_device *netdev)
4463{
4464 struct sk_buff *msg;
4465
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004466 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004467 if (!msg)
4468 return;
4469
Johannes Berga538e2d2009-06-16 19:56:42 +02004470 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4471 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004472 nlmsg_free(msg);
4473 return;
4474 }
4475
Johannes Berg463d0182009-07-14 00:33:35 +02004476 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4477 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004478}
4479
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004480/*
4481 * This can happen on global regulatory changes or device specific settings
4482 * based on custom world regulatory domains.
4483 */
4484void nl80211_send_reg_change_event(struct regulatory_request *request)
4485{
4486 struct sk_buff *msg;
4487 void *hdr;
4488
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004489 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004490 if (!msg)
4491 return;
4492
4493 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
4494 if (!hdr) {
4495 nlmsg_free(msg);
4496 return;
4497 }
4498
4499 /* Userspace can always count this one always being set */
4500 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
4501
4502 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
4503 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4504 NL80211_REGDOM_TYPE_WORLD);
4505 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
4506 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4507 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
4508 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
4509 request->intersect)
4510 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4511 NL80211_REGDOM_TYPE_INTERSECTION);
4512 else {
4513 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4514 NL80211_REGDOM_TYPE_COUNTRY);
4515 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
4516 }
4517
4518 if (wiphy_idx_valid(request->wiphy_idx))
4519 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
4520
4521 if (genlmsg_end(msg, hdr) < 0) {
4522 nlmsg_free(msg);
4523 return;
4524 }
4525
Johannes Bergbc43b282009-07-25 10:54:13 +02004526 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02004527 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02004528 GFP_ATOMIC);
4529 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004530
4531 return;
4532
4533nla_put_failure:
4534 genlmsg_cancel(msg, hdr);
4535 nlmsg_free(msg);
4536}
4537
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004538static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
4539 struct net_device *netdev,
4540 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004541 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004542{
4543 struct sk_buff *msg;
4544 void *hdr;
4545
Johannes Berge6d6e342009-07-01 21:26:47 +02004546 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004547 if (!msg)
4548 return;
4549
4550 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4551 if (!hdr) {
4552 nlmsg_free(msg);
4553 return;
4554 }
4555
4556 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4557 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4558 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
4559
4560 if (genlmsg_end(msg, hdr) < 0) {
4561 nlmsg_free(msg);
4562 return;
4563 }
4564
Johannes Berg463d0182009-07-14 00:33:35 +02004565 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4566 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004567 return;
4568
4569 nla_put_failure:
4570 genlmsg_cancel(msg, hdr);
4571 nlmsg_free(msg);
4572}
4573
4574void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004575 struct net_device *netdev, const u8 *buf,
4576 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004577{
4578 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004579 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004580}
4581
4582void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
4583 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02004584 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004585{
Johannes Berge6d6e342009-07-01 21:26:47 +02004586 nl80211_send_mlme_event(rdev, netdev, buf, len,
4587 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004588}
4589
Jouni Malinen53b46b82009-03-27 20:53:56 +02004590void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004591 struct net_device *netdev, const u8 *buf,
4592 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004593{
4594 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004595 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004596}
4597
Jouni Malinen53b46b82009-03-27 20:53:56 +02004598void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
4599 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02004600 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004601{
4602 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004603 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004604}
4605
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04004606static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
4607 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02004608 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004609{
4610 struct sk_buff *msg;
4611 void *hdr;
4612
Johannes Berge6d6e342009-07-01 21:26:47 +02004613 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004614 if (!msg)
4615 return;
4616
4617 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4618 if (!hdr) {
4619 nlmsg_free(msg);
4620 return;
4621 }
4622
4623 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4624 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4625 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
4626 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4627
4628 if (genlmsg_end(msg, hdr) < 0) {
4629 nlmsg_free(msg);
4630 return;
4631 }
4632
Johannes Berg463d0182009-07-14 00:33:35 +02004633 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4634 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004635 return;
4636
4637 nla_put_failure:
4638 genlmsg_cancel(msg, hdr);
4639 nlmsg_free(msg);
4640}
4641
4642void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004643 struct net_device *netdev, const u8 *addr,
4644 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004645{
4646 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02004647 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004648}
4649
4650void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004651 struct net_device *netdev, const u8 *addr,
4652 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004653{
Johannes Berge6d6e342009-07-01 21:26:47 +02004654 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
4655 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004656}
4657
Samuel Ortizb23aa672009-07-01 21:26:54 +02004658void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
4659 struct net_device *netdev, const u8 *bssid,
4660 const u8 *req_ie, size_t req_ie_len,
4661 const u8 *resp_ie, size_t resp_ie_len,
4662 u16 status, gfp_t gfp)
4663{
4664 struct sk_buff *msg;
4665 void *hdr;
4666
4667 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4668 if (!msg)
4669 return;
4670
4671 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
4672 if (!hdr) {
4673 nlmsg_free(msg);
4674 return;
4675 }
4676
4677 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4678 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4679 if (bssid)
4680 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4681 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
4682 if (req_ie)
4683 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4684 if (resp_ie)
4685 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4686
4687 if (genlmsg_end(msg, hdr) < 0) {
4688 nlmsg_free(msg);
4689 return;
4690 }
4691
Johannes Berg463d0182009-07-14 00:33:35 +02004692 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4693 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004694 return;
4695
4696 nla_put_failure:
4697 genlmsg_cancel(msg, hdr);
4698 nlmsg_free(msg);
4699
4700}
4701
4702void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
4703 struct net_device *netdev, const u8 *bssid,
4704 const u8 *req_ie, size_t req_ie_len,
4705 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
4706{
4707 struct sk_buff *msg;
4708 void *hdr;
4709
4710 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4711 if (!msg)
4712 return;
4713
4714 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
4715 if (!hdr) {
4716 nlmsg_free(msg);
4717 return;
4718 }
4719
4720 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4721 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4722 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4723 if (req_ie)
4724 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4725 if (resp_ie)
4726 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4727
4728 if (genlmsg_end(msg, hdr) < 0) {
4729 nlmsg_free(msg);
4730 return;
4731 }
4732
Johannes Berg463d0182009-07-14 00:33:35 +02004733 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4734 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004735 return;
4736
4737 nla_put_failure:
4738 genlmsg_cancel(msg, hdr);
4739 nlmsg_free(msg);
4740
4741}
4742
4743void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
4744 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02004745 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004746{
4747 struct sk_buff *msg;
4748 void *hdr;
4749
Johannes Berg667503d2009-07-07 03:56:11 +02004750 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004751 if (!msg)
4752 return;
4753
4754 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
4755 if (!hdr) {
4756 nlmsg_free(msg);
4757 return;
4758 }
4759
4760 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4761 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4762 if (from_ap && reason)
4763 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
4764 if (from_ap)
4765 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
4766 if (ie)
4767 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
4768
4769 if (genlmsg_end(msg, hdr) < 0) {
4770 nlmsg_free(msg);
4771 return;
4772 }
4773
Johannes Berg463d0182009-07-14 00:33:35 +02004774 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4775 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004776 return;
4777
4778 nla_put_failure:
4779 genlmsg_cancel(msg, hdr);
4780 nlmsg_free(msg);
4781
4782}
4783
Johannes Berg04a773a2009-04-19 21:24:32 +02004784void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
4785 struct net_device *netdev, const u8 *bssid,
4786 gfp_t gfp)
4787{
4788 struct sk_buff *msg;
4789 void *hdr;
4790
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004791 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02004792 if (!msg)
4793 return;
4794
4795 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
4796 if (!hdr) {
4797 nlmsg_free(msg);
4798 return;
4799 }
4800
4801 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4802 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4803 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4804
4805 if (genlmsg_end(msg, hdr) < 0) {
4806 nlmsg_free(msg);
4807 return;
4808 }
4809
Johannes Berg463d0182009-07-14 00:33:35 +02004810 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4811 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02004812 return;
4813
4814 nla_put_failure:
4815 genlmsg_cancel(msg, hdr);
4816 nlmsg_free(msg);
4817}
4818
Jouni Malinena3b8b052009-03-27 21:59:49 +02004819void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
4820 struct net_device *netdev, const u8 *addr,
4821 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02004822 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02004823{
4824 struct sk_buff *msg;
4825 void *hdr;
4826
Johannes Berge6d6e342009-07-01 21:26:47 +02004827 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02004828 if (!msg)
4829 return;
4830
4831 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
4832 if (!hdr) {
4833 nlmsg_free(msg);
4834 return;
4835 }
4836
4837 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4838 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4839 if (addr)
4840 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4841 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
4842 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
4843 if (tsc)
4844 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
4845
4846 if (genlmsg_end(msg, hdr) < 0) {
4847 nlmsg_free(msg);
4848 return;
4849 }
4850
Johannes Berg463d0182009-07-14 00:33:35 +02004851 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4852 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02004853 return;
4854
4855 nla_put_failure:
4856 genlmsg_cancel(msg, hdr);
4857 nlmsg_free(msg);
4858}
4859
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04004860void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
4861 struct ieee80211_channel *channel_before,
4862 struct ieee80211_channel *channel_after)
4863{
4864 struct sk_buff *msg;
4865 void *hdr;
4866 struct nlattr *nl_freq;
4867
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004868 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04004869 if (!msg)
4870 return;
4871
4872 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
4873 if (!hdr) {
4874 nlmsg_free(msg);
4875 return;
4876 }
4877
4878 /*
4879 * Since we are applying the beacon hint to a wiphy we know its
4880 * wiphy_idx is valid
4881 */
4882 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
4883
4884 /* Before */
4885 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
4886 if (!nl_freq)
4887 goto nla_put_failure;
4888 if (nl80211_msg_put_channel(msg, channel_before))
4889 goto nla_put_failure;
4890 nla_nest_end(msg, nl_freq);
4891
4892 /* After */
4893 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
4894 if (!nl_freq)
4895 goto nla_put_failure;
4896 if (nl80211_msg_put_channel(msg, channel_after))
4897 goto nla_put_failure;
4898 nla_nest_end(msg, nl_freq);
4899
4900 if (genlmsg_end(msg, hdr) < 0) {
4901 nlmsg_free(msg);
4902 return;
4903 }
4904
Johannes Berg463d0182009-07-14 00:33:35 +02004905 rcu_read_lock();
4906 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
4907 GFP_ATOMIC);
4908 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04004909
4910 return;
4911
4912nla_put_failure:
4913 genlmsg_cancel(msg, hdr);
4914 nlmsg_free(msg);
4915}
4916
Johannes Berg55682962007-09-20 13:09:35 -04004917/* initialisation/exit functions */
4918
4919int nl80211_init(void)
4920{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00004921 int err;
Johannes Berg55682962007-09-20 13:09:35 -04004922
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00004923 err = genl_register_family_with_ops(&nl80211_fam,
4924 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04004925 if (err)
4926 return err;
4927
Johannes Berg55682962007-09-20 13:09:35 -04004928 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
4929 if (err)
4930 goto err_out;
4931
Johannes Berg2a519312009-02-10 21:25:55 +01004932 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
4933 if (err)
4934 goto err_out;
4935
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004936 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
4937 if (err)
4938 goto err_out;
4939
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004940 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
4941 if (err)
4942 goto err_out;
4943
Johannes Bergaff89a92009-07-01 21:26:51 +02004944#ifdef CONFIG_NL80211_TESTMODE
4945 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
4946 if (err)
4947 goto err_out;
4948#endif
4949
Johannes Berg55682962007-09-20 13:09:35 -04004950 return 0;
4951 err_out:
4952 genl_unregister_family(&nl80211_fam);
4953 return err;
4954}
4955
4956void nl80211_exit(void)
4957{
4958 genl_unregister_family(&nl80211_fam);
4959}