blob: 8826888cc14e1dc107528bd3c7b91b83debc1e1e [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 Berge31b8212010-10-05 19:39:30 +020096 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010097
98 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
99 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
100 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
101 .len = IEEE80211_MAX_DATA_LEN },
102 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
103 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100104 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
105 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
106 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
107 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
108 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100109 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100110 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200111 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100112 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
113 .len = IEEE80211_MAX_MESH_ID_LEN },
114 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300115
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700116 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
117 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
118
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300119 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
120 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
121 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200122 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
123 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300124
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700125 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
126
Jouni Malinen36aedc92008-08-25 11:58:58 +0300127 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
128 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200129
130 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
131 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
132 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100133 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
134 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200135
136 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
137 .len = IEEE80211_MAX_SSID_LEN },
138 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
139 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200140 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300141 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300142 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300143 [NL80211_ATTR_STA_FLAGS2] = {
144 .len = sizeof(struct nl80211_sta_flag_update),
145 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300146 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300147 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
148 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200149 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
150 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
151 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200152 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100153 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100154 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
155 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100156 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
157 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200158 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200159 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
160 .len = IEEE80211_MAX_DATA_LEN },
161 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200162 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200163 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300164 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200165 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300166
167 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
168 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200169 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400170};
171
Johannes Berge31b8212010-10-05 19:39:30 +0200172/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000173static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200174 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200175 [NL80211_KEY_IDX] = { .type = NLA_U8 },
176 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
177 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
178 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
179 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200180 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200181};
182
Holger Schuriga0438972009-11-11 11:30:02 +0100183/* ifidx get helper */
184static int nl80211_get_ifidx(struct netlink_callback *cb)
185{
186 int res;
187
188 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
189 nl80211_fam.attrbuf, nl80211_fam.maxattr,
190 nl80211_policy);
191 if (res)
192 return res;
193
194 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
195 return -EINVAL;
196
197 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
198 if (!res)
199 return -EINVAL;
200 return res;
201}
202
Johannes Berg67748892010-10-04 21:14:06 +0200203static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
204 struct netlink_callback *cb,
205 struct cfg80211_registered_device **rdev,
206 struct net_device **dev)
207{
208 int ifidx = cb->args[0];
209 int err;
210
211 if (!ifidx)
212 ifidx = nl80211_get_ifidx(cb);
213 if (ifidx < 0)
214 return ifidx;
215
216 cb->args[0] = ifidx;
217
218 rtnl_lock();
219
220 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
221 if (!*dev) {
222 err = -ENODEV;
223 goto out_rtnl;
224 }
225
226 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
227 if (IS_ERR(dev)) {
228 err = PTR_ERR(dev);
229 goto out_rtnl;
230 }
231
232 return 0;
233 out_rtnl:
234 rtnl_unlock();
235 return err;
236}
237
238static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
239{
240 cfg80211_unlock_rdev(rdev);
241 rtnl_unlock();
242}
243
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100244/* IE validation */
245static bool is_valid_ie_attr(const struct nlattr *attr)
246{
247 const u8 *pos;
248 int len;
249
250 if (!attr)
251 return true;
252
253 pos = nla_data(attr);
254 len = nla_len(attr);
255
256 while (len) {
257 u8 elemlen;
258
259 if (len < 2)
260 return false;
261 len -= 2;
262
263 elemlen = pos[1];
264 if (elemlen > len)
265 return false;
266
267 len -= elemlen;
268 pos += 2 + elemlen;
269 }
270
271 return true;
272}
273
Johannes Berg55682962007-09-20 13:09:35 -0400274/* message building helper */
275static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
276 int flags, u8 cmd)
277{
278 /* since there is no private header just add the generic one */
279 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
280}
281
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400282static int nl80211_msg_put_channel(struct sk_buff *msg,
283 struct ieee80211_channel *chan)
284{
285 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
286 chan->center_freq);
287
288 if (chan->flags & IEEE80211_CHAN_DISABLED)
289 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
290 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
291 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
292 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
293 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
294 if (chan->flags & IEEE80211_CHAN_RADAR)
295 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
296
297 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
298 DBM_TO_MBM(chan->max_power));
299
300 return 0;
301
302 nla_put_failure:
303 return -ENOBUFS;
304}
305
Johannes Berg55682962007-09-20 13:09:35 -0400306/* netlink command implementations */
307
Johannes Bergb9454e82009-07-08 13:29:08 +0200308struct key_parse {
309 struct key_params p;
310 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200311 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200312 bool def, defmgmt;
313};
314
315static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
316{
317 struct nlattr *tb[NL80211_KEY_MAX + 1];
318 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
319 nl80211_key_policy);
320 if (err)
321 return err;
322
323 k->def = !!tb[NL80211_KEY_DEFAULT];
324 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
325
326 if (tb[NL80211_KEY_IDX])
327 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
328
329 if (tb[NL80211_KEY_DATA]) {
330 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
331 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
332 }
333
334 if (tb[NL80211_KEY_SEQ]) {
335 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
336 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
337 }
338
339 if (tb[NL80211_KEY_CIPHER])
340 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
341
Johannes Berge31b8212010-10-05 19:39:30 +0200342 if (tb[NL80211_KEY_TYPE]) {
343 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
344 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
345 return -EINVAL;
346 }
347
Johannes Bergb9454e82009-07-08 13:29:08 +0200348 return 0;
349}
350
351static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
352{
353 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
354 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
355 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
356 }
357
358 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
359 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
360 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
361 }
362
363 if (info->attrs[NL80211_ATTR_KEY_IDX])
364 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
365
366 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
367 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
368
369 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
370 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
371
Johannes Berge31b8212010-10-05 19:39:30 +0200372 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
373 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
374 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
375 return -EINVAL;
376 }
377
Johannes Bergb9454e82009-07-08 13:29:08 +0200378 return 0;
379}
380
381static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
382{
383 int err;
384
385 memset(k, 0, sizeof(*k));
386 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200387 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200388
389 if (info->attrs[NL80211_ATTR_KEY])
390 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
391 else
392 err = nl80211_parse_key_old(info, k);
393
394 if (err)
395 return err;
396
397 if (k->def && k->defmgmt)
398 return -EINVAL;
399
400 if (k->idx != -1) {
401 if (k->defmgmt) {
402 if (k->idx < 4 || k->idx > 5)
403 return -EINVAL;
404 } else if (k->def) {
405 if (k->idx < 0 || k->idx > 3)
406 return -EINVAL;
407 } else {
408 if (k->idx < 0 || k->idx > 5)
409 return -EINVAL;
410 }
411 }
412
413 return 0;
414}
415
Johannes Bergfffd0932009-07-08 14:22:54 +0200416static struct cfg80211_cached_keys *
417nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
418 struct nlattr *keys)
419{
420 struct key_parse parse;
421 struct nlattr *key;
422 struct cfg80211_cached_keys *result;
423 int rem, err, def = 0;
424
425 result = kzalloc(sizeof(*result), GFP_KERNEL);
426 if (!result)
427 return ERR_PTR(-ENOMEM);
428
429 result->def = -1;
430 result->defmgmt = -1;
431
432 nla_for_each_nested(key, keys, rem) {
433 memset(&parse, 0, sizeof(parse));
434 parse.idx = -1;
435
436 err = nl80211_parse_key_new(key, &parse);
437 if (err)
438 goto error;
439 err = -EINVAL;
440 if (!parse.p.key)
441 goto error;
442 if (parse.idx < 0 || parse.idx > 4)
443 goto error;
444 if (parse.def) {
445 if (def)
446 goto error;
447 def = 1;
448 result->def = parse.idx;
449 } else if (parse.defmgmt)
450 goto error;
451 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200452 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200453 if (err)
454 goto error;
455 result->params[parse.idx].cipher = parse.p.cipher;
456 result->params[parse.idx].key_len = parse.p.key_len;
457 result->params[parse.idx].key = result->data[parse.idx];
458 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
459 }
460
461 return result;
462 error:
463 kfree(result);
464 return ERR_PTR(err);
465}
466
467static int nl80211_key_allowed(struct wireless_dev *wdev)
468{
469 ASSERT_WDEV_LOCK(wdev);
470
Johannes Bergfffd0932009-07-08 14:22:54 +0200471 switch (wdev->iftype) {
472 case NL80211_IFTYPE_AP:
473 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200474 case NL80211_IFTYPE_P2P_GO:
Johannes Bergfffd0932009-07-08 14:22:54 +0200475 break;
476 case NL80211_IFTYPE_ADHOC:
477 if (!wdev->current_bss)
478 return -ENOLINK;
479 break;
480 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200481 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200482 if (wdev->sme_state != CFG80211_SME_CONNECTED)
483 return -ENOLINK;
484 break;
485 default:
486 return -EINVAL;
487 }
488
489 return 0;
490}
491
Johannes Berg55682962007-09-20 13:09:35 -0400492static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
493 struct cfg80211_registered_device *dev)
494{
495 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100496 struct nlattr *nl_bands, *nl_band;
497 struct nlattr *nl_freqs, *nl_freq;
498 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700499 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100500 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100501 enum ieee80211_band band;
502 struct ieee80211_channel *chan;
503 struct ieee80211_rate *rate;
504 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700505 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg2e161f72010-08-12 15:38:38 +0200506 const struct ieee80211_txrx_stypes *mgmt_stypes =
507 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400508
509 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
510 if (!hdr)
511 return -1;
512
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500513 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400514 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200515
Johannes Bergf5ea9122009-08-07 16:17:38 +0200516 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
517 cfg80211_rdev_list_generation);
518
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200519 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
520 dev->wiphy.retry_short);
521 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
522 dev->wiphy.retry_long);
523 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
524 dev->wiphy.frag_threshold);
525 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
526 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100527 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
528 dev->wiphy.coverage_class);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200529
Johannes Berg2a519312009-02-10 21:25:55 +0100530 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
531 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200532 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
533 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100534
Johannes Berge31b8212010-10-05 19:39:30 +0200535 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
536 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
537
Johannes Berg25e47c182009-04-02 20:14:06 +0200538 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
539 sizeof(u32) * dev->wiphy.n_cipher_suites,
540 dev->wiphy.cipher_suites);
541
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100542 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
543 dev->wiphy.max_num_pmkids);
544
Johannes Bergc0692b82010-08-27 14:26:53 +0300545 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
546 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
547
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700548 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
549 if (!nl_modes)
550 goto nla_put_failure;
551
552 i = 0;
553 while (ifmodes) {
554 if (ifmodes & 1)
555 NLA_PUT_FLAG(msg, i);
556 ifmodes >>= 1;
557 i++;
558 }
559
560 nla_nest_end(msg, nl_modes);
561
Johannes Bergee688b002008-01-24 19:38:39 +0100562 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
563 if (!nl_bands)
564 goto nla_put_failure;
565
566 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
567 if (!dev->wiphy.bands[band])
568 continue;
569
570 nl_band = nla_nest_start(msg, band);
571 if (!nl_band)
572 goto nla_put_failure;
573
Johannes Bergd51626d2008-10-09 12:20:13 +0200574 /* add HT info */
575 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
576 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
577 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
578 &dev->wiphy.bands[band]->ht_cap.mcs);
579 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
580 dev->wiphy.bands[band]->ht_cap.cap);
581 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
582 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
583 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
584 dev->wiphy.bands[band]->ht_cap.ampdu_density);
585 }
586
Johannes Bergee688b002008-01-24 19:38:39 +0100587 /* add frequencies */
588 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
589 if (!nl_freqs)
590 goto nla_put_failure;
591
592 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
593 nl_freq = nla_nest_start(msg, i);
594 if (!nl_freq)
595 goto nla_put_failure;
596
597 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100598
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400599 if (nl80211_msg_put_channel(msg, chan))
600 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200601
Johannes Bergee688b002008-01-24 19:38:39 +0100602 nla_nest_end(msg, nl_freq);
603 }
604
605 nla_nest_end(msg, nl_freqs);
606
607 /* add bitrates */
608 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
609 if (!nl_rates)
610 goto nla_put_failure;
611
612 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
613 nl_rate = nla_nest_start(msg, i);
614 if (!nl_rate)
615 goto nla_put_failure;
616
617 rate = &dev->wiphy.bands[band]->bitrates[i];
618 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
619 rate->bitrate);
620 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
621 NLA_PUT_FLAG(msg,
622 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
623
624 nla_nest_end(msg, nl_rate);
625 }
626
627 nla_nest_end(msg, nl_rates);
628
629 nla_nest_end(msg, nl_band);
630 }
631 nla_nest_end(msg, nl_bands);
632
Johannes Berg8fdc6212009-03-14 09:34:01 +0100633 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
634 if (!nl_cmds)
635 goto nla_put_failure;
636
637 i = 0;
638#define CMD(op, n) \
639 do { \
640 if (dev->ops->op) { \
641 i++; \
642 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
643 } \
644 } while (0)
645
646 CMD(add_virtual_intf, NEW_INTERFACE);
647 CMD(change_virtual_intf, SET_INTERFACE);
648 CMD(add_key, NEW_KEY);
649 CMD(add_beacon, NEW_BEACON);
650 CMD(add_station, NEW_STATION);
651 CMD(add_mpath, NEW_MPATH);
652 CMD(set_mesh_params, SET_MESH_PARAMS);
653 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200654 CMD(auth, AUTHENTICATE);
655 CMD(assoc, ASSOCIATE);
656 CMD(deauth, DEAUTHENTICATE);
657 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200658 CMD(join_ibss, JOIN_IBSS);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100659 CMD(set_pmksa, SET_PMKSA);
660 CMD(del_pmksa, DEL_PMKSA);
661 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100662 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200663 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200664 CMD(mgmt_tx, FRAME);
Johannes Berg5be83de2009-11-19 00:56:28 +0100665 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200666 i++;
667 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
668 }
Johannes Bergf444de02010-05-05 15:25:02 +0200669 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400670 CMD(set_wds_peer, SET_WDS_PEER);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100671
672#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200673
Johannes Berg6829c8782009-07-02 09:13:27 +0200674 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200675 i++;
676 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
677 }
678
Johannes Berg6829c8782009-07-02 09:13:27 +0200679 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200680 i++;
681 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
682 }
683
Johannes Berg8fdc6212009-03-14 09:34:01 +0100684 nla_nest_end(msg, nl_cmds);
685
Johannes Berg2e161f72010-08-12 15:38:38 +0200686 if (mgmt_stypes) {
687 u16 stypes;
688 struct nlattr *nl_ftypes, *nl_ifs;
689 enum nl80211_iftype ift;
690
691 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
692 if (!nl_ifs)
693 goto nla_put_failure;
694
695 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
696 nl_ftypes = nla_nest_start(msg, ift);
697 if (!nl_ftypes)
698 goto nla_put_failure;
699 i = 0;
700 stypes = mgmt_stypes[ift].tx;
701 while (stypes) {
702 if (stypes & 1)
703 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
704 (i << 4) | IEEE80211_FTYPE_MGMT);
705 stypes >>= 1;
706 i++;
707 }
708 nla_nest_end(msg, nl_ftypes);
709 }
710
Johannes Berg74b70a42010-08-24 12:15:53 +0200711 nla_nest_end(msg, nl_ifs);
712
Johannes Berg2e161f72010-08-12 15:38:38 +0200713 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
714 if (!nl_ifs)
715 goto nla_put_failure;
716
717 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
718 nl_ftypes = nla_nest_start(msg, ift);
719 if (!nl_ftypes)
720 goto nla_put_failure;
721 i = 0;
722 stypes = mgmt_stypes[ift].rx;
723 while (stypes) {
724 if (stypes & 1)
725 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
726 (i << 4) | IEEE80211_FTYPE_MGMT);
727 stypes >>= 1;
728 i++;
729 }
730 nla_nest_end(msg, nl_ftypes);
731 }
732 nla_nest_end(msg, nl_ifs);
733 }
734
Johannes Berg55682962007-09-20 13:09:35 -0400735 return genlmsg_end(msg, hdr);
736
737 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700738 genlmsg_cancel(msg, hdr);
739 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400740}
741
742static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
743{
744 int idx = 0;
745 int start = cb->args[0];
746 struct cfg80211_registered_device *dev;
747
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500748 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200749 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200750 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
751 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200752 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400753 continue;
754 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
755 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200756 dev) < 0) {
757 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400758 break;
Julius Volzb4637272008-07-08 14:02:19 +0200759 }
Johannes Berg55682962007-09-20 13:09:35 -0400760 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500761 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400762
763 cb->args[0] = idx;
764
765 return skb->len;
766}
767
768static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
769{
770 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200771 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400772
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700773 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400774 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200775 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400776
Johannes Berg4c476992010-10-04 21:36:35 +0200777 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
778 nlmsg_free(msg);
779 return -ENOBUFS;
780 }
Johannes Berg55682962007-09-20 13:09:35 -0400781
Johannes Berg134e6372009-07-10 09:51:34 +0000782 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400783}
784
Jouni Malinen31888482008-10-30 16:59:24 +0200785static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
786 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
787 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
788 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
789 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
790 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
791};
792
793static int parse_txq_params(struct nlattr *tb[],
794 struct ieee80211_txq_params *txq_params)
795{
796 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
797 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
798 !tb[NL80211_TXQ_ATTR_AIFS])
799 return -EINVAL;
800
801 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
802 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
803 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
804 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
805 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
806
807 return 0;
808}
809
Johannes Bergf444de02010-05-05 15:25:02 +0200810static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
811{
812 /*
813 * You can only set the channel explicitly for AP, mesh
814 * and WDS type interfaces; all others have their channel
815 * managed via their respective "establish a connection"
816 * command (connect, join, ...)
817 *
818 * Monitors are special as they are normally slaved to
819 * whatever else is going on, so they behave as though
820 * you tried setting the wiphy channel itself.
821 */
822 return !wdev ||
823 wdev->iftype == NL80211_IFTYPE_AP ||
824 wdev->iftype == NL80211_IFTYPE_WDS ||
825 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200826 wdev->iftype == NL80211_IFTYPE_MONITOR ||
827 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200828}
829
830static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
831 struct wireless_dev *wdev,
832 struct genl_info *info)
833{
834 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
835 u32 freq;
836 int result;
837
838 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
839 return -EINVAL;
840
841 if (!nl80211_can_set_dev_channel(wdev))
842 return -EOPNOTSUPP;
843
844 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
845 channel_type = nla_get_u32(info->attrs[
846 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
847 if (channel_type != NL80211_CHAN_NO_HT &&
848 channel_type != NL80211_CHAN_HT20 &&
849 channel_type != NL80211_CHAN_HT40PLUS &&
850 channel_type != NL80211_CHAN_HT40MINUS)
851 return -EINVAL;
852 }
853
854 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
855
856 mutex_lock(&rdev->devlist_mtx);
857 if (wdev) {
858 wdev_lock(wdev);
859 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
860 wdev_unlock(wdev);
861 } else {
862 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
863 }
864 mutex_unlock(&rdev->devlist_mtx);
865
866 return result;
867}
868
869static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
870{
Johannes Berg4c476992010-10-04 21:36:35 +0200871 struct cfg80211_registered_device *rdev = info->user_ptr[0];
872 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +0200873
Johannes Berg4c476992010-10-04 21:36:35 +0200874 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +0200875}
876
Bill Jordane8347eb2010-10-01 13:54:28 -0400877static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
878{
879 struct cfg80211_registered_device *rdev;
880 struct wireless_dev *wdev;
881 struct net_device *dev;
882 u8 *bssid;
883 int err;
884
885 if (!info->attrs[NL80211_ATTR_MAC])
886 return -EINVAL;
887
888 rtnl_lock();
889
890 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
891 if (err)
892 goto unlock_rtnl;
893
894 wdev = dev->ieee80211_ptr;
895
896 if (netif_running(dev)) {
897 err = -EBUSY;
898 goto out;
899 }
900
901 if (!rdev->ops->set_wds_peer) {
902 err = -EOPNOTSUPP;
903 goto out;
904 }
905
906 if (wdev->iftype != NL80211_IFTYPE_WDS) {
907 err = -EOPNOTSUPP;
908 goto out;
909 }
910
911 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
912 err = rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
913
914out:
915 cfg80211_unlock_rdev(rdev);
916 dev_put(dev);
917unlock_rtnl:
918 rtnl_unlock();
919
920 return err;
921}
922
923
Johannes Berg55682962007-09-20 13:09:35 -0400924static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
925{
926 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200927 struct net_device *netdev = NULL;
928 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -0400929 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200930 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200931 u32 changed;
932 u8 retry_short = 0, retry_long = 0;
933 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100934 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400935
Johannes Bergf444de02010-05-05 15:25:02 +0200936 /*
937 * Try to find the wiphy and netdev. Normally this
938 * function shouldn't need the netdev, but this is
939 * done for backward compatibility -- previously
940 * setting the channel was done per wiphy, but now
941 * it is per netdev. Previous userland like hostapd
942 * also passed a netdev to set_wiphy, so that it is
943 * possible to let that go to the right netdev!
944 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100945 mutex_lock(&cfg80211_mutex);
946
Johannes Bergf444de02010-05-05 15:25:02 +0200947 if (info->attrs[NL80211_ATTR_IFINDEX]) {
948 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
949
950 netdev = dev_get_by_index(genl_info_net(info), ifindex);
951 if (netdev && netdev->ieee80211_ptr) {
952 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
953 mutex_lock(&rdev->mtx);
954 } else
955 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100956 }
957
Johannes Bergf444de02010-05-05 15:25:02 +0200958 if (!netdev) {
959 rdev = __cfg80211_rdev_from_info(info);
960 if (IS_ERR(rdev)) {
961 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +0200962 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +0200963 }
964 wdev = NULL;
965 netdev = NULL;
966 result = 0;
967
968 mutex_lock(&rdev->mtx);
969 } else if (netif_running(netdev) &&
970 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
971 wdev = netdev->ieee80211_ptr;
972 else
973 wdev = NULL;
974
975 /*
976 * end workaround code, by now the rdev is available
977 * and locked, and wdev may or may not be NULL.
978 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100979
980 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200981 result = cfg80211_dev_rename(
982 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100983
984 mutex_unlock(&cfg80211_mutex);
985
986 if (result)
987 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400988
Jouni Malinen31888482008-10-30 16:59:24 +0200989 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
990 struct ieee80211_txq_params txq_params;
991 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
992
993 if (!rdev->ops->set_txq_params) {
994 result = -EOPNOTSUPP;
995 goto bad_res;
996 }
997
998 nla_for_each_nested(nl_txq_params,
999 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1000 rem_txq_params) {
1001 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1002 nla_data(nl_txq_params),
1003 nla_len(nl_txq_params),
1004 txq_params_policy);
1005 result = parse_txq_params(tb, &txq_params);
1006 if (result)
1007 goto bad_res;
1008
1009 result = rdev->ops->set_txq_params(&rdev->wiphy,
1010 &txq_params);
1011 if (result)
1012 goto bad_res;
1013 }
1014 }
1015
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001016 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001017 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001018 if (result)
1019 goto bad_res;
1020 }
1021
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001022 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1023 enum nl80211_tx_power_setting type;
1024 int idx, mbm = 0;
1025
1026 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001027 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001028 goto bad_res;
1029 }
1030
1031 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1032 type = nla_get_u32(info->attrs[idx]);
1033
1034 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1035 (type != NL80211_TX_POWER_AUTOMATIC)) {
1036 result = -EINVAL;
1037 goto bad_res;
1038 }
1039
1040 if (type != NL80211_TX_POWER_AUTOMATIC) {
1041 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1042 mbm = nla_get_u32(info->attrs[idx]);
1043 }
1044
1045 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1046 if (result)
1047 goto bad_res;
1048 }
1049
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001050 changed = 0;
1051
1052 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1053 retry_short = nla_get_u8(
1054 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1055 if (retry_short == 0) {
1056 result = -EINVAL;
1057 goto bad_res;
1058 }
1059 changed |= WIPHY_PARAM_RETRY_SHORT;
1060 }
1061
1062 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1063 retry_long = nla_get_u8(
1064 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1065 if (retry_long == 0) {
1066 result = -EINVAL;
1067 goto bad_res;
1068 }
1069 changed |= WIPHY_PARAM_RETRY_LONG;
1070 }
1071
1072 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1073 frag_threshold = nla_get_u32(
1074 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1075 if (frag_threshold < 256) {
1076 result = -EINVAL;
1077 goto bad_res;
1078 }
1079 if (frag_threshold != (u32) -1) {
1080 /*
1081 * Fragments (apart from the last one) are required to
1082 * have even length. Make the fragmentation code
1083 * simpler by stripping LSB should someone try to use
1084 * odd threshold value.
1085 */
1086 frag_threshold &= ~0x1;
1087 }
1088 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1089 }
1090
1091 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1092 rts_threshold = nla_get_u32(
1093 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1094 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1095 }
1096
Lukáš Turek81077e82009-12-21 22:50:47 +01001097 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1098 coverage_class = nla_get_u8(
1099 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1100 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1101 }
1102
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001103 if (changed) {
1104 u8 old_retry_short, old_retry_long;
1105 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001106 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001107
1108 if (!rdev->ops->set_wiphy_params) {
1109 result = -EOPNOTSUPP;
1110 goto bad_res;
1111 }
1112
1113 old_retry_short = rdev->wiphy.retry_short;
1114 old_retry_long = rdev->wiphy.retry_long;
1115 old_frag_threshold = rdev->wiphy.frag_threshold;
1116 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001117 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001118
1119 if (changed & WIPHY_PARAM_RETRY_SHORT)
1120 rdev->wiphy.retry_short = retry_short;
1121 if (changed & WIPHY_PARAM_RETRY_LONG)
1122 rdev->wiphy.retry_long = retry_long;
1123 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1124 rdev->wiphy.frag_threshold = frag_threshold;
1125 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1126 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001127 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1128 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001129
1130 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1131 if (result) {
1132 rdev->wiphy.retry_short = old_retry_short;
1133 rdev->wiphy.retry_long = old_retry_long;
1134 rdev->wiphy.frag_threshold = old_frag_threshold;
1135 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001136 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001137 }
1138 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001139
Johannes Berg306d6112008-12-08 12:39:04 +01001140 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001141 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001142 if (netdev)
1143 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001144 return result;
1145}
1146
1147
1148static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001149 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001150 struct net_device *dev)
1151{
1152 void *hdr;
1153
1154 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1155 if (!hdr)
1156 return -1;
1157
1158 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001159 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001160 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001161 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001162
1163 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1164 rdev->devlist_generation ^
1165 (cfg80211_rdev_list_generation << 2));
1166
Johannes Berg55682962007-09-20 13:09:35 -04001167 return genlmsg_end(msg, hdr);
1168
1169 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001170 genlmsg_cancel(msg, hdr);
1171 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001172}
1173
1174static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1175{
1176 int wp_idx = 0;
1177 int if_idx = 0;
1178 int wp_start = cb->args[0];
1179 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001180 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001181 struct wireless_dev *wdev;
1182
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001183 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001184 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1185 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001186 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001187 if (wp_idx < wp_start) {
1188 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001189 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001190 }
Johannes Berg55682962007-09-20 13:09:35 -04001191 if_idx = 0;
1192
Johannes Bergf5ea9122009-08-07 16:17:38 +02001193 mutex_lock(&rdev->devlist_mtx);
1194 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001195 if (if_idx < if_start) {
1196 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001197 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001198 }
Johannes Berg55682962007-09-20 13:09:35 -04001199 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1200 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001201 rdev, wdev->netdev) < 0) {
1202 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001203 goto out;
1204 }
1205 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001206 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001207 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001208
1209 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001210 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001211 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001212 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001213
1214 cb->args[0] = wp_idx;
1215 cb->args[1] = if_idx;
1216
1217 return skb->len;
1218}
1219
1220static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1221{
1222 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001223 struct cfg80211_registered_device *dev = info->user_ptr[0];
1224 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001225
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001226 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001227 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001228 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001229
Johannes Bergd7264052009-04-19 16:23:20 +02001230 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001231 dev, netdev) < 0) {
1232 nlmsg_free(msg);
1233 return -ENOBUFS;
1234 }
Johannes Berg55682962007-09-20 13:09:35 -04001235
Johannes Berg134e6372009-07-10 09:51:34 +00001236 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001237}
1238
Michael Wu66f7ac52008-01-31 19:48:22 +01001239static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1240 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1241 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1242 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1243 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1244 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1245};
1246
1247static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1248{
1249 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1250 int flag;
1251
1252 *mntrflags = 0;
1253
1254 if (!nla)
1255 return -EINVAL;
1256
1257 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1258 nla, mntr_flags_policy))
1259 return -EINVAL;
1260
1261 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1262 if (flags[flag])
1263 *mntrflags |= (1<<flag);
1264
1265 return 0;
1266}
1267
Johannes Berg9bc383d2009-11-19 11:55:19 +01001268static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001269 struct net_device *netdev, u8 use_4addr,
1270 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001271{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001272 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001273 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001274 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001275 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001276 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001277
1278 switch (iftype) {
1279 case NL80211_IFTYPE_AP_VLAN:
1280 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1281 return 0;
1282 break;
1283 case NL80211_IFTYPE_STATION:
1284 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1285 return 0;
1286 break;
1287 default:
1288 break;
1289 }
1290
1291 return -EOPNOTSUPP;
1292}
1293
Johannes Berg55682962007-09-20 13:09:35 -04001294static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1295{
Johannes Berg4c476992010-10-04 21:36:35 +02001296 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001297 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001298 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001299 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001300 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001301 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001302 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001303
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001304 memset(&params, 0, sizeof(params));
1305
Johannes Berg04a773a2009-04-19 21:24:32 +02001306 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001307
Johannes Berg723b0382008-09-16 20:22:09 +02001308 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001309 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001310 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001311 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001312 if (ntype > NL80211_IFTYPE_MAX)
1313 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001314 }
1315
Johannes Berg92ffe052008-09-16 20:39:36 +02001316 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001317 if (ntype != NL80211_IFTYPE_MESH_POINT)
1318 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001319 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1320 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001321 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001322 }
1323
Felix Fietkau8b787642009-11-10 18:53:10 +01001324 if (info->attrs[NL80211_ATTR_4ADDR]) {
1325 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1326 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001327 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001328 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001329 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001330 } else {
1331 params.use_4addr = -1;
1332 }
1333
Johannes Berg92ffe052008-09-16 20:39:36 +02001334 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001335 if (ntype != NL80211_IFTYPE_MONITOR)
1336 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001337 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1338 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001339 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001340 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001341
1342 flags = &_flags;
1343 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001344 }
Johannes Berg3b858752009-03-12 09:55:09 +01001345
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001346 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001347 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001348 else
1349 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001350
Johannes Berg9bc383d2009-11-19 11:55:19 +01001351 if (!err && params.use_4addr != -1)
1352 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1353
Johannes Berg55682962007-09-20 13:09:35 -04001354 return err;
1355}
1356
1357static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1358{
Johannes Berg4c476992010-10-04 21:36:35 +02001359 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001360 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001361 int err;
1362 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001363 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001364
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001365 memset(&params, 0, sizeof(params));
1366
Johannes Berg55682962007-09-20 13:09:35 -04001367 if (!info->attrs[NL80211_ATTR_IFNAME])
1368 return -EINVAL;
1369
1370 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1371 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1372 if (type > NL80211_IFTYPE_MAX)
1373 return -EINVAL;
1374 }
1375
Johannes Berg79c97e92009-07-07 03:56:12 +02001376 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001377 !(rdev->wiphy.interface_modes & (1 << type)))
1378 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001379
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001380 if (type == NL80211_IFTYPE_MESH_POINT &&
1381 info->attrs[NL80211_ATTR_MESH_ID]) {
1382 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1383 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1384 }
1385
Johannes Berg9bc383d2009-11-19 11:55:19 +01001386 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001387 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001388 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001389 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001390 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001391 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001392
Michael Wu66f7ac52008-01-31 19:48:22 +01001393 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1394 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1395 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001396 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001397 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001398 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001399
Johannes Berg55682962007-09-20 13:09:35 -04001400 return err;
1401}
1402
1403static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1404{
Johannes Berg4c476992010-10-04 21:36:35 +02001405 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1406 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001407
Johannes Berg4c476992010-10-04 21:36:35 +02001408 if (!rdev->ops->del_virtual_intf)
1409 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001410
Johannes Berg4c476992010-10-04 21:36:35 +02001411 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001412}
1413
Johannes Berg41ade002007-12-19 02:03:29 +01001414struct get_key_cookie {
1415 struct sk_buff *msg;
1416 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001417 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001418};
1419
1420static void get_key_callback(void *c, struct key_params *params)
1421{
Johannes Bergb9454e82009-07-08 13:29:08 +02001422 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001423 struct get_key_cookie *cookie = c;
1424
1425 if (params->key)
1426 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1427 params->key_len, params->key);
1428
1429 if (params->seq)
1430 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1431 params->seq_len, params->seq);
1432
1433 if (params->cipher)
1434 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1435 params->cipher);
1436
Johannes Bergb9454e82009-07-08 13:29:08 +02001437 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1438 if (!key)
1439 goto nla_put_failure;
1440
1441 if (params->key)
1442 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1443 params->key_len, params->key);
1444
1445 if (params->seq)
1446 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1447 params->seq_len, params->seq);
1448
1449 if (params->cipher)
1450 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1451 params->cipher);
1452
1453 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1454
1455 nla_nest_end(cookie->msg, key);
1456
Johannes Berg41ade002007-12-19 02:03:29 +01001457 return;
1458 nla_put_failure:
1459 cookie->error = 1;
1460}
1461
1462static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1463{
Johannes Berg4c476992010-10-04 21:36:35 +02001464 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001465 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001466 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001467 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001468 const u8 *mac_addr = NULL;
1469 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001470 struct get_key_cookie cookie = {
1471 .error = 0,
1472 };
1473 void *hdr;
1474 struct sk_buff *msg;
1475
1476 if (info->attrs[NL80211_ATTR_KEY_IDX])
1477 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1478
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001479 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001480 return -EINVAL;
1481
1482 if (info->attrs[NL80211_ATTR_MAC])
1483 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1484
Johannes Berge31b8212010-10-05 19:39:30 +02001485 pairwise = !!mac_addr;
1486 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1487 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1488 if (kt >= NUM_NL80211_KEYTYPES)
1489 return -EINVAL;
1490 if (kt != NL80211_KEYTYPE_GROUP &&
1491 kt != NL80211_KEYTYPE_PAIRWISE)
1492 return -EINVAL;
1493 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1494 }
1495
Johannes Berg4c476992010-10-04 21:36:35 +02001496 if (!rdev->ops->get_key)
1497 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001498
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001499 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001500 if (!msg)
1501 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001502
1503 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1504 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001505 if (IS_ERR(hdr))
1506 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001507
1508 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001509 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001510
1511 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1512 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1513 if (mac_addr)
1514 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1515
Johannes Berge31b8212010-10-05 19:39:30 +02001516 if (pairwise && mac_addr &&
1517 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1518 return -ENOENT;
1519
1520 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1521 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001522
1523 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001524 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001525
1526 if (cookie.error)
1527 goto nla_put_failure;
1528
1529 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001530 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001531
1532 nla_put_failure:
1533 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001534 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001535 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001536 return err;
1537}
1538
1539static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1540{
Johannes Berg4c476992010-10-04 21:36:35 +02001541 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001542 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001543 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001544 struct net_device *dev = info->user_ptr[1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001545 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1546 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001547
Johannes Bergb9454e82009-07-08 13:29:08 +02001548 err = nl80211_parse_key(info, &key);
1549 if (err)
1550 return err;
1551
1552 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001553 return -EINVAL;
1554
Johannes Bergb9454e82009-07-08 13:29:08 +02001555 /* only support setting default key */
1556 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001557 return -EINVAL;
1558
Johannes Bergb9454e82009-07-08 13:29:08 +02001559 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001560 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001561 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001562 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001563
Johannes Berg4c476992010-10-04 21:36:35 +02001564 if (!func)
1565 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001566
Johannes Bergfffd0932009-07-08 14:22:54 +02001567 wdev_lock(dev->ieee80211_ptr);
1568 err = nl80211_key_allowed(dev->ieee80211_ptr);
1569 if (!err)
1570 err = func(&rdev->wiphy, dev, key.idx);
1571
Johannes Berg3d23e342009-09-29 23:27:28 +02001572#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001573 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001574 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001575 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001576 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001577 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001578 }
1579#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001580 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001581
Johannes Berg41ade002007-12-19 02:03:29 +01001582 return err;
1583}
1584
1585static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1586{
Johannes Berg4c476992010-10-04 21:36:35 +02001587 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001588 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001589 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001590 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001591 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001592
Johannes Bergb9454e82009-07-08 13:29:08 +02001593 err = nl80211_parse_key(info, &key);
1594 if (err)
1595 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001596
Johannes Bergb9454e82009-07-08 13:29:08 +02001597 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001598 return -EINVAL;
1599
Johannes Berg41ade002007-12-19 02:03:29 +01001600 if (info->attrs[NL80211_ATTR_MAC])
1601 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1602
Johannes Berge31b8212010-10-05 19:39:30 +02001603 if (key.type == -1) {
1604 if (mac_addr)
1605 key.type = NL80211_KEYTYPE_PAIRWISE;
1606 else
1607 key.type = NL80211_KEYTYPE_GROUP;
1608 }
1609
1610 /* for now */
1611 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1612 key.type != NL80211_KEYTYPE_GROUP)
1613 return -EINVAL;
1614
Johannes Berg4c476992010-10-04 21:36:35 +02001615 if (!rdev->ops->add_key)
1616 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001617
Johannes Berge31b8212010-10-05 19:39:30 +02001618 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1619 key.type == NL80211_KEYTYPE_PAIRWISE,
1620 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001621 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001622
1623 wdev_lock(dev->ieee80211_ptr);
1624 err = nl80211_key_allowed(dev->ieee80211_ptr);
1625 if (!err)
1626 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001627 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001628 mac_addr, &key.p);
1629 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001630
Johannes Berg41ade002007-12-19 02:03:29 +01001631 return err;
1632}
1633
1634static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1635{
Johannes Berg4c476992010-10-04 21:36:35 +02001636 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001637 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001638 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001639 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001640 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001641
Johannes Bergb9454e82009-07-08 13:29:08 +02001642 err = nl80211_parse_key(info, &key);
1643 if (err)
1644 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001645
1646 if (info->attrs[NL80211_ATTR_MAC])
1647 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1648
Johannes Berge31b8212010-10-05 19:39:30 +02001649 if (key.type == -1) {
1650 if (mac_addr)
1651 key.type = NL80211_KEYTYPE_PAIRWISE;
1652 else
1653 key.type = NL80211_KEYTYPE_GROUP;
1654 }
1655
1656 /* for now */
1657 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1658 key.type != NL80211_KEYTYPE_GROUP)
1659 return -EINVAL;
1660
Johannes Berg4c476992010-10-04 21:36:35 +02001661 if (!rdev->ops->del_key)
1662 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001663
Johannes Bergfffd0932009-07-08 14:22:54 +02001664 wdev_lock(dev->ieee80211_ptr);
1665 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001666
1667 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1668 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1669 err = -ENOENT;
1670
Johannes Bergfffd0932009-07-08 14:22:54 +02001671 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001672 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1673 key.type == NL80211_KEYTYPE_PAIRWISE,
1674 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001675
Johannes Berg3d23e342009-09-29 23:27:28 +02001676#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001677 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001678 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001679 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001680 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001681 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1682 }
1683#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001684 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001685
Johannes Berg41ade002007-12-19 02:03:29 +01001686 return err;
1687}
1688
Johannes Berged1b6cc2007-12-19 02:03:32 +01001689static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1690{
1691 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1692 struct beacon_parameters *info);
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 struct beacon_parameters params;
1696 int haveinfo = 0;
1697
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001698 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1699 return -EINVAL;
1700
Johannes Berg074ac8d2010-09-16 14:58:22 +02001701 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001702 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1703 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001704
Johannes Berged1b6cc2007-12-19 02:03:32 +01001705 switch (info->genlhdr->cmd) {
1706 case NL80211_CMD_NEW_BEACON:
1707 /* these are required for NEW_BEACON */
1708 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1709 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001710 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1711 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001712
Johannes Berg79c97e92009-07-07 03:56:12 +02001713 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001714 break;
1715 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001716 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001717 break;
1718 default:
1719 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001720 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001721 }
1722
Johannes Berg4c476992010-10-04 21:36:35 +02001723 if (!call)
1724 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001725
1726 memset(&params, 0, sizeof(params));
1727
1728 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1729 params.interval =
1730 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1731 haveinfo = 1;
1732 }
1733
1734 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1735 params.dtim_period =
1736 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1737 haveinfo = 1;
1738 }
1739
1740 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1741 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1742 params.head_len =
1743 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1744 haveinfo = 1;
1745 }
1746
1747 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1748 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1749 params.tail_len =
1750 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1751 haveinfo = 1;
1752 }
1753
Johannes Berg4c476992010-10-04 21:36:35 +02001754 if (!haveinfo)
1755 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001756
Johannes Berg4c476992010-10-04 21:36:35 +02001757 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001758}
1759
1760static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1761{
Johannes Berg4c476992010-10-04 21:36:35 +02001762 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1763 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001764
Johannes Berg4c476992010-10-04 21:36:35 +02001765 if (!rdev->ops->del_beacon)
1766 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001767
Johannes Berg074ac8d2010-09-16 14:58:22 +02001768 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001769 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1770 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001771
Johannes Berg4c476992010-10-04 21:36:35 +02001772 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001773}
1774
Johannes Berg5727ef12007-12-19 02:03:34 +01001775static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1776 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1777 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1778 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001779 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001780};
1781
Johannes Bergeccb8e82009-05-11 21:57:56 +03001782static int parse_station_flags(struct genl_info *info,
1783 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001784{
1785 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001786 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001787 int flag;
1788
Johannes Bergeccb8e82009-05-11 21:57:56 +03001789 /*
1790 * Try parsing the new attribute first so userspace
1791 * can specify both for older kernels.
1792 */
1793 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1794 if (nla) {
1795 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001796
Johannes Bergeccb8e82009-05-11 21:57:56 +03001797 sta_flags = nla_data(nla);
1798 params->sta_flags_mask = sta_flags->mask;
1799 params->sta_flags_set = sta_flags->set;
1800 if ((params->sta_flags_mask |
1801 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1802 return -EINVAL;
1803 return 0;
1804 }
1805
1806 /* if present, parse the old attribute */
1807
1808 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001809 if (!nla)
1810 return 0;
1811
1812 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1813 nla, sta_flags_policy))
1814 return -EINVAL;
1815
Johannes Bergeccb8e82009-05-11 21:57:56 +03001816 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1817 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001818
1819 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1820 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001821 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001822
1823 return 0;
1824}
1825
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001826static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1827 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001828 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001829{
1830 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001831 struct nlattr *sinfoattr, *txrate;
1832 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001833
1834 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1835 if (!hdr)
1836 return -1;
1837
1838 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1839 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1840
Johannes Bergf5ea9122009-08-07 16:17:38 +02001841 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1842
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001843 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1844 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001845 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001846 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1847 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1848 sinfo->inactive_time);
1849 if (sinfo->filled & STATION_INFO_RX_BYTES)
1850 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1851 sinfo->rx_bytes);
1852 if (sinfo->filled & STATION_INFO_TX_BYTES)
1853 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1854 sinfo->tx_bytes);
1855 if (sinfo->filled & STATION_INFO_LLID)
1856 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1857 sinfo->llid);
1858 if (sinfo->filled & STATION_INFO_PLID)
1859 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1860 sinfo->plid);
1861 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1862 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1863 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001864 if (sinfo->filled & STATION_INFO_SIGNAL)
1865 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1866 sinfo->signal);
1867 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1868 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1869 if (!txrate)
1870 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001871
John W. Linville254416a2009-12-09 16:43:52 -05001872 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1873 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001874 if (bitrate > 0)
1875 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1876
1877 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1878 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1879 sinfo->txrate.mcs);
1880 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1881 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1882 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1883 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1884
1885 nla_nest_end(msg, txrate);
1886 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001887 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1888 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1889 sinfo->rx_packets);
1890 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1891 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1892 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001893 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001894
1895 return genlmsg_end(msg, hdr);
1896
1897 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001898 genlmsg_cancel(msg, hdr);
1899 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001900}
1901
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001902static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001903 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001904{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001905 struct station_info sinfo;
1906 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001907 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001908 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001909 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001910 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001911
Johannes Berg67748892010-10-04 21:14:06 +02001912 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1913 if (err)
1914 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001915
1916 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001917 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001918 goto out_err;
1919 }
1920
Johannes Bergbba95fe2008-07-29 13:22:51 +02001921 while (1) {
1922 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1923 mac_addr, &sinfo);
1924 if (err == -ENOENT)
1925 break;
1926 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001927 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001928
1929 if (nl80211_send_station(skb,
1930 NETLINK_CB(cb->skb).pid,
1931 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1932 netdev, mac_addr,
1933 &sinfo) < 0)
1934 goto out;
1935
1936 sta_idx++;
1937 }
1938
1939
1940 out:
1941 cb->args[1] = sta_idx;
1942 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001943 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02001944 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001945
1946 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001947}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001948
Johannes Berg5727ef12007-12-19 02:03:34 +01001949static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1950{
Johannes Berg4c476992010-10-04 21:36:35 +02001951 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1952 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001953 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001954 struct sk_buff *msg;
1955 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02001956 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001957
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001958 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001959
1960 if (!info->attrs[NL80211_ATTR_MAC])
1961 return -EINVAL;
1962
1963 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1964
Johannes Berg4c476992010-10-04 21:36:35 +02001965 if (!rdev->ops->get_station)
1966 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001967
Johannes Berg79c97e92009-07-07 03:56:12 +02001968 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001969 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001970 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001971
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001972 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001973 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001974 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001975
1976 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001977 dev, mac_addr, &sinfo) < 0) {
1978 nlmsg_free(msg);
1979 return -ENOBUFS;
1980 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001981
Johannes Berg4c476992010-10-04 21:36:35 +02001982 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01001983}
1984
1985/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001986 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01001987 */
Johannes Berg463d0182009-07-14 00:33:35 +02001988static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01001989 struct cfg80211_registered_device *rdev,
1990 struct net_device **vlan)
1991{
Johannes Berg463d0182009-07-14 00:33:35 +02001992 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01001993 *vlan = NULL;
1994
1995 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02001996 *vlan = dev_get_by_index(genl_info_net(info),
1997 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01001998 if (!*vlan)
1999 return -ENODEV;
2000 if (!(*vlan)->ieee80211_ptr)
2001 return -EINVAL;
2002 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2003 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002004 if (!netif_running(*vlan))
2005 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002006 }
2007 return 0;
2008}
2009
2010static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2011{
Johannes Berg4c476992010-10-04 21:36:35 +02002012 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002013 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002014 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002015 struct station_parameters params;
2016 u8 *mac_addr = NULL;
2017
2018 memset(&params, 0, sizeof(params));
2019
2020 params.listen_interval = -1;
2021
2022 if (info->attrs[NL80211_ATTR_STA_AID])
2023 return -EINVAL;
2024
2025 if (!info->attrs[NL80211_ATTR_MAC])
2026 return -EINVAL;
2027
2028 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2029
2030 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2031 params.supported_rates =
2032 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2033 params.supported_rates_len =
2034 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2035 }
2036
2037 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2038 params.listen_interval =
2039 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2040
Jouni Malinen36aedc92008-08-25 11:58:58 +03002041 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2042 params.ht_capa =
2043 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2044
Johannes Bergeccb8e82009-05-11 21:57:56 +03002045 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002046 return -EINVAL;
2047
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002048 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2049 params.plink_action =
2050 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2051
Johannes Berg463d0182009-07-14 00:33:35 +02002052 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002053 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002054 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002055
2056 /* validate settings */
2057 err = 0;
2058
2059 switch (dev->ieee80211_ptr->iftype) {
2060 case NL80211_IFTYPE_AP:
2061 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002062 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002063 /* disallow mesh-specific things */
2064 if (params.plink_action)
2065 err = -EINVAL;
2066 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002067 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002068 case NL80211_IFTYPE_STATION:
2069 /* disallow everything but AUTHORIZED flag */
2070 if (params.plink_action)
2071 err = -EINVAL;
2072 if (params.vlan)
2073 err = -EINVAL;
2074 if (params.supported_rates)
2075 err = -EINVAL;
2076 if (params.ht_capa)
2077 err = -EINVAL;
2078 if (params.listen_interval >= 0)
2079 err = -EINVAL;
2080 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2081 err = -EINVAL;
2082 break;
2083 case NL80211_IFTYPE_MESH_POINT:
2084 /* disallow things mesh doesn't support */
2085 if (params.vlan)
2086 err = -EINVAL;
2087 if (params.ht_capa)
2088 err = -EINVAL;
2089 if (params.listen_interval >= 0)
2090 err = -EINVAL;
2091 if (params.supported_rates)
2092 err = -EINVAL;
2093 if (params.sta_flags_mask)
2094 err = -EINVAL;
2095 break;
2096 default:
2097 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002098 }
2099
Johannes Berg5727ef12007-12-19 02:03:34 +01002100 if (err)
2101 goto out;
2102
Johannes Berg79c97e92009-07-07 03:56:12 +02002103 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002104 err = -EOPNOTSUPP;
2105 goto out;
2106 }
2107
Johannes Berg79c97e92009-07-07 03:56:12 +02002108 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002109
2110 out:
2111 if (params.vlan)
2112 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002113
Johannes Berg5727ef12007-12-19 02:03:34 +01002114 return err;
2115}
2116
2117static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2118{
Johannes Berg4c476992010-10-04 21:36:35 +02002119 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002120 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002121 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002122 struct station_parameters params;
2123 u8 *mac_addr = NULL;
2124
2125 memset(&params, 0, sizeof(params));
2126
2127 if (!info->attrs[NL80211_ATTR_MAC])
2128 return -EINVAL;
2129
Johannes Berg5727ef12007-12-19 02:03:34 +01002130 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2131 return -EINVAL;
2132
2133 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2134 return -EINVAL;
2135
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002136 if (!info->attrs[NL80211_ATTR_STA_AID])
2137 return -EINVAL;
2138
Johannes Berg5727ef12007-12-19 02:03:34 +01002139 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2140 params.supported_rates =
2141 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2142 params.supported_rates_len =
2143 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2144 params.listen_interval =
2145 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002146
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002147 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2148 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2149 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002150
Jouni Malinen36aedc92008-08-25 11:58:58 +03002151 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2152 params.ht_capa =
2153 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002154
Johannes Bergeccb8e82009-05-11 21:57:56 +03002155 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002156 return -EINVAL;
2157
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002158 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002159 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02002160 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2161 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002162
Johannes Berg463d0182009-07-14 00:33:35 +02002163 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002164 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002165 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002166
2167 /* validate settings */
2168 err = 0;
2169
Johannes Berg79c97e92009-07-07 03:56:12 +02002170 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002171 err = -EOPNOTSUPP;
2172 goto out;
2173 }
2174
Johannes Berg79c97e92009-07-07 03:56:12 +02002175 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002176
2177 out:
2178 if (params.vlan)
2179 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002180 return err;
2181}
2182
2183static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2184{
Johannes Berg4c476992010-10-04 21:36:35 +02002185 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2186 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002187 u8 *mac_addr = NULL;
2188
2189 if (info->attrs[NL80211_ATTR_MAC])
2190 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2191
Johannes Berge80cf852009-05-11 14:43:13 +02002192 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002193 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002194 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002195 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2196 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002197
Johannes Berg4c476992010-10-04 21:36:35 +02002198 if (!rdev->ops->del_station)
2199 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002200
Johannes Berg4c476992010-10-04 21:36:35 +02002201 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002202}
2203
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002204static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2205 int flags, struct net_device *dev,
2206 u8 *dst, u8 *next_hop,
2207 struct mpath_info *pinfo)
2208{
2209 void *hdr;
2210 struct nlattr *pinfoattr;
2211
2212 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2213 if (!hdr)
2214 return -1;
2215
2216 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2217 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2218 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2219
Johannes Bergf5ea9122009-08-07 16:17:38 +02002220 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2221
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002222 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2223 if (!pinfoattr)
2224 goto nla_put_failure;
2225 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2226 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2227 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002228 if (pinfo->filled & MPATH_INFO_SN)
2229 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2230 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002231 if (pinfo->filled & MPATH_INFO_METRIC)
2232 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2233 pinfo->metric);
2234 if (pinfo->filled & MPATH_INFO_EXPTIME)
2235 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2236 pinfo->exptime);
2237 if (pinfo->filled & MPATH_INFO_FLAGS)
2238 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2239 pinfo->flags);
2240 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2241 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2242 pinfo->discovery_timeout);
2243 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2244 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2245 pinfo->discovery_retries);
2246
2247 nla_nest_end(msg, pinfoattr);
2248
2249 return genlmsg_end(msg, hdr);
2250
2251 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002252 genlmsg_cancel(msg, hdr);
2253 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002254}
2255
2256static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002257 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002258{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002259 struct mpath_info pinfo;
2260 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002261 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002262 u8 dst[ETH_ALEN];
2263 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002264 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002265 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002266
Johannes Berg67748892010-10-04 21:14:06 +02002267 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2268 if (err)
2269 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002270
2271 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002272 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002273 goto out_err;
2274 }
2275
Jouni Malineneec60b02009-03-20 21:21:19 +02002276 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2277 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002278 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002279 }
2280
Johannes Bergbba95fe2008-07-29 13:22:51 +02002281 while (1) {
2282 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2283 dst, next_hop, &pinfo);
2284 if (err == -ENOENT)
2285 break;
2286 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002287 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002288
2289 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2290 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2291 netdev, dst, next_hop,
2292 &pinfo) < 0)
2293 goto out;
2294
2295 path_idx++;
2296 }
2297
2298
2299 out:
2300 cb->args[1] = path_idx;
2301 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002302 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002303 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002304 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002305}
2306
2307static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2308{
Johannes Berg4c476992010-10-04 21:36:35 +02002309 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002310 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002311 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002312 struct mpath_info pinfo;
2313 struct sk_buff *msg;
2314 u8 *dst = NULL;
2315 u8 next_hop[ETH_ALEN];
2316
2317 memset(&pinfo, 0, sizeof(pinfo));
2318
2319 if (!info->attrs[NL80211_ATTR_MAC])
2320 return -EINVAL;
2321
2322 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2323
Johannes Berg4c476992010-10-04 21:36:35 +02002324 if (!rdev->ops->get_mpath)
2325 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002326
Johannes Berg4c476992010-10-04 21:36:35 +02002327 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2328 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002329
Johannes Berg79c97e92009-07-07 03:56:12 +02002330 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002331 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002332 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002333
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002334 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002335 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002336 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002337
2338 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002339 dev, dst, next_hop, &pinfo) < 0) {
2340 nlmsg_free(msg);
2341 return -ENOBUFS;
2342 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002343
Johannes Berg4c476992010-10-04 21:36:35 +02002344 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002345}
2346
2347static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2348{
Johannes Berg4c476992010-10-04 21:36:35 +02002349 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2350 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002351 u8 *dst = NULL;
2352 u8 *next_hop = NULL;
2353
2354 if (!info->attrs[NL80211_ATTR_MAC])
2355 return -EINVAL;
2356
2357 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2358 return -EINVAL;
2359
2360 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2361 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2362
Johannes Berg4c476992010-10-04 21:36:35 +02002363 if (!rdev->ops->change_mpath)
2364 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002365
Johannes Berg4c476992010-10-04 21:36:35 +02002366 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2367 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002368
Johannes Berg4c476992010-10-04 21:36:35 +02002369 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002370}
Johannes Berg4c476992010-10-04 21:36:35 +02002371
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002372static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2373{
Johannes Berg4c476992010-10-04 21:36:35 +02002374 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2375 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002376 u8 *dst = NULL;
2377 u8 *next_hop = NULL;
2378
2379 if (!info->attrs[NL80211_ATTR_MAC])
2380 return -EINVAL;
2381
2382 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2383 return -EINVAL;
2384
2385 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2386 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2387
Johannes Berg4c476992010-10-04 21:36:35 +02002388 if (!rdev->ops->add_mpath)
2389 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002390
Johannes Berg4c476992010-10-04 21:36:35 +02002391 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2392 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002393
Johannes Berg4c476992010-10-04 21:36:35 +02002394 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002395}
2396
2397static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2398{
Johannes Berg4c476992010-10-04 21:36:35 +02002399 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2400 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002401 u8 *dst = NULL;
2402
2403 if (info->attrs[NL80211_ATTR_MAC])
2404 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2405
Johannes Berg4c476992010-10-04 21:36:35 +02002406 if (!rdev->ops->del_mpath)
2407 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002408
Johannes Berg4c476992010-10-04 21:36:35 +02002409 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002410}
2411
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002412static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2413{
Johannes Berg4c476992010-10-04 21:36:35 +02002414 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2415 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002416 struct bss_parameters params;
2417
2418 memset(&params, 0, sizeof(params));
2419 /* default to not changing parameters */
2420 params.use_cts_prot = -1;
2421 params.use_short_preamble = -1;
2422 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002423 params.ap_isolate = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002424
2425 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2426 params.use_cts_prot =
2427 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2428 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2429 params.use_short_preamble =
2430 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2431 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2432 params.use_short_slot_time =
2433 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002434 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2435 params.basic_rates =
2436 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2437 params.basic_rates_len =
2438 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2439 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002440 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2441 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002442
Johannes Berg4c476992010-10-04 21:36:35 +02002443 if (!rdev->ops->change_bss)
2444 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002445
Johannes Berg074ac8d2010-09-16 14:58:22 +02002446 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002447 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2448 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002449
Johannes Berg4c476992010-10-04 21:36:35 +02002450 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002451}
2452
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002453static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002454 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2455 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2456 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2457 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2458 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2459 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2460};
2461
2462static int parse_reg_rule(struct nlattr *tb[],
2463 struct ieee80211_reg_rule *reg_rule)
2464{
2465 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2466 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2467
2468 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2469 return -EINVAL;
2470 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2471 return -EINVAL;
2472 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2473 return -EINVAL;
2474 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2475 return -EINVAL;
2476 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2477 return -EINVAL;
2478
2479 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2480
2481 freq_range->start_freq_khz =
2482 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2483 freq_range->end_freq_khz =
2484 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2485 freq_range->max_bandwidth_khz =
2486 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2487
2488 power_rule->max_eirp =
2489 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2490
2491 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2492 power_rule->max_antenna_gain =
2493 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2494
2495 return 0;
2496}
2497
2498static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2499{
2500 int r;
2501 char *data = NULL;
2502
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002503 /*
2504 * You should only get this when cfg80211 hasn't yet initialized
2505 * completely when built-in to the kernel right between the time
2506 * window between nl80211_init() and regulatory_init(), if that is
2507 * even possible.
2508 */
2509 mutex_lock(&cfg80211_mutex);
2510 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002511 mutex_unlock(&cfg80211_mutex);
2512 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002513 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002514 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002515
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002516 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2517 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002518
2519 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2520
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002521 r = regulatory_hint_user(data);
2522
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002523 return r;
2524}
2525
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002526static int nl80211_get_mesh_params(struct sk_buff *skb,
2527 struct genl_info *info)
2528{
Johannes Berg4c476992010-10-04 21:36:35 +02002529 struct cfg80211_registered_device *rdev = info->user_ptr[0];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002530 struct mesh_config cur_params;
2531 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002532 struct net_device *dev = info->user_ptr[1];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002533 void *hdr;
2534 struct nlattr *pinfoattr;
2535 struct sk_buff *msg;
2536
Johannes Berg4c476992010-10-04 21:36:35 +02002537 if (!rdev->ops->get_mesh_params)
2538 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002539
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002540 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002541 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002542 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002543 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002544
2545 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002546 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002547 if (!msg)
2548 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002549 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2550 NL80211_CMD_GET_MESH_PARAMS);
2551 if (!hdr)
2552 goto nla_put_failure;
2553 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2554 if (!pinfoattr)
2555 goto nla_put_failure;
2556 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2557 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2558 cur_params.dot11MeshRetryTimeout);
2559 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2560 cur_params.dot11MeshConfirmTimeout);
2561 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2562 cur_params.dot11MeshHoldingTimeout);
2563 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2564 cur_params.dot11MeshMaxPeerLinks);
2565 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2566 cur_params.dot11MeshMaxRetries);
2567 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2568 cur_params.dot11MeshTTL);
2569 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2570 cur_params.auto_open_plinks);
2571 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2572 cur_params.dot11MeshHWMPmaxPREQretries);
2573 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2574 cur_params.path_refresh_time);
2575 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2576 cur_params.min_discovery_timeout);
2577 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2578 cur_params.dot11MeshHWMPactivePathTimeout);
2579 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2580 cur_params.dot11MeshHWMPpreqMinInterval);
2581 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2582 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002583 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2584 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002585 nla_nest_end(msg, pinfoattr);
2586 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002587 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002588
Johannes Berg3b858752009-03-12 09:55:09 +01002589 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002590 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002591 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002592 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002593}
2594
2595#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2596do {\
2597 if (table[attr_num]) {\
2598 cfg.param = nla_fn(table[attr_num]); \
2599 mask |= (1 << (attr_num - 1)); \
2600 } \
2601} while (0);\
2602
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002603static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002604 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2605 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2606 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2607 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2608 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2609 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2610 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2611
2612 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2613 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2614 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2615 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2616 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2617 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2618};
2619
2620static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2621{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002622 u32 mask;
Johannes Berg4c476992010-10-04 21:36:35 +02002623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2624 struct net_device *dev = info->user_ptr[1];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002625 struct mesh_config cfg;
2626 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2627 struct nlattr *parent_attr;
2628
2629 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2630 if (!parent_attr)
2631 return -EINVAL;
2632 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2633 parent_attr, nl80211_meshconf_params_policy))
2634 return -EINVAL;
2635
Johannes Berg4c476992010-10-04 21:36:35 +02002636 if (!rdev->ops->set_mesh_params)
2637 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002638
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002639 /* This makes sure that there aren't more than 32 mesh config
2640 * parameters (otherwise our bitfield scheme would not work.) */
2641 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2642
2643 /* Fill in the params struct */
2644 mask = 0;
2645 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2646 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2647 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2648 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2649 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2650 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2651 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2652 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2653 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2654 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2655 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2656 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2657 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2658 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2659 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2660 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2661 nla_get_u8);
2662 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2663 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2664 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2665 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2666 nla_get_u16);
2667 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2668 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2669 nla_get_u32);
2670 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2671 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2672 nla_get_u16);
2673 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2674 dot11MeshHWMPnetDiameterTraversalTime,
2675 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2676 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002677 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2678 dot11MeshHWMPRootMode, mask,
2679 NL80211_MESHCONF_HWMP_ROOTMODE,
2680 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002681
2682 /* Apply changes */
Johannes Berg4c476992010-10-04 21:36:35 +02002683 return rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002684}
2685
2686#undef FILL_IN_MESH_PARAM_IF_SET
2687
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002688static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2689{
2690 struct sk_buff *msg;
2691 void *hdr = NULL;
2692 struct nlattr *nl_reg_rules;
2693 unsigned int i;
2694 int err = -EINVAL;
2695
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002696 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002697
2698 if (!cfg80211_regdomain)
2699 goto out;
2700
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002701 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002702 if (!msg) {
2703 err = -ENOBUFS;
2704 goto out;
2705 }
2706
2707 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2708 NL80211_CMD_GET_REG);
2709 if (!hdr)
2710 goto nla_put_failure;
2711
2712 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2713 cfg80211_regdomain->alpha2);
2714
2715 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2716 if (!nl_reg_rules)
2717 goto nla_put_failure;
2718
2719 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2720 struct nlattr *nl_reg_rule;
2721 const struct ieee80211_reg_rule *reg_rule;
2722 const struct ieee80211_freq_range *freq_range;
2723 const struct ieee80211_power_rule *power_rule;
2724
2725 reg_rule = &cfg80211_regdomain->reg_rules[i];
2726 freq_range = &reg_rule->freq_range;
2727 power_rule = &reg_rule->power_rule;
2728
2729 nl_reg_rule = nla_nest_start(msg, i);
2730 if (!nl_reg_rule)
2731 goto nla_put_failure;
2732
2733 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2734 reg_rule->flags);
2735 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2736 freq_range->start_freq_khz);
2737 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2738 freq_range->end_freq_khz);
2739 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2740 freq_range->max_bandwidth_khz);
2741 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2742 power_rule->max_antenna_gain);
2743 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2744 power_rule->max_eirp);
2745
2746 nla_nest_end(msg, nl_reg_rule);
2747 }
2748
2749 nla_nest_end(msg, nl_reg_rules);
2750
2751 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002752 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002753 goto out;
2754
2755nla_put_failure:
2756 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002757 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002758 err = -EMSGSIZE;
2759out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002760 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002761 return err;
2762}
2763
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002764static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2765{
2766 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2767 struct nlattr *nl_reg_rule;
2768 char *alpha2 = NULL;
2769 int rem_reg_rules = 0, r = 0;
2770 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2771 struct ieee80211_regdomain *rd = NULL;
2772
2773 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2774 return -EINVAL;
2775
2776 if (!info->attrs[NL80211_ATTR_REG_RULES])
2777 return -EINVAL;
2778
2779 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2780
2781 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2782 rem_reg_rules) {
2783 num_rules++;
2784 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002785 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002786 }
2787
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002788 mutex_lock(&cfg80211_mutex);
2789
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002790 if (!reg_is_valid_request(alpha2)) {
2791 r = -EINVAL;
2792 goto bad_reg;
2793 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002794
2795 size_of_regd = sizeof(struct ieee80211_regdomain) +
2796 (num_rules * sizeof(struct ieee80211_reg_rule));
2797
2798 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002799 if (!rd) {
2800 r = -ENOMEM;
2801 goto bad_reg;
2802 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002803
2804 rd->n_reg_rules = num_rules;
2805 rd->alpha2[0] = alpha2[0];
2806 rd->alpha2[1] = alpha2[1];
2807
2808 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2809 rem_reg_rules) {
2810 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2811 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2812 reg_rule_policy);
2813 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2814 if (r)
2815 goto bad_reg;
2816
2817 rule_idx++;
2818
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002819 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2820 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002821 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002822 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002823 }
2824
2825 BUG_ON(rule_idx != num_rules);
2826
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002827 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002828
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002829 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002830
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002831 return r;
2832
Johannes Bergd2372b32008-10-24 20:32:20 +02002833 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002834 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002835 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002836 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002837}
2838
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002839static int validate_scan_freqs(struct nlattr *freqs)
2840{
2841 struct nlattr *attr1, *attr2;
2842 int n_channels = 0, tmp1, tmp2;
2843
2844 nla_for_each_nested(attr1, freqs, tmp1) {
2845 n_channels++;
2846 /*
2847 * Some hardware has a limited channel list for
2848 * scanning, and it is pretty much nonsensical
2849 * to scan for a channel twice, so disallow that
2850 * and don't require drivers to check that the
2851 * channel list they get isn't longer than what
2852 * they can scan, as long as they can scan all
2853 * the channels they registered at once.
2854 */
2855 nla_for_each_nested(attr2, freqs, tmp2)
2856 if (attr1 != attr2 &&
2857 nla_get_u32(attr1) == nla_get_u32(attr2))
2858 return 0;
2859 }
2860
2861 return n_channels;
2862}
2863
Johannes Berg2a519312009-02-10 21:25:55 +01002864static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2865{
Johannes Berg4c476992010-10-04 21:36:35 +02002866 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2867 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01002868 struct cfg80211_scan_request *request;
2869 struct cfg80211_ssid *ssid;
2870 struct ieee80211_channel *channel;
2871 struct nlattr *attr;
2872 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002873 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002874 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002875 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002876
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002877 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2878 return -EINVAL;
2879
Johannes Berg79c97e92009-07-07 03:56:12 +02002880 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002881
Johannes Berg4c476992010-10-04 21:36:35 +02002882 if (!rdev->ops->scan)
2883 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01002884
Johannes Berg4c476992010-10-04 21:36:35 +02002885 if (rdev->scan_req)
2886 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01002887
2888 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002889 n_channels = validate_scan_freqs(
2890 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02002891 if (!n_channels)
2892 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002893 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002894 n_channels = 0;
2895
Johannes Berg2a519312009-02-10 21:25:55 +01002896 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2897 if (wiphy->bands[band])
2898 n_channels += wiphy->bands[band]->n_channels;
2899 }
2900
2901 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2902 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2903 n_ssids++;
2904
Johannes Berg4c476992010-10-04 21:36:35 +02002905 if (n_ssids > wiphy->max_scan_ssids)
2906 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002907
Jouni Malinen70692ad2009-02-16 19:39:13 +02002908 if (info->attrs[NL80211_ATTR_IE])
2909 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2910 else
2911 ie_len = 0;
2912
Johannes Berg4c476992010-10-04 21:36:35 +02002913 if (ie_len > wiphy->max_scan_ie_len)
2914 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02002915
Johannes Berg2a519312009-02-10 21:25:55 +01002916 request = kzalloc(sizeof(*request)
2917 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002918 + sizeof(channel) * n_channels
2919 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002920 if (!request)
2921 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01002922
Johannes Berg2a519312009-02-10 21:25:55 +01002923 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02002924 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01002925 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002926 if (ie_len) {
2927 if (request->ssids)
2928 request->ie = (void *)(request->ssids + n_ssids);
2929 else
2930 request->ie = (void *)(request->channels + n_channels);
2931 }
Johannes Berg2a519312009-02-10 21:25:55 +01002932
Johannes Berg584991d2009-11-02 13:32:03 +01002933 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01002934 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2935 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01002936 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01002937 struct ieee80211_channel *chan;
2938
2939 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2940
2941 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002942 err = -EINVAL;
2943 goto out_free;
2944 }
Johannes Berg584991d2009-11-02 13:32:03 +01002945
2946 /* ignore disabled channels */
2947 if (chan->flags & IEEE80211_CHAN_DISABLED)
2948 continue;
2949
2950 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01002951 i++;
2952 }
2953 } else {
2954 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01002955 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2956 int j;
2957 if (!wiphy->bands[band])
2958 continue;
2959 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01002960 struct ieee80211_channel *chan;
2961
2962 chan = &wiphy->bands[band]->channels[j];
2963
2964 if (chan->flags & IEEE80211_CHAN_DISABLED)
2965 continue;
2966
2967 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01002968 i++;
2969 }
2970 }
2971 }
2972
Johannes Berg584991d2009-11-02 13:32:03 +01002973 if (!i) {
2974 err = -EINVAL;
2975 goto out_free;
2976 }
2977
2978 request->n_channels = i;
2979
Johannes Berg2a519312009-02-10 21:25:55 +01002980 i = 0;
2981 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2982 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2983 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2984 err = -EINVAL;
2985 goto out_free;
2986 }
2987 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2988 request->ssids[i].ssid_len = nla_len(attr);
2989 i++;
2990 }
2991 }
2992
Jouni Malinen70692ad2009-02-16 19:39:13 +02002993 if (info->attrs[NL80211_ATTR_IE]) {
2994 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02002995 memcpy((void *)request->ie,
2996 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02002997 request->ie_len);
2998 }
2999
Johannes Berg463d0182009-07-14 00:33:35 +02003000 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003001 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003002
Johannes Berg79c97e92009-07-07 03:56:12 +02003003 rdev->scan_req = request;
3004 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003005
Johannes Berg463d0182009-07-14 00:33:35 +02003006 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003007 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003008 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003009 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003010 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003011 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003012 kfree(request);
3013 }
Johannes Berg3b858752009-03-12 09:55:09 +01003014
Johannes Berg2a519312009-02-10 21:25:55 +01003015 return err;
3016}
3017
3018static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3019 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003020 struct wireless_dev *wdev,
3021 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003022{
Johannes Berg48ab9052009-07-10 18:42:31 +02003023 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003024 void *hdr;
3025 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003026 int i;
3027
3028 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003029
3030 hdr = nl80211hdr_put(msg, pid, seq, flags,
3031 NL80211_CMD_NEW_SCAN_RESULTS);
3032 if (!hdr)
3033 return -1;
3034
Johannes Bergf5ea9122009-08-07 16:17:38 +02003035 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003036 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003037
3038 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3039 if (!bss)
3040 goto nla_put_failure;
3041 if (!is_zero_ether_addr(res->bssid))
3042 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3043 if (res->information_elements && res->len_information_elements)
3044 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3045 res->len_information_elements,
3046 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003047 if (res->beacon_ies && res->len_beacon_ies &&
3048 res->beacon_ies != res->information_elements)
3049 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3050 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003051 if (res->tsf)
3052 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3053 if (res->beacon_interval)
3054 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3055 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3056 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003057 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3058 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003059
Johannes Berg77965c92009-02-18 18:45:06 +01003060 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003061 case CFG80211_SIGNAL_TYPE_MBM:
3062 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3063 break;
3064 case CFG80211_SIGNAL_TYPE_UNSPEC:
3065 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3066 break;
3067 default:
3068 break;
3069 }
3070
Johannes Berg48ab9052009-07-10 18:42:31 +02003071 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003072 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003073 case NL80211_IFTYPE_STATION:
3074 if (intbss == wdev->current_bss)
3075 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3076 NL80211_BSS_STATUS_ASSOCIATED);
3077 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3078 if (intbss != wdev->auth_bsses[i])
3079 continue;
3080 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3081 NL80211_BSS_STATUS_AUTHENTICATED);
3082 break;
3083 }
3084 break;
3085 case NL80211_IFTYPE_ADHOC:
3086 if (intbss == wdev->current_bss)
3087 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3088 NL80211_BSS_STATUS_IBSS_JOINED);
3089 break;
3090 default:
3091 break;
3092 }
3093
Johannes Berg2a519312009-02-10 21:25:55 +01003094 nla_nest_end(msg, bss);
3095
3096 return genlmsg_end(msg, hdr);
3097
3098 nla_put_failure:
3099 genlmsg_cancel(msg, hdr);
3100 return -EMSGSIZE;
3101}
3102
3103static int nl80211_dump_scan(struct sk_buff *skb,
3104 struct netlink_callback *cb)
3105{
Johannes Berg48ab9052009-07-10 18:42:31 +02003106 struct cfg80211_registered_device *rdev;
3107 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003108 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003109 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003110 int start = cb->args[1], idx = 0;
3111 int err;
3112
Johannes Berg67748892010-10-04 21:14:06 +02003113 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3114 if (err)
3115 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003116
Johannes Berg48ab9052009-07-10 18:42:31 +02003117 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003118
Johannes Berg48ab9052009-07-10 18:42:31 +02003119 wdev_lock(wdev);
3120 spin_lock_bh(&rdev->bss_lock);
3121 cfg80211_bss_expire(rdev);
3122
3123 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003124 if (++idx <= start)
3125 continue;
3126 if (nl80211_send_bss(skb,
3127 NETLINK_CB(cb->skb).pid,
3128 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003129 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003130 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003131 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003132 }
3133 }
3134
Johannes Berg48ab9052009-07-10 18:42:31 +02003135 spin_unlock_bh(&rdev->bss_lock);
3136 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003137
3138 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003139 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003140
Johannes Berg67748892010-10-04 21:14:06 +02003141 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003142}
3143
Holger Schurig61fa7132009-11-11 12:25:40 +01003144static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3145 int flags, struct net_device *dev,
3146 struct survey_info *survey)
3147{
3148 void *hdr;
3149 struct nlattr *infoattr;
3150
3151 /* Survey without a channel doesn't make sense */
3152 if (!survey->channel)
3153 return -EINVAL;
3154
3155 hdr = nl80211hdr_put(msg, pid, seq, flags,
3156 NL80211_CMD_NEW_SURVEY_RESULTS);
3157 if (!hdr)
3158 return -ENOMEM;
3159
3160 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3161
3162 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3163 if (!infoattr)
3164 goto nla_put_failure;
3165
3166 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3167 survey->channel->center_freq);
3168 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3169 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3170 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003171 if (survey->filled & SURVEY_INFO_IN_USE)
3172 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Holger Schurig61fa7132009-11-11 12:25:40 +01003173
3174 nla_nest_end(msg, infoattr);
3175
3176 return genlmsg_end(msg, hdr);
3177
3178 nla_put_failure:
3179 genlmsg_cancel(msg, hdr);
3180 return -EMSGSIZE;
3181}
3182
3183static int nl80211_dump_survey(struct sk_buff *skb,
3184 struct netlink_callback *cb)
3185{
3186 struct survey_info survey;
3187 struct cfg80211_registered_device *dev;
3188 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003189 int survey_idx = cb->args[1];
3190 int res;
3191
Johannes Berg67748892010-10-04 21:14:06 +02003192 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3193 if (res)
3194 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003195
3196 if (!dev->ops->dump_survey) {
3197 res = -EOPNOTSUPP;
3198 goto out_err;
3199 }
3200
3201 while (1) {
3202 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3203 &survey);
3204 if (res == -ENOENT)
3205 break;
3206 if (res)
3207 goto out_err;
3208
3209 if (nl80211_send_survey(skb,
3210 NETLINK_CB(cb->skb).pid,
3211 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3212 netdev,
3213 &survey) < 0)
3214 goto out;
3215 survey_idx++;
3216 }
3217
3218 out:
3219 cb->args[1] = survey_idx;
3220 res = skb->len;
3221 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003222 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003223 return res;
3224}
3225
Jouni Malinen255e7372009-03-20 21:21:17 +02003226static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3227{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003228 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003229}
3230
Samuel Ortizb23aa672009-07-01 21:26:54 +02003231static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3232{
3233 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3234 NL80211_WPA_VERSION_2));
3235}
3236
3237static bool nl80211_valid_akm_suite(u32 akm)
3238{
3239 return akm == WLAN_AKM_SUITE_8021X ||
3240 akm == WLAN_AKM_SUITE_PSK;
3241}
3242
3243static bool nl80211_valid_cipher_suite(u32 cipher)
3244{
3245 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3246 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3247 cipher == WLAN_CIPHER_SUITE_TKIP ||
3248 cipher == WLAN_CIPHER_SUITE_CCMP ||
3249 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3250}
3251
3252
Jouni Malinen636a5d32009-03-19 13:39:22 +02003253static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3254{
Johannes Berg4c476992010-10-04 21:36:35 +02003255 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3256 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003257 struct ieee80211_channel *chan;
3258 const u8 *bssid, *ssid, *ie = NULL;
3259 int err, ssid_len, ie_len = 0;
3260 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003261 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003262 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003263
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003264 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3265 return -EINVAL;
3266
3267 if (!info->attrs[NL80211_ATTR_MAC])
3268 return -EINVAL;
3269
Jouni Malinen17780922009-03-27 20:52:47 +02003270 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3271 return -EINVAL;
3272
Johannes Berg19957bb2009-07-02 17:20:43 +02003273 if (!info->attrs[NL80211_ATTR_SSID])
3274 return -EINVAL;
3275
3276 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3277 return -EINVAL;
3278
Johannes Bergfffd0932009-07-08 14:22:54 +02003279 err = nl80211_parse_key(info, &key);
3280 if (err)
3281 return err;
3282
3283 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003284 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3285 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003286 if (!key.p.key || !key.p.key_len)
3287 return -EINVAL;
3288 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3289 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3290 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3291 key.p.key_len != WLAN_KEY_LEN_WEP104))
3292 return -EINVAL;
3293 if (key.idx > 4)
3294 return -EINVAL;
3295 } else {
3296 key.p.key_len = 0;
3297 key.p.key = NULL;
3298 }
3299
Johannes Bergafea0b72010-08-10 09:46:42 +02003300 if (key.idx >= 0) {
3301 int i;
3302 bool ok = false;
3303 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3304 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3305 ok = true;
3306 break;
3307 }
3308 }
Johannes Berg4c476992010-10-04 21:36:35 +02003309 if (!ok)
3310 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003311 }
3312
Johannes Berg4c476992010-10-04 21:36:35 +02003313 if (!rdev->ops->auth)
3314 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003315
Johannes Berg074ac8d2010-09-16 14:58:22 +02003316 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003317 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3318 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003319
Johannes Berg19957bb2009-07-02 17:20:43 +02003320 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003321 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003322 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003323 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3324 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003325
Johannes Berg19957bb2009-07-02 17:20:43 +02003326 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3327 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3328
3329 if (info->attrs[NL80211_ATTR_IE]) {
3330 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3331 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3332 }
3333
3334 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003335 if (!nl80211_valid_auth_type(auth_type))
3336 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003337
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003338 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3339
Johannes Berg4c476992010-10-04 21:36:35 +02003340 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3341 ssid, ssid_len, ie, ie_len,
3342 key.p.key, key.p.key_len, key.idx,
3343 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003344}
3345
Johannes Bergc0692b82010-08-27 14:26:53 +03003346static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3347 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003348 struct cfg80211_crypto_settings *settings,
3349 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003350{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003351 memset(settings, 0, sizeof(*settings));
3352
Samuel Ortizb23aa672009-07-01 21:26:54 +02003353 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3354
Johannes Bergc0692b82010-08-27 14:26:53 +03003355 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3356 u16 proto;
3357 proto = nla_get_u16(
3358 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3359 settings->control_port_ethertype = cpu_to_be16(proto);
3360 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3361 proto != ETH_P_PAE)
3362 return -EINVAL;
3363 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3364 settings->control_port_no_encrypt = true;
3365 } else
3366 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3367
Samuel Ortizb23aa672009-07-01 21:26:54 +02003368 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3369 void *data;
3370 int len, i;
3371
3372 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3373 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3374 settings->n_ciphers_pairwise = len / sizeof(u32);
3375
3376 if (len % sizeof(u32))
3377 return -EINVAL;
3378
Johannes Berg3dc27d22009-07-02 21:36:37 +02003379 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003380 return -EINVAL;
3381
3382 memcpy(settings->ciphers_pairwise, data, len);
3383
3384 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3385 if (!nl80211_valid_cipher_suite(
3386 settings->ciphers_pairwise[i]))
3387 return -EINVAL;
3388 }
3389
3390 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3391 settings->cipher_group =
3392 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3393 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3394 return -EINVAL;
3395 }
3396
3397 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3398 settings->wpa_versions =
3399 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3400 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3401 return -EINVAL;
3402 }
3403
3404 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3405 void *data;
3406 int len, i;
3407
3408 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3409 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3410 settings->n_akm_suites = len / sizeof(u32);
3411
3412 if (len % sizeof(u32))
3413 return -EINVAL;
3414
3415 memcpy(settings->akm_suites, data, len);
3416
3417 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3418 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3419 return -EINVAL;
3420 }
3421
3422 return 0;
3423}
3424
Jouni Malinen636a5d32009-03-19 13:39:22 +02003425static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3426{
Johannes Berg4c476992010-10-04 21:36:35 +02003427 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3428 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003429 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003430 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003431 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003432 int err, ssid_len, ie_len = 0;
3433 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003434
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003435 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3436 return -EINVAL;
3437
3438 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003439 !info->attrs[NL80211_ATTR_SSID] ||
3440 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003441 return -EINVAL;
3442
Johannes Berg4c476992010-10-04 21:36:35 +02003443 if (!rdev->ops->assoc)
3444 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003445
Johannes Berg074ac8d2010-09-16 14:58:22 +02003446 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003447 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3448 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003449
Johannes Berg19957bb2009-07-02 17:20:43 +02003450 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003451
Johannes Berg19957bb2009-07-02 17:20:43 +02003452 chan = ieee80211_get_channel(&rdev->wiphy,
3453 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003454 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3455 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003456
Johannes Berg19957bb2009-07-02 17:20:43 +02003457 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3458 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003459
3460 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003461 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3462 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003463 }
3464
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003465 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003466 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003467 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003468 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003469 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02003470 else if (mfp != NL80211_MFP_NO)
3471 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003472 }
3473
Johannes Berg3e5d7642009-07-07 14:37:26 +02003474 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3475 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3476
Johannes Bergc0692b82010-08-27 14:26:53 +03003477 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003478 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003479 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3480 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003481 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003482
Jouni Malinen636a5d32009-03-19 13:39:22 +02003483 return err;
3484}
3485
3486static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3487{
Johannes Berg4c476992010-10-04 21:36:35 +02003488 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3489 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003490 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003491 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003492 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003493 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003494
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003495 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3496 return -EINVAL;
3497
3498 if (!info->attrs[NL80211_ATTR_MAC])
3499 return -EINVAL;
3500
3501 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3502 return -EINVAL;
3503
Johannes Berg4c476992010-10-04 21:36:35 +02003504 if (!rdev->ops->deauth)
3505 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003506
Johannes Berg074ac8d2010-09-16 14:58:22 +02003507 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003508 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3509 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003510
Johannes Berg19957bb2009-07-02 17:20:43 +02003511 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003512
Johannes Berg19957bb2009-07-02 17:20:43 +02003513 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3514 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003515 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003516 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003517 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003518
3519 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003520 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3521 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003522 }
3523
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003524 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3525
Johannes Berg4c476992010-10-04 21:36:35 +02003526 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3527 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003528}
3529
3530static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3531{
Johannes Berg4c476992010-10-04 21:36:35 +02003532 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3533 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003534 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003535 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003536 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003537 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003538
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003539 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3540 return -EINVAL;
3541
3542 if (!info->attrs[NL80211_ATTR_MAC])
3543 return -EINVAL;
3544
3545 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3546 return -EINVAL;
3547
Johannes Berg4c476992010-10-04 21:36:35 +02003548 if (!rdev->ops->disassoc)
3549 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003550
Johannes Berg074ac8d2010-09-16 14:58:22 +02003551 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003552 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3553 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003554
Johannes Berg19957bb2009-07-02 17:20:43 +02003555 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003556
Johannes Berg19957bb2009-07-02 17:20:43 +02003557 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3558 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003559 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003560 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003561 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003562
3563 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003564 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3565 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003566 }
3567
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003568 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3569
Johannes Berg4c476992010-10-04 21:36:35 +02003570 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3571 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003572}
3573
Johannes Berg04a773a2009-04-19 21:24:32 +02003574static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3575{
Johannes Berg4c476992010-10-04 21:36:35 +02003576 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3577 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003578 struct cfg80211_ibss_params ibss;
3579 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003580 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003581 int err;
3582
Johannes Berg8e30bc52009-04-22 17:45:38 +02003583 memset(&ibss, 0, sizeof(ibss));
3584
Johannes Berg04a773a2009-04-19 21:24:32 +02003585 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3586 return -EINVAL;
3587
3588 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3589 !info->attrs[NL80211_ATTR_SSID] ||
3590 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3591 return -EINVAL;
3592
Johannes Berg8e30bc52009-04-22 17:45:38 +02003593 ibss.beacon_interval = 100;
3594
3595 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3596 ibss.beacon_interval =
3597 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3598 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3599 return -EINVAL;
3600 }
3601
Johannes Berg4c476992010-10-04 21:36:35 +02003602 if (!rdev->ops->join_ibss)
3603 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003604
Johannes Berg4c476992010-10-04 21:36:35 +02003605 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3606 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003607
Johannes Berg79c97e92009-07-07 03:56:12 +02003608 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003609
3610 if (info->attrs[NL80211_ATTR_MAC])
3611 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3612 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3613 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3614
3615 if (info->attrs[NL80211_ATTR_IE]) {
3616 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3617 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3618 }
3619
3620 ibss.channel = ieee80211_get_channel(wiphy,
3621 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3622 if (!ibss.channel ||
3623 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02003624 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3625 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003626
3627 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003628 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003629
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003630 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3631 u8 *rates =
3632 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3633 int n_rates =
3634 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3635 struct ieee80211_supported_band *sband =
3636 wiphy->bands[ibss.channel->band];
3637 int i, j;
3638
Johannes Berg4c476992010-10-04 21:36:35 +02003639 if (n_rates == 0)
3640 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003641
3642 for (i = 0; i < n_rates; i++) {
3643 int rate = (rates[i] & 0x7f) * 5;
3644 bool found = false;
3645
3646 for (j = 0; j < sband->n_bitrates; j++) {
3647 if (sband->bitrates[j].bitrate == rate) {
3648 found = true;
3649 ibss.basic_rates |= BIT(j);
3650 break;
3651 }
3652 }
Johannes Berg4c476992010-10-04 21:36:35 +02003653 if (!found)
3654 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003655 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003656 }
3657
Johannes Berg4c476992010-10-04 21:36:35 +02003658 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3659 connkeys = nl80211_parse_connkeys(rdev,
3660 info->attrs[NL80211_ATTR_KEYS]);
3661 if (IS_ERR(connkeys))
3662 return PTR_ERR(connkeys);
3663 }
Johannes Berg04a773a2009-04-19 21:24:32 +02003664
Johannes Berg4c476992010-10-04 21:36:35 +02003665 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003666 if (err)
3667 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003668 return err;
3669}
3670
3671static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3672{
Johannes Berg4c476992010-10-04 21:36:35 +02003673 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3674 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003675
Johannes Berg4c476992010-10-04 21:36:35 +02003676 if (!rdev->ops->leave_ibss)
3677 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003678
Johannes Berg4c476992010-10-04 21:36:35 +02003679 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3680 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003681
Johannes Berg4c476992010-10-04 21:36:35 +02003682 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003683}
3684
Johannes Bergaff89a92009-07-01 21:26:51 +02003685#ifdef CONFIG_NL80211_TESTMODE
3686static struct genl_multicast_group nl80211_testmode_mcgrp = {
3687 .name = "testmode",
3688};
3689
3690static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3691{
Johannes Berg4c476992010-10-04 21:36:35 +02003692 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02003693 int err;
3694
3695 if (!info->attrs[NL80211_ATTR_TESTDATA])
3696 return -EINVAL;
3697
Johannes Bergaff89a92009-07-01 21:26:51 +02003698 err = -EOPNOTSUPP;
3699 if (rdev->ops->testmode_cmd) {
3700 rdev->testmode_info = info;
3701 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3702 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3703 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3704 rdev->testmode_info = NULL;
3705 }
3706
Johannes Bergaff89a92009-07-01 21:26:51 +02003707 return err;
3708}
3709
3710static struct sk_buff *
3711__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3712 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3713{
3714 struct sk_buff *skb;
3715 void *hdr;
3716 struct nlattr *data;
3717
3718 skb = nlmsg_new(approxlen + 100, gfp);
3719 if (!skb)
3720 return NULL;
3721
3722 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3723 if (!hdr) {
3724 kfree_skb(skb);
3725 return NULL;
3726 }
3727
3728 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3729 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3730
3731 ((void **)skb->cb)[0] = rdev;
3732 ((void **)skb->cb)[1] = hdr;
3733 ((void **)skb->cb)[2] = data;
3734
3735 return skb;
3736
3737 nla_put_failure:
3738 kfree_skb(skb);
3739 return NULL;
3740}
3741
3742struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3743 int approxlen)
3744{
3745 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3746
3747 if (WARN_ON(!rdev->testmode_info))
3748 return NULL;
3749
3750 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3751 rdev->testmode_info->snd_pid,
3752 rdev->testmode_info->snd_seq,
3753 GFP_KERNEL);
3754}
3755EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3756
3757int cfg80211_testmode_reply(struct sk_buff *skb)
3758{
3759 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3760 void *hdr = ((void **)skb->cb)[1];
3761 struct nlattr *data = ((void **)skb->cb)[2];
3762
3763 if (WARN_ON(!rdev->testmode_info)) {
3764 kfree_skb(skb);
3765 return -EINVAL;
3766 }
3767
3768 nla_nest_end(skb, data);
3769 genlmsg_end(skb, hdr);
3770 return genlmsg_reply(skb, rdev->testmode_info);
3771}
3772EXPORT_SYMBOL(cfg80211_testmode_reply);
3773
3774struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3775 int approxlen, gfp_t gfp)
3776{
3777 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3778
3779 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3780}
3781EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3782
3783void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3784{
3785 void *hdr = ((void **)skb->cb)[1];
3786 struct nlattr *data = ((void **)skb->cb)[2];
3787
3788 nla_nest_end(skb, data);
3789 genlmsg_end(skb, hdr);
3790 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3791}
3792EXPORT_SYMBOL(cfg80211_testmode_event);
3793#endif
3794
Samuel Ortizb23aa672009-07-01 21:26:54 +02003795static int nl80211_connect(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 struct cfg80211_connect_params connect;
3800 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003801 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003802 int err;
3803
3804 memset(&connect, 0, sizeof(connect));
3805
3806 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3807 return -EINVAL;
3808
3809 if (!info->attrs[NL80211_ATTR_SSID] ||
3810 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3811 return -EINVAL;
3812
3813 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3814 connect.auth_type =
3815 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3816 if (!nl80211_valid_auth_type(connect.auth_type))
3817 return -EINVAL;
3818 } else
3819 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3820
3821 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3822
Johannes Bergc0692b82010-08-27 14:26:53 +03003823 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003824 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003825 if (err)
3826 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003827
Johannes Berg074ac8d2010-09-16 14:58:22 +02003828 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003829 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3830 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003831
Johannes Berg79c97e92009-07-07 03:56:12 +02003832 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003833
Samuel Ortizb23aa672009-07-01 21:26:54 +02003834 if (info->attrs[NL80211_ATTR_MAC])
3835 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3836 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3837 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3838
3839 if (info->attrs[NL80211_ATTR_IE]) {
3840 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3841 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3842 }
3843
3844 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3845 connect.channel =
3846 ieee80211_get_channel(wiphy,
3847 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3848 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02003849 connect.channel->flags & IEEE80211_CHAN_DISABLED)
3850 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003851 }
3852
Johannes Bergfffd0932009-07-08 14:22:54 +02003853 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3854 connkeys = nl80211_parse_connkeys(rdev,
3855 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02003856 if (IS_ERR(connkeys))
3857 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003858 }
3859
3860 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003861 if (err)
3862 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003863 return err;
3864}
3865
3866static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3867{
Johannes Berg4c476992010-10-04 21:36:35 +02003868 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3869 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003870 u16 reason;
3871
3872 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3873 reason = WLAN_REASON_DEAUTH_LEAVING;
3874 else
3875 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3876
3877 if (reason == 0)
3878 return -EINVAL;
3879
Johannes Berg074ac8d2010-09-16 14:58:22 +02003880 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003881 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3882 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003883
Johannes Berg4c476992010-10-04 21:36:35 +02003884 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003885}
3886
Johannes Berg463d0182009-07-14 00:33:35 +02003887static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
3888{
Johannes Berg4c476992010-10-04 21:36:35 +02003889 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02003890 struct net *net;
3891 int err;
3892 u32 pid;
3893
3894 if (!info->attrs[NL80211_ATTR_PID])
3895 return -EINVAL;
3896
3897 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
3898
Johannes Berg463d0182009-07-14 00:33:35 +02003899 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02003900 if (IS_ERR(net))
3901 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02003902
3903 err = 0;
3904
3905 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02003906 if (!net_eq(wiphy_net(&rdev->wiphy), net))
3907 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02003908
Johannes Berg463d0182009-07-14 00:33:35 +02003909 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02003910 return err;
3911}
3912
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003913static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
3914{
Johannes Berg4c476992010-10-04 21:36:35 +02003915 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003916 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
3917 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003918 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003919 struct cfg80211_pmksa pmksa;
3920
3921 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
3922
3923 if (!info->attrs[NL80211_ATTR_MAC])
3924 return -EINVAL;
3925
3926 if (!info->attrs[NL80211_ATTR_PMKID])
3927 return -EINVAL;
3928
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003929 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
3930 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3931
Johannes Berg074ac8d2010-09-16 14:58:22 +02003932 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003933 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3934 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003935
3936 switch (info->genlhdr->cmd) {
3937 case NL80211_CMD_SET_PMKSA:
3938 rdev_ops = rdev->ops->set_pmksa;
3939 break;
3940 case NL80211_CMD_DEL_PMKSA:
3941 rdev_ops = rdev->ops->del_pmksa;
3942 break;
3943 default:
3944 WARN_ON(1);
3945 break;
3946 }
3947
Johannes Berg4c476992010-10-04 21:36:35 +02003948 if (!rdev_ops)
3949 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003950
Johannes Berg4c476992010-10-04 21:36:35 +02003951 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003952}
3953
3954static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
3955{
Johannes Berg4c476992010-10-04 21:36:35 +02003956 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3957 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003958
Johannes Berg074ac8d2010-09-16 14:58:22 +02003959 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003960 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3961 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003962
Johannes Berg4c476992010-10-04 21:36:35 +02003963 if (!rdev->ops->flush_pmksa)
3964 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003965
Johannes Berg4c476992010-10-04 21:36:35 +02003966 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003967}
3968
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003969static int nl80211_remain_on_channel(struct sk_buff *skb,
3970 struct genl_info *info)
3971{
Johannes Berg4c476992010-10-04 21:36:35 +02003972 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3973 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003974 struct ieee80211_channel *chan;
3975 struct sk_buff *msg;
3976 void *hdr;
3977 u64 cookie;
3978 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
3979 u32 freq, duration;
3980 int err;
3981
3982 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3983 !info->attrs[NL80211_ATTR_DURATION])
3984 return -EINVAL;
3985
3986 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
3987
3988 /*
3989 * We should be on that channel for at least one jiffie,
3990 * and more than 5 seconds seems excessive.
3991 */
3992 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
3993 return -EINVAL;
3994
Johannes Berg4c476992010-10-04 21:36:35 +02003995 if (!rdev->ops->remain_on_channel)
3996 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003997
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003998 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3999 channel_type = nla_get_u32(
4000 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4001 if (channel_type != NL80211_CHAN_NO_HT &&
4002 channel_type != NL80211_CHAN_HT20 &&
4003 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004004 channel_type != NL80211_CHAN_HT40MINUS)
4005 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004006 }
4007
4008 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4009 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004010 if (chan == NULL)
4011 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004012
4013 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004014 if (!msg)
4015 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004016
4017 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4018 NL80211_CMD_REMAIN_ON_CHANNEL);
4019
4020 if (IS_ERR(hdr)) {
4021 err = PTR_ERR(hdr);
4022 goto free_msg;
4023 }
4024
4025 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4026 channel_type, duration, &cookie);
4027
4028 if (err)
4029 goto free_msg;
4030
4031 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4032
4033 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004034
4035 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004036
4037 nla_put_failure:
4038 err = -ENOBUFS;
4039 free_msg:
4040 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004041 return err;
4042}
4043
4044static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4045 struct genl_info *info)
4046{
Johannes Berg4c476992010-10-04 21:36:35 +02004047 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4048 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004049 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004050
4051 if (!info->attrs[NL80211_ATTR_COOKIE])
4052 return -EINVAL;
4053
Johannes Berg4c476992010-10-04 21:36:35 +02004054 if (!rdev->ops->cancel_remain_on_channel)
4055 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004056
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004057 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4058
Johannes Berg4c476992010-10-04 21:36:35 +02004059 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004060}
4061
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004062static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4063 u8 *rates, u8 rates_len)
4064{
4065 u8 i;
4066 u32 mask = 0;
4067
4068 for (i = 0; i < rates_len; i++) {
4069 int rate = (rates[i] & 0x7f) * 5;
4070 int ridx;
4071 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4072 struct ieee80211_rate *srate =
4073 &sband->bitrates[ridx];
4074 if (rate == srate->bitrate) {
4075 mask |= 1 << ridx;
4076 break;
4077 }
4078 }
4079 if (ridx == sband->n_bitrates)
4080 return 0; /* rate not found */
4081 }
4082
4083 return mask;
4084}
4085
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004086static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004087 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4088 .len = NL80211_MAX_SUPP_RATES },
4089};
4090
4091static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4092 struct genl_info *info)
4093{
4094 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004095 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004096 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004097 int rem, i;
4098 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004099 struct nlattr *tx_rates;
4100 struct ieee80211_supported_band *sband;
4101
4102 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4103 return -EINVAL;
4104
Johannes Berg4c476992010-10-04 21:36:35 +02004105 if (!rdev->ops->set_bitrate_mask)
4106 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004107
4108 memset(&mask, 0, sizeof(mask));
4109 /* Default to all rates enabled */
4110 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4111 sband = rdev->wiphy.bands[i];
4112 mask.control[i].legacy =
4113 sband ? (1 << sband->n_bitrates) - 1 : 0;
4114 }
4115
4116 /*
4117 * The nested attribute uses enum nl80211_band as the index. This maps
4118 * directly to the enum ieee80211_band values used in cfg80211.
4119 */
4120 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4121 {
4122 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004123 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4124 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004125 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004126 if (sband == NULL)
4127 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004128 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4129 nla_len(tx_rates), nl80211_txattr_policy);
4130 if (tb[NL80211_TXRATE_LEGACY]) {
4131 mask.control[band].legacy = rateset_to_mask(
4132 sband,
4133 nla_data(tb[NL80211_TXRATE_LEGACY]),
4134 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004135 if (mask.control[band].legacy == 0)
4136 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004137 }
4138 }
4139
Johannes Berg4c476992010-10-04 21:36:35 +02004140 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004141}
4142
Johannes Berg2e161f72010-08-12 15:38:38 +02004143static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004144{
Johannes Berg4c476992010-10-04 21:36:35 +02004145 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4146 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004147 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004148
4149 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4150 return -EINVAL;
4151
Johannes Berg2e161f72010-08-12 15:38:38 +02004152 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4153 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004154
Johannes Berg9d38d852010-06-09 17:20:33 +02004155 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004156 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004157 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4158 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4159 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004160 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4161 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004162
4163 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004164 if (!rdev->ops->mgmt_tx)
4165 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004166
Johannes Berg4c476992010-10-04 21:36:35 +02004167 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004168 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004169 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4170 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004171}
4172
Johannes Berg2e161f72010-08-12 15:38:38 +02004173static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004174{
Johannes Berg4c476992010-10-04 21:36:35 +02004175 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4176 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004177 struct ieee80211_channel *chan;
4178 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004179 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004180 u32 freq;
4181 int err;
4182 void *hdr;
4183 u64 cookie;
4184 struct sk_buff *msg;
4185
4186 if (!info->attrs[NL80211_ATTR_FRAME] ||
4187 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4188 return -EINVAL;
4189
Johannes Berg4c476992010-10-04 21:36:35 +02004190 if (!rdev->ops->mgmt_tx)
4191 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004192
Johannes Berg9d38d852010-06-09 17:20:33 +02004193 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004194 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004195 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4196 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4197 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004198 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4199 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004200
Jouni Malinen026331c2010-02-15 12:53:10 +02004201 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4202 channel_type = nla_get_u32(
4203 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4204 if (channel_type != NL80211_CHAN_NO_HT &&
4205 channel_type != NL80211_CHAN_HT20 &&
4206 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004207 channel_type != NL80211_CHAN_HT40MINUS)
4208 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004209 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004210 }
4211
4212 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4213 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004214 if (chan == NULL)
4215 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004216
4217 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004218 if (!msg)
4219 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004220
4221 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004222 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004223
4224 if (IS_ERR(hdr)) {
4225 err = PTR_ERR(hdr);
4226 goto free_msg;
4227 }
Johannes Berg2e161f72010-08-12 15:38:38 +02004228 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, channel_type,
4229 channel_type_valid,
4230 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4231 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4232 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004233 if (err)
4234 goto free_msg;
4235
4236 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4237
4238 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004239 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004240
4241 nla_put_failure:
4242 err = -ENOBUFS;
4243 free_msg:
4244 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004245 return err;
4246}
4247
Kalle Valoffb9eb32010-02-17 17:58:10 +02004248static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4249{
Johannes Berg4c476992010-10-04 21:36:35 +02004250 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004251 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004252 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004253 u8 ps_state;
4254 bool state;
4255 int err;
4256
Johannes Berg4c476992010-10-04 21:36:35 +02004257 if (!info->attrs[NL80211_ATTR_PS_STATE])
4258 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004259
4260 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4261
Johannes Berg4c476992010-10-04 21:36:35 +02004262 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4263 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004264
4265 wdev = dev->ieee80211_ptr;
4266
Johannes Berg4c476992010-10-04 21:36:35 +02004267 if (!rdev->ops->set_power_mgmt)
4268 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004269
4270 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4271
4272 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004273 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004274
Johannes Berg4c476992010-10-04 21:36:35 +02004275 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4276 wdev->ps_timeout);
4277 if (!err)
4278 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004279 return err;
4280}
4281
4282static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4283{
Johannes Berg4c476992010-10-04 21:36:35 +02004284 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004285 enum nl80211_ps_state ps_state;
4286 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004287 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004288 struct sk_buff *msg;
4289 void *hdr;
4290 int err;
4291
Kalle Valoffb9eb32010-02-17 17:58:10 +02004292 wdev = dev->ieee80211_ptr;
4293
Johannes Berg4c476992010-10-04 21:36:35 +02004294 if (!rdev->ops->set_power_mgmt)
4295 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004296
4297 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004298 if (!msg)
4299 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004300
4301 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4302 NL80211_CMD_GET_POWER_SAVE);
4303 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004304 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004305 goto free_msg;
4306 }
4307
4308 if (wdev->ps)
4309 ps_state = NL80211_PS_ENABLED;
4310 else
4311 ps_state = NL80211_PS_DISABLED;
4312
4313 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4314
4315 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004316 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004317
Johannes Berg4c476992010-10-04 21:36:35 +02004318 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004319 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004320 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004321 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004322 return err;
4323}
4324
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004325static struct nla_policy
4326nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4327 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4328 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4329 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4330};
4331
4332static int nl80211_set_cqm_rssi(struct genl_info *info,
4333 s32 threshold, u32 hysteresis)
4334{
Johannes Berg4c476992010-10-04 21:36:35 +02004335 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004336 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004337 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004338
4339 if (threshold > 0)
4340 return -EINVAL;
4341
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004342 wdev = dev->ieee80211_ptr;
4343
Johannes Berg4c476992010-10-04 21:36:35 +02004344 if (!rdev->ops->set_cqm_rssi_config)
4345 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004346
Johannes Berg074ac8d2010-09-16 14:58:22 +02004347 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004348 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4349 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004350
Johannes Berg4c476992010-10-04 21:36:35 +02004351 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4352 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004353}
4354
4355static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4356{
4357 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4358 struct nlattr *cqm;
4359 int err;
4360
4361 cqm = info->attrs[NL80211_ATTR_CQM];
4362 if (!cqm) {
4363 err = -EINVAL;
4364 goto out;
4365 }
4366
4367 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4368 nl80211_attr_cqm_policy);
4369 if (err)
4370 goto out;
4371
4372 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4373 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4374 s32 threshold;
4375 u32 hysteresis;
4376 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4377 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4378 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4379 } else
4380 err = -EINVAL;
4381
4382out:
4383 return err;
4384}
4385
Johannes Berg4c476992010-10-04 21:36:35 +02004386#define NL80211_FLAG_NEED_WIPHY 0x01
4387#define NL80211_FLAG_NEED_NETDEV 0x02
4388#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02004389#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
4390#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
4391 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02004392
4393static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4394 struct genl_info *info)
4395{
4396 struct cfg80211_registered_device *rdev;
4397 struct net_device *dev;
4398 int err;
4399 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4400
4401 if (rtnl)
4402 rtnl_lock();
4403
4404 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4405 rdev = cfg80211_get_dev_from_info(info);
4406 if (IS_ERR(rdev)) {
4407 if (rtnl)
4408 rtnl_unlock();
4409 return PTR_ERR(rdev);
4410 }
4411 info->user_ptr[0] = rdev;
4412 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4413 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4414 if (err) {
4415 if (rtnl)
4416 rtnl_unlock();
4417 return err;
4418 }
Johannes Berg41265712010-10-04 21:14:05 +02004419 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4420 !netif_running(dev)) {
4421 if (rtnl)
4422 rtnl_unlock();
4423 return -ENETDOWN;
4424 }
Johannes Berg4c476992010-10-04 21:36:35 +02004425 info->user_ptr[0] = rdev;
4426 info->user_ptr[1] = dev;
4427 }
4428
4429 return 0;
4430}
4431
4432static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4433 struct genl_info *info)
4434{
4435 if (info->user_ptr[0])
4436 cfg80211_unlock_rdev(info->user_ptr[0]);
4437 if (info->user_ptr[1])
4438 dev_put(info->user_ptr[1]);
4439 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4440 rtnl_unlock();
4441}
4442
Johannes Berg55682962007-09-20 13:09:35 -04004443static struct genl_ops nl80211_ops[] = {
4444 {
4445 .cmd = NL80211_CMD_GET_WIPHY,
4446 .doit = nl80211_get_wiphy,
4447 .dumpit = nl80211_dump_wiphy,
4448 .policy = nl80211_policy,
4449 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004450 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04004451 },
4452 {
4453 .cmd = NL80211_CMD_SET_WIPHY,
4454 .doit = nl80211_set_wiphy,
4455 .policy = nl80211_policy,
4456 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004457 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004458 },
4459 {
4460 .cmd = NL80211_CMD_GET_INTERFACE,
4461 .doit = nl80211_get_interface,
4462 .dumpit = nl80211_dump_interface,
4463 .policy = nl80211_policy,
4464 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004465 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04004466 },
4467 {
4468 .cmd = NL80211_CMD_SET_INTERFACE,
4469 .doit = nl80211_set_interface,
4470 .policy = nl80211_policy,
4471 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004472 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4473 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004474 },
4475 {
4476 .cmd = NL80211_CMD_NEW_INTERFACE,
4477 .doit = nl80211_new_interface,
4478 .policy = nl80211_policy,
4479 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004480 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4481 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004482 },
4483 {
4484 .cmd = NL80211_CMD_DEL_INTERFACE,
4485 .doit = nl80211_del_interface,
4486 .policy = nl80211_policy,
4487 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004488 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4489 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004490 },
Johannes Berg41ade002007-12-19 02:03:29 +01004491 {
4492 .cmd = NL80211_CMD_GET_KEY,
4493 .doit = nl80211_get_key,
4494 .policy = nl80211_policy,
4495 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004496 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4497 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004498 },
4499 {
4500 .cmd = NL80211_CMD_SET_KEY,
4501 .doit = nl80211_set_key,
4502 .policy = nl80211_policy,
4503 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004504 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004505 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004506 },
4507 {
4508 .cmd = NL80211_CMD_NEW_KEY,
4509 .doit = nl80211_new_key,
4510 .policy = nl80211_policy,
4511 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004512 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004513 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004514 },
4515 {
4516 .cmd = NL80211_CMD_DEL_KEY,
4517 .doit = nl80211_del_key,
4518 .policy = nl80211_policy,
4519 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004520 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004521 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004522 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004523 {
4524 .cmd = NL80211_CMD_SET_BEACON,
4525 .policy = nl80211_policy,
4526 .flags = GENL_ADMIN_PERM,
4527 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004528 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4529 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004530 },
4531 {
4532 .cmd = NL80211_CMD_NEW_BEACON,
4533 .policy = nl80211_policy,
4534 .flags = GENL_ADMIN_PERM,
4535 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004536 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4537 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004538 },
4539 {
4540 .cmd = NL80211_CMD_DEL_BEACON,
4541 .policy = nl80211_policy,
4542 .flags = GENL_ADMIN_PERM,
4543 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004544 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4545 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004546 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004547 {
4548 .cmd = NL80211_CMD_GET_STATION,
4549 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004550 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004551 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02004552 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4553 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004554 },
4555 {
4556 .cmd = NL80211_CMD_SET_STATION,
4557 .doit = nl80211_set_station,
4558 .policy = nl80211_policy,
4559 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004560 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4561 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004562 },
4563 {
4564 .cmd = NL80211_CMD_NEW_STATION,
4565 .doit = nl80211_new_station,
4566 .policy = nl80211_policy,
4567 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004568 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004569 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004570 },
4571 {
4572 .cmd = NL80211_CMD_DEL_STATION,
4573 .doit = nl80211_del_station,
4574 .policy = nl80211_policy,
4575 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004576 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4577 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004578 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004579 {
4580 .cmd = NL80211_CMD_GET_MPATH,
4581 .doit = nl80211_get_mpath,
4582 .dumpit = nl80211_dump_mpath,
4583 .policy = nl80211_policy,
4584 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004585 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004586 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004587 },
4588 {
4589 .cmd = NL80211_CMD_SET_MPATH,
4590 .doit = nl80211_set_mpath,
4591 .policy = nl80211_policy,
4592 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004593 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004594 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004595 },
4596 {
4597 .cmd = NL80211_CMD_NEW_MPATH,
4598 .doit = nl80211_new_mpath,
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,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004603 },
4604 {
4605 .cmd = NL80211_CMD_DEL_MPATH,
4606 .doit = nl80211_del_mpath,
4607 .policy = nl80211_policy,
4608 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004609 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4610 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004611 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004612 {
4613 .cmd = NL80211_CMD_SET_BSS,
4614 .doit = nl80211_set_bss,
4615 .policy = nl80211_policy,
4616 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004617 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4618 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004619 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004620 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004621 .cmd = NL80211_CMD_GET_REG,
4622 .doit = nl80211_get_reg,
4623 .policy = nl80211_policy,
4624 /* can be retrieved by unprivileged users */
4625 },
4626 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004627 .cmd = NL80211_CMD_SET_REG,
4628 .doit = nl80211_set_reg,
4629 .policy = nl80211_policy,
4630 .flags = GENL_ADMIN_PERM,
4631 },
4632 {
4633 .cmd = NL80211_CMD_REQ_SET_REG,
4634 .doit = nl80211_req_set_reg,
4635 .policy = nl80211_policy,
4636 .flags = GENL_ADMIN_PERM,
4637 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004638 {
4639 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4640 .doit = nl80211_get_mesh_params,
4641 .policy = nl80211_policy,
4642 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004643 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4644 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004645 },
4646 {
4647 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4648 .doit = nl80211_set_mesh_params,
4649 .policy = nl80211_policy,
4650 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004651 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4652 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004653 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004654 {
Johannes Berg2a519312009-02-10 21:25:55 +01004655 .cmd = NL80211_CMD_TRIGGER_SCAN,
4656 .doit = nl80211_trigger_scan,
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,
Johannes Berg2a519312009-02-10 21:25:55 +01004661 },
4662 {
4663 .cmd = NL80211_CMD_GET_SCAN,
4664 .policy = nl80211_policy,
4665 .dumpit = nl80211_dump_scan,
4666 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004667 {
4668 .cmd = NL80211_CMD_AUTHENTICATE,
4669 .doit = nl80211_authenticate,
4670 .policy = nl80211_policy,
4671 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004672 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004673 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004674 },
4675 {
4676 .cmd = NL80211_CMD_ASSOCIATE,
4677 .doit = nl80211_associate,
4678 .policy = nl80211_policy,
4679 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004680 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004681 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004682 },
4683 {
4684 .cmd = NL80211_CMD_DEAUTHENTICATE,
4685 .doit = nl80211_deauthenticate,
4686 .policy = nl80211_policy,
4687 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004688 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004689 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004690 },
4691 {
4692 .cmd = NL80211_CMD_DISASSOCIATE,
4693 .doit = nl80211_disassociate,
4694 .policy = nl80211_policy,
4695 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004696 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004697 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004698 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004699 {
4700 .cmd = NL80211_CMD_JOIN_IBSS,
4701 .doit = nl80211_join_ibss,
4702 .policy = nl80211_policy,
4703 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004704 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004705 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004706 },
4707 {
4708 .cmd = NL80211_CMD_LEAVE_IBSS,
4709 .doit = nl80211_leave_ibss,
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,
Johannes Berg04a773a2009-04-19 21:24:32 +02004714 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004715#ifdef CONFIG_NL80211_TESTMODE
4716 {
4717 .cmd = NL80211_CMD_TESTMODE,
4718 .doit = nl80211_testmode_do,
4719 .policy = nl80211_policy,
4720 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004721 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4722 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02004723 },
4724#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004725 {
4726 .cmd = NL80211_CMD_CONNECT,
4727 .doit = nl80211_connect,
4728 .policy = nl80211_policy,
4729 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004730 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004731 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004732 },
4733 {
4734 .cmd = NL80211_CMD_DISCONNECT,
4735 .doit = nl80211_disconnect,
4736 .policy = nl80211_policy,
4737 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004738 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004739 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004740 },
Johannes Berg463d0182009-07-14 00:33:35 +02004741 {
4742 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4743 .doit = nl80211_wiphy_netns,
4744 .policy = nl80211_policy,
4745 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004746 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4747 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02004748 },
Holger Schurig61fa7132009-11-11 12:25:40 +01004749 {
4750 .cmd = NL80211_CMD_GET_SURVEY,
4751 .policy = nl80211_policy,
4752 .dumpit = nl80211_dump_survey,
4753 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004754 {
4755 .cmd = NL80211_CMD_SET_PMKSA,
4756 .doit = nl80211_setdel_pmksa,
4757 .policy = nl80211_policy,
4758 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004759 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4760 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004761 },
4762 {
4763 .cmd = NL80211_CMD_DEL_PMKSA,
4764 .doit = nl80211_setdel_pmksa,
4765 .policy = nl80211_policy,
4766 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004767 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4768 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004769 },
4770 {
4771 .cmd = NL80211_CMD_FLUSH_PMKSA,
4772 .doit = nl80211_flush_pmksa,
4773 .policy = nl80211_policy,
4774 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004775 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4776 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004777 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004778 {
4779 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
4780 .doit = nl80211_remain_on_channel,
4781 .policy = nl80211_policy,
4782 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004783 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004784 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004785 },
4786 {
4787 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
4788 .doit = nl80211_cancel_remain_on_channel,
4789 .policy = nl80211_policy,
4790 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004791 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004792 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004793 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004794 {
4795 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
4796 .doit = nl80211_set_tx_bitrate_mask,
4797 .policy = nl80211_policy,
4798 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004799 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4800 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004801 },
Jouni Malinen026331c2010-02-15 12:53:10 +02004802 {
Johannes Berg2e161f72010-08-12 15:38:38 +02004803 .cmd = NL80211_CMD_REGISTER_FRAME,
4804 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02004805 .policy = nl80211_policy,
4806 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004807 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4808 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02004809 },
4810 {
Johannes Berg2e161f72010-08-12 15:38:38 +02004811 .cmd = NL80211_CMD_FRAME,
4812 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02004813 .policy = nl80211_policy,
4814 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004815 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004816 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02004817 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02004818 {
4819 .cmd = NL80211_CMD_SET_POWER_SAVE,
4820 .doit = nl80211_set_power_save,
4821 .policy = nl80211_policy,
4822 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004823 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4824 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02004825 },
4826 {
4827 .cmd = NL80211_CMD_GET_POWER_SAVE,
4828 .doit = nl80211_get_power_save,
4829 .policy = nl80211_policy,
4830 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004831 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4832 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02004833 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004834 {
4835 .cmd = NL80211_CMD_SET_CQM,
4836 .doit = nl80211_set_cqm,
4837 .policy = nl80211_policy,
4838 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004839 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4840 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004841 },
Johannes Bergf444de02010-05-05 15:25:02 +02004842 {
4843 .cmd = NL80211_CMD_SET_CHANNEL,
4844 .doit = nl80211_set_channel,
4845 .policy = nl80211_policy,
4846 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004847 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4848 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02004849 },
Bill Jordane8347eb2010-10-01 13:54:28 -04004850 {
4851 .cmd = NL80211_CMD_SET_WDS_PEER,
4852 .doit = nl80211_set_wds_peer,
4853 .policy = nl80211_policy,
4854 .flags = GENL_ADMIN_PERM,
4855 },
Johannes Berg55682962007-09-20 13:09:35 -04004856};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004857
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004858static struct genl_multicast_group nl80211_mlme_mcgrp = {
4859 .name = "mlme",
4860};
Johannes Berg55682962007-09-20 13:09:35 -04004861
4862/* multicast groups */
4863static struct genl_multicast_group nl80211_config_mcgrp = {
4864 .name = "config",
4865};
Johannes Berg2a519312009-02-10 21:25:55 +01004866static struct genl_multicast_group nl80211_scan_mcgrp = {
4867 .name = "scan",
4868};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004869static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4870 .name = "regulatory",
4871};
Johannes Berg55682962007-09-20 13:09:35 -04004872
4873/* notification functions */
4874
4875void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4876{
4877 struct sk_buff *msg;
4878
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004879 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004880 if (!msg)
4881 return;
4882
4883 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4884 nlmsg_free(msg);
4885 return;
4886 }
4887
Johannes Berg463d0182009-07-14 00:33:35 +02004888 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4889 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004890}
4891
Johannes Berg362a4152009-05-24 16:43:15 +02004892static int nl80211_add_scan_req(struct sk_buff *msg,
4893 struct cfg80211_registered_device *rdev)
4894{
4895 struct cfg80211_scan_request *req = rdev->scan_req;
4896 struct nlattr *nest;
4897 int i;
4898
Johannes Berg667503d2009-07-07 03:56:11 +02004899 ASSERT_RDEV_LOCK(rdev);
4900
Johannes Berg362a4152009-05-24 16:43:15 +02004901 if (WARN_ON(!req))
4902 return 0;
4903
4904 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4905 if (!nest)
4906 goto nla_put_failure;
4907 for (i = 0; i < req->n_ssids; i++)
4908 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4909 nla_nest_end(msg, nest);
4910
4911 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4912 if (!nest)
4913 goto nla_put_failure;
4914 for (i = 0; i < req->n_channels; i++)
4915 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4916 nla_nest_end(msg, nest);
4917
4918 if (req->ie)
4919 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4920
4921 return 0;
4922 nla_put_failure:
4923 return -ENOBUFS;
4924}
4925
Johannes Berga538e2d2009-06-16 19:56:42 +02004926static int nl80211_send_scan_msg(struct sk_buff *msg,
4927 struct cfg80211_registered_device *rdev,
4928 struct net_device *netdev,
4929 u32 pid, u32 seq, int flags,
4930 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01004931{
4932 void *hdr;
4933
4934 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4935 if (!hdr)
4936 return -1;
4937
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05004938 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01004939 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4940
Johannes Berg362a4152009-05-24 16:43:15 +02004941 /* ignore errors and send incomplete event anyway */
4942 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004943
4944 return genlmsg_end(msg, hdr);
4945
4946 nla_put_failure:
4947 genlmsg_cancel(msg, hdr);
4948 return -EMSGSIZE;
4949}
4950
Johannes Berga538e2d2009-06-16 19:56:42 +02004951void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4952 struct net_device *netdev)
4953{
4954 struct sk_buff *msg;
4955
4956 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4957 if (!msg)
4958 return;
4959
4960 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4961 NL80211_CMD_TRIGGER_SCAN) < 0) {
4962 nlmsg_free(msg);
4963 return;
4964 }
4965
Johannes Berg463d0182009-07-14 00:33:35 +02004966 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4967 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02004968}
4969
Johannes Berg2a519312009-02-10 21:25:55 +01004970void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4971 struct net_device *netdev)
4972{
4973 struct sk_buff *msg;
4974
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004975 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004976 if (!msg)
4977 return;
4978
Johannes Berga538e2d2009-06-16 19:56:42 +02004979 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4980 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004981 nlmsg_free(msg);
4982 return;
4983 }
4984
Johannes Berg463d0182009-07-14 00:33:35 +02004985 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4986 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004987}
4988
4989void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4990 struct net_device *netdev)
4991{
4992 struct sk_buff *msg;
4993
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004994 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004995 if (!msg)
4996 return;
4997
Johannes Berga538e2d2009-06-16 19:56:42 +02004998 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4999 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005000 nlmsg_free(msg);
5001 return;
5002 }
5003
Johannes Berg463d0182009-07-14 00:33:35 +02005004 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5005 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005006}
5007
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005008/*
5009 * This can happen on global regulatory changes or device specific settings
5010 * based on custom world regulatory domains.
5011 */
5012void nl80211_send_reg_change_event(struct regulatory_request *request)
5013{
5014 struct sk_buff *msg;
5015 void *hdr;
5016
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005017 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005018 if (!msg)
5019 return;
5020
5021 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5022 if (!hdr) {
5023 nlmsg_free(msg);
5024 return;
5025 }
5026
5027 /* Userspace can always count this one always being set */
5028 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5029
5030 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5031 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5032 NL80211_REGDOM_TYPE_WORLD);
5033 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5034 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5035 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5036 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5037 request->intersect)
5038 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5039 NL80211_REGDOM_TYPE_INTERSECTION);
5040 else {
5041 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5042 NL80211_REGDOM_TYPE_COUNTRY);
5043 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5044 }
5045
5046 if (wiphy_idx_valid(request->wiphy_idx))
5047 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5048
5049 if (genlmsg_end(msg, hdr) < 0) {
5050 nlmsg_free(msg);
5051 return;
5052 }
5053
Johannes Bergbc43b282009-07-25 10:54:13 +02005054 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005055 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005056 GFP_ATOMIC);
5057 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005058
5059 return;
5060
5061nla_put_failure:
5062 genlmsg_cancel(msg, hdr);
5063 nlmsg_free(msg);
5064}
5065
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005066static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5067 struct net_device *netdev,
5068 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005069 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005070{
5071 struct sk_buff *msg;
5072 void *hdr;
5073
Johannes Berge6d6e342009-07-01 21:26:47 +02005074 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005075 if (!msg)
5076 return;
5077
5078 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5079 if (!hdr) {
5080 nlmsg_free(msg);
5081 return;
5082 }
5083
5084 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5085 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5086 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5087
5088 if (genlmsg_end(msg, hdr) < 0) {
5089 nlmsg_free(msg);
5090 return;
5091 }
5092
Johannes Berg463d0182009-07-14 00:33:35 +02005093 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5094 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005095 return;
5096
5097 nla_put_failure:
5098 genlmsg_cancel(msg, hdr);
5099 nlmsg_free(msg);
5100}
5101
5102void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005103 struct net_device *netdev, const u8 *buf,
5104 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005105{
5106 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005107 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005108}
5109
5110void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5111 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005112 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005113{
Johannes Berge6d6e342009-07-01 21:26:47 +02005114 nl80211_send_mlme_event(rdev, netdev, buf, len,
5115 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005116}
5117
Jouni Malinen53b46b82009-03-27 20:53:56 +02005118void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005119 struct net_device *netdev, const u8 *buf,
5120 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005121{
5122 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005123 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005124}
5125
Jouni Malinen53b46b82009-03-27 20:53:56 +02005126void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5127 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005128 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005129{
5130 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005131 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005132}
5133
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005134static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5135 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005136 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005137{
5138 struct sk_buff *msg;
5139 void *hdr;
5140
Johannes Berge6d6e342009-07-01 21:26:47 +02005141 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005142 if (!msg)
5143 return;
5144
5145 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5146 if (!hdr) {
5147 nlmsg_free(msg);
5148 return;
5149 }
5150
5151 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5152 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5153 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5154 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5155
5156 if (genlmsg_end(msg, hdr) < 0) {
5157 nlmsg_free(msg);
5158 return;
5159 }
5160
Johannes Berg463d0182009-07-14 00:33:35 +02005161 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5162 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005163 return;
5164
5165 nla_put_failure:
5166 genlmsg_cancel(msg, hdr);
5167 nlmsg_free(msg);
5168}
5169
5170void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005171 struct net_device *netdev, const u8 *addr,
5172 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005173{
5174 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005175 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005176}
5177
5178void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005179 struct net_device *netdev, const u8 *addr,
5180 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005181{
Johannes Berge6d6e342009-07-01 21:26:47 +02005182 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5183 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005184}
5185
Samuel Ortizb23aa672009-07-01 21:26:54 +02005186void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5187 struct net_device *netdev, const u8 *bssid,
5188 const u8 *req_ie, size_t req_ie_len,
5189 const u8 *resp_ie, size_t resp_ie_len,
5190 u16 status, gfp_t gfp)
5191{
5192 struct sk_buff *msg;
5193 void *hdr;
5194
5195 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5196 if (!msg)
5197 return;
5198
5199 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5200 if (!hdr) {
5201 nlmsg_free(msg);
5202 return;
5203 }
5204
5205 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5206 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5207 if (bssid)
5208 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5209 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5210 if (req_ie)
5211 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5212 if (resp_ie)
5213 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5214
5215 if (genlmsg_end(msg, hdr) < 0) {
5216 nlmsg_free(msg);
5217 return;
5218 }
5219
Johannes Berg463d0182009-07-14 00:33:35 +02005220 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5221 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005222 return;
5223
5224 nla_put_failure:
5225 genlmsg_cancel(msg, hdr);
5226 nlmsg_free(msg);
5227
5228}
5229
5230void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5231 struct net_device *netdev, const u8 *bssid,
5232 const u8 *req_ie, size_t req_ie_len,
5233 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5234{
5235 struct sk_buff *msg;
5236 void *hdr;
5237
5238 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5239 if (!msg)
5240 return;
5241
5242 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5243 if (!hdr) {
5244 nlmsg_free(msg);
5245 return;
5246 }
5247
5248 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5249 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5250 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5251 if (req_ie)
5252 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5253 if (resp_ie)
5254 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5255
5256 if (genlmsg_end(msg, hdr) < 0) {
5257 nlmsg_free(msg);
5258 return;
5259 }
5260
Johannes Berg463d0182009-07-14 00:33:35 +02005261 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5262 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005263 return;
5264
5265 nla_put_failure:
5266 genlmsg_cancel(msg, hdr);
5267 nlmsg_free(msg);
5268
5269}
5270
5271void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5272 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005273 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005274{
5275 struct sk_buff *msg;
5276 void *hdr;
5277
Johannes Berg667503d2009-07-07 03:56:11 +02005278 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005279 if (!msg)
5280 return;
5281
5282 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5283 if (!hdr) {
5284 nlmsg_free(msg);
5285 return;
5286 }
5287
5288 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5289 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5290 if (from_ap && reason)
5291 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5292 if (from_ap)
5293 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5294 if (ie)
5295 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5296
5297 if (genlmsg_end(msg, hdr) < 0) {
5298 nlmsg_free(msg);
5299 return;
5300 }
5301
Johannes Berg463d0182009-07-14 00:33:35 +02005302 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5303 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005304 return;
5305
5306 nla_put_failure:
5307 genlmsg_cancel(msg, hdr);
5308 nlmsg_free(msg);
5309
5310}
5311
Johannes Berg04a773a2009-04-19 21:24:32 +02005312void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5313 struct net_device *netdev, const u8 *bssid,
5314 gfp_t gfp)
5315{
5316 struct sk_buff *msg;
5317 void *hdr;
5318
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005319 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005320 if (!msg)
5321 return;
5322
5323 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5324 if (!hdr) {
5325 nlmsg_free(msg);
5326 return;
5327 }
5328
5329 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5330 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5331 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5332
5333 if (genlmsg_end(msg, hdr) < 0) {
5334 nlmsg_free(msg);
5335 return;
5336 }
5337
Johannes Berg463d0182009-07-14 00:33:35 +02005338 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5339 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005340 return;
5341
5342 nla_put_failure:
5343 genlmsg_cancel(msg, hdr);
5344 nlmsg_free(msg);
5345}
5346
Jouni Malinena3b8b052009-03-27 21:59:49 +02005347void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5348 struct net_device *netdev, const u8 *addr,
5349 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005350 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005351{
5352 struct sk_buff *msg;
5353 void *hdr;
5354
Johannes Berge6d6e342009-07-01 21:26:47 +02005355 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005356 if (!msg)
5357 return;
5358
5359 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5360 if (!hdr) {
5361 nlmsg_free(msg);
5362 return;
5363 }
5364
5365 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5366 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5367 if (addr)
5368 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5369 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5370 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5371 if (tsc)
5372 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5373
5374 if (genlmsg_end(msg, hdr) < 0) {
5375 nlmsg_free(msg);
5376 return;
5377 }
5378
Johannes Berg463d0182009-07-14 00:33:35 +02005379 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5380 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005381 return;
5382
5383 nla_put_failure:
5384 genlmsg_cancel(msg, hdr);
5385 nlmsg_free(msg);
5386}
5387
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005388void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5389 struct ieee80211_channel *channel_before,
5390 struct ieee80211_channel *channel_after)
5391{
5392 struct sk_buff *msg;
5393 void *hdr;
5394 struct nlattr *nl_freq;
5395
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005396 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005397 if (!msg)
5398 return;
5399
5400 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5401 if (!hdr) {
5402 nlmsg_free(msg);
5403 return;
5404 }
5405
5406 /*
5407 * Since we are applying the beacon hint to a wiphy we know its
5408 * wiphy_idx is valid
5409 */
5410 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5411
5412 /* Before */
5413 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5414 if (!nl_freq)
5415 goto nla_put_failure;
5416 if (nl80211_msg_put_channel(msg, channel_before))
5417 goto nla_put_failure;
5418 nla_nest_end(msg, nl_freq);
5419
5420 /* After */
5421 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5422 if (!nl_freq)
5423 goto nla_put_failure;
5424 if (nl80211_msg_put_channel(msg, channel_after))
5425 goto nla_put_failure;
5426 nla_nest_end(msg, nl_freq);
5427
5428 if (genlmsg_end(msg, hdr) < 0) {
5429 nlmsg_free(msg);
5430 return;
5431 }
5432
Johannes Berg463d0182009-07-14 00:33:35 +02005433 rcu_read_lock();
5434 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5435 GFP_ATOMIC);
5436 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005437
5438 return;
5439
5440nla_put_failure:
5441 genlmsg_cancel(msg, hdr);
5442 nlmsg_free(msg);
5443}
5444
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005445static void nl80211_send_remain_on_chan_event(
5446 int cmd, struct cfg80211_registered_device *rdev,
5447 struct net_device *netdev, u64 cookie,
5448 struct ieee80211_channel *chan,
5449 enum nl80211_channel_type channel_type,
5450 unsigned int duration, gfp_t gfp)
5451{
5452 struct sk_buff *msg;
5453 void *hdr;
5454
5455 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5456 if (!msg)
5457 return;
5458
5459 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5460 if (!hdr) {
5461 nlmsg_free(msg);
5462 return;
5463 }
5464
5465 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5466 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5467 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5468 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5469 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5470
5471 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5472 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5473
5474 if (genlmsg_end(msg, hdr) < 0) {
5475 nlmsg_free(msg);
5476 return;
5477 }
5478
5479 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5480 nl80211_mlme_mcgrp.id, gfp);
5481 return;
5482
5483 nla_put_failure:
5484 genlmsg_cancel(msg, hdr);
5485 nlmsg_free(msg);
5486}
5487
5488void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5489 struct net_device *netdev, u64 cookie,
5490 struct ieee80211_channel *chan,
5491 enum nl80211_channel_type channel_type,
5492 unsigned int duration, gfp_t gfp)
5493{
5494 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5495 rdev, netdev, cookie, chan,
5496 channel_type, duration, gfp);
5497}
5498
5499void nl80211_send_remain_on_channel_cancel(
5500 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5501 u64 cookie, struct ieee80211_channel *chan,
5502 enum nl80211_channel_type channel_type, gfp_t gfp)
5503{
5504 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5505 rdev, netdev, cookie, chan,
5506 channel_type, 0, gfp);
5507}
5508
Johannes Berg98b62182009-12-23 13:15:44 +01005509void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5510 struct net_device *dev, const u8 *mac_addr,
5511 struct station_info *sinfo, gfp_t gfp)
5512{
5513 struct sk_buff *msg;
5514
5515 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5516 if (!msg)
5517 return;
5518
5519 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5520 nlmsg_free(msg);
5521 return;
5522 }
5523
5524 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5525 nl80211_mlme_mcgrp.id, gfp);
5526}
5527
Johannes Berg2e161f72010-08-12 15:38:38 +02005528int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5529 struct net_device *netdev, u32 nlpid,
5530 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005531{
5532 struct sk_buff *msg;
5533 void *hdr;
5534 int err;
5535
5536 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5537 if (!msg)
5538 return -ENOMEM;
5539
Johannes Berg2e161f72010-08-12 15:38:38 +02005540 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005541 if (!hdr) {
5542 nlmsg_free(msg);
5543 return -ENOMEM;
5544 }
5545
5546 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5547 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5548 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5549 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5550
5551 err = genlmsg_end(msg, hdr);
5552 if (err < 0) {
5553 nlmsg_free(msg);
5554 return err;
5555 }
5556
5557 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5558 if (err < 0)
5559 return err;
5560 return 0;
5561
5562 nla_put_failure:
5563 genlmsg_cancel(msg, hdr);
5564 nlmsg_free(msg);
5565 return -ENOBUFS;
5566}
5567
Johannes Berg2e161f72010-08-12 15:38:38 +02005568void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5569 struct net_device *netdev, u64 cookie,
5570 const u8 *buf, size_t len, bool ack,
5571 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005572{
5573 struct sk_buff *msg;
5574 void *hdr;
5575
5576 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5577 if (!msg)
5578 return;
5579
Johannes Berg2e161f72010-08-12 15:38:38 +02005580 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02005581 if (!hdr) {
5582 nlmsg_free(msg);
5583 return;
5584 }
5585
5586 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5587 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5588 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5589 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5590 if (ack)
5591 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5592
5593 if (genlmsg_end(msg, hdr) < 0) {
5594 nlmsg_free(msg);
5595 return;
5596 }
5597
5598 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5599 return;
5600
5601 nla_put_failure:
5602 genlmsg_cancel(msg, hdr);
5603 nlmsg_free(msg);
5604}
5605
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005606void
5607nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5608 struct net_device *netdev,
5609 enum nl80211_cqm_rssi_threshold_event rssi_event,
5610 gfp_t gfp)
5611{
5612 struct sk_buff *msg;
5613 struct nlattr *pinfoattr;
5614 void *hdr;
5615
5616 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5617 if (!msg)
5618 return;
5619
5620 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5621 if (!hdr) {
5622 nlmsg_free(msg);
5623 return;
5624 }
5625
5626 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5627 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5628
5629 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5630 if (!pinfoattr)
5631 goto nla_put_failure;
5632
5633 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5634 rssi_event);
5635
5636 nla_nest_end(msg, pinfoattr);
5637
5638 if (genlmsg_end(msg, hdr) < 0) {
5639 nlmsg_free(msg);
5640 return;
5641 }
5642
5643 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5644 nl80211_mlme_mcgrp.id, gfp);
5645 return;
5646
5647 nla_put_failure:
5648 genlmsg_cancel(msg, hdr);
5649 nlmsg_free(msg);
5650}
5651
Jouni Malinen026331c2010-02-15 12:53:10 +02005652static int nl80211_netlink_notify(struct notifier_block * nb,
5653 unsigned long state,
5654 void *_notify)
5655{
5656 struct netlink_notify *notify = _notify;
5657 struct cfg80211_registered_device *rdev;
5658 struct wireless_dev *wdev;
5659
5660 if (state != NETLINK_URELEASE)
5661 return NOTIFY_DONE;
5662
5663 rcu_read_lock();
5664
5665 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5666 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02005667 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02005668
5669 rcu_read_unlock();
5670
5671 return NOTIFY_DONE;
5672}
5673
5674static struct notifier_block nl80211_netlink_notifier = {
5675 .notifier_call = nl80211_netlink_notify,
5676};
5677
Johannes Berg55682962007-09-20 13:09:35 -04005678/* initialisation/exit functions */
5679
5680int nl80211_init(void)
5681{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005682 int err;
Johannes Berg55682962007-09-20 13:09:35 -04005683
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005684 err = genl_register_family_with_ops(&nl80211_fam,
5685 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04005686 if (err)
5687 return err;
5688
Johannes Berg55682962007-09-20 13:09:35 -04005689 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5690 if (err)
5691 goto err_out;
5692
Johannes Berg2a519312009-02-10 21:25:55 +01005693 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5694 if (err)
5695 goto err_out;
5696
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005697 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5698 if (err)
5699 goto err_out;
5700
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005701 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5702 if (err)
5703 goto err_out;
5704
Johannes Bergaff89a92009-07-01 21:26:51 +02005705#ifdef CONFIG_NL80211_TESTMODE
5706 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5707 if (err)
5708 goto err_out;
5709#endif
5710
Jouni Malinen026331c2010-02-15 12:53:10 +02005711 err = netlink_register_notifier(&nl80211_netlink_notifier);
5712 if (err)
5713 goto err_out;
5714
Johannes Berg55682962007-09-20 13:09:35 -04005715 return 0;
5716 err_out:
5717 genl_unregister_family(&nl80211_fam);
5718 return err;
5719}
5720
5721void nl80211_exit(void)
5722{
Jouni Malinen026331c2010-02-15 12:53:10 +02005723 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04005724 genl_unregister_family(&nl80211_fam);
5725}