blob: b15eb77195d898daaef20cc9814f0a9829127f1c [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Johannes Berg4c476992010-10-04 21:36:35 +020026static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
27 struct genl_info *info);
28static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
29 struct genl_info *info);
30
Johannes Berg55682962007-09-20 13:09:35 -040031/* the netlink family */
32static struct genl_family nl80211_fam = {
33 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
34 .name = "nl80211", /* have users key off the name instead */
35 .hdrsize = 0, /* no private header */
36 .version = 1, /* no particular meaning now */
37 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020038 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020039 .pre_doit = nl80211_pre_doit,
40 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040041};
42
Johannes Berg79c97e92009-07-07 03:56:12 +020043/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020044static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020045 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040046 struct net_device **dev)
47{
Johannes Berg463d0182009-07-14 00:33:35 +020048 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040049 int ifindex;
50
Johannes Bergbba95fe2008-07-29 13:22:51 +020051 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040052 return -EINVAL;
53
Johannes Bergbba95fe2008-07-29 13:22:51 +020054 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020055 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040056 if (!*dev)
57 return -ENODEV;
58
Johannes Berg463d0182009-07-14 00:33:35 +020059 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020060 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040061 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020062 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040063 }
64
65 return 0;
66}
67
68/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000069static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040070 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070072 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020073 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020074 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053075 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020076 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
77 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
78 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
79 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010080 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040081
82 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
83 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
84 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010085
86 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020087 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010088
Johannes Bergb9454e82009-07-08 13:29:08 +020089 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010090 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
91 .len = WLAN_MAX_KEY_LEN },
92 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
93 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
94 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030095 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berge31b8212010-10-05 19:39:30 +020096 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010097
98 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
99 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
100 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
101 .len = IEEE80211_MAX_DATA_LEN },
102 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
103 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100104 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
105 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
106 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
107 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
108 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100109 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100110 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200111 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100112 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
113 .len = IEEE80211_MAX_MESH_ID_LEN },
114 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300115
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700116 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
117 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
118
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300119 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
120 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
121 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200122 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
123 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300124
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700125 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
126
Jouni Malinen36aedc92008-08-25 11:58:58 +0300127 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
128 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200129
130 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
131 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
132 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100133 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
134 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200135
136 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
137 .len = IEEE80211_MAX_SSID_LEN },
138 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
139 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200140 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300141 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300142 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300143 [NL80211_ATTR_STA_FLAGS2] = {
144 .len = sizeof(struct nl80211_sta_flag_update),
145 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300146 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300147 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
148 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200149 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
150 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
151 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200152 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100153 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100154 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
155 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100156 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
157 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200158 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200159 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
160 .len = IEEE80211_MAX_DATA_LEN },
161 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200162 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200163 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300164 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200165 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300166
167 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
168 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900169
Johannes Berg2e161f72010-08-12 15:38:38 +0200170 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900171
172 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100174
175 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400176};
177
Johannes Berge31b8212010-10-05 19:39:30 +0200178/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000179static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200180 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200181 [NL80211_KEY_IDX] = { .type = NLA_U8 },
182 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
183 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
184 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
185 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200186 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200187};
188
Holger Schuriga0438972009-11-11 11:30:02 +0100189/* ifidx get helper */
190static int nl80211_get_ifidx(struct netlink_callback *cb)
191{
192 int res;
193
194 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
195 nl80211_fam.attrbuf, nl80211_fam.maxattr,
196 nl80211_policy);
197 if (res)
198 return res;
199
200 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
201 return -EINVAL;
202
203 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
204 if (!res)
205 return -EINVAL;
206 return res;
207}
208
Johannes Berg67748892010-10-04 21:14:06 +0200209static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
210 struct netlink_callback *cb,
211 struct cfg80211_registered_device **rdev,
212 struct net_device **dev)
213{
214 int ifidx = cb->args[0];
215 int err;
216
217 if (!ifidx)
218 ifidx = nl80211_get_ifidx(cb);
219 if (ifidx < 0)
220 return ifidx;
221
222 cb->args[0] = ifidx;
223
224 rtnl_lock();
225
226 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
227 if (!*dev) {
228 err = -ENODEV;
229 goto out_rtnl;
230 }
231
232 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100233 if (IS_ERR(*rdev)) {
234 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200235 goto out_rtnl;
236 }
237
238 return 0;
239 out_rtnl:
240 rtnl_unlock();
241 return err;
242}
243
244static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
245{
246 cfg80211_unlock_rdev(rdev);
247 rtnl_unlock();
248}
249
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100250/* IE validation */
251static bool is_valid_ie_attr(const struct nlattr *attr)
252{
253 const u8 *pos;
254 int len;
255
256 if (!attr)
257 return true;
258
259 pos = nla_data(attr);
260 len = nla_len(attr);
261
262 while (len) {
263 u8 elemlen;
264
265 if (len < 2)
266 return false;
267 len -= 2;
268
269 elemlen = pos[1];
270 if (elemlen > len)
271 return false;
272
273 len -= elemlen;
274 pos += 2 + elemlen;
275 }
276
277 return true;
278}
279
Johannes Berg55682962007-09-20 13:09:35 -0400280/* message building helper */
281static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
282 int flags, u8 cmd)
283{
284 /* since there is no private header just add the generic one */
285 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
286}
287
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400288static int nl80211_msg_put_channel(struct sk_buff *msg,
289 struct ieee80211_channel *chan)
290{
291 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
292 chan->center_freq);
293
294 if (chan->flags & IEEE80211_CHAN_DISABLED)
295 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
296 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
297 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
298 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
299 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
300 if (chan->flags & IEEE80211_CHAN_RADAR)
301 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
302
303 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
304 DBM_TO_MBM(chan->max_power));
305
306 return 0;
307
308 nla_put_failure:
309 return -ENOBUFS;
310}
311
Johannes Berg55682962007-09-20 13:09:35 -0400312/* netlink command implementations */
313
Johannes Bergb9454e82009-07-08 13:29:08 +0200314struct key_parse {
315 struct key_params p;
316 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200317 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200318 bool def, defmgmt;
319};
320
321static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
322{
323 struct nlattr *tb[NL80211_KEY_MAX + 1];
324 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
325 nl80211_key_policy);
326 if (err)
327 return err;
328
329 k->def = !!tb[NL80211_KEY_DEFAULT];
330 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
331
332 if (tb[NL80211_KEY_IDX])
333 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
334
335 if (tb[NL80211_KEY_DATA]) {
336 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
337 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
338 }
339
340 if (tb[NL80211_KEY_SEQ]) {
341 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
342 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
343 }
344
345 if (tb[NL80211_KEY_CIPHER])
346 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
347
Johannes Berge31b8212010-10-05 19:39:30 +0200348 if (tb[NL80211_KEY_TYPE]) {
349 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
350 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
351 return -EINVAL;
352 }
353
Johannes Bergb9454e82009-07-08 13:29:08 +0200354 return 0;
355}
356
357static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
358{
359 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
360 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
361 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
362 }
363
364 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
365 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
366 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
367 }
368
369 if (info->attrs[NL80211_ATTR_KEY_IDX])
370 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
371
372 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
373 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
374
375 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
376 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
377
Johannes Berge31b8212010-10-05 19:39:30 +0200378 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
379 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
380 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
381 return -EINVAL;
382 }
383
Johannes Bergb9454e82009-07-08 13:29:08 +0200384 return 0;
385}
386
387static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
388{
389 int err;
390
391 memset(k, 0, sizeof(*k));
392 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200393 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200394
395 if (info->attrs[NL80211_ATTR_KEY])
396 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
397 else
398 err = nl80211_parse_key_old(info, k);
399
400 if (err)
401 return err;
402
403 if (k->def && k->defmgmt)
404 return -EINVAL;
405
406 if (k->idx != -1) {
407 if (k->defmgmt) {
408 if (k->idx < 4 || k->idx > 5)
409 return -EINVAL;
410 } else if (k->def) {
411 if (k->idx < 0 || k->idx > 3)
412 return -EINVAL;
413 } else {
414 if (k->idx < 0 || k->idx > 5)
415 return -EINVAL;
416 }
417 }
418
419 return 0;
420}
421
Johannes Bergfffd0932009-07-08 14:22:54 +0200422static struct cfg80211_cached_keys *
423nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
424 struct nlattr *keys)
425{
426 struct key_parse parse;
427 struct nlattr *key;
428 struct cfg80211_cached_keys *result;
429 int rem, err, def = 0;
430
431 result = kzalloc(sizeof(*result), GFP_KERNEL);
432 if (!result)
433 return ERR_PTR(-ENOMEM);
434
435 result->def = -1;
436 result->defmgmt = -1;
437
438 nla_for_each_nested(key, keys, rem) {
439 memset(&parse, 0, sizeof(parse));
440 parse.idx = -1;
441
442 err = nl80211_parse_key_new(key, &parse);
443 if (err)
444 goto error;
445 err = -EINVAL;
446 if (!parse.p.key)
447 goto error;
448 if (parse.idx < 0 || parse.idx > 4)
449 goto error;
450 if (parse.def) {
451 if (def)
452 goto error;
453 def = 1;
454 result->def = parse.idx;
455 } else if (parse.defmgmt)
456 goto error;
457 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200458 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200459 if (err)
460 goto error;
461 result->params[parse.idx].cipher = parse.p.cipher;
462 result->params[parse.idx].key_len = parse.p.key_len;
463 result->params[parse.idx].key = result->data[parse.idx];
464 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
465 }
466
467 return result;
468 error:
469 kfree(result);
470 return ERR_PTR(err);
471}
472
473static int nl80211_key_allowed(struct wireless_dev *wdev)
474{
475 ASSERT_WDEV_LOCK(wdev);
476
Johannes Bergfffd0932009-07-08 14:22:54 +0200477 switch (wdev->iftype) {
478 case NL80211_IFTYPE_AP:
479 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200480 case NL80211_IFTYPE_P2P_GO:
Johannes Bergfffd0932009-07-08 14:22:54 +0200481 break;
482 case NL80211_IFTYPE_ADHOC:
483 if (!wdev->current_bss)
484 return -ENOLINK;
485 break;
486 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200487 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200488 if (wdev->sme_state != CFG80211_SME_CONNECTED)
489 return -ENOLINK;
490 break;
491 default:
492 return -EINVAL;
493 }
494
495 return 0;
496}
497
Johannes Berg55682962007-09-20 13:09:35 -0400498static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
499 struct cfg80211_registered_device *dev)
500{
501 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100502 struct nlattr *nl_bands, *nl_band;
503 struct nlattr *nl_freqs, *nl_freq;
504 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700505 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100506 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100507 enum ieee80211_band band;
508 struct ieee80211_channel *chan;
509 struct ieee80211_rate *rate;
510 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700511 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg2e161f72010-08-12 15:38:38 +0200512 const struct ieee80211_txrx_stypes *mgmt_stypes =
513 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400514
515 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
516 if (!hdr)
517 return -1;
518
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500519 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400520 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200521
Johannes Bergf5ea9122009-08-07 16:17:38 +0200522 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
523 cfg80211_rdev_list_generation);
524
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200525 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
526 dev->wiphy.retry_short);
527 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
528 dev->wiphy.retry_long);
529 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
530 dev->wiphy.frag_threshold);
531 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
532 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100533 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
534 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100535 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
536 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200537 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
538 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100539
Johannes Berge31b8212010-10-05 19:39:30 +0200540 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
541 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
542
Johannes Berg25e47c182009-04-02 20:14:06 +0200543 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
544 sizeof(u32) * dev->wiphy.n_cipher_suites,
545 dev->wiphy.cipher_suites);
546
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100547 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
548 dev->wiphy.max_num_pmkids);
549
Johannes Bergc0692b82010-08-27 14:26:53 +0300550 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
551 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
552
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900553 if (dev->ops->get_antenna) {
554 u32 tx_ant = 0, rx_ant = 0;
555 int res;
556 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
557 if (!res) {
558 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
559 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
560 }
561 }
562
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700563 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
564 if (!nl_modes)
565 goto nla_put_failure;
566
567 i = 0;
568 while (ifmodes) {
569 if (ifmodes & 1)
570 NLA_PUT_FLAG(msg, i);
571 ifmodes >>= 1;
572 i++;
573 }
574
575 nla_nest_end(msg, nl_modes);
576
Johannes Bergee688b002008-01-24 19:38:39 +0100577 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
578 if (!nl_bands)
579 goto nla_put_failure;
580
581 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
582 if (!dev->wiphy.bands[band])
583 continue;
584
585 nl_band = nla_nest_start(msg, band);
586 if (!nl_band)
587 goto nla_put_failure;
588
Johannes Bergd51626d2008-10-09 12:20:13 +0200589 /* add HT info */
590 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
591 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
592 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
593 &dev->wiphy.bands[band]->ht_cap.mcs);
594 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
595 dev->wiphy.bands[band]->ht_cap.cap);
596 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
597 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
598 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
599 dev->wiphy.bands[band]->ht_cap.ampdu_density);
600 }
601
Johannes Bergee688b002008-01-24 19:38:39 +0100602 /* add frequencies */
603 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
604 if (!nl_freqs)
605 goto nla_put_failure;
606
607 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
608 nl_freq = nla_nest_start(msg, i);
609 if (!nl_freq)
610 goto nla_put_failure;
611
612 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100613
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400614 if (nl80211_msg_put_channel(msg, chan))
615 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200616
Johannes Bergee688b002008-01-24 19:38:39 +0100617 nla_nest_end(msg, nl_freq);
618 }
619
620 nla_nest_end(msg, nl_freqs);
621
622 /* add bitrates */
623 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
624 if (!nl_rates)
625 goto nla_put_failure;
626
627 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
628 nl_rate = nla_nest_start(msg, i);
629 if (!nl_rate)
630 goto nla_put_failure;
631
632 rate = &dev->wiphy.bands[band]->bitrates[i];
633 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
634 rate->bitrate);
635 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
636 NLA_PUT_FLAG(msg,
637 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
638
639 nla_nest_end(msg, nl_rate);
640 }
641
642 nla_nest_end(msg, nl_rates);
643
644 nla_nest_end(msg, nl_band);
645 }
646 nla_nest_end(msg, nl_bands);
647
Johannes Berg8fdc6212009-03-14 09:34:01 +0100648 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
649 if (!nl_cmds)
650 goto nla_put_failure;
651
652 i = 0;
653#define CMD(op, n) \
654 do { \
655 if (dev->ops->op) { \
656 i++; \
657 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
658 } \
659 } while (0)
660
661 CMD(add_virtual_intf, NEW_INTERFACE);
662 CMD(change_virtual_intf, SET_INTERFACE);
663 CMD(add_key, NEW_KEY);
664 CMD(add_beacon, NEW_BEACON);
665 CMD(add_station, NEW_STATION);
666 CMD(add_mpath, NEW_MPATH);
667 CMD(set_mesh_params, SET_MESH_PARAMS);
668 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200669 CMD(auth, AUTHENTICATE);
670 CMD(assoc, ASSOCIATE);
671 CMD(deauth, DEAUTHENTICATE);
672 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200673 CMD(join_ibss, JOIN_IBSS);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100674 CMD(set_pmksa, SET_PMKSA);
675 CMD(del_pmksa, DEL_PMKSA);
676 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100677 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200678 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200679 CMD(mgmt_tx, FRAME);
Johannes Berg5be83de2009-11-19 00:56:28 +0100680 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200681 i++;
682 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
683 }
Johannes Bergf444de02010-05-05 15:25:02 +0200684 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400685 CMD(set_wds_peer, SET_WDS_PEER);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100686
687#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200688
Johannes Berg6829c8782009-07-02 09:13:27 +0200689 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200690 i++;
691 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
692 }
693
Johannes Berg6829c8782009-07-02 09:13:27 +0200694 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200695 i++;
696 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
697 }
698
Johannes Berg8fdc6212009-03-14 09:34:01 +0100699 nla_nest_end(msg, nl_cmds);
700
Johannes Berg2e161f72010-08-12 15:38:38 +0200701 if (mgmt_stypes) {
702 u16 stypes;
703 struct nlattr *nl_ftypes, *nl_ifs;
704 enum nl80211_iftype ift;
705
706 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
707 if (!nl_ifs)
708 goto nla_put_failure;
709
710 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
711 nl_ftypes = nla_nest_start(msg, ift);
712 if (!nl_ftypes)
713 goto nla_put_failure;
714 i = 0;
715 stypes = mgmt_stypes[ift].tx;
716 while (stypes) {
717 if (stypes & 1)
718 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
719 (i << 4) | IEEE80211_FTYPE_MGMT);
720 stypes >>= 1;
721 i++;
722 }
723 nla_nest_end(msg, nl_ftypes);
724 }
725
Johannes Berg74b70a42010-08-24 12:15:53 +0200726 nla_nest_end(msg, nl_ifs);
727
Johannes Berg2e161f72010-08-12 15:38:38 +0200728 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
729 if (!nl_ifs)
730 goto nla_put_failure;
731
732 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
733 nl_ftypes = nla_nest_start(msg, ift);
734 if (!nl_ftypes)
735 goto nla_put_failure;
736 i = 0;
737 stypes = mgmt_stypes[ift].rx;
738 while (stypes) {
739 if (stypes & 1)
740 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
741 (i << 4) | IEEE80211_FTYPE_MGMT);
742 stypes >>= 1;
743 i++;
744 }
745 nla_nest_end(msg, nl_ftypes);
746 }
747 nla_nest_end(msg, nl_ifs);
748 }
749
Johannes Berg55682962007-09-20 13:09:35 -0400750 return genlmsg_end(msg, hdr);
751
752 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700753 genlmsg_cancel(msg, hdr);
754 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400755}
756
757static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
758{
759 int idx = 0;
760 int start = cb->args[0];
761 struct cfg80211_registered_device *dev;
762
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500763 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200764 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200765 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
766 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200767 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400768 continue;
769 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
770 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200771 dev) < 0) {
772 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400773 break;
Julius Volzb4637272008-07-08 14:02:19 +0200774 }
Johannes Berg55682962007-09-20 13:09:35 -0400775 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500776 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400777
778 cb->args[0] = idx;
779
780 return skb->len;
781}
782
783static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
784{
785 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200786 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400787
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700788 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400789 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200790 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400791
Johannes Berg4c476992010-10-04 21:36:35 +0200792 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
793 nlmsg_free(msg);
794 return -ENOBUFS;
795 }
Johannes Berg55682962007-09-20 13:09:35 -0400796
Johannes Berg134e6372009-07-10 09:51:34 +0000797 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400798}
799
Jouni Malinen31888482008-10-30 16:59:24 +0200800static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
801 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
802 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
803 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
804 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
805 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
806};
807
808static int parse_txq_params(struct nlattr *tb[],
809 struct ieee80211_txq_params *txq_params)
810{
811 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
812 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
813 !tb[NL80211_TXQ_ATTR_AIFS])
814 return -EINVAL;
815
816 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
817 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
818 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
819 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
820 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
821
822 return 0;
823}
824
Johannes Bergf444de02010-05-05 15:25:02 +0200825static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
826{
827 /*
828 * You can only set the channel explicitly for AP, mesh
829 * and WDS type interfaces; all others have their channel
830 * managed via their respective "establish a connection"
831 * command (connect, join, ...)
832 *
833 * Monitors are special as they are normally slaved to
834 * whatever else is going on, so they behave as though
835 * you tried setting the wiphy channel itself.
836 */
837 return !wdev ||
838 wdev->iftype == NL80211_IFTYPE_AP ||
839 wdev->iftype == NL80211_IFTYPE_WDS ||
840 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200841 wdev->iftype == NL80211_IFTYPE_MONITOR ||
842 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200843}
844
845static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
846 struct wireless_dev *wdev,
847 struct genl_info *info)
848{
849 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
850 u32 freq;
851 int result;
852
853 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
854 return -EINVAL;
855
856 if (!nl80211_can_set_dev_channel(wdev))
857 return -EOPNOTSUPP;
858
859 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
860 channel_type = nla_get_u32(info->attrs[
861 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
862 if (channel_type != NL80211_CHAN_NO_HT &&
863 channel_type != NL80211_CHAN_HT20 &&
864 channel_type != NL80211_CHAN_HT40PLUS &&
865 channel_type != NL80211_CHAN_HT40MINUS)
866 return -EINVAL;
867 }
868
869 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
870
871 mutex_lock(&rdev->devlist_mtx);
872 if (wdev) {
873 wdev_lock(wdev);
874 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
875 wdev_unlock(wdev);
876 } else {
877 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
878 }
879 mutex_unlock(&rdev->devlist_mtx);
880
881 return result;
882}
883
884static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
885{
Johannes Berg4c476992010-10-04 21:36:35 +0200886 struct cfg80211_registered_device *rdev = info->user_ptr[0];
887 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +0200888
Johannes Berg4c476992010-10-04 21:36:35 +0200889 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +0200890}
891
Bill Jordane8347eb2010-10-01 13:54:28 -0400892static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
893{
Johannes Berg43b19952010-10-07 13:10:30 +0200894 struct cfg80211_registered_device *rdev = info->user_ptr[0];
895 struct net_device *dev = info->user_ptr[1];
896 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +0200897 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -0400898
899 if (!info->attrs[NL80211_ATTR_MAC])
900 return -EINVAL;
901
Johannes Berg43b19952010-10-07 13:10:30 +0200902 if (netif_running(dev))
903 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -0400904
Johannes Berg43b19952010-10-07 13:10:30 +0200905 if (!rdev->ops->set_wds_peer)
906 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400907
Johannes Berg43b19952010-10-07 13:10:30 +0200908 if (wdev->iftype != NL80211_IFTYPE_WDS)
909 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400910
911 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +0200912 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -0400913}
914
915
Johannes Berg55682962007-09-20 13:09:35 -0400916static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
917{
918 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200919 struct net_device *netdev = NULL;
920 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -0400921 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200922 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200923 u32 changed;
924 u8 retry_short = 0, retry_long = 0;
925 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100926 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400927
Johannes Bergf444de02010-05-05 15:25:02 +0200928 /*
929 * Try to find the wiphy and netdev. Normally this
930 * function shouldn't need the netdev, but this is
931 * done for backward compatibility -- previously
932 * setting the channel was done per wiphy, but now
933 * it is per netdev. Previous userland like hostapd
934 * also passed a netdev to set_wiphy, so that it is
935 * possible to let that go to the right netdev!
936 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100937 mutex_lock(&cfg80211_mutex);
938
Johannes Bergf444de02010-05-05 15:25:02 +0200939 if (info->attrs[NL80211_ATTR_IFINDEX]) {
940 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
941
942 netdev = dev_get_by_index(genl_info_net(info), ifindex);
943 if (netdev && netdev->ieee80211_ptr) {
944 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
945 mutex_lock(&rdev->mtx);
946 } else
947 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100948 }
949
Johannes Bergf444de02010-05-05 15:25:02 +0200950 if (!netdev) {
951 rdev = __cfg80211_rdev_from_info(info);
952 if (IS_ERR(rdev)) {
953 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +0200954 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +0200955 }
956 wdev = NULL;
957 netdev = NULL;
958 result = 0;
959
960 mutex_lock(&rdev->mtx);
961 } else if (netif_running(netdev) &&
962 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
963 wdev = netdev->ieee80211_ptr;
964 else
965 wdev = NULL;
966
967 /*
968 * end workaround code, by now the rdev is available
969 * and locked, and wdev may or may not be NULL.
970 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100971
972 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200973 result = cfg80211_dev_rename(
974 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100975
976 mutex_unlock(&cfg80211_mutex);
977
978 if (result)
979 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400980
Jouni Malinen31888482008-10-30 16:59:24 +0200981 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
982 struct ieee80211_txq_params txq_params;
983 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
984
985 if (!rdev->ops->set_txq_params) {
986 result = -EOPNOTSUPP;
987 goto bad_res;
988 }
989
990 nla_for_each_nested(nl_txq_params,
991 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
992 rem_txq_params) {
993 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
994 nla_data(nl_txq_params),
995 nla_len(nl_txq_params),
996 txq_params_policy);
997 result = parse_txq_params(tb, &txq_params);
998 if (result)
999 goto bad_res;
1000
1001 result = rdev->ops->set_txq_params(&rdev->wiphy,
1002 &txq_params);
1003 if (result)
1004 goto bad_res;
1005 }
1006 }
1007
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001008 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001009 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001010 if (result)
1011 goto bad_res;
1012 }
1013
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001014 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1015 enum nl80211_tx_power_setting type;
1016 int idx, mbm = 0;
1017
1018 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001019 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001020 goto bad_res;
1021 }
1022
1023 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1024 type = nla_get_u32(info->attrs[idx]);
1025
1026 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1027 (type != NL80211_TX_POWER_AUTOMATIC)) {
1028 result = -EINVAL;
1029 goto bad_res;
1030 }
1031
1032 if (type != NL80211_TX_POWER_AUTOMATIC) {
1033 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1034 mbm = nla_get_u32(info->attrs[idx]);
1035 }
1036
1037 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1038 if (result)
1039 goto bad_res;
1040 }
1041
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001042 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1043 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1044 u32 tx_ant, rx_ant;
1045 if (!rdev->ops->set_antenna) {
1046 result = -EOPNOTSUPP;
1047 goto bad_res;
1048 }
1049
1050 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1051 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1052
1053 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1054 if (result)
1055 goto bad_res;
1056 }
1057
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001058 changed = 0;
1059
1060 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1061 retry_short = nla_get_u8(
1062 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1063 if (retry_short == 0) {
1064 result = -EINVAL;
1065 goto bad_res;
1066 }
1067 changed |= WIPHY_PARAM_RETRY_SHORT;
1068 }
1069
1070 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1071 retry_long = nla_get_u8(
1072 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1073 if (retry_long == 0) {
1074 result = -EINVAL;
1075 goto bad_res;
1076 }
1077 changed |= WIPHY_PARAM_RETRY_LONG;
1078 }
1079
1080 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1081 frag_threshold = nla_get_u32(
1082 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1083 if (frag_threshold < 256) {
1084 result = -EINVAL;
1085 goto bad_res;
1086 }
1087 if (frag_threshold != (u32) -1) {
1088 /*
1089 * Fragments (apart from the last one) are required to
1090 * have even length. Make the fragmentation code
1091 * simpler by stripping LSB should someone try to use
1092 * odd threshold value.
1093 */
1094 frag_threshold &= ~0x1;
1095 }
1096 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1097 }
1098
1099 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1100 rts_threshold = nla_get_u32(
1101 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1102 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1103 }
1104
Lukáš Turek81077e82009-12-21 22:50:47 +01001105 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1106 coverage_class = nla_get_u8(
1107 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1108 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1109 }
1110
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001111 if (changed) {
1112 u8 old_retry_short, old_retry_long;
1113 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001114 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001115
1116 if (!rdev->ops->set_wiphy_params) {
1117 result = -EOPNOTSUPP;
1118 goto bad_res;
1119 }
1120
1121 old_retry_short = rdev->wiphy.retry_short;
1122 old_retry_long = rdev->wiphy.retry_long;
1123 old_frag_threshold = rdev->wiphy.frag_threshold;
1124 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001125 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001126
1127 if (changed & WIPHY_PARAM_RETRY_SHORT)
1128 rdev->wiphy.retry_short = retry_short;
1129 if (changed & WIPHY_PARAM_RETRY_LONG)
1130 rdev->wiphy.retry_long = retry_long;
1131 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1132 rdev->wiphy.frag_threshold = frag_threshold;
1133 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1134 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001135 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1136 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001137
1138 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1139 if (result) {
1140 rdev->wiphy.retry_short = old_retry_short;
1141 rdev->wiphy.retry_long = old_retry_long;
1142 rdev->wiphy.frag_threshold = old_frag_threshold;
1143 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001144 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001145 }
1146 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001147
Johannes Berg306d6112008-12-08 12:39:04 +01001148 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001149 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001150 if (netdev)
1151 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001152 return result;
1153}
1154
1155
1156static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001157 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001158 struct net_device *dev)
1159{
1160 void *hdr;
1161
1162 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1163 if (!hdr)
1164 return -1;
1165
1166 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001167 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001168 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001169 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001170
1171 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1172 rdev->devlist_generation ^
1173 (cfg80211_rdev_list_generation << 2));
1174
Johannes Berg55682962007-09-20 13:09:35 -04001175 return genlmsg_end(msg, hdr);
1176
1177 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001178 genlmsg_cancel(msg, hdr);
1179 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001180}
1181
1182static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1183{
1184 int wp_idx = 0;
1185 int if_idx = 0;
1186 int wp_start = cb->args[0];
1187 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001188 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001189 struct wireless_dev *wdev;
1190
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001191 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001192 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1193 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001194 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001195 if (wp_idx < wp_start) {
1196 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001197 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001198 }
Johannes Berg55682962007-09-20 13:09:35 -04001199 if_idx = 0;
1200
Johannes Bergf5ea9122009-08-07 16:17:38 +02001201 mutex_lock(&rdev->devlist_mtx);
1202 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001203 if (if_idx < if_start) {
1204 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001205 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001206 }
Johannes Berg55682962007-09-20 13:09:35 -04001207 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1208 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001209 rdev, wdev->netdev) < 0) {
1210 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001211 goto out;
1212 }
1213 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001214 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001215 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001216
1217 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001218 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001219 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001220 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001221
1222 cb->args[0] = wp_idx;
1223 cb->args[1] = if_idx;
1224
1225 return skb->len;
1226}
1227
1228static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1229{
1230 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001231 struct cfg80211_registered_device *dev = info->user_ptr[0];
1232 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001233
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001234 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001235 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001236 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001237
Johannes Bergd7264052009-04-19 16:23:20 +02001238 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001239 dev, netdev) < 0) {
1240 nlmsg_free(msg);
1241 return -ENOBUFS;
1242 }
Johannes Berg55682962007-09-20 13:09:35 -04001243
Johannes Berg134e6372009-07-10 09:51:34 +00001244 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001245}
1246
Michael Wu66f7ac52008-01-31 19:48:22 +01001247static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1248 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1249 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1250 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1251 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1252 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1253};
1254
1255static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1256{
1257 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1258 int flag;
1259
1260 *mntrflags = 0;
1261
1262 if (!nla)
1263 return -EINVAL;
1264
1265 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1266 nla, mntr_flags_policy))
1267 return -EINVAL;
1268
1269 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1270 if (flags[flag])
1271 *mntrflags |= (1<<flag);
1272
1273 return 0;
1274}
1275
Johannes Berg9bc383d2009-11-19 11:55:19 +01001276static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001277 struct net_device *netdev, u8 use_4addr,
1278 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001279{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001280 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001281 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001282 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001283 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001284 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001285
1286 switch (iftype) {
1287 case NL80211_IFTYPE_AP_VLAN:
1288 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1289 return 0;
1290 break;
1291 case NL80211_IFTYPE_STATION:
1292 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1293 return 0;
1294 break;
1295 default:
1296 break;
1297 }
1298
1299 return -EOPNOTSUPP;
1300}
1301
Johannes Berg55682962007-09-20 13:09:35 -04001302static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1303{
Johannes Berg4c476992010-10-04 21:36:35 +02001304 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001305 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001306 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001307 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001308 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001309 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001310 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001311
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001312 memset(&params, 0, sizeof(params));
1313
Johannes Berg04a773a2009-04-19 21:24:32 +02001314 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001315
Johannes Berg723b0382008-09-16 20:22:09 +02001316 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001317 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001318 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001319 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001320 if (ntype > NL80211_IFTYPE_MAX)
1321 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001322 }
1323
Johannes Berg92ffe052008-09-16 20:39:36 +02001324 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001325 if (ntype != NL80211_IFTYPE_MESH_POINT)
1326 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001327 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1328 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001329 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001330 }
1331
Felix Fietkau8b787642009-11-10 18:53:10 +01001332 if (info->attrs[NL80211_ATTR_4ADDR]) {
1333 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1334 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001335 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001336 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001337 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001338 } else {
1339 params.use_4addr = -1;
1340 }
1341
Johannes Berg92ffe052008-09-16 20:39:36 +02001342 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001343 if (ntype != NL80211_IFTYPE_MONITOR)
1344 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001345 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1346 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001347 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001348 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001349
1350 flags = &_flags;
1351 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001352 }
Johannes Berg3b858752009-03-12 09:55:09 +01001353
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001354 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001355 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001356 else
1357 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001358
Johannes Berg9bc383d2009-11-19 11:55:19 +01001359 if (!err && params.use_4addr != -1)
1360 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1361
Johannes Berg55682962007-09-20 13:09:35 -04001362 return err;
1363}
1364
1365static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1366{
Johannes Berg4c476992010-10-04 21:36:35 +02001367 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001368 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001369 int err;
1370 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001371 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001372
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001373 memset(&params, 0, sizeof(params));
1374
Johannes Berg55682962007-09-20 13:09:35 -04001375 if (!info->attrs[NL80211_ATTR_IFNAME])
1376 return -EINVAL;
1377
1378 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1379 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1380 if (type > NL80211_IFTYPE_MAX)
1381 return -EINVAL;
1382 }
1383
Johannes Berg79c97e92009-07-07 03:56:12 +02001384 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001385 !(rdev->wiphy.interface_modes & (1 << type)))
1386 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001387
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001388 if (type == NL80211_IFTYPE_MESH_POINT &&
1389 info->attrs[NL80211_ATTR_MESH_ID]) {
1390 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1391 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1392 }
1393
Johannes Berg9bc383d2009-11-19 11:55:19 +01001394 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001395 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001396 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001397 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001398 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001399 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001400
Michael Wu66f7ac52008-01-31 19:48:22 +01001401 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1402 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1403 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001404 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001405 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001406 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001407
Johannes Berg55682962007-09-20 13:09:35 -04001408 return err;
1409}
1410
1411static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1412{
Johannes Berg4c476992010-10-04 21:36:35 +02001413 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1414 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001415
Johannes Berg4c476992010-10-04 21:36:35 +02001416 if (!rdev->ops->del_virtual_intf)
1417 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001418
Johannes Berg4c476992010-10-04 21:36:35 +02001419 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001420}
1421
Johannes Berg41ade002007-12-19 02:03:29 +01001422struct get_key_cookie {
1423 struct sk_buff *msg;
1424 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001425 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001426};
1427
1428static void get_key_callback(void *c, struct key_params *params)
1429{
Johannes Bergb9454e82009-07-08 13:29:08 +02001430 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001431 struct get_key_cookie *cookie = c;
1432
1433 if (params->key)
1434 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1435 params->key_len, params->key);
1436
1437 if (params->seq)
1438 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1439 params->seq_len, params->seq);
1440
1441 if (params->cipher)
1442 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1443 params->cipher);
1444
Johannes Bergb9454e82009-07-08 13:29:08 +02001445 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1446 if (!key)
1447 goto nla_put_failure;
1448
1449 if (params->key)
1450 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1451 params->key_len, params->key);
1452
1453 if (params->seq)
1454 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1455 params->seq_len, params->seq);
1456
1457 if (params->cipher)
1458 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1459 params->cipher);
1460
1461 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1462
1463 nla_nest_end(cookie->msg, key);
1464
Johannes Berg41ade002007-12-19 02:03:29 +01001465 return;
1466 nla_put_failure:
1467 cookie->error = 1;
1468}
1469
1470static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1471{
Johannes Berg4c476992010-10-04 21:36:35 +02001472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001473 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001474 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001475 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001476 const u8 *mac_addr = NULL;
1477 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001478 struct get_key_cookie cookie = {
1479 .error = 0,
1480 };
1481 void *hdr;
1482 struct sk_buff *msg;
1483
1484 if (info->attrs[NL80211_ATTR_KEY_IDX])
1485 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1486
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001487 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001488 return -EINVAL;
1489
1490 if (info->attrs[NL80211_ATTR_MAC])
1491 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1492
Johannes Berge31b8212010-10-05 19:39:30 +02001493 pairwise = !!mac_addr;
1494 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1495 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1496 if (kt >= NUM_NL80211_KEYTYPES)
1497 return -EINVAL;
1498 if (kt != NL80211_KEYTYPE_GROUP &&
1499 kt != NL80211_KEYTYPE_PAIRWISE)
1500 return -EINVAL;
1501 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1502 }
1503
Johannes Berg4c476992010-10-04 21:36:35 +02001504 if (!rdev->ops->get_key)
1505 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001506
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001507 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001508 if (!msg)
1509 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001510
1511 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1512 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001513 if (IS_ERR(hdr))
1514 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001515
1516 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001517 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001518
1519 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1520 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1521 if (mac_addr)
1522 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1523
Johannes Berge31b8212010-10-05 19:39:30 +02001524 if (pairwise && mac_addr &&
1525 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1526 return -ENOENT;
1527
1528 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1529 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001530
1531 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001532 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001533
1534 if (cookie.error)
1535 goto nla_put_failure;
1536
1537 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001538 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001539
1540 nla_put_failure:
1541 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001542 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001543 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001544 return err;
1545}
1546
1547static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1548{
Johannes Berg4c476992010-10-04 21:36:35 +02001549 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001550 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001551 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001552 struct net_device *dev = info->user_ptr[1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001553 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1554 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001555
Johannes Bergb9454e82009-07-08 13:29:08 +02001556 err = nl80211_parse_key(info, &key);
1557 if (err)
1558 return err;
1559
1560 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001561 return -EINVAL;
1562
Johannes Bergb9454e82009-07-08 13:29:08 +02001563 /* only support setting default key */
1564 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001565 return -EINVAL;
1566
Johannes Bergb9454e82009-07-08 13:29:08 +02001567 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001568 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001569 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001570 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001571
Johannes Berg4c476992010-10-04 21:36:35 +02001572 if (!func)
1573 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001574
Johannes Bergfffd0932009-07-08 14:22:54 +02001575 wdev_lock(dev->ieee80211_ptr);
1576 err = nl80211_key_allowed(dev->ieee80211_ptr);
1577 if (!err)
1578 err = func(&rdev->wiphy, dev, key.idx);
1579
Johannes Berg3d23e342009-09-29 23:27:28 +02001580#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001581 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001582 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001583 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001584 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001585 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001586 }
1587#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001588 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001589
Johannes Berg41ade002007-12-19 02:03:29 +01001590 return err;
1591}
1592
1593static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1594{
Johannes Berg4c476992010-10-04 21:36:35 +02001595 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001596 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001597 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001598 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001599 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001600
Johannes Bergb9454e82009-07-08 13:29:08 +02001601 err = nl80211_parse_key(info, &key);
1602 if (err)
1603 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001604
Johannes Bergb9454e82009-07-08 13:29:08 +02001605 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001606 return -EINVAL;
1607
Johannes Berg41ade002007-12-19 02:03:29 +01001608 if (info->attrs[NL80211_ATTR_MAC])
1609 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1610
Johannes Berge31b8212010-10-05 19:39:30 +02001611 if (key.type == -1) {
1612 if (mac_addr)
1613 key.type = NL80211_KEYTYPE_PAIRWISE;
1614 else
1615 key.type = NL80211_KEYTYPE_GROUP;
1616 }
1617
1618 /* for now */
1619 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1620 key.type != NL80211_KEYTYPE_GROUP)
1621 return -EINVAL;
1622
Johannes Berg4c476992010-10-04 21:36:35 +02001623 if (!rdev->ops->add_key)
1624 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001625
Johannes Berge31b8212010-10-05 19:39:30 +02001626 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1627 key.type == NL80211_KEYTYPE_PAIRWISE,
1628 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001629 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001630
1631 wdev_lock(dev->ieee80211_ptr);
1632 err = nl80211_key_allowed(dev->ieee80211_ptr);
1633 if (!err)
1634 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001635 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001636 mac_addr, &key.p);
1637 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001638
Johannes Berg41ade002007-12-19 02:03:29 +01001639 return err;
1640}
1641
1642static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1643{
Johannes Berg4c476992010-10-04 21:36:35 +02001644 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001645 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001646 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001647 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001648 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001649
Johannes Bergb9454e82009-07-08 13:29:08 +02001650 err = nl80211_parse_key(info, &key);
1651 if (err)
1652 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001653
1654 if (info->attrs[NL80211_ATTR_MAC])
1655 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1656
Johannes Berge31b8212010-10-05 19:39:30 +02001657 if (key.type == -1) {
1658 if (mac_addr)
1659 key.type = NL80211_KEYTYPE_PAIRWISE;
1660 else
1661 key.type = NL80211_KEYTYPE_GROUP;
1662 }
1663
1664 /* for now */
1665 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1666 key.type != NL80211_KEYTYPE_GROUP)
1667 return -EINVAL;
1668
Johannes Berg4c476992010-10-04 21:36:35 +02001669 if (!rdev->ops->del_key)
1670 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001671
Johannes Bergfffd0932009-07-08 14:22:54 +02001672 wdev_lock(dev->ieee80211_ptr);
1673 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001674
1675 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1676 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1677 err = -ENOENT;
1678
Johannes Bergfffd0932009-07-08 14:22:54 +02001679 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001680 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1681 key.type == NL80211_KEYTYPE_PAIRWISE,
1682 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001683
Johannes Berg3d23e342009-09-29 23:27:28 +02001684#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001685 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001686 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001687 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001688 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001689 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1690 }
1691#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001692 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001693
Johannes Berg41ade002007-12-19 02:03:29 +01001694 return err;
1695}
1696
Johannes Berged1b6cc2007-12-19 02:03:32 +01001697static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1698{
1699 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1700 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001701 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1702 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001703 struct beacon_parameters params;
1704 int haveinfo = 0;
1705
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001706 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1707 return -EINVAL;
1708
Johannes Berg074ac8d2010-09-16 14:58:22 +02001709 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001710 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1711 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001712
Johannes Berged1b6cc2007-12-19 02:03:32 +01001713 switch (info->genlhdr->cmd) {
1714 case NL80211_CMD_NEW_BEACON:
1715 /* these are required for NEW_BEACON */
1716 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1717 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001718 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1719 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001720
Johannes Berg79c97e92009-07-07 03:56:12 +02001721 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001722 break;
1723 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001724 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001725 break;
1726 default:
1727 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001728 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001729 }
1730
Johannes Berg4c476992010-10-04 21:36:35 +02001731 if (!call)
1732 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001733
1734 memset(&params, 0, sizeof(params));
1735
1736 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1737 params.interval =
1738 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1739 haveinfo = 1;
1740 }
1741
1742 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1743 params.dtim_period =
1744 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1745 haveinfo = 1;
1746 }
1747
1748 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1749 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1750 params.head_len =
1751 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1752 haveinfo = 1;
1753 }
1754
1755 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1756 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1757 params.tail_len =
1758 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1759 haveinfo = 1;
1760 }
1761
Johannes Berg4c476992010-10-04 21:36:35 +02001762 if (!haveinfo)
1763 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001764
Johannes Berg4c476992010-10-04 21:36:35 +02001765 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001766}
1767
1768static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1769{
Johannes Berg4c476992010-10-04 21:36:35 +02001770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1771 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001772
Johannes Berg4c476992010-10-04 21:36:35 +02001773 if (!rdev->ops->del_beacon)
1774 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001775
Johannes Berg074ac8d2010-09-16 14:58:22 +02001776 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001777 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1778 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001779
Johannes Berg4c476992010-10-04 21:36:35 +02001780 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001781}
1782
Johannes Berg5727ef12007-12-19 02:03:34 +01001783static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1784 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1785 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1786 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001787 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001788};
1789
Johannes Bergeccb8e82009-05-11 21:57:56 +03001790static int parse_station_flags(struct genl_info *info,
1791 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001792{
1793 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001794 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001795 int flag;
1796
Johannes Bergeccb8e82009-05-11 21:57:56 +03001797 /*
1798 * Try parsing the new attribute first so userspace
1799 * can specify both for older kernels.
1800 */
1801 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1802 if (nla) {
1803 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001804
Johannes Bergeccb8e82009-05-11 21:57:56 +03001805 sta_flags = nla_data(nla);
1806 params->sta_flags_mask = sta_flags->mask;
1807 params->sta_flags_set = sta_flags->set;
1808 if ((params->sta_flags_mask |
1809 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1810 return -EINVAL;
1811 return 0;
1812 }
1813
1814 /* if present, parse the old attribute */
1815
1816 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001817 if (!nla)
1818 return 0;
1819
1820 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1821 nla, sta_flags_policy))
1822 return -EINVAL;
1823
Johannes Bergeccb8e82009-05-11 21:57:56 +03001824 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1825 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001826
1827 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1828 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001829 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001830
1831 return 0;
1832}
1833
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001834static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1835 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001836 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001837{
1838 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001839 struct nlattr *sinfoattr, *txrate;
1840 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001841
1842 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1843 if (!hdr)
1844 return -1;
1845
1846 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1847 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1848
Johannes Bergf5ea9122009-08-07 16:17:38 +02001849 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1850
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001851 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1852 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001853 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001854 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1855 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1856 sinfo->inactive_time);
1857 if (sinfo->filled & STATION_INFO_RX_BYTES)
1858 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1859 sinfo->rx_bytes);
1860 if (sinfo->filled & STATION_INFO_TX_BYTES)
1861 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1862 sinfo->tx_bytes);
1863 if (sinfo->filled & STATION_INFO_LLID)
1864 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1865 sinfo->llid);
1866 if (sinfo->filled & STATION_INFO_PLID)
1867 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1868 sinfo->plid);
1869 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1870 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1871 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001872 if (sinfo->filled & STATION_INFO_SIGNAL)
1873 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1874 sinfo->signal);
1875 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1876 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1877 if (!txrate)
1878 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001879
John W. Linville254416a2009-12-09 16:43:52 -05001880 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1881 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001882 if (bitrate > 0)
1883 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1884
1885 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1886 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1887 sinfo->txrate.mcs);
1888 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1889 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1890 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1891 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1892
1893 nla_nest_end(msg, txrate);
1894 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001895 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1896 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1897 sinfo->rx_packets);
1898 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1899 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1900 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09001901 if (sinfo->filled & STATION_INFO_TX_RETRIES)
1902 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
1903 sinfo->tx_retries);
1904 if (sinfo->filled & STATION_INFO_TX_FAILED)
1905 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
1906 sinfo->tx_failed);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001907 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001908
1909 return genlmsg_end(msg, hdr);
1910
1911 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001912 genlmsg_cancel(msg, hdr);
1913 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001914}
1915
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001916static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001917 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001918{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001919 struct station_info sinfo;
1920 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001921 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001922 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001923 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001924 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001925
Johannes Berg67748892010-10-04 21:14:06 +02001926 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1927 if (err)
1928 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001929
1930 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001931 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001932 goto out_err;
1933 }
1934
Johannes Bergbba95fe2008-07-29 13:22:51 +02001935 while (1) {
1936 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1937 mac_addr, &sinfo);
1938 if (err == -ENOENT)
1939 break;
1940 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001941 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001942
1943 if (nl80211_send_station(skb,
1944 NETLINK_CB(cb->skb).pid,
1945 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1946 netdev, mac_addr,
1947 &sinfo) < 0)
1948 goto out;
1949
1950 sta_idx++;
1951 }
1952
1953
1954 out:
1955 cb->args[1] = sta_idx;
1956 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001957 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02001958 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001959
1960 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001961}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001962
Johannes Berg5727ef12007-12-19 02:03:34 +01001963static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1964{
Johannes Berg4c476992010-10-04 21:36:35 +02001965 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1966 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001967 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001968 struct sk_buff *msg;
1969 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02001970 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001971
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001972 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001973
1974 if (!info->attrs[NL80211_ATTR_MAC])
1975 return -EINVAL;
1976
1977 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1978
Johannes Berg4c476992010-10-04 21:36:35 +02001979 if (!rdev->ops->get_station)
1980 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001981
Johannes Berg79c97e92009-07-07 03:56:12 +02001982 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001983 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001984 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001985
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001986 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001987 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001988 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001989
1990 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001991 dev, mac_addr, &sinfo) < 0) {
1992 nlmsg_free(msg);
1993 return -ENOBUFS;
1994 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001995
Johannes Berg4c476992010-10-04 21:36:35 +02001996 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01001997}
1998
1999/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002000 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002001 */
Johannes Berg463d0182009-07-14 00:33:35 +02002002static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002003 struct cfg80211_registered_device *rdev,
2004 struct net_device **vlan)
2005{
Johannes Berg463d0182009-07-14 00:33:35 +02002006 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002007 *vlan = NULL;
2008
2009 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002010 *vlan = dev_get_by_index(genl_info_net(info),
2011 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002012 if (!*vlan)
2013 return -ENODEV;
2014 if (!(*vlan)->ieee80211_ptr)
2015 return -EINVAL;
2016 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2017 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002018 if (!netif_running(*vlan))
2019 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002020 }
2021 return 0;
2022}
2023
2024static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2025{
Johannes Berg4c476992010-10-04 21:36:35 +02002026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002027 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002028 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002029 struct station_parameters params;
2030 u8 *mac_addr = NULL;
2031
2032 memset(&params, 0, sizeof(params));
2033
2034 params.listen_interval = -1;
2035
2036 if (info->attrs[NL80211_ATTR_STA_AID])
2037 return -EINVAL;
2038
2039 if (!info->attrs[NL80211_ATTR_MAC])
2040 return -EINVAL;
2041
2042 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2043
2044 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2045 params.supported_rates =
2046 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2047 params.supported_rates_len =
2048 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2049 }
2050
2051 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2052 params.listen_interval =
2053 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2054
Jouni Malinen36aedc92008-08-25 11:58:58 +03002055 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2056 params.ht_capa =
2057 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2058
Johannes Bergeccb8e82009-05-11 21:57:56 +03002059 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002060 return -EINVAL;
2061
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002062 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2063 params.plink_action =
2064 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2065
Johannes Berg463d0182009-07-14 00:33:35 +02002066 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002067 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002068 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002069
2070 /* validate settings */
2071 err = 0;
2072
2073 switch (dev->ieee80211_ptr->iftype) {
2074 case NL80211_IFTYPE_AP:
2075 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002076 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002077 /* disallow mesh-specific things */
2078 if (params.plink_action)
2079 err = -EINVAL;
2080 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002081 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002082 case NL80211_IFTYPE_STATION:
2083 /* disallow everything but AUTHORIZED flag */
2084 if (params.plink_action)
2085 err = -EINVAL;
2086 if (params.vlan)
2087 err = -EINVAL;
2088 if (params.supported_rates)
2089 err = -EINVAL;
2090 if (params.ht_capa)
2091 err = -EINVAL;
2092 if (params.listen_interval >= 0)
2093 err = -EINVAL;
2094 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2095 err = -EINVAL;
2096 break;
2097 case NL80211_IFTYPE_MESH_POINT:
2098 /* disallow things mesh doesn't support */
2099 if (params.vlan)
2100 err = -EINVAL;
2101 if (params.ht_capa)
2102 err = -EINVAL;
2103 if (params.listen_interval >= 0)
2104 err = -EINVAL;
2105 if (params.supported_rates)
2106 err = -EINVAL;
2107 if (params.sta_flags_mask)
2108 err = -EINVAL;
2109 break;
2110 default:
2111 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002112 }
2113
Johannes Berg5727ef12007-12-19 02:03:34 +01002114 if (err)
2115 goto out;
2116
Johannes Berg79c97e92009-07-07 03:56:12 +02002117 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002118 err = -EOPNOTSUPP;
2119 goto out;
2120 }
2121
Johannes Berg79c97e92009-07-07 03:56:12 +02002122 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002123
2124 out:
2125 if (params.vlan)
2126 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002127
Johannes Berg5727ef12007-12-19 02:03:34 +01002128 return err;
2129}
2130
2131static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2132{
Johannes Berg4c476992010-10-04 21:36:35 +02002133 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002134 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002135 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002136 struct station_parameters params;
2137 u8 *mac_addr = NULL;
2138
2139 memset(&params, 0, sizeof(params));
2140
2141 if (!info->attrs[NL80211_ATTR_MAC])
2142 return -EINVAL;
2143
Johannes Berg5727ef12007-12-19 02:03:34 +01002144 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2145 return -EINVAL;
2146
2147 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2148 return -EINVAL;
2149
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002150 if (!info->attrs[NL80211_ATTR_STA_AID])
2151 return -EINVAL;
2152
Johannes Berg5727ef12007-12-19 02:03:34 +01002153 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2154 params.supported_rates =
2155 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2156 params.supported_rates_len =
2157 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2158 params.listen_interval =
2159 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002160
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002161 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2162 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2163 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002164
Jouni Malinen36aedc92008-08-25 11:58:58 +03002165 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2166 params.ht_capa =
2167 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002168
Johannes Bergeccb8e82009-05-11 21:57:56 +03002169 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002170 return -EINVAL;
2171
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002172 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002173 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02002174 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2175 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002176
Johannes Berg463d0182009-07-14 00:33:35 +02002177 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002178 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002179 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002180
2181 /* validate settings */
2182 err = 0;
2183
Johannes Berg79c97e92009-07-07 03:56:12 +02002184 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002185 err = -EOPNOTSUPP;
2186 goto out;
2187 }
2188
Johannes Berg79c97e92009-07-07 03:56:12 +02002189 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002190
2191 out:
2192 if (params.vlan)
2193 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002194 return err;
2195}
2196
2197static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2198{
Johannes Berg4c476992010-10-04 21:36:35 +02002199 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2200 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002201 u8 *mac_addr = NULL;
2202
2203 if (info->attrs[NL80211_ATTR_MAC])
2204 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2205
Johannes Berge80cf852009-05-11 14:43:13 +02002206 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002207 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002208 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002209 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2210 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002211
Johannes Berg4c476992010-10-04 21:36:35 +02002212 if (!rdev->ops->del_station)
2213 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002214
Johannes Berg4c476992010-10-04 21:36:35 +02002215 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002216}
2217
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002218static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2219 int flags, struct net_device *dev,
2220 u8 *dst, u8 *next_hop,
2221 struct mpath_info *pinfo)
2222{
2223 void *hdr;
2224 struct nlattr *pinfoattr;
2225
2226 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2227 if (!hdr)
2228 return -1;
2229
2230 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2231 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2232 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2233
Johannes Bergf5ea9122009-08-07 16:17:38 +02002234 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2235
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002236 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2237 if (!pinfoattr)
2238 goto nla_put_failure;
2239 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2240 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2241 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002242 if (pinfo->filled & MPATH_INFO_SN)
2243 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2244 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002245 if (pinfo->filled & MPATH_INFO_METRIC)
2246 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2247 pinfo->metric);
2248 if (pinfo->filled & MPATH_INFO_EXPTIME)
2249 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2250 pinfo->exptime);
2251 if (pinfo->filled & MPATH_INFO_FLAGS)
2252 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2253 pinfo->flags);
2254 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2255 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2256 pinfo->discovery_timeout);
2257 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2258 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2259 pinfo->discovery_retries);
2260
2261 nla_nest_end(msg, pinfoattr);
2262
2263 return genlmsg_end(msg, hdr);
2264
2265 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002266 genlmsg_cancel(msg, hdr);
2267 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002268}
2269
2270static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002271 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002272{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002273 struct mpath_info pinfo;
2274 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002275 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002276 u8 dst[ETH_ALEN];
2277 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002278 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002279 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002280
Johannes Berg67748892010-10-04 21:14:06 +02002281 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2282 if (err)
2283 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002284
2285 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002286 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002287 goto out_err;
2288 }
2289
Jouni Malineneec60b02009-03-20 21:21:19 +02002290 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2291 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002292 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002293 }
2294
Johannes Bergbba95fe2008-07-29 13:22:51 +02002295 while (1) {
2296 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2297 dst, next_hop, &pinfo);
2298 if (err == -ENOENT)
2299 break;
2300 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002301 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002302
2303 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2304 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2305 netdev, dst, next_hop,
2306 &pinfo) < 0)
2307 goto out;
2308
2309 path_idx++;
2310 }
2311
2312
2313 out:
2314 cb->args[1] = path_idx;
2315 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002316 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002317 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002318 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002319}
2320
2321static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2322{
Johannes Berg4c476992010-10-04 21:36:35 +02002323 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002324 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002325 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002326 struct mpath_info pinfo;
2327 struct sk_buff *msg;
2328 u8 *dst = NULL;
2329 u8 next_hop[ETH_ALEN];
2330
2331 memset(&pinfo, 0, sizeof(pinfo));
2332
2333 if (!info->attrs[NL80211_ATTR_MAC])
2334 return -EINVAL;
2335
2336 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2337
Johannes Berg4c476992010-10-04 21:36:35 +02002338 if (!rdev->ops->get_mpath)
2339 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002340
Johannes Berg4c476992010-10-04 21:36:35 +02002341 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2342 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002343
Johannes Berg79c97e92009-07-07 03:56:12 +02002344 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002345 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002346 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002347
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002348 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002349 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002350 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002351
2352 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002353 dev, dst, next_hop, &pinfo) < 0) {
2354 nlmsg_free(msg);
2355 return -ENOBUFS;
2356 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002357
Johannes Berg4c476992010-10-04 21:36:35 +02002358 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002359}
2360
2361static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2362{
Johannes Berg4c476992010-10-04 21:36:35 +02002363 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2364 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002365 u8 *dst = NULL;
2366 u8 *next_hop = NULL;
2367
2368 if (!info->attrs[NL80211_ATTR_MAC])
2369 return -EINVAL;
2370
2371 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2372 return -EINVAL;
2373
2374 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2375 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2376
Johannes Berg4c476992010-10-04 21:36:35 +02002377 if (!rdev->ops->change_mpath)
2378 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002379
Johannes Berg4c476992010-10-04 21:36:35 +02002380 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2381 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002382
Johannes Berg4c476992010-10-04 21:36:35 +02002383 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002384}
Johannes Berg4c476992010-10-04 21:36:35 +02002385
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002386static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2387{
Johannes Berg4c476992010-10-04 21:36:35 +02002388 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2389 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002390 u8 *dst = NULL;
2391 u8 *next_hop = NULL;
2392
2393 if (!info->attrs[NL80211_ATTR_MAC])
2394 return -EINVAL;
2395
2396 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2397 return -EINVAL;
2398
2399 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2400 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2401
Johannes Berg4c476992010-10-04 21:36:35 +02002402 if (!rdev->ops->add_mpath)
2403 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002404
Johannes Berg4c476992010-10-04 21:36:35 +02002405 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2406 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002407
Johannes Berg4c476992010-10-04 21:36:35 +02002408 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002409}
2410
2411static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2412{
Johannes Berg4c476992010-10-04 21:36:35 +02002413 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2414 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002415 u8 *dst = NULL;
2416
2417 if (info->attrs[NL80211_ATTR_MAC])
2418 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2419
Johannes Berg4c476992010-10-04 21:36:35 +02002420 if (!rdev->ops->del_mpath)
2421 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002422
Johannes Berg4c476992010-10-04 21:36:35 +02002423 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002424}
2425
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002426static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2427{
Johannes Berg4c476992010-10-04 21:36:35 +02002428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2429 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002430 struct bss_parameters params;
2431
2432 memset(&params, 0, sizeof(params));
2433 /* default to not changing parameters */
2434 params.use_cts_prot = -1;
2435 params.use_short_preamble = -1;
2436 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002437 params.ap_isolate = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002438
2439 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2440 params.use_cts_prot =
2441 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2442 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2443 params.use_short_preamble =
2444 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2445 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2446 params.use_short_slot_time =
2447 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002448 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2449 params.basic_rates =
2450 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2451 params.basic_rates_len =
2452 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2453 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002454 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2455 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002456
Johannes Berg4c476992010-10-04 21:36:35 +02002457 if (!rdev->ops->change_bss)
2458 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002459
Johannes Berg074ac8d2010-09-16 14:58:22 +02002460 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002461 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2462 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002463
Johannes Berg4c476992010-10-04 21:36:35 +02002464 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002465}
2466
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002467static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002468 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2469 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2470 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2471 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2472 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2473 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2474};
2475
2476static int parse_reg_rule(struct nlattr *tb[],
2477 struct ieee80211_reg_rule *reg_rule)
2478{
2479 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2480 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2481
2482 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2483 return -EINVAL;
2484 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2485 return -EINVAL;
2486 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2487 return -EINVAL;
2488 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2489 return -EINVAL;
2490 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2491 return -EINVAL;
2492
2493 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2494
2495 freq_range->start_freq_khz =
2496 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2497 freq_range->end_freq_khz =
2498 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2499 freq_range->max_bandwidth_khz =
2500 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2501
2502 power_rule->max_eirp =
2503 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2504
2505 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2506 power_rule->max_antenna_gain =
2507 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2508
2509 return 0;
2510}
2511
2512static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2513{
2514 int r;
2515 char *data = NULL;
2516
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002517 /*
2518 * You should only get this when cfg80211 hasn't yet initialized
2519 * completely when built-in to the kernel right between the time
2520 * window between nl80211_init() and regulatory_init(), if that is
2521 * even possible.
2522 */
2523 mutex_lock(&cfg80211_mutex);
2524 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002525 mutex_unlock(&cfg80211_mutex);
2526 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002527 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002528 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002529
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002530 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2531 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002532
2533 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2534
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002535 r = regulatory_hint_user(data);
2536
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002537 return r;
2538}
2539
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002540static int nl80211_get_mesh_params(struct sk_buff *skb,
2541 struct genl_info *info)
2542{
Johannes Berg4c476992010-10-04 21:36:35 +02002543 struct cfg80211_registered_device *rdev = info->user_ptr[0];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002544 struct mesh_config cur_params;
2545 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002546 struct net_device *dev = info->user_ptr[1];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002547 void *hdr;
2548 struct nlattr *pinfoattr;
2549 struct sk_buff *msg;
2550
Johannes Berg4c476992010-10-04 21:36:35 +02002551 if (!rdev->ops->get_mesh_params)
2552 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002553
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002554 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002555 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002556 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002557 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002558
2559 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002560 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002561 if (!msg)
2562 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002563 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2564 NL80211_CMD_GET_MESH_PARAMS);
2565 if (!hdr)
2566 goto nla_put_failure;
2567 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2568 if (!pinfoattr)
2569 goto nla_put_failure;
2570 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2571 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2572 cur_params.dot11MeshRetryTimeout);
2573 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2574 cur_params.dot11MeshConfirmTimeout);
2575 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2576 cur_params.dot11MeshHoldingTimeout);
2577 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2578 cur_params.dot11MeshMaxPeerLinks);
2579 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2580 cur_params.dot11MeshMaxRetries);
2581 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2582 cur_params.dot11MeshTTL);
2583 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2584 cur_params.auto_open_plinks);
2585 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2586 cur_params.dot11MeshHWMPmaxPREQretries);
2587 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2588 cur_params.path_refresh_time);
2589 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2590 cur_params.min_discovery_timeout);
2591 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2592 cur_params.dot11MeshHWMPactivePathTimeout);
2593 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2594 cur_params.dot11MeshHWMPpreqMinInterval);
2595 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2596 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002597 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2598 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002599 nla_nest_end(msg, pinfoattr);
2600 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002601 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002602
Johannes Berg3b858752009-03-12 09:55:09 +01002603 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002604 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002605 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002606 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002607}
2608
2609#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2610do {\
2611 if (table[attr_num]) {\
2612 cfg.param = nla_fn(table[attr_num]); \
2613 mask |= (1 << (attr_num - 1)); \
2614 } \
2615} while (0);\
2616
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002617static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002618 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2619 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2620 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2621 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2622 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2623 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2624 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2625
2626 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2627 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2628 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2629 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2630 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2631 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2632};
2633
2634static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2635{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002636 u32 mask;
Johannes Berg4c476992010-10-04 21:36:35 +02002637 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2638 struct net_device *dev = info->user_ptr[1];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002639 struct mesh_config cfg;
2640 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2641 struct nlattr *parent_attr;
2642
2643 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2644 if (!parent_attr)
2645 return -EINVAL;
2646 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2647 parent_attr, nl80211_meshconf_params_policy))
2648 return -EINVAL;
2649
Johannes Berg4c476992010-10-04 21:36:35 +02002650 if (!rdev->ops->set_mesh_params)
2651 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002652
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002653 /* This makes sure that there aren't more than 32 mesh config
2654 * parameters (otherwise our bitfield scheme would not work.) */
2655 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2656
2657 /* Fill in the params struct */
2658 mask = 0;
2659 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2660 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2661 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2662 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2663 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2664 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2665 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2666 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2667 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2668 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2669 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2670 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2671 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2672 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2673 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2674 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2675 nla_get_u8);
2676 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2677 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2678 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2679 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2680 nla_get_u16);
2681 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2682 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2683 nla_get_u32);
2684 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2685 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2686 nla_get_u16);
2687 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2688 dot11MeshHWMPnetDiameterTraversalTime,
2689 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2690 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002691 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2692 dot11MeshHWMPRootMode, mask,
2693 NL80211_MESHCONF_HWMP_ROOTMODE,
2694 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002695
2696 /* Apply changes */
Johannes Berg4c476992010-10-04 21:36:35 +02002697 return rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002698}
2699
2700#undef FILL_IN_MESH_PARAM_IF_SET
2701
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002702static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2703{
2704 struct sk_buff *msg;
2705 void *hdr = NULL;
2706 struct nlattr *nl_reg_rules;
2707 unsigned int i;
2708 int err = -EINVAL;
2709
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002710 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002711
2712 if (!cfg80211_regdomain)
2713 goto out;
2714
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002715 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002716 if (!msg) {
2717 err = -ENOBUFS;
2718 goto out;
2719 }
2720
2721 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2722 NL80211_CMD_GET_REG);
2723 if (!hdr)
2724 goto nla_put_failure;
2725
2726 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2727 cfg80211_regdomain->alpha2);
2728
2729 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2730 if (!nl_reg_rules)
2731 goto nla_put_failure;
2732
2733 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2734 struct nlattr *nl_reg_rule;
2735 const struct ieee80211_reg_rule *reg_rule;
2736 const struct ieee80211_freq_range *freq_range;
2737 const struct ieee80211_power_rule *power_rule;
2738
2739 reg_rule = &cfg80211_regdomain->reg_rules[i];
2740 freq_range = &reg_rule->freq_range;
2741 power_rule = &reg_rule->power_rule;
2742
2743 nl_reg_rule = nla_nest_start(msg, i);
2744 if (!nl_reg_rule)
2745 goto nla_put_failure;
2746
2747 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2748 reg_rule->flags);
2749 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2750 freq_range->start_freq_khz);
2751 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2752 freq_range->end_freq_khz);
2753 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2754 freq_range->max_bandwidth_khz);
2755 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2756 power_rule->max_antenna_gain);
2757 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2758 power_rule->max_eirp);
2759
2760 nla_nest_end(msg, nl_reg_rule);
2761 }
2762
2763 nla_nest_end(msg, nl_reg_rules);
2764
2765 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002766 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002767 goto out;
2768
2769nla_put_failure:
2770 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002771 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002772 err = -EMSGSIZE;
2773out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002774 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002775 return err;
2776}
2777
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002778static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2779{
2780 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2781 struct nlattr *nl_reg_rule;
2782 char *alpha2 = NULL;
2783 int rem_reg_rules = 0, r = 0;
2784 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2785 struct ieee80211_regdomain *rd = NULL;
2786
2787 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2788 return -EINVAL;
2789
2790 if (!info->attrs[NL80211_ATTR_REG_RULES])
2791 return -EINVAL;
2792
2793 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2794
2795 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2796 rem_reg_rules) {
2797 num_rules++;
2798 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002799 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002800 }
2801
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002802 mutex_lock(&cfg80211_mutex);
2803
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002804 if (!reg_is_valid_request(alpha2)) {
2805 r = -EINVAL;
2806 goto bad_reg;
2807 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002808
2809 size_of_regd = sizeof(struct ieee80211_regdomain) +
2810 (num_rules * sizeof(struct ieee80211_reg_rule));
2811
2812 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002813 if (!rd) {
2814 r = -ENOMEM;
2815 goto bad_reg;
2816 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002817
2818 rd->n_reg_rules = num_rules;
2819 rd->alpha2[0] = alpha2[0];
2820 rd->alpha2[1] = alpha2[1];
2821
2822 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2823 rem_reg_rules) {
2824 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2825 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2826 reg_rule_policy);
2827 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2828 if (r)
2829 goto bad_reg;
2830
2831 rule_idx++;
2832
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002833 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2834 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002835 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002836 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002837 }
2838
2839 BUG_ON(rule_idx != num_rules);
2840
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002841 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002842
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002843 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002844
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002845 return r;
2846
Johannes Bergd2372b32008-10-24 20:32:20 +02002847 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002848 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002849 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002850 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002851}
2852
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002853static int validate_scan_freqs(struct nlattr *freqs)
2854{
2855 struct nlattr *attr1, *attr2;
2856 int n_channels = 0, tmp1, tmp2;
2857
2858 nla_for_each_nested(attr1, freqs, tmp1) {
2859 n_channels++;
2860 /*
2861 * Some hardware has a limited channel list for
2862 * scanning, and it is pretty much nonsensical
2863 * to scan for a channel twice, so disallow that
2864 * and don't require drivers to check that the
2865 * channel list they get isn't longer than what
2866 * they can scan, as long as they can scan all
2867 * the channels they registered at once.
2868 */
2869 nla_for_each_nested(attr2, freqs, tmp2)
2870 if (attr1 != attr2 &&
2871 nla_get_u32(attr1) == nla_get_u32(attr2))
2872 return 0;
2873 }
2874
2875 return n_channels;
2876}
2877
Johannes Berg2a519312009-02-10 21:25:55 +01002878static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2879{
Johannes Berg4c476992010-10-04 21:36:35 +02002880 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2881 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01002882 struct cfg80211_scan_request *request;
2883 struct cfg80211_ssid *ssid;
2884 struct ieee80211_channel *channel;
2885 struct nlattr *attr;
2886 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002887 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002888 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002889 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002890
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002891 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2892 return -EINVAL;
2893
Johannes Berg79c97e92009-07-07 03:56:12 +02002894 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002895
Johannes Berg4c476992010-10-04 21:36:35 +02002896 if (!rdev->ops->scan)
2897 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01002898
Johannes Berg4c476992010-10-04 21:36:35 +02002899 if (rdev->scan_req)
2900 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01002901
2902 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002903 n_channels = validate_scan_freqs(
2904 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02002905 if (!n_channels)
2906 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002907 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002908 n_channels = 0;
2909
Johannes Berg2a519312009-02-10 21:25:55 +01002910 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2911 if (wiphy->bands[band])
2912 n_channels += wiphy->bands[band]->n_channels;
2913 }
2914
2915 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2916 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2917 n_ssids++;
2918
Johannes Berg4c476992010-10-04 21:36:35 +02002919 if (n_ssids > wiphy->max_scan_ssids)
2920 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002921
Jouni Malinen70692ad2009-02-16 19:39:13 +02002922 if (info->attrs[NL80211_ATTR_IE])
2923 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2924 else
2925 ie_len = 0;
2926
Johannes Berg4c476992010-10-04 21:36:35 +02002927 if (ie_len > wiphy->max_scan_ie_len)
2928 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02002929
Johannes Berg2a519312009-02-10 21:25:55 +01002930 request = kzalloc(sizeof(*request)
2931 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002932 + sizeof(channel) * n_channels
2933 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002934 if (!request)
2935 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01002936
Johannes Berg2a519312009-02-10 21:25:55 +01002937 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02002938 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01002939 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002940 if (ie_len) {
2941 if (request->ssids)
2942 request->ie = (void *)(request->ssids + n_ssids);
2943 else
2944 request->ie = (void *)(request->channels + n_channels);
2945 }
Johannes Berg2a519312009-02-10 21:25:55 +01002946
Johannes Berg584991d2009-11-02 13:32:03 +01002947 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01002948 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2949 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01002950 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01002951 struct ieee80211_channel *chan;
2952
2953 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2954
2955 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002956 err = -EINVAL;
2957 goto out_free;
2958 }
Johannes Berg584991d2009-11-02 13:32:03 +01002959
2960 /* ignore disabled channels */
2961 if (chan->flags & IEEE80211_CHAN_DISABLED)
2962 continue;
2963
2964 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01002965 i++;
2966 }
2967 } else {
2968 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01002969 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2970 int j;
2971 if (!wiphy->bands[band])
2972 continue;
2973 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01002974 struct ieee80211_channel *chan;
2975
2976 chan = &wiphy->bands[band]->channels[j];
2977
2978 if (chan->flags & IEEE80211_CHAN_DISABLED)
2979 continue;
2980
2981 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01002982 i++;
2983 }
2984 }
2985 }
2986
Johannes Berg584991d2009-11-02 13:32:03 +01002987 if (!i) {
2988 err = -EINVAL;
2989 goto out_free;
2990 }
2991
2992 request->n_channels = i;
2993
Johannes Berg2a519312009-02-10 21:25:55 +01002994 i = 0;
2995 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2996 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2997 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2998 err = -EINVAL;
2999 goto out_free;
3000 }
3001 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3002 request->ssids[i].ssid_len = nla_len(attr);
3003 i++;
3004 }
3005 }
3006
Jouni Malinen70692ad2009-02-16 19:39:13 +02003007 if (info->attrs[NL80211_ATTR_IE]) {
3008 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003009 memcpy((void *)request->ie,
3010 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003011 request->ie_len);
3012 }
3013
Johannes Berg463d0182009-07-14 00:33:35 +02003014 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003015 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003016
Johannes Berg79c97e92009-07-07 03:56:12 +02003017 rdev->scan_req = request;
3018 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003019
Johannes Berg463d0182009-07-14 00:33:35 +02003020 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003021 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003022 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003023 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003024 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003025 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003026 kfree(request);
3027 }
Johannes Berg3b858752009-03-12 09:55:09 +01003028
Johannes Berg2a519312009-02-10 21:25:55 +01003029 return err;
3030}
3031
3032static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3033 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003034 struct wireless_dev *wdev,
3035 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003036{
Johannes Berg48ab9052009-07-10 18:42:31 +02003037 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003038 void *hdr;
3039 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003040 int i;
3041
3042 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003043
3044 hdr = nl80211hdr_put(msg, pid, seq, flags,
3045 NL80211_CMD_NEW_SCAN_RESULTS);
3046 if (!hdr)
3047 return -1;
3048
Johannes Bergf5ea9122009-08-07 16:17:38 +02003049 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003050 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003051
3052 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3053 if (!bss)
3054 goto nla_put_failure;
3055 if (!is_zero_ether_addr(res->bssid))
3056 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3057 if (res->information_elements && res->len_information_elements)
3058 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3059 res->len_information_elements,
3060 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003061 if (res->beacon_ies && res->len_beacon_ies &&
3062 res->beacon_ies != res->information_elements)
3063 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3064 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003065 if (res->tsf)
3066 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3067 if (res->beacon_interval)
3068 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3069 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3070 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003071 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3072 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003073
Johannes Berg77965c92009-02-18 18:45:06 +01003074 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003075 case CFG80211_SIGNAL_TYPE_MBM:
3076 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3077 break;
3078 case CFG80211_SIGNAL_TYPE_UNSPEC:
3079 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3080 break;
3081 default:
3082 break;
3083 }
3084
Johannes Berg48ab9052009-07-10 18:42:31 +02003085 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003086 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003087 case NL80211_IFTYPE_STATION:
3088 if (intbss == wdev->current_bss)
3089 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3090 NL80211_BSS_STATUS_ASSOCIATED);
3091 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3092 if (intbss != wdev->auth_bsses[i])
3093 continue;
3094 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3095 NL80211_BSS_STATUS_AUTHENTICATED);
3096 break;
3097 }
3098 break;
3099 case NL80211_IFTYPE_ADHOC:
3100 if (intbss == wdev->current_bss)
3101 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3102 NL80211_BSS_STATUS_IBSS_JOINED);
3103 break;
3104 default:
3105 break;
3106 }
3107
Johannes Berg2a519312009-02-10 21:25:55 +01003108 nla_nest_end(msg, bss);
3109
3110 return genlmsg_end(msg, hdr);
3111
3112 nla_put_failure:
3113 genlmsg_cancel(msg, hdr);
3114 return -EMSGSIZE;
3115}
3116
3117static int nl80211_dump_scan(struct sk_buff *skb,
3118 struct netlink_callback *cb)
3119{
Johannes Berg48ab9052009-07-10 18:42:31 +02003120 struct cfg80211_registered_device *rdev;
3121 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003122 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003123 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003124 int start = cb->args[1], idx = 0;
3125 int err;
3126
Johannes Berg67748892010-10-04 21:14:06 +02003127 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3128 if (err)
3129 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003130
Johannes Berg48ab9052009-07-10 18:42:31 +02003131 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003132
Johannes Berg48ab9052009-07-10 18:42:31 +02003133 wdev_lock(wdev);
3134 spin_lock_bh(&rdev->bss_lock);
3135 cfg80211_bss_expire(rdev);
3136
3137 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003138 if (++idx <= start)
3139 continue;
3140 if (nl80211_send_bss(skb,
3141 NETLINK_CB(cb->skb).pid,
3142 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003143 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003144 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003145 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003146 }
3147 }
3148
Johannes Berg48ab9052009-07-10 18:42:31 +02003149 spin_unlock_bh(&rdev->bss_lock);
3150 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003151
3152 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003153 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003154
Johannes Berg67748892010-10-04 21:14:06 +02003155 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003156}
3157
Holger Schurig61fa7132009-11-11 12:25:40 +01003158static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3159 int flags, struct net_device *dev,
3160 struct survey_info *survey)
3161{
3162 void *hdr;
3163 struct nlattr *infoattr;
3164
3165 /* Survey without a channel doesn't make sense */
3166 if (!survey->channel)
3167 return -EINVAL;
3168
3169 hdr = nl80211hdr_put(msg, pid, seq, flags,
3170 NL80211_CMD_NEW_SURVEY_RESULTS);
3171 if (!hdr)
3172 return -ENOMEM;
3173
3174 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3175
3176 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3177 if (!infoattr)
3178 goto nla_put_failure;
3179
3180 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3181 survey->channel->center_freq);
3182 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3183 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3184 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003185 if (survey->filled & SURVEY_INFO_IN_USE)
3186 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02003187 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3188 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3189 survey->channel_time);
3190 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3191 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3192 survey->channel_time_busy);
3193 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3194 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3195 survey->channel_time_ext_busy);
3196 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3197 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3198 survey->channel_time_rx);
3199 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3200 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3201 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003202
3203 nla_nest_end(msg, infoattr);
3204
3205 return genlmsg_end(msg, hdr);
3206
3207 nla_put_failure:
3208 genlmsg_cancel(msg, hdr);
3209 return -EMSGSIZE;
3210}
3211
3212static int nl80211_dump_survey(struct sk_buff *skb,
3213 struct netlink_callback *cb)
3214{
3215 struct survey_info survey;
3216 struct cfg80211_registered_device *dev;
3217 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003218 int survey_idx = cb->args[1];
3219 int res;
3220
Johannes Berg67748892010-10-04 21:14:06 +02003221 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3222 if (res)
3223 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003224
3225 if (!dev->ops->dump_survey) {
3226 res = -EOPNOTSUPP;
3227 goto out_err;
3228 }
3229
3230 while (1) {
3231 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3232 &survey);
3233 if (res == -ENOENT)
3234 break;
3235 if (res)
3236 goto out_err;
3237
3238 if (nl80211_send_survey(skb,
3239 NETLINK_CB(cb->skb).pid,
3240 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3241 netdev,
3242 &survey) < 0)
3243 goto out;
3244 survey_idx++;
3245 }
3246
3247 out:
3248 cb->args[1] = survey_idx;
3249 res = skb->len;
3250 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003251 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003252 return res;
3253}
3254
Jouni Malinen255e7372009-03-20 21:21:17 +02003255static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3256{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003257 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003258}
3259
Samuel Ortizb23aa672009-07-01 21:26:54 +02003260static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3261{
3262 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3263 NL80211_WPA_VERSION_2));
3264}
3265
3266static bool nl80211_valid_akm_suite(u32 akm)
3267{
3268 return akm == WLAN_AKM_SUITE_8021X ||
3269 akm == WLAN_AKM_SUITE_PSK;
3270}
3271
3272static bool nl80211_valid_cipher_suite(u32 cipher)
3273{
3274 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3275 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3276 cipher == WLAN_CIPHER_SUITE_TKIP ||
3277 cipher == WLAN_CIPHER_SUITE_CCMP ||
3278 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3279}
3280
3281
Jouni Malinen636a5d32009-03-19 13:39:22 +02003282static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3283{
Johannes Berg4c476992010-10-04 21:36:35 +02003284 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3285 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003286 struct ieee80211_channel *chan;
3287 const u8 *bssid, *ssid, *ie = NULL;
3288 int err, ssid_len, ie_len = 0;
3289 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003290 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003291 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003292
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003293 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3294 return -EINVAL;
3295
3296 if (!info->attrs[NL80211_ATTR_MAC])
3297 return -EINVAL;
3298
Jouni Malinen17780922009-03-27 20:52:47 +02003299 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3300 return -EINVAL;
3301
Johannes Berg19957bb2009-07-02 17:20:43 +02003302 if (!info->attrs[NL80211_ATTR_SSID])
3303 return -EINVAL;
3304
3305 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3306 return -EINVAL;
3307
Johannes Bergfffd0932009-07-08 14:22:54 +02003308 err = nl80211_parse_key(info, &key);
3309 if (err)
3310 return err;
3311
3312 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003313 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3314 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003315 if (!key.p.key || !key.p.key_len)
3316 return -EINVAL;
3317 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3318 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3319 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3320 key.p.key_len != WLAN_KEY_LEN_WEP104))
3321 return -EINVAL;
3322 if (key.idx > 4)
3323 return -EINVAL;
3324 } else {
3325 key.p.key_len = 0;
3326 key.p.key = NULL;
3327 }
3328
Johannes Bergafea0b72010-08-10 09:46:42 +02003329 if (key.idx >= 0) {
3330 int i;
3331 bool ok = false;
3332 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3333 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3334 ok = true;
3335 break;
3336 }
3337 }
Johannes Berg4c476992010-10-04 21:36:35 +02003338 if (!ok)
3339 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003340 }
3341
Johannes Berg4c476992010-10-04 21:36:35 +02003342 if (!rdev->ops->auth)
3343 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003344
Johannes Berg074ac8d2010-09-16 14:58:22 +02003345 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003346 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3347 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003348
Johannes Berg19957bb2009-07-02 17:20:43 +02003349 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003350 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003351 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003352 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3353 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003354
Johannes Berg19957bb2009-07-02 17:20:43 +02003355 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3356 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3357
3358 if (info->attrs[NL80211_ATTR_IE]) {
3359 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3360 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3361 }
3362
3363 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003364 if (!nl80211_valid_auth_type(auth_type))
3365 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003366
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003367 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3368
Johannes Berg4c476992010-10-04 21:36:35 +02003369 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3370 ssid, ssid_len, ie, ie_len,
3371 key.p.key, key.p.key_len, key.idx,
3372 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003373}
3374
Johannes Bergc0692b82010-08-27 14:26:53 +03003375static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3376 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003377 struct cfg80211_crypto_settings *settings,
3378 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003379{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003380 memset(settings, 0, sizeof(*settings));
3381
Samuel Ortizb23aa672009-07-01 21:26:54 +02003382 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3383
Johannes Bergc0692b82010-08-27 14:26:53 +03003384 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3385 u16 proto;
3386 proto = nla_get_u16(
3387 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3388 settings->control_port_ethertype = cpu_to_be16(proto);
3389 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3390 proto != ETH_P_PAE)
3391 return -EINVAL;
3392 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3393 settings->control_port_no_encrypt = true;
3394 } else
3395 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3396
Samuel Ortizb23aa672009-07-01 21:26:54 +02003397 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3398 void *data;
3399 int len, i;
3400
3401 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3402 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3403 settings->n_ciphers_pairwise = len / sizeof(u32);
3404
3405 if (len % sizeof(u32))
3406 return -EINVAL;
3407
Johannes Berg3dc27d22009-07-02 21:36:37 +02003408 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003409 return -EINVAL;
3410
3411 memcpy(settings->ciphers_pairwise, data, len);
3412
3413 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3414 if (!nl80211_valid_cipher_suite(
3415 settings->ciphers_pairwise[i]))
3416 return -EINVAL;
3417 }
3418
3419 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3420 settings->cipher_group =
3421 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3422 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3423 return -EINVAL;
3424 }
3425
3426 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3427 settings->wpa_versions =
3428 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3429 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3430 return -EINVAL;
3431 }
3432
3433 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3434 void *data;
3435 int len, i;
3436
3437 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3438 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3439 settings->n_akm_suites = len / sizeof(u32);
3440
3441 if (len % sizeof(u32))
3442 return -EINVAL;
3443
3444 memcpy(settings->akm_suites, data, len);
3445
3446 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3447 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3448 return -EINVAL;
3449 }
3450
3451 return 0;
3452}
3453
Jouni Malinen636a5d32009-03-19 13:39:22 +02003454static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3455{
Johannes Berg4c476992010-10-04 21:36:35 +02003456 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3457 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003458 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003459 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003460 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003461 int err, ssid_len, ie_len = 0;
3462 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003463
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003464 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3465 return -EINVAL;
3466
3467 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003468 !info->attrs[NL80211_ATTR_SSID] ||
3469 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003470 return -EINVAL;
3471
Johannes Berg4c476992010-10-04 21:36:35 +02003472 if (!rdev->ops->assoc)
3473 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003474
Johannes Berg074ac8d2010-09-16 14:58:22 +02003475 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003476 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3477 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003478
Johannes Berg19957bb2009-07-02 17:20:43 +02003479 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003480
Johannes Berg19957bb2009-07-02 17:20:43 +02003481 chan = ieee80211_get_channel(&rdev->wiphy,
3482 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003483 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3484 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003485
Johannes Berg19957bb2009-07-02 17:20:43 +02003486 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3487 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003488
3489 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003490 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3491 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003492 }
3493
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003494 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003495 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003496 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003497 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003498 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02003499 else if (mfp != NL80211_MFP_NO)
3500 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003501 }
3502
Johannes Berg3e5d7642009-07-07 14:37:26 +02003503 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3504 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3505
Johannes Bergc0692b82010-08-27 14:26:53 +03003506 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003507 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003508 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3509 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003510 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003511
Jouni Malinen636a5d32009-03-19 13:39:22 +02003512 return err;
3513}
3514
3515static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3516{
Johannes Berg4c476992010-10-04 21:36:35 +02003517 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3518 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003519 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003520 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003521 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003522 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003523
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003524 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3525 return -EINVAL;
3526
3527 if (!info->attrs[NL80211_ATTR_MAC])
3528 return -EINVAL;
3529
3530 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3531 return -EINVAL;
3532
Johannes Berg4c476992010-10-04 21:36:35 +02003533 if (!rdev->ops->deauth)
3534 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003535
Johannes Berg074ac8d2010-09-16 14:58:22 +02003536 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003537 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3538 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003539
Johannes Berg19957bb2009-07-02 17:20:43 +02003540 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003541
Johannes Berg19957bb2009-07-02 17:20:43 +02003542 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3543 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003544 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003545 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003546 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003547
3548 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003549 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3550 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003551 }
3552
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003553 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3554
Johannes Berg4c476992010-10-04 21:36:35 +02003555 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3556 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003557}
3558
3559static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3560{
Johannes Berg4c476992010-10-04 21:36:35 +02003561 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3562 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003563 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003564 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003565 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003566 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003567
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003568 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3569 return -EINVAL;
3570
3571 if (!info->attrs[NL80211_ATTR_MAC])
3572 return -EINVAL;
3573
3574 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3575 return -EINVAL;
3576
Johannes Berg4c476992010-10-04 21:36:35 +02003577 if (!rdev->ops->disassoc)
3578 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003579
Johannes Berg074ac8d2010-09-16 14:58:22 +02003580 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003581 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3582 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003583
Johannes Berg19957bb2009-07-02 17:20:43 +02003584 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003585
Johannes Berg19957bb2009-07-02 17:20:43 +02003586 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3587 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003588 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003589 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003590 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003591
3592 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003593 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3594 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003595 }
3596
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003597 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3598
Johannes Berg4c476992010-10-04 21:36:35 +02003599 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3600 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003601}
3602
Johannes Berg04a773a2009-04-19 21:24:32 +02003603static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3604{
Johannes Berg4c476992010-10-04 21:36:35 +02003605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3606 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003607 struct cfg80211_ibss_params ibss;
3608 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003609 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003610 int err;
3611
Johannes Berg8e30bc52009-04-22 17:45:38 +02003612 memset(&ibss, 0, sizeof(ibss));
3613
Johannes Berg04a773a2009-04-19 21:24:32 +02003614 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3615 return -EINVAL;
3616
3617 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3618 !info->attrs[NL80211_ATTR_SSID] ||
3619 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3620 return -EINVAL;
3621
Johannes Berg8e30bc52009-04-22 17:45:38 +02003622 ibss.beacon_interval = 100;
3623
3624 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3625 ibss.beacon_interval =
3626 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3627 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3628 return -EINVAL;
3629 }
3630
Johannes Berg4c476992010-10-04 21:36:35 +02003631 if (!rdev->ops->join_ibss)
3632 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003633
Johannes Berg4c476992010-10-04 21:36:35 +02003634 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3635 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003636
Johannes Berg79c97e92009-07-07 03:56:12 +02003637 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003638
3639 if (info->attrs[NL80211_ATTR_MAC])
3640 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3641 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3642 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3643
3644 if (info->attrs[NL80211_ATTR_IE]) {
3645 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3646 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3647 }
3648
3649 ibss.channel = ieee80211_get_channel(wiphy,
3650 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3651 if (!ibss.channel ||
3652 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02003653 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3654 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003655
3656 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003657 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003658
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003659 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3660 u8 *rates =
3661 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3662 int n_rates =
3663 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3664 struct ieee80211_supported_band *sband =
3665 wiphy->bands[ibss.channel->band];
3666 int i, j;
3667
Johannes Berg4c476992010-10-04 21:36:35 +02003668 if (n_rates == 0)
3669 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003670
3671 for (i = 0; i < n_rates; i++) {
3672 int rate = (rates[i] & 0x7f) * 5;
3673 bool found = false;
3674
3675 for (j = 0; j < sband->n_bitrates; j++) {
3676 if (sband->bitrates[j].bitrate == rate) {
3677 found = true;
3678 ibss.basic_rates |= BIT(j);
3679 break;
3680 }
3681 }
Johannes Berg4c476992010-10-04 21:36:35 +02003682 if (!found)
3683 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003684 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003685 }
Felix Fietkau885a46d2010-11-11 15:07:22 +01003686 if (info->attrs[NL80211_ATTR_MCAST_RATE])
3687 ibss.mcast_rate =
3688 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003689
Johannes Berg4c476992010-10-04 21:36:35 +02003690 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3691 connkeys = nl80211_parse_connkeys(rdev,
3692 info->attrs[NL80211_ATTR_KEYS]);
3693 if (IS_ERR(connkeys))
3694 return PTR_ERR(connkeys);
3695 }
Johannes Berg04a773a2009-04-19 21:24:32 +02003696
Johannes Berg4c476992010-10-04 21:36:35 +02003697 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003698 if (err)
3699 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003700 return err;
3701}
3702
3703static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3704{
Johannes Berg4c476992010-10-04 21:36:35 +02003705 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3706 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003707
Johannes Berg4c476992010-10-04 21:36:35 +02003708 if (!rdev->ops->leave_ibss)
3709 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003710
Johannes Berg4c476992010-10-04 21:36:35 +02003711 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3712 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003713
Johannes Berg4c476992010-10-04 21:36:35 +02003714 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003715}
3716
Johannes Bergaff89a92009-07-01 21:26:51 +02003717#ifdef CONFIG_NL80211_TESTMODE
3718static struct genl_multicast_group nl80211_testmode_mcgrp = {
3719 .name = "testmode",
3720};
3721
3722static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3723{
Johannes Berg4c476992010-10-04 21:36:35 +02003724 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02003725 int err;
3726
3727 if (!info->attrs[NL80211_ATTR_TESTDATA])
3728 return -EINVAL;
3729
Johannes Bergaff89a92009-07-01 21:26:51 +02003730 err = -EOPNOTSUPP;
3731 if (rdev->ops->testmode_cmd) {
3732 rdev->testmode_info = info;
3733 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3734 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3735 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3736 rdev->testmode_info = NULL;
3737 }
3738
Johannes Bergaff89a92009-07-01 21:26:51 +02003739 return err;
3740}
3741
3742static struct sk_buff *
3743__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3744 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3745{
3746 struct sk_buff *skb;
3747 void *hdr;
3748 struct nlattr *data;
3749
3750 skb = nlmsg_new(approxlen + 100, gfp);
3751 if (!skb)
3752 return NULL;
3753
3754 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3755 if (!hdr) {
3756 kfree_skb(skb);
3757 return NULL;
3758 }
3759
3760 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3761 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3762
3763 ((void **)skb->cb)[0] = rdev;
3764 ((void **)skb->cb)[1] = hdr;
3765 ((void **)skb->cb)[2] = data;
3766
3767 return skb;
3768
3769 nla_put_failure:
3770 kfree_skb(skb);
3771 return NULL;
3772}
3773
3774struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3775 int approxlen)
3776{
3777 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3778
3779 if (WARN_ON(!rdev->testmode_info))
3780 return NULL;
3781
3782 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3783 rdev->testmode_info->snd_pid,
3784 rdev->testmode_info->snd_seq,
3785 GFP_KERNEL);
3786}
3787EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3788
3789int cfg80211_testmode_reply(struct sk_buff *skb)
3790{
3791 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3792 void *hdr = ((void **)skb->cb)[1];
3793 struct nlattr *data = ((void **)skb->cb)[2];
3794
3795 if (WARN_ON(!rdev->testmode_info)) {
3796 kfree_skb(skb);
3797 return -EINVAL;
3798 }
3799
3800 nla_nest_end(skb, data);
3801 genlmsg_end(skb, hdr);
3802 return genlmsg_reply(skb, rdev->testmode_info);
3803}
3804EXPORT_SYMBOL(cfg80211_testmode_reply);
3805
3806struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3807 int approxlen, gfp_t gfp)
3808{
3809 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3810
3811 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3812}
3813EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3814
3815void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3816{
3817 void *hdr = ((void **)skb->cb)[1];
3818 struct nlattr *data = ((void **)skb->cb)[2];
3819
3820 nla_nest_end(skb, data);
3821 genlmsg_end(skb, hdr);
3822 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3823}
3824EXPORT_SYMBOL(cfg80211_testmode_event);
3825#endif
3826
Samuel Ortizb23aa672009-07-01 21:26:54 +02003827static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3828{
Johannes Berg4c476992010-10-04 21:36:35 +02003829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3830 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003831 struct cfg80211_connect_params connect;
3832 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003833 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003834 int err;
3835
3836 memset(&connect, 0, sizeof(connect));
3837
3838 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3839 return -EINVAL;
3840
3841 if (!info->attrs[NL80211_ATTR_SSID] ||
3842 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3843 return -EINVAL;
3844
3845 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3846 connect.auth_type =
3847 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3848 if (!nl80211_valid_auth_type(connect.auth_type))
3849 return -EINVAL;
3850 } else
3851 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3852
3853 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3854
Johannes Bergc0692b82010-08-27 14:26:53 +03003855 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003856 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003857 if (err)
3858 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003859
Johannes Berg074ac8d2010-09-16 14:58:22 +02003860 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003861 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3862 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003863
Johannes Berg79c97e92009-07-07 03:56:12 +02003864 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003865
Samuel Ortizb23aa672009-07-01 21:26:54 +02003866 if (info->attrs[NL80211_ATTR_MAC])
3867 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3868 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3869 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3870
3871 if (info->attrs[NL80211_ATTR_IE]) {
3872 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3873 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3874 }
3875
3876 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3877 connect.channel =
3878 ieee80211_get_channel(wiphy,
3879 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3880 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02003881 connect.channel->flags & IEEE80211_CHAN_DISABLED)
3882 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003883 }
3884
Johannes Bergfffd0932009-07-08 14:22:54 +02003885 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3886 connkeys = nl80211_parse_connkeys(rdev,
3887 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02003888 if (IS_ERR(connkeys))
3889 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003890 }
3891
3892 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003893 if (err)
3894 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003895 return err;
3896}
3897
3898static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3899{
Johannes Berg4c476992010-10-04 21:36:35 +02003900 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3901 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003902 u16 reason;
3903
3904 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3905 reason = WLAN_REASON_DEAUTH_LEAVING;
3906 else
3907 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3908
3909 if (reason == 0)
3910 return -EINVAL;
3911
Johannes Berg074ac8d2010-09-16 14:58:22 +02003912 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003913 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3914 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003915
Johannes Berg4c476992010-10-04 21:36:35 +02003916 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003917}
3918
Johannes Berg463d0182009-07-14 00:33:35 +02003919static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
3920{
Johannes Berg4c476992010-10-04 21:36:35 +02003921 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02003922 struct net *net;
3923 int err;
3924 u32 pid;
3925
3926 if (!info->attrs[NL80211_ATTR_PID])
3927 return -EINVAL;
3928
3929 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
3930
Johannes Berg463d0182009-07-14 00:33:35 +02003931 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02003932 if (IS_ERR(net))
3933 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02003934
3935 err = 0;
3936
3937 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02003938 if (!net_eq(wiphy_net(&rdev->wiphy), net))
3939 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02003940
Johannes Berg463d0182009-07-14 00:33:35 +02003941 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02003942 return err;
3943}
3944
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003945static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
3946{
Johannes Berg4c476992010-10-04 21:36:35 +02003947 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003948 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
3949 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003950 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003951 struct cfg80211_pmksa pmksa;
3952
3953 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
3954
3955 if (!info->attrs[NL80211_ATTR_MAC])
3956 return -EINVAL;
3957
3958 if (!info->attrs[NL80211_ATTR_PMKID])
3959 return -EINVAL;
3960
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003961 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
3962 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3963
Johannes Berg074ac8d2010-09-16 14:58:22 +02003964 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003965 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3966 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003967
3968 switch (info->genlhdr->cmd) {
3969 case NL80211_CMD_SET_PMKSA:
3970 rdev_ops = rdev->ops->set_pmksa;
3971 break;
3972 case NL80211_CMD_DEL_PMKSA:
3973 rdev_ops = rdev->ops->del_pmksa;
3974 break;
3975 default:
3976 WARN_ON(1);
3977 break;
3978 }
3979
Johannes Berg4c476992010-10-04 21:36:35 +02003980 if (!rdev_ops)
3981 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003982
Johannes Berg4c476992010-10-04 21:36:35 +02003983 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003984}
3985
3986static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
3987{
Johannes Berg4c476992010-10-04 21:36:35 +02003988 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3989 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003990
Johannes Berg074ac8d2010-09-16 14:58:22 +02003991 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003992 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3993 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003994
Johannes Berg4c476992010-10-04 21:36:35 +02003995 if (!rdev->ops->flush_pmksa)
3996 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003997
Johannes Berg4c476992010-10-04 21:36:35 +02003998 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01003999}
4000
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004001static int nl80211_remain_on_channel(struct sk_buff *skb,
4002 struct genl_info *info)
4003{
Johannes Berg4c476992010-10-04 21:36:35 +02004004 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4005 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004006 struct ieee80211_channel *chan;
4007 struct sk_buff *msg;
4008 void *hdr;
4009 u64 cookie;
4010 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4011 u32 freq, duration;
4012 int err;
4013
4014 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4015 !info->attrs[NL80211_ATTR_DURATION])
4016 return -EINVAL;
4017
4018 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4019
4020 /*
4021 * We should be on that channel for at least one jiffie,
4022 * and more than 5 seconds seems excessive.
4023 */
4024 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4025 return -EINVAL;
4026
Johannes Berg4c476992010-10-04 21:36:35 +02004027 if (!rdev->ops->remain_on_channel)
4028 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004029
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004030 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4031 channel_type = nla_get_u32(
4032 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4033 if (channel_type != NL80211_CHAN_NO_HT &&
4034 channel_type != NL80211_CHAN_HT20 &&
4035 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004036 channel_type != NL80211_CHAN_HT40MINUS)
4037 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004038 }
4039
4040 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4041 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004042 if (chan == NULL)
4043 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004044
4045 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004046 if (!msg)
4047 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004048
4049 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4050 NL80211_CMD_REMAIN_ON_CHANNEL);
4051
4052 if (IS_ERR(hdr)) {
4053 err = PTR_ERR(hdr);
4054 goto free_msg;
4055 }
4056
4057 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4058 channel_type, duration, &cookie);
4059
4060 if (err)
4061 goto free_msg;
4062
4063 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4064
4065 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004066
4067 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004068
4069 nla_put_failure:
4070 err = -ENOBUFS;
4071 free_msg:
4072 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004073 return err;
4074}
4075
4076static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4077 struct genl_info *info)
4078{
Johannes Berg4c476992010-10-04 21:36:35 +02004079 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4080 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004081 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004082
4083 if (!info->attrs[NL80211_ATTR_COOKIE])
4084 return -EINVAL;
4085
Johannes Berg4c476992010-10-04 21:36:35 +02004086 if (!rdev->ops->cancel_remain_on_channel)
4087 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004088
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004089 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4090
Johannes Berg4c476992010-10-04 21:36:35 +02004091 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004092}
4093
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004094static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4095 u8 *rates, u8 rates_len)
4096{
4097 u8 i;
4098 u32 mask = 0;
4099
4100 for (i = 0; i < rates_len; i++) {
4101 int rate = (rates[i] & 0x7f) * 5;
4102 int ridx;
4103 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4104 struct ieee80211_rate *srate =
4105 &sband->bitrates[ridx];
4106 if (rate == srate->bitrate) {
4107 mask |= 1 << ridx;
4108 break;
4109 }
4110 }
4111 if (ridx == sband->n_bitrates)
4112 return 0; /* rate not found */
4113 }
4114
4115 return mask;
4116}
4117
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004118static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004119 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4120 .len = NL80211_MAX_SUPP_RATES },
4121};
4122
4123static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4124 struct genl_info *info)
4125{
4126 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004127 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004128 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004129 int rem, i;
4130 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004131 struct nlattr *tx_rates;
4132 struct ieee80211_supported_band *sband;
4133
4134 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4135 return -EINVAL;
4136
Johannes Berg4c476992010-10-04 21:36:35 +02004137 if (!rdev->ops->set_bitrate_mask)
4138 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004139
4140 memset(&mask, 0, sizeof(mask));
4141 /* Default to all rates enabled */
4142 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4143 sband = rdev->wiphy.bands[i];
4144 mask.control[i].legacy =
4145 sband ? (1 << sband->n_bitrates) - 1 : 0;
4146 }
4147
4148 /*
4149 * The nested attribute uses enum nl80211_band as the index. This maps
4150 * directly to the enum ieee80211_band values used in cfg80211.
4151 */
4152 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4153 {
4154 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004155 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4156 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004157 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004158 if (sband == NULL)
4159 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004160 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4161 nla_len(tx_rates), nl80211_txattr_policy);
4162 if (tb[NL80211_TXRATE_LEGACY]) {
4163 mask.control[band].legacy = rateset_to_mask(
4164 sband,
4165 nla_data(tb[NL80211_TXRATE_LEGACY]),
4166 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004167 if (mask.control[band].legacy == 0)
4168 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004169 }
4170 }
4171
Johannes Berg4c476992010-10-04 21:36:35 +02004172 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004173}
4174
Johannes Berg2e161f72010-08-12 15:38:38 +02004175static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004176{
Johannes Berg4c476992010-10-04 21:36:35 +02004177 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4178 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004179 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004180
4181 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4182 return -EINVAL;
4183
Johannes Berg2e161f72010-08-12 15:38:38 +02004184 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4185 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004186
Johannes Berg9d38d852010-06-09 17:20:33 +02004187 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004188 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004189 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4190 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4191 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004192 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4193 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004194
4195 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004196 if (!rdev->ops->mgmt_tx)
4197 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004198
Johannes Berg4c476992010-10-04 21:36:35 +02004199 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004200 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004201 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4202 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004203}
4204
Johannes Berg2e161f72010-08-12 15:38:38 +02004205static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004206{
Johannes Berg4c476992010-10-04 21:36:35 +02004207 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4208 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004209 struct ieee80211_channel *chan;
4210 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004211 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004212 u32 freq;
4213 int err;
4214 void *hdr;
4215 u64 cookie;
4216 struct sk_buff *msg;
4217
4218 if (!info->attrs[NL80211_ATTR_FRAME] ||
4219 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4220 return -EINVAL;
4221
Johannes Berg4c476992010-10-04 21:36:35 +02004222 if (!rdev->ops->mgmt_tx)
4223 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004224
Johannes Berg9d38d852010-06-09 17:20:33 +02004225 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004226 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004227 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4228 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4229 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004230 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4231 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004232
Jouni Malinen026331c2010-02-15 12:53:10 +02004233 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4234 channel_type = nla_get_u32(
4235 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4236 if (channel_type != NL80211_CHAN_NO_HT &&
4237 channel_type != NL80211_CHAN_HT20 &&
4238 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004239 channel_type != NL80211_CHAN_HT40MINUS)
4240 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004241 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004242 }
4243
4244 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4245 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004246 if (chan == NULL)
4247 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004248
4249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004250 if (!msg)
4251 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004252
4253 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004254 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004255
4256 if (IS_ERR(hdr)) {
4257 err = PTR_ERR(hdr);
4258 goto free_msg;
4259 }
Johannes Berg2e161f72010-08-12 15:38:38 +02004260 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, channel_type,
4261 channel_type_valid,
4262 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4263 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4264 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004265 if (err)
4266 goto free_msg;
4267
4268 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4269
4270 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004271 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004272
4273 nla_put_failure:
4274 err = -ENOBUFS;
4275 free_msg:
4276 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004277 return err;
4278}
4279
Kalle Valoffb9eb32010-02-17 17:58:10 +02004280static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4281{
Johannes Berg4c476992010-10-04 21:36:35 +02004282 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004283 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004284 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004285 u8 ps_state;
4286 bool state;
4287 int err;
4288
Johannes Berg4c476992010-10-04 21:36:35 +02004289 if (!info->attrs[NL80211_ATTR_PS_STATE])
4290 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004291
4292 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4293
Johannes Berg4c476992010-10-04 21:36:35 +02004294 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4295 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004296
4297 wdev = dev->ieee80211_ptr;
4298
Johannes Berg4c476992010-10-04 21:36:35 +02004299 if (!rdev->ops->set_power_mgmt)
4300 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004301
4302 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4303
4304 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004305 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004306
Johannes Berg4c476992010-10-04 21:36:35 +02004307 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4308 wdev->ps_timeout);
4309 if (!err)
4310 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004311 return err;
4312}
4313
4314static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4315{
Johannes Berg4c476992010-10-04 21:36:35 +02004316 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004317 enum nl80211_ps_state ps_state;
4318 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004319 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004320 struct sk_buff *msg;
4321 void *hdr;
4322 int err;
4323
Kalle Valoffb9eb32010-02-17 17:58:10 +02004324 wdev = dev->ieee80211_ptr;
4325
Johannes Berg4c476992010-10-04 21:36:35 +02004326 if (!rdev->ops->set_power_mgmt)
4327 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004328
4329 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004330 if (!msg)
4331 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004332
4333 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4334 NL80211_CMD_GET_POWER_SAVE);
4335 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004336 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004337 goto free_msg;
4338 }
4339
4340 if (wdev->ps)
4341 ps_state = NL80211_PS_ENABLED;
4342 else
4343 ps_state = NL80211_PS_DISABLED;
4344
4345 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4346
4347 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004348 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004349
Johannes Berg4c476992010-10-04 21:36:35 +02004350 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004351 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004352 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004353 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004354 return err;
4355}
4356
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004357static struct nla_policy
4358nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4359 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4360 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4361 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4362};
4363
4364static int nl80211_set_cqm_rssi(struct genl_info *info,
4365 s32 threshold, u32 hysteresis)
4366{
Johannes Berg4c476992010-10-04 21:36:35 +02004367 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004368 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004369 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004370
4371 if (threshold > 0)
4372 return -EINVAL;
4373
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004374 wdev = dev->ieee80211_ptr;
4375
Johannes Berg4c476992010-10-04 21:36:35 +02004376 if (!rdev->ops->set_cqm_rssi_config)
4377 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004378
Johannes Berg074ac8d2010-09-16 14:58:22 +02004379 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004380 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4381 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004382
Johannes Berg4c476992010-10-04 21:36:35 +02004383 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4384 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004385}
4386
4387static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4388{
4389 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4390 struct nlattr *cqm;
4391 int err;
4392
4393 cqm = info->attrs[NL80211_ATTR_CQM];
4394 if (!cqm) {
4395 err = -EINVAL;
4396 goto out;
4397 }
4398
4399 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4400 nl80211_attr_cqm_policy);
4401 if (err)
4402 goto out;
4403
4404 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4405 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4406 s32 threshold;
4407 u32 hysteresis;
4408 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4409 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4410 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4411 } else
4412 err = -EINVAL;
4413
4414out:
4415 return err;
4416}
4417
Johannes Berg4c476992010-10-04 21:36:35 +02004418#define NL80211_FLAG_NEED_WIPHY 0x01
4419#define NL80211_FLAG_NEED_NETDEV 0x02
4420#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02004421#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
4422#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
4423 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02004424
4425static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4426 struct genl_info *info)
4427{
4428 struct cfg80211_registered_device *rdev;
4429 struct net_device *dev;
4430 int err;
4431 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4432
4433 if (rtnl)
4434 rtnl_lock();
4435
4436 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4437 rdev = cfg80211_get_dev_from_info(info);
4438 if (IS_ERR(rdev)) {
4439 if (rtnl)
4440 rtnl_unlock();
4441 return PTR_ERR(rdev);
4442 }
4443 info->user_ptr[0] = rdev;
4444 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4445 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4446 if (err) {
4447 if (rtnl)
4448 rtnl_unlock();
4449 return err;
4450 }
Johannes Berg41265712010-10-04 21:14:05 +02004451 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4452 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02004453 cfg80211_unlock_rdev(rdev);
4454 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02004455 if (rtnl)
4456 rtnl_unlock();
4457 return -ENETDOWN;
4458 }
Johannes Berg4c476992010-10-04 21:36:35 +02004459 info->user_ptr[0] = rdev;
4460 info->user_ptr[1] = dev;
4461 }
4462
4463 return 0;
4464}
4465
4466static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4467 struct genl_info *info)
4468{
4469 if (info->user_ptr[0])
4470 cfg80211_unlock_rdev(info->user_ptr[0]);
4471 if (info->user_ptr[1])
4472 dev_put(info->user_ptr[1]);
4473 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4474 rtnl_unlock();
4475}
4476
Johannes Berg55682962007-09-20 13:09:35 -04004477static struct genl_ops nl80211_ops[] = {
4478 {
4479 .cmd = NL80211_CMD_GET_WIPHY,
4480 .doit = nl80211_get_wiphy,
4481 .dumpit = nl80211_dump_wiphy,
4482 .policy = nl80211_policy,
4483 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004484 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04004485 },
4486 {
4487 .cmd = NL80211_CMD_SET_WIPHY,
4488 .doit = nl80211_set_wiphy,
4489 .policy = nl80211_policy,
4490 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004491 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004492 },
4493 {
4494 .cmd = NL80211_CMD_GET_INTERFACE,
4495 .doit = nl80211_get_interface,
4496 .dumpit = nl80211_dump_interface,
4497 .policy = nl80211_policy,
4498 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004499 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04004500 },
4501 {
4502 .cmd = NL80211_CMD_SET_INTERFACE,
4503 .doit = nl80211_set_interface,
4504 .policy = nl80211_policy,
4505 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004506 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4507 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004508 },
4509 {
4510 .cmd = NL80211_CMD_NEW_INTERFACE,
4511 .doit = nl80211_new_interface,
4512 .policy = nl80211_policy,
4513 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004514 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4515 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004516 },
4517 {
4518 .cmd = NL80211_CMD_DEL_INTERFACE,
4519 .doit = nl80211_del_interface,
4520 .policy = nl80211_policy,
4521 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004522 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4523 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004524 },
Johannes Berg41ade002007-12-19 02:03:29 +01004525 {
4526 .cmd = NL80211_CMD_GET_KEY,
4527 .doit = nl80211_get_key,
4528 .policy = nl80211_policy,
4529 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004530 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4531 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004532 },
4533 {
4534 .cmd = NL80211_CMD_SET_KEY,
4535 .doit = nl80211_set_key,
4536 .policy = nl80211_policy,
4537 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004538 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004539 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004540 },
4541 {
4542 .cmd = NL80211_CMD_NEW_KEY,
4543 .doit = nl80211_new_key,
4544 .policy = nl80211_policy,
4545 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004546 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004547 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004548 },
4549 {
4550 .cmd = NL80211_CMD_DEL_KEY,
4551 .doit = nl80211_del_key,
4552 .policy = nl80211_policy,
4553 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004554 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004555 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004556 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004557 {
4558 .cmd = NL80211_CMD_SET_BEACON,
4559 .policy = nl80211_policy,
4560 .flags = GENL_ADMIN_PERM,
4561 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004562 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4563 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004564 },
4565 {
4566 .cmd = NL80211_CMD_NEW_BEACON,
4567 .policy = nl80211_policy,
4568 .flags = GENL_ADMIN_PERM,
4569 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004570 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4571 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004572 },
4573 {
4574 .cmd = NL80211_CMD_DEL_BEACON,
4575 .policy = nl80211_policy,
4576 .flags = GENL_ADMIN_PERM,
4577 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004578 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4579 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004580 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004581 {
4582 .cmd = NL80211_CMD_GET_STATION,
4583 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004584 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004585 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02004586 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4587 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004588 },
4589 {
4590 .cmd = NL80211_CMD_SET_STATION,
4591 .doit = nl80211_set_station,
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,
Johannes Berg5727ef12007-12-19 02:03:34 +01004596 },
4597 {
4598 .cmd = NL80211_CMD_NEW_STATION,
4599 .doit = nl80211_new_station,
4600 .policy = nl80211_policy,
4601 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004602 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004603 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004604 },
4605 {
4606 .cmd = NL80211_CMD_DEL_STATION,
4607 .doit = nl80211_del_station,
4608 .policy = nl80211_policy,
4609 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004610 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4611 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004612 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004613 {
4614 .cmd = NL80211_CMD_GET_MPATH,
4615 .doit = nl80211_get_mpath,
4616 .dumpit = nl80211_dump_mpath,
4617 .policy = nl80211_policy,
4618 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004619 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004620 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004621 },
4622 {
4623 .cmd = NL80211_CMD_SET_MPATH,
4624 .doit = nl80211_set_mpath,
4625 .policy = nl80211_policy,
4626 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004627 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004628 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004629 },
4630 {
4631 .cmd = NL80211_CMD_NEW_MPATH,
4632 .doit = nl80211_new_mpath,
4633 .policy = nl80211_policy,
4634 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004635 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004636 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004637 },
4638 {
4639 .cmd = NL80211_CMD_DEL_MPATH,
4640 .doit = nl80211_del_mpath,
4641 .policy = nl80211_policy,
4642 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004643 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4644 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004645 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004646 {
4647 .cmd = NL80211_CMD_SET_BSS,
4648 .doit = nl80211_set_bss,
4649 .policy = nl80211_policy,
4650 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004651 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4652 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004653 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004654 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004655 .cmd = NL80211_CMD_GET_REG,
4656 .doit = nl80211_get_reg,
4657 .policy = nl80211_policy,
4658 /* can be retrieved by unprivileged users */
4659 },
4660 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004661 .cmd = NL80211_CMD_SET_REG,
4662 .doit = nl80211_set_reg,
4663 .policy = nl80211_policy,
4664 .flags = GENL_ADMIN_PERM,
4665 },
4666 {
4667 .cmd = NL80211_CMD_REQ_SET_REG,
4668 .doit = nl80211_req_set_reg,
4669 .policy = nl80211_policy,
4670 .flags = GENL_ADMIN_PERM,
4671 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004672 {
4673 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4674 .doit = nl80211_get_mesh_params,
4675 .policy = nl80211_policy,
4676 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004677 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4678 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004679 },
4680 {
4681 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4682 .doit = nl80211_set_mesh_params,
4683 .policy = nl80211_policy,
4684 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004685 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4686 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004687 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004688 {
Johannes Berg2a519312009-02-10 21:25:55 +01004689 .cmd = NL80211_CMD_TRIGGER_SCAN,
4690 .doit = nl80211_trigger_scan,
4691 .policy = nl80211_policy,
4692 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004693 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004694 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01004695 },
4696 {
4697 .cmd = NL80211_CMD_GET_SCAN,
4698 .policy = nl80211_policy,
4699 .dumpit = nl80211_dump_scan,
4700 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004701 {
4702 .cmd = NL80211_CMD_AUTHENTICATE,
4703 .doit = nl80211_authenticate,
4704 .policy = nl80211_policy,
4705 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004706 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004707 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004708 },
4709 {
4710 .cmd = NL80211_CMD_ASSOCIATE,
4711 .doit = nl80211_associate,
4712 .policy = nl80211_policy,
4713 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004714 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004715 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004716 },
4717 {
4718 .cmd = NL80211_CMD_DEAUTHENTICATE,
4719 .doit = nl80211_deauthenticate,
4720 .policy = nl80211_policy,
4721 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004722 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004723 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004724 },
4725 {
4726 .cmd = NL80211_CMD_DISASSOCIATE,
4727 .doit = nl80211_disassociate,
4728 .policy = nl80211_policy,
4729 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004730 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004731 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004732 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004733 {
4734 .cmd = NL80211_CMD_JOIN_IBSS,
4735 .doit = nl80211_join_ibss,
4736 .policy = nl80211_policy,
4737 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004738 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004739 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004740 },
4741 {
4742 .cmd = NL80211_CMD_LEAVE_IBSS,
4743 .doit = nl80211_leave_ibss,
4744 .policy = nl80211_policy,
4745 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004746 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004747 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004748 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004749#ifdef CONFIG_NL80211_TESTMODE
4750 {
4751 .cmd = NL80211_CMD_TESTMODE,
4752 .doit = nl80211_testmode_do,
4753 .policy = nl80211_policy,
4754 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004755 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4756 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02004757 },
4758#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004759 {
4760 .cmd = NL80211_CMD_CONNECT,
4761 .doit = nl80211_connect,
4762 .policy = nl80211_policy,
4763 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004764 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004765 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004766 },
4767 {
4768 .cmd = NL80211_CMD_DISCONNECT,
4769 .doit = nl80211_disconnect,
4770 .policy = nl80211_policy,
4771 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004772 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004773 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004774 },
Johannes Berg463d0182009-07-14 00:33:35 +02004775 {
4776 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4777 .doit = nl80211_wiphy_netns,
4778 .policy = nl80211_policy,
4779 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004780 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4781 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02004782 },
Holger Schurig61fa7132009-11-11 12:25:40 +01004783 {
4784 .cmd = NL80211_CMD_GET_SURVEY,
4785 .policy = nl80211_policy,
4786 .dumpit = nl80211_dump_survey,
4787 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004788 {
4789 .cmd = NL80211_CMD_SET_PMKSA,
4790 .doit = nl80211_setdel_pmksa,
4791 .policy = nl80211_policy,
4792 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004793 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4794 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004795 },
4796 {
4797 .cmd = NL80211_CMD_DEL_PMKSA,
4798 .doit = nl80211_setdel_pmksa,
4799 .policy = nl80211_policy,
4800 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004801 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4802 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004803 },
4804 {
4805 .cmd = NL80211_CMD_FLUSH_PMKSA,
4806 .doit = nl80211_flush_pmksa,
4807 .policy = nl80211_policy,
4808 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004809 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4810 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004811 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004812 {
4813 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
4814 .doit = nl80211_remain_on_channel,
4815 .policy = nl80211_policy,
4816 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004817 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004818 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004819 },
4820 {
4821 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
4822 .doit = nl80211_cancel_remain_on_channel,
4823 .policy = nl80211_policy,
4824 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004825 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004826 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004827 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004828 {
4829 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
4830 .doit = nl80211_set_tx_bitrate_mask,
4831 .policy = nl80211_policy,
4832 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004833 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4834 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004835 },
Jouni Malinen026331c2010-02-15 12:53:10 +02004836 {
Johannes Berg2e161f72010-08-12 15:38:38 +02004837 .cmd = NL80211_CMD_REGISTER_FRAME,
4838 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02004839 .policy = nl80211_policy,
4840 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004841 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4842 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02004843 },
4844 {
Johannes Berg2e161f72010-08-12 15:38:38 +02004845 .cmd = NL80211_CMD_FRAME,
4846 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02004847 .policy = nl80211_policy,
4848 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004849 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004850 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02004851 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02004852 {
4853 .cmd = NL80211_CMD_SET_POWER_SAVE,
4854 .doit = nl80211_set_power_save,
4855 .policy = nl80211_policy,
4856 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004857 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4858 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02004859 },
4860 {
4861 .cmd = NL80211_CMD_GET_POWER_SAVE,
4862 .doit = nl80211_get_power_save,
4863 .policy = nl80211_policy,
4864 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004865 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4866 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02004867 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004868 {
4869 .cmd = NL80211_CMD_SET_CQM,
4870 .doit = nl80211_set_cqm,
4871 .policy = nl80211_policy,
4872 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004873 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4874 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004875 },
Johannes Bergf444de02010-05-05 15:25:02 +02004876 {
4877 .cmd = NL80211_CMD_SET_CHANNEL,
4878 .doit = nl80211_set_channel,
4879 .policy = nl80211_policy,
4880 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004881 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4882 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02004883 },
Bill Jordane8347eb2010-10-01 13:54:28 -04004884 {
4885 .cmd = NL80211_CMD_SET_WDS_PEER,
4886 .doit = nl80211_set_wds_peer,
4887 .policy = nl80211_policy,
4888 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02004889 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4890 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04004891 },
Johannes Berg55682962007-09-20 13:09:35 -04004892};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004893
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004894static struct genl_multicast_group nl80211_mlme_mcgrp = {
4895 .name = "mlme",
4896};
Johannes Berg55682962007-09-20 13:09:35 -04004897
4898/* multicast groups */
4899static struct genl_multicast_group nl80211_config_mcgrp = {
4900 .name = "config",
4901};
Johannes Berg2a519312009-02-10 21:25:55 +01004902static struct genl_multicast_group nl80211_scan_mcgrp = {
4903 .name = "scan",
4904};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004905static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4906 .name = "regulatory",
4907};
Johannes Berg55682962007-09-20 13:09:35 -04004908
4909/* notification functions */
4910
4911void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4912{
4913 struct sk_buff *msg;
4914
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004915 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004916 if (!msg)
4917 return;
4918
4919 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4920 nlmsg_free(msg);
4921 return;
4922 }
4923
Johannes Berg463d0182009-07-14 00:33:35 +02004924 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4925 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004926}
4927
Johannes Berg362a4152009-05-24 16:43:15 +02004928static int nl80211_add_scan_req(struct sk_buff *msg,
4929 struct cfg80211_registered_device *rdev)
4930{
4931 struct cfg80211_scan_request *req = rdev->scan_req;
4932 struct nlattr *nest;
4933 int i;
4934
Johannes Berg667503d2009-07-07 03:56:11 +02004935 ASSERT_RDEV_LOCK(rdev);
4936
Johannes Berg362a4152009-05-24 16:43:15 +02004937 if (WARN_ON(!req))
4938 return 0;
4939
4940 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4941 if (!nest)
4942 goto nla_put_failure;
4943 for (i = 0; i < req->n_ssids; i++)
4944 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4945 nla_nest_end(msg, nest);
4946
4947 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4948 if (!nest)
4949 goto nla_put_failure;
4950 for (i = 0; i < req->n_channels; i++)
4951 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4952 nla_nest_end(msg, nest);
4953
4954 if (req->ie)
4955 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4956
4957 return 0;
4958 nla_put_failure:
4959 return -ENOBUFS;
4960}
4961
Johannes Berga538e2d2009-06-16 19:56:42 +02004962static int nl80211_send_scan_msg(struct sk_buff *msg,
4963 struct cfg80211_registered_device *rdev,
4964 struct net_device *netdev,
4965 u32 pid, u32 seq, int flags,
4966 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01004967{
4968 void *hdr;
4969
4970 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4971 if (!hdr)
4972 return -1;
4973
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05004974 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01004975 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4976
Johannes Berg362a4152009-05-24 16:43:15 +02004977 /* ignore errors and send incomplete event anyway */
4978 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004979
4980 return genlmsg_end(msg, hdr);
4981
4982 nla_put_failure:
4983 genlmsg_cancel(msg, hdr);
4984 return -EMSGSIZE;
4985}
4986
Johannes Berga538e2d2009-06-16 19:56:42 +02004987void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4988 struct net_device *netdev)
4989{
4990 struct sk_buff *msg;
4991
4992 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4993 if (!msg)
4994 return;
4995
4996 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4997 NL80211_CMD_TRIGGER_SCAN) < 0) {
4998 nlmsg_free(msg);
4999 return;
5000 }
5001
Johannes Berg463d0182009-07-14 00:33:35 +02005002 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5003 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005004}
5005
Johannes Berg2a519312009-02-10 21:25:55 +01005006void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5007 struct net_device *netdev)
5008{
5009 struct sk_buff *msg;
5010
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005011 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005012 if (!msg)
5013 return;
5014
Johannes Berga538e2d2009-06-16 19:56:42 +02005015 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5016 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005017 nlmsg_free(msg);
5018 return;
5019 }
5020
Johannes Berg463d0182009-07-14 00:33:35 +02005021 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5022 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005023}
5024
5025void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5026 struct net_device *netdev)
5027{
5028 struct sk_buff *msg;
5029
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005030 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005031 if (!msg)
5032 return;
5033
Johannes Berga538e2d2009-06-16 19:56:42 +02005034 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5035 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005036 nlmsg_free(msg);
5037 return;
5038 }
5039
Johannes Berg463d0182009-07-14 00:33:35 +02005040 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5041 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005042}
5043
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005044/*
5045 * This can happen on global regulatory changes or device specific settings
5046 * based on custom world regulatory domains.
5047 */
5048void nl80211_send_reg_change_event(struct regulatory_request *request)
5049{
5050 struct sk_buff *msg;
5051 void *hdr;
5052
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005053 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005054 if (!msg)
5055 return;
5056
5057 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5058 if (!hdr) {
5059 nlmsg_free(msg);
5060 return;
5061 }
5062
5063 /* Userspace can always count this one always being set */
5064 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5065
5066 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5067 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5068 NL80211_REGDOM_TYPE_WORLD);
5069 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5070 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5071 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5072 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5073 request->intersect)
5074 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5075 NL80211_REGDOM_TYPE_INTERSECTION);
5076 else {
5077 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5078 NL80211_REGDOM_TYPE_COUNTRY);
5079 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5080 }
5081
5082 if (wiphy_idx_valid(request->wiphy_idx))
5083 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5084
5085 if (genlmsg_end(msg, hdr) < 0) {
5086 nlmsg_free(msg);
5087 return;
5088 }
5089
Johannes Bergbc43b282009-07-25 10:54:13 +02005090 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005091 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005092 GFP_ATOMIC);
5093 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005094
5095 return;
5096
5097nla_put_failure:
5098 genlmsg_cancel(msg, hdr);
5099 nlmsg_free(msg);
5100}
5101
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005102static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5103 struct net_device *netdev,
5104 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005105 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005106{
5107 struct sk_buff *msg;
5108 void *hdr;
5109
Johannes Berge6d6e342009-07-01 21:26:47 +02005110 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005111 if (!msg)
5112 return;
5113
5114 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5115 if (!hdr) {
5116 nlmsg_free(msg);
5117 return;
5118 }
5119
5120 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5121 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5122 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5123
5124 if (genlmsg_end(msg, hdr) < 0) {
5125 nlmsg_free(msg);
5126 return;
5127 }
5128
Johannes Berg463d0182009-07-14 00:33:35 +02005129 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5130 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005131 return;
5132
5133 nla_put_failure:
5134 genlmsg_cancel(msg, hdr);
5135 nlmsg_free(msg);
5136}
5137
5138void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005139 struct net_device *netdev, const u8 *buf,
5140 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005141{
5142 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005143 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005144}
5145
5146void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5147 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005148 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005149{
Johannes Berge6d6e342009-07-01 21:26:47 +02005150 nl80211_send_mlme_event(rdev, netdev, buf, len,
5151 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005152}
5153
Jouni Malinen53b46b82009-03-27 20:53:56 +02005154void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005155 struct net_device *netdev, const u8 *buf,
5156 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005157{
5158 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005159 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005160}
5161
Jouni Malinen53b46b82009-03-27 20:53:56 +02005162void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5163 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005164 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005165{
5166 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005167 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005168}
5169
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005170static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5171 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005172 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005173{
5174 struct sk_buff *msg;
5175 void *hdr;
5176
Johannes Berge6d6e342009-07-01 21:26:47 +02005177 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005178 if (!msg)
5179 return;
5180
5181 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5182 if (!hdr) {
5183 nlmsg_free(msg);
5184 return;
5185 }
5186
5187 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5188 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5189 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5190 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5191
5192 if (genlmsg_end(msg, hdr) < 0) {
5193 nlmsg_free(msg);
5194 return;
5195 }
5196
Johannes Berg463d0182009-07-14 00:33:35 +02005197 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5198 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005199 return;
5200
5201 nla_put_failure:
5202 genlmsg_cancel(msg, hdr);
5203 nlmsg_free(msg);
5204}
5205
5206void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005207 struct net_device *netdev, const u8 *addr,
5208 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005209{
5210 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005211 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005212}
5213
5214void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005215 struct net_device *netdev, const u8 *addr,
5216 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005217{
Johannes Berge6d6e342009-07-01 21:26:47 +02005218 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5219 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005220}
5221
Samuel Ortizb23aa672009-07-01 21:26:54 +02005222void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5223 struct net_device *netdev, const u8 *bssid,
5224 const u8 *req_ie, size_t req_ie_len,
5225 const u8 *resp_ie, size_t resp_ie_len,
5226 u16 status, gfp_t gfp)
5227{
5228 struct sk_buff *msg;
5229 void *hdr;
5230
5231 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5232 if (!msg)
5233 return;
5234
5235 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5236 if (!hdr) {
5237 nlmsg_free(msg);
5238 return;
5239 }
5240
5241 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5242 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5243 if (bssid)
5244 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5245 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5246 if (req_ie)
5247 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5248 if (resp_ie)
5249 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5250
5251 if (genlmsg_end(msg, hdr) < 0) {
5252 nlmsg_free(msg);
5253 return;
5254 }
5255
Johannes Berg463d0182009-07-14 00:33:35 +02005256 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5257 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005258 return;
5259
5260 nla_put_failure:
5261 genlmsg_cancel(msg, hdr);
5262 nlmsg_free(msg);
5263
5264}
5265
5266void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5267 struct net_device *netdev, const u8 *bssid,
5268 const u8 *req_ie, size_t req_ie_len,
5269 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5270{
5271 struct sk_buff *msg;
5272 void *hdr;
5273
5274 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5275 if (!msg)
5276 return;
5277
5278 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5279 if (!hdr) {
5280 nlmsg_free(msg);
5281 return;
5282 }
5283
5284 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5285 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5286 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5287 if (req_ie)
5288 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5289 if (resp_ie)
5290 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5291
5292 if (genlmsg_end(msg, hdr) < 0) {
5293 nlmsg_free(msg);
5294 return;
5295 }
5296
Johannes Berg463d0182009-07-14 00:33:35 +02005297 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5298 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005299 return;
5300
5301 nla_put_failure:
5302 genlmsg_cancel(msg, hdr);
5303 nlmsg_free(msg);
5304
5305}
5306
5307void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5308 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005309 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005310{
5311 struct sk_buff *msg;
5312 void *hdr;
5313
Johannes Berg667503d2009-07-07 03:56:11 +02005314 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005315 if (!msg)
5316 return;
5317
5318 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5319 if (!hdr) {
5320 nlmsg_free(msg);
5321 return;
5322 }
5323
5324 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5325 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5326 if (from_ap && reason)
5327 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5328 if (from_ap)
5329 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5330 if (ie)
5331 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5332
5333 if (genlmsg_end(msg, hdr) < 0) {
5334 nlmsg_free(msg);
5335 return;
5336 }
5337
Johannes Berg463d0182009-07-14 00:33:35 +02005338 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5339 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005340 return;
5341
5342 nla_put_failure:
5343 genlmsg_cancel(msg, hdr);
5344 nlmsg_free(msg);
5345
5346}
5347
Johannes Berg04a773a2009-04-19 21:24:32 +02005348void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5349 struct net_device *netdev, const u8 *bssid,
5350 gfp_t gfp)
5351{
5352 struct sk_buff *msg;
5353 void *hdr;
5354
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005355 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005356 if (!msg)
5357 return;
5358
5359 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5360 if (!hdr) {
5361 nlmsg_free(msg);
5362 return;
5363 }
5364
5365 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5366 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5367 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5368
5369 if (genlmsg_end(msg, hdr) < 0) {
5370 nlmsg_free(msg);
5371 return;
5372 }
5373
Johannes Berg463d0182009-07-14 00:33:35 +02005374 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5375 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005376 return;
5377
5378 nla_put_failure:
5379 genlmsg_cancel(msg, hdr);
5380 nlmsg_free(msg);
5381}
5382
Jouni Malinena3b8b052009-03-27 21:59:49 +02005383void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5384 struct net_device *netdev, const u8 *addr,
5385 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005386 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005387{
5388 struct sk_buff *msg;
5389 void *hdr;
5390
Johannes Berge6d6e342009-07-01 21:26:47 +02005391 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005392 if (!msg)
5393 return;
5394
5395 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5396 if (!hdr) {
5397 nlmsg_free(msg);
5398 return;
5399 }
5400
5401 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5402 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5403 if (addr)
5404 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5405 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5406 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5407 if (tsc)
5408 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5409
5410 if (genlmsg_end(msg, hdr) < 0) {
5411 nlmsg_free(msg);
5412 return;
5413 }
5414
Johannes Berg463d0182009-07-14 00:33:35 +02005415 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5416 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005417 return;
5418
5419 nla_put_failure:
5420 genlmsg_cancel(msg, hdr);
5421 nlmsg_free(msg);
5422}
5423
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005424void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5425 struct ieee80211_channel *channel_before,
5426 struct ieee80211_channel *channel_after)
5427{
5428 struct sk_buff *msg;
5429 void *hdr;
5430 struct nlattr *nl_freq;
5431
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005432 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005433 if (!msg)
5434 return;
5435
5436 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5437 if (!hdr) {
5438 nlmsg_free(msg);
5439 return;
5440 }
5441
5442 /*
5443 * Since we are applying the beacon hint to a wiphy we know its
5444 * wiphy_idx is valid
5445 */
5446 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5447
5448 /* Before */
5449 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5450 if (!nl_freq)
5451 goto nla_put_failure;
5452 if (nl80211_msg_put_channel(msg, channel_before))
5453 goto nla_put_failure;
5454 nla_nest_end(msg, nl_freq);
5455
5456 /* After */
5457 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5458 if (!nl_freq)
5459 goto nla_put_failure;
5460 if (nl80211_msg_put_channel(msg, channel_after))
5461 goto nla_put_failure;
5462 nla_nest_end(msg, nl_freq);
5463
5464 if (genlmsg_end(msg, hdr) < 0) {
5465 nlmsg_free(msg);
5466 return;
5467 }
5468
Johannes Berg463d0182009-07-14 00:33:35 +02005469 rcu_read_lock();
5470 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5471 GFP_ATOMIC);
5472 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005473
5474 return;
5475
5476nla_put_failure:
5477 genlmsg_cancel(msg, hdr);
5478 nlmsg_free(msg);
5479}
5480
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005481static void nl80211_send_remain_on_chan_event(
5482 int cmd, struct cfg80211_registered_device *rdev,
5483 struct net_device *netdev, u64 cookie,
5484 struct ieee80211_channel *chan,
5485 enum nl80211_channel_type channel_type,
5486 unsigned int duration, gfp_t gfp)
5487{
5488 struct sk_buff *msg;
5489 void *hdr;
5490
5491 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5492 if (!msg)
5493 return;
5494
5495 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5496 if (!hdr) {
5497 nlmsg_free(msg);
5498 return;
5499 }
5500
5501 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5502 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5503 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5504 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5505 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5506
5507 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5508 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5509
5510 if (genlmsg_end(msg, hdr) < 0) {
5511 nlmsg_free(msg);
5512 return;
5513 }
5514
5515 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5516 nl80211_mlme_mcgrp.id, gfp);
5517 return;
5518
5519 nla_put_failure:
5520 genlmsg_cancel(msg, hdr);
5521 nlmsg_free(msg);
5522}
5523
5524void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5525 struct net_device *netdev, u64 cookie,
5526 struct ieee80211_channel *chan,
5527 enum nl80211_channel_type channel_type,
5528 unsigned int duration, gfp_t gfp)
5529{
5530 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5531 rdev, netdev, cookie, chan,
5532 channel_type, duration, gfp);
5533}
5534
5535void nl80211_send_remain_on_channel_cancel(
5536 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5537 u64 cookie, struct ieee80211_channel *chan,
5538 enum nl80211_channel_type channel_type, gfp_t gfp)
5539{
5540 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5541 rdev, netdev, cookie, chan,
5542 channel_type, 0, gfp);
5543}
5544
Johannes Berg98b62182009-12-23 13:15:44 +01005545void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5546 struct net_device *dev, const u8 *mac_addr,
5547 struct station_info *sinfo, gfp_t gfp)
5548{
5549 struct sk_buff *msg;
5550
5551 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5552 if (!msg)
5553 return;
5554
5555 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5556 nlmsg_free(msg);
5557 return;
5558 }
5559
5560 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5561 nl80211_mlme_mcgrp.id, gfp);
5562}
5563
Johannes Berg2e161f72010-08-12 15:38:38 +02005564int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5565 struct net_device *netdev, u32 nlpid,
5566 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005567{
5568 struct sk_buff *msg;
5569 void *hdr;
5570 int err;
5571
5572 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5573 if (!msg)
5574 return -ENOMEM;
5575
Johannes Berg2e161f72010-08-12 15:38:38 +02005576 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005577 if (!hdr) {
5578 nlmsg_free(msg);
5579 return -ENOMEM;
5580 }
5581
5582 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5583 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5584 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5585 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5586
5587 err = genlmsg_end(msg, hdr);
5588 if (err < 0) {
5589 nlmsg_free(msg);
5590 return err;
5591 }
5592
5593 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5594 if (err < 0)
5595 return err;
5596 return 0;
5597
5598 nla_put_failure:
5599 genlmsg_cancel(msg, hdr);
5600 nlmsg_free(msg);
5601 return -ENOBUFS;
5602}
5603
Johannes Berg2e161f72010-08-12 15:38:38 +02005604void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5605 struct net_device *netdev, u64 cookie,
5606 const u8 *buf, size_t len, bool ack,
5607 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005608{
5609 struct sk_buff *msg;
5610 void *hdr;
5611
5612 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5613 if (!msg)
5614 return;
5615
Johannes Berg2e161f72010-08-12 15:38:38 +02005616 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02005617 if (!hdr) {
5618 nlmsg_free(msg);
5619 return;
5620 }
5621
5622 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5623 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5624 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5625 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5626 if (ack)
5627 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5628
5629 if (genlmsg_end(msg, hdr) < 0) {
5630 nlmsg_free(msg);
5631 return;
5632 }
5633
5634 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5635 return;
5636
5637 nla_put_failure:
5638 genlmsg_cancel(msg, hdr);
5639 nlmsg_free(msg);
5640}
5641
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005642void
5643nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5644 struct net_device *netdev,
5645 enum nl80211_cqm_rssi_threshold_event rssi_event,
5646 gfp_t gfp)
5647{
5648 struct sk_buff *msg;
5649 struct nlattr *pinfoattr;
5650 void *hdr;
5651
5652 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5653 if (!msg)
5654 return;
5655
5656 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5657 if (!hdr) {
5658 nlmsg_free(msg);
5659 return;
5660 }
5661
5662 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5663 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5664
5665 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5666 if (!pinfoattr)
5667 goto nla_put_failure;
5668
5669 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5670 rssi_event);
5671
5672 nla_nest_end(msg, pinfoattr);
5673
5674 if (genlmsg_end(msg, hdr) < 0) {
5675 nlmsg_free(msg);
5676 return;
5677 }
5678
5679 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5680 nl80211_mlme_mcgrp.id, gfp);
5681 return;
5682
5683 nla_put_failure:
5684 genlmsg_cancel(msg, hdr);
5685 nlmsg_free(msg);
5686}
5687
Jouni Malinen026331c2010-02-15 12:53:10 +02005688static int nl80211_netlink_notify(struct notifier_block * nb,
5689 unsigned long state,
5690 void *_notify)
5691{
5692 struct netlink_notify *notify = _notify;
5693 struct cfg80211_registered_device *rdev;
5694 struct wireless_dev *wdev;
5695
5696 if (state != NETLINK_URELEASE)
5697 return NOTIFY_DONE;
5698
5699 rcu_read_lock();
5700
5701 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5702 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02005703 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02005704
5705 rcu_read_unlock();
5706
5707 return NOTIFY_DONE;
5708}
5709
5710static struct notifier_block nl80211_netlink_notifier = {
5711 .notifier_call = nl80211_netlink_notify,
5712};
5713
Johannes Berg55682962007-09-20 13:09:35 -04005714/* initialisation/exit functions */
5715
5716int nl80211_init(void)
5717{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005718 int err;
Johannes Berg55682962007-09-20 13:09:35 -04005719
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005720 err = genl_register_family_with_ops(&nl80211_fam,
5721 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04005722 if (err)
5723 return err;
5724
Johannes Berg55682962007-09-20 13:09:35 -04005725 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5726 if (err)
5727 goto err_out;
5728
Johannes Berg2a519312009-02-10 21:25:55 +01005729 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5730 if (err)
5731 goto err_out;
5732
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005733 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5734 if (err)
5735 goto err_out;
5736
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005737 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5738 if (err)
5739 goto err_out;
5740
Johannes Bergaff89a92009-07-01 21:26:51 +02005741#ifdef CONFIG_NL80211_TESTMODE
5742 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5743 if (err)
5744 goto err_out;
5745#endif
5746
Jouni Malinen026331c2010-02-15 12:53:10 +02005747 err = netlink_register_notifier(&nl80211_netlink_notifier);
5748 if (err)
5749 goto err_out;
5750
Johannes Berg55682962007-09-20 13:09:35 -04005751 return 0;
5752 err_out:
5753 genl_unregister_family(&nl80211_fam);
5754 return err;
5755}
5756
5757void nl80211_exit(void)
5758{
Jouni Malinen026331c2010-02-15 12:53:10 +02005759 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04005760 genl_unregister_family(&nl80211_fam);
5761}