blob: eddab097435cd9a0473b6f12f98575e45ef1e0f2 [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 Malinen36aedc902008-08-25 11:58:58 +0300114
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700115 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
116
Jouni Malinen36aedc902008-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 Malinendc6382c2009-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 },
Johannes Berg55682962007-09-20 13:09:35 -0400141};
142
Johannes Bergb9454e82009-07-08 13:29:08 +0200143/* policy for the attributes */
144static struct nla_policy
145nl80211_key_policy[NL80211_KEY_MAX + 1] __read_mostly = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200146 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200147 [NL80211_KEY_IDX] = { .type = NLA_U8 },
148 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
149 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
150 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
151 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
152};
153
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100154/* IE validation */
155static bool is_valid_ie_attr(const struct nlattr *attr)
156{
157 const u8 *pos;
158 int len;
159
160 if (!attr)
161 return true;
162
163 pos = nla_data(attr);
164 len = nla_len(attr);
165
166 while (len) {
167 u8 elemlen;
168
169 if (len < 2)
170 return false;
171 len -= 2;
172
173 elemlen = pos[1];
174 if (elemlen > len)
175 return false;
176
177 len -= elemlen;
178 pos += 2 + elemlen;
179 }
180
181 return true;
182}
183
Johannes Berg55682962007-09-20 13:09:35 -0400184/* message building helper */
185static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
186 int flags, u8 cmd)
187{
188 /* since there is no private header just add the generic one */
189 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
190}
191
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400192static int nl80211_msg_put_channel(struct sk_buff *msg,
193 struct ieee80211_channel *chan)
194{
195 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
196 chan->center_freq);
197
198 if (chan->flags & IEEE80211_CHAN_DISABLED)
199 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
200 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
201 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
202 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
203 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
204 if (chan->flags & IEEE80211_CHAN_RADAR)
205 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
206
207 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
208 DBM_TO_MBM(chan->max_power));
209
210 return 0;
211
212 nla_put_failure:
213 return -ENOBUFS;
214}
215
Johannes Berg55682962007-09-20 13:09:35 -0400216/* netlink command implementations */
217
Johannes Bergb9454e82009-07-08 13:29:08 +0200218struct key_parse {
219 struct key_params p;
220 int idx;
221 bool def, defmgmt;
222};
223
224static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
225{
226 struct nlattr *tb[NL80211_KEY_MAX + 1];
227 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
228 nl80211_key_policy);
229 if (err)
230 return err;
231
232 k->def = !!tb[NL80211_KEY_DEFAULT];
233 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
234
235 if (tb[NL80211_KEY_IDX])
236 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
237
238 if (tb[NL80211_KEY_DATA]) {
239 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
240 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
241 }
242
243 if (tb[NL80211_KEY_SEQ]) {
244 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
245 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
246 }
247
248 if (tb[NL80211_KEY_CIPHER])
249 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
250
251 return 0;
252}
253
254static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
255{
256 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
257 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
258 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
259 }
260
261 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
262 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
263 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
264 }
265
266 if (info->attrs[NL80211_ATTR_KEY_IDX])
267 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
268
269 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
270 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
271
272 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
273 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
274
275 return 0;
276}
277
278static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
279{
280 int err;
281
282 memset(k, 0, sizeof(*k));
283 k->idx = -1;
284
285 if (info->attrs[NL80211_ATTR_KEY])
286 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
287 else
288 err = nl80211_parse_key_old(info, k);
289
290 if (err)
291 return err;
292
293 if (k->def && k->defmgmt)
294 return -EINVAL;
295
296 if (k->idx != -1) {
297 if (k->defmgmt) {
298 if (k->idx < 4 || k->idx > 5)
299 return -EINVAL;
300 } else if (k->def) {
301 if (k->idx < 0 || k->idx > 3)
302 return -EINVAL;
303 } else {
304 if (k->idx < 0 || k->idx > 5)
305 return -EINVAL;
306 }
307 }
308
309 return 0;
310}
311
Johannes Bergfffd0932009-07-08 14:22:54 +0200312static struct cfg80211_cached_keys *
313nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
314 struct nlattr *keys)
315{
316 struct key_parse parse;
317 struct nlattr *key;
318 struct cfg80211_cached_keys *result;
319 int rem, err, def = 0;
320
321 result = kzalloc(sizeof(*result), GFP_KERNEL);
322 if (!result)
323 return ERR_PTR(-ENOMEM);
324
325 result->def = -1;
326 result->defmgmt = -1;
327
328 nla_for_each_nested(key, keys, rem) {
329 memset(&parse, 0, sizeof(parse));
330 parse.idx = -1;
331
332 err = nl80211_parse_key_new(key, &parse);
333 if (err)
334 goto error;
335 err = -EINVAL;
336 if (!parse.p.key)
337 goto error;
338 if (parse.idx < 0 || parse.idx > 4)
339 goto error;
340 if (parse.def) {
341 if (def)
342 goto error;
343 def = 1;
344 result->def = parse.idx;
345 } else if (parse.defmgmt)
346 goto error;
347 err = cfg80211_validate_key_settings(rdev, &parse.p,
348 parse.idx, NULL);
349 if (err)
350 goto error;
351 result->params[parse.idx].cipher = parse.p.cipher;
352 result->params[parse.idx].key_len = parse.p.key_len;
353 result->params[parse.idx].key = result->data[parse.idx];
354 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
355 }
356
357 return result;
358 error:
359 kfree(result);
360 return ERR_PTR(err);
361}
362
363static int nl80211_key_allowed(struct wireless_dev *wdev)
364{
365 ASSERT_WDEV_LOCK(wdev);
366
367 if (!netif_running(wdev->netdev))
368 return -ENETDOWN;
369
370 switch (wdev->iftype) {
371 case NL80211_IFTYPE_AP:
372 case NL80211_IFTYPE_AP_VLAN:
373 break;
374 case NL80211_IFTYPE_ADHOC:
375 if (!wdev->current_bss)
376 return -ENOLINK;
377 break;
378 case NL80211_IFTYPE_STATION:
379 if (wdev->sme_state != CFG80211_SME_CONNECTED)
380 return -ENOLINK;
381 break;
382 default:
383 return -EINVAL;
384 }
385
386 return 0;
387}
388
Johannes Berg55682962007-09-20 13:09:35 -0400389static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
390 struct cfg80211_registered_device *dev)
391{
392 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100393 struct nlattr *nl_bands, *nl_band;
394 struct nlattr *nl_freqs, *nl_freq;
395 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700396 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100397 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100398 enum ieee80211_band band;
399 struct ieee80211_channel *chan;
400 struct ieee80211_rate *rate;
401 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700402 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400403
404 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
405 if (!hdr)
406 return -1;
407
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500408 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400409 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200410
Johannes Bergf5ea9122009-08-07 16:17:38 +0200411 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
412 cfg80211_rdev_list_generation);
413
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200414 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
415 dev->wiphy.retry_short);
416 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
417 dev->wiphy.retry_long);
418 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
419 dev->wiphy.frag_threshold);
420 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
421 dev->wiphy.rts_threshold);
422
Johannes Berg2a519312009-02-10 21:25:55 +0100423 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
424 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200425 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
426 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100427
Johannes Berg25e47c12009-04-02 20:14:06 +0200428 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
429 sizeof(u32) * dev->wiphy.n_cipher_suites,
430 dev->wiphy.cipher_suites);
431
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700432 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
433 if (!nl_modes)
434 goto nla_put_failure;
435
436 i = 0;
437 while (ifmodes) {
438 if (ifmodes & 1)
439 NLA_PUT_FLAG(msg, i);
440 ifmodes >>= 1;
441 i++;
442 }
443
444 nla_nest_end(msg, nl_modes);
445
Johannes Bergee688b002008-01-24 19:38:39 +0100446 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
447 if (!nl_bands)
448 goto nla_put_failure;
449
450 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
451 if (!dev->wiphy.bands[band])
452 continue;
453
454 nl_band = nla_nest_start(msg, band);
455 if (!nl_band)
456 goto nla_put_failure;
457
Johannes Bergd51626d2008-10-09 12:20:13 +0200458 /* add HT info */
459 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
460 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
461 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
462 &dev->wiphy.bands[band]->ht_cap.mcs);
463 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
464 dev->wiphy.bands[band]->ht_cap.cap);
465 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
466 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
467 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
468 dev->wiphy.bands[band]->ht_cap.ampdu_density);
469 }
470
Johannes Bergee688b002008-01-24 19:38:39 +0100471 /* add frequencies */
472 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
473 if (!nl_freqs)
474 goto nla_put_failure;
475
476 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
477 nl_freq = nla_nest_start(msg, i);
478 if (!nl_freq)
479 goto nla_put_failure;
480
481 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100482
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400483 if (nl80211_msg_put_channel(msg, chan))
484 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200485
Johannes Bergee688b002008-01-24 19:38:39 +0100486 nla_nest_end(msg, nl_freq);
487 }
488
489 nla_nest_end(msg, nl_freqs);
490
491 /* add bitrates */
492 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
493 if (!nl_rates)
494 goto nla_put_failure;
495
496 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
497 nl_rate = nla_nest_start(msg, i);
498 if (!nl_rate)
499 goto nla_put_failure;
500
501 rate = &dev->wiphy.bands[band]->bitrates[i];
502 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
503 rate->bitrate);
504 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
505 NLA_PUT_FLAG(msg,
506 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
507
508 nla_nest_end(msg, nl_rate);
509 }
510
511 nla_nest_end(msg, nl_rates);
512
513 nla_nest_end(msg, nl_band);
514 }
515 nla_nest_end(msg, nl_bands);
516
Johannes Berg8fdc6212009-03-14 09:34:01 +0100517 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
518 if (!nl_cmds)
519 goto nla_put_failure;
520
521 i = 0;
522#define CMD(op, n) \
523 do { \
524 if (dev->ops->op) { \
525 i++; \
526 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
527 } \
528 } while (0)
529
530 CMD(add_virtual_intf, NEW_INTERFACE);
531 CMD(change_virtual_intf, SET_INTERFACE);
532 CMD(add_key, NEW_KEY);
533 CMD(add_beacon, NEW_BEACON);
534 CMD(add_station, NEW_STATION);
535 CMD(add_mpath, NEW_MPATH);
536 CMD(set_mesh_params, SET_MESH_PARAMS);
537 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200538 CMD(auth, AUTHENTICATE);
539 CMD(assoc, ASSOCIATE);
540 CMD(deauth, DEAUTHENTICATE);
541 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200542 CMD(join_ibss, JOIN_IBSS);
Johannes Berg463d0182009-07-14 00:33:35 +0200543 if (dev->wiphy.netnsok) {
544 i++;
545 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
546 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100547
548#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200549
Johannes Berg6829c872009-07-02 09:13:27 +0200550 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200551 i++;
552 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
553 }
554
Johannes Berg6829c872009-07-02 09:13:27 +0200555 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200556 i++;
557 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
558 }
559
Johannes Berg8fdc6212009-03-14 09:34:01 +0100560 nla_nest_end(msg, nl_cmds);
561
Johannes Berg55682962007-09-20 13:09:35 -0400562 return genlmsg_end(msg, hdr);
563
564 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700565 genlmsg_cancel(msg, hdr);
566 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400567}
568
569static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
570{
571 int idx = 0;
572 int start = cb->args[0];
573 struct cfg80211_registered_device *dev;
574
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500575 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200576 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200577 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
578 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200579 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400580 continue;
581 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
582 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200583 dev) < 0) {
584 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400585 break;
Julius Volzb4637272008-07-08 14:02:19 +0200586 }
Johannes Berg55682962007-09-20 13:09:35 -0400587 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500588 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400589
590 cb->args[0] = idx;
591
592 return skb->len;
593}
594
595static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
596{
597 struct sk_buff *msg;
598 struct cfg80211_registered_device *dev;
599
600 dev = cfg80211_get_dev_from_info(info);
601 if (IS_ERR(dev))
602 return PTR_ERR(dev);
603
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700604 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400605 if (!msg)
606 goto out_err;
607
608 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
609 goto out_free;
610
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200611 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400612
Johannes Berg134e6372009-07-10 09:51:34 +0000613 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400614
615 out_free:
616 nlmsg_free(msg);
617 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200618 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400619 return -ENOBUFS;
620}
621
Jouni Malinen31888482008-10-30 16:59:24 +0200622static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
623 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
624 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
625 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
626 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
627 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
628};
629
630static int parse_txq_params(struct nlattr *tb[],
631 struct ieee80211_txq_params *txq_params)
632{
633 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
634 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
635 !tb[NL80211_TXQ_ATTR_AIFS])
636 return -EINVAL;
637
638 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
639 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
640 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
641 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
642 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
643
644 return 0;
645}
646
Johannes Berg55682962007-09-20 13:09:35 -0400647static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
648{
649 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200650 int result = 0, rem_txq_params = 0;
651 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200652 u32 changed;
653 u8 retry_short = 0, retry_long = 0;
654 u32 frag_threshold = 0, rts_threshold = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400655
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100656 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400657
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100658 mutex_lock(&cfg80211_mutex);
659
Johannes Berg79c97e92009-07-07 03:56:12 +0200660 rdev = __cfg80211_rdev_from_info(info);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100661 if (IS_ERR(rdev)) {
Jiri Slaby1f5fc702009-06-20 12:31:11 +0200662 mutex_unlock(&cfg80211_mutex);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100663 result = PTR_ERR(rdev);
664 goto unlock;
665 }
666
667 mutex_lock(&rdev->mtx);
668
669 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200670 result = cfg80211_dev_rename(
671 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100672
673 mutex_unlock(&cfg80211_mutex);
674
675 if (result)
676 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400677
Jouni Malinen31888482008-10-30 16:59:24 +0200678 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
679 struct ieee80211_txq_params txq_params;
680 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
681
682 if (!rdev->ops->set_txq_params) {
683 result = -EOPNOTSUPP;
684 goto bad_res;
685 }
686
687 nla_for_each_nested(nl_txq_params,
688 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
689 rem_txq_params) {
690 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
691 nla_data(nl_txq_params),
692 nla_len(nl_txq_params),
693 txq_params_policy);
694 result = parse_txq_params(tb, &txq_params);
695 if (result)
696 goto bad_res;
697
698 result = rdev->ops->set_txq_params(&rdev->wiphy,
699 &txq_params);
700 if (result)
701 goto bad_res;
702 }
703 }
704
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200705 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530706 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400707 u32 freq;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200708
Johannes Berg306d6112008-12-08 12:39:04 +0100709 result = -EINVAL;
710
Sujith094d05d2008-12-12 11:57:43 +0530711 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
712 channel_type = nla_get_u32(info->attrs[
713 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
714 if (channel_type != NL80211_CHAN_NO_HT &&
715 channel_type != NL80211_CHAN_HT20 &&
716 channel_type != NL80211_CHAN_HT40PLUS &&
717 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200718 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200719 }
720
721 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
Johannes Berg306d6112008-12-08 12:39:04 +0100722
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200723 mutex_lock(&rdev->devlist_mtx);
Johannes Berg4b181142009-08-08 11:03:58 +0200724 result = rdev_set_freq(rdev, NULL, freq, channel_type);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200725 mutex_unlock(&rdev->devlist_mtx);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200726 if (result)
727 goto bad_res;
728 }
729
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200730 changed = 0;
731
732 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
733 retry_short = nla_get_u8(
734 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
735 if (retry_short == 0) {
736 result = -EINVAL;
737 goto bad_res;
738 }
739 changed |= WIPHY_PARAM_RETRY_SHORT;
740 }
741
742 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
743 retry_long = nla_get_u8(
744 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
745 if (retry_long == 0) {
746 result = -EINVAL;
747 goto bad_res;
748 }
749 changed |= WIPHY_PARAM_RETRY_LONG;
750 }
751
752 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
753 frag_threshold = nla_get_u32(
754 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
755 if (frag_threshold < 256) {
756 result = -EINVAL;
757 goto bad_res;
758 }
759 if (frag_threshold != (u32) -1) {
760 /*
761 * Fragments (apart from the last one) are required to
762 * have even length. Make the fragmentation code
763 * simpler by stripping LSB should someone try to use
764 * odd threshold value.
765 */
766 frag_threshold &= ~0x1;
767 }
768 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
769 }
770
771 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
772 rts_threshold = nla_get_u32(
773 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
774 changed |= WIPHY_PARAM_RTS_THRESHOLD;
775 }
776
777 if (changed) {
778 u8 old_retry_short, old_retry_long;
779 u32 old_frag_threshold, old_rts_threshold;
780
781 if (!rdev->ops->set_wiphy_params) {
782 result = -EOPNOTSUPP;
783 goto bad_res;
784 }
785
786 old_retry_short = rdev->wiphy.retry_short;
787 old_retry_long = rdev->wiphy.retry_long;
788 old_frag_threshold = rdev->wiphy.frag_threshold;
789 old_rts_threshold = rdev->wiphy.rts_threshold;
790
791 if (changed & WIPHY_PARAM_RETRY_SHORT)
792 rdev->wiphy.retry_short = retry_short;
793 if (changed & WIPHY_PARAM_RETRY_LONG)
794 rdev->wiphy.retry_long = retry_long;
795 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
796 rdev->wiphy.frag_threshold = frag_threshold;
797 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
798 rdev->wiphy.rts_threshold = rts_threshold;
799
800 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
801 if (result) {
802 rdev->wiphy.retry_short = old_retry_short;
803 rdev->wiphy.retry_long = old_retry_long;
804 rdev->wiphy.frag_threshold = old_frag_threshold;
805 rdev->wiphy.rts_threshold = old_rts_threshold;
806 }
807 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200808
Johannes Berg306d6112008-12-08 12:39:04 +0100809 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100810 mutex_unlock(&rdev->mtx);
811 unlock:
812 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400813 return result;
814}
815
816
817static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200818 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400819 struct net_device *dev)
820{
821 void *hdr;
822
823 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
824 if (!hdr)
825 return -1;
826
827 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200828 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400829 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200830 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200831
832 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
833 rdev->devlist_generation ^
834 (cfg80211_rdev_list_generation << 2));
835
Johannes Berg55682962007-09-20 13:09:35 -0400836 return genlmsg_end(msg, hdr);
837
838 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700839 genlmsg_cancel(msg, hdr);
840 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400841}
842
843static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
844{
845 int wp_idx = 0;
846 int if_idx = 0;
847 int wp_start = cb->args[0];
848 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +0200849 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -0400850 struct wireless_dev *wdev;
851
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500852 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200853 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
854 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +0200855 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200856 if (wp_idx < wp_start) {
857 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400858 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200859 }
Johannes Berg55682962007-09-20 13:09:35 -0400860 if_idx = 0;
861
Johannes Bergf5ea9122009-08-07 16:17:38 +0200862 mutex_lock(&rdev->devlist_mtx);
863 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200864 if (if_idx < if_start) {
865 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400866 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200867 }
Johannes Berg55682962007-09-20 13:09:35 -0400868 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
869 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +0200870 rdev, wdev->netdev) < 0) {
871 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200872 goto out;
873 }
874 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400875 }
Johannes Bergf5ea9122009-08-07 16:17:38 +0200876 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200877
878 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400879 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200880 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500881 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400882
883 cb->args[0] = wp_idx;
884 cb->args[1] = if_idx;
885
886 return skb->len;
887}
888
889static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
890{
891 struct sk_buff *msg;
892 struct cfg80211_registered_device *dev;
893 struct net_device *netdev;
894 int err;
895
Johannes Berg463d0182009-07-14 00:33:35 +0200896 err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400897 if (err)
898 return err;
899
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700900 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400901 if (!msg)
902 goto out_err;
903
Johannes Bergd7264052009-04-19 16:23:20 +0200904 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
905 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -0400906 goto out_free;
907
908 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200909 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400910
Johannes Berg134e6372009-07-10 09:51:34 +0000911 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400912
913 out_free:
914 nlmsg_free(msg);
915 out_err:
916 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200917 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400918 return -ENOBUFS;
919}
920
Michael Wu66f7ac52008-01-31 19:48:22 +0100921static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
922 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
923 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
924 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
925 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
926 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
927};
928
929static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
930{
931 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
932 int flag;
933
934 *mntrflags = 0;
935
936 if (!nla)
937 return -EINVAL;
938
939 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
940 nla, mntr_flags_policy))
941 return -EINVAL;
942
943 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
944 if (flags[flag])
945 *mntrflags |= (1<<flag);
946
947 return 0;
948}
949
Johannes Berg55682962007-09-20 13:09:35 -0400950static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
951{
Johannes Berg79c97e92009-07-07 03:56:12 +0200952 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100953 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +0200954 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200955 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -0400956 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +0200957 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100958 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -0400959
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100960 memset(&params, 0, sizeof(params));
961
Johannes Berg3b858752009-03-12 09:55:09 +0100962 rtnl_lock();
963
Johannes Berg463d0182009-07-14 00:33:35 +0200964 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400965 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100966 goto unlock_rtnl;
967
Johannes Berg04a773a2009-04-19 21:24:32 +0200968 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -0400969
Johannes Berg723b0382008-09-16 20:22:09 +0200970 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100971 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +0200972 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100973 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200974 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100975 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +0200976 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100977 }
Johannes Berg723b0382008-09-16 20:22:09 +0200978 }
979
Johannes Berg92ffe052008-09-16 20:39:36 +0200980 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200981 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +0200982 err = -EINVAL;
983 goto unlock;
984 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100985 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
986 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100987 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100988 }
989
Johannes Berg92ffe052008-09-16 20:39:36 +0200990 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200991 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +0200992 err = -EINVAL;
993 goto unlock;
994 }
995 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
996 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100997 if (err)
998 goto unlock;
999
1000 flags = &_flags;
1001 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001002 }
Johannes Berg3b858752009-03-12 09:55:09 +01001003
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001004 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001005 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001006 else
1007 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001008
Johannes Berg55682962007-09-20 13:09:35 -04001009 unlock:
Johannes Berge36d56b2009-06-09 21:04:43 +02001010 dev_put(dev);
Johannes Berg79c97e92009-07-07 03:56:12 +02001011 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001012 unlock_rtnl:
1013 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001014 return err;
1015}
1016
1017static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1018{
Johannes Berg79c97e92009-07-07 03:56:12 +02001019 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001020 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001021 int err;
1022 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001023 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001024
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001025 memset(&params, 0, sizeof(params));
1026
Johannes Berg55682962007-09-20 13:09:35 -04001027 if (!info->attrs[NL80211_ATTR_IFNAME])
1028 return -EINVAL;
1029
1030 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1031 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1032 if (type > NL80211_IFTYPE_MAX)
1033 return -EINVAL;
1034 }
1035
Johannes Berg3b858752009-03-12 09:55:09 +01001036 rtnl_lock();
1037
Johannes Berg79c97e92009-07-07 03:56:12 +02001038 rdev = cfg80211_get_dev_from_info(info);
1039 if (IS_ERR(rdev)) {
1040 err = PTR_ERR(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001041 goto unlock_rtnl;
1042 }
Johannes Berg55682962007-09-20 13:09:35 -04001043
Johannes Berg79c97e92009-07-07 03:56:12 +02001044 if (!rdev->ops->add_virtual_intf ||
1045 !(rdev->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -04001046 err = -EOPNOTSUPP;
1047 goto unlock;
1048 }
1049
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001050 if (type == NL80211_IFTYPE_MESH_POINT &&
1051 info->attrs[NL80211_ATTR_MESH_ID]) {
1052 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1053 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1054 }
1055
Michael Wu66f7ac52008-01-31 19:48:22 +01001056 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1057 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1058 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001059 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001060 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001061 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001062
Johannes Berg55682962007-09-20 13:09:35 -04001063 unlock:
Johannes Berg79c97e92009-07-07 03:56:12 +02001064 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001065 unlock_rtnl:
1066 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001067 return err;
1068}
1069
1070static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1071{
Johannes Berg79c97e92009-07-07 03:56:12 +02001072 struct cfg80211_registered_device *rdev;
Johannes Berg463d0182009-07-14 00:33:35 +02001073 int err;
Johannes Berg55682962007-09-20 13:09:35 -04001074 struct net_device *dev;
1075
Johannes Berg3b858752009-03-12 09:55:09 +01001076 rtnl_lock();
1077
Johannes Berg463d0182009-07-14 00:33:35 +02001078 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001079 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001080 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -04001081
Johannes Berg79c97e92009-07-07 03:56:12 +02001082 if (!rdev->ops->del_virtual_intf) {
Johannes Berg55682962007-09-20 13:09:35 -04001083 err = -EOPNOTSUPP;
1084 goto out;
1085 }
1086
Johannes Berg463d0182009-07-14 00:33:35 +02001087 err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001088
1089 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001090 cfg80211_unlock_rdev(rdev);
Johannes Berg463d0182009-07-14 00:33:35 +02001091 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001092 unlock_rtnl:
1093 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001094 return err;
1095}
1096
Johannes Berg41ade002007-12-19 02:03:29 +01001097struct get_key_cookie {
1098 struct sk_buff *msg;
1099 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001100 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001101};
1102
1103static void get_key_callback(void *c, struct key_params *params)
1104{
Johannes Bergb9454e82009-07-08 13:29:08 +02001105 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001106 struct get_key_cookie *cookie = c;
1107
1108 if (params->key)
1109 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1110 params->key_len, params->key);
1111
1112 if (params->seq)
1113 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1114 params->seq_len, params->seq);
1115
1116 if (params->cipher)
1117 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1118 params->cipher);
1119
Johannes Bergb9454e82009-07-08 13:29:08 +02001120 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1121 if (!key)
1122 goto nla_put_failure;
1123
1124 if (params->key)
1125 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1126 params->key_len, params->key);
1127
1128 if (params->seq)
1129 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1130 params->seq_len, params->seq);
1131
1132 if (params->cipher)
1133 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1134 params->cipher);
1135
1136 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1137
1138 nla_nest_end(cookie->msg, key);
1139
Johannes Berg41ade002007-12-19 02:03:29 +01001140 return;
1141 nla_put_failure:
1142 cookie->error = 1;
1143}
1144
1145static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1146{
Johannes Berg79c97e92009-07-07 03:56:12 +02001147 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001148 int err;
1149 struct net_device *dev;
1150 u8 key_idx = 0;
1151 u8 *mac_addr = NULL;
1152 struct get_key_cookie cookie = {
1153 .error = 0,
1154 };
1155 void *hdr;
1156 struct sk_buff *msg;
1157
1158 if (info->attrs[NL80211_ATTR_KEY_IDX])
1159 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1160
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001161 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001162 return -EINVAL;
1163
1164 if (info->attrs[NL80211_ATTR_MAC])
1165 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1166
Johannes Berg3b858752009-03-12 09:55:09 +01001167 rtnl_lock();
1168
Johannes Berg463d0182009-07-14 00:33:35 +02001169 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001170 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001171 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001172
Johannes Berg79c97e92009-07-07 03:56:12 +02001173 if (!rdev->ops->get_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001174 err = -EOPNOTSUPP;
1175 goto out;
1176 }
1177
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001178 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +01001179 if (!msg) {
1180 err = -ENOMEM;
1181 goto out;
1182 }
1183
1184 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1185 NL80211_CMD_NEW_KEY);
1186
1187 if (IS_ERR(hdr)) {
1188 err = PTR_ERR(hdr);
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001189 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001190 }
1191
1192 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001193 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001194
1195 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1196 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1197 if (mac_addr)
1198 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1199
Johannes Berg79c97e92009-07-07 03:56:12 +02001200 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001201 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001202
1203 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001204 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001205
1206 if (cookie.error)
1207 goto nla_put_failure;
1208
1209 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00001210 err = genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001211 goto out;
1212
1213 nla_put_failure:
1214 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001215 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001216 nlmsg_free(msg);
1217 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001218 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001219 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001220 unlock_rtnl:
1221 rtnl_unlock();
1222
Johannes Berg41ade002007-12-19 02:03:29 +01001223 return err;
1224}
1225
1226static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1227{
Johannes Berg79c97e92009-07-07 03:56:12 +02001228 struct cfg80211_registered_device *rdev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001229 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001230 int err;
1231 struct net_device *dev;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001232 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1233 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001234
Johannes Bergb9454e82009-07-08 13:29:08 +02001235 err = nl80211_parse_key(info, &key);
1236 if (err)
1237 return err;
1238
1239 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001240 return -EINVAL;
1241
Johannes Bergb9454e82009-07-08 13:29:08 +02001242 /* only support setting default key */
1243 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001244 return -EINVAL;
1245
Johannes Berg3b858752009-03-12 09:55:09 +01001246 rtnl_lock();
1247
Johannes Berg463d0182009-07-14 00:33:35 +02001248 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001249 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001250 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001251
Johannes Bergb9454e82009-07-08 13:29:08 +02001252 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001253 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001254 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001255 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001256
1257 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001258 err = -EOPNOTSUPP;
1259 goto out;
1260 }
1261
Johannes Bergfffd0932009-07-08 14:22:54 +02001262 wdev_lock(dev->ieee80211_ptr);
1263 err = nl80211_key_allowed(dev->ieee80211_ptr);
1264 if (!err)
1265 err = func(&rdev->wiphy, dev, key.idx);
1266
Johannes Berg08645122009-05-11 13:54:58 +02001267#ifdef CONFIG_WIRELESS_EXT
1268 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001269 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001270 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001271 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001272 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001273 }
1274#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001275 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001276
1277 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001278 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001279 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001280
1281 unlock_rtnl:
1282 rtnl_unlock();
1283
Johannes Berg41ade002007-12-19 02:03:29 +01001284 return err;
1285}
1286
1287static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1288{
Johannes Berg79c97e92009-07-07 03:56:12 +02001289 struct cfg80211_registered_device *rdev;
Johannes Bergfffd0932009-07-08 14:22:54 +02001290 int err;
Johannes Berg41ade002007-12-19 02:03:29 +01001291 struct net_device *dev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001292 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001293 u8 *mac_addr = NULL;
1294
Johannes Bergb9454e82009-07-08 13:29:08 +02001295 err = nl80211_parse_key(info, &key);
1296 if (err)
1297 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001298
Johannes Bergb9454e82009-07-08 13:29:08 +02001299 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001300 return -EINVAL;
1301
Johannes Berg41ade002007-12-19 02:03:29 +01001302 if (info->attrs[NL80211_ATTR_MAC])
1303 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1304
Johannes Berg3b858752009-03-12 09:55:09 +01001305 rtnl_lock();
1306
Johannes Berg463d0182009-07-14 00:33:35 +02001307 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001308 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001309 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001310
Johannes Berg79c97e92009-07-07 03:56:12 +02001311 if (!rdev->ops->add_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001312 err = -EOPNOTSUPP;
1313 goto out;
1314 }
1315
Johannes Bergfffd0932009-07-08 14:22:54 +02001316 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1317 err = -EINVAL;
1318 goto out;
1319 }
1320
1321 wdev_lock(dev->ieee80211_ptr);
1322 err = nl80211_key_allowed(dev->ieee80211_ptr);
1323 if (!err)
1324 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1325 mac_addr, &key.p);
1326 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001327
1328 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001329 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001330 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001331 unlock_rtnl:
1332 rtnl_unlock();
1333
Johannes Berg41ade002007-12-19 02:03:29 +01001334 return err;
1335}
1336
1337static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1338{
Johannes Berg79c97e92009-07-07 03:56:12 +02001339 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001340 int err;
1341 struct net_device *dev;
Johannes Berg41ade002007-12-19 02:03:29 +01001342 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001343 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001344
Johannes Bergb9454e82009-07-08 13:29:08 +02001345 err = nl80211_parse_key(info, &key);
1346 if (err)
1347 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001348
1349 if (info->attrs[NL80211_ATTR_MAC])
1350 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1351
Johannes Berg3b858752009-03-12 09:55:09 +01001352 rtnl_lock();
1353
Johannes Berg463d0182009-07-14 00:33:35 +02001354 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001355 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001356 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001357
Johannes Berg79c97e92009-07-07 03:56:12 +02001358 if (!rdev->ops->del_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001359 err = -EOPNOTSUPP;
1360 goto out;
1361 }
1362
Johannes Bergfffd0932009-07-08 14:22:54 +02001363 wdev_lock(dev->ieee80211_ptr);
1364 err = nl80211_key_allowed(dev->ieee80211_ptr);
1365 if (!err)
1366 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001367
Johannes Berg08645122009-05-11 13:54:58 +02001368#ifdef CONFIG_WIRELESS_EXT
1369 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001370 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001371 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001372 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001373 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1374 }
1375#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001376 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001377
Johannes Berg41ade002007-12-19 02:03:29 +01001378 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001379 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001380 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001381
1382 unlock_rtnl:
1383 rtnl_unlock();
1384
Johannes Berg41ade002007-12-19 02:03:29 +01001385 return err;
1386}
1387
Johannes Berged1b6cc2007-12-19 02:03:32 +01001388static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1389{
1390 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1391 struct beacon_parameters *info);
Johannes Berg79c97e92009-07-07 03:56:12 +02001392 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001393 int err;
1394 struct net_device *dev;
1395 struct beacon_parameters params;
1396 int haveinfo = 0;
1397
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001398 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1399 return -EINVAL;
1400
Johannes Berg3b858752009-03-12 09:55:09 +01001401 rtnl_lock();
1402
Johannes Berg463d0182009-07-14 00:33:35 +02001403 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001404 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001405 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001406
Jouni Malineneec60b02009-03-20 21:21:19 +02001407 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1408 err = -EOPNOTSUPP;
1409 goto out;
1410 }
1411
Johannes Berged1b6cc2007-12-19 02:03:32 +01001412 switch (info->genlhdr->cmd) {
1413 case NL80211_CMD_NEW_BEACON:
1414 /* these are required for NEW_BEACON */
1415 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1416 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1417 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1418 err = -EINVAL;
1419 goto out;
1420 }
1421
Johannes Berg79c97e92009-07-07 03:56:12 +02001422 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001423 break;
1424 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001425 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001426 break;
1427 default:
1428 WARN_ON(1);
1429 err = -EOPNOTSUPP;
1430 goto out;
1431 }
1432
1433 if (!call) {
1434 err = -EOPNOTSUPP;
1435 goto out;
1436 }
1437
1438 memset(&params, 0, sizeof(params));
1439
1440 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1441 params.interval =
1442 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1443 haveinfo = 1;
1444 }
1445
1446 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1447 params.dtim_period =
1448 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1449 haveinfo = 1;
1450 }
1451
1452 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1453 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1454 params.head_len =
1455 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1456 haveinfo = 1;
1457 }
1458
1459 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1460 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1461 params.tail_len =
1462 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1463 haveinfo = 1;
1464 }
1465
1466 if (!haveinfo) {
1467 err = -EINVAL;
1468 goto out;
1469 }
1470
Johannes Berg79c97e92009-07-07 03:56:12 +02001471 err = call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001472
1473 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001474 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001475 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001476 unlock_rtnl:
1477 rtnl_unlock();
1478
Johannes Berged1b6cc2007-12-19 02:03:32 +01001479 return err;
1480}
1481
1482static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1483{
Johannes Berg79c97e92009-07-07 03:56:12 +02001484 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001485 int err;
1486 struct net_device *dev;
1487
Johannes Berg3b858752009-03-12 09:55:09 +01001488 rtnl_lock();
1489
Johannes Berg463d0182009-07-14 00:33:35 +02001490 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001491 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001492 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001493
Johannes Berg79c97e92009-07-07 03:56:12 +02001494 if (!rdev->ops->del_beacon) {
Johannes Berged1b6cc2007-12-19 02:03:32 +01001495 err = -EOPNOTSUPP;
1496 goto out;
1497 }
1498
Jouni Malineneec60b02009-03-20 21:21:19 +02001499 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1500 err = -EOPNOTSUPP;
1501 goto out;
1502 }
Johannes Berg79c97e92009-07-07 03:56:12 +02001503 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001504
1505 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001506 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001507 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001508 unlock_rtnl:
1509 rtnl_unlock();
1510
Johannes Berged1b6cc2007-12-19 02:03:32 +01001511 return err;
1512}
1513
Johannes Berg5727ef12007-12-19 02:03:34 +01001514static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1515 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1516 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1517 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001518 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001519};
1520
Johannes Bergeccb8e82009-05-11 21:57:56 +03001521static int parse_station_flags(struct genl_info *info,
1522 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001523{
1524 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001525 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001526 int flag;
1527
Johannes Bergeccb8e82009-05-11 21:57:56 +03001528 /*
1529 * Try parsing the new attribute first so userspace
1530 * can specify both for older kernels.
1531 */
1532 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1533 if (nla) {
1534 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001535
Johannes Bergeccb8e82009-05-11 21:57:56 +03001536 sta_flags = nla_data(nla);
1537 params->sta_flags_mask = sta_flags->mask;
1538 params->sta_flags_set = sta_flags->set;
1539 if ((params->sta_flags_mask |
1540 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1541 return -EINVAL;
1542 return 0;
1543 }
1544
1545 /* if present, parse the old attribute */
1546
1547 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001548 if (!nla)
1549 return 0;
1550
1551 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1552 nla, sta_flags_policy))
1553 return -EINVAL;
1554
Johannes Bergeccb8e82009-05-11 21:57:56 +03001555 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1556 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001557
1558 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1559 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001560 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001561
1562 return 0;
1563}
1564
Henning Rogge420e7fa2008-12-11 22:04:19 +01001565static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1566{
1567 int modulation, streams, bitrate;
1568
1569 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1570 return rate->legacy;
1571
1572 /* the formula below does only work for MCS values smaller than 32 */
1573 if (rate->mcs >= 32)
1574 return 0;
1575
1576 modulation = rate->mcs & 7;
1577 streams = (rate->mcs >> 3) + 1;
1578
1579 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1580 13500000 : 6500000;
1581
1582 if (modulation < 4)
1583 bitrate *= (modulation + 1);
1584 else if (modulation == 4)
1585 bitrate *= (modulation + 2);
1586 else
1587 bitrate *= (modulation + 3);
1588
1589 bitrate *= streams;
1590
1591 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1592 bitrate = (bitrate / 9) * 10;
1593
1594 /* do NOT round down here */
1595 return (bitrate + 50000) / 100000;
1596}
1597
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001598static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1599 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001600 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001601{
1602 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001603 struct nlattr *sinfoattr, *txrate;
1604 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001605
1606 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1607 if (!hdr)
1608 return -1;
1609
1610 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1611 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1612
Johannes Bergf5ea9122009-08-07 16:17:38 +02001613 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1614
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001615 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1616 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001617 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001618 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1619 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1620 sinfo->inactive_time);
1621 if (sinfo->filled & STATION_INFO_RX_BYTES)
1622 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1623 sinfo->rx_bytes);
1624 if (sinfo->filled & STATION_INFO_TX_BYTES)
1625 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1626 sinfo->tx_bytes);
1627 if (sinfo->filled & STATION_INFO_LLID)
1628 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1629 sinfo->llid);
1630 if (sinfo->filled & STATION_INFO_PLID)
1631 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1632 sinfo->plid);
1633 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1634 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1635 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001636 if (sinfo->filled & STATION_INFO_SIGNAL)
1637 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1638 sinfo->signal);
1639 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1640 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1641 if (!txrate)
1642 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001643
Henning Rogge420e7fa2008-12-11 22:04:19 +01001644 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1645 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1646 if (bitrate > 0)
1647 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1648
1649 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1650 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1651 sinfo->txrate.mcs);
1652 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1653 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1654 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1655 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1656
1657 nla_nest_end(msg, txrate);
1658 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001659 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1660 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1661 sinfo->rx_packets);
1662 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1663 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1664 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001665 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001666
1667 return genlmsg_end(msg, hdr);
1668
1669 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001670 genlmsg_cancel(msg, hdr);
1671 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001672}
1673
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001674static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001675 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001676{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001677 struct station_info sinfo;
1678 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001679 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001680 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001681 int ifidx = cb->args[0];
1682 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001683 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001684
Johannes Bergbba95fe2008-07-29 13:22:51 +02001685 if (!ifidx) {
1686 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1687 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1688 nl80211_policy);
1689 if (err)
1690 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001691
Johannes Bergbba95fe2008-07-29 13:22:51 +02001692 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1693 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001694
Johannes Bergbba95fe2008-07-29 13:22:51 +02001695 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1696 if (!ifidx)
1697 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001698 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001699
Johannes Berg3b858752009-03-12 09:55:09 +01001700 rtnl_lock();
1701
Johannes Berg463d0182009-07-14 00:33:35 +02001702 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01001703 if (!netdev) {
1704 err = -ENODEV;
1705 goto out_rtnl;
1706 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001707
Johannes Berg463d0182009-07-14 00:33:35 +02001708 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001709 if (IS_ERR(dev)) {
1710 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001711 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001712 }
1713
1714 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001715 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001716 goto out_err;
1717 }
1718
Johannes Bergbba95fe2008-07-29 13:22:51 +02001719 while (1) {
1720 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1721 mac_addr, &sinfo);
1722 if (err == -ENOENT)
1723 break;
1724 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001725 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001726
1727 if (nl80211_send_station(skb,
1728 NETLINK_CB(cb->skb).pid,
1729 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1730 netdev, mac_addr,
1731 &sinfo) < 0)
1732 goto out;
1733
1734 sta_idx++;
1735 }
1736
1737
1738 out:
1739 cb->args[1] = sta_idx;
1740 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001741 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001742 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001743 out_rtnl:
1744 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001745
1746 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001747}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001748
Johannes Berg5727ef12007-12-19 02:03:34 +01001749static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1750{
Johannes Berg79c97e92009-07-07 03:56:12 +02001751 struct cfg80211_registered_device *rdev;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001752 int err;
1753 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001754 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001755 struct sk_buff *msg;
1756 u8 *mac_addr = NULL;
1757
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001758 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001759
1760 if (!info->attrs[NL80211_ATTR_MAC])
1761 return -EINVAL;
1762
1763 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1764
Johannes Berg3b858752009-03-12 09:55:09 +01001765 rtnl_lock();
1766
Johannes Berg463d0182009-07-14 00:33:35 +02001767 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001768 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001769 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001770
Johannes Berg79c97e92009-07-07 03:56:12 +02001771 if (!rdev->ops->get_station) {
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001772 err = -EOPNOTSUPP;
1773 goto out;
1774 }
1775
Johannes Berg79c97e92009-07-07 03:56:12 +02001776 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001777 if (err)
1778 goto out;
1779
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001780 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001781 if (!msg)
1782 goto out;
1783
1784 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001785 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001786 goto out_free;
1787
Johannes Berg134e6372009-07-10 09:51:34 +00001788 err = genlmsg_reply(msg, info);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001789 goto out;
1790
1791 out_free:
1792 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001793 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001794 cfg80211_unlock_rdev(rdev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001795 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001796 out_rtnl:
1797 rtnl_unlock();
1798
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001799 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001800}
1801
1802/*
1803 * Get vlan interface making sure it is on the right wiphy.
1804 */
Johannes Berg463d0182009-07-14 00:33:35 +02001805static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01001806 struct cfg80211_registered_device *rdev,
1807 struct net_device **vlan)
1808{
Johannes Berg463d0182009-07-14 00:33:35 +02001809 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01001810 *vlan = NULL;
1811
1812 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02001813 *vlan = dev_get_by_index(genl_info_net(info),
1814 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01001815 if (!*vlan)
1816 return -ENODEV;
1817 if (!(*vlan)->ieee80211_ptr)
1818 return -EINVAL;
1819 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1820 return -EINVAL;
1821 }
1822 return 0;
1823}
1824
1825static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1826{
Johannes Berg79c97e92009-07-07 03:56:12 +02001827 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001828 int err;
1829 struct net_device *dev;
1830 struct station_parameters params;
1831 u8 *mac_addr = NULL;
1832
1833 memset(&params, 0, sizeof(params));
1834
1835 params.listen_interval = -1;
1836
1837 if (info->attrs[NL80211_ATTR_STA_AID])
1838 return -EINVAL;
1839
1840 if (!info->attrs[NL80211_ATTR_MAC])
1841 return -EINVAL;
1842
1843 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1844
1845 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1846 params.supported_rates =
1847 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1848 params.supported_rates_len =
1849 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1850 }
1851
1852 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1853 params.listen_interval =
1854 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1855
Jouni Malinen36aedc902008-08-25 11:58:58 +03001856 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1857 params.ht_capa =
1858 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1859
Johannes Bergeccb8e82009-05-11 21:57:56 +03001860 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001861 return -EINVAL;
1862
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001863 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1864 params.plink_action =
1865 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1866
Johannes Berg3b858752009-03-12 09:55:09 +01001867 rtnl_lock();
1868
Johannes Berg463d0182009-07-14 00:33:35 +02001869 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001870 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001871 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001872
Johannes Berg463d0182009-07-14 00:33:35 +02001873 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001874 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02001875 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001876
1877 /* validate settings */
1878 err = 0;
1879
1880 switch (dev->ieee80211_ptr->iftype) {
1881 case NL80211_IFTYPE_AP:
1882 case NL80211_IFTYPE_AP_VLAN:
1883 /* disallow mesh-specific things */
1884 if (params.plink_action)
1885 err = -EINVAL;
1886 break;
1887 case NL80211_IFTYPE_STATION:
1888 /* disallow everything but AUTHORIZED flag */
1889 if (params.plink_action)
1890 err = -EINVAL;
1891 if (params.vlan)
1892 err = -EINVAL;
1893 if (params.supported_rates)
1894 err = -EINVAL;
1895 if (params.ht_capa)
1896 err = -EINVAL;
1897 if (params.listen_interval >= 0)
1898 err = -EINVAL;
1899 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
1900 err = -EINVAL;
1901 break;
1902 case NL80211_IFTYPE_MESH_POINT:
1903 /* disallow things mesh doesn't support */
1904 if (params.vlan)
1905 err = -EINVAL;
1906 if (params.ht_capa)
1907 err = -EINVAL;
1908 if (params.listen_interval >= 0)
1909 err = -EINVAL;
1910 if (params.supported_rates)
1911 err = -EINVAL;
1912 if (params.sta_flags_mask)
1913 err = -EINVAL;
1914 break;
1915 default:
1916 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02001917 }
1918
Johannes Berg5727ef12007-12-19 02:03:34 +01001919 if (err)
1920 goto out;
1921
Johannes Berg79c97e92009-07-07 03:56:12 +02001922 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01001923 err = -EOPNOTSUPP;
1924 goto out;
1925 }
1926
Johannes Berg79c97e92009-07-07 03:56:12 +02001927 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001928
1929 out:
1930 if (params.vlan)
1931 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02001932 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001933 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001934 out_rtnl:
1935 rtnl_unlock();
1936
Johannes Berg5727ef12007-12-19 02:03:34 +01001937 return err;
1938}
1939
1940static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1941{
Johannes Berg79c97e92009-07-07 03:56:12 +02001942 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001943 int err;
1944 struct net_device *dev;
1945 struct station_parameters params;
1946 u8 *mac_addr = NULL;
1947
1948 memset(&params, 0, sizeof(params));
1949
1950 if (!info->attrs[NL80211_ATTR_MAC])
1951 return -EINVAL;
1952
Johannes Berg5727ef12007-12-19 02:03:34 +01001953 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1954 return -EINVAL;
1955
1956 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1957 return -EINVAL;
1958
1959 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1960 params.supported_rates =
1961 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1962 params.supported_rates_len =
1963 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1964 params.listen_interval =
1965 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02001966
Johannes Berga97f4422009-06-18 17:23:43 +02001967 if (info->attrs[NL80211_ATTR_STA_AID]) {
1968 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1969 if (!params.aid || params.aid > IEEE80211_MAX_AID)
1970 return -EINVAL;
1971 }
Johannes Berg51b50fb2009-05-24 16:42:30 +02001972
Jouni Malinen36aedc902008-08-25 11:58:58 +03001973 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1974 params.ht_capa =
1975 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01001976
Johannes Bergeccb8e82009-05-11 21:57:56 +03001977 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001978 return -EINVAL;
1979
Johannes Berg3b858752009-03-12 09:55:09 +01001980 rtnl_lock();
1981
Johannes Berg463d0182009-07-14 00:33:35 +02001982 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001983 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001984 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001985
Johannes Berg463d0182009-07-14 00:33:35 +02001986 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001987 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02001988 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001989
1990 /* validate settings */
1991 err = 0;
1992
1993 switch (dev->ieee80211_ptr->iftype) {
1994 case NL80211_IFTYPE_AP:
1995 case NL80211_IFTYPE_AP_VLAN:
1996 /* all ok but must have AID */
1997 if (!params.aid)
1998 err = -EINVAL;
1999 break;
2000 case NL80211_IFTYPE_MESH_POINT:
2001 /* disallow things mesh doesn't support */
2002 if (params.vlan)
2003 err = -EINVAL;
2004 if (params.aid)
2005 err = -EINVAL;
2006 if (params.ht_capa)
2007 err = -EINVAL;
2008 if (params.listen_interval >= 0)
2009 err = -EINVAL;
2010 if (params.supported_rates)
2011 err = -EINVAL;
2012 if (params.sta_flags_mask)
2013 err = -EINVAL;
2014 break;
2015 default:
2016 err = -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002017 }
2018
Johannes Berg5727ef12007-12-19 02:03:34 +01002019 if (err)
2020 goto out;
2021
Johannes Berg79c97e92009-07-07 03:56:12 +02002022 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002023 err = -EOPNOTSUPP;
2024 goto out;
2025 }
2026
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002027 if (!netif_running(dev)) {
2028 err = -ENETDOWN;
2029 goto out;
2030 }
2031
Johannes Berg79c97e92009-07-07 03:56:12 +02002032 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002033
2034 out:
2035 if (params.vlan)
2036 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002037 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002038 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002039 out_rtnl:
2040 rtnl_unlock();
2041
Johannes Berg5727ef12007-12-19 02:03:34 +01002042 return err;
2043}
2044
2045static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2046{
Johannes Berg79c97e92009-07-07 03:56:12 +02002047 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002048 int err;
2049 struct net_device *dev;
2050 u8 *mac_addr = NULL;
2051
2052 if (info->attrs[NL80211_ATTR_MAC])
2053 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2054
Johannes Berg3b858752009-03-12 09:55:09 +01002055 rtnl_lock();
2056
Johannes Berg463d0182009-07-14 00:33:35 +02002057 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002058 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002059 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002060
Johannes Berge80cf852009-05-11 14:43:13 +02002061 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Andrey Yurovsky155cc9e2009-06-16 11:31:04 -07002062 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2063 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berge80cf852009-05-11 14:43:13 +02002064 err = -EINVAL;
2065 goto out;
2066 }
2067
Johannes Berg79c97e92009-07-07 03:56:12 +02002068 if (!rdev->ops->del_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002069 err = -EOPNOTSUPP;
2070 goto out;
2071 }
2072
Johannes Berg79c97e92009-07-07 03:56:12 +02002073 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002074
2075 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002076 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002077 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002078 out_rtnl:
2079 rtnl_unlock();
2080
Johannes Berg5727ef12007-12-19 02:03:34 +01002081 return err;
2082}
2083
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002084static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2085 int flags, struct net_device *dev,
2086 u8 *dst, u8 *next_hop,
2087 struct mpath_info *pinfo)
2088{
2089 void *hdr;
2090 struct nlattr *pinfoattr;
2091
2092 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2093 if (!hdr)
2094 return -1;
2095
2096 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2097 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2098 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2099
Johannes Bergf5ea9122009-08-07 16:17:38 +02002100 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2101
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002102 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2103 if (!pinfoattr)
2104 goto nla_put_failure;
2105 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2106 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2107 pinfo->frame_qlen);
2108 if (pinfo->filled & MPATH_INFO_DSN)
2109 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
2110 pinfo->dsn);
2111 if (pinfo->filled & MPATH_INFO_METRIC)
2112 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2113 pinfo->metric);
2114 if (pinfo->filled & MPATH_INFO_EXPTIME)
2115 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2116 pinfo->exptime);
2117 if (pinfo->filled & MPATH_INFO_FLAGS)
2118 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2119 pinfo->flags);
2120 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2121 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2122 pinfo->discovery_timeout);
2123 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2124 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2125 pinfo->discovery_retries);
2126
2127 nla_nest_end(msg, pinfoattr);
2128
2129 return genlmsg_end(msg, hdr);
2130
2131 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002132 genlmsg_cancel(msg, hdr);
2133 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002134}
2135
2136static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002137 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002138{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002139 struct mpath_info pinfo;
2140 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002141 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002142 u8 dst[ETH_ALEN];
2143 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002144 int ifidx = cb->args[0];
2145 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002146 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002147
Johannes Bergbba95fe2008-07-29 13:22:51 +02002148 if (!ifidx) {
2149 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2150 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2151 nl80211_policy);
2152 if (err)
2153 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002154
Johannes Bergbba95fe2008-07-29 13:22:51 +02002155 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2156 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002157
Johannes Bergbba95fe2008-07-29 13:22:51 +02002158 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2159 if (!ifidx)
2160 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002161 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002162
Johannes Berg3b858752009-03-12 09:55:09 +01002163 rtnl_lock();
2164
Johannes Berg463d0182009-07-14 00:33:35 +02002165 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01002166 if (!netdev) {
2167 err = -ENODEV;
2168 goto out_rtnl;
2169 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002170
Johannes Berg463d0182009-07-14 00:33:35 +02002171 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002172 if (IS_ERR(dev)) {
2173 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002174 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002175 }
2176
2177 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002178 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002179 goto out_err;
2180 }
2181
Jouni Malineneec60b02009-03-20 21:21:19 +02002182 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2183 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002184 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002185 }
2186
Johannes Bergbba95fe2008-07-29 13:22:51 +02002187 while (1) {
2188 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2189 dst, next_hop, &pinfo);
2190 if (err == -ENOENT)
2191 break;
2192 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002193 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002194
2195 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2196 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2197 netdev, dst, next_hop,
2198 &pinfo) < 0)
2199 goto out;
2200
2201 path_idx++;
2202 }
2203
2204
2205 out:
2206 cb->args[1] = path_idx;
2207 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002208 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02002209 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002210 out_rtnl:
2211 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002212
2213 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002214}
2215
2216static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2217{
Johannes Berg79c97e92009-07-07 03:56:12 +02002218 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002219 int err;
2220 struct net_device *dev;
2221 struct mpath_info pinfo;
2222 struct sk_buff *msg;
2223 u8 *dst = NULL;
2224 u8 next_hop[ETH_ALEN];
2225
2226 memset(&pinfo, 0, sizeof(pinfo));
2227
2228 if (!info->attrs[NL80211_ATTR_MAC])
2229 return -EINVAL;
2230
2231 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2232
Johannes Berg3b858752009-03-12 09:55:09 +01002233 rtnl_lock();
2234
Johannes Berg463d0182009-07-14 00:33:35 +02002235 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002236 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002237 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002238
Johannes Berg79c97e92009-07-07 03:56:12 +02002239 if (!rdev->ops->get_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002240 err = -EOPNOTSUPP;
2241 goto out;
2242 }
2243
Jouni Malineneec60b02009-03-20 21:21:19 +02002244 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2245 err = -EOPNOTSUPP;
2246 goto out;
2247 }
2248
Johannes Berg79c97e92009-07-07 03:56:12 +02002249 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002250 if (err)
2251 goto out;
2252
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002253 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002254 if (!msg)
2255 goto out;
2256
2257 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2258 dev, dst, next_hop, &pinfo) < 0)
2259 goto out_free;
2260
Johannes Berg134e6372009-07-10 09:51:34 +00002261 err = genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002262 goto out;
2263
2264 out_free:
2265 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002266 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002267 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002268 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002269 out_rtnl:
2270 rtnl_unlock();
2271
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002272 return err;
2273}
2274
2275static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2276{
Johannes Berg79c97e92009-07-07 03:56:12 +02002277 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002278 int err;
2279 struct net_device *dev;
2280 u8 *dst = NULL;
2281 u8 *next_hop = NULL;
2282
2283 if (!info->attrs[NL80211_ATTR_MAC])
2284 return -EINVAL;
2285
2286 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2287 return -EINVAL;
2288
2289 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2290 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2291
Johannes Berg3b858752009-03-12 09:55:09 +01002292 rtnl_lock();
2293
Johannes Berg463d0182009-07-14 00:33:35 +02002294 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002295 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002296 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002297
Johannes Berg79c97e92009-07-07 03:56:12 +02002298 if (!rdev->ops->change_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002299 err = -EOPNOTSUPP;
2300 goto out;
2301 }
2302
Jouni Malineneec60b02009-03-20 21:21:19 +02002303 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2304 err = -EOPNOTSUPP;
2305 goto out;
2306 }
2307
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002308 if (!netif_running(dev)) {
2309 err = -ENETDOWN;
2310 goto out;
2311 }
2312
Johannes Berg79c97e92009-07-07 03:56:12 +02002313 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002314
2315 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002316 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002317 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002318 out_rtnl:
2319 rtnl_unlock();
2320
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002321 return err;
2322}
2323static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2324{
Johannes Berg79c97e92009-07-07 03:56:12 +02002325 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002326 int err;
2327 struct net_device *dev;
2328 u8 *dst = NULL;
2329 u8 *next_hop = NULL;
2330
2331 if (!info->attrs[NL80211_ATTR_MAC])
2332 return -EINVAL;
2333
2334 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2335 return -EINVAL;
2336
2337 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2338 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2339
Johannes Berg3b858752009-03-12 09:55:09 +01002340 rtnl_lock();
2341
Johannes Berg463d0182009-07-14 00:33:35 +02002342 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002343 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002344 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002345
Johannes Berg79c97e92009-07-07 03:56:12 +02002346 if (!rdev->ops->add_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002347 err = -EOPNOTSUPP;
2348 goto out;
2349 }
2350
Jouni Malineneec60b02009-03-20 21:21:19 +02002351 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2352 err = -EOPNOTSUPP;
2353 goto out;
2354 }
2355
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002356 if (!netif_running(dev)) {
2357 err = -ENETDOWN;
2358 goto out;
2359 }
2360
Johannes Berg79c97e92009-07-07 03:56:12 +02002361 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002362
2363 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002364 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002365 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002366 out_rtnl:
2367 rtnl_unlock();
2368
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002369 return err;
2370}
2371
2372static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2373{
Johannes Berg79c97e92009-07-07 03:56:12 +02002374 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002375 int err;
2376 struct net_device *dev;
2377 u8 *dst = NULL;
2378
2379 if (info->attrs[NL80211_ATTR_MAC])
2380 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2381
Johannes Berg3b858752009-03-12 09:55:09 +01002382 rtnl_lock();
2383
Johannes Berg463d0182009-07-14 00:33:35 +02002384 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002385 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002386 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002387
Johannes Berg79c97e92009-07-07 03:56:12 +02002388 if (!rdev->ops->del_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002389 err = -EOPNOTSUPP;
2390 goto out;
2391 }
2392
Johannes Berg79c97e92009-07-07 03:56:12 +02002393 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002394
2395 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002396 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002397 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002398 out_rtnl:
2399 rtnl_unlock();
2400
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002401 return err;
2402}
2403
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002404static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2405{
Johannes Berg79c97e92009-07-07 03:56:12 +02002406 struct cfg80211_registered_device *rdev;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002407 int err;
2408 struct net_device *dev;
2409 struct bss_parameters params;
2410
2411 memset(&params, 0, sizeof(params));
2412 /* default to not changing parameters */
2413 params.use_cts_prot = -1;
2414 params.use_short_preamble = -1;
2415 params.use_short_slot_time = -1;
2416
2417 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2418 params.use_cts_prot =
2419 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2420 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2421 params.use_short_preamble =
2422 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2423 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2424 params.use_short_slot_time =
2425 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002426 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2427 params.basic_rates =
2428 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2429 params.basic_rates_len =
2430 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2431 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002432
Johannes Berg3b858752009-03-12 09:55:09 +01002433 rtnl_lock();
2434
Johannes Berg463d0182009-07-14 00:33:35 +02002435 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002436 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002437 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002438
Johannes Berg79c97e92009-07-07 03:56:12 +02002439 if (!rdev->ops->change_bss) {
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002440 err = -EOPNOTSUPP;
2441 goto out;
2442 }
2443
Jouni Malineneec60b02009-03-20 21:21:19 +02002444 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2445 err = -EOPNOTSUPP;
2446 goto out;
2447 }
2448
Johannes Berg79c97e92009-07-07 03:56:12 +02002449 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002450
2451 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002452 cfg80211_unlock_rdev(rdev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002453 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002454 out_rtnl:
2455 rtnl_unlock();
2456
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002457 return err;
2458}
2459
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002460static const struct nla_policy
2461 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2462 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2463 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2464 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2465 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2466 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2467 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2468};
2469
2470static int parse_reg_rule(struct nlattr *tb[],
2471 struct ieee80211_reg_rule *reg_rule)
2472{
2473 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2474 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2475
2476 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2477 return -EINVAL;
2478 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2479 return -EINVAL;
2480 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2481 return -EINVAL;
2482 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2483 return -EINVAL;
2484 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2485 return -EINVAL;
2486
2487 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2488
2489 freq_range->start_freq_khz =
2490 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2491 freq_range->end_freq_khz =
2492 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2493 freq_range->max_bandwidth_khz =
2494 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2495
2496 power_rule->max_eirp =
2497 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2498
2499 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2500 power_rule->max_antenna_gain =
2501 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2502
2503 return 0;
2504}
2505
2506static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2507{
2508 int r;
2509 char *data = NULL;
2510
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002511 /*
2512 * You should only get this when cfg80211 hasn't yet initialized
2513 * completely when built-in to the kernel right between the time
2514 * window between nl80211_init() and regulatory_init(), if that is
2515 * even possible.
2516 */
2517 mutex_lock(&cfg80211_mutex);
2518 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002519 mutex_unlock(&cfg80211_mutex);
2520 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002521 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002522 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002523
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002524 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2525 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002526
2527 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2528
2529#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2530 /* We ignore world regdom requests with the old regdom setup */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002531 if (is_world_regdom(data))
2532 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002533#endif
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002534
2535 r = regulatory_hint_user(data);
2536
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002537 return r;
2538}
2539
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002540static int nl80211_get_mesh_params(struct sk_buff *skb,
2541 struct genl_info *info)
2542{
Johannes Berg79c97e92009-07-07 03:56:12 +02002543 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002544 struct mesh_config cur_params;
2545 int err;
2546 struct net_device *dev;
2547 void *hdr;
2548 struct nlattr *pinfoattr;
2549 struct sk_buff *msg;
2550
Johannes Berg3b858752009-03-12 09:55:09 +01002551 rtnl_lock();
2552
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002553 /* Look up our device */
Johannes Berg463d0182009-07-14 00:33:35 +02002554 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002555 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002556 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002557
Johannes Berg79c97e92009-07-07 03:56:12 +02002558 if (!rdev->ops->get_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002559 err = -EOPNOTSUPP;
2560 goto out;
2561 }
2562
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002563 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002564 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002565 if (err)
2566 goto out;
2567
2568 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002569 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002570 if (!msg) {
2571 err = -ENOBUFS;
2572 goto out;
2573 }
2574 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2575 NL80211_CMD_GET_MESH_PARAMS);
2576 if (!hdr)
2577 goto nla_put_failure;
2578 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2579 if (!pinfoattr)
2580 goto nla_put_failure;
2581 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2582 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2583 cur_params.dot11MeshRetryTimeout);
2584 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2585 cur_params.dot11MeshConfirmTimeout);
2586 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2587 cur_params.dot11MeshHoldingTimeout);
2588 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2589 cur_params.dot11MeshMaxPeerLinks);
2590 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2591 cur_params.dot11MeshMaxRetries);
2592 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2593 cur_params.dot11MeshTTL);
2594 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2595 cur_params.auto_open_plinks);
2596 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2597 cur_params.dot11MeshHWMPmaxPREQretries);
2598 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2599 cur_params.path_refresh_time);
2600 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2601 cur_params.min_discovery_timeout);
2602 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2603 cur_params.dot11MeshHWMPactivePathTimeout);
2604 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2605 cur_params.dot11MeshHWMPpreqMinInterval);
2606 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2607 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2608 nla_nest_end(msg, pinfoattr);
2609 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002610 err = genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002611 goto out;
2612
Johannes Berg3b858752009-03-12 09:55:09 +01002613 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002614 genlmsg_cancel(msg, hdr);
2615 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002616 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002617 /* Cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002618 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002619 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002620 out_rtnl:
2621 rtnl_unlock();
2622
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002623 return err;
2624}
2625
2626#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2627do {\
2628 if (table[attr_num]) {\
2629 cfg.param = nla_fn(table[attr_num]); \
2630 mask |= (1 << (attr_num - 1)); \
2631 } \
2632} while (0);\
2633
2634static struct nla_policy
2635nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2636 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2637 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2638 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2639 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2640 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2641 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2642 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2643
2644 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2645 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2646 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2647 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2648 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2649 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2650};
2651
2652static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2653{
2654 int err;
2655 u32 mask;
Johannes Berg79c97e92009-07-07 03:56:12 +02002656 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002657 struct net_device *dev;
2658 struct mesh_config cfg;
2659 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2660 struct nlattr *parent_attr;
2661
2662 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2663 if (!parent_attr)
2664 return -EINVAL;
2665 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2666 parent_attr, nl80211_meshconf_params_policy))
2667 return -EINVAL;
2668
Johannes Berg3b858752009-03-12 09:55:09 +01002669 rtnl_lock();
2670
Johannes Berg463d0182009-07-14 00:33:35 +02002671 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002672 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002673 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002674
Johannes Berg79c97e92009-07-07 03:56:12 +02002675 if (!rdev->ops->set_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002676 err = -EOPNOTSUPP;
2677 goto out;
2678 }
2679
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002680 /* This makes sure that there aren't more than 32 mesh config
2681 * parameters (otherwise our bitfield scheme would not work.) */
2682 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2683
2684 /* Fill in the params struct */
2685 mask = 0;
2686 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2687 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2688 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2689 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2690 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2691 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2692 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2693 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2694 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2695 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2696 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2697 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2698 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2699 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2700 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2701 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2702 nla_get_u8);
2703 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2704 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2705 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2706 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2707 nla_get_u16);
2708 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2709 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2710 nla_get_u32);
2711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2712 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2713 nla_get_u16);
2714 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2715 dot11MeshHWMPnetDiameterTraversalTime,
2716 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2717 nla_get_u16);
2718
2719 /* Apply changes */
Johannes Berg79c97e92009-07-07 03:56:12 +02002720 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002721
Jouni Malinenf3f92582009-03-20 17:57:36 +02002722 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002723 /* cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002724 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002725 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002726 out_rtnl:
2727 rtnl_unlock();
2728
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002729 return err;
2730}
2731
2732#undef FILL_IN_MESH_PARAM_IF_SET
2733
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002734static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2735{
2736 struct sk_buff *msg;
2737 void *hdr = NULL;
2738 struct nlattr *nl_reg_rules;
2739 unsigned int i;
2740 int err = -EINVAL;
2741
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002742 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002743
2744 if (!cfg80211_regdomain)
2745 goto out;
2746
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002747 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002748 if (!msg) {
2749 err = -ENOBUFS;
2750 goto out;
2751 }
2752
2753 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2754 NL80211_CMD_GET_REG);
2755 if (!hdr)
2756 goto nla_put_failure;
2757
2758 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2759 cfg80211_regdomain->alpha2);
2760
2761 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2762 if (!nl_reg_rules)
2763 goto nla_put_failure;
2764
2765 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2766 struct nlattr *nl_reg_rule;
2767 const struct ieee80211_reg_rule *reg_rule;
2768 const struct ieee80211_freq_range *freq_range;
2769 const struct ieee80211_power_rule *power_rule;
2770
2771 reg_rule = &cfg80211_regdomain->reg_rules[i];
2772 freq_range = &reg_rule->freq_range;
2773 power_rule = &reg_rule->power_rule;
2774
2775 nl_reg_rule = nla_nest_start(msg, i);
2776 if (!nl_reg_rule)
2777 goto nla_put_failure;
2778
2779 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2780 reg_rule->flags);
2781 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2782 freq_range->start_freq_khz);
2783 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2784 freq_range->end_freq_khz);
2785 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2786 freq_range->max_bandwidth_khz);
2787 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2788 power_rule->max_antenna_gain);
2789 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2790 power_rule->max_eirp);
2791
2792 nla_nest_end(msg, nl_reg_rule);
2793 }
2794
2795 nla_nest_end(msg, nl_reg_rules);
2796
2797 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002798 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002799 goto out;
2800
2801nla_put_failure:
2802 genlmsg_cancel(msg, hdr);
2803 err = -EMSGSIZE;
2804out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002805 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002806 return err;
2807}
2808
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002809static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2810{
2811 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2812 struct nlattr *nl_reg_rule;
2813 char *alpha2 = NULL;
2814 int rem_reg_rules = 0, r = 0;
2815 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2816 struct ieee80211_regdomain *rd = NULL;
2817
2818 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2819 return -EINVAL;
2820
2821 if (!info->attrs[NL80211_ATTR_REG_RULES])
2822 return -EINVAL;
2823
2824 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2825
2826 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2827 rem_reg_rules) {
2828 num_rules++;
2829 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002830 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002831 }
2832
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002833 mutex_lock(&cfg80211_mutex);
2834
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002835 if (!reg_is_valid_request(alpha2)) {
2836 r = -EINVAL;
2837 goto bad_reg;
2838 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002839
2840 size_of_regd = sizeof(struct ieee80211_regdomain) +
2841 (num_rules * sizeof(struct ieee80211_reg_rule));
2842
2843 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002844 if (!rd) {
2845 r = -ENOMEM;
2846 goto bad_reg;
2847 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002848
2849 rd->n_reg_rules = num_rules;
2850 rd->alpha2[0] = alpha2[0];
2851 rd->alpha2[1] = alpha2[1];
2852
2853 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2854 rem_reg_rules) {
2855 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2856 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2857 reg_rule_policy);
2858 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2859 if (r)
2860 goto bad_reg;
2861
2862 rule_idx++;
2863
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002864 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2865 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002866 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002867 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002868 }
2869
2870 BUG_ON(rule_idx != num_rules);
2871
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002872 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002873
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002874 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002875
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002876 return r;
2877
Johannes Bergd2372b32008-10-24 20:32:20 +02002878 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002879 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002880 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002881 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002882}
2883
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002884static int validate_scan_freqs(struct nlattr *freqs)
2885{
2886 struct nlattr *attr1, *attr2;
2887 int n_channels = 0, tmp1, tmp2;
2888
2889 nla_for_each_nested(attr1, freqs, tmp1) {
2890 n_channels++;
2891 /*
2892 * Some hardware has a limited channel list for
2893 * scanning, and it is pretty much nonsensical
2894 * to scan for a channel twice, so disallow that
2895 * and don't require drivers to check that the
2896 * channel list they get isn't longer than what
2897 * they can scan, as long as they can scan all
2898 * the channels they registered at once.
2899 */
2900 nla_for_each_nested(attr2, freqs, tmp2)
2901 if (attr1 != attr2 &&
2902 nla_get_u32(attr1) == nla_get_u32(attr2))
2903 return 0;
2904 }
2905
2906 return n_channels;
2907}
2908
Johannes Berg2a519312009-02-10 21:25:55 +01002909static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2910{
Johannes Berg79c97e92009-07-07 03:56:12 +02002911 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01002912 struct net_device *dev;
2913 struct cfg80211_scan_request *request;
2914 struct cfg80211_ssid *ssid;
2915 struct ieee80211_channel *channel;
2916 struct nlattr *attr;
2917 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002918 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002919 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002920 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002921
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002922 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2923 return -EINVAL;
2924
Johannes Berg3b858752009-03-12 09:55:09 +01002925 rtnl_lock();
2926
Johannes Berg463d0182009-07-14 00:33:35 +02002927 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg2a519312009-02-10 21:25:55 +01002928 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002929 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002930
Johannes Berg79c97e92009-07-07 03:56:12 +02002931 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002932
Johannes Berg79c97e92009-07-07 03:56:12 +02002933 if (!rdev->ops->scan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002934 err = -EOPNOTSUPP;
2935 goto out;
2936 }
2937
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002938 if (!netif_running(dev)) {
2939 err = -ENETDOWN;
2940 goto out;
2941 }
2942
Johannes Berg79c97e92009-07-07 03:56:12 +02002943 if (rdev->scan_req) {
Johannes Berg2a519312009-02-10 21:25:55 +01002944 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002945 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002946 }
2947
2948 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002949 n_channels = validate_scan_freqs(
2950 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg2a519312009-02-10 21:25:55 +01002951 if (!n_channels) {
2952 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002953 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002954 }
2955 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002956 n_channels = 0;
2957
Johannes Berg2a519312009-02-10 21:25:55 +01002958 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2959 if (wiphy->bands[band])
2960 n_channels += wiphy->bands[band]->n_channels;
2961 }
2962
2963 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2964 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2965 n_ssids++;
2966
2967 if (n_ssids > wiphy->max_scan_ssids) {
2968 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002969 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002970 }
2971
Jouni Malinen70692ad2009-02-16 19:39:13 +02002972 if (info->attrs[NL80211_ATTR_IE])
2973 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2974 else
2975 ie_len = 0;
2976
Johannes Berg18a83652009-03-31 12:12:05 +02002977 if (ie_len > wiphy->max_scan_ie_len) {
2978 err = -EINVAL;
2979 goto out;
2980 }
2981
Johannes Berg2a519312009-02-10 21:25:55 +01002982 request = kzalloc(sizeof(*request)
2983 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002984 + sizeof(channel) * n_channels
2985 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01002986 if (!request) {
2987 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01002988 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002989 }
2990
Johannes Berg2a519312009-02-10 21:25:55 +01002991 request->n_channels = n_channels;
2992 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02002993 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01002994 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002995 if (ie_len) {
2996 if (request->ssids)
2997 request->ie = (void *)(request->ssids + n_ssids);
2998 else
2999 request->ie = (void *)(request->channels + n_channels);
3000 }
Johannes Berg2a519312009-02-10 21:25:55 +01003001
3002 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3003 /* user specified, bail out if channel not found */
3004 request->n_channels = n_channels;
3005 i = 0;
3006 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
3007 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3008 if (!request->channels[i]) {
3009 err = -EINVAL;
3010 goto out_free;
3011 }
3012 i++;
3013 }
3014 } else {
3015 /* all channels */
3016 i = 0;
3017 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3018 int j;
3019 if (!wiphy->bands[band])
3020 continue;
3021 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3022 request->channels[i] = &wiphy->bands[band]->channels[j];
3023 i++;
3024 }
3025 }
3026 }
3027
3028 i = 0;
3029 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3030 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3031 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3032 err = -EINVAL;
3033 goto out_free;
3034 }
3035 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3036 request->ssids[i].ssid_len = nla_len(attr);
3037 i++;
3038 }
3039 }
3040
Jouni Malinen70692ad2009-02-16 19:39:13 +02003041 if (info->attrs[NL80211_ATTR_IE]) {
3042 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003043 memcpy((void *)request->ie,
3044 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003045 request->ie_len);
3046 }
3047
Johannes Berg463d0182009-07-14 00:33:35 +02003048 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003049 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003050
Johannes Berg79c97e92009-07-07 03:56:12 +02003051 rdev->scan_req = request;
3052 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003053
Johannes Berg463d0182009-07-14 00:33:35 +02003054 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003055 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003056 dev_hold(dev);
3057 }
Johannes Berga538e2d2009-06-16 19:56:42 +02003058
Johannes Berg2a519312009-02-10 21:25:55 +01003059 out_free:
3060 if (err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003061 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003062 kfree(request);
3063 }
Johannes Berg2a519312009-02-10 21:25:55 +01003064 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003065 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003066 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01003067 out_rtnl:
3068 rtnl_unlock();
3069
Johannes Berg2a519312009-02-10 21:25:55 +01003070 return err;
3071}
3072
3073static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3074 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003075 struct wireless_dev *wdev,
3076 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003077{
Johannes Berg48ab9052009-07-10 18:42:31 +02003078 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003079 void *hdr;
3080 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003081 int i;
3082
3083 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003084
3085 hdr = nl80211hdr_put(msg, pid, seq, flags,
3086 NL80211_CMD_NEW_SCAN_RESULTS);
3087 if (!hdr)
3088 return -1;
3089
Johannes Bergf5ea9122009-08-07 16:17:38 +02003090 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003091 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003092
3093 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3094 if (!bss)
3095 goto nla_put_failure;
3096 if (!is_zero_ether_addr(res->bssid))
3097 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3098 if (res->information_elements && res->len_information_elements)
3099 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3100 res->len_information_elements,
3101 res->information_elements);
3102 if (res->tsf)
3103 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3104 if (res->beacon_interval)
3105 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3106 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3107 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
3108
Johannes Berg77965c92009-02-18 18:45:06 +01003109 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003110 case CFG80211_SIGNAL_TYPE_MBM:
3111 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3112 break;
3113 case CFG80211_SIGNAL_TYPE_UNSPEC:
3114 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3115 break;
3116 default:
3117 break;
3118 }
3119
Johannes Berg48ab9052009-07-10 18:42:31 +02003120 switch (wdev->iftype) {
3121 case NL80211_IFTYPE_STATION:
3122 if (intbss == wdev->current_bss)
3123 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3124 NL80211_BSS_STATUS_ASSOCIATED);
3125 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3126 if (intbss != wdev->auth_bsses[i])
3127 continue;
3128 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3129 NL80211_BSS_STATUS_AUTHENTICATED);
3130 break;
3131 }
3132 break;
3133 case NL80211_IFTYPE_ADHOC:
3134 if (intbss == wdev->current_bss)
3135 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3136 NL80211_BSS_STATUS_IBSS_JOINED);
3137 break;
3138 default:
3139 break;
3140 }
3141
Johannes Berg2a519312009-02-10 21:25:55 +01003142 nla_nest_end(msg, bss);
3143
3144 return genlmsg_end(msg, hdr);
3145
3146 nla_put_failure:
3147 genlmsg_cancel(msg, hdr);
3148 return -EMSGSIZE;
3149}
3150
3151static int nl80211_dump_scan(struct sk_buff *skb,
3152 struct netlink_callback *cb)
3153{
Johannes Berg48ab9052009-07-10 18:42:31 +02003154 struct cfg80211_registered_device *rdev;
3155 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003156 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003157 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003158 int ifidx = cb->args[0];
3159 int start = cb->args[1], idx = 0;
3160 int err;
3161
3162 if (!ifidx) {
3163 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
3164 nl80211_fam.attrbuf, nl80211_fam.maxattr,
3165 nl80211_policy);
3166 if (err)
3167 return err;
3168
3169 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
3170 return -EINVAL;
3171
3172 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
3173 if (!ifidx)
3174 return -EINVAL;
3175 cb->args[0] = ifidx;
3176 }
3177
Johannes Berg463d0182009-07-14 00:33:35 +02003178 dev = dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003179 if (!dev)
Johannes Berg2a519312009-02-10 21:25:55 +01003180 return -ENODEV;
3181
Johannes Berg463d0182009-07-14 00:33:35 +02003182 rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003183 if (IS_ERR(rdev)) {
3184 err = PTR_ERR(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003185 goto out_put_netdev;
3186 }
3187
Johannes Berg48ab9052009-07-10 18:42:31 +02003188 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003189
Johannes Berg48ab9052009-07-10 18:42:31 +02003190 wdev_lock(wdev);
3191 spin_lock_bh(&rdev->bss_lock);
3192 cfg80211_bss_expire(rdev);
3193
3194 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003195 if (++idx <= start)
3196 continue;
3197 if (nl80211_send_bss(skb,
3198 NETLINK_CB(cb->skb).pid,
3199 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003200 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003201 idx--;
3202 goto out;
3203 }
3204 }
3205
3206 out:
Johannes Berg48ab9052009-07-10 18:42:31 +02003207 spin_unlock_bh(&rdev->bss_lock);
3208 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003209
3210 cb->args[1] = idx;
3211 err = skb->len;
Johannes Berg48ab9052009-07-10 18:42:31 +02003212 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003213 out_put_netdev:
Johannes Berg48ab9052009-07-10 18:42:31 +02003214 dev_put(dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003215
3216 return err;
3217}
3218
Jouni Malinen255e7372009-03-20 21:21:17 +02003219static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3220{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003221 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003222}
3223
Samuel Ortizb23aa672009-07-01 21:26:54 +02003224static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3225{
3226 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3227 NL80211_WPA_VERSION_2));
3228}
3229
3230static bool nl80211_valid_akm_suite(u32 akm)
3231{
3232 return akm == WLAN_AKM_SUITE_8021X ||
3233 akm == WLAN_AKM_SUITE_PSK;
3234}
3235
3236static bool nl80211_valid_cipher_suite(u32 cipher)
3237{
3238 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3239 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3240 cipher == WLAN_CIPHER_SUITE_TKIP ||
3241 cipher == WLAN_CIPHER_SUITE_CCMP ||
3242 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3243}
3244
3245
Jouni Malinen636a5d32009-03-19 13:39:22 +02003246static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3247{
Johannes Berg79c97e92009-07-07 03:56:12 +02003248 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003249 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003250 struct ieee80211_channel *chan;
3251 const u8 *bssid, *ssid, *ie = NULL;
3252 int err, ssid_len, ie_len = 0;
3253 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003254 struct key_parse key;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003255
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003256 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3257 return -EINVAL;
3258
3259 if (!info->attrs[NL80211_ATTR_MAC])
3260 return -EINVAL;
3261
Jouni Malinen17780922009-03-27 20:52:47 +02003262 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3263 return -EINVAL;
3264
Johannes Berg19957bb2009-07-02 17:20:43 +02003265 if (!info->attrs[NL80211_ATTR_SSID])
3266 return -EINVAL;
3267
3268 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3269 return -EINVAL;
3270
Johannes Bergfffd0932009-07-08 14:22:54 +02003271 err = nl80211_parse_key(info, &key);
3272 if (err)
3273 return err;
3274
3275 if (key.idx >= 0) {
3276 if (!key.p.key || !key.p.key_len)
3277 return -EINVAL;
3278 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3279 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3280 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3281 key.p.key_len != WLAN_KEY_LEN_WEP104))
3282 return -EINVAL;
3283 if (key.idx > 4)
3284 return -EINVAL;
3285 } else {
3286 key.p.key_len = 0;
3287 key.p.key = NULL;
3288 }
3289
Jouni Malinen636a5d32009-03-19 13:39:22 +02003290 rtnl_lock();
3291
Johannes Berg463d0182009-07-14 00:33:35 +02003292 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003293 if (err)
3294 goto unlock_rtnl;
3295
Johannes Berg79c97e92009-07-07 03:56:12 +02003296 if (!rdev->ops->auth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003297 err = -EOPNOTSUPP;
3298 goto out;
3299 }
3300
Jouni Malineneec60b02009-03-20 21:21:19 +02003301 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3302 err = -EOPNOTSUPP;
3303 goto out;
3304 }
3305
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003306 if (!netif_running(dev)) {
3307 err = -ENETDOWN;
3308 goto out;
3309 }
3310
Johannes Berg19957bb2009-07-02 17:20:43 +02003311 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003312 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003313 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3314 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
Jouni Malinen17780922009-03-27 20:52:47 +02003315 err = -EINVAL;
3316 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003317 }
3318
Johannes Berg19957bb2009-07-02 17:20:43 +02003319 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3320 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3321
3322 if (info->attrs[NL80211_ATTR_IE]) {
3323 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3324 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3325 }
3326
3327 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3328 if (!nl80211_valid_auth_type(auth_type)) {
3329 err = -EINVAL;
3330 goto out;
3331 }
3332
Johannes Berg79c97e92009-07-07 03:56:12 +02003333 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +02003334 ssid, ssid_len, ie, ie_len,
3335 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003336
3337out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003338 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003339 dev_put(dev);
3340unlock_rtnl:
3341 rtnl_unlock();
3342 return err;
3343}
3344
Samuel Ortizb23aa672009-07-01 21:26:54 +02003345static int nl80211_crypto_settings(struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003346 struct cfg80211_crypto_settings *settings,
3347 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003348{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003349 memset(settings, 0, sizeof(*settings));
3350
Samuel Ortizb23aa672009-07-01 21:26:54 +02003351 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3352
3353 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3354 void *data;
3355 int len, i;
3356
3357 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3358 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3359 settings->n_ciphers_pairwise = len / sizeof(u32);
3360
3361 if (len % sizeof(u32))
3362 return -EINVAL;
3363
Johannes Berg3dc27d22009-07-02 21:36:37 +02003364 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003365 return -EINVAL;
3366
3367 memcpy(settings->ciphers_pairwise, data, len);
3368
3369 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3370 if (!nl80211_valid_cipher_suite(
3371 settings->ciphers_pairwise[i]))
3372 return -EINVAL;
3373 }
3374
3375 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3376 settings->cipher_group =
3377 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3378 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3379 return -EINVAL;
3380 }
3381
3382 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3383 settings->wpa_versions =
3384 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3385 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3386 return -EINVAL;
3387 }
3388
3389 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3390 void *data;
3391 int len, i;
3392
3393 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3394 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3395 settings->n_akm_suites = len / sizeof(u32);
3396
3397 if (len % sizeof(u32))
3398 return -EINVAL;
3399
3400 memcpy(settings->akm_suites, data, len);
3401
3402 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3403 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3404 return -EINVAL;
3405 }
3406
3407 return 0;
3408}
3409
Jouni Malinen636a5d32009-03-19 13:39:22 +02003410static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3411{
Johannes Berg19957bb2009-07-02 17:20:43 +02003412 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003413 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003414 struct cfg80211_crypto_settings crypto;
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003415 struct ieee80211_channel *chan, *fixedchan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003416 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003417 int err, ssid_len, ie_len = 0;
3418 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003419
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003420 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3421 return -EINVAL;
3422
3423 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003424 !info->attrs[NL80211_ATTR_SSID] ||
3425 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003426 return -EINVAL;
3427
Jouni Malinen636a5d32009-03-19 13:39:22 +02003428 rtnl_lock();
3429
Johannes Berg463d0182009-07-14 00:33:35 +02003430 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003431 if (err)
3432 goto unlock_rtnl;
3433
Johannes Berg19957bb2009-07-02 17:20:43 +02003434 if (!rdev->ops->assoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003435 err = -EOPNOTSUPP;
3436 goto out;
3437 }
3438
Jouni Malineneec60b02009-03-20 21:21:19 +02003439 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3440 err = -EOPNOTSUPP;
3441 goto out;
3442 }
3443
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003444 if (!netif_running(dev)) {
3445 err = -ENETDOWN;
3446 goto out;
3447 }
3448
Johannes Berg19957bb2009-07-02 17:20:43 +02003449 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003450
Johannes Berg19957bb2009-07-02 17:20:43 +02003451 chan = ieee80211_get_channel(&rdev->wiphy,
3452 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3453 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3454 err = -EINVAL;
3455 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003456 }
3457
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003458 mutex_lock(&rdev->devlist_mtx);
3459 fixedchan = rdev_fixed_channel(rdev, NULL);
3460 if (fixedchan && chan != fixedchan) {
3461 err = -EBUSY;
3462 mutex_unlock(&rdev->devlist_mtx);
3463 goto out;
3464 }
3465 mutex_unlock(&rdev->devlist_mtx);
3466
Johannes Berg19957bb2009-07-02 17:20:43 +02003467 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3468 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003469
3470 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003471 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3472 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003473 }
3474
Jouni Malinendc6382c2009-05-06 22:09:37 +03003475 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003476 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03003477 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003478 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003479 use_mfp = true;
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003480 else if (mfp != NL80211_MFP_NO) {
Jouni Malinendc6382c2009-05-06 22:09:37 +03003481 err = -EINVAL;
3482 goto out;
3483 }
3484 }
3485
Johannes Berg3e5d7642009-07-07 14:37:26 +02003486 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3487 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3488
Johannes Berg3dc27d22009-07-02 21:36:37 +02003489 err = nl80211_crypto_settings(info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003490 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003491 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3492 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003493 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003494
3495out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003496 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003497 dev_put(dev);
3498unlock_rtnl:
3499 rtnl_unlock();
3500 return err;
3501}
3502
3503static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3504{
Johannes Berg79c97e92009-07-07 03:56:12 +02003505 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003506 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003507 const u8 *ie = NULL, *bssid;
3508 int err, ie_len = 0;
3509 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003510
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003511 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3512 return -EINVAL;
3513
3514 if (!info->attrs[NL80211_ATTR_MAC])
3515 return -EINVAL;
3516
3517 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3518 return -EINVAL;
3519
Jouni Malinen636a5d32009-03-19 13:39:22 +02003520 rtnl_lock();
3521
Johannes Berg463d0182009-07-14 00:33:35 +02003522 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003523 if (err)
3524 goto unlock_rtnl;
3525
Johannes Berg79c97e92009-07-07 03:56:12 +02003526 if (!rdev->ops->deauth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003527 err = -EOPNOTSUPP;
3528 goto out;
3529 }
3530
Jouni Malineneec60b02009-03-20 21:21:19 +02003531 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3532 err = -EOPNOTSUPP;
3533 goto out;
3534 }
3535
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003536 if (!netif_running(dev)) {
3537 err = -ENETDOWN;
3538 goto out;
3539 }
3540
Johannes Berg19957bb2009-07-02 17:20:43 +02003541 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003542
Johannes Berg19957bb2009-07-02 17:20:43 +02003543 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3544 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003545 /* Reason Code 0 is reserved */
3546 err = -EINVAL;
3547 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003548 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003549
3550 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003551 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3552 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003553 }
3554
Johannes Berg79c97e92009-07-07 03:56:12 +02003555 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003556
3557out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003558 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003559 dev_put(dev);
3560unlock_rtnl:
3561 rtnl_unlock();
3562 return err;
3563}
3564
3565static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3566{
Johannes Berg79c97e92009-07-07 03:56:12 +02003567 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003568 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003569 const u8 *ie = NULL, *bssid;
3570 int err, ie_len = 0;
3571 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003572
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003573 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3574 return -EINVAL;
3575
3576 if (!info->attrs[NL80211_ATTR_MAC])
3577 return -EINVAL;
3578
3579 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3580 return -EINVAL;
3581
Jouni Malinen636a5d32009-03-19 13:39:22 +02003582 rtnl_lock();
3583
Johannes Berg463d0182009-07-14 00:33:35 +02003584 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003585 if (err)
3586 goto unlock_rtnl;
3587
Johannes Berg79c97e92009-07-07 03:56:12 +02003588 if (!rdev->ops->disassoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003589 err = -EOPNOTSUPP;
3590 goto out;
3591 }
3592
Jouni Malineneec60b02009-03-20 21:21:19 +02003593 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3594 err = -EOPNOTSUPP;
3595 goto out;
3596 }
3597
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003598 if (!netif_running(dev)) {
3599 err = -ENETDOWN;
3600 goto out;
3601 }
3602
Johannes Berg19957bb2009-07-02 17:20:43 +02003603 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003604
Johannes Berg19957bb2009-07-02 17:20:43 +02003605 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3606 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003607 /* Reason Code 0 is reserved */
3608 err = -EINVAL;
3609 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003610 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003611
3612 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003613 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3614 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003615 }
3616
Johannes Berg79c97e92009-07-07 03:56:12 +02003617 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003618
3619out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003620 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003621 dev_put(dev);
3622unlock_rtnl:
3623 rtnl_unlock();
3624 return err;
3625}
3626
Johannes Berg04a773a2009-04-19 21:24:32 +02003627static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3628{
Johannes Berg79c97e92009-07-07 03:56:12 +02003629 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003630 struct net_device *dev;
3631 struct cfg80211_ibss_params ibss;
3632 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003633 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003634 int err;
3635
Johannes Berg8e30bc52009-04-22 17:45:38 +02003636 memset(&ibss, 0, sizeof(ibss));
3637
Johannes Berg04a773a2009-04-19 21:24:32 +02003638 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3639 return -EINVAL;
3640
3641 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3642 !info->attrs[NL80211_ATTR_SSID] ||
3643 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3644 return -EINVAL;
3645
Johannes Berg8e30bc52009-04-22 17:45:38 +02003646 ibss.beacon_interval = 100;
3647
3648 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3649 ibss.beacon_interval =
3650 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3651 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3652 return -EINVAL;
3653 }
3654
Johannes Berg04a773a2009-04-19 21:24:32 +02003655 rtnl_lock();
3656
Johannes Berg463d0182009-07-14 00:33:35 +02003657 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003658 if (err)
3659 goto unlock_rtnl;
3660
Johannes Berg79c97e92009-07-07 03:56:12 +02003661 if (!rdev->ops->join_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003662 err = -EOPNOTSUPP;
3663 goto out;
3664 }
3665
3666 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3667 err = -EOPNOTSUPP;
3668 goto out;
3669 }
3670
3671 if (!netif_running(dev)) {
3672 err = -ENETDOWN;
3673 goto out;
3674 }
3675
Johannes Berg79c97e92009-07-07 03:56:12 +02003676 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003677
3678 if (info->attrs[NL80211_ATTR_MAC])
3679 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3680 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3681 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3682
3683 if (info->attrs[NL80211_ATTR_IE]) {
3684 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3685 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3686 }
3687
3688 ibss.channel = ieee80211_get_channel(wiphy,
3689 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3690 if (!ibss.channel ||
3691 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3692 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3693 err = -EINVAL;
3694 goto out;
3695 }
3696
3697 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003698 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003699
Johannes Bergfffd0932009-07-08 14:22:54 +02003700 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3701 connkeys = nl80211_parse_connkeys(rdev,
3702 info->attrs[NL80211_ATTR_KEYS]);
3703 if (IS_ERR(connkeys)) {
3704 err = PTR_ERR(connkeys);
3705 connkeys = NULL;
3706 goto out;
3707 }
3708 }
3709
3710 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003711
3712out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003713 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003714 dev_put(dev);
3715unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02003716 if (err)
3717 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003718 rtnl_unlock();
3719 return err;
3720}
3721
3722static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3723{
Johannes Berg79c97e92009-07-07 03:56:12 +02003724 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003725 struct net_device *dev;
3726 int err;
3727
3728 rtnl_lock();
3729
Johannes Berg463d0182009-07-14 00:33:35 +02003730 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003731 if (err)
3732 goto unlock_rtnl;
3733
Johannes Berg79c97e92009-07-07 03:56:12 +02003734 if (!rdev->ops->leave_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003735 err = -EOPNOTSUPP;
3736 goto out;
3737 }
3738
3739 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3740 err = -EOPNOTSUPP;
3741 goto out;
3742 }
3743
3744 if (!netif_running(dev)) {
3745 err = -ENETDOWN;
3746 goto out;
3747 }
3748
Johannes Berg79c97e92009-07-07 03:56:12 +02003749 err = cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003750
3751out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003752 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003753 dev_put(dev);
3754unlock_rtnl:
3755 rtnl_unlock();
3756 return err;
3757}
3758
Johannes Bergaff89a92009-07-01 21:26:51 +02003759#ifdef CONFIG_NL80211_TESTMODE
3760static struct genl_multicast_group nl80211_testmode_mcgrp = {
3761 .name = "testmode",
3762};
3763
3764static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3765{
3766 struct cfg80211_registered_device *rdev;
3767 int err;
3768
3769 if (!info->attrs[NL80211_ATTR_TESTDATA])
3770 return -EINVAL;
3771
3772 rtnl_lock();
3773
3774 rdev = cfg80211_get_dev_from_info(info);
3775 if (IS_ERR(rdev)) {
3776 err = PTR_ERR(rdev);
3777 goto unlock_rtnl;
3778 }
3779
3780 err = -EOPNOTSUPP;
3781 if (rdev->ops->testmode_cmd) {
3782 rdev->testmode_info = info;
3783 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3784 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3785 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3786 rdev->testmode_info = NULL;
3787 }
3788
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003789 cfg80211_unlock_rdev(rdev);
Johannes Bergaff89a92009-07-01 21:26:51 +02003790
3791 unlock_rtnl:
3792 rtnl_unlock();
3793 return err;
3794}
3795
3796static struct sk_buff *
3797__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3798 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3799{
3800 struct sk_buff *skb;
3801 void *hdr;
3802 struct nlattr *data;
3803
3804 skb = nlmsg_new(approxlen + 100, gfp);
3805 if (!skb)
3806 return NULL;
3807
3808 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3809 if (!hdr) {
3810 kfree_skb(skb);
3811 return NULL;
3812 }
3813
3814 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3815 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3816
3817 ((void **)skb->cb)[0] = rdev;
3818 ((void **)skb->cb)[1] = hdr;
3819 ((void **)skb->cb)[2] = data;
3820
3821 return skb;
3822
3823 nla_put_failure:
3824 kfree_skb(skb);
3825 return NULL;
3826}
3827
3828struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3829 int approxlen)
3830{
3831 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3832
3833 if (WARN_ON(!rdev->testmode_info))
3834 return NULL;
3835
3836 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3837 rdev->testmode_info->snd_pid,
3838 rdev->testmode_info->snd_seq,
3839 GFP_KERNEL);
3840}
3841EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3842
3843int cfg80211_testmode_reply(struct sk_buff *skb)
3844{
3845 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3846 void *hdr = ((void **)skb->cb)[1];
3847 struct nlattr *data = ((void **)skb->cb)[2];
3848
3849 if (WARN_ON(!rdev->testmode_info)) {
3850 kfree_skb(skb);
3851 return -EINVAL;
3852 }
3853
3854 nla_nest_end(skb, data);
3855 genlmsg_end(skb, hdr);
3856 return genlmsg_reply(skb, rdev->testmode_info);
3857}
3858EXPORT_SYMBOL(cfg80211_testmode_reply);
3859
3860struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3861 int approxlen, gfp_t gfp)
3862{
3863 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3864
3865 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3866}
3867EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3868
3869void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3870{
3871 void *hdr = ((void **)skb->cb)[1];
3872 struct nlattr *data = ((void **)skb->cb)[2];
3873
3874 nla_nest_end(skb, data);
3875 genlmsg_end(skb, hdr);
3876 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3877}
3878EXPORT_SYMBOL(cfg80211_testmode_event);
3879#endif
3880
Samuel Ortizb23aa672009-07-01 21:26:54 +02003881static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3882{
Johannes Berg79c97e92009-07-07 03:56:12 +02003883 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003884 struct net_device *dev;
3885 struct cfg80211_connect_params connect;
3886 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003887 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003888 int err;
3889
3890 memset(&connect, 0, sizeof(connect));
3891
3892 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3893 return -EINVAL;
3894
3895 if (!info->attrs[NL80211_ATTR_SSID] ||
3896 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3897 return -EINVAL;
3898
3899 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3900 connect.auth_type =
3901 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3902 if (!nl80211_valid_auth_type(connect.auth_type))
3903 return -EINVAL;
3904 } else
3905 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3906
3907 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3908
Johannes Berg3dc27d22009-07-02 21:36:37 +02003909 err = nl80211_crypto_settings(info, &connect.crypto,
3910 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003911 if (err)
3912 return err;
3913 rtnl_lock();
3914
Johannes Berg463d0182009-07-14 00:33:35 +02003915 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003916 if (err)
3917 goto unlock_rtnl;
3918
3919 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3920 err = -EOPNOTSUPP;
3921 goto out;
3922 }
3923
3924 if (!netif_running(dev)) {
3925 err = -ENETDOWN;
3926 goto out;
3927 }
3928
Johannes Berg79c97e92009-07-07 03:56:12 +02003929 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003930
Samuel Ortizb23aa672009-07-01 21:26:54 +02003931 if (info->attrs[NL80211_ATTR_MAC])
3932 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3933 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3934 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3935
3936 if (info->attrs[NL80211_ATTR_IE]) {
3937 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3938 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3939 }
3940
3941 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3942 connect.channel =
3943 ieee80211_get_channel(wiphy,
3944 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3945 if (!connect.channel ||
3946 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
3947 err = -EINVAL;
3948 goto out;
3949 }
3950 }
3951
Johannes Bergfffd0932009-07-08 14:22:54 +02003952 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3953 connkeys = nl80211_parse_connkeys(rdev,
3954 info->attrs[NL80211_ATTR_KEYS]);
3955 if (IS_ERR(connkeys)) {
3956 err = PTR_ERR(connkeys);
3957 connkeys = NULL;
3958 goto out;
3959 }
3960 }
3961
3962 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003963
3964out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003965 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003966 dev_put(dev);
3967unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02003968 if (err)
3969 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003970 rtnl_unlock();
3971 return err;
3972}
3973
3974static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3975{
Johannes Berg79c97e92009-07-07 03:56:12 +02003976 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003977 struct net_device *dev;
3978 int err;
3979 u16 reason;
3980
3981 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3982 reason = WLAN_REASON_DEAUTH_LEAVING;
3983 else
3984 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3985
3986 if (reason == 0)
3987 return -EINVAL;
3988
3989 rtnl_lock();
3990
Johannes Berg463d0182009-07-14 00:33:35 +02003991 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003992 if (err)
3993 goto unlock_rtnl;
3994
3995 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3996 err = -EOPNOTSUPP;
3997 goto out;
3998 }
3999
4000 if (!netif_running(dev)) {
4001 err = -ENETDOWN;
4002 goto out;
4003 }
4004
Johannes Berg79c97e92009-07-07 03:56:12 +02004005 err = cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004006
4007out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004008 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004009 dev_put(dev);
4010unlock_rtnl:
4011 rtnl_unlock();
4012 return err;
4013}
4014
Johannes Berg463d0182009-07-14 00:33:35 +02004015static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4016{
4017 struct cfg80211_registered_device *rdev;
4018 struct net *net;
4019 int err;
4020 u32 pid;
4021
4022 if (!info->attrs[NL80211_ATTR_PID])
4023 return -EINVAL;
4024
4025 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4026
4027 rtnl_lock();
4028
4029 rdev = cfg80211_get_dev_from_info(info);
4030 if (IS_ERR(rdev)) {
4031 err = PTR_ERR(rdev);
4032 goto out;
4033 }
4034
4035 net = get_net_ns_by_pid(pid);
4036 if (IS_ERR(net)) {
4037 err = PTR_ERR(net);
4038 goto out;
4039 }
4040
4041 err = 0;
4042
4043 /* check if anything to do */
4044 if (net_eq(wiphy_net(&rdev->wiphy), net))
4045 goto out_put_net;
4046
4047 err = cfg80211_switch_netns(rdev, net);
4048 out_put_net:
4049 put_net(net);
4050 out:
4051 cfg80211_unlock_rdev(rdev);
4052 rtnl_unlock();
4053 return err;
4054}
4055
Johannes Berg55682962007-09-20 13:09:35 -04004056static struct genl_ops nl80211_ops[] = {
4057 {
4058 .cmd = NL80211_CMD_GET_WIPHY,
4059 .doit = nl80211_get_wiphy,
4060 .dumpit = nl80211_dump_wiphy,
4061 .policy = nl80211_policy,
4062 /* can be retrieved by unprivileged users */
4063 },
4064 {
4065 .cmd = NL80211_CMD_SET_WIPHY,
4066 .doit = nl80211_set_wiphy,
4067 .policy = nl80211_policy,
4068 .flags = GENL_ADMIN_PERM,
4069 },
4070 {
4071 .cmd = NL80211_CMD_GET_INTERFACE,
4072 .doit = nl80211_get_interface,
4073 .dumpit = nl80211_dump_interface,
4074 .policy = nl80211_policy,
4075 /* can be retrieved by unprivileged users */
4076 },
4077 {
4078 .cmd = NL80211_CMD_SET_INTERFACE,
4079 .doit = nl80211_set_interface,
4080 .policy = nl80211_policy,
4081 .flags = GENL_ADMIN_PERM,
4082 },
4083 {
4084 .cmd = NL80211_CMD_NEW_INTERFACE,
4085 .doit = nl80211_new_interface,
4086 .policy = nl80211_policy,
4087 .flags = GENL_ADMIN_PERM,
4088 },
4089 {
4090 .cmd = NL80211_CMD_DEL_INTERFACE,
4091 .doit = nl80211_del_interface,
4092 .policy = nl80211_policy,
4093 .flags = GENL_ADMIN_PERM,
4094 },
Johannes Berg41ade002007-12-19 02:03:29 +01004095 {
4096 .cmd = NL80211_CMD_GET_KEY,
4097 .doit = nl80211_get_key,
4098 .policy = nl80211_policy,
4099 .flags = GENL_ADMIN_PERM,
4100 },
4101 {
4102 .cmd = NL80211_CMD_SET_KEY,
4103 .doit = nl80211_set_key,
4104 .policy = nl80211_policy,
4105 .flags = GENL_ADMIN_PERM,
4106 },
4107 {
4108 .cmd = NL80211_CMD_NEW_KEY,
4109 .doit = nl80211_new_key,
4110 .policy = nl80211_policy,
4111 .flags = GENL_ADMIN_PERM,
4112 },
4113 {
4114 .cmd = NL80211_CMD_DEL_KEY,
4115 .doit = nl80211_del_key,
4116 .policy = nl80211_policy,
4117 .flags = GENL_ADMIN_PERM,
4118 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004119 {
4120 .cmd = NL80211_CMD_SET_BEACON,
4121 .policy = nl80211_policy,
4122 .flags = GENL_ADMIN_PERM,
4123 .doit = nl80211_addset_beacon,
4124 },
4125 {
4126 .cmd = NL80211_CMD_NEW_BEACON,
4127 .policy = nl80211_policy,
4128 .flags = GENL_ADMIN_PERM,
4129 .doit = nl80211_addset_beacon,
4130 },
4131 {
4132 .cmd = NL80211_CMD_DEL_BEACON,
4133 .policy = nl80211_policy,
4134 .flags = GENL_ADMIN_PERM,
4135 .doit = nl80211_del_beacon,
4136 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004137 {
4138 .cmd = NL80211_CMD_GET_STATION,
4139 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004140 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004141 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01004142 },
4143 {
4144 .cmd = NL80211_CMD_SET_STATION,
4145 .doit = nl80211_set_station,
4146 .policy = nl80211_policy,
4147 .flags = GENL_ADMIN_PERM,
4148 },
4149 {
4150 .cmd = NL80211_CMD_NEW_STATION,
4151 .doit = nl80211_new_station,
4152 .policy = nl80211_policy,
4153 .flags = GENL_ADMIN_PERM,
4154 },
4155 {
4156 .cmd = NL80211_CMD_DEL_STATION,
4157 .doit = nl80211_del_station,
4158 .policy = nl80211_policy,
4159 .flags = GENL_ADMIN_PERM,
4160 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004161 {
4162 .cmd = NL80211_CMD_GET_MPATH,
4163 .doit = nl80211_get_mpath,
4164 .dumpit = nl80211_dump_mpath,
4165 .policy = nl80211_policy,
4166 .flags = GENL_ADMIN_PERM,
4167 },
4168 {
4169 .cmd = NL80211_CMD_SET_MPATH,
4170 .doit = nl80211_set_mpath,
4171 .policy = nl80211_policy,
4172 .flags = GENL_ADMIN_PERM,
4173 },
4174 {
4175 .cmd = NL80211_CMD_NEW_MPATH,
4176 .doit = nl80211_new_mpath,
4177 .policy = nl80211_policy,
4178 .flags = GENL_ADMIN_PERM,
4179 },
4180 {
4181 .cmd = NL80211_CMD_DEL_MPATH,
4182 .doit = nl80211_del_mpath,
4183 .policy = nl80211_policy,
4184 .flags = GENL_ADMIN_PERM,
4185 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004186 {
4187 .cmd = NL80211_CMD_SET_BSS,
4188 .doit = nl80211_set_bss,
4189 .policy = nl80211_policy,
4190 .flags = GENL_ADMIN_PERM,
4191 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004192 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004193 .cmd = NL80211_CMD_GET_REG,
4194 .doit = nl80211_get_reg,
4195 .policy = nl80211_policy,
4196 /* can be retrieved by unprivileged users */
4197 },
4198 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004199 .cmd = NL80211_CMD_SET_REG,
4200 .doit = nl80211_set_reg,
4201 .policy = nl80211_policy,
4202 .flags = GENL_ADMIN_PERM,
4203 },
4204 {
4205 .cmd = NL80211_CMD_REQ_SET_REG,
4206 .doit = nl80211_req_set_reg,
4207 .policy = nl80211_policy,
4208 .flags = GENL_ADMIN_PERM,
4209 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004210 {
4211 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4212 .doit = nl80211_get_mesh_params,
4213 .policy = nl80211_policy,
4214 /* can be retrieved by unprivileged users */
4215 },
4216 {
4217 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4218 .doit = nl80211_set_mesh_params,
4219 .policy = nl80211_policy,
4220 .flags = GENL_ADMIN_PERM,
4221 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004222 {
Johannes Berg2a519312009-02-10 21:25:55 +01004223 .cmd = NL80211_CMD_TRIGGER_SCAN,
4224 .doit = nl80211_trigger_scan,
4225 .policy = nl80211_policy,
4226 .flags = GENL_ADMIN_PERM,
4227 },
4228 {
4229 .cmd = NL80211_CMD_GET_SCAN,
4230 .policy = nl80211_policy,
4231 .dumpit = nl80211_dump_scan,
4232 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004233 {
4234 .cmd = NL80211_CMD_AUTHENTICATE,
4235 .doit = nl80211_authenticate,
4236 .policy = nl80211_policy,
4237 .flags = GENL_ADMIN_PERM,
4238 },
4239 {
4240 .cmd = NL80211_CMD_ASSOCIATE,
4241 .doit = nl80211_associate,
4242 .policy = nl80211_policy,
4243 .flags = GENL_ADMIN_PERM,
4244 },
4245 {
4246 .cmd = NL80211_CMD_DEAUTHENTICATE,
4247 .doit = nl80211_deauthenticate,
4248 .policy = nl80211_policy,
4249 .flags = GENL_ADMIN_PERM,
4250 },
4251 {
4252 .cmd = NL80211_CMD_DISASSOCIATE,
4253 .doit = nl80211_disassociate,
4254 .policy = nl80211_policy,
4255 .flags = GENL_ADMIN_PERM,
4256 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004257 {
4258 .cmd = NL80211_CMD_JOIN_IBSS,
4259 .doit = nl80211_join_ibss,
4260 .policy = nl80211_policy,
4261 .flags = GENL_ADMIN_PERM,
4262 },
4263 {
4264 .cmd = NL80211_CMD_LEAVE_IBSS,
4265 .doit = nl80211_leave_ibss,
4266 .policy = nl80211_policy,
4267 .flags = GENL_ADMIN_PERM,
4268 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004269#ifdef CONFIG_NL80211_TESTMODE
4270 {
4271 .cmd = NL80211_CMD_TESTMODE,
4272 .doit = nl80211_testmode_do,
4273 .policy = nl80211_policy,
4274 .flags = GENL_ADMIN_PERM,
4275 },
4276#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004277 {
4278 .cmd = NL80211_CMD_CONNECT,
4279 .doit = nl80211_connect,
4280 .policy = nl80211_policy,
4281 .flags = GENL_ADMIN_PERM,
4282 },
4283 {
4284 .cmd = NL80211_CMD_DISCONNECT,
4285 .doit = nl80211_disconnect,
4286 .policy = nl80211_policy,
4287 .flags = GENL_ADMIN_PERM,
4288 },
Johannes Berg463d0182009-07-14 00:33:35 +02004289 {
4290 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4291 .doit = nl80211_wiphy_netns,
4292 .policy = nl80211_policy,
4293 .flags = GENL_ADMIN_PERM,
4294 },
Johannes Berg55682962007-09-20 13:09:35 -04004295};
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004296static struct genl_multicast_group nl80211_mlme_mcgrp = {
4297 .name = "mlme",
4298};
Johannes Berg55682962007-09-20 13:09:35 -04004299
4300/* multicast groups */
4301static struct genl_multicast_group nl80211_config_mcgrp = {
4302 .name = "config",
4303};
Johannes Berg2a519312009-02-10 21:25:55 +01004304static struct genl_multicast_group nl80211_scan_mcgrp = {
4305 .name = "scan",
4306};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004307static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4308 .name = "regulatory",
4309};
Johannes Berg55682962007-09-20 13:09:35 -04004310
4311/* notification functions */
4312
4313void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4314{
4315 struct sk_buff *msg;
4316
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004317 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004318 if (!msg)
4319 return;
4320
4321 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4322 nlmsg_free(msg);
4323 return;
4324 }
4325
Johannes Berg463d0182009-07-14 00:33:35 +02004326 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4327 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004328}
4329
Johannes Berg362a4152009-05-24 16:43:15 +02004330static int nl80211_add_scan_req(struct sk_buff *msg,
4331 struct cfg80211_registered_device *rdev)
4332{
4333 struct cfg80211_scan_request *req = rdev->scan_req;
4334 struct nlattr *nest;
4335 int i;
4336
Johannes Berg667503dd2009-07-07 03:56:11 +02004337 ASSERT_RDEV_LOCK(rdev);
4338
Johannes Berg362a4152009-05-24 16:43:15 +02004339 if (WARN_ON(!req))
4340 return 0;
4341
4342 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4343 if (!nest)
4344 goto nla_put_failure;
4345 for (i = 0; i < req->n_ssids; i++)
4346 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4347 nla_nest_end(msg, nest);
4348
4349 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4350 if (!nest)
4351 goto nla_put_failure;
4352 for (i = 0; i < req->n_channels; i++)
4353 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4354 nla_nest_end(msg, nest);
4355
4356 if (req->ie)
4357 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4358
4359 return 0;
4360 nla_put_failure:
4361 return -ENOBUFS;
4362}
4363
Johannes Berga538e2d2009-06-16 19:56:42 +02004364static int nl80211_send_scan_msg(struct sk_buff *msg,
4365 struct cfg80211_registered_device *rdev,
4366 struct net_device *netdev,
4367 u32 pid, u32 seq, int flags,
4368 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01004369{
4370 void *hdr;
4371
4372 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4373 if (!hdr)
4374 return -1;
4375
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05004376 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01004377 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4378
Johannes Berg362a4152009-05-24 16:43:15 +02004379 /* ignore errors and send incomplete event anyway */
4380 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004381
4382 return genlmsg_end(msg, hdr);
4383
4384 nla_put_failure:
4385 genlmsg_cancel(msg, hdr);
4386 return -EMSGSIZE;
4387}
4388
Johannes Berga538e2d2009-06-16 19:56:42 +02004389void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4390 struct net_device *netdev)
4391{
4392 struct sk_buff *msg;
4393
4394 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4395 if (!msg)
4396 return;
4397
4398 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4399 NL80211_CMD_TRIGGER_SCAN) < 0) {
4400 nlmsg_free(msg);
4401 return;
4402 }
4403
Johannes Berg463d0182009-07-14 00:33:35 +02004404 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4405 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02004406}
4407
Johannes Berg2a519312009-02-10 21:25:55 +01004408void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4409 struct net_device *netdev)
4410{
4411 struct sk_buff *msg;
4412
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004413 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004414 if (!msg)
4415 return;
4416
Johannes Berga538e2d2009-06-16 19:56:42 +02004417 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4418 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004419 nlmsg_free(msg);
4420 return;
4421 }
4422
Johannes Berg463d0182009-07-14 00:33:35 +02004423 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4424 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004425}
4426
4427void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4428 struct net_device *netdev)
4429{
4430 struct sk_buff *msg;
4431
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004432 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004433 if (!msg)
4434 return;
4435
Johannes Berga538e2d2009-06-16 19:56:42 +02004436 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4437 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004438 nlmsg_free(msg);
4439 return;
4440 }
4441
Johannes Berg463d0182009-07-14 00:33:35 +02004442 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4443 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004444}
4445
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004446/*
4447 * This can happen on global regulatory changes or device specific settings
4448 * based on custom world regulatory domains.
4449 */
4450void nl80211_send_reg_change_event(struct regulatory_request *request)
4451{
4452 struct sk_buff *msg;
4453 void *hdr;
4454
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004455 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004456 if (!msg)
4457 return;
4458
4459 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
4460 if (!hdr) {
4461 nlmsg_free(msg);
4462 return;
4463 }
4464
4465 /* Userspace can always count this one always being set */
4466 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
4467
4468 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
4469 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4470 NL80211_REGDOM_TYPE_WORLD);
4471 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
4472 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4473 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
4474 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
4475 request->intersect)
4476 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4477 NL80211_REGDOM_TYPE_INTERSECTION);
4478 else {
4479 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4480 NL80211_REGDOM_TYPE_COUNTRY);
4481 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
4482 }
4483
4484 if (wiphy_idx_valid(request->wiphy_idx))
4485 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
4486
4487 if (genlmsg_end(msg, hdr) < 0) {
4488 nlmsg_free(msg);
4489 return;
4490 }
4491
Johannes Bergbc43b282009-07-25 10:54:13 +02004492 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02004493 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02004494 GFP_ATOMIC);
4495 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004496
4497 return;
4498
4499nla_put_failure:
4500 genlmsg_cancel(msg, hdr);
4501 nlmsg_free(msg);
4502}
4503
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004504static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
4505 struct net_device *netdev,
4506 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004507 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004508{
4509 struct sk_buff *msg;
4510 void *hdr;
4511
Johannes Berge6d6e342009-07-01 21:26:47 +02004512 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004513 if (!msg)
4514 return;
4515
4516 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4517 if (!hdr) {
4518 nlmsg_free(msg);
4519 return;
4520 }
4521
4522 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4523 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4524 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
4525
4526 if (genlmsg_end(msg, hdr) < 0) {
4527 nlmsg_free(msg);
4528 return;
4529 }
4530
Johannes Berg463d0182009-07-14 00:33:35 +02004531 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4532 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004533 return;
4534
4535 nla_put_failure:
4536 genlmsg_cancel(msg, hdr);
4537 nlmsg_free(msg);
4538}
4539
4540void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004541 struct net_device *netdev, const u8 *buf,
4542 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004543{
4544 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004545 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004546}
4547
4548void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
4549 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02004550 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004551{
Johannes Berge6d6e342009-07-01 21:26:47 +02004552 nl80211_send_mlme_event(rdev, netdev, buf, len,
4553 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004554}
4555
Jouni Malinen53b46b82009-03-27 20:53:56 +02004556void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004557 struct net_device *netdev, const u8 *buf,
4558 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004559{
4560 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004561 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004562}
4563
Jouni Malinen53b46b82009-03-27 20:53:56 +02004564void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
4565 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02004566 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004567{
4568 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004569 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004570}
4571
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04004572static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
4573 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02004574 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004575{
4576 struct sk_buff *msg;
4577 void *hdr;
4578
Johannes Berge6d6e342009-07-01 21:26:47 +02004579 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004580 if (!msg)
4581 return;
4582
4583 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4584 if (!hdr) {
4585 nlmsg_free(msg);
4586 return;
4587 }
4588
4589 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4590 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4591 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
4592 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4593
4594 if (genlmsg_end(msg, hdr) < 0) {
4595 nlmsg_free(msg);
4596 return;
4597 }
4598
Johannes Berg463d0182009-07-14 00:33:35 +02004599 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4600 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004601 return;
4602
4603 nla_put_failure:
4604 genlmsg_cancel(msg, hdr);
4605 nlmsg_free(msg);
4606}
4607
4608void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004609 struct net_device *netdev, const u8 *addr,
4610 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004611{
4612 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02004613 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004614}
4615
4616void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004617 struct net_device *netdev, const u8 *addr,
4618 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004619{
Johannes Berge6d6e342009-07-01 21:26:47 +02004620 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
4621 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004622}
4623
Samuel Ortizb23aa672009-07-01 21:26:54 +02004624void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
4625 struct net_device *netdev, const u8 *bssid,
4626 const u8 *req_ie, size_t req_ie_len,
4627 const u8 *resp_ie, size_t resp_ie_len,
4628 u16 status, gfp_t gfp)
4629{
4630 struct sk_buff *msg;
4631 void *hdr;
4632
4633 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4634 if (!msg)
4635 return;
4636
4637 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
4638 if (!hdr) {
4639 nlmsg_free(msg);
4640 return;
4641 }
4642
4643 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4644 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4645 if (bssid)
4646 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4647 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
4648 if (req_ie)
4649 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4650 if (resp_ie)
4651 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4652
4653 if (genlmsg_end(msg, hdr) < 0) {
4654 nlmsg_free(msg);
4655 return;
4656 }
4657
Johannes Berg463d0182009-07-14 00:33:35 +02004658 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4659 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004660 return;
4661
4662 nla_put_failure:
4663 genlmsg_cancel(msg, hdr);
4664 nlmsg_free(msg);
4665
4666}
4667
4668void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
4669 struct net_device *netdev, const u8 *bssid,
4670 const u8 *req_ie, size_t req_ie_len,
4671 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
4672{
4673 struct sk_buff *msg;
4674 void *hdr;
4675
4676 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4677 if (!msg)
4678 return;
4679
4680 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
4681 if (!hdr) {
4682 nlmsg_free(msg);
4683 return;
4684 }
4685
4686 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4687 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4688 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4689 if (req_ie)
4690 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4691 if (resp_ie)
4692 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4693
4694 if (genlmsg_end(msg, hdr) < 0) {
4695 nlmsg_free(msg);
4696 return;
4697 }
4698
Johannes Berg463d0182009-07-14 00:33:35 +02004699 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4700 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004701 return;
4702
4703 nla_put_failure:
4704 genlmsg_cancel(msg, hdr);
4705 nlmsg_free(msg);
4706
4707}
4708
4709void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
4710 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02004711 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004712{
4713 struct sk_buff *msg;
4714 void *hdr;
4715
Johannes Berg667503dd2009-07-07 03:56:11 +02004716 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004717 if (!msg)
4718 return;
4719
4720 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
4721 if (!hdr) {
4722 nlmsg_free(msg);
4723 return;
4724 }
4725
4726 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4727 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4728 if (from_ap && reason)
4729 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
4730 if (from_ap)
4731 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
4732 if (ie)
4733 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
4734
4735 if (genlmsg_end(msg, hdr) < 0) {
4736 nlmsg_free(msg);
4737 return;
4738 }
4739
Johannes Berg463d0182009-07-14 00:33:35 +02004740 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4741 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004742 return;
4743
4744 nla_put_failure:
4745 genlmsg_cancel(msg, hdr);
4746 nlmsg_free(msg);
4747
4748}
4749
Johannes Berg04a773a2009-04-19 21:24:32 +02004750void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
4751 struct net_device *netdev, const u8 *bssid,
4752 gfp_t gfp)
4753{
4754 struct sk_buff *msg;
4755 void *hdr;
4756
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004757 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02004758 if (!msg)
4759 return;
4760
4761 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
4762 if (!hdr) {
4763 nlmsg_free(msg);
4764 return;
4765 }
4766
4767 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4768 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4769 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4770
4771 if (genlmsg_end(msg, hdr) < 0) {
4772 nlmsg_free(msg);
4773 return;
4774 }
4775
Johannes Berg463d0182009-07-14 00:33:35 +02004776 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4777 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02004778 return;
4779
4780 nla_put_failure:
4781 genlmsg_cancel(msg, hdr);
4782 nlmsg_free(msg);
4783}
4784
Jouni Malinena3b8b052009-03-27 21:59:49 +02004785void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
4786 struct net_device *netdev, const u8 *addr,
4787 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02004788 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02004789{
4790 struct sk_buff *msg;
4791 void *hdr;
4792
Johannes Berge6d6e342009-07-01 21:26:47 +02004793 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02004794 if (!msg)
4795 return;
4796
4797 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
4798 if (!hdr) {
4799 nlmsg_free(msg);
4800 return;
4801 }
4802
4803 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4804 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4805 if (addr)
4806 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4807 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
4808 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
4809 if (tsc)
4810 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
4811
4812 if (genlmsg_end(msg, hdr) < 0) {
4813 nlmsg_free(msg);
4814 return;
4815 }
4816
Johannes Berg463d0182009-07-14 00:33:35 +02004817 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4818 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02004819 return;
4820
4821 nla_put_failure:
4822 genlmsg_cancel(msg, hdr);
4823 nlmsg_free(msg);
4824}
4825
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04004826void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
4827 struct ieee80211_channel *channel_before,
4828 struct ieee80211_channel *channel_after)
4829{
4830 struct sk_buff *msg;
4831 void *hdr;
4832 struct nlattr *nl_freq;
4833
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004834 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04004835 if (!msg)
4836 return;
4837
4838 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
4839 if (!hdr) {
4840 nlmsg_free(msg);
4841 return;
4842 }
4843
4844 /*
4845 * Since we are applying the beacon hint to a wiphy we know its
4846 * wiphy_idx is valid
4847 */
4848 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
4849
4850 /* Before */
4851 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
4852 if (!nl_freq)
4853 goto nla_put_failure;
4854 if (nl80211_msg_put_channel(msg, channel_before))
4855 goto nla_put_failure;
4856 nla_nest_end(msg, nl_freq);
4857
4858 /* After */
4859 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
4860 if (!nl_freq)
4861 goto nla_put_failure;
4862 if (nl80211_msg_put_channel(msg, channel_after))
4863 goto nla_put_failure;
4864 nla_nest_end(msg, nl_freq);
4865
4866 if (genlmsg_end(msg, hdr) < 0) {
4867 nlmsg_free(msg);
4868 return;
4869 }
4870
Johannes Berg463d0182009-07-14 00:33:35 +02004871 rcu_read_lock();
4872 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
4873 GFP_ATOMIC);
4874 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04004875
4876 return;
4877
4878nla_put_failure:
4879 genlmsg_cancel(msg, hdr);
4880 nlmsg_free(msg);
4881}
4882
Johannes Berg55682962007-09-20 13:09:35 -04004883/* initialisation/exit functions */
4884
4885int nl80211_init(void)
4886{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00004887 int err;
Johannes Berg55682962007-09-20 13:09:35 -04004888
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00004889 err = genl_register_family_with_ops(&nl80211_fam,
4890 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04004891 if (err)
4892 return err;
4893
Johannes Berg55682962007-09-20 13:09:35 -04004894 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
4895 if (err)
4896 goto err_out;
4897
Johannes Berg2a519312009-02-10 21:25:55 +01004898 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
4899 if (err)
4900 goto err_out;
4901
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004902 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
4903 if (err)
4904 goto err_out;
4905
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004906 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
4907 if (err)
4908 goto err_out;
4909
Johannes Bergaff89a92009-07-01 21:26:51 +02004910#ifdef CONFIG_NL80211_TESTMODE
4911 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
4912 if (err)
4913 goto err_out;
4914#endif
4915
Johannes Berg55682962007-09-20 13:09:35 -04004916 return 0;
4917 err_out:
4918 genl_unregister_family(&nl80211_fam);
4919 return err;
4920}
4921
4922void nl80211_exit(void)
4923{
4924 genl_unregister_family(&nl80211_fam);
4925}