blob: 882dc921103ba91d29b630a703752b7d3a322ed2 [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 Malinendc6382c2009-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 Berg25e47c12009-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 Berg6829c872009-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 Berg6829c872009-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{
Johannes Berg43b19952010-10-07 13:10:30 +0200879 struct cfg80211_registered_device *rdev = info->user_ptr[0];
880 struct net_device *dev = info->user_ptr[1];
881 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +0200882 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -0400883
884 if (!info->attrs[NL80211_ATTR_MAC])
885 return -EINVAL;
886
Johannes Berg43b19952010-10-07 13:10:30 +0200887 if (netif_running(dev))
888 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -0400889
Johannes Berg43b19952010-10-07 13:10:30 +0200890 if (!rdev->ops->set_wds_peer)
891 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400892
Johannes Berg43b19952010-10-07 13:10:30 +0200893 if (wdev->iftype != NL80211_IFTYPE_WDS)
894 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400895
896 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +0200897 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -0400898}
899
900
Johannes Berg55682962007-09-20 13:09:35 -0400901static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
902{
903 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200904 struct net_device *netdev = NULL;
905 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -0400906 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200907 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200908 u32 changed;
909 u8 retry_short = 0, retry_long = 0;
910 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100911 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400912
Johannes Bergf444de02010-05-05 15:25:02 +0200913 /*
914 * Try to find the wiphy and netdev. Normally this
915 * function shouldn't need the netdev, but this is
916 * done for backward compatibility -- previously
917 * setting the channel was done per wiphy, but now
918 * it is per netdev. Previous userland like hostapd
919 * also passed a netdev to set_wiphy, so that it is
920 * possible to let that go to the right netdev!
921 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100922 mutex_lock(&cfg80211_mutex);
923
Johannes Bergf444de02010-05-05 15:25:02 +0200924 if (info->attrs[NL80211_ATTR_IFINDEX]) {
925 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
926
927 netdev = dev_get_by_index(genl_info_net(info), ifindex);
928 if (netdev && netdev->ieee80211_ptr) {
929 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
930 mutex_lock(&rdev->mtx);
931 } else
932 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100933 }
934
Johannes Bergf444de02010-05-05 15:25:02 +0200935 if (!netdev) {
936 rdev = __cfg80211_rdev_from_info(info);
937 if (IS_ERR(rdev)) {
938 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +0200939 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +0200940 }
941 wdev = NULL;
942 netdev = NULL;
943 result = 0;
944
945 mutex_lock(&rdev->mtx);
946 } else if (netif_running(netdev) &&
947 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
948 wdev = netdev->ieee80211_ptr;
949 else
950 wdev = NULL;
951
952 /*
953 * end workaround code, by now the rdev is available
954 * and locked, and wdev may or may not be NULL.
955 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100956
957 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200958 result = cfg80211_dev_rename(
959 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100960
961 mutex_unlock(&cfg80211_mutex);
962
963 if (result)
964 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400965
Jouni Malinen31888482008-10-30 16:59:24 +0200966 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
967 struct ieee80211_txq_params txq_params;
968 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
969
970 if (!rdev->ops->set_txq_params) {
971 result = -EOPNOTSUPP;
972 goto bad_res;
973 }
974
975 nla_for_each_nested(nl_txq_params,
976 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
977 rem_txq_params) {
978 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
979 nla_data(nl_txq_params),
980 nla_len(nl_txq_params),
981 txq_params_policy);
982 result = parse_txq_params(tb, &txq_params);
983 if (result)
984 goto bad_res;
985
986 result = rdev->ops->set_txq_params(&rdev->wiphy,
987 &txq_params);
988 if (result)
989 goto bad_res;
990 }
991 }
992
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200993 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +0200994 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200995 if (result)
996 goto bad_res;
997 }
998
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300999 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1000 enum nl80211_tx_power_setting type;
1001 int idx, mbm = 0;
1002
1003 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001004 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001005 goto bad_res;
1006 }
1007
1008 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1009 type = nla_get_u32(info->attrs[idx]);
1010
1011 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1012 (type != NL80211_TX_POWER_AUTOMATIC)) {
1013 result = -EINVAL;
1014 goto bad_res;
1015 }
1016
1017 if (type != NL80211_TX_POWER_AUTOMATIC) {
1018 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1019 mbm = nla_get_u32(info->attrs[idx]);
1020 }
1021
1022 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1023 if (result)
1024 goto bad_res;
1025 }
1026
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001027 changed = 0;
1028
1029 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1030 retry_short = nla_get_u8(
1031 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1032 if (retry_short == 0) {
1033 result = -EINVAL;
1034 goto bad_res;
1035 }
1036 changed |= WIPHY_PARAM_RETRY_SHORT;
1037 }
1038
1039 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1040 retry_long = nla_get_u8(
1041 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1042 if (retry_long == 0) {
1043 result = -EINVAL;
1044 goto bad_res;
1045 }
1046 changed |= WIPHY_PARAM_RETRY_LONG;
1047 }
1048
1049 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1050 frag_threshold = nla_get_u32(
1051 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1052 if (frag_threshold < 256) {
1053 result = -EINVAL;
1054 goto bad_res;
1055 }
1056 if (frag_threshold != (u32) -1) {
1057 /*
1058 * Fragments (apart from the last one) are required to
1059 * have even length. Make the fragmentation code
1060 * simpler by stripping LSB should someone try to use
1061 * odd threshold value.
1062 */
1063 frag_threshold &= ~0x1;
1064 }
1065 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1066 }
1067
1068 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1069 rts_threshold = nla_get_u32(
1070 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1071 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1072 }
1073
Lukáš Turek81077e82009-12-21 22:50:47 +01001074 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1075 coverage_class = nla_get_u8(
1076 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1077 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1078 }
1079
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001080 if (changed) {
1081 u8 old_retry_short, old_retry_long;
1082 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001083 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001084
1085 if (!rdev->ops->set_wiphy_params) {
1086 result = -EOPNOTSUPP;
1087 goto bad_res;
1088 }
1089
1090 old_retry_short = rdev->wiphy.retry_short;
1091 old_retry_long = rdev->wiphy.retry_long;
1092 old_frag_threshold = rdev->wiphy.frag_threshold;
1093 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001094 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001095
1096 if (changed & WIPHY_PARAM_RETRY_SHORT)
1097 rdev->wiphy.retry_short = retry_short;
1098 if (changed & WIPHY_PARAM_RETRY_LONG)
1099 rdev->wiphy.retry_long = retry_long;
1100 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1101 rdev->wiphy.frag_threshold = frag_threshold;
1102 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1103 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001104 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1105 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001106
1107 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1108 if (result) {
1109 rdev->wiphy.retry_short = old_retry_short;
1110 rdev->wiphy.retry_long = old_retry_long;
1111 rdev->wiphy.frag_threshold = old_frag_threshold;
1112 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001113 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001114 }
1115 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001116
Johannes Berg306d6112008-12-08 12:39:04 +01001117 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001118 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001119 if (netdev)
1120 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001121 return result;
1122}
1123
1124
1125static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001126 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001127 struct net_device *dev)
1128{
1129 void *hdr;
1130
1131 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1132 if (!hdr)
1133 return -1;
1134
1135 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001136 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001137 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001138 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001139
1140 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1141 rdev->devlist_generation ^
1142 (cfg80211_rdev_list_generation << 2));
1143
Johannes Berg55682962007-09-20 13:09:35 -04001144 return genlmsg_end(msg, hdr);
1145
1146 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001147 genlmsg_cancel(msg, hdr);
1148 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001149}
1150
1151static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1152{
1153 int wp_idx = 0;
1154 int if_idx = 0;
1155 int wp_start = cb->args[0];
1156 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001157 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001158 struct wireless_dev *wdev;
1159
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001160 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001161 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1162 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001163 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001164 if (wp_idx < wp_start) {
1165 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001166 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001167 }
Johannes Berg55682962007-09-20 13:09:35 -04001168 if_idx = 0;
1169
Johannes Bergf5ea9122009-08-07 16:17:38 +02001170 mutex_lock(&rdev->devlist_mtx);
1171 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001172 if (if_idx < if_start) {
1173 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001174 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001175 }
Johannes Berg55682962007-09-20 13:09:35 -04001176 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1177 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001178 rdev, wdev->netdev) < 0) {
1179 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001180 goto out;
1181 }
1182 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001183 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001184 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001185
1186 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001187 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001188 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001189 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001190
1191 cb->args[0] = wp_idx;
1192 cb->args[1] = if_idx;
1193
1194 return skb->len;
1195}
1196
1197static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1198{
1199 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001200 struct cfg80211_registered_device *dev = info->user_ptr[0];
1201 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001202
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001203 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001204 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001205 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001206
Johannes Bergd7264052009-04-19 16:23:20 +02001207 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001208 dev, netdev) < 0) {
1209 nlmsg_free(msg);
1210 return -ENOBUFS;
1211 }
Johannes Berg55682962007-09-20 13:09:35 -04001212
Johannes Berg134e6372009-07-10 09:51:34 +00001213 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001214}
1215
Michael Wu66f7ac52008-01-31 19:48:22 +01001216static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1217 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1218 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1219 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1220 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1221 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1222};
1223
1224static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1225{
1226 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1227 int flag;
1228
1229 *mntrflags = 0;
1230
1231 if (!nla)
1232 return -EINVAL;
1233
1234 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1235 nla, mntr_flags_policy))
1236 return -EINVAL;
1237
1238 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1239 if (flags[flag])
1240 *mntrflags |= (1<<flag);
1241
1242 return 0;
1243}
1244
Johannes Berg9bc383d2009-11-19 11:55:19 +01001245static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001246 struct net_device *netdev, u8 use_4addr,
1247 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001248{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001249 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001250 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001251 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001252 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001253 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001254
1255 switch (iftype) {
1256 case NL80211_IFTYPE_AP_VLAN:
1257 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1258 return 0;
1259 break;
1260 case NL80211_IFTYPE_STATION:
1261 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1262 return 0;
1263 break;
1264 default:
1265 break;
1266 }
1267
1268 return -EOPNOTSUPP;
1269}
1270
Johannes Berg55682962007-09-20 13:09:35 -04001271static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1272{
Johannes Berg4c476992010-10-04 21:36:35 +02001273 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001274 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001275 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001276 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001277 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001278 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001279 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001280
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001281 memset(&params, 0, sizeof(params));
1282
Johannes Berg04a773a2009-04-19 21:24:32 +02001283 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001284
Johannes Berg723b0382008-09-16 20:22:09 +02001285 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001286 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001287 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001288 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001289 if (ntype > NL80211_IFTYPE_MAX)
1290 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001291 }
1292
Johannes Berg92ffe052008-09-16 20:39:36 +02001293 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001294 if (ntype != NL80211_IFTYPE_MESH_POINT)
1295 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001296 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1297 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001298 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001299 }
1300
Felix Fietkau8b787642009-11-10 18:53:10 +01001301 if (info->attrs[NL80211_ATTR_4ADDR]) {
1302 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1303 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001304 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001305 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001306 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001307 } else {
1308 params.use_4addr = -1;
1309 }
1310
Johannes Berg92ffe052008-09-16 20:39:36 +02001311 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001312 if (ntype != NL80211_IFTYPE_MONITOR)
1313 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001314 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1315 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001316 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001317 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001318
1319 flags = &_flags;
1320 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001321 }
Johannes Berg3b858752009-03-12 09:55:09 +01001322
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001323 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001324 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001325 else
1326 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001327
Johannes Berg9bc383d2009-11-19 11:55:19 +01001328 if (!err && params.use_4addr != -1)
1329 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1330
Johannes Berg55682962007-09-20 13:09:35 -04001331 return err;
1332}
1333
1334static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1335{
Johannes Berg4c476992010-10-04 21:36:35 +02001336 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001337 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001338 int err;
1339 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001340 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001341
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001342 memset(&params, 0, sizeof(params));
1343
Johannes Berg55682962007-09-20 13:09:35 -04001344 if (!info->attrs[NL80211_ATTR_IFNAME])
1345 return -EINVAL;
1346
1347 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1348 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1349 if (type > NL80211_IFTYPE_MAX)
1350 return -EINVAL;
1351 }
1352
Johannes Berg79c97e92009-07-07 03:56:12 +02001353 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001354 !(rdev->wiphy.interface_modes & (1 << type)))
1355 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001356
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001357 if (type == NL80211_IFTYPE_MESH_POINT &&
1358 info->attrs[NL80211_ATTR_MESH_ID]) {
1359 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1360 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1361 }
1362
Johannes Berg9bc383d2009-11-19 11:55:19 +01001363 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001364 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001365 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001366 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001367 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001368 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001369
Michael Wu66f7ac52008-01-31 19:48:22 +01001370 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1371 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1372 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001373 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001374 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001375 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001376
Johannes Berg55682962007-09-20 13:09:35 -04001377 return err;
1378}
1379
1380static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1381{
Johannes Berg4c476992010-10-04 21:36:35 +02001382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1383 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001384
Johannes Berg4c476992010-10-04 21:36:35 +02001385 if (!rdev->ops->del_virtual_intf)
1386 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001387
Johannes Berg4c476992010-10-04 21:36:35 +02001388 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001389}
1390
Johannes Berg41ade002007-12-19 02:03:29 +01001391struct get_key_cookie {
1392 struct sk_buff *msg;
1393 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001394 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001395};
1396
1397static void get_key_callback(void *c, struct key_params *params)
1398{
Johannes Bergb9454e82009-07-08 13:29:08 +02001399 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001400 struct get_key_cookie *cookie = c;
1401
1402 if (params->key)
1403 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1404 params->key_len, params->key);
1405
1406 if (params->seq)
1407 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1408 params->seq_len, params->seq);
1409
1410 if (params->cipher)
1411 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1412 params->cipher);
1413
Johannes Bergb9454e82009-07-08 13:29:08 +02001414 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1415 if (!key)
1416 goto nla_put_failure;
1417
1418 if (params->key)
1419 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1420 params->key_len, params->key);
1421
1422 if (params->seq)
1423 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1424 params->seq_len, params->seq);
1425
1426 if (params->cipher)
1427 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1428 params->cipher);
1429
1430 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1431
1432 nla_nest_end(cookie->msg, key);
1433
Johannes Berg41ade002007-12-19 02:03:29 +01001434 return;
1435 nla_put_failure:
1436 cookie->error = 1;
1437}
1438
1439static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1440{
Johannes Berg4c476992010-10-04 21:36:35 +02001441 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001442 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001443 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001444 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001445 const u8 *mac_addr = NULL;
1446 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001447 struct get_key_cookie cookie = {
1448 .error = 0,
1449 };
1450 void *hdr;
1451 struct sk_buff *msg;
1452
1453 if (info->attrs[NL80211_ATTR_KEY_IDX])
1454 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1455
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001456 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001457 return -EINVAL;
1458
1459 if (info->attrs[NL80211_ATTR_MAC])
1460 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1461
Johannes Berge31b8212010-10-05 19:39:30 +02001462 pairwise = !!mac_addr;
1463 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1464 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1465 if (kt >= NUM_NL80211_KEYTYPES)
1466 return -EINVAL;
1467 if (kt != NL80211_KEYTYPE_GROUP &&
1468 kt != NL80211_KEYTYPE_PAIRWISE)
1469 return -EINVAL;
1470 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1471 }
1472
Johannes Berg4c476992010-10-04 21:36:35 +02001473 if (!rdev->ops->get_key)
1474 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001475
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001476 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001477 if (!msg)
1478 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001479
1480 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1481 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001482 if (IS_ERR(hdr))
1483 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001484
1485 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001486 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001487
1488 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1489 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1490 if (mac_addr)
1491 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1492
Johannes Berge31b8212010-10-05 19:39:30 +02001493 if (pairwise && mac_addr &&
1494 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1495 return -ENOENT;
1496
1497 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1498 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001499
1500 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001501 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001502
1503 if (cookie.error)
1504 goto nla_put_failure;
1505
1506 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001507 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001508
1509 nla_put_failure:
1510 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001511 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001512 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001513 return err;
1514}
1515
1516static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1517{
Johannes Berg4c476992010-10-04 21:36:35 +02001518 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001519 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001520 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001521 struct net_device *dev = info->user_ptr[1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001522 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1523 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001524
Johannes Bergb9454e82009-07-08 13:29:08 +02001525 err = nl80211_parse_key(info, &key);
1526 if (err)
1527 return err;
1528
1529 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001530 return -EINVAL;
1531
Johannes Bergb9454e82009-07-08 13:29:08 +02001532 /* only support setting default key */
1533 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001534 return -EINVAL;
1535
Johannes Bergb9454e82009-07-08 13:29:08 +02001536 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001537 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001538 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001539 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001540
Johannes Berg4c476992010-10-04 21:36:35 +02001541 if (!func)
1542 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001543
Johannes Bergfffd0932009-07-08 14:22:54 +02001544 wdev_lock(dev->ieee80211_ptr);
1545 err = nl80211_key_allowed(dev->ieee80211_ptr);
1546 if (!err)
1547 err = func(&rdev->wiphy, dev, key.idx);
1548
Johannes Berg3d23e342009-09-29 23:27:28 +02001549#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001550 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001551 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001552 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001553 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001554 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001555 }
1556#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001557 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001558
Johannes Berg41ade002007-12-19 02:03:29 +01001559 return err;
1560}
1561
1562static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1563{
Johannes Berg4c476992010-10-04 21:36:35 +02001564 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001565 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001566 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001567 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001568 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001569
Johannes Bergb9454e82009-07-08 13:29:08 +02001570 err = nl80211_parse_key(info, &key);
1571 if (err)
1572 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001573
Johannes Bergb9454e82009-07-08 13:29:08 +02001574 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001575 return -EINVAL;
1576
Johannes Berg41ade002007-12-19 02:03:29 +01001577 if (info->attrs[NL80211_ATTR_MAC])
1578 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1579
Johannes Berge31b8212010-10-05 19:39:30 +02001580 if (key.type == -1) {
1581 if (mac_addr)
1582 key.type = NL80211_KEYTYPE_PAIRWISE;
1583 else
1584 key.type = NL80211_KEYTYPE_GROUP;
1585 }
1586
1587 /* for now */
1588 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1589 key.type != NL80211_KEYTYPE_GROUP)
1590 return -EINVAL;
1591
Johannes Berg4c476992010-10-04 21:36:35 +02001592 if (!rdev->ops->add_key)
1593 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001594
Johannes Berge31b8212010-10-05 19:39:30 +02001595 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1596 key.type == NL80211_KEYTYPE_PAIRWISE,
1597 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001598 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001599
1600 wdev_lock(dev->ieee80211_ptr);
1601 err = nl80211_key_allowed(dev->ieee80211_ptr);
1602 if (!err)
1603 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001604 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001605 mac_addr, &key.p);
1606 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001607
Johannes Berg41ade002007-12-19 02:03:29 +01001608 return err;
1609}
1610
1611static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1612{
Johannes Berg4c476992010-10-04 21:36:35 +02001613 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001614 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001615 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001616 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001617 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001618
Johannes Bergb9454e82009-07-08 13:29:08 +02001619 err = nl80211_parse_key(info, &key);
1620 if (err)
1621 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001622
1623 if (info->attrs[NL80211_ATTR_MAC])
1624 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1625
Johannes Berge31b8212010-10-05 19:39:30 +02001626 if (key.type == -1) {
1627 if (mac_addr)
1628 key.type = NL80211_KEYTYPE_PAIRWISE;
1629 else
1630 key.type = NL80211_KEYTYPE_GROUP;
1631 }
1632
1633 /* for now */
1634 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1635 key.type != NL80211_KEYTYPE_GROUP)
1636 return -EINVAL;
1637
Johannes Berg4c476992010-10-04 21:36:35 +02001638 if (!rdev->ops->del_key)
1639 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001640
Johannes Bergfffd0932009-07-08 14:22:54 +02001641 wdev_lock(dev->ieee80211_ptr);
1642 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001643
1644 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1645 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1646 err = -ENOENT;
1647
Johannes Bergfffd0932009-07-08 14:22:54 +02001648 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001649 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1650 key.type == NL80211_KEYTYPE_PAIRWISE,
1651 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001652
Johannes Berg3d23e342009-09-29 23:27:28 +02001653#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001654 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001655 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001656 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001657 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001658 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1659 }
1660#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001661 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001662
Johannes Berg41ade002007-12-19 02:03:29 +01001663 return err;
1664}
1665
Johannes Berged1b6cc2007-12-19 02:03:32 +01001666static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1667{
1668 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1669 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001670 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1671 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001672 struct beacon_parameters params;
1673 int haveinfo = 0;
1674
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001675 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1676 return -EINVAL;
1677
Johannes Berg074ac8d2010-09-16 14:58:22 +02001678 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001679 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1680 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001681
Johannes Berged1b6cc2007-12-19 02:03:32 +01001682 switch (info->genlhdr->cmd) {
1683 case NL80211_CMD_NEW_BEACON:
1684 /* these are required for NEW_BEACON */
1685 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1686 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001687 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1688 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001689
Johannes Berg79c97e92009-07-07 03:56:12 +02001690 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001691 break;
1692 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001693 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001694 break;
1695 default:
1696 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001697 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001698 }
1699
Johannes Berg4c476992010-10-04 21:36:35 +02001700 if (!call)
1701 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001702
1703 memset(&params, 0, sizeof(params));
1704
1705 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1706 params.interval =
1707 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1708 haveinfo = 1;
1709 }
1710
1711 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1712 params.dtim_period =
1713 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1714 haveinfo = 1;
1715 }
1716
1717 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1718 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1719 params.head_len =
1720 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1721 haveinfo = 1;
1722 }
1723
1724 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1725 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1726 params.tail_len =
1727 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1728 haveinfo = 1;
1729 }
1730
Johannes Berg4c476992010-10-04 21:36:35 +02001731 if (!haveinfo)
1732 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001733
Johannes Berg4c476992010-10-04 21:36:35 +02001734 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001735}
1736
1737static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1738{
Johannes Berg4c476992010-10-04 21:36:35 +02001739 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1740 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001741
Johannes Berg4c476992010-10-04 21:36:35 +02001742 if (!rdev->ops->del_beacon)
1743 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001744
Johannes Berg074ac8d2010-09-16 14:58:22 +02001745 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001746 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1747 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001748
Johannes Berg4c476992010-10-04 21:36:35 +02001749 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001750}
1751
Johannes Berg5727ef12007-12-19 02:03:34 +01001752static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1753 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1754 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1755 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001756 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001757};
1758
Johannes Bergeccb8e82009-05-11 21:57:56 +03001759static int parse_station_flags(struct genl_info *info,
1760 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001761{
1762 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001763 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001764 int flag;
1765
Johannes Bergeccb8e82009-05-11 21:57:56 +03001766 /*
1767 * Try parsing the new attribute first so userspace
1768 * can specify both for older kernels.
1769 */
1770 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1771 if (nla) {
1772 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001773
Johannes Bergeccb8e82009-05-11 21:57:56 +03001774 sta_flags = nla_data(nla);
1775 params->sta_flags_mask = sta_flags->mask;
1776 params->sta_flags_set = sta_flags->set;
1777 if ((params->sta_flags_mask |
1778 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1779 return -EINVAL;
1780 return 0;
1781 }
1782
1783 /* if present, parse the old attribute */
1784
1785 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001786 if (!nla)
1787 return 0;
1788
1789 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1790 nla, sta_flags_policy))
1791 return -EINVAL;
1792
Johannes Bergeccb8e82009-05-11 21:57:56 +03001793 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1794 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001795
1796 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1797 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001798 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001799
1800 return 0;
1801}
1802
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001803static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1804 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001805 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001806{
1807 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001808 struct nlattr *sinfoattr, *txrate;
1809 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001810
1811 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1812 if (!hdr)
1813 return -1;
1814
1815 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1816 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1817
Johannes Bergf5ea9122009-08-07 16:17:38 +02001818 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1819
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001820 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1821 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001822 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001823 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1824 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1825 sinfo->inactive_time);
1826 if (sinfo->filled & STATION_INFO_RX_BYTES)
1827 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1828 sinfo->rx_bytes);
1829 if (sinfo->filled & STATION_INFO_TX_BYTES)
1830 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1831 sinfo->tx_bytes);
1832 if (sinfo->filled & STATION_INFO_LLID)
1833 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1834 sinfo->llid);
1835 if (sinfo->filled & STATION_INFO_PLID)
1836 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1837 sinfo->plid);
1838 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1839 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1840 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001841 if (sinfo->filled & STATION_INFO_SIGNAL)
1842 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1843 sinfo->signal);
1844 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1845 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1846 if (!txrate)
1847 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001848
John W. Linville254416a2009-12-09 16:43:52 -05001849 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1850 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001851 if (bitrate > 0)
1852 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1853
1854 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1855 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1856 sinfo->txrate.mcs);
1857 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1858 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1859 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1860 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1861
1862 nla_nest_end(msg, txrate);
1863 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001864 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1865 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1866 sinfo->rx_packets);
1867 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1868 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1869 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09001870 if (sinfo->filled & STATION_INFO_TX_RETRIES)
1871 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
1872 sinfo->tx_retries);
1873 if (sinfo->filled & STATION_INFO_TX_FAILED)
1874 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
1875 sinfo->tx_failed);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001876 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001877
1878 return genlmsg_end(msg, hdr);
1879
1880 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001881 genlmsg_cancel(msg, hdr);
1882 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001883}
1884
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001885static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001886 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001887{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001888 struct station_info sinfo;
1889 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001890 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001891 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001892 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001893 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001894
Johannes Berg67748892010-10-04 21:14:06 +02001895 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1896 if (err)
1897 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001898
1899 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001900 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001901 goto out_err;
1902 }
1903
Johannes Bergbba95fe2008-07-29 13:22:51 +02001904 while (1) {
1905 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1906 mac_addr, &sinfo);
1907 if (err == -ENOENT)
1908 break;
1909 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001910 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001911
1912 if (nl80211_send_station(skb,
1913 NETLINK_CB(cb->skb).pid,
1914 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1915 netdev, mac_addr,
1916 &sinfo) < 0)
1917 goto out;
1918
1919 sta_idx++;
1920 }
1921
1922
1923 out:
1924 cb->args[1] = sta_idx;
1925 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001926 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02001927 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001928
1929 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001930}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001931
Johannes Berg5727ef12007-12-19 02:03:34 +01001932static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1933{
Johannes Berg4c476992010-10-04 21:36:35 +02001934 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1935 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001936 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001937 struct sk_buff *msg;
1938 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02001939 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001940
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001941 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001942
1943 if (!info->attrs[NL80211_ATTR_MAC])
1944 return -EINVAL;
1945
1946 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1947
Johannes Berg4c476992010-10-04 21:36:35 +02001948 if (!rdev->ops->get_station)
1949 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001950
Johannes Berg79c97e92009-07-07 03:56:12 +02001951 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001952 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001953 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001954
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001955 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001956 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001957 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001958
1959 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001960 dev, mac_addr, &sinfo) < 0) {
1961 nlmsg_free(msg);
1962 return -ENOBUFS;
1963 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001964
Johannes Berg4c476992010-10-04 21:36:35 +02001965 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01001966}
1967
1968/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001969 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01001970 */
Johannes Berg463d0182009-07-14 00:33:35 +02001971static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01001972 struct cfg80211_registered_device *rdev,
1973 struct net_device **vlan)
1974{
Johannes Berg463d0182009-07-14 00:33:35 +02001975 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01001976 *vlan = NULL;
1977
1978 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02001979 *vlan = dev_get_by_index(genl_info_net(info),
1980 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01001981 if (!*vlan)
1982 return -ENODEV;
1983 if (!(*vlan)->ieee80211_ptr)
1984 return -EINVAL;
1985 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1986 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001987 if (!netif_running(*vlan))
1988 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01001989 }
1990 return 0;
1991}
1992
1993static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1994{
Johannes Berg4c476992010-10-04 21:36:35 +02001995 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01001996 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001997 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01001998 struct station_parameters params;
1999 u8 *mac_addr = NULL;
2000
2001 memset(&params, 0, sizeof(params));
2002
2003 params.listen_interval = -1;
2004
2005 if (info->attrs[NL80211_ATTR_STA_AID])
2006 return -EINVAL;
2007
2008 if (!info->attrs[NL80211_ATTR_MAC])
2009 return -EINVAL;
2010
2011 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2012
2013 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2014 params.supported_rates =
2015 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2016 params.supported_rates_len =
2017 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2018 }
2019
2020 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2021 params.listen_interval =
2022 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2023
Jouni Malinen36aedc92008-08-25 11:58:58 +03002024 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2025 params.ht_capa =
2026 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2027
Johannes Bergeccb8e82009-05-11 21:57:56 +03002028 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002029 return -EINVAL;
2030
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002031 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2032 params.plink_action =
2033 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2034
Johannes Berg463d0182009-07-14 00:33:35 +02002035 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002036 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002037 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002038
2039 /* validate settings */
2040 err = 0;
2041
2042 switch (dev->ieee80211_ptr->iftype) {
2043 case NL80211_IFTYPE_AP:
2044 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002045 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002046 /* disallow mesh-specific things */
2047 if (params.plink_action)
2048 err = -EINVAL;
2049 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002050 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002051 case NL80211_IFTYPE_STATION:
2052 /* disallow everything but AUTHORIZED flag */
2053 if (params.plink_action)
2054 err = -EINVAL;
2055 if (params.vlan)
2056 err = -EINVAL;
2057 if (params.supported_rates)
2058 err = -EINVAL;
2059 if (params.ht_capa)
2060 err = -EINVAL;
2061 if (params.listen_interval >= 0)
2062 err = -EINVAL;
2063 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2064 err = -EINVAL;
2065 break;
2066 case NL80211_IFTYPE_MESH_POINT:
2067 /* disallow things mesh doesn't support */
2068 if (params.vlan)
2069 err = -EINVAL;
2070 if (params.ht_capa)
2071 err = -EINVAL;
2072 if (params.listen_interval >= 0)
2073 err = -EINVAL;
2074 if (params.supported_rates)
2075 err = -EINVAL;
2076 if (params.sta_flags_mask)
2077 err = -EINVAL;
2078 break;
2079 default:
2080 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002081 }
2082
Johannes Berg5727ef12007-12-19 02:03:34 +01002083 if (err)
2084 goto out;
2085
Johannes Berg79c97e92009-07-07 03:56:12 +02002086 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002087 err = -EOPNOTSUPP;
2088 goto out;
2089 }
2090
Johannes Berg79c97e92009-07-07 03:56:12 +02002091 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002092
2093 out:
2094 if (params.vlan)
2095 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002096
Johannes Berg5727ef12007-12-19 02:03:34 +01002097 return err;
2098}
2099
2100static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2101{
Johannes Berg4c476992010-10-04 21:36:35 +02002102 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002103 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002104 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002105 struct station_parameters params;
2106 u8 *mac_addr = NULL;
2107
2108 memset(&params, 0, sizeof(params));
2109
2110 if (!info->attrs[NL80211_ATTR_MAC])
2111 return -EINVAL;
2112
Johannes Berg5727ef12007-12-19 02:03:34 +01002113 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2114 return -EINVAL;
2115
2116 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2117 return -EINVAL;
2118
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002119 if (!info->attrs[NL80211_ATTR_STA_AID])
2120 return -EINVAL;
2121
Johannes Berg5727ef12007-12-19 02:03:34 +01002122 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2123 params.supported_rates =
2124 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2125 params.supported_rates_len =
2126 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2127 params.listen_interval =
2128 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002129
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002130 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2131 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2132 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002133
Jouni Malinen36aedc92008-08-25 11:58:58 +03002134 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2135 params.ht_capa =
2136 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002137
Johannes Bergeccb8e82009-05-11 21:57:56 +03002138 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002139 return -EINVAL;
2140
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002141 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002142 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02002143 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2144 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002145
Johannes Berg463d0182009-07-14 00:33:35 +02002146 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002147 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002148 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002149
2150 /* validate settings */
2151 err = 0;
2152
Johannes Berg79c97e92009-07-07 03:56:12 +02002153 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002154 err = -EOPNOTSUPP;
2155 goto out;
2156 }
2157
Johannes Berg79c97e92009-07-07 03:56:12 +02002158 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002159
2160 out:
2161 if (params.vlan)
2162 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002163 return err;
2164}
2165
2166static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2167{
Johannes Berg4c476992010-10-04 21:36:35 +02002168 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2169 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002170 u8 *mac_addr = NULL;
2171
2172 if (info->attrs[NL80211_ATTR_MAC])
2173 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2174
Johannes Berge80cf852009-05-11 14:43:13 +02002175 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002176 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002177 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002178 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2179 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002180
Johannes Berg4c476992010-10-04 21:36:35 +02002181 if (!rdev->ops->del_station)
2182 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002183
Johannes Berg4c476992010-10-04 21:36:35 +02002184 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002185}
2186
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002187static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2188 int flags, struct net_device *dev,
2189 u8 *dst, u8 *next_hop,
2190 struct mpath_info *pinfo)
2191{
2192 void *hdr;
2193 struct nlattr *pinfoattr;
2194
2195 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2196 if (!hdr)
2197 return -1;
2198
2199 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2200 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2201 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2202
Johannes Bergf5ea9122009-08-07 16:17:38 +02002203 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2204
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002205 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2206 if (!pinfoattr)
2207 goto nla_put_failure;
2208 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2209 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2210 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002211 if (pinfo->filled & MPATH_INFO_SN)
2212 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2213 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002214 if (pinfo->filled & MPATH_INFO_METRIC)
2215 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2216 pinfo->metric);
2217 if (pinfo->filled & MPATH_INFO_EXPTIME)
2218 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2219 pinfo->exptime);
2220 if (pinfo->filled & MPATH_INFO_FLAGS)
2221 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2222 pinfo->flags);
2223 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2224 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2225 pinfo->discovery_timeout);
2226 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2227 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2228 pinfo->discovery_retries);
2229
2230 nla_nest_end(msg, pinfoattr);
2231
2232 return genlmsg_end(msg, hdr);
2233
2234 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002235 genlmsg_cancel(msg, hdr);
2236 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002237}
2238
2239static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002240 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002241{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002242 struct mpath_info pinfo;
2243 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002244 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002245 u8 dst[ETH_ALEN];
2246 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002247 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002248 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002249
Johannes Berg67748892010-10-04 21:14:06 +02002250 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2251 if (err)
2252 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002253
2254 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002255 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002256 goto out_err;
2257 }
2258
Jouni Malineneec60b02009-03-20 21:21:19 +02002259 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2260 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002261 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002262 }
2263
Johannes Bergbba95fe2008-07-29 13:22:51 +02002264 while (1) {
2265 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2266 dst, next_hop, &pinfo);
2267 if (err == -ENOENT)
2268 break;
2269 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002270 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002271
2272 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2273 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2274 netdev, dst, next_hop,
2275 &pinfo) < 0)
2276 goto out;
2277
2278 path_idx++;
2279 }
2280
2281
2282 out:
2283 cb->args[1] = path_idx;
2284 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002285 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002286 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002287 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002288}
2289
2290static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2291{
Johannes Berg4c476992010-10-04 21:36:35 +02002292 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002293 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002294 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002295 struct mpath_info pinfo;
2296 struct sk_buff *msg;
2297 u8 *dst = NULL;
2298 u8 next_hop[ETH_ALEN];
2299
2300 memset(&pinfo, 0, sizeof(pinfo));
2301
2302 if (!info->attrs[NL80211_ATTR_MAC])
2303 return -EINVAL;
2304
2305 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2306
Johannes Berg4c476992010-10-04 21:36:35 +02002307 if (!rdev->ops->get_mpath)
2308 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002309
Johannes Berg4c476992010-10-04 21:36:35 +02002310 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2311 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002312
Johannes Berg79c97e92009-07-07 03:56:12 +02002313 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002314 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002315 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002316
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002317 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002318 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002319 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002320
2321 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002322 dev, dst, next_hop, &pinfo) < 0) {
2323 nlmsg_free(msg);
2324 return -ENOBUFS;
2325 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002326
Johannes Berg4c476992010-10-04 21:36:35 +02002327 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002328}
2329
2330static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2331{
Johannes Berg4c476992010-10-04 21:36:35 +02002332 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2333 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002334 u8 *dst = NULL;
2335 u8 *next_hop = NULL;
2336
2337 if (!info->attrs[NL80211_ATTR_MAC])
2338 return -EINVAL;
2339
2340 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2341 return -EINVAL;
2342
2343 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2344 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2345
Johannes Berg4c476992010-10-04 21:36:35 +02002346 if (!rdev->ops->change_mpath)
2347 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002348
Johannes Berg4c476992010-10-04 21:36:35 +02002349 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2350 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002351
Johannes Berg4c476992010-10-04 21:36:35 +02002352 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002353}
Johannes Berg4c476992010-10-04 21:36:35 +02002354
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002355static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2356{
Johannes Berg4c476992010-10-04 21:36:35 +02002357 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2358 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002359 u8 *dst = NULL;
2360 u8 *next_hop = NULL;
2361
2362 if (!info->attrs[NL80211_ATTR_MAC])
2363 return -EINVAL;
2364
2365 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2366 return -EINVAL;
2367
2368 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2369 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2370
Johannes Berg4c476992010-10-04 21:36:35 +02002371 if (!rdev->ops->add_mpath)
2372 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002373
Johannes Berg4c476992010-10-04 21:36:35 +02002374 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2375 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002376
Johannes Berg4c476992010-10-04 21:36:35 +02002377 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002378}
2379
2380static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2381{
Johannes Berg4c476992010-10-04 21:36:35 +02002382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2383 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002384 u8 *dst = NULL;
2385
2386 if (info->attrs[NL80211_ATTR_MAC])
2387 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2388
Johannes Berg4c476992010-10-04 21:36:35 +02002389 if (!rdev->ops->del_mpath)
2390 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002391
Johannes Berg4c476992010-10-04 21:36:35 +02002392 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002393}
2394
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002395static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2396{
Johannes Berg4c476992010-10-04 21:36:35 +02002397 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2398 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002399 struct bss_parameters params;
2400
2401 memset(&params, 0, sizeof(params));
2402 /* default to not changing parameters */
2403 params.use_cts_prot = -1;
2404 params.use_short_preamble = -1;
2405 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002406 params.ap_isolate = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002407
2408 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2409 params.use_cts_prot =
2410 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2411 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2412 params.use_short_preamble =
2413 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2414 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2415 params.use_short_slot_time =
2416 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002417 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2418 params.basic_rates =
2419 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2420 params.basic_rates_len =
2421 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2422 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002423 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2424 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002425
Johannes Berg4c476992010-10-04 21:36:35 +02002426 if (!rdev->ops->change_bss)
2427 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002428
Johannes Berg074ac8d2010-09-16 14:58:22 +02002429 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002430 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2431 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002432
Johannes Berg4c476992010-10-04 21:36:35 +02002433 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002434}
2435
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002436static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002437 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2438 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2439 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2440 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2441 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2442 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2443};
2444
2445static int parse_reg_rule(struct nlattr *tb[],
2446 struct ieee80211_reg_rule *reg_rule)
2447{
2448 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2449 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2450
2451 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2452 return -EINVAL;
2453 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2454 return -EINVAL;
2455 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2456 return -EINVAL;
2457 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2458 return -EINVAL;
2459 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2460 return -EINVAL;
2461
2462 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2463
2464 freq_range->start_freq_khz =
2465 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2466 freq_range->end_freq_khz =
2467 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2468 freq_range->max_bandwidth_khz =
2469 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2470
2471 power_rule->max_eirp =
2472 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2473
2474 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2475 power_rule->max_antenna_gain =
2476 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2477
2478 return 0;
2479}
2480
2481static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2482{
2483 int r;
2484 char *data = NULL;
2485
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002486 /*
2487 * You should only get this when cfg80211 hasn't yet initialized
2488 * completely when built-in to the kernel right between the time
2489 * window between nl80211_init() and regulatory_init(), if that is
2490 * even possible.
2491 */
2492 mutex_lock(&cfg80211_mutex);
2493 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002494 mutex_unlock(&cfg80211_mutex);
2495 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002496 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002497 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002498
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002499 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2500 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002501
2502 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2503
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002504 r = regulatory_hint_user(data);
2505
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002506 return r;
2507}
2508
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002509static int nl80211_get_mesh_params(struct sk_buff *skb,
2510 struct genl_info *info)
2511{
Johannes Berg4c476992010-10-04 21:36:35 +02002512 struct cfg80211_registered_device *rdev = info->user_ptr[0];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002513 struct mesh_config cur_params;
2514 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002515 struct net_device *dev = info->user_ptr[1];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002516 void *hdr;
2517 struct nlattr *pinfoattr;
2518 struct sk_buff *msg;
2519
Johannes Berg4c476992010-10-04 21:36:35 +02002520 if (!rdev->ops->get_mesh_params)
2521 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002522
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002523 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002524 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002525 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002526 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002527
2528 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002529 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002530 if (!msg)
2531 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002532 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2533 NL80211_CMD_GET_MESH_PARAMS);
2534 if (!hdr)
2535 goto nla_put_failure;
2536 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2537 if (!pinfoattr)
2538 goto nla_put_failure;
2539 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2540 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2541 cur_params.dot11MeshRetryTimeout);
2542 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2543 cur_params.dot11MeshConfirmTimeout);
2544 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2545 cur_params.dot11MeshHoldingTimeout);
2546 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2547 cur_params.dot11MeshMaxPeerLinks);
2548 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2549 cur_params.dot11MeshMaxRetries);
2550 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2551 cur_params.dot11MeshTTL);
2552 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2553 cur_params.auto_open_plinks);
2554 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2555 cur_params.dot11MeshHWMPmaxPREQretries);
2556 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2557 cur_params.path_refresh_time);
2558 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2559 cur_params.min_discovery_timeout);
2560 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2561 cur_params.dot11MeshHWMPactivePathTimeout);
2562 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2563 cur_params.dot11MeshHWMPpreqMinInterval);
2564 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2565 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002566 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2567 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002568 nla_nest_end(msg, pinfoattr);
2569 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002570 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002571
Johannes Berg3b858752009-03-12 09:55:09 +01002572 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002573 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002574 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002575 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002576}
2577
2578#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2579do {\
2580 if (table[attr_num]) {\
2581 cfg.param = nla_fn(table[attr_num]); \
2582 mask |= (1 << (attr_num - 1)); \
2583 } \
2584} while (0);\
2585
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002586static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002587 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2588 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2589 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2590 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2591 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2592 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2593 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2594
2595 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2596 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2597 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2598 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2599 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2600 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2601};
2602
2603static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2604{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002605 u32 mask;
Johannes Berg4c476992010-10-04 21:36:35 +02002606 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2607 struct net_device *dev = info->user_ptr[1];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002608 struct mesh_config cfg;
2609 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2610 struct nlattr *parent_attr;
2611
2612 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2613 if (!parent_attr)
2614 return -EINVAL;
2615 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2616 parent_attr, nl80211_meshconf_params_policy))
2617 return -EINVAL;
2618
Johannes Berg4c476992010-10-04 21:36:35 +02002619 if (!rdev->ops->set_mesh_params)
2620 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002621
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002622 /* This makes sure that there aren't more than 32 mesh config
2623 * parameters (otherwise our bitfield scheme would not work.) */
2624 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2625
2626 /* Fill in the params struct */
2627 mask = 0;
2628 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2629 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2630 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2631 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2632 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2633 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2634 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2635 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2636 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2637 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2638 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2639 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2640 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2641 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2642 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2643 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2644 nla_get_u8);
2645 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2646 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2647 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2648 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2649 nla_get_u16);
2650 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2651 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2652 nla_get_u32);
2653 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2654 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2655 nla_get_u16);
2656 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2657 dot11MeshHWMPnetDiameterTraversalTime,
2658 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2659 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002660 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2661 dot11MeshHWMPRootMode, mask,
2662 NL80211_MESHCONF_HWMP_ROOTMODE,
2663 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002664
2665 /* Apply changes */
Johannes Berg4c476992010-10-04 21:36:35 +02002666 return rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002667}
2668
2669#undef FILL_IN_MESH_PARAM_IF_SET
2670
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002671static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2672{
2673 struct sk_buff *msg;
2674 void *hdr = NULL;
2675 struct nlattr *nl_reg_rules;
2676 unsigned int i;
2677 int err = -EINVAL;
2678
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002679 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002680
2681 if (!cfg80211_regdomain)
2682 goto out;
2683
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002684 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002685 if (!msg) {
2686 err = -ENOBUFS;
2687 goto out;
2688 }
2689
2690 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2691 NL80211_CMD_GET_REG);
2692 if (!hdr)
2693 goto nla_put_failure;
2694
2695 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2696 cfg80211_regdomain->alpha2);
2697
2698 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2699 if (!nl_reg_rules)
2700 goto nla_put_failure;
2701
2702 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2703 struct nlattr *nl_reg_rule;
2704 const struct ieee80211_reg_rule *reg_rule;
2705 const struct ieee80211_freq_range *freq_range;
2706 const struct ieee80211_power_rule *power_rule;
2707
2708 reg_rule = &cfg80211_regdomain->reg_rules[i];
2709 freq_range = &reg_rule->freq_range;
2710 power_rule = &reg_rule->power_rule;
2711
2712 nl_reg_rule = nla_nest_start(msg, i);
2713 if (!nl_reg_rule)
2714 goto nla_put_failure;
2715
2716 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2717 reg_rule->flags);
2718 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2719 freq_range->start_freq_khz);
2720 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2721 freq_range->end_freq_khz);
2722 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2723 freq_range->max_bandwidth_khz);
2724 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2725 power_rule->max_antenna_gain);
2726 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2727 power_rule->max_eirp);
2728
2729 nla_nest_end(msg, nl_reg_rule);
2730 }
2731
2732 nla_nest_end(msg, nl_reg_rules);
2733
2734 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002735 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002736 goto out;
2737
2738nla_put_failure:
2739 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002740 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002741 err = -EMSGSIZE;
2742out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002743 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002744 return err;
2745}
2746
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002747static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2748{
2749 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2750 struct nlattr *nl_reg_rule;
2751 char *alpha2 = NULL;
2752 int rem_reg_rules = 0, r = 0;
2753 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2754 struct ieee80211_regdomain *rd = NULL;
2755
2756 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2757 return -EINVAL;
2758
2759 if (!info->attrs[NL80211_ATTR_REG_RULES])
2760 return -EINVAL;
2761
2762 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2763
2764 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2765 rem_reg_rules) {
2766 num_rules++;
2767 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002768 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002769 }
2770
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002771 mutex_lock(&cfg80211_mutex);
2772
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002773 if (!reg_is_valid_request(alpha2)) {
2774 r = -EINVAL;
2775 goto bad_reg;
2776 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002777
2778 size_of_regd = sizeof(struct ieee80211_regdomain) +
2779 (num_rules * sizeof(struct ieee80211_reg_rule));
2780
2781 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002782 if (!rd) {
2783 r = -ENOMEM;
2784 goto bad_reg;
2785 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002786
2787 rd->n_reg_rules = num_rules;
2788 rd->alpha2[0] = alpha2[0];
2789 rd->alpha2[1] = alpha2[1];
2790
2791 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2792 rem_reg_rules) {
2793 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2794 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2795 reg_rule_policy);
2796 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2797 if (r)
2798 goto bad_reg;
2799
2800 rule_idx++;
2801
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002802 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2803 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002804 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002805 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002806 }
2807
2808 BUG_ON(rule_idx != num_rules);
2809
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002810 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002811
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002812 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002813
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002814 return r;
2815
Johannes Bergd2372b32008-10-24 20:32:20 +02002816 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002817 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002818 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002819 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002820}
2821
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002822static int validate_scan_freqs(struct nlattr *freqs)
2823{
2824 struct nlattr *attr1, *attr2;
2825 int n_channels = 0, tmp1, tmp2;
2826
2827 nla_for_each_nested(attr1, freqs, tmp1) {
2828 n_channels++;
2829 /*
2830 * Some hardware has a limited channel list for
2831 * scanning, and it is pretty much nonsensical
2832 * to scan for a channel twice, so disallow that
2833 * and don't require drivers to check that the
2834 * channel list they get isn't longer than what
2835 * they can scan, as long as they can scan all
2836 * the channels they registered at once.
2837 */
2838 nla_for_each_nested(attr2, freqs, tmp2)
2839 if (attr1 != attr2 &&
2840 nla_get_u32(attr1) == nla_get_u32(attr2))
2841 return 0;
2842 }
2843
2844 return n_channels;
2845}
2846
Johannes Berg2a519312009-02-10 21:25:55 +01002847static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2848{
Johannes Berg4c476992010-10-04 21:36:35 +02002849 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2850 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01002851 struct cfg80211_scan_request *request;
2852 struct cfg80211_ssid *ssid;
2853 struct ieee80211_channel *channel;
2854 struct nlattr *attr;
2855 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002856 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002857 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002858 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002859
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002860 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2861 return -EINVAL;
2862
Johannes Berg79c97e92009-07-07 03:56:12 +02002863 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002864
Johannes Berg4c476992010-10-04 21:36:35 +02002865 if (!rdev->ops->scan)
2866 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01002867
Johannes Berg4c476992010-10-04 21:36:35 +02002868 if (rdev->scan_req)
2869 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01002870
2871 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002872 n_channels = validate_scan_freqs(
2873 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02002874 if (!n_channels)
2875 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002876 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002877 n_channels = 0;
2878
Johannes Berg2a519312009-02-10 21:25:55 +01002879 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2880 if (wiphy->bands[band])
2881 n_channels += wiphy->bands[band]->n_channels;
2882 }
2883
2884 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2885 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2886 n_ssids++;
2887
Johannes Berg4c476992010-10-04 21:36:35 +02002888 if (n_ssids > wiphy->max_scan_ssids)
2889 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002890
Jouni Malinen70692ad2009-02-16 19:39:13 +02002891 if (info->attrs[NL80211_ATTR_IE])
2892 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2893 else
2894 ie_len = 0;
2895
Johannes Berg4c476992010-10-04 21:36:35 +02002896 if (ie_len > wiphy->max_scan_ie_len)
2897 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02002898
Johannes Berg2a519312009-02-10 21:25:55 +01002899 request = kzalloc(sizeof(*request)
2900 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002901 + sizeof(channel) * n_channels
2902 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002903 if (!request)
2904 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01002905
Johannes Berg2a519312009-02-10 21:25:55 +01002906 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02002907 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01002908 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002909 if (ie_len) {
2910 if (request->ssids)
2911 request->ie = (void *)(request->ssids + n_ssids);
2912 else
2913 request->ie = (void *)(request->channels + n_channels);
2914 }
Johannes Berg2a519312009-02-10 21:25:55 +01002915
Johannes Berg584991d2009-11-02 13:32:03 +01002916 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01002917 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2918 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01002919 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01002920 struct ieee80211_channel *chan;
2921
2922 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2923
2924 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002925 err = -EINVAL;
2926 goto out_free;
2927 }
Johannes Berg584991d2009-11-02 13:32:03 +01002928
2929 /* ignore disabled channels */
2930 if (chan->flags & IEEE80211_CHAN_DISABLED)
2931 continue;
2932
2933 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01002934 i++;
2935 }
2936 } else {
2937 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01002938 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2939 int j;
2940 if (!wiphy->bands[band])
2941 continue;
2942 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01002943 struct ieee80211_channel *chan;
2944
2945 chan = &wiphy->bands[band]->channels[j];
2946
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 }
2954 }
2955
Johannes Berg584991d2009-11-02 13:32:03 +01002956 if (!i) {
2957 err = -EINVAL;
2958 goto out_free;
2959 }
2960
2961 request->n_channels = i;
2962
Johannes Berg2a519312009-02-10 21:25:55 +01002963 i = 0;
2964 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2965 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2966 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2967 err = -EINVAL;
2968 goto out_free;
2969 }
2970 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2971 request->ssids[i].ssid_len = nla_len(attr);
2972 i++;
2973 }
2974 }
2975
Jouni Malinen70692ad2009-02-16 19:39:13 +02002976 if (info->attrs[NL80211_ATTR_IE]) {
2977 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02002978 memcpy((void *)request->ie,
2979 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02002980 request->ie_len);
2981 }
2982
Johannes Berg463d0182009-07-14 00:33:35 +02002983 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02002984 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002985
Johannes Berg79c97e92009-07-07 03:56:12 +02002986 rdev->scan_req = request;
2987 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01002988
Johannes Berg463d0182009-07-14 00:33:35 +02002989 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002990 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02002991 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02002992 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01002993 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02002994 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01002995 kfree(request);
2996 }
Johannes Berg3b858752009-03-12 09:55:09 +01002997
Johannes Berg2a519312009-02-10 21:25:55 +01002998 return err;
2999}
3000
3001static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3002 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003003 struct wireless_dev *wdev,
3004 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003005{
Johannes Berg48ab9052009-07-10 18:42:31 +02003006 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003007 void *hdr;
3008 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003009 int i;
3010
3011 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003012
3013 hdr = nl80211hdr_put(msg, pid, seq, flags,
3014 NL80211_CMD_NEW_SCAN_RESULTS);
3015 if (!hdr)
3016 return -1;
3017
Johannes Bergf5ea9122009-08-07 16:17:38 +02003018 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003019 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003020
3021 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3022 if (!bss)
3023 goto nla_put_failure;
3024 if (!is_zero_ether_addr(res->bssid))
3025 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3026 if (res->information_elements && res->len_information_elements)
3027 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3028 res->len_information_elements,
3029 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003030 if (res->beacon_ies && res->len_beacon_ies &&
3031 res->beacon_ies != res->information_elements)
3032 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3033 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003034 if (res->tsf)
3035 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3036 if (res->beacon_interval)
3037 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3038 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3039 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003040 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3041 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003042
Johannes Berg77965c92009-02-18 18:45:06 +01003043 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003044 case CFG80211_SIGNAL_TYPE_MBM:
3045 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3046 break;
3047 case CFG80211_SIGNAL_TYPE_UNSPEC:
3048 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3049 break;
3050 default:
3051 break;
3052 }
3053
Johannes Berg48ab9052009-07-10 18:42:31 +02003054 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003055 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003056 case NL80211_IFTYPE_STATION:
3057 if (intbss == wdev->current_bss)
3058 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3059 NL80211_BSS_STATUS_ASSOCIATED);
3060 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3061 if (intbss != wdev->auth_bsses[i])
3062 continue;
3063 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3064 NL80211_BSS_STATUS_AUTHENTICATED);
3065 break;
3066 }
3067 break;
3068 case NL80211_IFTYPE_ADHOC:
3069 if (intbss == wdev->current_bss)
3070 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3071 NL80211_BSS_STATUS_IBSS_JOINED);
3072 break;
3073 default:
3074 break;
3075 }
3076
Johannes Berg2a519312009-02-10 21:25:55 +01003077 nla_nest_end(msg, bss);
3078
3079 return genlmsg_end(msg, hdr);
3080
3081 nla_put_failure:
3082 genlmsg_cancel(msg, hdr);
3083 return -EMSGSIZE;
3084}
3085
3086static int nl80211_dump_scan(struct sk_buff *skb,
3087 struct netlink_callback *cb)
3088{
Johannes Berg48ab9052009-07-10 18:42:31 +02003089 struct cfg80211_registered_device *rdev;
3090 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003091 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003092 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003093 int start = cb->args[1], idx = 0;
3094 int err;
3095
Johannes Berg67748892010-10-04 21:14:06 +02003096 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3097 if (err)
3098 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003099
Johannes Berg48ab9052009-07-10 18:42:31 +02003100 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003101
Johannes Berg48ab9052009-07-10 18:42:31 +02003102 wdev_lock(wdev);
3103 spin_lock_bh(&rdev->bss_lock);
3104 cfg80211_bss_expire(rdev);
3105
3106 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003107 if (++idx <= start)
3108 continue;
3109 if (nl80211_send_bss(skb,
3110 NETLINK_CB(cb->skb).pid,
3111 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003112 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003113 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003114 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003115 }
3116 }
3117
Johannes Berg48ab9052009-07-10 18:42:31 +02003118 spin_unlock_bh(&rdev->bss_lock);
3119 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003120
3121 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003122 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003123
Johannes Berg67748892010-10-04 21:14:06 +02003124 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003125}
3126
Holger Schurig61fa7132009-11-11 12:25:40 +01003127static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3128 int flags, struct net_device *dev,
3129 struct survey_info *survey)
3130{
3131 void *hdr;
3132 struct nlattr *infoattr;
3133
3134 /* Survey without a channel doesn't make sense */
3135 if (!survey->channel)
3136 return -EINVAL;
3137
3138 hdr = nl80211hdr_put(msg, pid, seq, flags,
3139 NL80211_CMD_NEW_SURVEY_RESULTS);
3140 if (!hdr)
3141 return -ENOMEM;
3142
3143 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3144
3145 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3146 if (!infoattr)
3147 goto nla_put_failure;
3148
3149 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3150 survey->channel->center_freq);
3151 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3152 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3153 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003154 if (survey->filled & SURVEY_INFO_IN_USE)
3155 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Holger Schurig61fa7132009-11-11 12:25:40 +01003156
3157 nla_nest_end(msg, infoattr);
3158
3159 return genlmsg_end(msg, hdr);
3160
3161 nla_put_failure:
3162 genlmsg_cancel(msg, hdr);
3163 return -EMSGSIZE;
3164}
3165
3166static int nl80211_dump_survey(struct sk_buff *skb,
3167 struct netlink_callback *cb)
3168{
3169 struct survey_info survey;
3170 struct cfg80211_registered_device *dev;
3171 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003172 int survey_idx = cb->args[1];
3173 int res;
3174
Johannes Berg67748892010-10-04 21:14:06 +02003175 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3176 if (res)
3177 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003178
3179 if (!dev->ops->dump_survey) {
3180 res = -EOPNOTSUPP;
3181 goto out_err;
3182 }
3183
3184 while (1) {
3185 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3186 &survey);
3187 if (res == -ENOENT)
3188 break;
3189 if (res)
3190 goto out_err;
3191
3192 if (nl80211_send_survey(skb,
3193 NETLINK_CB(cb->skb).pid,
3194 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3195 netdev,
3196 &survey) < 0)
3197 goto out;
3198 survey_idx++;
3199 }
3200
3201 out:
3202 cb->args[1] = survey_idx;
3203 res = skb->len;
3204 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003205 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003206 return res;
3207}
3208
Jouni Malinen255e7372009-03-20 21:21:17 +02003209static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3210{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003211 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003212}
3213
Samuel Ortizb23aa672009-07-01 21:26:54 +02003214static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3215{
3216 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3217 NL80211_WPA_VERSION_2));
3218}
3219
3220static bool nl80211_valid_akm_suite(u32 akm)
3221{
3222 return akm == WLAN_AKM_SUITE_8021X ||
3223 akm == WLAN_AKM_SUITE_PSK;
3224}
3225
3226static bool nl80211_valid_cipher_suite(u32 cipher)
3227{
3228 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3229 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3230 cipher == WLAN_CIPHER_SUITE_TKIP ||
3231 cipher == WLAN_CIPHER_SUITE_CCMP ||
3232 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3233}
3234
3235
Jouni Malinen636a5d32009-03-19 13:39:22 +02003236static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3237{
Johannes Berg4c476992010-10-04 21:36:35 +02003238 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3239 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003240 struct ieee80211_channel *chan;
3241 const u8 *bssid, *ssid, *ie = NULL;
3242 int err, ssid_len, ie_len = 0;
3243 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003244 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003245 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003246
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003247 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3248 return -EINVAL;
3249
3250 if (!info->attrs[NL80211_ATTR_MAC])
3251 return -EINVAL;
3252
Jouni Malinen17780922009-03-27 20:52:47 +02003253 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3254 return -EINVAL;
3255
Johannes Berg19957bb2009-07-02 17:20:43 +02003256 if (!info->attrs[NL80211_ATTR_SSID])
3257 return -EINVAL;
3258
3259 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3260 return -EINVAL;
3261
Johannes Bergfffd0932009-07-08 14:22:54 +02003262 err = nl80211_parse_key(info, &key);
3263 if (err)
3264 return err;
3265
3266 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003267 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3268 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003269 if (!key.p.key || !key.p.key_len)
3270 return -EINVAL;
3271 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3272 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3273 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3274 key.p.key_len != WLAN_KEY_LEN_WEP104))
3275 return -EINVAL;
3276 if (key.idx > 4)
3277 return -EINVAL;
3278 } else {
3279 key.p.key_len = 0;
3280 key.p.key = NULL;
3281 }
3282
Johannes Bergafea0b72010-08-10 09:46:42 +02003283 if (key.idx >= 0) {
3284 int i;
3285 bool ok = false;
3286 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3287 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3288 ok = true;
3289 break;
3290 }
3291 }
Johannes Berg4c476992010-10-04 21:36:35 +02003292 if (!ok)
3293 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003294 }
3295
Johannes Berg4c476992010-10-04 21:36:35 +02003296 if (!rdev->ops->auth)
3297 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003298
Johannes Berg074ac8d2010-09-16 14:58:22 +02003299 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003300 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3301 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003302
Johannes Berg19957bb2009-07-02 17:20:43 +02003303 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003304 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003305 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003306 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3307 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003308
Johannes Berg19957bb2009-07-02 17:20:43 +02003309 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3310 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3311
3312 if (info->attrs[NL80211_ATTR_IE]) {
3313 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3314 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3315 }
3316
3317 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003318 if (!nl80211_valid_auth_type(auth_type))
3319 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003320
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003321 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3322
Johannes Berg4c476992010-10-04 21:36:35 +02003323 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3324 ssid, ssid_len, ie, ie_len,
3325 key.p.key, key.p.key_len, key.idx,
3326 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003327}
3328
Johannes Bergc0692b82010-08-27 14:26:53 +03003329static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3330 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003331 struct cfg80211_crypto_settings *settings,
3332 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003333{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003334 memset(settings, 0, sizeof(*settings));
3335
Samuel Ortizb23aa672009-07-01 21:26:54 +02003336 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3337
Johannes Bergc0692b82010-08-27 14:26:53 +03003338 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3339 u16 proto;
3340 proto = nla_get_u16(
3341 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3342 settings->control_port_ethertype = cpu_to_be16(proto);
3343 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3344 proto != ETH_P_PAE)
3345 return -EINVAL;
3346 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3347 settings->control_port_no_encrypt = true;
3348 } else
3349 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3350
Samuel Ortizb23aa672009-07-01 21:26:54 +02003351 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3352 void *data;
3353 int len, i;
3354
3355 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3356 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3357 settings->n_ciphers_pairwise = len / sizeof(u32);
3358
3359 if (len % sizeof(u32))
3360 return -EINVAL;
3361
Johannes Berg3dc27d22009-07-02 21:36:37 +02003362 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003363 return -EINVAL;
3364
3365 memcpy(settings->ciphers_pairwise, data, len);
3366
3367 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3368 if (!nl80211_valid_cipher_suite(
3369 settings->ciphers_pairwise[i]))
3370 return -EINVAL;
3371 }
3372
3373 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3374 settings->cipher_group =
3375 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3376 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3377 return -EINVAL;
3378 }
3379
3380 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3381 settings->wpa_versions =
3382 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3383 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3384 return -EINVAL;
3385 }
3386
3387 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3388 void *data;
3389 int len, i;
3390
3391 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3392 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3393 settings->n_akm_suites = len / sizeof(u32);
3394
3395 if (len % sizeof(u32))
3396 return -EINVAL;
3397
3398 memcpy(settings->akm_suites, data, len);
3399
3400 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3401 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3402 return -EINVAL;
3403 }
3404
3405 return 0;
3406}
3407
Jouni Malinen636a5d32009-03-19 13:39:22 +02003408static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3409{
Johannes Berg4c476992010-10-04 21:36:35 +02003410 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3411 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003412 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003413 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003414 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003415 int err, ssid_len, ie_len = 0;
3416 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003417
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003418 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3419 return -EINVAL;
3420
3421 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003422 !info->attrs[NL80211_ATTR_SSID] ||
3423 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003424 return -EINVAL;
3425
Johannes Berg4c476992010-10-04 21:36:35 +02003426 if (!rdev->ops->assoc)
3427 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003428
Johannes Berg074ac8d2010-09-16 14:58:22 +02003429 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003430 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3431 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003432
Johannes Berg19957bb2009-07-02 17:20:43 +02003433 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003434
Johannes Berg19957bb2009-07-02 17:20:43 +02003435 chan = ieee80211_get_channel(&rdev->wiphy,
3436 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003437 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3438 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003439
Johannes Berg19957bb2009-07-02 17:20:43 +02003440 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3441 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003442
3443 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003444 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3445 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003446 }
3447
Jouni Malinendc6382c2009-05-06 22:09:37 +03003448 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003449 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03003450 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003451 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003452 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02003453 else if (mfp != NL80211_MFP_NO)
3454 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03003455 }
3456
Johannes Berg3e5d7642009-07-07 14:37:26 +02003457 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3458 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3459
Johannes Bergc0692b82010-08-27 14:26:53 +03003460 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003461 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003462 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3463 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003464 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003465
Jouni Malinen636a5d32009-03-19 13:39:22 +02003466 return err;
3467}
3468
3469static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3470{
Johannes Berg4c476992010-10-04 21:36:35 +02003471 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3472 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003473 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003474 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003475 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003476 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003477
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003478 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3479 return -EINVAL;
3480
3481 if (!info->attrs[NL80211_ATTR_MAC])
3482 return -EINVAL;
3483
3484 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3485 return -EINVAL;
3486
Johannes Berg4c476992010-10-04 21:36:35 +02003487 if (!rdev->ops->deauth)
3488 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003489
Johannes Berg074ac8d2010-09-16 14:58:22 +02003490 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003491 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3492 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003493
Johannes Berg19957bb2009-07-02 17:20:43 +02003494 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003495
Johannes Berg19957bb2009-07-02 17:20:43 +02003496 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3497 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003498 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003499 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003500 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003501
3502 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003503 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3504 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003505 }
3506
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003507 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3508
Johannes Berg4c476992010-10-04 21:36:35 +02003509 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3510 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003511}
3512
3513static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3514{
Johannes Berg4c476992010-10-04 21:36:35 +02003515 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3516 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003517 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003518 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003519 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003520 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003521
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003522 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3523 return -EINVAL;
3524
3525 if (!info->attrs[NL80211_ATTR_MAC])
3526 return -EINVAL;
3527
3528 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3529 return -EINVAL;
3530
Johannes Berg4c476992010-10-04 21:36:35 +02003531 if (!rdev->ops->disassoc)
3532 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003533
Johannes Berg074ac8d2010-09-16 14:58:22 +02003534 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003535 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3536 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003537
Johannes Berg19957bb2009-07-02 17:20:43 +02003538 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003539
Johannes Berg19957bb2009-07-02 17:20:43 +02003540 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3541 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003542 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003543 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003544 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003545
3546 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003547 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3548 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003549 }
3550
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003551 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3552
Johannes Berg4c476992010-10-04 21:36:35 +02003553 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3554 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003555}
3556
Johannes Berg04a773a2009-04-19 21:24:32 +02003557static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3558{
Johannes Berg4c476992010-10-04 21:36:35 +02003559 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3560 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003561 struct cfg80211_ibss_params ibss;
3562 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003563 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003564 int err;
3565
Johannes Berg8e30bc52009-04-22 17:45:38 +02003566 memset(&ibss, 0, sizeof(ibss));
3567
Johannes Berg04a773a2009-04-19 21:24:32 +02003568 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3569 return -EINVAL;
3570
3571 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3572 !info->attrs[NL80211_ATTR_SSID] ||
3573 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3574 return -EINVAL;
3575
Johannes Berg8e30bc52009-04-22 17:45:38 +02003576 ibss.beacon_interval = 100;
3577
3578 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3579 ibss.beacon_interval =
3580 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3581 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3582 return -EINVAL;
3583 }
3584
Johannes Berg4c476992010-10-04 21:36:35 +02003585 if (!rdev->ops->join_ibss)
3586 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003587
Johannes Berg4c476992010-10-04 21:36:35 +02003588 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3589 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003590
Johannes Berg79c97e92009-07-07 03:56:12 +02003591 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003592
3593 if (info->attrs[NL80211_ATTR_MAC])
3594 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3595 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3596 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3597
3598 if (info->attrs[NL80211_ATTR_IE]) {
3599 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3600 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3601 }
3602
3603 ibss.channel = ieee80211_get_channel(wiphy,
3604 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3605 if (!ibss.channel ||
3606 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02003607 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3608 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003609
3610 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003611 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003612
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003613 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3614 u8 *rates =
3615 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3616 int n_rates =
3617 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3618 struct ieee80211_supported_band *sband =
3619 wiphy->bands[ibss.channel->band];
3620 int i, j;
3621
Johannes Berg4c476992010-10-04 21:36:35 +02003622 if (n_rates == 0)
3623 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003624
3625 for (i = 0; i < n_rates; i++) {
3626 int rate = (rates[i] & 0x7f) * 5;
3627 bool found = false;
3628
3629 for (j = 0; j < sband->n_bitrates; j++) {
3630 if (sband->bitrates[j].bitrate == rate) {
3631 found = true;
3632 ibss.basic_rates |= BIT(j);
3633 break;
3634 }
3635 }
Johannes Berg4c476992010-10-04 21:36:35 +02003636 if (!found)
3637 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003638 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003639 }
3640
Johannes Berg4c476992010-10-04 21:36:35 +02003641 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3642 connkeys = nl80211_parse_connkeys(rdev,
3643 info->attrs[NL80211_ATTR_KEYS]);
3644 if (IS_ERR(connkeys))
3645 return PTR_ERR(connkeys);
3646 }
Johannes Berg04a773a2009-04-19 21:24:32 +02003647
Johannes Berg4c476992010-10-04 21:36:35 +02003648 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003649 if (err)
3650 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003651 return err;
3652}
3653
3654static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3655{
Johannes Berg4c476992010-10-04 21:36:35 +02003656 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3657 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003658
Johannes Berg4c476992010-10-04 21:36:35 +02003659 if (!rdev->ops->leave_ibss)
3660 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003661
Johannes Berg4c476992010-10-04 21:36:35 +02003662 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3663 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003664
Johannes Berg4c476992010-10-04 21:36:35 +02003665 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003666}
3667
Johannes Bergaff89a92009-07-01 21:26:51 +02003668#ifdef CONFIG_NL80211_TESTMODE
3669static struct genl_multicast_group nl80211_testmode_mcgrp = {
3670 .name = "testmode",
3671};
3672
3673static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3674{
Johannes Berg4c476992010-10-04 21:36:35 +02003675 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02003676 int err;
3677
3678 if (!info->attrs[NL80211_ATTR_TESTDATA])
3679 return -EINVAL;
3680
Johannes Bergaff89a92009-07-01 21:26:51 +02003681 err = -EOPNOTSUPP;
3682 if (rdev->ops->testmode_cmd) {
3683 rdev->testmode_info = info;
3684 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3685 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3686 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3687 rdev->testmode_info = NULL;
3688 }
3689
Johannes Bergaff89a92009-07-01 21:26:51 +02003690 return err;
3691}
3692
3693static struct sk_buff *
3694__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3695 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3696{
3697 struct sk_buff *skb;
3698 void *hdr;
3699 struct nlattr *data;
3700
3701 skb = nlmsg_new(approxlen + 100, gfp);
3702 if (!skb)
3703 return NULL;
3704
3705 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3706 if (!hdr) {
3707 kfree_skb(skb);
3708 return NULL;
3709 }
3710
3711 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3712 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3713
3714 ((void **)skb->cb)[0] = rdev;
3715 ((void **)skb->cb)[1] = hdr;
3716 ((void **)skb->cb)[2] = data;
3717
3718 return skb;
3719
3720 nla_put_failure:
3721 kfree_skb(skb);
3722 return NULL;
3723}
3724
3725struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3726 int approxlen)
3727{
3728 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3729
3730 if (WARN_ON(!rdev->testmode_info))
3731 return NULL;
3732
3733 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3734 rdev->testmode_info->snd_pid,
3735 rdev->testmode_info->snd_seq,
3736 GFP_KERNEL);
3737}
3738EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3739
3740int cfg80211_testmode_reply(struct sk_buff *skb)
3741{
3742 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3743 void *hdr = ((void **)skb->cb)[1];
3744 struct nlattr *data = ((void **)skb->cb)[2];
3745
3746 if (WARN_ON(!rdev->testmode_info)) {
3747 kfree_skb(skb);
3748 return -EINVAL;
3749 }
3750
3751 nla_nest_end(skb, data);
3752 genlmsg_end(skb, hdr);
3753 return genlmsg_reply(skb, rdev->testmode_info);
3754}
3755EXPORT_SYMBOL(cfg80211_testmode_reply);
3756
3757struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3758 int approxlen, gfp_t gfp)
3759{
3760 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3761
3762 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3763}
3764EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3765
3766void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3767{
3768 void *hdr = ((void **)skb->cb)[1];
3769 struct nlattr *data = ((void **)skb->cb)[2];
3770
3771 nla_nest_end(skb, data);
3772 genlmsg_end(skb, hdr);
3773 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3774}
3775EXPORT_SYMBOL(cfg80211_testmode_event);
3776#endif
3777
Samuel Ortizb23aa672009-07-01 21:26:54 +02003778static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3779{
Johannes Berg4c476992010-10-04 21:36:35 +02003780 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3781 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003782 struct cfg80211_connect_params connect;
3783 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003784 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003785 int err;
3786
3787 memset(&connect, 0, sizeof(connect));
3788
3789 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3790 return -EINVAL;
3791
3792 if (!info->attrs[NL80211_ATTR_SSID] ||
3793 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3794 return -EINVAL;
3795
3796 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3797 connect.auth_type =
3798 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3799 if (!nl80211_valid_auth_type(connect.auth_type))
3800 return -EINVAL;
3801 } else
3802 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3803
3804 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3805
Johannes Bergc0692b82010-08-27 14:26:53 +03003806 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003807 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003808 if (err)
3809 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003810
Johannes Berg074ac8d2010-09-16 14:58:22 +02003811 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003812 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3813 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003814
Johannes Berg79c97e92009-07-07 03:56:12 +02003815 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003816
Samuel Ortizb23aa672009-07-01 21:26:54 +02003817 if (info->attrs[NL80211_ATTR_MAC])
3818 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3819 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3820 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3821
3822 if (info->attrs[NL80211_ATTR_IE]) {
3823 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3824 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3825 }
3826
3827 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3828 connect.channel =
3829 ieee80211_get_channel(wiphy,
3830 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3831 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02003832 connect.channel->flags & IEEE80211_CHAN_DISABLED)
3833 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003834 }
3835
Johannes Bergfffd0932009-07-08 14:22:54 +02003836 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3837 connkeys = nl80211_parse_connkeys(rdev,
3838 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02003839 if (IS_ERR(connkeys))
3840 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003841 }
3842
3843 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003844 if (err)
3845 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003846 return err;
3847}
3848
3849static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3850{
Johannes Berg4c476992010-10-04 21:36:35 +02003851 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3852 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003853 u16 reason;
3854
3855 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3856 reason = WLAN_REASON_DEAUTH_LEAVING;
3857 else
3858 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3859
3860 if (reason == 0)
3861 return -EINVAL;
3862
Johannes Berg074ac8d2010-09-16 14:58:22 +02003863 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003864 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3865 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003866
Johannes Berg4c476992010-10-04 21:36:35 +02003867 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003868}
3869
Johannes Berg463d0182009-07-14 00:33:35 +02003870static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
3871{
Johannes Berg4c476992010-10-04 21:36:35 +02003872 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02003873 struct net *net;
3874 int err;
3875 u32 pid;
3876
3877 if (!info->attrs[NL80211_ATTR_PID])
3878 return -EINVAL;
3879
3880 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
3881
Johannes Berg463d0182009-07-14 00:33:35 +02003882 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02003883 if (IS_ERR(net))
3884 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02003885
3886 err = 0;
3887
3888 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02003889 if (!net_eq(wiphy_net(&rdev->wiphy), net))
3890 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02003891
Johannes Berg463d0182009-07-14 00:33:35 +02003892 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02003893 return err;
3894}
3895
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003896static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
3897{
Johannes Berg4c476992010-10-04 21:36:35 +02003898 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003899 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
3900 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003901 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003902 struct cfg80211_pmksa pmksa;
3903
3904 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
3905
3906 if (!info->attrs[NL80211_ATTR_MAC])
3907 return -EINVAL;
3908
3909 if (!info->attrs[NL80211_ATTR_PMKID])
3910 return -EINVAL;
3911
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003912 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
3913 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3914
Johannes Berg074ac8d2010-09-16 14:58:22 +02003915 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003916 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3917 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003918
3919 switch (info->genlhdr->cmd) {
3920 case NL80211_CMD_SET_PMKSA:
3921 rdev_ops = rdev->ops->set_pmksa;
3922 break;
3923 case NL80211_CMD_DEL_PMKSA:
3924 rdev_ops = rdev->ops->del_pmksa;
3925 break;
3926 default:
3927 WARN_ON(1);
3928 break;
3929 }
3930
Johannes Berg4c476992010-10-04 21:36:35 +02003931 if (!rdev_ops)
3932 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003933
Johannes Berg4c476992010-10-04 21:36:35 +02003934 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003935}
3936
3937static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
3938{
Johannes Berg4c476992010-10-04 21:36:35 +02003939 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3940 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003941
Johannes Berg074ac8d2010-09-16 14:58:22 +02003942 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003943 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3944 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003945
Johannes Berg4c476992010-10-04 21:36:35 +02003946 if (!rdev->ops->flush_pmksa)
3947 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003948
Johannes Berg4c476992010-10-04 21:36:35 +02003949 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003950}
3951
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003952static int nl80211_remain_on_channel(struct sk_buff *skb,
3953 struct genl_info *info)
3954{
Johannes Berg4c476992010-10-04 21:36:35 +02003955 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3956 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003957 struct ieee80211_channel *chan;
3958 struct sk_buff *msg;
3959 void *hdr;
3960 u64 cookie;
3961 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
3962 u32 freq, duration;
3963 int err;
3964
3965 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3966 !info->attrs[NL80211_ATTR_DURATION])
3967 return -EINVAL;
3968
3969 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
3970
3971 /*
3972 * We should be on that channel for at least one jiffie,
3973 * and more than 5 seconds seems excessive.
3974 */
3975 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
3976 return -EINVAL;
3977
Johannes Berg4c476992010-10-04 21:36:35 +02003978 if (!rdev->ops->remain_on_channel)
3979 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003980
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003981 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3982 channel_type = nla_get_u32(
3983 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3984 if (channel_type != NL80211_CHAN_NO_HT &&
3985 channel_type != NL80211_CHAN_HT20 &&
3986 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02003987 channel_type != NL80211_CHAN_HT40MINUS)
3988 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003989 }
3990
3991 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
3992 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02003993 if (chan == NULL)
3994 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003995
3996 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003997 if (!msg)
3998 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01003999
4000 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4001 NL80211_CMD_REMAIN_ON_CHANNEL);
4002
4003 if (IS_ERR(hdr)) {
4004 err = PTR_ERR(hdr);
4005 goto free_msg;
4006 }
4007
4008 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4009 channel_type, duration, &cookie);
4010
4011 if (err)
4012 goto free_msg;
4013
4014 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4015
4016 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004017
4018 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004019
4020 nla_put_failure:
4021 err = -ENOBUFS;
4022 free_msg:
4023 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004024 return err;
4025}
4026
4027static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4028 struct genl_info *info)
4029{
Johannes Berg4c476992010-10-04 21:36:35 +02004030 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4031 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004032 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004033
4034 if (!info->attrs[NL80211_ATTR_COOKIE])
4035 return -EINVAL;
4036
Johannes Berg4c476992010-10-04 21:36:35 +02004037 if (!rdev->ops->cancel_remain_on_channel)
4038 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004039
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004040 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4041
Johannes Berg4c476992010-10-04 21:36:35 +02004042 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004043}
4044
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004045static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4046 u8 *rates, u8 rates_len)
4047{
4048 u8 i;
4049 u32 mask = 0;
4050
4051 for (i = 0; i < rates_len; i++) {
4052 int rate = (rates[i] & 0x7f) * 5;
4053 int ridx;
4054 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4055 struct ieee80211_rate *srate =
4056 &sband->bitrates[ridx];
4057 if (rate == srate->bitrate) {
4058 mask |= 1 << ridx;
4059 break;
4060 }
4061 }
4062 if (ridx == sband->n_bitrates)
4063 return 0; /* rate not found */
4064 }
4065
4066 return mask;
4067}
4068
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004069static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004070 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4071 .len = NL80211_MAX_SUPP_RATES },
4072};
4073
4074static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4075 struct genl_info *info)
4076{
4077 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004078 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004079 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004080 int rem, i;
4081 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004082 struct nlattr *tx_rates;
4083 struct ieee80211_supported_band *sband;
4084
4085 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4086 return -EINVAL;
4087
Johannes Berg4c476992010-10-04 21:36:35 +02004088 if (!rdev->ops->set_bitrate_mask)
4089 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004090
4091 memset(&mask, 0, sizeof(mask));
4092 /* Default to all rates enabled */
4093 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4094 sband = rdev->wiphy.bands[i];
4095 mask.control[i].legacy =
4096 sband ? (1 << sband->n_bitrates) - 1 : 0;
4097 }
4098
4099 /*
4100 * The nested attribute uses enum nl80211_band as the index. This maps
4101 * directly to the enum ieee80211_band values used in cfg80211.
4102 */
4103 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4104 {
4105 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004106 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4107 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004108 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004109 if (sband == NULL)
4110 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004111 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4112 nla_len(tx_rates), nl80211_txattr_policy);
4113 if (tb[NL80211_TXRATE_LEGACY]) {
4114 mask.control[band].legacy = rateset_to_mask(
4115 sband,
4116 nla_data(tb[NL80211_TXRATE_LEGACY]),
4117 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004118 if (mask.control[band].legacy == 0)
4119 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004120 }
4121 }
4122
Johannes Berg4c476992010-10-04 21:36:35 +02004123 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004124}
4125
Johannes Berg2e161f72010-08-12 15:38:38 +02004126static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004127{
Johannes Berg4c476992010-10-04 21:36:35 +02004128 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4129 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004130 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004131
4132 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4133 return -EINVAL;
4134
Johannes Berg2e161f72010-08-12 15:38:38 +02004135 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4136 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004137
Johannes Berg9d38d852010-06-09 17:20:33 +02004138 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004139 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004140 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4141 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4142 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004143 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4144 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004145
4146 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004147 if (!rdev->ops->mgmt_tx)
4148 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004149
Johannes Berg4c476992010-10-04 21:36:35 +02004150 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004151 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004152 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4153 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004154}
4155
Johannes Berg2e161f72010-08-12 15:38:38 +02004156static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004157{
Johannes Berg4c476992010-10-04 21:36:35 +02004158 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4159 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004160 struct ieee80211_channel *chan;
4161 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004162 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004163 u32 freq;
4164 int err;
4165 void *hdr;
4166 u64 cookie;
4167 struct sk_buff *msg;
4168
4169 if (!info->attrs[NL80211_ATTR_FRAME] ||
4170 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4171 return -EINVAL;
4172
Johannes Berg4c476992010-10-04 21:36:35 +02004173 if (!rdev->ops->mgmt_tx)
4174 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004175
Johannes Berg9d38d852010-06-09 17:20:33 +02004176 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004177 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004178 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4179 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4180 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004181 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4182 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004183
Jouni Malinen026331c2010-02-15 12:53:10 +02004184 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4185 channel_type = nla_get_u32(
4186 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4187 if (channel_type != NL80211_CHAN_NO_HT &&
4188 channel_type != NL80211_CHAN_HT20 &&
4189 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004190 channel_type != NL80211_CHAN_HT40MINUS)
4191 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004192 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004193 }
4194
4195 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4196 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004197 if (chan == NULL)
4198 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004199
4200 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004201 if (!msg)
4202 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004203
4204 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004205 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004206
4207 if (IS_ERR(hdr)) {
4208 err = PTR_ERR(hdr);
4209 goto free_msg;
4210 }
Johannes Berg2e161f72010-08-12 15:38:38 +02004211 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, channel_type,
4212 channel_type_valid,
4213 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4214 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4215 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004216 if (err)
4217 goto free_msg;
4218
4219 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4220
4221 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004222 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004223
4224 nla_put_failure:
4225 err = -ENOBUFS;
4226 free_msg:
4227 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004228 return err;
4229}
4230
Kalle Valoffb9eb32010-02-17 17:58:10 +02004231static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4232{
Johannes Berg4c476992010-10-04 21:36:35 +02004233 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004234 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004235 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004236 u8 ps_state;
4237 bool state;
4238 int err;
4239
Johannes Berg4c476992010-10-04 21:36:35 +02004240 if (!info->attrs[NL80211_ATTR_PS_STATE])
4241 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004242
4243 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4244
Johannes Berg4c476992010-10-04 21:36:35 +02004245 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4246 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004247
4248 wdev = dev->ieee80211_ptr;
4249
Johannes Berg4c476992010-10-04 21:36:35 +02004250 if (!rdev->ops->set_power_mgmt)
4251 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004252
4253 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4254
4255 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004256 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004257
Johannes Berg4c476992010-10-04 21:36:35 +02004258 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4259 wdev->ps_timeout);
4260 if (!err)
4261 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004262 return err;
4263}
4264
4265static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4266{
Johannes Berg4c476992010-10-04 21:36:35 +02004267 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004268 enum nl80211_ps_state ps_state;
4269 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004270 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004271 struct sk_buff *msg;
4272 void *hdr;
4273 int err;
4274
Kalle Valoffb9eb32010-02-17 17:58:10 +02004275 wdev = dev->ieee80211_ptr;
4276
Johannes Berg4c476992010-10-04 21:36:35 +02004277 if (!rdev->ops->set_power_mgmt)
4278 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004279
4280 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004281 if (!msg)
4282 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004283
4284 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4285 NL80211_CMD_GET_POWER_SAVE);
4286 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004287 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004288 goto free_msg;
4289 }
4290
4291 if (wdev->ps)
4292 ps_state = NL80211_PS_ENABLED;
4293 else
4294 ps_state = NL80211_PS_DISABLED;
4295
4296 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4297
4298 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004299 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004300
Johannes Berg4c476992010-10-04 21:36:35 +02004301 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004302 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004303 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004304 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004305 return err;
4306}
4307
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004308static struct nla_policy
4309nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4310 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4311 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4312 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4313};
4314
4315static int nl80211_set_cqm_rssi(struct genl_info *info,
4316 s32 threshold, u32 hysteresis)
4317{
Johannes Berg4c476992010-10-04 21:36:35 +02004318 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004319 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004320 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004321
4322 if (threshold > 0)
4323 return -EINVAL;
4324
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004325 wdev = dev->ieee80211_ptr;
4326
Johannes Berg4c476992010-10-04 21:36:35 +02004327 if (!rdev->ops->set_cqm_rssi_config)
4328 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004329
Johannes Berg074ac8d2010-09-16 14:58:22 +02004330 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004331 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4332 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004333
Johannes Berg4c476992010-10-04 21:36:35 +02004334 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4335 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004336}
4337
4338static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4339{
4340 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4341 struct nlattr *cqm;
4342 int err;
4343
4344 cqm = info->attrs[NL80211_ATTR_CQM];
4345 if (!cqm) {
4346 err = -EINVAL;
4347 goto out;
4348 }
4349
4350 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4351 nl80211_attr_cqm_policy);
4352 if (err)
4353 goto out;
4354
4355 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4356 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4357 s32 threshold;
4358 u32 hysteresis;
4359 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4360 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4361 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4362 } else
4363 err = -EINVAL;
4364
4365out:
4366 return err;
4367}
4368
Johannes Berg4c476992010-10-04 21:36:35 +02004369#define NL80211_FLAG_NEED_WIPHY 0x01
4370#define NL80211_FLAG_NEED_NETDEV 0x02
4371#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02004372#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
4373#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
4374 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02004375
4376static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4377 struct genl_info *info)
4378{
4379 struct cfg80211_registered_device *rdev;
4380 struct net_device *dev;
4381 int err;
4382 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4383
4384 if (rtnl)
4385 rtnl_lock();
4386
4387 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4388 rdev = cfg80211_get_dev_from_info(info);
4389 if (IS_ERR(rdev)) {
4390 if (rtnl)
4391 rtnl_unlock();
4392 return PTR_ERR(rdev);
4393 }
4394 info->user_ptr[0] = rdev;
4395 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4396 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4397 if (err) {
4398 if (rtnl)
4399 rtnl_unlock();
4400 return err;
4401 }
Johannes Berg41265712010-10-04 21:14:05 +02004402 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4403 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02004404 cfg80211_unlock_rdev(rdev);
4405 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02004406 if (rtnl)
4407 rtnl_unlock();
4408 return -ENETDOWN;
4409 }
Johannes Berg4c476992010-10-04 21:36:35 +02004410 info->user_ptr[0] = rdev;
4411 info->user_ptr[1] = dev;
4412 }
4413
4414 return 0;
4415}
4416
4417static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4418 struct genl_info *info)
4419{
4420 if (info->user_ptr[0])
4421 cfg80211_unlock_rdev(info->user_ptr[0]);
4422 if (info->user_ptr[1])
4423 dev_put(info->user_ptr[1]);
4424 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4425 rtnl_unlock();
4426}
4427
Johannes Berg55682962007-09-20 13:09:35 -04004428static struct genl_ops nl80211_ops[] = {
4429 {
4430 .cmd = NL80211_CMD_GET_WIPHY,
4431 .doit = nl80211_get_wiphy,
4432 .dumpit = nl80211_dump_wiphy,
4433 .policy = nl80211_policy,
4434 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004435 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04004436 },
4437 {
4438 .cmd = NL80211_CMD_SET_WIPHY,
4439 .doit = nl80211_set_wiphy,
4440 .policy = nl80211_policy,
4441 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004442 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004443 },
4444 {
4445 .cmd = NL80211_CMD_GET_INTERFACE,
4446 .doit = nl80211_get_interface,
4447 .dumpit = nl80211_dump_interface,
4448 .policy = nl80211_policy,
4449 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004450 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04004451 },
4452 {
4453 .cmd = NL80211_CMD_SET_INTERFACE,
4454 .doit = nl80211_set_interface,
4455 .policy = nl80211_policy,
4456 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004457 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4458 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004459 },
4460 {
4461 .cmd = NL80211_CMD_NEW_INTERFACE,
4462 .doit = nl80211_new_interface,
4463 .policy = nl80211_policy,
4464 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004465 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4466 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004467 },
4468 {
4469 .cmd = NL80211_CMD_DEL_INTERFACE,
4470 .doit = nl80211_del_interface,
4471 .policy = nl80211_policy,
4472 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004473 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4474 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004475 },
Johannes Berg41ade002007-12-19 02:03:29 +01004476 {
4477 .cmd = NL80211_CMD_GET_KEY,
4478 .doit = nl80211_get_key,
4479 .policy = nl80211_policy,
4480 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004481 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4482 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004483 },
4484 {
4485 .cmd = NL80211_CMD_SET_KEY,
4486 .doit = nl80211_set_key,
4487 .policy = nl80211_policy,
4488 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004489 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004490 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004491 },
4492 {
4493 .cmd = NL80211_CMD_NEW_KEY,
4494 .doit = nl80211_new_key,
4495 .policy = nl80211_policy,
4496 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004497 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004498 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004499 },
4500 {
4501 .cmd = NL80211_CMD_DEL_KEY,
4502 .doit = nl80211_del_key,
4503 .policy = nl80211_policy,
4504 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004505 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004506 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004507 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004508 {
4509 .cmd = NL80211_CMD_SET_BEACON,
4510 .policy = nl80211_policy,
4511 .flags = GENL_ADMIN_PERM,
4512 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004513 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4514 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004515 },
4516 {
4517 .cmd = NL80211_CMD_NEW_BEACON,
4518 .policy = nl80211_policy,
4519 .flags = GENL_ADMIN_PERM,
4520 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004521 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4522 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004523 },
4524 {
4525 .cmd = NL80211_CMD_DEL_BEACON,
4526 .policy = nl80211_policy,
4527 .flags = GENL_ADMIN_PERM,
4528 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004529 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4530 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004531 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004532 {
4533 .cmd = NL80211_CMD_GET_STATION,
4534 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004535 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004536 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02004537 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4538 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004539 },
4540 {
4541 .cmd = NL80211_CMD_SET_STATION,
4542 .doit = nl80211_set_station,
4543 .policy = nl80211_policy,
4544 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004545 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4546 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004547 },
4548 {
4549 .cmd = NL80211_CMD_NEW_STATION,
4550 .doit = nl80211_new_station,
4551 .policy = nl80211_policy,
4552 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004553 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004554 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004555 },
4556 {
4557 .cmd = NL80211_CMD_DEL_STATION,
4558 .doit = nl80211_del_station,
4559 .policy = nl80211_policy,
4560 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004561 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4562 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004563 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004564 {
4565 .cmd = NL80211_CMD_GET_MPATH,
4566 .doit = nl80211_get_mpath,
4567 .dumpit = nl80211_dump_mpath,
4568 .policy = nl80211_policy,
4569 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004570 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004571 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004572 },
4573 {
4574 .cmd = NL80211_CMD_SET_MPATH,
4575 .doit = nl80211_set_mpath,
4576 .policy = nl80211_policy,
4577 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004578 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004579 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004580 },
4581 {
4582 .cmd = NL80211_CMD_NEW_MPATH,
4583 .doit = nl80211_new_mpath,
4584 .policy = nl80211_policy,
4585 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004586 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004587 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004588 },
4589 {
4590 .cmd = NL80211_CMD_DEL_MPATH,
4591 .doit = nl80211_del_mpath,
4592 .policy = nl80211_policy,
4593 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004594 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4595 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004596 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004597 {
4598 .cmd = NL80211_CMD_SET_BSS,
4599 .doit = nl80211_set_bss,
4600 .policy = nl80211_policy,
4601 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004602 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4603 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004604 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004605 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004606 .cmd = NL80211_CMD_GET_REG,
4607 .doit = nl80211_get_reg,
4608 .policy = nl80211_policy,
4609 /* can be retrieved by unprivileged users */
4610 },
4611 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004612 .cmd = NL80211_CMD_SET_REG,
4613 .doit = nl80211_set_reg,
4614 .policy = nl80211_policy,
4615 .flags = GENL_ADMIN_PERM,
4616 },
4617 {
4618 .cmd = NL80211_CMD_REQ_SET_REG,
4619 .doit = nl80211_req_set_reg,
4620 .policy = nl80211_policy,
4621 .flags = GENL_ADMIN_PERM,
4622 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004623 {
4624 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4625 .doit = nl80211_get_mesh_params,
4626 .policy = nl80211_policy,
4627 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004628 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4629 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004630 },
4631 {
4632 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4633 .doit = nl80211_set_mesh_params,
4634 .policy = nl80211_policy,
4635 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004636 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4637 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004638 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004639 {
Johannes Berg2a519312009-02-10 21:25:55 +01004640 .cmd = NL80211_CMD_TRIGGER_SCAN,
4641 .doit = nl80211_trigger_scan,
4642 .policy = nl80211_policy,
4643 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004644 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004645 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01004646 },
4647 {
4648 .cmd = NL80211_CMD_GET_SCAN,
4649 .policy = nl80211_policy,
4650 .dumpit = nl80211_dump_scan,
4651 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004652 {
4653 .cmd = NL80211_CMD_AUTHENTICATE,
4654 .doit = nl80211_authenticate,
4655 .policy = nl80211_policy,
4656 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004657 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004658 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004659 },
4660 {
4661 .cmd = NL80211_CMD_ASSOCIATE,
4662 .doit = nl80211_associate,
4663 .policy = nl80211_policy,
4664 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004665 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004666 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004667 },
4668 {
4669 .cmd = NL80211_CMD_DEAUTHENTICATE,
4670 .doit = nl80211_deauthenticate,
4671 .policy = nl80211_policy,
4672 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004673 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004674 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004675 },
4676 {
4677 .cmd = NL80211_CMD_DISASSOCIATE,
4678 .doit = nl80211_disassociate,
4679 .policy = nl80211_policy,
4680 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004681 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004682 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004683 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004684 {
4685 .cmd = NL80211_CMD_JOIN_IBSS,
4686 .doit = nl80211_join_ibss,
4687 .policy = nl80211_policy,
4688 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004689 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004690 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004691 },
4692 {
4693 .cmd = NL80211_CMD_LEAVE_IBSS,
4694 .doit = nl80211_leave_ibss,
4695 .policy = nl80211_policy,
4696 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004697 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004698 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004699 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004700#ifdef CONFIG_NL80211_TESTMODE
4701 {
4702 .cmd = NL80211_CMD_TESTMODE,
4703 .doit = nl80211_testmode_do,
4704 .policy = nl80211_policy,
4705 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004706 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4707 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02004708 },
4709#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004710 {
4711 .cmd = NL80211_CMD_CONNECT,
4712 .doit = nl80211_connect,
4713 .policy = nl80211_policy,
4714 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004715 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004716 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004717 },
4718 {
4719 .cmd = NL80211_CMD_DISCONNECT,
4720 .doit = nl80211_disconnect,
4721 .policy = nl80211_policy,
4722 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004723 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004724 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004725 },
Johannes Berg463d0182009-07-14 00:33:35 +02004726 {
4727 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4728 .doit = nl80211_wiphy_netns,
4729 .policy = nl80211_policy,
4730 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004731 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4732 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02004733 },
Holger Schurig61fa7132009-11-11 12:25:40 +01004734 {
4735 .cmd = NL80211_CMD_GET_SURVEY,
4736 .policy = nl80211_policy,
4737 .dumpit = nl80211_dump_survey,
4738 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004739 {
4740 .cmd = NL80211_CMD_SET_PMKSA,
4741 .doit = nl80211_setdel_pmksa,
4742 .policy = nl80211_policy,
4743 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004744 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4745 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004746 },
4747 {
4748 .cmd = NL80211_CMD_DEL_PMKSA,
4749 .doit = nl80211_setdel_pmksa,
4750 .policy = nl80211_policy,
4751 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004752 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4753 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004754 },
4755 {
4756 .cmd = NL80211_CMD_FLUSH_PMKSA,
4757 .doit = nl80211_flush_pmksa,
4758 .policy = nl80211_policy,
4759 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004760 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4761 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004762 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004763 {
4764 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
4765 .doit = nl80211_remain_on_channel,
4766 .policy = nl80211_policy,
4767 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004768 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004769 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004770 },
4771 {
4772 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
4773 .doit = nl80211_cancel_remain_on_channel,
4774 .policy = nl80211_policy,
4775 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004776 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004777 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004778 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004779 {
4780 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
4781 .doit = nl80211_set_tx_bitrate_mask,
4782 .policy = nl80211_policy,
4783 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004784 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4785 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004786 },
Jouni Malinen026331c2010-02-15 12:53:10 +02004787 {
Johannes Berg2e161f72010-08-12 15:38:38 +02004788 .cmd = NL80211_CMD_REGISTER_FRAME,
4789 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02004790 .policy = nl80211_policy,
4791 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004792 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4793 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02004794 },
4795 {
Johannes Berg2e161f72010-08-12 15:38:38 +02004796 .cmd = NL80211_CMD_FRAME,
4797 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02004798 .policy = nl80211_policy,
4799 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004800 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004801 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02004802 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02004803 {
4804 .cmd = NL80211_CMD_SET_POWER_SAVE,
4805 .doit = nl80211_set_power_save,
4806 .policy = nl80211_policy,
4807 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004808 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4809 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02004810 },
4811 {
4812 .cmd = NL80211_CMD_GET_POWER_SAVE,
4813 .doit = nl80211_get_power_save,
4814 .policy = nl80211_policy,
4815 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004816 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4817 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02004818 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004819 {
4820 .cmd = NL80211_CMD_SET_CQM,
4821 .doit = nl80211_set_cqm,
4822 .policy = nl80211_policy,
4823 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004824 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4825 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004826 },
Johannes Bergf444de02010-05-05 15:25:02 +02004827 {
4828 .cmd = NL80211_CMD_SET_CHANNEL,
4829 .doit = nl80211_set_channel,
4830 .policy = nl80211_policy,
4831 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004832 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4833 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02004834 },
Bill Jordane8347eb2010-10-01 13:54:28 -04004835 {
4836 .cmd = NL80211_CMD_SET_WDS_PEER,
4837 .doit = nl80211_set_wds_peer,
4838 .policy = nl80211_policy,
4839 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02004840 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4841 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04004842 },
Johannes Berg55682962007-09-20 13:09:35 -04004843};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004844
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004845static struct genl_multicast_group nl80211_mlme_mcgrp = {
4846 .name = "mlme",
4847};
Johannes Berg55682962007-09-20 13:09:35 -04004848
4849/* multicast groups */
4850static struct genl_multicast_group nl80211_config_mcgrp = {
4851 .name = "config",
4852};
Johannes Berg2a519312009-02-10 21:25:55 +01004853static struct genl_multicast_group nl80211_scan_mcgrp = {
4854 .name = "scan",
4855};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004856static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4857 .name = "regulatory",
4858};
Johannes Berg55682962007-09-20 13:09:35 -04004859
4860/* notification functions */
4861
4862void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4863{
4864 struct sk_buff *msg;
4865
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004866 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004867 if (!msg)
4868 return;
4869
4870 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4871 nlmsg_free(msg);
4872 return;
4873 }
4874
Johannes Berg463d0182009-07-14 00:33:35 +02004875 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4876 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004877}
4878
Johannes Berg362a4152009-05-24 16:43:15 +02004879static int nl80211_add_scan_req(struct sk_buff *msg,
4880 struct cfg80211_registered_device *rdev)
4881{
4882 struct cfg80211_scan_request *req = rdev->scan_req;
4883 struct nlattr *nest;
4884 int i;
4885
Johannes Berg667503dd2009-07-07 03:56:11 +02004886 ASSERT_RDEV_LOCK(rdev);
4887
Johannes Berg362a4152009-05-24 16:43:15 +02004888 if (WARN_ON(!req))
4889 return 0;
4890
4891 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4892 if (!nest)
4893 goto nla_put_failure;
4894 for (i = 0; i < req->n_ssids; i++)
4895 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4896 nla_nest_end(msg, nest);
4897
4898 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4899 if (!nest)
4900 goto nla_put_failure;
4901 for (i = 0; i < req->n_channels; i++)
4902 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4903 nla_nest_end(msg, nest);
4904
4905 if (req->ie)
4906 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4907
4908 return 0;
4909 nla_put_failure:
4910 return -ENOBUFS;
4911}
4912
Johannes Berga538e2d2009-06-16 19:56:42 +02004913static int nl80211_send_scan_msg(struct sk_buff *msg,
4914 struct cfg80211_registered_device *rdev,
4915 struct net_device *netdev,
4916 u32 pid, u32 seq, int flags,
4917 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01004918{
4919 void *hdr;
4920
4921 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4922 if (!hdr)
4923 return -1;
4924
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05004925 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01004926 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4927
Johannes Berg362a4152009-05-24 16:43:15 +02004928 /* ignore errors and send incomplete event anyway */
4929 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004930
4931 return genlmsg_end(msg, hdr);
4932
4933 nla_put_failure:
4934 genlmsg_cancel(msg, hdr);
4935 return -EMSGSIZE;
4936}
4937
Johannes Berga538e2d2009-06-16 19:56:42 +02004938void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4939 struct net_device *netdev)
4940{
4941 struct sk_buff *msg;
4942
4943 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4944 if (!msg)
4945 return;
4946
4947 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4948 NL80211_CMD_TRIGGER_SCAN) < 0) {
4949 nlmsg_free(msg);
4950 return;
4951 }
4952
Johannes Berg463d0182009-07-14 00:33:35 +02004953 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4954 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02004955}
4956
Johannes Berg2a519312009-02-10 21:25:55 +01004957void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4958 struct net_device *netdev)
4959{
4960 struct sk_buff *msg;
4961
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004962 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004963 if (!msg)
4964 return;
4965
Johannes Berga538e2d2009-06-16 19:56:42 +02004966 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4967 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004968 nlmsg_free(msg);
4969 return;
4970 }
4971
Johannes Berg463d0182009-07-14 00:33:35 +02004972 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4973 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004974}
4975
4976void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4977 struct net_device *netdev)
4978{
4979 struct sk_buff *msg;
4980
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004981 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004982 if (!msg)
4983 return;
4984
Johannes Berga538e2d2009-06-16 19:56:42 +02004985 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4986 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004987 nlmsg_free(msg);
4988 return;
4989 }
4990
Johannes Berg463d0182009-07-14 00:33:35 +02004991 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4992 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004993}
4994
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004995/*
4996 * This can happen on global regulatory changes or device specific settings
4997 * based on custom world regulatory domains.
4998 */
4999void nl80211_send_reg_change_event(struct regulatory_request *request)
5000{
5001 struct sk_buff *msg;
5002 void *hdr;
5003
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005004 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005005 if (!msg)
5006 return;
5007
5008 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5009 if (!hdr) {
5010 nlmsg_free(msg);
5011 return;
5012 }
5013
5014 /* Userspace can always count this one always being set */
5015 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5016
5017 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5018 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5019 NL80211_REGDOM_TYPE_WORLD);
5020 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5021 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5022 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5023 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5024 request->intersect)
5025 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5026 NL80211_REGDOM_TYPE_INTERSECTION);
5027 else {
5028 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5029 NL80211_REGDOM_TYPE_COUNTRY);
5030 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5031 }
5032
5033 if (wiphy_idx_valid(request->wiphy_idx))
5034 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5035
5036 if (genlmsg_end(msg, hdr) < 0) {
5037 nlmsg_free(msg);
5038 return;
5039 }
5040
Johannes Bergbc43b282009-07-25 10:54:13 +02005041 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005042 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005043 GFP_ATOMIC);
5044 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005045
5046 return;
5047
5048nla_put_failure:
5049 genlmsg_cancel(msg, hdr);
5050 nlmsg_free(msg);
5051}
5052
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005053static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5054 struct net_device *netdev,
5055 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005056 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005057{
5058 struct sk_buff *msg;
5059 void *hdr;
5060
Johannes Berge6d6e342009-07-01 21:26:47 +02005061 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005062 if (!msg)
5063 return;
5064
5065 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5066 if (!hdr) {
5067 nlmsg_free(msg);
5068 return;
5069 }
5070
5071 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5072 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5073 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5074
5075 if (genlmsg_end(msg, hdr) < 0) {
5076 nlmsg_free(msg);
5077 return;
5078 }
5079
Johannes Berg463d0182009-07-14 00:33:35 +02005080 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5081 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005082 return;
5083
5084 nla_put_failure:
5085 genlmsg_cancel(msg, hdr);
5086 nlmsg_free(msg);
5087}
5088
5089void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005090 struct net_device *netdev, const u8 *buf,
5091 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005092{
5093 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005094 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005095}
5096
5097void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5098 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005099 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005100{
Johannes Berge6d6e342009-07-01 21:26:47 +02005101 nl80211_send_mlme_event(rdev, netdev, buf, len,
5102 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005103}
5104
Jouni Malinen53b46b82009-03-27 20:53:56 +02005105void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005106 struct net_device *netdev, const u8 *buf,
5107 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005108{
5109 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005110 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005111}
5112
Jouni Malinen53b46b82009-03-27 20:53:56 +02005113void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5114 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005115 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005116{
5117 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005118 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005119}
5120
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005121static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5122 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005123 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005124{
5125 struct sk_buff *msg;
5126 void *hdr;
5127
Johannes Berge6d6e342009-07-01 21:26:47 +02005128 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005129 if (!msg)
5130 return;
5131
5132 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5133 if (!hdr) {
5134 nlmsg_free(msg);
5135 return;
5136 }
5137
5138 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5139 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5140 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5141 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5142
5143 if (genlmsg_end(msg, hdr) < 0) {
5144 nlmsg_free(msg);
5145 return;
5146 }
5147
Johannes Berg463d0182009-07-14 00:33:35 +02005148 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5149 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005150 return;
5151
5152 nla_put_failure:
5153 genlmsg_cancel(msg, hdr);
5154 nlmsg_free(msg);
5155}
5156
5157void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005158 struct net_device *netdev, const u8 *addr,
5159 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005160{
5161 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005162 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005163}
5164
5165void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005166 struct net_device *netdev, const u8 *addr,
5167 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005168{
Johannes Berge6d6e342009-07-01 21:26:47 +02005169 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5170 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005171}
5172
Samuel Ortizb23aa672009-07-01 21:26:54 +02005173void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5174 struct net_device *netdev, const u8 *bssid,
5175 const u8 *req_ie, size_t req_ie_len,
5176 const u8 *resp_ie, size_t resp_ie_len,
5177 u16 status, gfp_t gfp)
5178{
5179 struct sk_buff *msg;
5180 void *hdr;
5181
5182 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5183 if (!msg)
5184 return;
5185
5186 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5187 if (!hdr) {
5188 nlmsg_free(msg);
5189 return;
5190 }
5191
5192 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5193 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5194 if (bssid)
5195 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5196 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5197 if (req_ie)
5198 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5199 if (resp_ie)
5200 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5201
5202 if (genlmsg_end(msg, hdr) < 0) {
5203 nlmsg_free(msg);
5204 return;
5205 }
5206
Johannes Berg463d0182009-07-14 00:33:35 +02005207 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5208 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005209 return;
5210
5211 nla_put_failure:
5212 genlmsg_cancel(msg, hdr);
5213 nlmsg_free(msg);
5214
5215}
5216
5217void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5218 struct net_device *netdev, const u8 *bssid,
5219 const u8 *req_ie, size_t req_ie_len,
5220 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5221{
5222 struct sk_buff *msg;
5223 void *hdr;
5224
5225 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5226 if (!msg)
5227 return;
5228
5229 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5230 if (!hdr) {
5231 nlmsg_free(msg);
5232 return;
5233 }
5234
5235 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5236 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5237 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5238 if (req_ie)
5239 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5240 if (resp_ie)
5241 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5242
5243 if (genlmsg_end(msg, hdr) < 0) {
5244 nlmsg_free(msg);
5245 return;
5246 }
5247
Johannes Berg463d0182009-07-14 00:33:35 +02005248 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5249 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005250 return;
5251
5252 nla_put_failure:
5253 genlmsg_cancel(msg, hdr);
5254 nlmsg_free(msg);
5255
5256}
5257
5258void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5259 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02005260 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005261{
5262 struct sk_buff *msg;
5263 void *hdr;
5264
Johannes Berg667503dd2009-07-07 03:56:11 +02005265 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005266 if (!msg)
5267 return;
5268
5269 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5270 if (!hdr) {
5271 nlmsg_free(msg);
5272 return;
5273 }
5274
5275 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5276 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5277 if (from_ap && reason)
5278 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5279 if (from_ap)
5280 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5281 if (ie)
5282 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5283
5284 if (genlmsg_end(msg, hdr) < 0) {
5285 nlmsg_free(msg);
5286 return;
5287 }
5288
Johannes Berg463d0182009-07-14 00:33:35 +02005289 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5290 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005291 return;
5292
5293 nla_put_failure:
5294 genlmsg_cancel(msg, hdr);
5295 nlmsg_free(msg);
5296
5297}
5298
Johannes Berg04a773a2009-04-19 21:24:32 +02005299void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5300 struct net_device *netdev, const u8 *bssid,
5301 gfp_t gfp)
5302{
5303 struct sk_buff *msg;
5304 void *hdr;
5305
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005306 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005307 if (!msg)
5308 return;
5309
5310 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5311 if (!hdr) {
5312 nlmsg_free(msg);
5313 return;
5314 }
5315
5316 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5317 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5318 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5319
5320 if (genlmsg_end(msg, hdr) < 0) {
5321 nlmsg_free(msg);
5322 return;
5323 }
5324
Johannes Berg463d0182009-07-14 00:33:35 +02005325 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5326 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005327 return;
5328
5329 nla_put_failure:
5330 genlmsg_cancel(msg, hdr);
5331 nlmsg_free(msg);
5332}
5333
Jouni Malinena3b8b052009-03-27 21:59:49 +02005334void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5335 struct net_device *netdev, const u8 *addr,
5336 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005337 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005338{
5339 struct sk_buff *msg;
5340 void *hdr;
5341
Johannes Berge6d6e342009-07-01 21:26:47 +02005342 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005343 if (!msg)
5344 return;
5345
5346 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5347 if (!hdr) {
5348 nlmsg_free(msg);
5349 return;
5350 }
5351
5352 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5353 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5354 if (addr)
5355 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5356 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5357 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5358 if (tsc)
5359 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5360
5361 if (genlmsg_end(msg, hdr) < 0) {
5362 nlmsg_free(msg);
5363 return;
5364 }
5365
Johannes Berg463d0182009-07-14 00:33:35 +02005366 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5367 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005368 return;
5369
5370 nla_put_failure:
5371 genlmsg_cancel(msg, hdr);
5372 nlmsg_free(msg);
5373}
5374
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005375void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5376 struct ieee80211_channel *channel_before,
5377 struct ieee80211_channel *channel_after)
5378{
5379 struct sk_buff *msg;
5380 void *hdr;
5381 struct nlattr *nl_freq;
5382
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005383 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005384 if (!msg)
5385 return;
5386
5387 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5388 if (!hdr) {
5389 nlmsg_free(msg);
5390 return;
5391 }
5392
5393 /*
5394 * Since we are applying the beacon hint to a wiphy we know its
5395 * wiphy_idx is valid
5396 */
5397 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5398
5399 /* Before */
5400 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5401 if (!nl_freq)
5402 goto nla_put_failure;
5403 if (nl80211_msg_put_channel(msg, channel_before))
5404 goto nla_put_failure;
5405 nla_nest_end(msg, nl_freq);
5406
5407 /* After */
5408 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5409 if (!nl_freq)
5410 goto nla_put_failure;
5411 if (nl80211_msg_put_channel(msg, channel_after))
5412 goto nla_put_failure;
5413 nla_nest_end(msg, nl_freq);
5414
5415 if (genlmsg_end(msg, hdr) < 0) {
5416 nlmsg_free(msg);
5417 return;
5418 }
5419
Johannes Berg463d0182009-07-14 00:33:35 +02005420 rcu_read_lock();
5421 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5422 GFP_ATOMIC);
5423 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005424
5425 return;
5426
5427nla_put_failure:
5428 genlmsg_cancel(msg, hdr);
5429 nlmsg_free(msg);
5430}
5431
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005432static void nl80211_send_remain_on_chan_event(
5433 int cmd, struct cfg80211_registered_device *rdev,
5434 struct net_device *netdev, u64 cookie,
5435 struct ieee80211_channel *chan,
5436 enum nl80211_channel_type channel_type,
5437 unsigned int duration, gfp_t gfp)
5438{
5439 struct sk_buff *msg;
5440 void *hdr;
5441
5442 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5443 if (!msg)
5444 return;
5445
5446 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5447 if (!hdr) {
5448 nlmsg_free(msg);
5449 return;
5450 }
5451
5452 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5453 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5454 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5455 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5456 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5457
5458 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5459 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5460
5461 if (genlmsg_end(msg, hdr) < 0) {
5462 nlmsg_free(msg);
5463 return;
5464 }
5465
5466 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5467 nl80211_mlme_mcgrp.id, gfp);
5468 return;
5469
5470 nla_put_failure:
5471 genlmsg_cancel(msg, hdr);
5472 nlmsg_free(msg);
5473}
5474
5475void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5476 struct net_device *netdev, u64 cookie,
5477 struct ieee80211_channel *chan,
5478 enum nl80211_channel_type channel_type,
5479 unsigned int duration, gfp_t gfp)
5480{
5481 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5482 rdev, netdev, cookie, chan,
5483 channel_type, duration, gfp);
5484}
5485
5486void nl80211_send_remain_on_channel_cancel(
5487 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5488 u64 cookie, struct ieee80211_channel *chan,
5489 enum nl80211_channel_type channel_type, gfp_t gfp)
5490{
5491 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5492 rdev, netdev, cookie, chan,
5493 channel_type, 0, gfp);
5494}
5495
Johannes Berg98b62182009-12-23 13:15:44 +01005496void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5497 struct net_device *dev, const u8 *mac_addr,
5498 struct station_info *sinfo, gfp_t gfp)
5499{
5500 struct sk_buff *msg;
5501
5502 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5503 if (!msg)
5504 return;
5505
5506 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5507 nlmsg_free(msg);
5508 return;
5509 }
5510
5511 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5512 nl80211_mlme_mcgrp.id, gfp);
5513}
5514
Johannes Berg2e161f72010-08-12 15:38:38 +02005515int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5516 struct net_device *netdev, u32 nlpid,
5517 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005518{
5519 struct sk_buff *msg;
5520 void *hdr;
5521 int err;
5522
5523 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5524 if (!msg)
5525 return -ENOMEM;
5526
Johannes Berg2e161f72010-08-12 15:38:38 +02005527 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005528 if (!hdr) {
5529 nlmsg_free(msg);
5530 return -ENOMEM;
5531 }
5532
5533 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5534 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5535 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5536 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5537
5538 err = genlmsg_end(msg, hdr);
5539 if (err < 0) {
5540 nlmsg_free(msg);
5541 return err;
5542 }
5543
5544 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5545 if (err < 0)
5546 return err;
5547 return 0;
5548
5549 nla_put_failure:
5550 genlmsg_cancel(msg, hdr);
5551 nlmsg_free(msg);
5552 return -ENOBUFS;
5553}
5554
Johannes Berg2e161f72010-08-12 15:38:38 +02005555void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5556 struct net_device *netdev, u64 cookie,
5557 const u8 *buf, size_t len, bool ack,
5558 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005559{
5560 struct sk_buff *msg;
5561 void *hdr;
5562
5563 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5564 if (!msg)
5565 return;
5566
Johannes Berg2e161f72010-08-12 15:38:38 +02005567 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02005568 if (!hdr) {
5569 nlmsg_free(msg);
5570 return;
5571 }
5572
5573 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5574 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5575 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5576 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5577 if (ack)
5578 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5579
5580 if (genlmsg_end(msg, hdr) < 0) {
5581 nlmsg_free(msg);
5582 return;
5583 }
5584
5585 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5586 return;
5587
5588 nla_put_failure:
5589 genlmsg_cancel(msg, hdr);
5590 nlmsg_free(msg);
5591}
5592
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005593void
5594nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5595 struct net_device *netdev,
5596 enum nl80211_cqm_rssi_threshold_event rssi_event,
5597 gfp_t gfp)
5598{
5599 struct sk_buff *msg;
5600 struct nlattr *pinfoattr;
5601 void *hdr;
5602
5603 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5604 if (!msg)
5605 return;
5606
5607 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5608 if (!hdr) {
5609 nlmsg_free(msg);
5610 return;
5611 }
5612
5613 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5614 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5615
5616 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5617 if (!pinfoattr)
5618 goto nla_put_failure;
5619
5620 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5621 rssi_event);
5622
5623 nla_nest_end(msg, pinfoattr);
5624
5625 if (genlmsg_end(msg, hdr) < 0) {
5626 nlmsg_free(msg);
5627 return;
5628 }
5629
5630 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5631 nl80211_mlme_mcgrp.id, gfp);
5632 return;
5633
5634 nla_put_failure:
5635 genlmsg_cancel(msg, hdr);
5636 nlmsg_free(msg);
5637}
5638
Jouni Malinen026331c2010-02-15 12:53:10 +02005639static int nl80211_netlink_notify(struct notifier_block * nb,
5640 unsigned long state,
5641 void *_notify)
5642{
5643 struct netlink_notify *notify = _notify;
5644 struct cfg80211_registered_device *rdev;
5645 struct wireless_dev *wdev;
5646
5647 if (state != NETLINK_URELEASE)
5648 return NOTIFY_DONE;
5649
5650 rcu_read_lock();
5651
5652 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5653 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02005654 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02005655
5656 rcu_read_unlock();
5657
5658 return NOTIFY_DONE;
5659}
5660
5661static struct notifier_block nl80211_netlink_notifier = {
5662 .notifier_call = nl80211_netlink_notify,
5663};
5664
Johannes Berg55682962007-09-20 13:09:35 -04005665/* initialisation/exit functions */
5666
5667int nl80211_init(void)
5668{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005669 int err;
Johannes Berg55682962007-09-20 13:09:35 -04005670
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005671 err = genl_register_family_with_ops(&nl80211_fam,
5672 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04005673 if (err)
5674 return err;
5675
Johannes Berg55682962007-09-20 13:09:35 -04005676 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5677 if (err)
5678 goto err_out;
5679
Johannes Berg2a519312009-02-10 21:25:55 +01005680 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5681 if (err)
5682 goto err_out;
5683
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005684 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5685 if (err)
5686 goto err_out;
5687
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005688 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5689 if (err)
5690 goto err_out;
5691
Johannes Bergaff89a92009-07-01 21:26:51 +02005692#ifdef CONFIG_NL80211_TESTMODE
5693 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5694 if (err)
5695 goto err_out;
5696#endif
5697
Jouni Malinen026331c2010-02-15 12:53:10 +02005698 err = netlink_register_notifier(&nl80211_netlink_notifier);
5699 if (err)
5700 goto err_out;
5701
Johannes Berg55682962007-09-20 13:09:35 -04005702 return 0;
5703 err_out:
5704 genl_unregister_family(&nl80211_fam);
5705 return err;
5706}
5707
5708void nl80211_exit(void)
5709{
Jouni Malinen026331c2010-02-15 12:53:10 +02005710 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04005711 genl_unregister_family(&nl80211_fam);
5712}