blob: 0c9497170f1f0e52fa63f8ea804aac168054d622 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Johannes Berg4c476992010-10-04 21:36:35 +020026static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
27 struct genl_info *info);
28static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
29 struct genl_info *info);
30
Johannes Berg55682962007-09-20 13:09:35 -040031/* the netlink family */
32static struct genl_family nl80211_fam = {
33 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
34 .name = "nl80211", /* have users key off the name instead */
35 .hdrsize = 0, /* no private header */
36 .version = 1, /* no particular meaning now */
37 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020038 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020039 .pre_doit = nl80211_pre_doit,
40 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040041};
42
Johannes Berg79c97e92009-07-07 03:56:12 +020043/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020044static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020045 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040046 struct net_device **dev)
47{
Johannes Berg463d0182009-07-14 00:33:35 +020048 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040049 int ifindex;
50
Johannes Bergbba95fe2008-07-29 13:22:51 +020051 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040052 return -EINVAL;
53
Johannes Bergbba95fe2008-07-29 13:22:51 +020054 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020055 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040056 if (!*dev)
57 return -ENODEV;
58
Johannes Berg463d0182009-07-14 00:33:35 +020059 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020060 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040061 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020062 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040063 }
64
65 return 0;
66}
67
68/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000069static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040070 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070072 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020073 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020074 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053075 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020076 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
77 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
78 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
79 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010080 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040081
82 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
83 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
84 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010085
86 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020087 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010088
Johannes Bergb9454e82009-07-08 13:29:08 +020089 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010090 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
91 .len = WLAN_MAX_KEY_LEN },
92 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
93 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
94 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030095 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010096
97 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
98 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
99 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
100 .len = IEEE80211_MAX_DATA_LEN },
101 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
102 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100103 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
104 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
105 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
106 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
107 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100108 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100109 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200110 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100111 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
112 .len = IEEE80211_MAX_MESH_ID_LEN },
113 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300114
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700115 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
116 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
117
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300118 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
119 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
120 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200121 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
122 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300123
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700124 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
125
Jouni Malinen36aedc92008-08-25 11:58:58 +0300126 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
127 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200128
129 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
130 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
131 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100132 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
133 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200134
135 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
136 .len = IEEE80211_MAX_SSID_LEN },
137 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
138 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200139 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300140 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300141 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300142 [NL80211_ATTR_STA_FLAGS2] = {
143 .len = sizeof(struct nl80211_sta_flag_update),
144 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300145 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300146 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
147 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200148 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
149 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
150 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200151 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100152 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100153 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
154 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100155 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
156 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200157 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200158 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
159 .len = IEEE80211_MAX_DATA_LEN },
160 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200161 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200162 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300163 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200164 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300165
166 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
167 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200168 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400169};
170
Johannes Bergb9454e82009-07-08 13:29:08 +0200171/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000172static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200173 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200174 [NL80211_KEY_IDX] = { .type = NLA_U8 },
175 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
176 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
177 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
178 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
179};
180
Holger Schuriga0438972009-11-11 11:30:02 +0100181/* ifidx get helper */
182static int nl80211_get_ifidx(struct netlink_callback *cb)
183{
184 int res;
185
186 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
187 nl80211_fam.attrbuf, nl80211_fam.maxattr,
188 nl80211_policy);
189 if (res)
190 return res;
191
192 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
193 return -EINVAL;
194
195 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
196 if (!res)
197 return -EINVAL;
198 return res;
199}
200
Johannes Berg67748892010-10-04 21:14:06 +0200201static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
202 struct netlink_callback *cb,
203 struct cfg80211_registered_device **rdev,
204 struct net_device **dev)
205{
206 int ifidx = cb->args[0];
207 int err;
208
209 if (!ifidx)
210 ifidx = nl80211_get_ifidx(cb);
211 if (ifidx < 0)
212 return ifidx;
213
214 cb->args[0] = ifidx;
215
216 rtnl_lock();
217
218 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
219 if (!*dev) {
220 err = -ENODEV;
221 goto out_rtnl;
222 }
223
224 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
225 if (IS_ERR(dev)) {
226 err = PTR_ERR(dev);
227 goto out_rtnl;
228 }
229
230 return 0;
231 out_rtnl:
232 rtnl_unlock();
233 return err;
234}
235
236static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
237{
238 cfg80211_unlock_rdev(rdev);
239 rtnl_unlock();
240}
241
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100242/* IE validation */
243static bool is_valid_ie_attr(const struct nlattr *attr)
244{
245 const u8 *pos;
246 int len;
247
248 if (!attr)
249 return true;
250
251 pos = nla_data(attr);
252 len = nla_len(attr);
253
254 while (len) {
255 u8 elemlen;
256
257 if (len < 2)
258 return false;
259 len -= 2;
260
261 elemlen = pos[1];
262 if (elemlen > len)
263 return false;
264
265 len -= elemlen;
266 pos += 2 + elemlen;
267 }
268
269 return true;
270}
271
Johannes Berg55682962007-09-20 13:09:35 -0400272/* message building helper */
273static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
274 int flags, u8 cmd)
275{
276 /* since there is no private header just add the generic one */
277 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
278}
279
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400280static int nl80211_msg_put_channel(struct sk_buff *msg,
281 struct ieee80211_channel *chan)
282{
283 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
284 chan->center_freq);
285
286 if (chan->flags & IEEE80211_CHAN_DISABLED)
287 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
288 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
289 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
290 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
291 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
292 if (chan->flags & IEEE80211_CHAN_RADAR)
293 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
294
295 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
296 DBM_TO_MBM(chan->max_power));
297
298 return 0;
299
300 nla_put_failure:
301 return -ENOBUFS;
302}
303
Johannes Berg55682962007-09-20 13:09:35 -0400304/* netlink command implementations */
305
Johannes Bergb9454e82009-07-08 13:29:08 +0200306struct key_parse {
307 struct key_params p;
308 int idx;
309 bool def, defmgmt;
310};
311
312static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
313{
314 struct nlattr *tb[NL80211_KEY_MAX + 1];
315 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
316 nl80211_key_policy);
317 if (err)
318 return err;
319
320 k->def = !!tb[NL80211_KEY_DEFAULT];
321 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
322
323 if (tb[NL80211_KEY_IDX])
324 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
325
326 if (tb[NL80211_KEY_DATA]) {
327 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
328 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
329 }
330
331 if (tb[NL80211_KEY_SEQ]) {
332 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
333 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
334 }
335
336 if (tb[NL80211_KEY_CIPHER])
337 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
338
339 return 0;
340}
341
342static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
343{
344 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
345 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
346 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
347 }
348
349 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
350 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
351 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
352 }
353
354 if (info->attrs[NL80211_ATTR_KEY_IDX])
355 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
356
357 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
358 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
359
360 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
361 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
362
363 return 0;
364}
365
366static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
367{
368 int err;
369
370 memset(k, 0, sizeof(*k));
371 k->idx = -1;
372
373 if (info->attrs[NL80211_ATTR_KEY])
374 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
375 else
376 err = nl80211_parse_key_old(info, k);
377
378 if (err)
379 return err;
380
381 if (k->def && k->defmgmt)
382 return -EINVAL;
383
384 if (k->idx != -1) {
385 if (k->defmgmt) {
386 if (k->idx < 4 || k->idx > 5)
387 return -EINVAL;
388 } else if (k->def) {
389 if (k->idx < 0 || k->idx > 3)
390 return -EINVAL;
391 } else {
392 if (k->idx < 0 || k->idx > 5)
393 return -EINVAL;
394 }
395 }
396
397 return 0;
398}
399
Johannes Bergfffd0932009-07-08 14:22:54 +0200400static struct cfg80211_cached_keys *
401nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
402 struct nlattr *keys)
403{
404 struct key_parse parse;
405 struct nlattr *key;
406 struct cfg80211_cached_keys *result;
407 int rem, err, def = 0;
408
409 result = kzalloc(sizeof(*result), GFP_KERNEL);
410 if (!result)
411 return ERR_PTR(-ENOMEM);
412
413 result->def = -1;
414 result->defmgmt = -1;
415
416 nla_for_each_nested(key, keys, rem) {
417 memset(&parse, 0, sizeof(parse));
418 parse.idx = -1;
419
420 err = nl80211_parse_key_new(key, &parse);
421 if (err)
422 goto error;
423 err = -EINVAL;
424 if (!parse.p.key)
425 goto error;
426 if (parse.idx < 0 || parse.idx > 4)
427 goto error;
428 if (parse.def) {
429 if (def)
430 goto error;
431 def = 1;
432 result->def = parse.idx;
433 } else if (parse.defmgmt)
434 goto error;
435 err = cfg80211_validate_key_settings(rdev, &parse.p,
436 parse.idx, NULL);
437 if (err)
438 goto error;
439 result->params[parse.idx].cipher = parse.p.cipher;
440 result->params[parse.idx].key_len = parse.p.key_len;
441 result->params[parse.idx].key = result->data[parse.idx];
442 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
443 }
444
445 return result;
446 error:
447 kfree(result);
448 return ERR_PTR(err);
449}
450
451static int nl80211_key_allowed(struct wireless_dev *wdev)
452{
453 ASSERT_WDEV_LOCK(wdev);
454
Johannes Bergfffd0932009-07-08 14:22:54 +0200455 switch (wdev->iftype) {
456 case NL80211_IFTYPE_AP:
457 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200458 case NL80211_IFTYPE_P2P_GO:
Johannes Bergfffd0932009-07-08 14:22:54 +0200459 break;
460 case NL80211_IFTYPE_ADHOC:
461 if (!wdev->current_bss)
462 return -ENOLINK;
463 break;
464 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200465 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200466 if (wdev->sme_state != CFG80211_SME_CONNECTED)
467 return -ENOLINK;
468 break;
469 default:
470 return -EINVAL;
471 }
472
473 return 0;
474}
475
Johannes Berg55682962007-09-20 13:09:35 -0400476static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
477 struct cfg80211_registered_device *dev)
478{
479 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100480 struct nlattr *nl_bands, *nl_band;
481 struct nlattr *nl_freqs, *nl_freq;
482 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700483 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100484 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100485 enum ieee80211_band band;
486 struct ieee80211_channel *chan;
487 struct ieee80211_rate *rate;
488 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700489 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg2e161f72010-08-12 15:38:38 +0200490 const struct ieee80211_txrx_stypes *mgmt_stypes =
491 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400492
493 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
494 if (!hdr)
495 return -1;
496
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500497 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400498 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200499
Johannes Bergf5ea9122009-08-07 16:17:38 +0200500 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
501 cfg80211_rdev_list_generation);
502
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200503 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
504 dev->wiphy.retry_short);
505 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
506 dev->wiphy.retry_long);
507 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
508 dev->wiphy.frag_threshold);
509 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
510 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100511 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
512 dev->wiphy.coverage_class);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200513
Johannes Berg2a519312009-02-10 21:25:55 +0100514 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
515 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200516 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
517 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100518
Johannes Berg25e47c12009-04-02 20:14:06 +0200519 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
520 sizeof(u32) * dev->wiphy.n_cipher_suites,
521 dev->wiphy.cipher_suites);
522
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100523 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
524 dev->wiphy.max_num_pmkids);
525
Johannes Bergc0692b82010-08-27 14:26:53 +0300526 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
527 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
528
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700529 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
530 if (!nl_modes)
531 goto nla_put_failure;
532
533 i = 0;
534 while (ifmodes) {
535 if (ifmodes & 1)
536 NLA_PUT_FLAG(msg, i);
537 ifmodes >>= 1;
538 i++;
539 }
540
541 nla_nest_end(msg, nl_modes);
542
Johannes Bergee688b002008-01-24 19:38:39 +0100543 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
544 if (!nl_bands)
545 goto nla_put_failure;
546
547 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
548 if (!dev->wiphy.bands[band])
549 continue;
550
551 nl_band = nla_nest_start(msg, band);
552 if (!nl_band)
553 goto nla_put_failure;
554
Johannes Bergd51626d2008-10-09 12:20:13 +0200555 /* add HT info */
556 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
557 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
558 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
559 &dev->wiphy.bands[band]->ht_cap.mcs);
560 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
561 dev->wiphy.bands[band]->ht_cap.cap);
562 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
563 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
564 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
565 dev->wiphy.bands[band]->ht_cap.ampdu_density);
566 }
567
Johannes Bergee688b002008-01-24 19:38:39 +0100568 /* add frequencies */
569 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
570 if (!nl_freqs)
571 goto nla_put_failure;
572
573 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
574 nl_freq = nla_nest_start(msg, i);
575 if (!nl_freq)
576 goto nla_put_failure;
577
578 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100579
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400580 if (nl80211_msg_put_channel(msg, chan))
581 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200582
Johannes Bergee688b002008-01-24 19:38:39 +0100583 nla_nest_end(msg, nl_freq);
584 }
585
586 nla_nest_end(msg, nl_freqs);
587
588 /* add bitrates */
589 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
590 if (!nl_rates)
591 goto nla_put_failure;
592
593 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
594 nl_rate = nla_nest_start(msg, i);
595 if (!nl_rate)
596 goto nla_put_failure;
597
598 rate = &dev->wiphy.bands[band]->bitrates[i];
599 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
600 rate->bitrate);
601 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
602 NLA_PUT_FLAG(msg,
603 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
604
605 nla_nest_end(msg, nl_rate);
606 }
607
608 nla_nest_end(msg, nl_rates);
609
610 nla_nest_end(msg, nl_band);
611 }
612 nla_nest_end(msg, nl_bands);
613
Johannes Berg8fdc6212009-03-14 09:34:01 +0100614 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
615 if (!nl_cmds)
616 goto nla_put_failure;
617
618 i = 0;
619#define CMD(op, n) \
620 do { \
621 if (dev->ops->op) { \
622 i++; \
623 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
624 } \
625 } while (0)
626
627 CMD(add_virtual_intf, NEW_INTERFACE);
628 CMD(change_virtual_intf, SET_INTERFACE);
629 CMD(add_key, NEW_KEY);
630 CMD(add_beacon, NEW_BEACON);
631 CMD(add_station, NEW_STATION);
632 CMD(add_mpath, NEW_MPATH);
633 CMD(set_mesh_params, SET_MESH_PARAMS);
634 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200635 CMD(auth, AUTHENTICATE);
636 CMD(assoc, ASSOCIATE);
637 CMD(deauth, DEAUTHENTICATE);
638 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200639 CMD(join_ibss, JOIN_IBSS);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100640 CMD(set_pmksa, SET_PMKSA);
641 CMD(del_pmksa, DEL_PMKSA);
642 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100643 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200644 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200645 CMD(mgmt_tx, FRAME);
Johannes Berg5be83de2009-11-19 00:56:28 +0100646 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200647 i++;
648 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
649 }
Johannes Bergf444de02010-05-05 15:25:02 +0200650 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400651 CMD(set_wds_peer, SET_WDS_PEER);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100652
653#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200654
Johannes Berg6829c872009-07-02 09:13:27 +0200655 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200656 i++;
657 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
658 }
659
Johannes Berg6829c872009-07-02 09:13:27 +0200660 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200661 i++;
662 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
663 }
664
Johannes Berg8fdc6212009-03-14 09:34:01 +0100665 nla_nest_end(msg, nl_cmds);
666
Johannes Berg2e161f72010-08-12 15:38:38 +0200667 if (mgmt_stypes) {
668 u16 stypes;
669 struct nlattr *nl_ftypes, *nl_ifs;
670 enum nl80211_iftype ift;
671
672 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
673 if (!nl_ifs)
674 goto nla_put_failure;
675
676 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
677 nl_ftypes = nla_nest_start(msg, ift);
678 if (!nl_ftypes)
679 goto nla_put_failure;
680 i = 0;
681 stypes = mgmt_stypes[ift].tx;
682 while (stypes) {
683 if (stypes & 1)
684 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
685 (i << 4) | IEEE80211_FTYPE_MGMT);
686 stypes >>= 1;
687 i++;
688 }
689 nla_nest_end(msg, nl_ftypes);
690 }
691
Johannes Berg74b70a42010-08-24 12:15:53 +0200692 nla_nest_end(msg, nl_ifs);
693
Johannes Berg2e161f72010-08-12 15:38:38 +0200694 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
695 if (!nl_ifs)
696 goto nla_put_failure;
697
698 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
699 nl_ftypes = nla_nest_start(msg, ift);
700 if (!nl_ftypes)
701 goto nla_put_failure;
702 i = 0;
703 stypes = mgmt_stypes[ift].rx;
704 while (stypes) {
705 if (stypes & 1)
706 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
707 (i << 4) | IEEE80211_FTYPE_MGMT);
708 stypes >>= 1;
709 i++;
710 }
711 nla_nest_end(msg, nl_ftypes);
712 }
713 nla_nest_end(msg, nl_ifs);
714 }
715
Johannes Berg55682962007-09-20 13:09:35 -0400716 return genlmsg_end(msg, hdr);
717
718 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700719 genlmsg_cancel(msg, hdr);
720 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400721}
722
723static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
724{
725 int idx = 0;
726 int start = cb->args[0];
727 struct cfg80211_registered_device *dev;
728
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500729 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200730 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200731 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
732 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200733 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400734 continue;
735 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
736 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200737 dev) < 0) {
738 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400739 break;
Julius Volzb4637272008-07-08 14:02:19 +0200740 }
Johannes Berg55682962007-09-20 13:09:35 -0400741 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500742 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400743
744 cb->args[0] = idx;
745
746 return skb->len;
747}
748
749static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
750{
751 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200752 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400753
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700754 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400755 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200756 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400757
Johannes Berg4c476992010-10-04 21:36:35 +0200758 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
759 nlmsg_free(msg);
760 return -ENOBUFS;
761 }
Johannes Berg55682962007-09-20 13:09:35 -0400762
Johannes Berg134e6372009-07-10 09:51:34 +0000763 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400764}
765
Jouni Malinen31888482008-10-30 16:59:24 +0200766static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
767 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
768 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
769 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
770 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
771 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
772};
773
774static int parse_txq_params(struct nlattr *tb[],
775 struct ieee80211_txq_params *txq_params)
776{
777 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
778 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
779 !tb[NL80211_TXQ_ATTR_AIFS])
780 return -EINVAL;
781
782 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
783 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
784 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
785 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
786 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
787
788 return 0;
789}
790
Johannes Bergf444de02010-05-05 15:25:02 +0200791static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
792{
793 /*
794 * You can only set the channel explicitly for AP, mesh
795 * and WDS type interfaces; all others have their channel
796 * managed via their respective "establish a connection"
797 * command (connect, join, ...)
798 *
799 * Monitors are special as they are normally slaved to
800 * whatever else is going on, so they behave as though
801 * you tried setting the wiphy channel itself.
802 */
803 return !wdev ||
804 wdev->iftype == NL80211_IFTYPE_AP ||
805 wdev->iftype == NL80211_IFTYPE_WDS ||
806 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200807 wdev->iftype == NL80211_IFTYPE_MONITOR ||
808 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200809}
810
811static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
812 struct wireless_dev *wdev,
813 struct genl_info *info)
814{
815 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
816 u32 freq;
817 int result;
818
819 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
820 return -EINVAL;
821
822 if (!nl80211_can_set_dev_channel(wdev))
823 return -EOPNOTSUPP;
824
825 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
826 channel_type = nla_get_u32(info->attrs[
827 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
828 if (channel_type != NL80211_CHAN_NO_HT &&
829 channel_type != NL80211_CHAN_HT20 &&
830 channel_type != NL80211_CHAN_HT40PLUS &&
831 channel_type != NL80211_CHAN_HT40MINUS)
832 return -EINVAL;
833 }
834
835 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
836
837 mutex_lock(&rdev->devlist_mtx);
838 if (wdev) {
839 wdev_lock(wdev);
840 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
841 wdev_unlock(wdev);
842 } else {
843 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
844 }
845 mutex_unlock(&rdev->devlist_mtx);
846
847 return result;
848}
849
850static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
851{
Johannes Berg4c476992010-10-04 21:36:35 +0200852 struct cfg80211_registered_device *rdev = info->user_ptr[0];
853 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +0200854
Johannes Berg4c476992010-10-04 21:36:35 +0200855 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +0200856}
857
Bill Jordane8347eb2010-10-01 13:54:28 -0400858static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
859{
860 struct cfg80211_registered_device *rdev;
861 struct wireless_dev *wdev;
862 struct net_device *dev;
863 u8 *bssid;
864 int err;
865
866 if (!info->attrs[NL80211_ATTR_MAC])
867 return -EINVAL;
868
869 rtnl_lock();
870
871 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
872 if (err)
873 goto unlock_rtnl;
874
875 wdev = dev->ieee80211_ptr;
876
877 if (netif_running(dev)) {
878 err = -EBUSY;
879 goto out;
880 }
881
882 if (!rdev->ops->set_wds_peer) {
883 err = -EOPNOTSUPP;
884 goto out;
885 }
886
887 if (wdev->iftype != NL80211_IFTYPE_WDS) {
888 err = -EOPNOTSUPP;
889 goto out;
890 }
891
892 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
893 err = rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
894
895out:
896 cfg80211_unlock_rdev(rdev);
897 dev_put(dev);
898unlock_rtnl:
899 rtnl_unlock();
900
901 return err;
902}
903
904
Johannes Berg55682962007-09-20 13:09:35 -0400905static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
906{
907 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200908 struct net_device *netdev = NULL;
909 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -0400910 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200911 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200912 u32 changed;
913 u8 retry_short = 0, retry_long = 0;
914 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100915 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400916
Johannes Bergf444de02010-05-05 15:25:02 +0200917 /*
918 * Try to find the wiphy and netdev. Normally this
919 * function shouldn't need the netdev, but this is
920 * done for backward compatibility -- previously
921 * setting the channel was done per wiphy, but now
922 * it is per netdev. Previous userland like hostapd
923 * also passed a netdev to set_wiphy, so that it is
924 * possible to let that go to the right netdev!
925 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100926 mutex_lock(&cfg80211_mutex);
927
Johannes Bergf444de02010-05-05 15:25:02 +0200928 if (info->attrs[NL80211_ATTR_IFINDEX]) {
929 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
930
931 netdev = dev_get_by_index(genl_info_net(info), ifindex);
932 if (netdev && netdev->ieee80211_ptr) {
933 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
934 mutex_lock(&rdev->mtx);
935 } else
936 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100937 }
938
Johannes Bergf444de02010-05-05 15:25:02 +0200939 if (!netdev) {
940 rdev = __cfg80211_rdev_from_info(info);
941 if (IS_ERR(rdev)) {
942 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +0200943 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +0200944 }
945 wdev = NULL;
946 netdev = NULL;
947 result = 0;
948
949 mutex_lock(&rdev->mtx);
950 } else if (netif_running(netdev) &&
951 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
952 wdev = netdev->ieee80211_ptr;
953 else
954 wdev = NULL;
955
956 /*
957 * end workaround code, by now the rdev is available
958 * and locked, and wdev may or may not be NULL.
959 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100960
961 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200962 result = cfg80211_dev_rename(
963 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100964
965 mutex_unlock(&cfg80211_mutex);
966
967 if (result)
968 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400969
Jouni Malinen31888482008-10-30 16:59:24 +0200970 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
971 struct ieee80211_txq_params txq_params;
972 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
973
974 if (!rdev->ops->set_txq_params) {
975 result = -EOPNOTSUPP;
976 goto bad_res;
977 }
978
979 nla_for_each_nested(nl_txq_params,
980 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
981 rem_txq_params) {
982 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
983 nla_data(nl_txq_params),
984 nla_len(nl_txq_params),
985 txq_params_policy);
986 result = parse_txq_params(tb, &txq_params);
987 if (result)
988 goto bad_res;
989
990 result = rdev->ops->set_txq_params(&rdev->wiphy,
991 &txq_params);
992 if (result)
993 goto bad_res;
994 }
995 }
996
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200997 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +0200998 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200999 if (result)
1000 goto bad_res;
1001 }
1002
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001003 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1004 enum nl80211_tx_power_setting type;
1005 int idx, mbm = 0;
1006
1007 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001008 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001009 goto bad_res;
1010 }
1011
1012 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1013 type = nla_get_u32(info->attrs[idx]);
1014
1015 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1016 (type != NL80211_TX_POWER_AUTOMATIC)) {
1017 result = -EINVAL;
1018 goto bad_res;
1019 }
1020
1021 if (type != NL80211_TX_POWER_AUTOMATIC) {
1022 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1023 mbm = nla_get_u32(info->attrs[idx]);
1024 }
1025
1026 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1027 if (result)
1028 goto bad_res;
1029 }
1030
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001031 changed = 0;
1032
1033 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1034 retry_short = nla_get_u8(
1035 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1036 if (retry_short == 0) {
1037 result = -EINVAL;
1038 goto bad_res;
1039 }
1040 changed |= WIPHY_PARAM_RETRY_SHORT;
1041 }
1042
1043 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1044 retry_long = nla_get_u8(
1045 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1046 if (retry_long == 0) {
1047 result = -EINVAL;
1048 goto bad_res;
1049 }
1050 changed |= WIPHY_PARAM_RETRY_LONG;
1051 }
1052
1053 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1054 frag_threshold = nla_get_u32(
1055 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1056 if (frag_threshold < 256) {
1057 result = -EINVAL;
1058 goto bad_res;
1059 }
1060 if (frag_threshold != (u32) -1) {
1061 /*
1062 * Fragments (apart from the last one) are required to
1063 * have even length. Make the fragmentation code
1064 * simpler by stripping LSB should someone try to use
1065 * odd threshold value.
1066 */
1067 frag_threshold &= ~0x1;
1068 }
1069 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1070 }
1071
1072 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1073 rts_threshold = nla_get_u32(
1074 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1075 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1076 }
1077
Lukáš Turek81077e82009-12-21 22:50:47 +01001078 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1079 coverage_class = nla_get_u8(
1080 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1081 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1082 }
1083
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001084 if (changed) {
1085 u8 old_retry_short, old_retry_long;
1086 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001087 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001088
1089 if (!rdev->ops->set_wiphy_params) {
1090 result = -EOPNOTSUPP;
1091 goto bad_res;
1092 }
1093
1094 old_retry_short = rdev->wiphy.retry_short;
1095 old_retry_long = rdev->wiphy.retry_long;
1096 old_frag_threshold = rdev->wiphy.frag_threshold;
1097 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001098 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001099
1100 if (changed & WIPHY_PARAM_RETRY_SHORT)
1101 rdev->wiphy.retry_short = retry_short;
1102 if (changed & WIPHY_PARAM_RETRY_LONG)
1103 rdev->wiphy.retry_long = retry_long;
1104 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1105 rdev->wiphy.frag_threshold = frag_threshold;
1106 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1107 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001108 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1109 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001110
1111 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1112 if (result) {
1113 rdev->wiphy.retry_short = old_retry_short;
1114 rdev->wiphy.retry_long = old_retry_long;
1115 rdev->wiphy.frag_threshold = old_frag_threshold;
1116 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001117 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001118 }
1119 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001120
Johannes Berg306d6112008-12-08 12:39:04 +01001121 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001122 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001123 if (netdev)
1124 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001125 return result;
1126}
1127
1128
1129static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001130 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001131 struct net_device *dev)
1132{
1133 void *hdr;
1134
1135 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1136 if (!hdr)
1137 return -1;
1138
1139 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001140 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001141 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001142 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001143
1144 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1145 rdev->devlist_generation ^
1146 (cfg80211_rdev_list_generation << 2));
1147
Johannes Berg55682962007-09-20 13:09:35 -04001148 return genlmsg_end(msg, hdr);
1149
1150 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001151 genlmsg_cancel(msg, hdr);
1152 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001153}
1154
1155static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1156{
1157 int wp_idx = 0;
1158 int if_idx = 0;
1159 int wp_start = cb->args[0];
1160 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001161 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001162 struct wireless_dev *wdev;
1163
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001164 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001165 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1166 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001167 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001168 if (wp_idx < wp_start) {
1169 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001170 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001171 }
Johannes Berg55682962007-09-20 13:09:35 -04001172 if_idx = 0;
1173
Johannes Bergf5ea9122009-08-07 16:17:38 +02001174 mutex_lock(&rdev->devlist_mtx);
1175 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001176 if (if_idx < if_start) {
1177 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001178 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001179 }
Johannes Berg55682962007-09-20 13:09:35 -04001180 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1181 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001182 rdev, wdev->netdev) < 0) {
1183 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001184 goto out;
1185 }
1186 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001187 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001188 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001189
1190 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001191 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001192 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001193 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001194
1195 cb->args[0] = wp_idx;
1196 cb->args[1] = if_idx;
1197
1198 return skb->len;
1199}
1200
1201static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1202{
1203 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001204 struct cfg80211_registered_device *dev = info->user_ptr[0];
1205 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001206
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001207 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001208 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001209 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001210
Johannes Bergd7264052009-04-19 16:23:20 +02001211 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001212 dev, netdev) < 0) {
1213 nlmsg_free(msg);
1214 return -ENOBUFS;
1215 }
Johannes Berg55682962007-09-20 13:09:35 -04001216
Johannes Berg134e6372009-07-10 09:51:34 +00001217 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001218}
1219
Michael Wu66f7ac52008-01-31 19:48:22 +01001220static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1221 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1222 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1223 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1224 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1225 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1226};
1227
1228static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1229{
1230 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1231 int flag;
1232
1233 *mntrflags = 0;
1234
1235 if (!nla)
1236 return -EINVAL;
1237
1238 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1239 nla, mntr_flags_policy))
1240 return -EINVAL;
1241
1242 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1243 if (flags[flag])
1244 *mntrflags |= (1<<flag);
1245
1246 return 0;
1247}
1248
Johannes Berg9bc383d2009-11-19 11:55:19 +01001249static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001250 struct net_device *netdev, u8 use_4addr,
1251 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001252{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001253 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001254 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001255 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001256 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001257 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001258
1259 switch (iftype) {
1260 case NL80211_IFTYPE_AP_VLAN:
1261 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1262 return 0;
1263 break;
1264 case NL80211_IFTYPE_STATION:
1265 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1266 return 0;
1267 break;
1268 default:
1269 break;
1270 }
1271
1272 return -EOPNOTSUPP;
1273}
1274
Johannes Berg55682962007-09-20 13:09:35 -04001275static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1276{
Johannes Berg4c476992010-10-04 21:36:35 +02001277 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001278 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001279 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001280 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001281 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001282 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001283 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001284
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001285 memset(&params, 0, sizeof(params));
1286
Johannes Berg04a773a2009-04-19 21:24:32 +02001287 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001288
Johannes Berg723b0382008-09-16 20:22:09 +02001289 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001290 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001291 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001292 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001293 if (ntype > NL80211_IFTYPE_MAX)
1294 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001295 }
1296
Johannes Berg92ffe052008-09-16 20:39:36 +02001297 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001298 if (ntype != NL80211_IFTYPE_MESH_POINT)
1299 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001300 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1301 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001302 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001303 }
1304
Felix Fietkau8b787642009-11-10 18:53:10 +01001305 if (info->attrs[NL80211_ATTR_4ADDR]) {
1306 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1307 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001308 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001309 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001310 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001311 } else {
1312 params.use_4addr = -1;
1313 }
1314
Johannes Berg92ffe052008-09-16 20:39:36 +02001315 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001316 if (ntype != NL80211_IFTYPE_MONITOR)
1317 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001318 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1319 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001320 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001321 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001322
1323 flags = &_flags;
1324 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001325 }
Johannes Berg3b858752009-03-12 09:55:09 +01001326
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001327 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001328 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001329 else
1330 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001331
Johannes Berg9bc383d2009-11-19 11:55:19 +01001332 if (!err && params.use_4addr != -1)
1333 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1334
Johannes Berg55682962007-09-20 13:09:35 -04001335 return err;
1336}
1337
1338static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1339{
Johannes Berg4c476992010-10-04 21:36:35 +02001340 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001341 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001342 int err;
1343 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001344 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001345
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001346 memset(&params, 0, sizeof(params));
1347
Johannes Berg55682962007-09-20 13:09:35 -04001348 if (!info->attrs[NL80211_ATTR_IFNAME])
1349 return -EINVAL;
1350
1351 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1352 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1353 if (type > NL80211_IFTYPE_MAX)
1354 return -EINVAL;
1355 }
1356
Johannes Berg79c97e92009-07-07 03:56:12 +02001357 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001358 !(rdev->wiphy.interface_modes & (1 << type)))
1359 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001360
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001361 if (type == NL80211_IFTYPE_MESH_POINT &&
1362 info->attrs[NL80211_ATTR_MESH_ID]) {
1363 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1364 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1365 }
1366
Johannes Berg9bc383d2009-11-19 11:55:19 +01001367 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001368 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001369 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001370 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001371 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001372 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001373
Michael Wu66f7ac52008-01-31 19:48:22 +01001374 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1375 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1376 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001377 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001378 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001379 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001380
Johannes Berg55682962007-09-20 13:09:35 -04001381 return err;
1382}
1383
1384static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1385{
Johannes Berg4c476992010-10-04 21:36:35 +02001386 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1387 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001388
Johannes Berg4c476992010-10-04 21:36:35 +02001389 if (!rdev->ops->del_virtual_intf)
1390 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001391
Johannes Berg4c476992010-10-04 21:36:35 +02001392 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001393}
1394
Johannes Berg41ade002007-12-19 02:03:29 +01001395struct get_key_cookie {
1396 struct sk_buff *msg;
1397 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001398 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001399};
1400
1401static void get_key_callback(void *c, struct key_params *params)
1402{
Johannes Bergb9454e82009-07-08 13:29:08 +02001403 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001404 struct get_key_cookie *cookie = c;
1405
1406 if (params->key)
1407 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1408 params->key_len, params->key);
1409
1410 if (params->seq)
1411 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1412 params->seq_len, params->seq);
1413
1414 if (params->cipher)
1415 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1416 params->cipher);
1417
Johannes Bergb9454e82009-07-08 13:29:08 +02001418 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1419 if (!key)
1420 goto nla_put_failure;
1421
1422 if (params->key)
1423 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1424 params->key_len, params->key);
1425
1426 if (params->seq)
1427 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1428 params->seq_len, params->seq);
1429
1430 if (params->cipher)
1431 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1432 params->cipher);
1433
1434 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1435
1436 nla_nest_end(cookie->msg, key);
1437
Johannes Berg41ade002007-12-19 02:03:29 +01001438 return;
1439 nla_put_failure:
1440 cookie->error = 1;
1441}
1442
1443static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1444{
Johannes Berg4c476992010-10-04 21:36:35 +02001445 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001446 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001447 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001448 u8 key_idx = 0;
1449 u8 *mac_addr = NULL;
1450 struct get_key_cookie cookie = {
1451 .error = 0,
1452 };
1453 void *hdr;
1454 struct sk_buff *msg;
1455
1456 if (info->attrs[NL80211_ATTR_KEY_IDX])
1457 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1458
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001459 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001460 return -EINVAL;
1461
1462 if (info->attrs[NL80211_ATTR_MAC])
1463 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1464
Johannes Berg4c476992010-10-04 21:36:35 +02001465 if (!rdev->ops->get_key)
1466 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001467
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001468 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001469 if (!msg)
1470 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001471
1472 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1473 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001474 if (IS_ERR(hdr))
1475 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001476
1477 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001478 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001479
1480 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1481 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1482 if (mac_addr)
1483 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1484
Johannes Berg79c97e92009-07-07 03:56:12 +02001485 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001486 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001487
1488 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001489 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001490
1491 if (cookie.error)
1492 goto nla_put_failure;
1493
1494 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001495 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001496
1497 nla_put_failure:
1498 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001499 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001500 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001501 return err;
1502}
1503
1504static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1505{
Johannes Berg4c476992010-10-04 21:36:35 +02001506 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001507 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001508 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001509 struct net_device *dev = info->user_ptr[1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001510 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1511 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001512
Johannes Bergb9454e82009-07-08 13:29:08 +02001513 err = nl80211_parse_key(info, &key);
1514 if (err)
1515 return err;
1516
1517 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001518 return -EINVAL;
1519
Johannes Bergb9454e82009-07-08 13:29:08 +02001520 /* only support setting default key */
1521 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001522 return -EINVAL;
1523
Johannes Bergb9454e82009-07-08 13:29:08 +02001524 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001525 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001526 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001527 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001528
Johannes Berg4c476992010-10-04 21:36:35 +02001529 if (!func)
1530 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001531
Johannes Bergfffd0932009-07-08 14:22:54 +02001532 wdev_lock(dev->ieee80211_ptr);
1533 err = nl80211_key_allowed(dev->ieee80211_ptr);
1534 if (!err)
1535 err = func(&rdev->wiphy, dev, key.idx);
1536
Johannes Berg3d23e342009-09-29 23:27:28 +02001537#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001538 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001539 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001540 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001541 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001542 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001543 }
1544#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001545 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001546
Johannes Berg41ade002007-12-19 02:03:29 +01001547 return err;
1548}
1549
1550static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1551{
Johannes Berg4c476992010-10-04 21:36:35 +02001552 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001553 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001554 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001555 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001556 u8 *mac_addr = NULL;
1557
Johannes Bergb9454e82009-07-08 13:29:08 +02001558 err = nl80211_parse_key(info, &key);
1559 if (err)
1560 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001561
Johannes Bergb9454e82009-07-08 13:29:08 +02001562 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001563 return -EINVAL;
1564
Johannes Berg41ade002007-12-19 02:03:29 +01001565 if (info->attrs[NL80211_ATTR_MAC])
1566 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1567
Johannes Berg4c476992010-10-04 21:36:35 +02001568 if (!rdev->ops->add_key)
1569 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001570
Johannes Berg4c476992010-10-04 21:36:35 +02001571 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr))
1572 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001573
1574 wdev_lock(dev->ieee80211_ptr);
1575 err = nl80211_key_allowed(dev->ieee80211_ptr);
1576 if (!err)
1577 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1578 mac_addr, &key.p);
1579 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001580
Johannes Berg41ade002007-12-19 02:03:29 +01001581 return err;
1582}
1583
1584static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1585{
Johannes Berg4c476992010-10-04 21:36:35 +02001586 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001587 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001588 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001589 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001590 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001591
Johannes Bergb9454e82009-07-08 13:29:08 +02001592 err = nl80211_parse_key(info, &key);
1593 if (err)
1594 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001595
1596 if (info->attrs[NL80211_ATTR_MAC])
1597 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1598
Johannes Berg4c476992010-10-04 21:36:35 +02001599 if (!rdev->ops->del_key)
1600 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001601
Johannes Bergfffd0932009-07-08 14:22:54 +02001602 wdev_lock(dev->ieee80211_ptr);
1603 err = nl80211_key_allowed(dev->ieee80211_ptr);
1604 if (!err)
1605 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001606
Johannes Berg3d23e342009-09-29 23:27:28 +02001607#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001608 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001609 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001610 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001611 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001612 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1613 }
1614#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001615 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001616
Johannes Berg41ade002007-12-19 02:03:29 +01001617 return err;
1618}
1619
Johannes Berged1b6cc2007-12-19 02:03:32 +01001620static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1621{
1622 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1623 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001624 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1625 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001626 struct beacon_parameters params;
1627 int haveinfo = 0;
1628
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001629 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1630 return -EINVAL;
1631
Johannes Berg074ac8d2010-09-16 14:58:22 +02001632 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001633 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1634 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001635
Johannes Berged1b6cc2007-12-19 02:03:32 +01001636 switch (info->genlhdr->cmd) {
1637 case NL80211_CMD_NEW_BEACON:
1638 /* these are required for NEW_BEACON */
1639 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1640 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001641 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1642 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001643
Johannes Berg79c97e92009-07-07 03:56:12 +02001644 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001645 break;
1646 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001647 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001648 break;
1649 default:
1650 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001651 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001652 }
1653
Johannes Berg4c476992010-10-04 21:36:35 +02001654 if (!call)
1655 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001656
1657 memset(&params, 0, sizeof(params));
1658
1659 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1660 params.interval =
1661 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1662 haveinfo = 1;
1663 }
1664
1665 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1666 params.dtim_period =
1667 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1668 haveinfo = 1;
1669 }
1670
1671 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1672 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1673 params.head_len =
1674 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1675 haveinfo = 1;
1676 }
1677
1678 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1679 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1680 params.tail_len =
1681 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1682 haveinfo = 1;
1683 }
1684
Johannes Berg4c476992010-10-04 21:36:35 +02001685 if (!haveinfo)
1686 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001687
Johannes Berg4c476992010-10-04 21:36:35 +02001688 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001689}
1690
1691static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1692{
Johannes Berg4c476992010-10-04 21:36:35 +02001693 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1694 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001695
Johannes Berg4c476992010-10-04 21:36:35 +02001696 if (!rdev->ops->del_beacon)
1697 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001698
Johannes Berg074ac8d2010-09-16 14:58:22 +02001699 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001700 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1701 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001702
Johannes Berg4c476992010-10-04 21:36:35 +02001703 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001704}
1705
Johannes Berg5727ef12007-12-19 02:03:34 +01001706static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1707 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1708 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1709 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001710 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001711};
1712
Johannes Bergeccb8e82009-05-11 21:57:56 +03001713static int parse_station_flags(struct genl_info *info,
1714 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001715{
1716 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001717 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001718 int flag;
1719
Johannes Bergeccb8e82009-05-11 21:57:56 +03001720 /*
1721 * Try parsing the new attribute first so userspace
1722 * can specify both for older kernels.
1723 */
1724 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1725 if (nla) {
1726 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001727
Johannes Bergeccb8e82009-05-11 21:57:56 +03001728 sta_flags = nla_data(nla);
1729 params->sta_flags_mask = sta_flags->mask;
1730 params->sta_flags_set = sta_flags->set;
1731 if ((params->sta_flags_mask |
1732 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1733 return -EINVAL;
1734 return 0;
1735 }
1736
1737 /* if present, parse the old attribute */
1738
1739 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001740 if (!nla)
1741 return 0;
1742
1743 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1744 nla, sta_flags_policy))
1745 return -EINVAL;
1746
Johannes Bergeccb8e82009-05-11 21:57:56 +03001747 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1748 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001749
1750 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1751 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001752 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001753
1754 return 0;
1755}
1756
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001757static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1758 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001759 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001760{
1761 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001762 struct nlattr *sinfoattr, *txrate;
1763 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001764
1765 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1766 if (!hdr)
1767 return -1;
1768
1769 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1770 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1771
Johannes Bergf5ea9122009-08-07 16:17:38 +02001772 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1773
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001774 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1775 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001776 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001777 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1778 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1779 sinfo->inactive_time);
1780 if (sinfo->filled & STATION_INFO_RX_BYTES)
1781 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1782 sinfo->rx_bytes);
1783 if (sinfo->filled & STATION_INFO_TX_BYTES)
1784 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1785 sinfo->tx_bytes);
1786 if (sinfo->filled & STATION_INFO_LLID)
1787 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1788 sinfo->llid);
1789 if (sinfo->filled & STATION_INFO_PLID)
1790 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1791 sinfo->plid);
1792 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1793 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1794 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001795 if (sinfo->filled & STATION_INFO_SIGNAL)
1796 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1797 sinfo->signal);
1798 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1799 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1800 if (!txrate)
1801 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001802
John W. Linville254416a2009-12-09 16:43:52 -05001803 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1804 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001805 if (bitrate > 0)
1806 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1807
1808 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1809 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1810 sinfo->txrate.mcs);
1811 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1812 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1813 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1814 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1815
1816 nla_nest_end(msg, txrate);
1817 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001818 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1819 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1820 sinfo->rx_packets);
1821 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1822 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1823 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001824 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001825
1826 return genlmsg_end(msg, hdr);
1827
1828 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001829 genlmsg_cancel(msg, hdr);
1830 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001831}
1832
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001833static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001834 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001835{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001836 struct station_info sinfo;
1837 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001838 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001839 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001840 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001841 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001842
Johannes Berg67748892010-10-04 21:14:06 +02001843 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1844 if (err)
1845 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001846
1847 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001848 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001849 goto out_err;
1850 }
1851
Johannes Bergbba95fe2008-07-29 13:22:51 +02001852 while (1) {
1853 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1854 mac_addr, &sinfo);
1855 if (err == -ENOENT)
1856 break;
1857 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001858 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001859
1860 if (nl80211_send_station(skb,
1861 NETLINK_CB(cb->skb).pid,
1862 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1863 netdev, mac_addr,
1864 &sinfo) < 0)
1865 goto out;
1866
1867 sta_idx++;
1868 }
1869
1870
1871 out:
1872 cb->args[1] = sta_idx;
1873 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001874 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02001875 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001876
1877 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001878}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001879
Johannes Berg5727ef12007-12-19 02:03:34 +01001880static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1881{
Johannes Berg4c476992010-10-04 21:36:35 +02001882 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1883 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001884 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001885 struct sk_buff *msg;
1886 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02001887 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001888
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001889 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001890
1891 if (!info->attrs[NL80211_ATTR_MAC])
1892 return -EINVAL;
1893
1894 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1895
Johannes Berg4c476992010-10-04 21:36:35 +02001896 if (!rdev->ops->get_station)
1897 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001898
Johannes Berg79c97e92009-07-07 03:56:12 +02001899 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001900 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001901 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001902
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001903 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001904 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001905 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001906
1907 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001908 dev, mac_addr, &sinfo) < 0) {
1909 nlmsg_free(msg);
1910 return -ENOBUFS;
1911 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001912
Johannes Berg4c476992010-10-04 21:36:35 +02001913 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01001914}
1915
1916/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001917 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01001918 */
Johannes Berg463d0182009-07-14 00:33:35 +02001919static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01001920 struct cfg80211_registered_device *rdev,
1921 struct net_device **vlan)
1922{
Johannes Berg463d0182009-07-14 00:33:35 +02001923 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01001924 *vlan = NULL;
1925
1926 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02001927 *vlan = dev_get_by_index(genl_info_net(info),
1928 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01001929 if (!*vlan)
1930 return -ENODEV;
1931 if (!(*vlan)->ieee80211_ptr)
1932 return -EINVAL;
1933 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1934 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001935 if (!netif_running(*vlan))
1936 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01001937 }
1938 return 0;
1939}
1940
1941static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1942{
Johannes Berg4c476992010-10-04 21:36:35 +02001943 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01001944 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001945 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01001946 struct station_parameters params;
1947 u8 *mac_addr = NULL;
1948
1949 memset(&params, 0, sizeof(params));
1950
1951 params.listen_interval = -1;
1952
1953 if (info->attrs[NL80211_ATTR_STA_AID])
1954 return -EINVAL;
1955
1956 if (!info->attrs[NL80211_ATTR_MAC])
1957 return -EINVAL;
1958
1959 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1960
1961 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1962 params.supported_rates =
1963 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1964 params.supported_rates_len =
1965 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1966 }
1967
1968 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1969 params.listen_interval =
1970 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1971
Jouni Malinen36aedc92008-08-25 11:58:58 +03001972 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1973 params.ht_capa =
1974 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1975
Johannes Bergeccb8e82009-05-11 21:57:56 +03001976 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001977 return -EINVAL;
1978
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001979 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1980 params.plink_action =
1981 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1982
Johannes Berg463d0182009-07-14 00:33:35 +02001983 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001984 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02001985 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001986
1987 /* validate settings */
1988 err = 0;
1989
1990 switch (dev->ieee80211_ptr->iftype) {
1991 case NL80211_IFTYPE_AP:
1992 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02001993 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02001994 /* disallow mesh-specific things */
1995 if (params.plink_action)
1996 err = -EINVAL;
1997 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02001998 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02001999 case NL80211_IFTYPE_STATION:
2000 /* disallow everything but AUTHORIZED flag */
2001 if (params.plink_action)
2002 err = -EINVAL;
2003 if (params.vlan)
2004 err = -EINVAL;
2005 if (params.supported_rates)
2006 err = -EINVAL;
2007 if (params.ht_capa)
2008 err = -EINVAL;
2009 if (params.listen_interval >= 0)
2010 err = -EINVAL;
2011 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2012 err = -EINVAL;
2013 break;
2014 case NL80211_IFTYPE_MESH_POINT:
2015 /* disallow things mesh doesn't support */
2016 if (params.vlan)
2017 err = -EINVAL;
2018 if (params.ht_capa)
2019 err = -EINVAL;
2020 if (params.listen_interval >= 0)
2021 err = -EINVAL;
2022 if (params.supported_rates)
2023 err = -EINVAL;
2024 if (params.sta_flags_mask)
2025 err = -EINVAL;
2026 break;
2027 default:
2028 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002029 }
2030
Johannes Berg5727ef12007-12-19 02:03:34 +01002031 if (err)
2032 goto out;
2033
Johannes Berg79c97e92009-07-07 03:56:12 +02002034 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002035 err = -EOPNOTSUPP;
2036 goto out;
2037 }
2038
Johannes Berg79c97e92009-07-07 03:56:12 +02002039 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002040
2041 out:
2042 if (params.vlan)
2043 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002044
Johannes Berg5727ef12007-12-19 02:03:34 +01002045 return err;
2046}
2047
2048static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2049{
Johannes Berg4c476992010-10-04 21:36:35 +02002050 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002051 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002052 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002053 struct station_parameters params;
2054 u8 *mac_addr = NULL;
2055
2056 memset(&params, 0, sizeof(params));
2057
2058 if (!info->attrs[NL80211_ATTR_MAC])
2059 return -EINVAL;
2060
Johannes Berg5727ef12007-12-19 02:03:34 +01002061 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2062 return -EINVAL;
2063
2064 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2065 return -EINVAL;
2066
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002067 if (!info->attrs[NL80211_ATTR_STA_AID])
2068 return -EINVAL;
2069
Johannes Berg5727ef12007-12-19 02:03:34 +01002070 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2071 params.supported_rates =
2072 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2073 params.supported_rates_len =
2074 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2075 params.listen_interval =
2076 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002077
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002078 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2079 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2080 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002081
Jouni Malinen36aedc92008-08-25 11:58:58 +03002082 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2083 params.ht_capa =
2084 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002085
Johannes Bergeccb8e82009-05-11 21:57:56 +03002086 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002087 return -EINVAL;
2088
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002089 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002090 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02002091 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2092 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002093
Johannes Berg463d0182009-07-14 00:33:35 +02002094 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002095 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002096 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002097
2098 /* validate settings */
2099 err = 0;
2100
Johannes Berg79c97e92009-07-07 03:56:12 +02002101 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002102 err = -EOPNOTSUPP;
2103 goto out;
2104 }
2105
Johannes Berg79c97e92009-07-07 03:56:12 +02002106 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002107
2108 out:
2109 if (params.vlan)
2110 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002111 return err;
2112}
2113
2114static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2115{
Johannes Berg4c476992010-10-04 21:36:35 +02002116 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2117 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002118 u8 *mac_addr = NULL;
2119
2120 if (info->attrs[NL80211_ATTR_MAC])
2121 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2122
Johannes Berge80cf852009-05-11 14:43:13 +02002123 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002124 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002125 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002126 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2127 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002128
Johannes Berg4c476992010-10-04 21:36:35 +02002129 if (!rdev->ops->del_station)
2130 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002131
Johannes Berg4c476992010-10-04 21:36:35 +02002132 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002133}
2134
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002135static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2136 int flags, struct net_device *dev,
2137 u8 *dst, u8 *next_hop,
2138 struct mpath_info *pinfo)
2139{
2140 void *hdr;
2141 struct nlattr *pinfoattr;
2142
2143 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2144 if (!hdr)
2145 return -1;
2146
2147 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2148 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2149 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2150
Johannes Bergf5ea9122009-08-07 16:17:38 +02002151 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2152
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002153 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2154 if (!pinfoattr)
2155 goto nla_put_failure;
2156 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2157 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2158 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002159 if (pinfo->filled & MPATH_INFO_SN)
2160 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2161 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002162 if (pinfo->filled & MPATH_INFO_METRIC)
2163 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2164 pinfo->metric);
2165 if (pinfo->filled & MPATH_INFO_EXPTIME)
2166 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2167 pinfo->exptime);
2168 if (pinfo->filled & MPATH_INFO_FLAGS)
2169 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2170 pinfo->flags);
2171 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2172 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2173 pinfo->discovery_timeout);
2174 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2175 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2176 pinfo->discovery_retries);
2177
2178 nla_nest_end(msg, pinfoattr);
2179
2180 return genlmsg_end(msg, hdr);
2181
2182 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002183 genlmsg_cancel(msg, hdr);
2184 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002185}
2186
2187static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002188 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002189{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002190 struct mpath_info pinfo;
2191 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002192 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002193 u8 dst[ETH_ALEN];
2194 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002195 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002196 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002197
Johannes Berg67748892010-10-04 21:14:06 +02002198 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2199 if (err)
2200 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002201
2202 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002203 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002204 goto out_err;
2205 }
2206
Jouni Malineneec60b02009-03-20 21:21:19 +02002207 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2208 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002209 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002210 }
2211
Johannes Bergbba95fe2008-07-29 13:22:51 +02002212 while (1) {
2213 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2214 dst, next_hop, &pinfo);
2215 if (err == -ENOENT)
2216 break;
2217 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002218 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002219
2220 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2221 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2222 netdev, dst, next_hop,
2223 &pinfo) < 0)
2224 goto out;
2225
2226 path_idx++;
2227 }
2228
2229
2230 out:
2231 cb->args[1] = path_idx;
2232 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002233 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002234 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002235 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002236}
2237
2238static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2239{
Johannes Berg4c476992010-10-04 21:36:35 +02002240 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002241 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002242 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002243 struct mpath_info pinfo;
2244 struct sk_buff *msg;
2245 u8 *dst = NULL;
2246 u8 next_hop[ETH_ALEN];
2247
2248 memset(&pinfo, 0, sizeof(pinfo));
2249
2250 if (!info->attrs[NL80211_ATTR_MAC])
2251 return -EINVAL;
2252
2253 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2254
Johannes Berg4c476992010-10-04 21:36:35 +02002255 if (!rdev->ops->get_mpath)
2256 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002257
Johannes Berg4c476992010-10-04 21:36:35 +02002258 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2259 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002260
Johannes Berg79c97e92009-07-07 03:56:12 +02002261 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002262 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002263 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002264
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002265 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002266 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002267 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002268
2269 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002270 dev, dst, next_hop, &pinfo) < 0) {
2271 nlmsg_free(msg);
2272 return -ENOBUFS;
2273 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002274
Johannes Berg4c476992010-10-04 21:36:35 +02002275 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002276}
2277
2278static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2279{
Johannes Berg4c476992010-10-04 21:36:35 +02002280 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2281 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002282 u8 *dst = NULL;
2283 u8 *next_hop = NULL;
2284
2285 if (!info->attrs[NL80211_ATTR_MAC])
2286 return -EINVAL;
2287
2288 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2289 return -EINVAL;
2290
2291 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2292 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2293
Johannes Berg4c476992010-10-04 21:36:35 +02002294 if (!rdev->ops->change_mpath)
2295 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002296
Johannes Berg4c476992010-10-04 21:36:35 +02002297 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2298 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002299
Johannes Berg4c476992010-10-04 21:36:35 +02002300 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002301}
Johannes Berg4c476992010-10-04 21:36:35 +02002302
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002303static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2304{
Johannes Berg4c476992010-10-04 21:36:35 +02002305 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2306 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002307 u8 *dst = NULL;
2308 u8 *next_hop = NULL;
2309
2310 if (!info->attrs[NL80211_ATTR_MAC])
2311 return -EINVAL;
2312
2313 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2314 return -EINVAL;
2315
2316 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2317 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2318
Johannes Berg4c476992010-10-04 21:36:35 +02002319 if (!rdev->ops->add_mpath)
2320 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002321
Johannes Berg4c476992010-10-04 21:36:35 +02002322 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2323 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002324
Johannes Berg4c476992010-10-04 21:36:35 +02002325 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002326}
2327
2328static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2329{
Johannes Berg4c476992010-10-04 21:36:35 +02002330 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2331 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002332 u8 *dst = NULL;
2333
2334 if (info->attrs[NL80211_ATTR_MAC])
2335 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2336
Johannes Berg4c476992010-10-04 21:36:35 +02002337 if (!rdev->ops->del_mpath)
2338 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002339
Johannes Berg4c476992010-10-04 21:36:35 +02002340 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002341}
2342
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002343static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2344{
Johannes Berg4c476992010-10-04 21:36:35 +02002345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2346 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002347 struct bss_parameters params;
2348
2349 memset(&params, 0, sizeof(params));
2350 /* default to not changing parameters */
2351 params.use_cts_prot = -1;
2352 params.use_short_preamble = -1;
2353 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002354 params.ap_isolate = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002355
2356 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2357 params.use_cts_prot =
2358 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2359 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2360 params.use_short_preamble =
2361 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2362 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2363 params.use_short_slot_time =
2364 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002365 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2366 params.basic_rates =
2367 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2368 params.basic_rates_len =
2369 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2370 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002371 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2372 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002373
Johannes Berg4c476992010-10-04 21:36:35 +02002374 if (!rdev->ops->change_bss)
2375 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002376
Johannes Berg074ac8d2010-09-16 14:58:22 +02002377 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002378 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2379 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002380
Johannes Berg4c476992010-10-04 21:36:35 +02002381 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002382}
2383
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002384static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002385 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2386 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2387 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2388 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2389 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2390 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2391};
2392
2393static int parse_reg_rule(struct nlattr *tb[],
2394 struct ieee80211_reg_rule *reg_rule)
2395{
2396 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2397 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2398
2399 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2400 return -EINVAL;
2401 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2402 return -EINVAL;
2403 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2404 return -EINVAL;
2405 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2406 return -EINVAL;
2407 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2408 return -EINVAL;
2409
2410 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2411
2412 freq_range->start_freq_khz =
2413 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2414 freq_range->end_freq_khz =
2415 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2416 freq_range->max_bandwidth_khz =
2417 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2418
2419 power_rule->max_eirp =
2420 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2421
2422 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2423 power_rule->max_antenna_gain =
2424 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2425
2426 return 0;
2427}
2428
2429static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2430{
2431 int r;
2432 char *data = NULL;
2433
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002434 /*
2435 * You should only get this when cfg80211 hasn't yet initialized
2436 * completely when built-in to the kernel right between the time
2437 * window between nl80211_init() and regulatory_init(), if that is
2438 * even possible.
2439 */
2440 mutex_lock(&cfg80211_mutex);
2441 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002442 mutex_unlock(&cfg80211_mutex);
2443 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002444 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002445 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002446
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002447 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2448 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002449
2450 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2451
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002452 r = regulatory_hint_user(data);
2453
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002454 return r;
2455}
2456
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002457static int nl80211_get_mesh_params(struct sk_buff *skb,
2458 struct genl_info *info)
2459{
Johannes Berg4c476992010-10-04 21:36:35 +02002460 struct cfg80211_registered_device *rdev = info->user_ptr[0];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002461 struct mesh_config cur_params;
2462 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002463 struct net_device *dev = info->user_ptr[1];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002464 void *hdr;
2465 struct nlattr *pinfoattr;
2466 struct sk_buff *msg;
2467
Johannes Berg4c476992010-10-04 21:36:35 +02002468 if (!rdev->ops->get_mesh_params)
2469 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002470
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002471 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002472 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002473 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002474 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002475
2476 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002477 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002478 if (!msg)
2479 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002480 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2481 NL80211_CMD_GET_MESH_PARAMS);
2482 if (!hdr)
2483 goto nla_put_failure;
2484 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2485 if (!pinfoattr)
2486 goto nla_put_failure;
2487 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2488 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2489 cur_params.dot11MeshRetryTimeout);
2490 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2491 cur_params.dot11MeshConfirmTimeout);
2492 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2493 cur_params.dot11MeshHoldingTimeout);
2494 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2495 cur_params.dot11MeshMaxPeerLinks);
2496 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2497 cur_params.dot11MeshMaxRetries);
2498 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2499 cur_params.dot11MeshTTL);
2500 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2501 cur_params.auto_open_plinks);
2502 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2503 cur_params.dot11MeshHWMPmaxPREQretries);
2504 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2505 cur_params.path_refresh_time);
2506 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2507 cur_params.min_discovery_timeout);
2508 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2509 cur_params.dot11MeshHWMPactivePathTimeout);
2510 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2511 cur_params.dot11MeshHWMPpreqMinInterval);
2512 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2513 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002514 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2515 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002516 nla_nest_end(msg, pinfoattr);
2517 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002518 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002519
Johannes Berg3b858752009-03-12 09:55:09 +01002520 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002521 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002522 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002523 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002524}
2525
2526#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2527do {\
2528 if (table[attr_num]) {\
2529 cfg.param = nla_fn(table[attr_num]); \
2530 mask |= (1 << (attr_num - 1)); \
2531 } \
2532} while (0);\
2533
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002534static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002535 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2536 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2537 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2538 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2539 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2540 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2541 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2542
2543 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2544 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2545 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2546 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2547 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2548 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2549};
2550
2551static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2552{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002553 u32 mask;
Johannes Berg4c476992010-10-04 21:36:35 +02002554 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2555 struct net_device *dev = info->user_ptr[1];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002556 struct mesh_config cfg;
2557 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2558 struct nlattr *parent_attr;
2559
2560 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2561 if (!parent_attr)
2562 return -EINVAL;
2563 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2564 parent_attr, nl80211_meshconf_params_policy))
2565 return -EINVAL;
2566
Johannes Berg4c476992010-10-04 21:36:35 +02002567 if (!rdev->ops->set_mesh_params)
2568 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002569
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002570 /* This makes sure that there aren't more than 32 mesh config
2571 * parameters (otherwise our bitfield scheme would not work.) */
2572 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2573
2574 /* Fill in the params struct */
2575 mask = 0;
2576 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2577 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2578 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2579 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2580 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2581 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2582 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2583 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2584 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2585 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2586 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2587 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2588 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2589 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2590 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2591 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2592 nla_get_u8);
2593 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2594 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2595 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2596 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2597 nla_get_u16);
2598 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2599 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2600 nla_get_u32);
2601 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2602 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2603 nla_get_u16);
2604 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2605 dot11MeshHWMPnetDiameterTraversalTime,
2606 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2607 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002608 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2609 dot11MeshHWMPRootMode, mask,
2610 NL80211_MESHCONF_HWMP_ROOTMODE,
2611 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002612
2613 /* Apply changes */
Johannes Berg4c476992010-10-04 21:36:35 +02002614 return rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002615}
2616
2617#undef FILL_IN_MESH_PARAM_IF_SET
2618
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002619static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2620{
2621 struct sk_buff *msg;
2622 void *hdr = NULL;
2623 struct nlattr *nl_reg_rules;
2624 unsigned int i;
2625 int err = -EINVAL;
2626
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002627 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002628
2629 if (!cfg80211_regdomain)
2630 goto out;
2631
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002632 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002633 if (!msg) {
2634 err = -ENOBUFS;
2635 goto out;
2636 }
2637
2638 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2639 NL80211_CMD_GET_REG);
2640 if (!hdr)
2641 goto nla_put_failure;
2642
2643 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2644 cfg80211_regdomain->alpha2);
2645
2646 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2647 if (!nl_reg_rules)
2648 goto nla_put_failure;
2649
2650 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2651 struct nlattr *nl_reg_rule;
2652 const struct ieee80211_reg_rule *reg_rule;
2653 const struct ieee80211_freq_range *freq_range;
2654 const struct ieee80211_power_rule *power_rule;
2655
2656 reg_rule = &cfg80211_regdomain->reg_rules[i];
2657 freq_range = &reg_rule->freq_range;
2658 power_rule = &reg_rule->power_rule;
2659
2660 nl_reg_rule = nla_nest_start(msg, i);
2661 if (!nl_reg_rule)
2662 goto nla_put_failure;
2663
2664 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2665 reg_rule->flags);
2666 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2667 freq_range->start_freq_khz);
2668 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2669 freq_range->end_freq_khz);
2670 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2671 freq_range->max_bandwidth_khz);
2672 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2673 power_rule->max_antenna_gain);
2674 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2675 power_rule->max_eirp);
2676
2677 nla_nest_end(msg, nl_reg_rule);
2678 }
2679
2680 nla_nest_end(msg, nl_reg_rules);
2681
2682 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002683 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002684 goto out;
2685
2686nla_put_failure:
2687 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002688 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002689 err = -EMSGSIZE;
2690out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002691 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002692 return err;
2693}
2694
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002695static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2696{
2697 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2698 struct nlattr *nl_reg_rule;
2699 char *alpha2 = NULL;
2700 int rem_reg_rules = 0, r = 0;
2701 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2702 struct ieee80211_regdomain *rd = NULL;
2703
2704 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2705 return -EINVAL;
2706
2707 if (!info->attrs[NL80211_ATTR_REG_RULES])
2708 return -EINVAL;
2709
2710 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2711
2712 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2713 rem_reg_rules) {
2714 num_rules++;
2715 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002716 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002717 }
2718
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002719 mutex_lock(&cfg80211_mutex);
2720
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002721 if (!reg_is_valid_request(alpha2)) {
2722 r = -EINVAL;
2723 goto bad_reg;
2724 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002725
2726 size_of_regd = sizeof(struct ieee80211_regdomain) +
2727 (num_rules * sizeof(struct ieee80211_reg_rule));
2728
2729 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002730 if (!rd) {
2731 r = -ENOMEM;
2732 goto bad_reg;
2733 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002734
2735 rd->n_reg_rules = num_rules;
2736 rd->alpha2[0] = alpha2[0];
2737 rd->alpha2[1] = alpha2[1];
2738
2739 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2740 rem_reg_rules) {
2741 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2742 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2743 reg_rule_policy);
2744 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2745 if (r)
2746 goto bad_reg;
2747
2748 rule_idx++;
2749
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002750 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2751 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002752 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002753 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002754 }
2755
2756 BUG_ON(rule_idx != num_rules);
2757
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002758 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002759
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002760 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002761
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002762 return r;
2763
Johannes Bergd2372b32008-10-24 20:32:20 +02002764 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002765 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002766 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002767 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002768}
2769
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002770static int validate_scan_freqs(struct nlattr *freqs)
2771{
2772 struct nlattr *attr1, *attr2;
2773 int n_channels = 0, tmp1, tmp2;
2774
2775 nla_for_each_nested(attr1, freqs, tmp1) {
2776 n_channels++;
2777 /*
2778 * Some hardware has a limited channel list for
2779 * scanning, and it is pretty much nonsensical
2780 * to scan for a channel twice, so disallow that
2781 * and don't require drivers to check that the
2782 * channel list they get isn't longer than what
2783 * they can scan, as long as they can scan all
2784 * the channels they registered at once.
2785 */
2786 nla_for_each_nested(attr2, freqs, tmp2)
2787 if (attr1 != attr2 &&
2788 nla_get_u32(attr1) == nla_get_u32(attr2))
2789 return 0;
2790 }
2791
2792 return n_channels;
2793}
2794
Johannes Berg2a519312009-02-10 21:25:55 +01002795static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2796{
Johannes Berg4c476992010-10-04 21:36:35 +02002797 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2798 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01002799 struct cfg80211_scan_request *request;
2800 struct cfg80211_ssid *ssid;
2801 struct ieee80211_channel *channel;
2802 struct nlattr *attr;
2803 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002804 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002805 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002806 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002807
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002808 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2809 return -EINVAL;
2810
Johannes Berg79c97e92009-07-07 03:56:12 +02002811 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002812
Johannes Berg4c476992010-10-04 21:36:35 +02002813 if (!rdev->ops->scan)
2814 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01002815
Johannes Berg4c476992010-10-04 21:36:35 +02002816 if (rdev->scan_req)
2817 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01002818
2819 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002820 n_channels = validate_scan_freqs(
2821 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02002822 if (!n_channels)
2823 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002824 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002825 n_channels = 0;
2826
Johannes Berg2a519312009-02-10 21:25:55 +01002827 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2828 if (wiphy->bands[band])
2829 n_channels += wiphy->bands[band]->n_channels;
2830 }
2831
2832 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2833 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2834 n_ssids++;
2835
Johannes Berg4c476992010-10-04 21:36:35 +02002836 if (n_ssids > wiphy->max_scan_ssids)
2837 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002838
Jouni Malinen70692ad2009-02-16 19:39:13 +02002839 if (info->attrs[NL80211_ATTR_IE])
2840 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2841 else
2842 ie_len = 0;
2843
Johannes Berg4c476992010-10-04 21:36:35 +02002844 if (ie_len > wiphy->max_scan_ie_len)
2845 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02002846
Johannes Berg2a519312009-02-10 21:25:55 +01002847 request = kzalloc(sizeof(*request)
2848 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002849 + sizeof(channel) * n_channels
2850 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002851 if (!request)
2852 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01002853
Johannes Berg2a519312009-02-10 21:25:55 +01002854 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02002855 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01002856 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002857 if (ie_len) {
2858 if (request->ssids)
2859 request->ie = (void *)(request->ssids + n_ssids);
2860 else
2861 request->ie = (void *)(request->channels + n_channels);
2862 }
Johannes Berg2a519312009-02-10 21:25:55 +01002863
Johannes Berg584991d2009-11-02 13:32:03 +01002864 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01002865 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2866 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01002867 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01002868 struct ieee80211_channel *chan;
2869
2870 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2871
2872 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002873 err = -EINVAL;
2874 goto out_free;
2875 }
Johannes Berg584991d2009-11-02 13:32:03 +01002876
2877 /* ignore disabled channels */
2878 if (chan->flags & IEEE80211_CHAN_DISABLED)
2879 continue;
2880
2881 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01002882 i++;
2883 }
2884 } else {
2885 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01002886 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2887 int j;
2888 if (!wiphy->bands[band])
2889 continue;
2890 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01002891 struct ieee80211_channel *chan;
2892
2893 chan = &wiphy->bands[band]->channels[j];
2894
2895 if (chan->flags & IEEE80211_CHAN_DISABLED)
2896 continue;
2897
2898 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01002899 i++;
2900 }
2901 }
2902 }
2903
Johannes Berg584991d2009-11-02 13:32:03 +01002904 if (!i) {
2905 err = -EINVAL;
2906 goto out_free;
2907 }
2908
2909 request->n_channels = i;
2910
Johannes Berg2a519312009-02-10 21:25:55 +01002911 i = 0;
2912 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2913 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2914 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2915 err = -EINVAL;
2916 goto out_free;
2917 }
2918 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2919 request->ssids[i].ssid_len = nla_len(attr);
2920 i++;
2921 }
2922 }
2923
Jouni Malinen70692ad2009-02-16 19:39:13 +02002924 if (info->attrs[NL80211_ATTR_IE]) {
2925 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02002926 memcpy((void *)request->ie,
2927 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02002928 request->ie_len);
2929 }
2930
Johannes Berg463d0182009-07-14 00:33:35 +02002931 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02002932 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002933
Johannes Berg79c97e92009-07-07 03:56:12 +02002934 rdev->scan_req = request;
2935 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01002936
Johannes Berg463d0182009-07-14 00:33:35 +02002937 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002938 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02002939 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02002940 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01002941 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02002942 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01002943 kfree(request);
2944 }
Johannes Berg3b858752009-03-12 09:55:09 +01002945
Johannes Berg2a519312009-02-10 21:25:55 +01002946 return err;
2947}
2948
2949static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2950 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02002951 struct wireless_dev *wdev,
2952 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01002953{
Johannes Berg48ab9052009-07-10 18:42:31 +02002954 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01002955 void *hdr;
2956 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02002957 int i;
2958
2959 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01002960
2961 hdr = nl80211hdr_put(msg, pid, seq, flags,
2962 NL80211_CMD_NEW_SCAN_RESULTS);
2963 if (!hdr)
2964 return -1;
2965
Johannes Bergf5ea9122009-08-07 16:17:38 +02002966 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02002967 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01002968
2969 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2970 if (!bss)
2971 goto nla_put_failure;
2972 if (!is_zero_ether_addr(res->bssid))
2973 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2974 if (res->information_elements && res->len_information_elements)
2975 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2976 res->len_information_elements,
2977 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02002978 if (res->beacon_ies && res->len_beacon_ies &&
2979 res->beacon_ies != res->information_elements)
2980 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
2981 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01002982 if (res->tsf)
2983 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2984 if (res->beacon_interval)
2985 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2986 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2987 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02002988 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
2989 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01002990
Johannes Berg77965c92009-02-18 18:45:06 +01002991 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002992 case CFG80211_SIGNAL_TYPE_MBM:
2993 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2994 break;
2995 case CFG80211_SIGNAL_TYPE_UNSPEC:
2996 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2997 break;
2998 default:
2999 break;
3000 }
3001
Johannes Berg48ab9052009-07-10 18:42:31 +02003002 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003003 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003004 case NL80211_IFTYPE_STATION:
3005 if (intbss == wdev->current_bss)
3006 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3007 NL80211_BSS_STATUS_ASSOCIATED);
3008 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3009 if (intbss != wdev->auth_bsses[i])
3010 continue;
3011 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3012 NL80211_BSS_STATUS_AUTHENTICATED);
3013 break;
3014 }
3015 break;
3016 case NL80211_IFTYPE_ADHOC:
3017 if (intbss == wdev->current_bss)
3018 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3019 NL80211_BSS_STATUS_IBSS_JOINED);
3020 break;
3021 default:
3022 break;
3023 }
3024
Johannes Berg2a519312009-02-10 21:25:55 +01003025 nla_nest_end(msg, bss);
3026
3027 return genlmsg_end(msg, hdr);
3028
3029 nla_put_failure:
3030 genlmsg_cancel(msg, hdr);
3031 return -EMSGSIZE;
3032}
3033
3034static int nl80211_dump_scan(struct sk_buff *skb,
3035 struct netlink_callback *cb)
3036{
Johannes Berg48ab9052009-07-10 18:42:31 +02003037 struct cfg80211_registered_device *rdev;
3038 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003039 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003040 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003041 int start = cb->args[1], idx = 0;
3042 int err;
3043
Johannes Berg67748892010-10-04 21:14:06 +02003044 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3045 if (err)
3046 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003047
Johannes Berg48ab9052009-07-10 18:42:31 +02003048 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003049
Johannes Berg48ab9052009-07-10 18:42:31 +02003050 wdev_lock(wdev);
3051 spin_lock_bh(&rdev->bss_lock);
3052 cfg80211_bss_expire(rdev);
3053
3054 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003055 if (++idx <= start)
3056 continue;
3057 if (nl80211_send_bss(skb,
3058 NETLINK_CB(cb->skb).pid,
3059 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003060 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003061 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003062 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003063 }
3064 }
3065
Johannes Berg48ab9052009-07-10 18:42:31 +02003066 spin_unlock_bh(&rdev->bss_lock);
3067 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003068
3069 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003070 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003071
Johannes Berg67748892010-10-04 21:14:06 +02003072 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003073}
3074
Holger Schurig61fa7132009-11-11 12:25:40 +01003075static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3076 int flags, struct net_device *dev,
3077 struct survey_info *survey)
3078{
3079 void *hdr;
3080 struct nlattr *infoattr;
3081
3082 /* Survey without a channel doesn't make sense */
3083 if (!survey->channel)
3084 return -EINVAL;
3085
3086 hdr = nl80211hdr_put(msg, pid, seq, flags,
3087 NL80211_CMD_NEW_SURVEY_RESULTS);
3088 if (!hdr)
3089 return -ENOMEM;
3090
3091 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3092
3093 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3094 if (!infoattr)
3095 goto nla_put_failure;
3096
3097 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3098 survey->channel->center_freq);
3099 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3100 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3101 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003102 if (survey->filled & SURVEY_INFO_IN_USE)
3103 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Holger Schurig61fa7132009-11-11 12:25:40 +01003104
3105 nla_nest_end(msg, infoattr);
3106
3107 return genlmsg_end(msg, hdr);
3108
3109 nla_put_failure:
3110 genlmsg_cancel(msg, hdr);
3111 return -EMSGSIZE;
3112}
3113
3114static int nl80211_dump_survey(struct sk_buff *skb,
3115 struct netlink_callback *cb)
3116{
3117 struct survey_info survey;
3118 struct cfg80211_registered_device *dev;
3119 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003120 int survey_idx = cb->args[1];
3121 int res;
3122
Johannes Berg67748892010-10-04 21:14:06 +02003123 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3124 if (res)
3125 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003126
3127 if (!dev->ops->dump_survey) {
3128 res = -EOPNOTSUPP;
3129 goto out_err;
3130 }
3131
3132 while (1) {
3133 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3134 &survey);
3135 if (res == -ENOENT)
3136 break;
3137 if (res)
3138 goto out_err;
3139
3140 if (nl80211_send_survey(skb,
3141 NETLINK_CB(cb->skb).pid,
3142 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3143 netdev,
3144 &survey) < 0)
3145 goto out;
3146 survey_idx++;
3147 }
3148
3149 out:
3150 cb->args[1] = survey_idx;
3151 res = skb->len;
3152 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003153 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003154 return res;
3155}
3156
Jouni Malinen255e7372009-03-20 21:21:17 +02003157static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3158{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003159 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003160}
3161
Samuel Ortizb23aa672009-07-01 21:26:54 +02003162static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3163{
3164 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3165 NL80211_WPA_VERSION_2));
3166}
3167
3168static bool nl80211_valid_akm_suite(u32 akm)
3169{
3170 return akm == WLAN_AKM_SUITE_8021X ||
3171 akm == WLAN_AKM_SUITE_PSK;
3172}
3173
3174static bool nl80211_valid_cipher_suite(u32 cipher)
3175{
3176 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3177 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3178 cipher == WLAN_CIPHER_SUITE_TKIP ||
3179 cipher == WLAN_CIPHER_SUITE_CCMP ||
3180 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3181}
3182
3183
Jouni Malinen636a5d32009-03-19 13:39:22 +02003184static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3185{
Johannes Berg4c476992010-10-04 21:36:35 +02003186 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3187 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003188 struct ieee80211_channel *chan;
3189 const u8 *bssid, *ssid, *ie = NULL;
3190 int err, ssid_len, ie_len = 0;
3191 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003192 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003193 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003194
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003195 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3196 return -EINVAL;
3197
3198 if (!info->attrs[NL80211_ATTR_MAC])
3199 return -EINVAL;
3200
Jouni Malinen17780922009-03-27 20:52:47 +02003201 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3202 return -EINVAL;
3203
Johannes Berg19957bb2009-07-02 17:20:43 +02003204 if (!info->attrs[NL80211_ATTR_SSID])
3205 return -EINVAL;
3206
3207 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3208 return -EINVAL;
3209
Johannes Bergfffd0932009-07-08 14:22:54 +02003210 err = nl80211_parse_key(info, &key);
3211 if (err)
3212 return err;
3213
3214 if (key.idx >= 0) {
3215 if (!key.p.key || !key.p.key_len)
3216 return -EINVAL;
3217 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3218 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3219 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3220 key.p.key_len != WLAN_KEY_LEN_WEP104))
3221 return -EINVAL;
3222 if (key.idx > 4)
3223 return -EINVAL;
3224 } else {
3225 key.p.key_len = 0;
3226 key.p.key = NULL;
3227 }
3228
Johannes Bergafea0b72010-08-10 09:46:42 +02003229 if (key.idx >= 0) {
3230 int i;
3231 bool ok = false;
3232 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3233 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3234 ok = true;
3235 break;
3236 }
3237 }
Johannes Berg4c476992010-10-04 21:36:35 +02003238 if (!ok)
3239 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003240 }
3241
Johannes Berg4c476992010-10-04 21:36:35 +02003242 if (!rdev->ops->auth)
3243 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003244
Johannes Berg074ac8d2010-09-16 14:58:22 +02003245 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003246 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3247 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003248
Johannes Berg19957bb2009-07-02 17:20:43 +02003249 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003250 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003251 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003252 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3253 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003254
Johannes Berg19957bb2009-07-02 17:20:43 +02003255 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3256 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3257
3258 if (info->attrs[NL80211_ATTR_IE]) {
3259 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3260 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3261 }
3262
3263 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003264 if (!nl80211_valid_auth_type(auth_type))
3265 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003266
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003267 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3268
Johannes Berg4c476992010-10-04 21:36:35 +02003269 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3270 ssid, ssid_len, ie, ie_len,
3271 key.p.key, key.p.key_len, key.idx,
3272 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003273}
3274
Johannes Bergc0692b82010-08-27 14:26:53 +03003275static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3276 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003277 struct cfg80211_crypto_settings *settings,
3278 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003279{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003280 memset(settings, 0, sizeof(*settings));
3281
Samuel Ortizb23aa672009-07-01 21:26:54 +02003282 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3283
Johannes Bergc0692b82010-08-27 14:26:53 +03003284 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3285 u16 proto;
3286 proto = nla_get_u16(
3287 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3288 settings->control_port_ethertype = cpu_to_be16(proto);
3289 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3290 proto != ETH_P_PAE)
3291 return -EINVAL;
3292 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3293 settings->control_port_no_encrypt = true;
3294 } else
3295 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3296
Samuel Ortizb23aa672009-07-01 21:26:54 +02003297 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3298 void *data;
3299 int len, i;
3300
3301 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3302 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3303 settings->n_ciphers_pairwise = len / sizeof(u32);
3304
3305 if (len % sizeof(u32))
3306 return -EINVAL;
3307
Johannes Berg3dc27d22009-07-02 21:36:37 +02003308 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003309 return -EINVAL;
3310
3311 memcpy(settings->ciphers_pairwise, data, len);
3312
3313 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3314 if (!nl80211_valid_cipher_suite(
3315 settings->ciphers_pairwise[i]))
3316 return -EINVAL;
3317 }
3318
3319 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3320 settings->cipher_group =
3321 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3322 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3323 return -EINVAL;
3324 }
3325
3326 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3327 settings->wpa_versions =
3328 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3329 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3330 return -EINVAL;
3331 }
3332
3333 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3334 void *data;
3335 int len, i;
3336
3337 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3338 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3339 settings->n_akm_suites = len / sizeof(u32);
3340
3341 if (len % sizeof(u32))
3342 return -EINVAL;
3343
3344 memcpy(settings->akm_suites, data, len);
3345
3346 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3347 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3348 return -EINVAL;
3349 }
3350
3351 return 0;
3352}
3353
Jouni Malinen636a5d32009-03-19 13:39:22 +02003354static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3355{
Johannes Berg4c476992010-10-04 21:36:35 +02003356 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3357 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003358 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003359 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003360 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003361 int err, ssid_len, ie_len = 0;
3362 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003363
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003364 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3365 return -EINVAL;
3366
3367 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003368 !info->attrs[NL80211_ATTR_SSID] ||
3369 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003370 return -EINVAL;
3371
Johannes Berg4c476992010-10-04 21:36:35 +02003372 if (!rdev->ops->assoc)
3373 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003374
Johannes Berg074ac8d2010-09-16 14:58:22 +02003375 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003376 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3377 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003378
Johannes Berg19957bb2009-07-02 17:20:43 +02003379 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003380
Johannes Berg19957bb2009-07-02 17:20:43 +02003381 chan = ieee80211_get_channel(&rdev->wiphy,
3382 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003383 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3384 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003385
Johannes Berg19957bb2009-07-02 17:20:43 +02003386 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3387 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003388
3389 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003390 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3391 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003392 }
3393
Jouni Malinendc6382c2009-05-06 22:09:37 +03003394 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003395 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03003396 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003397 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003398 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02003399 else if (mfp != NL80211_MFP_NO)
3400 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03003401 }
3402
Johannes Berg3e5d7642009-07-07 14:37:26 +02003403 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3404 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3405
Johannes Bergc0692b82010-08-27 14:26:53 +03003406 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003407 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003408 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3409 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003410 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003411
Jouni Malinen636a5d32009-03-19 13:39:22 +02003412 return err;
3413}
3414
3415static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3416{
Johannes Berg4c476992010-10-04 21:36:35 +02003417 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3418 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003419 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003420 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003421 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003422 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003423
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003424 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3425 return -EINVAL;
3426
3427 if (!info->attrs[NL80211_ATTR_MAC])
3428 return -EINVAL;
3429
3430 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3431 return -EINVAL;
3432
Johannes Berg4c476992010-10-04 21:36:35 +02003433 if (!rdev->ops->deauth)
3434 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003435
Johannes Berg074ac8d2010-09-16 14:58:22 +02003436 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003437 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3438 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003439
Johannes Berg19957bb2009-07-02 17:20:43 +02003440 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003441
Johannes Berg19957bb2009-07-02 17:20:43 +02003442 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3443 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003444 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003445 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003446 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003447
3448 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003449 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3450 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003451 }
3452
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003453 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3454
Johannes Berg4c476992010-10-04 21:36:35 +02003455 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3456 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003457}
3458
3459static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3460{
Johannes Berg4c476992010-10-04 21:36:35 +02003461 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3462 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003463 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003464 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003465 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003466 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003467
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003468 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3469 return -EINVAL;
3470
3471 if (!info->attrs[NL80211_ATTR_MAC])
3472 return -EINVAL;
3473
3474 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3475 return -EINVAL;
3476
Johannes Berg4c476992010-10-04 21:36:35 +02003477 if (!rdev->ops->disassoc)
3478 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003479
Johannes Berg074ac8d2010-09-16 14:58:22 +02003480 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003481 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3482 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003483
Johannes Berg19957bb2009-07-02 17:20:43 +02003484 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003485
Johannes Berg19957bb2009-07-02 17:20:43 +02003486 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3487 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003488 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003489 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003490 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003491
3492 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003493 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3494 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003495 }
3496
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003497 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3498
Johannes Berg4c476992010-10-04 21:36:35 +02003499 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3500 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003501}
3502
Johannes Berg04a773a2009-04-19 21:24:32 +02003503static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3504{
Johannes Berg4c476992010-10-04 21:36:35 +02003505 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3506 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003507 struct cfg80211_ibss_params ibss;
3508 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003509 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003510 int err;
3511
Johannes Berg8e30bc52009-04-22 17:45:38 +02003512 memset(&ibss, 0, sizeof(ibss));
3513
Johannes Berg04a773a2009-04-19 21:24:32 +02003514 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3515 return -EINVAL;
3516
3517 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3518 !info->attrs[NL80211_ATTR_SSID] ||
3519 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3520 return -EINVAL;
3521
Johannes Berg8e30bc52009-04-22 17:45:38 +02003522 ibss.beacon_interval = 100;
3523
3524 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3525 ibss.beacon_interval =
3526 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3527 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3528 return -EINVAL;
3529 }
3530
Johannes Berg4c476992010-10-04 21:36:35 +02003531 if (!rdev->ops->join_ibss)
3532 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003533
Johannes Berg4c476992010-10-04 21:36:35 +02003534 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3535 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003536
Johannes Berg79c97e92009-07-07 03:56:12 +02003537 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003538
3539 if (info->attrs[NL80211_ATTR_MAC])
3540 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3541 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3542 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3543
3544 if (info->attrs[NL80211_ATTR_IE]) {
3545 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3546 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3547 }
3548
3549 ibss.channel = ieee80211_get_channel(wiphy,
3550 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3551 if (!ibss.channel ||
3552 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02003553 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3554 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003555
3556 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003557 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003558
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003559 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3560 u8 *rates =
3561 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3562 int n_rates =
3563 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3564 struct ieee80211_supported_band *sband =
3565 wiphy->bands[ibss.channel->band];
3566 int i, j;
3567
Johannes Berg4c476992010-10-04 21:36:35 +02003568 if (n_rates == 0)
3569 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003570
3571 for (i = 0; i < n_rates; i++) {
3572 int rate = (rates[i] & 0x7f) * 5;
3573 bool found = false;
3574
3575 for (j = 0; j < sband->n_bitrates; j++) {
3576 if (sband->bitrates[j].bitrate == rate) {
3577 found = true;
3578 ibss.basic_rates |= BIT(j);
3579 break;
3580 }
3581 }
Johannes Berg4c476992010-10-04 21:36:35 +02003582 if (!found)
3583 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003584 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003585 }
3586
Johannes Berg4c476992010-10-04 21:36:35 +02003587 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3588 connkeys = nl80211_parse_connkeys(rdev,
3589 info->attrs[NL80211_ATTR_KEYS]);
3590 if (IS_ERR(connkeys))
3591 return PTR_ERR(connkeys);
3592 }
Johannes Berg04a773a2009-04-19 21:24:32 +02003593
Johannes Berg4c476992010-10-04 21:36:35 +02003594 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003595 if (err)
3596 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003597 return err;
3598}
3599
3600static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3601{
Johannes Berg4c476992010-10-04 21:36:35 +02003602 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3603 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003604
Johannes Berg4c476992010-10-04 21:36:35 +02003605 if (!rdev->ops->leave_ibss)
3606 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003607
Johannes Berg4c476992010-10-04 21:36:35 +02003608 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3609 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003610
Johannes Berg4c476992010-10-04 21:36:35 +02003611 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003612}
3613
Johannes Bergaff89a92009-07-01 21:26:51 +02003614#ifdef CONFIG_NL80211_TESTMODE
3615static struct genl_multicast_group nl80211_testmode_mcgrp = {
3616 .name = "testmode",
3617};
3618
3619static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3620{
Johannes Berg4c476992010-10-04 21:36:35 +02003621 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02003622 int err;
3623
3624 if (!info->attrs[NL80211_ATTR_TESTDATA])
3625 return -EINVAL;
3626
Johannes Bergaff89a92009-07-01 21:26:51 +02003627 err = -EOPNOTSUPP;
3628 if (rdev->ops->testmode_cmd) {
3629 rdev->testmode_info = info;
3630 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3631 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3632 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3633 rdev->testmode_info = NULL;
3634 }
3635
Johannes Bergaff89a92009-07-01 21:26:51 +02003636 return err;
3637}
3638
3639static struct sk_buff *
3640__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3641 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3642{
3643 struct sk_buff *skb;
3644 void *hdr;
3645 struct nlattr *data;
3646
3647 skb = nlmsg_new(approxlen + 100, gfp);
3648 if (!skb)
3649 return NULL;
3650
3651 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3652 if (!hdr) {
3653 kfree_skb(skb);
3654 return NULL;
3655 }
3656
3657 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3658 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3659
3660 ((void **)skb->cb)[0] = rdev;
3661 ((void **)skb->cb)[1] = hdr;
3662 ((void **)skb->cb)[2] = data;
3663
3664 return skb;
3665
3666 nla_put_failure:
3667 kfree_skb(skb);
3668 return NULL;
3669}
3670
3671struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3672 int approxlen)
3673{
3674 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3675
3676 if (WARN_ON(!rdev->testmode_info))
3677 return NULL;
3678
3679 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3680 rdev->testmode_info->snd_pid,
3681 rdev->testmode_info->snd_seq,
3682 GFP_KERNEL);
3683}
3684EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3685
3686int cfg80211_testmode_reply(struct sk_buff *skb)
3687{
3688 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3689 void *hdr = ((void **)skb->cb)[1];
3690 struct nlattr *data = ((void **)skb->cb)[2];
3691
3692 if (WARN_ON(!rdev->testmode_info)) {
3693 kfree_skb(skb);
3694 return -EINVAL;
3695 }
3696
3697 nla_nest_end(skb, data);
3698 genlmsg_end(skb, hdr);
3699 return genlmsg_reply(skb, rdev->testmode_info);
3700}
3701EXPORT_SYMBOL(cfg80211_testmode_reply);
3702
3703struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3704 int approxlen, gfp_t gfp)
3705{
3706 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3707
3708 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3709}
3710EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3711
3712void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3713{
3714 void *hdr = ((void **)skb->cb)[1];
3715 struct nlattr *data = ((void **)skb->cb)[2];
3716
3717 nla_nest_end(skb, data);
3718 genlmsg_end(skb, hdr);
3719 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3720}
3721EXPORT_SYMBOL(cfg80211_testmode_event);
3722#endif
3723
Samuel Ortizb23aa672009-07-01 21:26:54 +02003724static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3725{
Johannes Berg4c476992010-10-04 21:36:35 +02003726 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3727 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003728 struct cfg80211_connect_params connect;
3729 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003730 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003731 int err;
3732
3733 memset(&connect, 0, sizeof(connect));
3734
3735 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3736 return -EINVAL;
3737
3738 if (!info->attrs[NL80211_ATTR_SSID] ||
3739 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3740 return -EINVAL;
3741
3742 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3743 connect.auth_type =
3744 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3745 if (!nl80211_valid_auth_type(connect.auth_type))
3746 return -EINVAL;
3747 } else
3748 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3749
3750 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3751
Johannes Bergc0692b82010-08-27 14:26:53 +03003752 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003753 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003754 if (err)
3755 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003756
Johannes Berg074ac8d2010-09-16 14:58:22 +02003757 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003758 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3759 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003760
Johannes Berg79c97e92009-07-07 03:56:12 +02003761 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003762
Samuel Ortizb23aa672009-07-01 21:26:54 +02003763 if (info->attrs[NL80211_ATTR_MAC])
3764 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3765 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3766 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3767
3768 if (info->attrs[NL80211_ATTR_IE]) {
3769 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3770 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3771 }
3772
3773 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3774 connect.channel =
3775 ieee80211_get_channel(wiphy,
3776 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3777 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02003778 connect.channel->flags & IEEE80211_CHAN_DISABLED)
3779 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003780 }
3781
Johannes Bergfffd0932009-07-08 14:22:54 +02003782 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3783 connkeys = nl80211_parse_connkeys(rdev,
3784 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02003785 if (IS_ERR(connkeys))
3786 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003787 }
3788
3789 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003790 if (err)
3791 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003792 return err;
3793}
3794
3795static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3796{
Johannes Berg4c476992010-10-04 21:36:35 +02003797 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3798 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003799 u16 reason;
3800
3801 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3802 reason = WLAN_REASON_DEAUTH_LEAVING;
3803 else
3804 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3805
3806 if (reason == 0)
3807 return -EINVAL;
3808
Johannes Berg074ac8d2010-09-16 14:58:22 +02003809 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003810 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3811 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003812
Johannes Berg4c476992010-10-04 21:36:35 +02003813 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003814}
3815
Johannes Berg463d0182009-07-14 00:33:35 +02003816static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
3817{
Johannes Berg4c476992010-10-04 21:36:35 +02003818 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02003819 struct net *net;
3820 int err;
3821 u32 pid;
3822
3823 if (!info->attrs[NL80211_ATTR_PID])
3824 return -EINVAL;
3825
3826 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
3827
Johannes Berg463d0182009-07-14 00:33:35 +02003828 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02003829 if (IS_ERR(net))
3830 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02003831
3832 err = 0;
3833
3834 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02003835 if (!net_eq(wiphy_net(&rdev->wiphy), net))
3836 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02003837
Johannes Berg463d0182009-07-14 00:33:35 +02003838 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02003839 return err;
3840}
3841
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003842static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
3843{
Johannes Berg4c476992010-10-04 21:36:35 +02003844 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003845 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
3846 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003847 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003848 struct cfg80211_pmksa pmksa;
3849
3850 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
3851
3852 if (!info->attrs[NL80211_ATTR_MAC])
3853 return -EINVAL;
3854
3855 if (!info->attrs[NL80211_ATTR_PMKID])
3856 return -EINVAL;
3857
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003858 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
3859 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3860
Johannes Berg074ac8d2010-09-16 14:58:22 +02003861 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003862 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3863 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003864
3865 switch (info->genlhdr->cmd) {
3866 case NL80211_CMD_SET_PMKSA:
3867 rdev_ops = rdev->ops->set_pmksa;
3868 break;
3869 case NL80211_CMD_DEL_PMKSA:
3870 rdev_ops = rdev->ops->del_pmksa;
3871 break;
3872 default:
3873 WARN_ON(1);
3874 break;
3875 }
3876
Johannes Berg4c476992010-10-04 21:36:35 +02003877 if (!rdev_ops)
3878 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003879
Johannes Berg4c476992010-10-04 21:36:35 +02003880 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003881}
3882
3883static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
3884{
Johannes Berg4c476992010-10-04 21:36:35 +02003885 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3886 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003887
Johannes Berg074ac8d2010-09-16 14:58:22 +02003888 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003889 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3890 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003891
Johannes Berg4c476992010-10-04 21:36:35 +02003892 if (!rdev->ops->flush_pmksa)
3893 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003894
Johannes Berg4c476992010-10-04 21:36:35 +02003895 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003896}
3897
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003898static int nl80211_remain_on_channel(struct sk_buff *skb,
3899 struct genl_info *info)
3900{
Johannes Berg4c476992010-10-04 21:36:35 +02003901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3902 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003903 struct ieee80211_channel *chan;
3904 struct sk_buff *msg;
3905 void *hdr;
3906 u64 cookie;
3907 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
3908 u32 freq, duration;
3909 int err;
3910
3911 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3912 !info->attrs[NL80211_ATTR_DURATION])
3913 return -EINVAL;
3914
3915 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
3916
3917 /*
3918 * We should be on that channel for at least one jiffie,
3919 * and more than 5 seconds seems excessive.
3920 */
3921 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
3922 return -EINVAL;
3923
Johannes Berg4c476992010-10-04 21:36:35 +02003924 if (!rdev->ops->remain_on_channel)
3925 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003926
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003927 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3928 channel_type = nla_get_u32(
3929 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3930 if (channel_type != NL80211_CHAN_NO_HT &&
3931 channel_type != NL80211_CHAN_HT20 &&
3932 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02003933 channel_type != NL80211_CHAN_HT40MINUS)
3934 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003935 }
3936
3937 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
3938 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02003939 if (chan == NULL)
3940 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003941
3942 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003943 if (!msg)
3944 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003945
3946 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3947 NL80211_CMD_REMAIN_ON_CHANNEL);
3948
3949 if (IS_ERR(hdr)) {
3950 err = PTR_ERR(hdr);
3951 goto free_msg;
3952 }
3953
3954 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
3955 channel_type, duration, &cookie);
3956
3957 if (err)
3958 goto free_msg;
3959
3960 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
3961
3962 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003963
3964 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003965
3966 nla_put_failure:
3967 err = -ENOBUFS;
3968 free_msg:
3969 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003970 return err;
3971}
3972
3973static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
3974 struct genl_info *info)
3975{
Johannes Berg4c476992010-10-04 21:36:35 +02003976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3977 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003978 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003979
3980 if (!info->attrs[NL80211_ATTR_COOKIE])
3981 return -EINVAL;
3982
Johannes Berg4c476992010-10-04 21:36:35 +02003983 if (!rdev->ops->cancel_remain_on_channel)
3984 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003985
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003986 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
3987
Johannes Berg4c476992010-10-04 21:36:35 +02003988 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003989}
3990
Jouni Malinen13ae75b2009-12-29 12:59:45 +02003991static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
3992 u8 *rates, u8 rates_len)
3993{
3994 u8 i;
3995 u32 mask = 0;
3996
3997 for (i = 0; i < rates_len; i++) {
3998 int rate = (rates[i] & 0x7f) * 5;
3999 int ridx;
4000 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4001 struct ieee80211_rate *srate =
4002 &sband->bitrates[ridx];
4003 if (rate == srate->bitrate) {
4004 mask |= 1 << ridx;
4005 break;
4006 }
4007 }
4008 if (ridx == sband->n_bitrates)
4009 return 0; /* rate not found */
4010 }
4011
4012 return mask;
4013}
4014
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004015static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004016 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4017 .len = NL80211_MAX_SUPP_RATES },
4018};
4019
4020static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4021 struct genl_info *info)
4022{
4023 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004024 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004025 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004026 int rem, i;
4027 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004028 struct nlattr *tx_rates;
4029 struct ieee80211_supported_band *sband;
4030
4031 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4032 return -EINVAL;
4033
Johannes Berg4c476992010-10-04 21:36:35 +02004034 if (!rdev->ops->set_bitrate_mask)
4035 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004036
4037 memset(&mask, 0, sizeof(mask));
4038 /* Default to all rates enabled */
4039 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4040 sband = rdev->wiphy.bands[i];
4041 mask.control[i].legacy =
4042 sband ? (1 << sband->n_bitrates) - 1 : 0;
4043 }
4044
4045 /*
4046 * The nested attribute uses enum nl80211_band as the index. This maps
4047 * directly to the enum ieee80211_band values used in cfg80211.
4048 */
4049 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4050 {
4051 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004052 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4053 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004054 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004055 if (sband == NULL)
4056 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004057 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4058 nla_len(tx_rates), nl80211_txattr_policy);
4059 if (tb[NL80211_TXRATE_LEGACY]) {
4060 mask.control[band].legacy = rateset_to_mask(
4061 sband,
4062 nla_data(tb[NL80211_TXRATE_LEGACY]),
4063 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004064 if (mask.control[band].legacy == 0)
4065 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004066 }
4067 }
4068
Johannes Berg4c476992010-10-04 21:36:35 +02004069 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004070}
4071
Johannes Berg2e161f72010-08-12 15:38:38 +02004072static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004073{
Johannes Berg4c476992010-10-04 21:36:35 +02004074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4075 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004076 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004077
4078 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4079 return -EINVAL;
4080
Johannes Berg2e161f72010-08-12 15:38:38 +02004081 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4082 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004083
Johannes Berg9d38d852010-06-09 17:20:33 +02004084 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004085 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004086 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4087 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4088 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004089 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4090 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004091
4092 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004093 if (!rdev->ops->mgmt_tx)
4094 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004095
Johannes Berg4c476992010-10-04 21:36:35 +02004096 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004097 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004098 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4099 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004100}
4101
Johannes Berg2e161f72010-08-12 15:38:38 +02004102static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004103{
Johannes Berg4c476992010-10-04 21:36:35 +02004104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4105 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004106 struct ieee80211_channel *chan;
4107 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004108 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004109 u32 freq;
4110 int err;
4111 void *hdr;
4112 u64 cookie;
4113 struct sk_buff *msg;
4114
4115 if (!info->attrs[NL80211_ATTR_FRAME] ||
4116 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4117 return -EINVAL;
4118
Johannes Berg4c476992010-10-04 21:36:35 +02004119 if (!rdev->ops->mgmt_tx)
4120 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004121
Johannes Berg9d38d852010-06-09 17:20:33 +02004122 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004123 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004124 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4125 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4126 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004127 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4128 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004129
Jouni Malinen026331c2010-02-15 12:53:10 +02004130 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4131 channel_type = nla_get_u32(
4132 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4133 if (channel_type != NL80211_CHAN_NO_HT &&
4134 channel_type != NL80211_CHAN_HT20 &&
4135 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004136 channel_type != NL80211_CHAN_HT40MINUS)
4137 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004138 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004139 }
4140
4141 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4142 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004143 if (chan == NULL)
4144 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004145
4146 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004147 if (!msg)
4148 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004149
4150 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004151 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004152
4153 if (IS_ERR(hdr)) {
4154 err = PTR_ERR(hdr);
4155 goto free_msg;
4156 }
Johannes Berg2e161f72010-08-12 15:38:38 +02004157 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, channel_type,
4158 channel_type_valid,
4159 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4160 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4161 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004162 if (err)
4163 goto free_msg;
4164
4165 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4166
4167 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004168 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004169
4170 nla_put_failure:
4171 err = -ENOBUFS;
4172 free_msg:
4173 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004174 return err;
4175}
4176
Kalle Valoffb9eb32010-02-17 17:58:10 +02004177static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4178{
Johannes Berg4c476992010-10-04 21:36:35 +02004179 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004180 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004181 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004182 u8 ps_state;
4183 bool state;
4184 int err;
4185
Johannes Berg4c476992010-10-04 21:36:35 +02004186 if (!info->attrs[NL80211_ATTR_PS_STATE])
4187 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004188
4189 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4190
Johannes Berg4c476992010-10-04 21:36:35 +02004191 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4192 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004193
4194 wdev = dev->ieee80211_ptr;
4195
Johannes Berg4c476992010-10-04 21:36:35 +02004196 if (!rdev->ops->set_power_mgmt)
4197 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004198
4199 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4200
4201 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004202 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004203
Johannes Berg4c476992010-10-04 21:36:35 +02004204 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4205 wdev->ps_timeout);
4206 if (!err)
4207 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004208 return err;
4209}
4210
4211static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4212{
Johannes Berg4c476992010-10-04 21:36:35 +02004213 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004214 enum nl80211_ps_state ps_state;
4215 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004216 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004217 struct sk_buff *msg;
4218 void *hdr;
4219 int err;
4220
Kalle Valoffb9eb32010-02-17 17:58:10 +02004221 wdev = dev->ieee80211_ptr;
4222
Johannes Berg4c476992010-10-04 21:36:35 +02004223 if (!rdev->ops->set_power_mgmt)
4224 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004225
4226 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004227 if (!msg)
4228 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004229
4230 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4231 NL80211_CMD_GET_POWER_SAVE);
4232 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004233 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004234 goto free_msg;
4235 }
4236
4237 if (wdev->ps)
4238 ps_state = NL80211_PS_ENABLED;
4239 else
4240 ps_state = NL80211_PS_DISABLED;
4241
4242 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4243
4244 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004245 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004246
Johannes Berg4c476992010-10-04 21:36:35 +02004247 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004248 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004249 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004250 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004251 return err;
4252}
4253
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004254static struct nla_policy
4255nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4256 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4257 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4258 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4259};
4260
4261static int nl80211_set_cqm_rssi(struct genl_info *info,
4262 s32 threshold, u32 hysteresis)
4263{
Johannes Berg4c476992010-10-04 21:36:35 +02004264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004265 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004266 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004267
4268 if (threshold > 0)
4269 return -EINVAL;
4270
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004271 wdev = dev->ieee80211_ptr;
4272
Johannes Berg4c476992010-10-04 21:36:35 +02004273 if (!rdev->ops->set_cqm_rssi_config)
4274 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004275
Johannes Berg074ac8d2010-09-16 14:58:22 +02004276 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004277 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4278 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004279
Johannes Berg4c476992010-10-04 21:36:35 +02004280 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4281 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004282}
4283
4284static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4285{
4286 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4287 struct nlattr *cqm;
4288 int err;
4289
4290 cqm = info->attrs[NL80211_ATTR_CQM];
4291 if (!cqm) {
4292 err = -EINVAL;
4293 goto out;
4294 }
4295
4296 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4297 nl80211_attr_cqm_policy);
4298 if (err)
4299 goto out;
4300
4301 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4302 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4303 s32 threshold;
4304 u32 hysteresis;
4305 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4306 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4307 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4308 } else
4309 err = -EINVAL;
4310
4311out:
4312 return err;
4313}
4314
Johannes Berg4c476992010-10-04 21:36:35 +02004315#define NL80211_FLAG_NEED_WIPHY 0x01
4316#define NL80211_FLAG_NEED_NETDEV 0x02
4317#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02004318#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
4319#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
4320 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02004321
4322static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4323 struct genl_info *info)
4324{
4325 struct cfg80211_registered_device *rdev;
4326 struct net_device *dev;
4327 int err;
4328 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4329
4330 if (rtnl)
4331 rtnl_lock();
4332
4333 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4334 rdev = cfg80211_get_dev_from_info(info);
4335 if (IS_ERR(rdev)) {
4336 if (rtnl)
4337 rtnl_unlock();
4338 return PTR_ERR(rdev);
4339 }
4340 info->user_ptr[0] = rdev;
4341 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4342 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4343 if (err) {
4344 if (rtnl)
4345 rtnl_unlock();
4346 return err;
4347 }
Johannes Berg41265712010-10-04 21:14:05 +02004348 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4349 !netif_running(dev)) {
4350 if (rtnl)
4351 rtnl_unlock();
4352 return -ENETDOWN;
4353 }
Johannes Berg4c476992010-10-04 21:36:35 +02004354 info->user_ptr[0] = rdev;
4355 info->user_ptr[1] = dev;
4356 }
4357
4358 return 0;
4359}
4360
4361static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4362 struct genl_info *info)
4363{
4364 if (info->user_ptr[0])
4365 cfg80211_unlock_rdev(info->user_ptr[0]);
4366 if (info->user_ptr[1])
4367 dev_put(info->user_ptr[1]);
4368 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4369 rtnl_unlock();
4370}
4371
Johannes Berg55682962007-09-20 13:09:35 -04004372static struct genl_ops nl80211_ops[] = {
4373 {
4374 .cmd = NL80211_CMD_GET_WIPHY,
4375 .doit = nl80211_get_wiphy,
4376 .dumpit = nl80211_dump_wiphy,
4377 .policy = nl80211_policy,
4378 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004379 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04004380 },
4381 {
4382 .cmd = NL80211_CMD_SET_WIPHY,
4383 .doit = nl80211_set_wiphy,
4384 .policy = nl80211_policy,
4385 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004386 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004387 },
4388 {
4389 .cmd = NL80211_CMD_GET_INTERFACE,
4390 .doit = nl80211_get_interface,
4391 .dumpit = nl80211_dump_interface,
4392 .policy = nl80211_policy,
4393 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004394 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04004395 },
4396 {
4397 .cmd = NL80211_CMD_SET_INTERFACE,
4398 .doit = nl80211_set_interface,
4399 .policy = nl80211_policy,
4400 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004401 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4402 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004403 },
4404 {
4405 .cmd = NL80211_CMD_NEW_INTERFACE,
4406 .doit = nl80211_new_interface,
4407 .policy = nl80211_policy,
4408 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004409 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4410 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004411 },
4412 {
4413 .cmd = NL80211_CMD_DEL_INTERFACE,
4414 .doit = nl80211_del_interface,
4415 .policy = nl80211_policy,
4416 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004417 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4418 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004419 },
Johannes Berg41ade002007-12-19 02:03:29 +01004420 {
4421 .cmd = NL80211_CMD_GET_KEY,
4422 .doit = nl80211_get_key,
4423 .policy = nl80211_policy,
4424 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004425 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4426 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004427 },
4428 {
4429 .cmd = NL80211_CMD_SET_KEY,
4430 .doit = nl80211_set_key,
4431 .policy = nl80211_policy,
4432 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004433 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004434 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004435 },
4436 {
4437 .cmd = NL80211_CMD_NEW_KEY,
4438 .doit = nl80211_new_key,
4439 .policy = nl80211_policy,
4440 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004441 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004442 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004443 },
4444 {
4445 .cmd = NL80211_CMD_DEL_KEY,
4446 .doit = nl80211_del_key,
4447 .policy = nl80211_policy,
4448 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004449 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004450 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004451 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004452 {
4453 .cmd = NL80211_CMD_SET_BEACON,
4454 .policy = nl80211_policy,
4455 .flags = GENL_ADMIN_PERM,
4456 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004457 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4458 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004459 },
4460 {
4461 .cmd = NL80211_CMD_NEW_BEACON,
4462 .policy = nl80211_policy,
4463 .flags = GENL_ADMIN_PERM,
4464 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004465 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4466 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004467 },
4468 {
4469 .cmd = NL80211_CMD_DEL_BEACON,
4470 .policy = nl80211_policy,
4471 .flags = GENL_ADMIN_PERM,
4472 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004473 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4474 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004475 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004476 {
4477 .cmd = NL80211_CMD_GET_STATION,
4478 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004479 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004480 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02004481 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4482 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004483 },
4484 {
4485 .cmd = NL80211_CMD_SET_STATION,
4486 .doit = nl80211_set_station,
4487 .policy = nl80211_policy,
4488 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004489 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4490 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004491 },
4492 {
4493 .cmd = NL80211_CMD_NEW_STATION,
4494 .doit = nl80211_new_station,
4495 .policy = nl80211_policy,
4496 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004497 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004498 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004499 },
4500 {
4501 .cmd = NL80211_CMD_DEL_STATION,
4502 .doit = nl80211_del_station,
4503 .policy = nl80211_policy,
4504 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004505 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4506 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004507 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004508 {
4509 .cmd = NL80211_CMD_GET_MPATH,
4510 .doit = nl80211_get_mpath,
4511 .dumpit = nl80211_dump_mpath,
4512 .policy = nl80211_policy,
4513 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004514 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004515 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004516 },
4517 {
4518 .cmd = NL80211_CMD_SET_MPATH,
4519 .doit = nl80211_set_mpath,
4520 .policy = nl80211_policy,
4521 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004522 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004523 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004524 },
4525 {
4526 .cmd = NL80211_CMD_NEW_MPATH,
4527 .doit = nl80211_new_mpath,
4528 .policy = nl80211_policy,
4529 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004530 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004531 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004532 },
4533 {
4534 .cmd = NL80211_CMD_DEL_MPATH,
4535 .doit = nl80211_del_mpath,
4536 .policy = nl80211_policy,
4537 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004538 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4539 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004540 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004541 {
4542 .cmd = NL80211_CMD_SET_BSS,
4543 .doit = nl80211_set_bss,
4544 .policy = nl80211_policy,
4545 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004546 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4547 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004548 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004549 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004550 .cmd = NL80211_CMD_GET_REG,
4551 .doit = nl80211_get_reg,
4552 .policy = nl80211_policy,
4553 /* can be retrieved by unprivileged users */
4554 },
4555 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004556 .cmd = NL80211_CMD_SET_REG,
4557 .doit = nl80211_set_reg,
4558 .policy = nl80211_policy,
4559 .flags = GENL_ADMIN_PERM,
4560 },
4561 {
4562 .cmd = NL80211_CMD_REQ_SET_REG,
4563 .doit = nl80211_req_set_reg,
4564 .policy = nl80211_policy,
4565 .flags = GENL_ADMIN_PERM,
4566 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004567 {
4568 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4569 .doit = nl80211_get_mesh_params,
4570 .policy = nl80211_policy,
4571 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004572 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4573 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004574 },
4575 {
4576 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4577 .doit = nl80211_set_mesh_params,
4578 .policy = nl80211_policy,
4579 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004580 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4581 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004582 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004583 {
Johannes Berg2a519312009-02-10 21:25:55 +01004584 .cmd = NL80211_CMD_TRIGGER_SCAN,
4585 .doit = nl80211_trigger_scan,
4586 .policy = nl80211_policy,
4587 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004588 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004589 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01004590 },
4591 {
4592 .cmd = NL80211_CMD_GET_SCAN,
4593 .policy = nl80211_policy,
4594 .dumpit = nl80211_dump_scan,
4595 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004596 {
4597 .cmd = NL80211_CMD_AUTHENTICATE,
4598 .doit = nl80211_authenticate,
4599 .policy = nl80211_policy,
4600 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004601 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004602 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004603 },
4604 {
4605 .cmd = NL80211_CMD_ASSOCIATE,
4606 .doit = nl80211_associate,
4607 .policy = nl80211_policy,
4608 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004609 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004610 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004611 },
4612 {
4613 .cmd = NL80211_CMD_DEAUTHENTICATE,
4614 .doit = nl80211_deauthenticate,
4615 .policy = nl80211_policy,
4616 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004617 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004618 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004619 },
4620 {
4621 .cmd = NL80211_CMD_DISASSOCIATE,
4622 .doit = nl80211_disassociate,
4623 .policy = nl80211_policy,
4624 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004625 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004626 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004627 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004628 {
4629 .cmd = NL80211_CMD_JOIN_IBSS,
4630 .doit = nl80211_join_ibss,
4631 .policy = nl80211_policy,
4632 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004633 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004634 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004635 },
4636 {
4637 .cmd = NL80211_CMD_LEAVE_IBSS,
4638 .doit = nl80211_leave_ibss,
4639 .policy = nl80211_policy,
4640 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004641 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004642 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004643 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004644#ifdef CONFIG_NL80211_TESTMODE
4645 {
4646 .cmd = NL80211_CMD_TESTMODE,
4647 .doit = nl80211_testmode_do,
4648 .policy = nl80211_policy,
4649 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004650 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4651 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02004652 },
4653#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004654 {
4655 .cmd = NL80211_CMD_CONNECT,
4656 .doit = nl80211_connect,
4657 .policy = nl80211_policy,
4658 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004659 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004660 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004661 },
4662 {
4663 .cmd = NL80211_CMD_DISCONNECT,
4664 .doit = nl80211_disconnect,
4665 .policy = nl80211_policy,
4666 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004667 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004668 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004669 },
Johannes Berg463d0182009-07-14 00:33:35 +02004670 {
4671 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4672 .doit = nl80211_wiphy_netns,
4673 .policy = nl80211_policy,
4674 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004675 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4676 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02004677 },
Holger Schurig61fa7132009-11-11 12:25:40 +01004678 {
4679 .cmd = NL80211_CMD_GET_SURVEY,
4680 .policy = nl80211_policy,
4681 .dumpit = nl80211_dump_survey,
4682 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004683 {
4684 .cmd = NL80211_CMD_SET_PMKSA,
4685 .doit = nl80211_setdel_pmksa,
4686 .policy = nl80211_policy,
4687 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004688 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4689 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004690 },
4691 {
4692 .cmd = NL80211_CMD_DEL_PMKSA,
4693 .doit = nl80211_setdel_pmksa,
4694 .policy = nl80211_policy,
4695 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004696 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4697 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004698 },
4699 {
4700 .cmd = NL80211_CMD_FLUSH_PMKSA,
4701 .doit = nl80211_flush_pmksa,
4702 .policy = nl80211_policy,
4703 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004704 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4705 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004706 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004707 {
4708 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
4709 .doit = nl80211_remain_on_channel,
4710 .policy = nl80211_policy,
4711 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004712 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004713 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004714 },
4715 {
4716 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
4717 .doit = nl80211_cancel_remain_on_channel,
4718 .policy = nl80211_policy,
4719 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004720 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004721 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004722 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004723 {
4724 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
4725 .doit = nl80211_set_tx_bitrate_mask,
4726 .policy = nl80211_policy,
4727 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004728 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4729 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004730 },
Jouni Malinen026331c2010-02-15 12:53:10 +02004731 {
Johannes Berg2e161f72010-08-12 15:38:38 +02004732 .cmd = NL80211_CMD_REGISTER_FRAME,
4733 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02004734 .policy = nl80211_policy,
4735 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004736 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4737 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02004738 },
4739 {
Johannes Berg2e161f72010-08-12 15:38:38 +02004740 .cmd = NL80211_CMD_FRAME,
4741 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02004742 .policy = nl80211_policy,
4743 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004744 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004745 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02004746 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02004747 {
4748 .cmd = NL80211_CMD_SET_POWER_SAVE,
4749 .doit = nl80211_set_power_save,
4750 .policy = nl80211_policy,
4751 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004752 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4753 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02004754 },
4755 {
4756 .cmd = NL80211_CMD_GET_POWER_SAVE,
4757 .doit = nl80211_get_power_save,
4758 .policy = nl80211_policy,
4759 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004760 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4761 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02004762 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004763 {
4764 .cmd = NL80211_CMD_SET_CQM,
4765 .doit = nl80211_set_cqm,
4766 .policy = nl80211_policy,
4767 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004768 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4769 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004770 },
Johannes Bergf444de02010-05-05 15:25:02 +02004771 {
4772 .cmd = NL80211_CMD_SET_CHANNEL,
4773 .doit = nl80211_set_channel,
4774 .policy = nl80211_policy,
4775 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004776 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4777 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02004778 },
Bill Jordane8347eb2010-10-01 13:54:28 -04004779 {
4780 .cmd = NL80211_CMD_SET_WDS_PEER,
4781 .doit = nl80211_set_wds_peer,
4782 .policy = nl80211_policy,
4783 .flags = GENL_ADMIN_PERM,
4784 },
Johannes Berg55682962007-09-20 13:09:35 -04004785};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004786
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004787static struct genl_multicast_group nl80211_mlme_mcgrp = {
4788 .name = "mlme",
4789};
Johannes Berg55682962007-09-20 13:09:35 -04004790
4791/* multicast groups */
4792static struct genl_multicast_group nl80211_config_mcgrp = {
4793 .name = "config",
4794};
Johannes Berg2a519312009-02-10 21:25:55 +01004795static struct genl_multicast_group nl80211_scan_mcgrp = {
4796 .name = "scan",
4797};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004798static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4799 .name = "regulatory",
4800};
Johannes Berg55682962007-09-20 13:09:35 -04004801
4802/* notification functions */
4803
4804void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4805{
4806 struct sk_buff *msg;
4807
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004808 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004809 if (!msg)
4810 return;
4811
4812 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 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_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004819}
4820
Johannes Berg362a4152009-05-24 16:43:15 +02004821static int nl80211_add_scan_req(struct sk_buff *msg,
4822 struct cfg80211_registered_device *rdev)
4823{
4824 struct cfg80211_scan_request *req = rdev->scan_req;
4825 struct nlattr *nest;
4826 int i;
4827
Johannes Berg667503d2009-07-07 03:56:11 +02004828 ASSERT_RDEV_LOCK(rdev);
4829
Johannes Berg362a4152009-05-24 16:43:15 +02004830 if (WARN_ON(!req))
4831 return 0;
4832
4833 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4834 if (!nest)
4835 goto nla_put_failure;
4836 for (i = 0; i < req->n_ssids; i++)
4837 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4838 nla_nest_end(msg, nest);
4839
4840 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4841 if (!nest)
4842 goto nla_put_failure;
4843 for (i = 0; i < req->n_channels; i++)
4844 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4845 nla_nest_end(msg, nest);
4846
4847 if (req->ie)
4848 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4849
4850 return 0;
4851 nla_put_failure:
4852 return -ENOBUFS;
4853}
4854
Johannes Berga538e2d2009-06-16 19:56:42 +02004855static int nl80211_send_scan_msg(struct sk_buff *msg,
4856 struct cfg80211_registered_device *rdev,
4857 struct net_device *netdev,
4858 u32 pid, u32 seq, int flags,
4859 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01004860{
4861 void *hdr;
4862
4863 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4864 if (!hdr)
4865 return -1;
4866
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05004867 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01004868 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4869
Johannes Berg362a4152009-05-24 16:43:15 +02004870 /* ignore errors and send incomplete event anyway */
4871 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004872
4873 return genlmsg_end(msg, hdr);
4874
4875 nla_put_failure:
4876 genlmsg_cancel(msg, hdr);
4877 return -EMSGSIZE;
4878}
4879
Johannes Berga538e2d2009-06-16 19:56:42 +02004880void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4881 struct net_device *netdev)
4882{
4883 struct sk_buff *msg;
4884
4885 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4886 if (!msg)
4887 return;
4888
4889 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4890 NL80211_CMD_TRIGGER_SCAN) < 0) {
4891 nlmsg_free(msg);
4892 return;
4893 }
4894
Johannes Berg463d0182009-07-14 00:33:35 +02004895 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4896 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02004897}
4898
Johannes Berg2a519312009-02-10 21:25:55 +01004899void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4900 struct net_device *netdev)
4901{
4902 struct sk_buff *msg;
4903
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004904 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004905 if (!msg)
4906 return;
4907
Johannes Berga538e2d2009-06-16 19:56:42 +02004908 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4909 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004910 nlmsg_free(msg);
4911 return;
4912 }
4913
Johannes Berg463d0182009-07-14 00:33:35 +02004914 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4915 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004916}
4917
4918void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4919 struct net_device *netdev)
4920{
4921 struct sk_buff *msg;
4922
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004923 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004924 if (!msg)
4925 return;
4926
Johannes Berga538e2d2009-06-16 19:56:42 +02004927 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4928 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004929 nlmsg_free(msg);
4930 return;
4931 }
4932
Johannes Berg463d0182009-07-14 00:33:35 +02004933 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4934 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004935}
4936
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004937/*
4938 * This can happen on global regulatory changes or device specific settings
4939 * based on custom world regulatory domains.
4940 */
4941void nl80211_send_reg_change_event(struct regulatory_request *request)
4942{
4943 struct sk_buff *msg;
4944 void *hdr;
4945
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004946 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004947 if (!msg)
4948 return;
4949
4950 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
4951 if (!hdr) {
4952 nlmsg_free(msg);
4953 return;
4954 }
4955
4956 /* Userspace can always count this one always being set */
4957 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
4958
4959 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
4960 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4961 NL80211_REGDOM_TYPE_WORLD);
4962 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
4963 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4964 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
4965 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
4966 request->intersect)
4967 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4968 NL80211_REGDOM_TYPE_INTERSECTION);
4969 else {
4970 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4971 NL80211_REGDOM_TYPE_COUNTRY);
4972 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
4973 }
4974
4975 if (wiphy_idx_valid(request->wiphy_idx))
4976 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
4977
4978 if (genlmsg_end(msg, hdr) < 0) {
4979 nlmsg_free(msg);
4980 return;
4981 }
4982
Johannes Bergbc43b282009-07-25 10:54:13 +02004983 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02004984 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02004985 GFP_ATOMIC);
4986 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004987
4988 return;
4989
4990nla_put_failure:
4991 genlmsg_cancel(msg, hdr);
4992 nlmsg_free(msg);
4993}
4994
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004995static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
4996 struct net_device *netdev,
4997 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004998 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004999{
5000 struct sk_buff *msg;
5001 void *hdr;
5002
Johannes Berge6d6e342009-07-01 21:26:47 +02005003 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005004 if (!msg)
5005 return;
5006
5007 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5008 if (!hdr) {
5009 nlmsg_free(msg);
5010 return;
5011 }
5012
5013 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5014 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5015 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5016
5017 if (genlmsg_end(msg, hdr) < 0) {
5018 nlmsg_free(msg);
5019 return;
5020 }
5021
Johannes Berg463d0182009-07-14 00:33:35 +02005022 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5023 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005024 return;
5025
5026 nla_put_failure:
5027 genlmsg_cancel(msg, hdr);
5028 nlmsg_free(msg);
5029}
5030
5031void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005032 struct net_device *netdev, const u8 *buf,
5033 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005034{
5035 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005036 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005037}
5038
5039void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5040 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005041 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005042{
Johannes Berge6d6e342009-07-01 21:26:47 +02005043 nl80211_send_mlme_event(rdev, netdev, buf, len,
5044 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005045}
5046
Jouni Malinen53b46b82009-03-27 20:53:56 +02005047void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005048 struct net_device *netdev, const u8 *buf,
5049 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005050{
5051 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005052 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005053}
5054
Jouni Malinen53b46b82009-03-27 20:53:56 +02005055void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5056 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005057 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005058{
5059 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005060 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005061}
5062
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005063static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5064 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005065 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005066{
5067 struct sk_buff *msg;
5068 void *hdr;
5069
Johannes Berge6d6e342009-07-01 21:26:47 +02005070 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005071 if (!msg)
5072 return;
5073
5074 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5075 if (!hdr) {
5076 nlmsg_free(msg);
5077 return;
5078 }
5079
5080 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5081 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5082 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5083 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5084
5085 if (genlmsg_end(msg, hdr) < 0) {
5086 nlmsg_free(msg);
5087 return;
5088 }
5089
Johannes Berg463d0182009-07-14 00:33:35 +02005090 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5091 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005092 return;
5093
5094 nla_put_failure:
5095 genlmsg_cancel(msg, hdr);
5096 nlmsg_free(msg);
5097}
5098
5099void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005100 struct net_device *netdev, const u8 *addr,
5101 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005102{
5103 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005104 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005105}
5106
5107void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005108 struct net_device *netdev, const u8 *addr,
5109 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005110{
Johannes Berge6d6e342009-07-01 21:26:47 +02005111 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5112 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005113}
5114
Samuel Ortizb23aa672009-07-01 21:26:54 +02005115void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5116 struct net_device *netdev, const u8 *bssid,
5117 const u8 *req_ie, size_t req_ie_len,
5118 const u8 *resp_ie, size_t resp_ie_len,
5119 u16 status, gfp_t gfp)
5120{
5121 struct sk_buff *msg;
5122 void *hdr;
5123
5124 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5125 if (!msg)
5126 return;
5127
5128 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5129 if (!hdr) {
5130 nlmsg_free(msg);
5131 return;
5132 }
5133
5134 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5135 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5136 if (bssid)
5137 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5138 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5139 if (req_ie)
5140 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5141 if (resp_ie)
5142 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5143
5144 if (genlmsg_end(msg, hdr) < 0) {
5145 nlmsg_free(msg);
5146 return;
5147 }
5148
Johannes Berg463d0182009-07-14 00:33:35 +02005149 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5150 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005151 return;
5152
5153 nla_put_failure:
5154 genlmsg_cancel(msg, hdr);
5155 nlmsg_free(msg);
5156
5157}
5158
5159void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5160 struct net_device *netdev, const u8 *bssid,
5161 const u8 *req_ie, size_t req_ie_len,
5162 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5163{
5164 struct sk_buff *msg;
5165 void *hdr;
5166
5167 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5168 if (!msg)
5169 return;
5170
5171 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5172 if (!hdr) {
5173 nlmsg_free(msg);
5174 return;
5175 }
5176
5177 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5178 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5179 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5180 if (req_ie)
5181 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5182 if (resp_ie)
5183 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5184
5185 if (genlmsg_end(msg, hdr) < 0) {
5186 nlmsg_free(msg);
5187 return;
5188 }
5189
Johannes Berg463d0182009-07-14 00:33:35 +02005190 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5191 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005192 return;
5193
5194 nla_put_failure:
5195 genlmsg_cancel(msg, hdr);
5196 nlmsg_free(msg);
5197
5198}
5199
5200void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5201 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005202 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005203{
5204 struct sk_buff *msg;
5205 void *hdr;
5206
Johannes Berg667503d2009-07-07 03:56:11 +02005207 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005208 if (!msg)
5209 return;
5210
5211 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5212 if (!hdr) {
5213 nlmsg_free(msg);
5214 return;
5215 }
5216
5217 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5218 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5219 if (from_ap && reason)
5220 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5221 if (from_ap)
5222 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5223 if (ie)
5224 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5225
5226 if (genlmsg_end(msg, hdr) < 0) {
5227 nlmsg_free(msg);
5228 return;
5229 }
5230
Johannes Berg463d0182009-07-14 00:33:35 +02005231 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5232 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005233 return;
5234
5235 nla_put_failure:
5236 genlmsg_cancel(msg, hdr);
5237 nlmsg_free(msg);
5238
5239}
5240
Johannes Berg04a773a2009-04-19 21:24:32 +02005241void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5242 struct net_device *netdev, const u8 *bssid,
5243 gfp_t gfp)
5244{
5245 struct sk_buff *msg;
5246 void *hdr;
5247
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005248 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005249 if (!msg)
5250 return;
5251
5252 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5253 if (!hdr) {
5254 nlmsg_free(msg);
5255 return;
5256 }
5257
5258 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5259 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5260 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5261
5262 if (genlmsg_end(msg, hdr) < 0) {
5263 nlmsg_free(msg);
5264 return;
5265 }
5266
Johannes Berg463d0182009-07-14 00:33:35 +02005267 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5268 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005269 return;
5270
5271 nla_put_failure:
5272 genlmsg_cancel(msg, hdr);
5273 nlmsg_free(msg);
5274}
5275
Jouni Malinena3b8b052009-03-27 21:59:49 +02005276void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5277 struct net_device *netdev, const u8 *addr,
5278 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005279 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005280{
5281 struct sk_buff *msg;
5282 void *hdr;
5283
Johannes Berge6d6e342009-07-01 21:26:47 +02005284 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005285 if (!msg)
5286 return;
5287
5288 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5289 if (!hdr) {
5290 nlmsg_free(msg);
5291 return;
5292 }
5293
5294 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5295 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5296 if (addr)
5297 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5298 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5299 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5300 if (tsc)
5301 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5302
5303 if (genlmsg_end(msg, hdr) < 0) {
5304 nlmsg_free(msg);
5305 return;
5306 }
5307
Johannes Berg463d0182009-07-14 00:33:35 +02005308 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5309 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005310 return;
5311
5312 nla_put_failure:
5313 genlmsg_cancel(msg, hdr);
5314 nlmsg_free(msg);
5315}
5316
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005317void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5318 struct ieee80211_channel *channel_before,
5319 struct ieee80211_channel *channel_after)
5320{
5321 struct sk_buff *msg;
5322 void *hdr;
5323 struct nlattr *nl_freq;
5324
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005325 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005326 if (!msg)
5327 return;
5328
5329 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5330 if (!hdr) {
5331 nlmsg_free(msg);
5332 return;
5333 }
5334
5335 /*
5336 * Since we are applying the beacon hint to a wiphy we know its
5337 * wiphy_idx is valid
5338 */
5339 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5340
5341 /* Before */
5342 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5343 if (!nl_freq)
5344 goto nla_put_failure;
5345 if (nl80211_msg_put_channel(msg, channel_before))
5346 goto nla_put_failure;
5347 nla_nest_end(msg, nl_freq);
5348
5349 /* After */
5350 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5351 if (!nl_freq)
5352 goto nla_put_failure;
5353 if (nl80211_msg_put_channel(msg, channel_after))
5354 goto nla_put_failure;
5355 nla_nest_end(msg, nl_freq);
5356
5357 if (genlmsg_end(msg, hdr) < 0) {
5358 nlmsg_free(msg);
5359 return;
5360 }
5361
Johannes Berg463d0182009-07-14 00:33:35 +02005362 rcu_read_lock();
5363 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5364 GFP_ATOMIC);
5365 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005366
5367 return;
5368
5369nla_put_failure:
5370 genlmsg_cancel(msg, hdr);
5371 nlmsg_free(msg);
5372}
5373
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005374static void nl80211_send_remain_on_chan_event(
5375 int cmd, struct cfg80211_registered_device *rdev,
5376 struct net_device *netdev, u64 cookie,
5377 struct ieee80211_channel *chan,
5378 enum nl80211_channel_type channel_type,
5379 unsigned int duration, gfp_t gfp)
5380{
5381 struct sk_buff *msg;
5382 void *hdr;
5383
5384 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5385 if (!msg)
5386 return;
5387
5388 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5389 if (!hdr) {
5390 nlmsg_free(msg);
5391 return;
5392 }
5393
5394 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5395 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5396 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5397 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5398 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5399
5400 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5401 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5402
5403 if (genlmsg_end(msg, hdr) < 0) {
5404 nlmsg_free(msg);
5405 return;
5406 }
5407
5408 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5409 nl80211_mlme_mcgrp.id, gfp);
5410 return;
5411
5412 nla_put_failure:
5413 genlmsg_cancel(msg, hdr);
5414 nlmsg_free(msg);
5415}
5416
5417void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5418 struct net_device *netdev, u64 cookie,
5419 struct ieee80211_channel *chan,
5420 enum nl80211_channel_type channel_type,
5421 unsigned int duration, gfp_t gfp)
5422{
5423 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5424 rdev, netdev, cookie, chan,
5425 channel_type, duration, gfp);
5426}
5427
5428void nl80211_send_remain_on_channel_cancel(
5429 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5430 u64 cookie, struct ieee80211_channel *chan,
5431 enum nl80211_channel_type channel_type, gfp_t gfp)
5432{
5433 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5434 rdev, netdev, cookie, chan,
5435 channel_type, 0, gfp);
5436}
5437
Johannes Berg98b62182009-12-23 13:15:44 +01005438void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5439 struct net_device *dev, const u8 *mac_addr,
5440 struct station_info *sinfo, gfp_t gfp)
5441{
5442 struct sk_buff *msg;
5443
5444 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5445 if (!msg)
5446 return;
5447
5448 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5449 nlmsg_free(msg);
5450 return;
5451 }
5452
5453 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5454 nl80211_mlme_mcgrp.id, gfp);
5455}
5456
Johannes Berg2e161f72010-08-12 15:38:38 +02005457int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5458 struct net_device *netdev, u32 nlpid,
5459 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005460{
5461 struct sk_buff *msg;
5462 void *hdr;
5463 int err;
5464
5465 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5466 if (!msg)
5467 return -ENOMEM;
5468
Johannes Berg2e161f72010-08-12 15:38:38 +02005469 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005470 if (!hdr) {
5471 nlmsg_free(msg);
5472 return -ENOMEM;
5473 }
5474
5475 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5476 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5477 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5478 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5479
5480 err = genlmsg_end(msg, hdr);
5481 if (err < 0) {
5482 nlmsg_free(msg);
5483 return err;
5484 }
5485
5486 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5487 if (err < 0)
5488 return err;
5489 return 0;
5490
5491 nla_put_failure:
5492 genlmsg_cancel(msg, hdr);
5493 nlmsg_free(msg);
5494 return -ENOBUFS;
5495}
5496
Johannes Berg2e161f72010-08-12 15:38:38 +02005497void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5498 struct net_device *netdev, u64 cookie,
5499 const u8 *buf, size_t len, bool ack,
5500 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005501{
5502 struct sk_buff *msg;
5503 void *hdr;
5504
5505 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5506 if (!msg)
5507 return;
5508
Johannes Berg2e161f72010-08-12 15:38:38 +02005509 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02005510 if (!hdr) {
5511 nlmsg_free(msg);
5512 return;
5513 }
5514
5515 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5516 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5517 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5518 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5519 if (ack)
5520 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5521
5522 if (genlmsg_end(msg, hdr) < 0) {
5523 nlmsg_free(msg);
5524 return;
5525 }
5526
5527 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5528 return;
5529
5530 nla_put_failure:
5531 genlmsg_cancel(msg, hdr);
5532 nlmsg_free(msg);
5533}
5534
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005535void
5536nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5537 struct net_device *netdev,
5538 enum nl80211_cqm_rssi_threshold_event rssi_event,
5539 gfp_t gfp)
5540{
5541 struct sk_buff *msg;
5542 struct nlattr *pinfoattr;
5543 void *hdr;
5544
5545 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5546 if (!msg)
5547 return;
5548
5549 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5550 if (!hdr) {
5551 nlmsg_free(msg);
5552 return;
5553 }
5554
5555 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5556 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5557
5558 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5559 if (!pinfoattr)
5560 goto nla_put_failure;
5561
5562 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5563 rssi_event);
5564
5565 nla_nest_end(msg, pinfoattr);
5566
5567 if (genlmsg_end(msg, hdr) < 0) {
5568 nlmsg_free(msg);
5569 return;
5570 }
5571
5572 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5573 nl80211_mlme_mcgrp.id, gfp);
5574 return;
5575
5576 nla_put_failure:
5577 genlmsg_cancel(msg, hdr);
5578 nlmsg_free(msg);
5579}
5580
Jouni Malinen026331c2010-02-15 12:53:10 +02005581static int nl80211_netlink_notify(struct notifier_block * nb,
5582 unsigned long state,
5583 void *_notify)
5584{
5585 struct netlink_notify *notify = _notify;
5586 struct cfg80211_registered_device *rdev;
5587 struct wireless_dev *wdev;
5588
5589 if (state != NETLINK_URELEASE)
5590 return NOTIFY_DONE;
5591
5592 rcu_read_lock();
5593
5594 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5595 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02005596 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02005597
5598 rcu_read_unlock();
5599
5600 return NOTIFY_DONE;
5601}
5602
5603static struct notifier_block nl80211_netlink_notifier = {
5604 .notifier_call = nl80211_netlink_notify,
5605};
5606
Johannes Berg55682962007-09-20 13:09:35 -04005607/* initialisation/exit functions */
5608
5609int nl80211_init(void)
5610{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005611 int err;
Johannes Berg55682962007-09-20 13:09:35 -04005612
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005613 err = genl_register_family_with_ops(&nl80211_fam,
5614 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04005615 if (err)
5616 return err;
5617
Johannes Berg55682962007-09-20 13:09:35 -04005618 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5619 if (err)
5620 goto err_out;
5621
Johannes Berg2a519312009-02-10 21:25:55 +01005622 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5623 if (err)
5624 goto err_out;
5625
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005626 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5627 if (err)
5628 goto err_out;
5629
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005630 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5631 if (err)
5632 goto err_out;
5633
Johannes Bergaff89a92009-07-01 21:26:51 +02005634#ifdef CONFIG_NL80211_TESTMODE
5635 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5636 if (err)
5637 goto err_out;
5638#endif
5639
Jouni Malinen026331c2010-02-15 12:53:10 +02005640 err = netlink_register_notifier(&nl80211_netlink_notifier);
5641 if (err)
5642 goto err_out;
5643
Johannes Berg55682962007-09-20 13:09:35 -04005644 return 0;
5645 err_out:
5646 genl_unregister_family(&nl80211_fam);
5647 return err;
5648}
5649
5650void nl80211_exit(void)
5651{
Jouni Malinen026331c2010-02-15 12:53:10 +02005652 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04005653 genl_unregister_family(&nl80211_fam);
5654}