blob: c8d4d53fc450014b37d2a6f514c29512b81e504d [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 Malinen36aedc902008-08-25 11:58:58 +0300124
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700125 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
126
Jouni Malinen36aedc902008-08-25 11:58:58 +0300127 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
128 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200129
130 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
131 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
132 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100133 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
134 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200135
136 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
137 .len = IEEE80211_MAX_SSID_LEN },
138 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
139 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200140 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300141 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300142 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300143 [NL80211_ATTR_STA_FLAGS2] = {
144 .len = sizeof(struct nl80211_sta_flag_update),
145 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300146 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300147 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
148 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200149 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
150 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
151 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200152 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100153 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100154 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
155 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100156 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
157 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200158 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200159 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
160 .len = IEEE80211_MAX_DATA_LEN },
161 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200162 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200163 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300164 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200165 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300166 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
167 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200168 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900169 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
170 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100171 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100172 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400173};
174
Johannes Berge31b8212010-10-05 19:39:30 +0200175/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000176static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200177 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200178 [NL80211_KEY_IDX] = { .type = NLA_U8 },
179 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
180 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
181 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
182 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200183 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200184};
185
Holger Schuriga0438972009-11-11 11:30:02 +0100186/* ifidx get helper */
187static int nl80211_get_ifidx(struct netlink_callback *cb)
188{
189 int res;
190
191 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
192 nl80211_fam.attrbuf, nl80211_fam.maxattr,
193 nl80211_policy);
194 if (res)
195 return res;
196
197 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
198 return -EINVAL;
199
200 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
201 if (!res)
202 return -EINVAL;
203 return res;
204}
205
Johannes Berg67748892010-10-04 21:14:06 +0200206static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
207 struct netlink_callback *cb,
208 struct cfg80211_registered_device **rdev,
209 struct net_device **dev)
210{
211 int ifidx = cb->args[0];
212 int err;
213
214 if (!ifidx)
215 ifidx = nl80211_get_ifidx(cb);
216 if (ifidx < 0)
217 return ifidx;
218
219 cb->args[0] = ifidx;
220
221 rtnl_lock();
222
223 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
224 if (!*dev) {
225 err = -ENODEV;
226 goto out_rtnl;
227 }
228
229 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100230 if (IS_ERR(*rdev)) {
231 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200232 goto out_rtnl;
233 }
234
235 return 0;
236 out_rtnl:
237 rtnl_unlock();
238 return err;
239}
240
241static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
242{
243 cfg80211_unlock_rdev(rdev);
244 rtnl_unlock();
245}
246
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100247/* IE validation */
248static bool is_valid_ie_attr(const struct nlattr *attr)
249{
250 const u8 *pos;
251 int len;
252
253 if (!attr)
254 return true;
255
256 pos = nla_data(attr);
257 len = nla_len(attr);
258
259 while (len) {
260 u8 elemlen;
261
262 if (len < 2)
263 return false;
264 len -= 2;
265
266 elemlen = pos[1];
267 if (elemlen > len)
268 return false;
269
270 len -= elemlen;
271 pos += 2 + elemlen;
272 }
273
274 return true;
275}
276
Johannes Berg55682962007-09-20 13:09:35 -0400277/* message building helper */
278static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
279 int flags, u8 cmd)
280{
281 /* since there is no private header just add the generic one */
282 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
283}
284
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400285static int nl80211_msg_put_channel(struct sk_buff *msg,
286 struct ieee80211_channel *chan)
287{
288 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
289 chan->center_freq);
290
291 if (chan->flags & IEEE80211_CHAN_DISABLED)
292 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
293 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
294 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
295 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
296 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
297 if (chan->flags & IEEE80211_CHAN_RADAR)
298 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
299
300 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
301 DBM_TO_MBM(chan->max_power));
302
303 return 0;
304
305 nla_put_failure:
306 return -ENOBUFS;
307}
308
Johannes Berg55682962007-09-20 13:09:35 -0400309/* netlink command implementations */
310
Johannes Bergb9454e82009-07-08 13:29:08 +0200311struct key_parse {
312 struct key_params p;
313 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200314 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200315 bool def, defmgmt;
316};
317
318static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
319{
320 struct nlattr *tb[NL80211_KEY_MAX + 1];
321 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
322 nl80211_key_policy);
323 if (err)
324 return err;
325
326 k->def = !!tb[NL80211_KEY_DEFAULT];
327 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
328
329 if (tb[NL80211_KEY_IDX])
330 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
331
332 if (tb[NL80211_KEY_DATA]) {
333 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
334 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
335 }
336
337 if (tb[NL80211_KEY_SEQ]) {
338 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
339 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
340 }
341
342 if (tb[NL80211_KEY_CIPHER])
343 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
344
Johannes Berge31b8212010-10-05 19:39:30 +0200345 if (tb[NL80211_KEY_TYPE]) {
346 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
347 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
348 return -EINVAL;
349 }
350
Johannes Bergb9454e82009-07-08 13:29:08 +0200351 return 0;
352}
353
354static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
355{
356 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
357 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
358 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
359 }
360
361 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
362 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
363 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
364 }
365
366 if (info->attrs[NL80211_ATTR_KEY_IDX])
367 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
368
369 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
370 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
371
372 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
373 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
374
Johannes Berge31b8212010-10-05 19:39:30 +0200375 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
376 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
377 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
378 return -EINVAL;
379 }
380
Johannes Bergb9454e82009-07-08 13:29:08 +0200381 return 0;
382}
383
384static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
385{
386 int err;
387
388 memset(k, 0, sizeof(*k));
389 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200390 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200391
392 if (info->attrs[NL80211_ATTR_KEY])
393 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
394 else
395 err = nl80211_parse_key_old(info, k);
396
397 if (err)
398 return err;
399
400 if (k->def && k->defmgmt)
401 return -EINVAL;
402
403 if (k->idx != -1) {
404 if (k->defmgmt) {
405 if (k->idx < 4 || k->idx > 5)
406 return -EINVAL;
407 } else if (k->def) {
408 if (k->idx < 0 || k->idx > 3)
409 return -EINVAL;
410 } else {
411 if (k->idx < 0 || k->idx > 5)
412 return -EINVAL;
413 }
414 }
415
416 return 0;
417}
418
Johannes Bergfffd0932009-07-08 14:22:54 +0200419static struct cfg80211_cached_keys *
420nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
421 struct nlattr *keys)
422{
423 struct key_parse parse;
424 struct nlattr *key;
425 struct cfg80211_cached_keys *result;
426 int rem, err, def = 0;
427
428 result = kzalloc(sizeof(*result), GFP_KERNEL);
429 if (!result)
430 return ERR_PTR(-ENOMEM);
431
432 result->def = -1;
433 result->defmgmt = -1;
434
435 nla_for_each_nested(key, keys, rem) {
436 memset(&parse, 0, sizeof(parse));
437 parse.idx = -1;
438
439 err = nl80211_parse_key_new(key, &parse);
440 if (err)
441 goto error;
442 err = -EINVAL;
443 if (!parse.p.key)
444 goto error;
445 if (parse.idx < 0 || parse.idx > 4)
446 goto error;
447 if (parse.def) {
448 if (def)
449 goto error;
450 def = 1;
451 result->def = parse.idx;
452 } else if (parse.defmgmt)
453 goto error;
454 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200455 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200456 if (err)
457 goto error;
458 result->params[parse.idx].cipher = parse.p.cipher;
459 result->params[parse.idx].key_len = parse.p.key_len;
460 result->params[parse.idx].key = result->data[parse.idx];
461 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
462 }
463
464 return result;
465 error:
466 kfree(result);
467 return ERR_PTR(err);
468}
469
470static int nl80211_key_allowed(struct wireless_dev *wdev)
471{
472 ASSERT_WDEV_LOCK(wdev);
473
Johannes Bergfffd0932009-07-08 14:22:54 +0200474 switch (wdev->iftype) {
475 case NL80211_IFTYPE_AP:
476 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200477 case NL80211_IFTYPE_P2P_GO:
Johannes Bergfffd0932009-07-08 14:22:54 +0200478 break;
479 case NL80211_IFTYPE_ADHOC:
480 if (!wdev->current_bss)
481 return -ENOLINK;
482 break;
483 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200484 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200485 if (wdev->sme_state != CFG80211_SME_CONNECTED)
486 return -ENOLINK;
487 break;
488 default:
489 return -EINVAL;
490 }
491
492 return 0;
493}
494
Johannes Berg55682962007-09-20 13:09:35 -0400495static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
496 struct cfg80211_registered_device *dev)
497{
498 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100499 struct nlattr *nl_bands, *nl_band;
500 struct nlattr *nl_freqs, *nl_freq;
501 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700502 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100503 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100504 enum ieee80211_band band;
505 struct ieee80211_channel *chan;
506 struct ieee80211_rate *rate;
507 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700508 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg2e161f72010-08-12 15:38:38 +0200509 const struct ieee80211_txrx_stypes *mgmt_stypes =
510 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400511
512 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
513 if (!hdr)
514 return -1;
515
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500516 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400517 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200518
Johannes Bergf5ea9122009-08-07 16:17:38 +0200519 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
520 cfg80211_rdev_list_generation);
521
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200522 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
523 dev->wiphy.retry_short);
524 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
525 dev->wiphy.retry_long);
526 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
527 dev->wiphy.frag_threshold);
528 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
529 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100530 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
531 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100532 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
533 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200534 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
535 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100536
Johannes Berge31b8212010-10-05 19:39:30 +0200537 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
538 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
539
Johannes Berg25e47c12009-04-02 20:14:06 +0200540 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
541 sizeof(u32) * dev->wiphy.n_cipher_suites,
542 dev->wiphy.cipher_suites);
543
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100544 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
545 dev->wiphy.max_num_pmkids);
546
Johannes Bergc0692b82010-08-27 14:26:53 +0300547 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
548 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
549
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900550 if (dev->ops->get_antenna) {
551 u32 tx_ant = 0, rx_ant = 0;
552 int res;
553 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
554 if (!res) {
555 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
556 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
557 }
558 }
559
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700560 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
561 if (!nl_modes)
562 goto nla_put_failure;
563
564 i = 0;
565 while (ifmodes) {
566 if (ifmodes & 1)
567 NLA_PUT_FLAG(msg, i);
568 ifmodes >>= 1;
569 i++;
570 }
571
572 nla_nest_end(msg, nl_modes);
573
Johannes Bergee688b002008-01-24 19:38:39 +0100574 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
575 if (!nl_bands)
576 goto nla_put_failure;
577
578 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
579 if (!dev->wiphy.bands[band])
580 continue;
581
582 nl_band = nla_nest_start(msg, band);
583 if (!nl_band)
584 goto nla_put_failure;
585
Johannes Bergd51626d2008-10-09 12:20:13 +0200586 /* add HT info */
587 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
588 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
589 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
590 &dev->wiphy.bands[band]->ht_cap.mcs);
591 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
592 dev->wiphy.bands[band]->ht_cap.cap);
593 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
594 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
595 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
596 dev->wiphy.bands[band]->ht_cap.ampdu_density);
597 }
598
Johannes Bergee688b002008-01-24 19:38:39 +0100599 /* add frequencies */
600 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
601 if (!nl_freqs)
602 goto nla_put_failure;
603
604 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
605 nl_freq = nla_nest_start(msg, i);
606 if (!nl_freq)
607 goto nla_put_failure;
608
609 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100610
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400611 if (nl80211_msg_put_channel(msg, chan))
612 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200613
Johannes Bergee688b002008-01-24 19:38:39 +0100614 nla_nest_end(msg, nl_freq);
615 }
616
617 nla_nest_end(msg, nl_freqs);
618
619 /* add bitrates */
620 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
621 if (!nl_rates)
622 goto nla_put_failure;
623
624 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
625 nl_rate = nla_nest_start(msg, i);
626 if (!nl_rate)
627 goto nla_put_failure;
628
629 rate = &dev->wiphy.bands[band]->bitrates[i];
630 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
631 rate->bitrate);
632 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
633 NLA_PUT_FLAG(msg,
634 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
635
636 nla_nest_end(msg, nl_rate);
637 }
638
639 nla_nest_end(msg, nl_rates);
640
641 nla_nest_end(msg, nl_band);
642 }
643 nla_nest_end(msg, nl_bands);
644
Johannes Berg8fdc6212009-03-14 09:34:01 +0100645 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
646 if (!nl_cmds)
647 goto nla_put_failure;
648
649 i = 0;
650#define CMD(op, n) \
651 do { \
652 if (dev->ops->op) { \
653 i++; \
654 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
655 } \
656 } while (0)
657
658 CMD(add_virtual_intf, NEW_INTERFACE);
659 CMD(change_virtual_intf, SET_INTERFACE);
660 CMD(add_key, NEW_KEY);
661 CMD(add_beacon, NEW_BEACON);
662 CMD(add_station, NEW_STATION);
663 CMD(add_mpath, NEW_MPATH);
664 CMD(set_mesh_params, SET_MESH_PARAMS);
665 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200666 CMD(auth, AUTHENTICATE);
667 CMD(assoc, ASSOCIATE);
668 CMD(deauth, DEAUTHENTICATE);
669 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200670 CMD(join_ibss, JOIN_IBSS);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100671 CMD(set_pmksa, SET_PMKSA);
672 CMD(del_pmksa, DEL_PMKSA);
673 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100674 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200675 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200676 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100677 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100678 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200679 i++;
680 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
681 }
Johannes Bergf444de02010-05-05 15:25:02 +0200682 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400683 CMD(set_wds_peer, SET_WDS_PEER);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100684
685#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200686
Johannes Berg6829c872009-07-02 09:13:27 +0200687 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200688 i++;
689 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
690 }
691
Johannes Berg6829c872009-07-02 09:13:27 +0200692 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200693 i++;
694 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
695 }
696
Johannes Berg8fdc6212009-03-14 09:34:01 +0100697 nla_nest_end(msg, nl_cmds);
698
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100699 /* for now at least assume all drivers have it */
700 if (dev->ops->mgmt_tx)
701 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
702
Johannes Berg2e161f72010-08-12 15:38:38 +0200703 if (mgmt_stypes) {
704 u16 stypes;
705 struct nlattr *nl_ftypes, *nl_ifs;
706 enum nl80211_iftype ift;
707
708 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
709 if (!nl_ifs)
710 goto nla_put_failure;
711
712 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
713 nl_ftypes = nla_nest_start(msg, ift);
714 if (!nl_ftypes)
715 goto nla_put_failure;
716 i = 0;
717 stypes = mgmt_stypes[ift].tx;
718 while (stypes) {
719 if (stypes & 1)
720 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
721 (i << 4) | IEEE80211_FTYPE_MGMT);
722 stypes >>= 1;
723 i++;
724 }
725 nla_nest_end(msg, nl_ftypes);
726 }
727
Johannes Berg74b70a42010-08-24 12:15:53 +0200728 nla_nest_end(msg, nl_ifs);
729
Johannes Berg2e161f72010-08-12 15:38:38 +0200730 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
731 if (!nl_ifs)
732 goto nla_put_failure;
733
734 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
735 nl_ftypes = nla_nest_start(msg, ift);
736 if (!nl_ftypes)
737 goto nla_put_failure;
738 i = 0;
739 stypes = mgmt_stypes[ift].rx;
740 while (stypes) {
741 if (stypes & 1)
742 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
743 (i << 4) | IEEE80211_FTYPE_MGMT);
744 stypes >>= 1;
745 i++;
746 }
747 nla_nest_end(msg, nl_ftypes);
748 }
749 nla_nest_end(msg, nl_ifs);
750 }
751
Johannes Berg55682962007-09-20 13:09:35 -0400752 return genlmsg_end(msg, hdr);
753
754 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700755 genlmsg_cancel(msg, hdr);
756 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400757}
758
759static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
760{
761 int idx = 0;
762 int start = cb->args[0];
763 struct cfg80211_registered_device *dev;
764
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500765 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200766 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200767 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
768 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200769 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400770 continue;
771 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
772 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200773 dev) < 0) {
774 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400775 break;
Julius Volzb4637272008-07-08 14:02:19 +0200776 }
Johannes Berg55682962007-09-20 13:09:35 -0400777 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500778 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400779
780 cb->args[0] = idx;
781
782 return skb->len;
783}
784
785static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
786{
787 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200788 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400789
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700790 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400791 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200792 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400793
Johannes Berg4c476992010-10-04 21:36:35 +0200794 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
795 nlmsg_free(msg);
796 return -ENOBUFS;
797 }
Johannes Berg55682962007-09-20 13:09:35 -0400798
Johannes Berg134e6372009-07-10 09:51:34 +0000799 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400800}
801
Jouni Malinen31888482008-10-30 16:59:24 +0200802static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
803 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
804 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
805 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
806 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
807 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
808};
809
810static int parse_txq_params(struct nlattr *tb[],
811 struct ieee80211_txq_params *txq_params)
812{
813 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
814 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
815 !tb[NL80211_TXQ_ATTR_AIFS])
816 return -EINVAL;
817
818 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
819 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
820 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
821 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
822 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
823
824 return 0;
825}
826
Johannes Bergf444de02010-05-05 15:25:02 +0200827static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
828{
829 /*
830 * You can only set the channel explicitly for AP, mesh
831 * and WDS type interfaces; all others have their channel
832 * managed via their respective "establish a connection"
833 * command (connect, join, ...)
834 *
835 * Monitors are special as they are normally slaved to
836 * whatever else is going on, so they behave as though
837 * you tried setting the wiphy channel itself.
838 */
839 return !wdev ||
840 wdev->iftype == NL80211_IFTYPE_AP ||
841 wdev->iftype == NL80211_IFTYPE_WDS ||
842 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200843 wdev->iftype == NL80211_IFTYPE_MONITOR ||
844 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200845}
846
847static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
848 struct wireless_dev *wdev,
849 struct genl_info *info)
850{
851 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
852 u32 freq;
853 int result;
854
855 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
856 return -EINVAL;
857
858 if (!nl80211_can_set_dev_channel(wdev))
859 return -EOPNOTSUPP;
860
861 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
862 channel_type = nla_get_u32(info->attrs[
863 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
864 if (channel_type != NL80211_CHAN_NO_HT &&
865 channel_type != NL80211_CHAN_HT20 &&
866 channel_type != NL80211_CHAN_HT40PLUS &&
867 channel_type != NL80211_CHAN_HT40MINUS)
868 return -EINVAL;
869 }
870
871 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
872
873 mutex_lock(&rdev->devlist_mtx);
874 if (wdev) {
875 wdev_lock(wdev);
876 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
877 wdev_unlock(wdev);
878 } else {
879 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
880 }
881 mutex_unlock(&rdev->devlist_mtx);
882
883 return result;
884}
885
886static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
887{
Johannes Berg4c476992010-10-04 21:36:35 +0200888 struct cfg80211_registered_device *rdev = info->user_ptr[0];
889 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +0200890
Johannes Berg4c476992010-10-04 21:36:35 +0200891 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +0200892}
893
Bill Jordane8347eb2010-10-01 13:54:28 -0400894static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
895{
Johannes Berg43b19952010-10-07 13:10:30 +0200896 struct cfg80211_registered_device *rdev = info->user_ptr[0];
897 struct net_device *dev = info->user_ptr[1];
898 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +0200899 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -0400900
901 if (!info->attrs[NL80211_ATTR_MAC])
902 return -EINVAL;
903
Johannes Berg43b19952010-10-07 13:10:30 +0200904 if (netif_running(dev))
905 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -0400906
Johannes Berg43b19952010-10-07 13:10:30 +0200907 if (!rdev->ops->set_wds_peer)
908 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400909
Johannes Berg43b19952010-10-07 13:10:30 +0200910 if (wdev->iftype != NL80211_IFTYPE_WDS)
911 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400912
913 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +0200914 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -0400915}
916
917
Johannes Berg55682962007-09-20 13:09:35 -0400918static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
919{
920 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200921 struct net_device *netdev = NULL;
922 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -0400923 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200924 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200925 u32 changed;
926 u8 retry_short = 0, retry_long = 0;
927 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100928 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400929
Johannes Bergf444de02010-05-05 15:25:02 +0200930 /*
931 * Try to find the wiphy and netdev. Normally this
932 * function shouldn't need the netdev, but this is
933 * done for backward compatibility -- previously
934 * setting the channel was done per wiphy, but now
935 * it is per netdev. Previous userland like hostapd
936 * also passed a netdev to set_wiphy, so that it is
937 * possible to let that go to the right netdev!
938 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100939 mutex_lock(&cfg80211_mutex);
940
Johannes Bergf444de02010-05-05 15:25:02 +0200941 if (info->attrs[NL80211_ATTR_IFINDEX]) {
942 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
943
944 netdev = dev_get_by_index(genl_info_net(info), ifindex);
945 if (netdev && netdev->ieee80211_ptr) {
946 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
947 mutex_lock(&rdev->mtx);
948 } else
949 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100950 }
951
Johannes Bergf444de02010-05-05 15:25:02 +0200952 if (!netdev) {
953 rdev = __cfg80211_rdev_from_info(info);
954 if (IS_ERR(rdev)) {
955 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +0200956 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +0200957 }
958 wdev = NULL;
959 netdev = NULL;
960 result = 0;
961
962 mutex_lock(&rdev->mtx);
963 } else if (netif_running(netdev) &&
964 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
965 wdev = netdev->ieee80211_ptr;
966 else
967 wdev = NULL;
968
969 /*
970 * end workaround code, by now the rdev is available
971 * and locked, and wdev may or may not be NULL.
972 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100973
974 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200975 result = cfg80211_dev_rename(
976 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100977
978 mutex_unlock(&cfg80211_mutex);
979
980 if (result)
981 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400982
Jouni Malinen31888482008-10-30 16:59:24 +0200983 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
984 struct ieee80211_txq_params txq_params;
985 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
986
987 if (!rdev->ops->set_txq_params) {
988 result = -EOPNOTSUPP;
989 goto bad_res;
990 }
991
992 nla_for_each_nested(nl_txq_params,
993 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
994 rem_txq_params) {
995 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
996 nla_data(nl_txq_params),
997 nla_len(nl_txq_params),
998 txq_params_policy);
999 result = parse_txq_params(tb, &txq_params);
1000 if (result)
1001 goto bad_res;
1002
1003 result = rdev->ops->set_txq_params(&rdev->wiphy,
1004 &txq_params);
1005 if (result)
1006 goto bad_res;
1007 }
1008 }
1009
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001010 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001011 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001012 if (result)
1013 goto bad_res;
1014 }
1015
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001016 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1017 enum nl80211_tx_power_setting type;
1018 int idx, mbm = 0;
1019
1020 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001021 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001022 goto bad_res;
1023 }
1024
1025 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1026 type = nla_get_u32(info->attrs[idx]);
1027
1028 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1029 (type != NL80211_TX_POWER_AUTOMATIC)) {
1030 result = -EINVAL;
1031 goto bad_res;
1032 }
1033
1034 if (type != NL80211_TX_POWER_AUTOMATIC) {
1035 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1036 mbm = nla_get_u32(info->attrs[idx]);
1037 }
1038
1039 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1040 if (result)
1041 goto bad_res;
1042 }
1043
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001044 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1045 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1046 u32 tx_ant, rx_ant;
1047 if (!rdev->ops->set_antenna) {
1048 result = -EOPNOTSUPP;
1049 goto bad_res;
1050 }
1051
1052 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1053 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1054
1055 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1056 if (result)
1057 goto bad_res;
1058 }
1059
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001060 changed = 0;
1061
1062 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1063 retry_short = nla_get_u8(
1064 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1065 if (retry_short == 0) {
1066 result = -EINVAL;
1067 goto bad_res;
1068 }
1069 changed |= WIPHY_PARAM_RETRY_SHORT;
1070 }
1071
1072 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1073 retry_long = nla_get_u8(
1074 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1075 if (retry_long == 0) {
1076 result = -EINVAL;
1077 goto bad_res;
1078 }
1079 changed |= WIPHY_PARAM_RETRY_LONG;
1080 }
1081
1082 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1083 frag_threshold = nla_get_u32(
1084 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1085 if (frag_threshold < 256) {
1086 result = -EINVAL;
1087 goto bad_res;
1088 }
1089 if (frag_threshold != (u32) -1) {
1090 /*
1091 * Fragments (apart from the last one) are required to
1092 * have even length. Make the fragmentation code
1093 * simpler by stripping LSB should someone try to use
1094 * odd threshold value.
1095 */
1096 frag_threshold &= ~0x1;
1097 }
1098 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1099 }
1100
1101 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1102 rts_threshold = nla_get_u32(
1103 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1104 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1105 }
1106
Lukáš Turek81077e82009-12-21 22:50:47 +01001107 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1108 coverage_class = nla_get_u8(
1109 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1110 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1111 }
1112
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001113 if (changed) {
1114 u8 old_retry_short, old_retry_long;
1115 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001116 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001117
1118 if (!rdev->ops->set_wiphy_params) {
1119 result = -EOPNOTSUPP;
1120 goto bad_res;
1121 }
1122
1123 old_retry_short = rdev->wiphy.retry_short;
1124 old_retry_long = rdev->wiphy.retry_long;
1125 old_frag_threshold = rdev->wiphy.frag_threshold;
1126 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001127 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001128
1129 if (changed & WIPHY_PARAM_RETRY_SHORT)
1130 rdev->wiphy.retry_short = retry_short;
1131 if (changed & WIPHY_PARAM_RETRY_LONG)
1132 rdev->wiphy.retry_long = retry_long;
1133 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1134 rdev->wiphy.frag_threshold = frag_threshold;
1135 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1136 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001137 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1138 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001139
1140 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1141 if (result) {
1142 rdev->wiphy.retry_short = old_retry_short;
1143 rdev->wiphy.retry_long = old_retry_long;
1144 rdev->wiphy.frag_threshold = old_frag_threshold;
1145 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001146 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001147 }
1148 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001149
Johannes Berg306d6112008-12-08 12:39:04 +01001150 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001151 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001152 if (netdev)
1153 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001154 return result;
1155}
1156
1157
1158static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001159 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001160 struct net_device *dev)
1161{
1162 void *hdr;
1163
1164 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1165 if (!hdr)
1166 return -1;
1167
1168 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001169 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001170 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001171 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001172
1173 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1174 rdev->devlist_generation ^
1175 (cfg80211_rdev_list_generation << 2));
1176
Johannes Berg55682962007-09-20 13:09:35 -04001177 return genlmsg_end(msg, hdr);
1178
1179 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001180 genlmsg_cancel(msg, hdr);
1181 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001182}
1183
1184static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1185{
1186 int wp_idx = 0;
1187 int if_idx = 0;
1188 int wp_start = cb->args[0];
1189 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001190 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001191 struct wireless_dev *wdev;
1192
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001193 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001194 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1195 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001196 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001197 if (wp_idx < wp_start) {
1198 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001199 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001200 }
Johannes Berg55682962007-09-20 13:09:35 -04001201 if_idx = 0;
1202
Johannes Bergf5ea9122009-08-07 16:17:38 +02001203 mutex_lock(&rdev->devlist_mtx);
1204 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001205 if (if_idx < if_start) {
1206 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001207 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001208 }
Johannes Berg55682962007-09-20 13:09:35 -04001209 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1210 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001211 rdev, wdev->netdev) < 0) {
1212 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001213 goto out;
1214 }
1215 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001216 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001217 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001218
1219 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001220 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001221 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001222 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001223
1224 cb->args[0] = wp_idx;
1225 cb->args[1] = if_idx;
1226
1227 return skb->len;
1228}
1229
1230static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1231{
1232 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001233 struct cfg80211_registered_device *dev = info->user_ptr[0];
1234 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001235
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001236 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001237 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001238 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001239
Johannes Bergd7264052009-04-19 16:23:20 +02001240 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001241 dev, netdev) < 0) {
1242 nlmsg_free(msg);
1243 return -ENOBUFS;
1244 }
Johannes Berg55682962007-09-20 13:09:35 -04001245
Johannes Berg134e6372009-07-10 09:51:34 +00001246 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001247}
1248
Michael Wu66f7ac52008-01-31 19:48:22 +01001249static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1250 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1251 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1252 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1253 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1254 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1255};
1256
1257static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1258{
1259 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1260 int flag;
1261
1262 *mntrflags = 0;
1263
1264 if (!nla)
1265 return -EINVAL;
1266
1267 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1268 nla, mntr_flags_policy))
1269 return -EINVAL;
1270
1271 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1272 if (flags[flag])
1273 *mntrflags |= (1<<flag);
1274
1275 return 0;
1276}
1277
Johannes Berg9bc383d2009-11-19 11:55:19 +01001278static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001279 struct net_device *netdev, u8 use_4addr,
1280 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001281{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001282 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001283 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001284 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001285 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001286 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001287
1288 switch (iftype) {
1289 case NL80211_IFTYPE_AP_VLAN:
1290 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1291 return 0;
1292 break;
1293 case NL80211_IFTYPE_STATION:
1294 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1295 return 0;
1296 break;
1297 default:
1298 break;
1299 }
1300
1301 return -EOPNOTSUPP;
1302}
1303
Johannes Berg55682962007-09-20 13:09:35 -04001304static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1305{
Johannes Berg4c476992010-10-04 21:36:35 +02001306 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001307 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001308 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001309 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001310 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001311 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001312 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001313
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001314 memset(&params, 0, sizeof(params));
1315
Johannes Berg04a773a2009-04-19 21:24:32 +02001316 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001317
Johannes Berg723b0382008-09-16 20:22:09 +02001318 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001319 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001320 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001321 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001322 if (ntype > NL80211_IFTYPE_MAX)
1323 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001324 }
1325
Johannes Berg92ffe052008-09-16 20:39:36 +02001326 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001327 if (ntype != NL80211_IFTYPE_MESH_POINT)
1328 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001329 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1330 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001331 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001332 }
1333
Felix Fietkau8b787642009-11-10 18:53:10 +01001334 if (info->attrs[NL80211_ATTR_4ADDR]) {
1335 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1336 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001337 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001338 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001339 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001340 } else {
1341 params.use_4addr = -1;
1342 }
1343
Johannes Berg92ffe052008-09-16 20:39:36 +02001344 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001345 if (ntype != NL80211_IFTYPE_MONITOR)
1346 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001347 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1348 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001349 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001350 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001351
1352 flags = &_flags;
1353 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001354 }
Johannes Berg3b858752009-03-12 09:55:09 +01001355
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001356 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001357 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001358 else
1359 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001360
Johannes Berg9bc383d2009-11-19 11:55:19 +01001361 if (!err && params.use_4addr != -1)
1362 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1363
Johannes Berg55682962007-09-20 13:09:35 -04001364 return err;
1365}
1366
1367static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1368{
Johannes Berg4c476992010-10-04 21:36:35 +02001369 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001370 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001371 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001372 int err;
1373 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001374 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001375
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001376 memset(&params, 0, sizeof(params));
1377
Johannes Berg55682962007-09-20 13:09:35 -04001378 if (!info->attrs[NL80211_ATTR_IFNAME])
1379 return -EINVAL;
1380
1381 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1382 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1383 if (type > NL80211_IFTYPE_MAX)
1384 return -EINVAL;
1385 }
1386
Johannes Berg79c97e92009-07-07 03:56:12 +02001387 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001388 !(rdev->wiphy.interface_modes & (1 << type)))
1389 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001390
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001391 if (type == NL80211_IFTYPE_MESH_POINT &&
1392 info->attrs[NL80211_ATTR_MESH_ID]) {
1393 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1394 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1395 }
1396
Johannes Berg9bc383d2009-11-19 11:55:19 +01001397 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001398 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001399 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001400 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001401 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001402 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001403
Michael Wu66f7ac52008-01-31 19:48:22 +01001404 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1405 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1406 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001407 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001408 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001409 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001410 if (IS_ERR(dev))
1411 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001412
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001413 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001414}
1415
1416static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1417{
Johannes Berg4c476992010-10-04 21:36:35 +02001418 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1419 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001420
Johannes Berg4c476992010-10-04 21:36:35 +02001421 if (!rdev->ops->del_virtual_intf)
1422 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001423
Johannes Berg4c476992010-10-04 21:36:35 +02001424 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001425}
1426
Johannes Berg41ade002007-12-19 02:03:29 +01001427struct get_key_cookie {
1428 struct sk_buff *msg;
1429 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001430 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001431};
1432
1433static void get_key_callback(void *c, struct key_params *params)
1434{
Johannes Bergb9454e82009-07-08 13:29:08 +02001435 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001436 struct get_key_cookie *cookie = c;
1437
1438 if (params->key)
1439 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1440 params->key_len, params->key);
1441
1442 if (params->seq)
1443 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1444 params->seq_len, params->seq);
1445
1446 if (params->cipher)
1447 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1448 params->cipher);
1449
Johannes Bergb9454e82009-07-08 13:29:08 +02001450 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1451 if (!key)
1452 goto nla_put_failure;
1453
1454 if (params->key)
1455 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1456 params->key_len, params->key);
1457
1458 if (params->seq)
1459 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1460 params->seq_len, params->seq);
1461
1462 if (params->cipher)
1463 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1464 params->cipher);
1465
1466 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1467
1468 nla_nest_end(cookie->msg, key);
1469
Johannes Berg41ade002007-12-19 02:03:29 +01001470 return;
1471 nla_put_failure:
1472 cookie->error = 1;
1473}
1474
1475static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1476{
Johannes Berg4c476992010-10-04 21:36:35 +02001477 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001478 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001479 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001480 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001481 const u8 *mac_addr = NULL;
1482 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001483 struct get_key_cookie cookie = {
1484 .error = 0,
1485 };
1486 void *hdr;
1487 struct sk_buff *msg;
1488
1489 if (info->attrs[NL80211_ATTR_KEY_IDX])
1490 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1491
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001492 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001493 return -EINVAL;
1494
1495 if (info->attrs[NL80211_ATTR_MAC])
1496 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1497
Johannes Berge31b8212010-10-05 19:39:30 +02001498 pairwise = !!mac_addr;
1499 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1500 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1501 if (kt >= NUM_NL80211_KEYTYPES)
1502 return -EINVAL;
1503 if (kt != NL80211_KEYTYPE_GROUP &&
1504 kt != NL80211_KEYTYPE_PAIRWISE)
1505 return -EINVAL;
1506 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1507 }
1508
Johannes Berg4c476992010-10-04 21:36:35 +02001509 if (!rdev->ops->get_key)
1510 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001511
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001512 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001513 if (!msg)
1514 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001515
1516 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1517 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001518 if (IS_ERR(hdr))
1519 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001520
1521 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001522 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001523
1524 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1525 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1526 if (mac_addr)
1527 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1528
Johannes Berge31b8212010-10-05 19:39:30 +02001529 if (pairwise && mac_addr &&
1530 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1531 return -ENOENT;
1532
1533 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1534 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001535
1536 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001537 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001538
1539 if (cookie.error)
1540 goto nla_put_failure;
1541
1542 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001543 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001544
1545 nla_put_failure:
1546 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001547 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001548 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001549 return err;
1550}
1551
1552static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1553{
Johannes Berg4c476992010-10-04 21:36:35 +02001554 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001555 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001556 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001557 struct net_device *dev = info->user_ptr[1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001558 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1559 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001560
Johannes Bergb9454e82009-07-08 13:29:08 +02001561 err = nl80211_parse_key(info, &key);
1562 if (err)
1563 return err;
1564
1565 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001566 return -EINVAL;
1567
Johannes Bergb9454e82009-07-08 13:29:08 +02001568 /* only support setting default key */
1569 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001570 return -EINVAL;
1571
Johannes Bergb9454e82009-07-08 13:29:08 +02001572 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001573 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001574 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001575 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001576
Johannes Berg4c476992010-10-04 21:36:35 +02001577 if (!func)
1578 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001579
Johannes Bergfffd0932009-07-08 14:22:54 +02001580 wdev_lock(dev->ieee80211_ptr);
1581 err = nl80211_key_allowed(dev->ieee80211_ptr);
1582 if (!err)
1583 err = func(&rdev->wiphy, dev, key.idx);
1584
Johannes Berg3d23e342009-09-29 23:27:28 +02001585#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001586 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001587 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001588 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001589 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001590 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001591 }
1592#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001593 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001594
Johannes Berg41ade002007-12-19 02:03:29 +01001595 return err;
1596}
1597
1598static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1599{
Johannes Berg4c476992010-10-04 21:36:35 +02001600 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001601 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001602 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001603 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001604 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001605
Johannes Bergb9454e82009-07-08 13:29:08 +02001606 err = nl80211_parse_key(info, &key);
1607 if (err)
1608 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001609
Johannes Bergb9454e82009-07-08 13:29:08 +02001610 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001611 return -EINVAL;
1612
Johannes Berg41ade002007-12-19 02:03:29 +01001613 if (info->attrs[NL80211_ATTR_MAC])
1614 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1615
Johannes Berge31b8212010-10-05 19:39:30 +02001616 if (key.type == -1) {
1617 if (mac_addr)
1618 key.type = NL80211_KEYTYPE_PAIRWISE;
1619 else
1620 key.type = NL80211_KEYTYPE_GROUP;
1621 }
1622
1623 /* for now */
1624 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1625 key.type != NL80211_KEYTYPE_GROUP)
1626 return -EINVAL;
1627
Johannes Berg4c476992010-10-04 21:36:35 +02001628 if (!rdev->ops->add_key)
1629 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001630
Johannes Berge31b8212010-10-05 19:39:30 +02001631 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1632 key.type == NL80211_KEYTYPE_PAIRWISE,
1633 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001634 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001635
1636 wdev_lock(dev->ieee80211_ptr);
1637 err = nl80211_key_allowed(dev->ieee80211_ptr);
1638 if (!err)
1639 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001640 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001641 mac_addr, &key.p);
1642 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001643
Johannes Berg41ade002007-12-19 02:03:29 +01001644 return err;
1645}
1646
1647static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1648{
Johannes Berg4c476992010-10-04 21:36:35 +02001649 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001650 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001651 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001652 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001653 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001654
Johannes Bergb9454e82009-07-08 13:29:08 +02001655 err = nl80211_parse_key(info, &key);
1656 if (err)
1657 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001658
1659 if (info->attrs[NL80211_ATTR_MAC])
1660 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1661
Johannes Berge31b8212010-10-05 19:39:30 +02001662 if (key.type == -1) {
1663 if (mac_addr)
1664 key.type = NL80211_KEYTYPE_PAIRWISE;
1665 else
1666 key.type = NL80211_KEYTYPE_GROUP;
1667 }
1668
1669 /* for now */
1670 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1671 key.type != NL80211_KEYTYPE_GROUP)
1672 return -EINVAL;
1673
Johannes Berg4c476992010-10-04 21:36:35 +02001674 if (!rdev->ops->del_key)
1675 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001676
Johannes Bergfffd0932009-07-08 14:22:54 +02001677 wdev_lock(dev->ieee80211_ptr);
1678 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001679
1680 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1681 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1682 err = -ENOENT;
1683
Johannes Bergfffd0932009-07-08 14:22:54 +02001684 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001685 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1686 key.type == NL80211_KEYTYPE_PAIRWISE,
1687 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001688
Johannes Berg3d23e342009-09-29 23:27:28 +02001689#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001690 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001691 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001692 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001693 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001694 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1695 }
1696#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001697 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001698
Johannes Berg41ade002007-12-19 02:03:29 +01001699 return err;
1700}
1701
Johannes Berged1b6cc2007-12-19 02:03:32 +01001702static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1703{
1704 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1705 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001706 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1707 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001708 struct beacon_parameters params;
1709 int haveinfo = 0;
1710
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001711 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1712 return -EINVAL;
1713
Johannes Berg074ac8d2010-09-16 14:58:22 +02001714 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001715 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1716 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001717
Johannes Berged1b6cc2007-12-19 02:03:32 +01001718 switch (info->genlhdr->cmd) {
1719 case NL80211_CMD_NEW_BEACON:
1720 /* these are required for NEW_BEACON */
1721 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1722 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001723 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1724 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001725
Johannes Berg79c97e92009-07-07 03:56:12 +02001726 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001727 break;
1728 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001729 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001730 break;
1731 default:
1732 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001733 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001734 }
1735
Johannes Berg4c476992010-10-04 21:36:35 +02001736 if (!call)
1737 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001738
1739 memset(&params, 0, sizeof(params));
1740
1741 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1742 params.interval =
1743 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1744 haveinfo = 1;
1745 }
1746
1747 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1748 params.dtim_period =
1749 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1750 haveinfo = 1;
1751 }
1752
1753 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1754 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1755 params.head_len =
1756 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1757 haveinfo = 1;
1758 }
1759
1760 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1761 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1762 params.tail_len =
1763 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1764 haveinfo = 1;
1765 }
1766
Johannes Berg4c476992010-10-04 21:36:35 +02001767 if (!haveinfo)
1768 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001769
Johannes Berg4c476992010-10-04 21:36:35 +02001770 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001771}
1772
1773static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1774{
Johannes Berg4c476992010-10-04 21:36:35 +02001775 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1776 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001777
Johannes Berg4c476992010-10-04 21:36:35 +02001778 if (!rdev->ops->del_beacon)
1779 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001780
Johannes Berg074ac8d2010-09-16 14:58:22 +02001781 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001782 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1783 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001784
Johannes Berg4c476992010-10-04 21:36:35 +02001785 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001786}
1787
Johannes Berg5727ef12007-12-19 02:03:34 +01001788static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1789 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1790 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1791 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001792 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001793};
1794
Johannes Bergeccb8e82009-05-11 21:57:56 +03001795static int parse_station_flags(struct genl_info *info,
1796 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001797{
1798 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001799 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001800 int flag;
1801
Johannes Bergeccb8e82009-05-11 21:57:56 +03001802 /*
1803 * Try parsing the new attribute first so userspace
1804 * can specify both for older kernels.
1805 */
1806 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1807 if (nla) {
1808 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001809
Johannes Bergeccb8e82009-05-11 21:57:56 +03001810 sta_flags = nla_data(nla);
1811 params->sta_flags_mask = sta_flags->mask;
1812 params->sta_flags_set = sta_flags->set;
1813 if ((params->sta_flags_mask |
1814 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1815 return -EINVAL;
1816 return 0;
1817 }
1818
1819 /* if present, parse the old attribute */
1820
1821 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001822 if (!nla)
1823 return 0;
1824
1825 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1826 nla, sta_flags_policy))
1827 return -EINVAL;
1828
Johannes Bergeccb8e82009-05-11 21:57:56 +03001829 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1830 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001831
1832 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1833 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001834 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001835
1836 return 0;
1837}
1838
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001839static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1840 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001841 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001842{
1843 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001844 struct nlattr *sinfoattr, *txrate;
1845 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001846
1847 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1848 if (!hdr)
1849 return -1;
1850
1851 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1852 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1853
Johannes Bergf5ea9122009-08-07 16:17:38 +02001854 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1855
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001856 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1857 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001858 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001859 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1860 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1861 sinfo->inactive_time);
1862 if (sinfo->filled & STATION_INFO_RX_BYTES)
1863 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1864 sinfo->rx_bytes);
1865 if (sinfo->filled & STATION_INFO_TX_BYTES)
1866 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1867 sinfo->tx_bytes);
1868 if (sinfo->filled & STATION_INFO_LLID)
1869 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1870 sinfo->llid);
1871 if (sinfo->filled & STATION_INFO_PLID)
1872 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1873 sinfo->plid);
1874 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1875 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1876 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001877 if (sinfo->filled & STATION_INFO_SIGNAL)
1878 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1879 sinfo->signal);
1880 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1881 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1882 if (!txrate)
1883 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001884
John W. Linville254416a2009-12-09 16:43:52 -05001885 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1886 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001887 if (bitrate > 0)
1888 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1889
1890 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1891 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1892 sinfo->txrate.mcs);
1893 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1894 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1895 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1896 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1897
1898 nla_nest_end(msg, txrate);
1899 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001900 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1901 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1902 sinfo->rx_packets);
1903 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1904 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1905 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09001906 if (sinfo->filled & STATION_INFO_TX_RETRIES)
1907 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
1908 sinfo->tx_retries);
1909 if (sinfo->filled & STATION_INFO_TX_FAILED)
1910 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
1911 sinfo->tx_failed);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001912 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001913
1914 return genlmsg_end(msg, hdr);
1915
1916 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001917 genlmsg_cancel(msg, hdr);
1918 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001919}
1920
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001921static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001922 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001923{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001924 struct station_info sinfo;
1925 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001926 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001927 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001928 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001929 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001930
Johannes Berg67748892010-10-04 21:14:06 +02001931 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1932 if (err)
1933 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001934
1935 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001936 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001937 goto out_err;
1938 }
1939
Johannes Bergbba95fe2008-07-29 13:22:51 +02001940 while (1) {
1941 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1942 mac_addr, &sinfo);
1943 if (err == -ENOENT)
1944 break;
1945 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001946 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001947
1948 if (nl80211_send_station(skb,
1949 NETLINK_CB(cb->skb).pid,
1950 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1951 netdev, mac_addr,
1952 &sinfo) < 0)
1953 goto out;
1954
1955 sta_idx++;
1956 }
1957
1958
1959 out:
1960 cb->args[1] = sta_idx;
1961 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001962 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02001963 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001964
1965 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001966}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001967
Johannes Berg5727ef12007-12-19 02:03:34 +01001968static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1969{
Johannes Berg4c476992010-10-04 21:36:35 +02001970 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1971 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001972 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001973 struct sk_buff *msg;
1974 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02001975 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001976
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001977 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001978
1979 if (!info->attrs[NL80211_ATTR_MAC])
1980 return -EINVAL;
1981
1982 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1983
Johannes Berg4c476992010-10-04 21:36:35 +02001984 if (!rdev->ops->get_station)
1985 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001986
Johannes Berg79c97e92009-07-07 03:56:12 +02001987 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001988 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001989 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001990
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001991 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001992 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001993 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001994
1995 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001996 dev, mac_addr, &sinfo) < 0) {
1997 nlmsg_free(msg);
1998 return -ENOBUFS;
1999 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002000
Johannes Berg4c476992010-10-04 21:36:35 +02002001 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002002}
2003
2004/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002005 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002006 */
Johannes Berg463d0182009-07-14 00:33:35 +02002007static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002008 struct cfg80211_registered_device *rdev,
2009 struct net_device **vlan)
2010{
Johannes Berg463d0182009-07-14 00:33:35 +02002011 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002012 *vlan = NULL;
2013
2014 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002015 *vlan = dev_get_by_index(genl_info_net(info),
2016 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002017 if (!*vlan)
2018 return -ENODEV;
2019 if (!(*vlan)->ieee80211_ptr)
2020 return -EINVAL;
2021 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2022 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002023 if (!netif_running(*vlan))
2024 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002025 }
2026 return 0;
2027}
2028
2029static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2030{
Johannes Berg4c476992010-10-04 21:36:35 +02002031 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002032 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002033 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002034 struct station_parameters params;
2035 u8 *mac_addr = NULL;
2036
2037 memset(&params, 0, sizeof(params));
2038
2039 params.listen_interval = -1;
2040
2041 if (info->attrs[NL80211_ATTR_STA_AID])
2042 return -EINVAL;
2043
2044 if (!info->attrs[NL80211_ATTR_MAC])
2045 return -EINVAL;
2046
2047 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2048
2049 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2050 params.supported_rates =
2051 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2052 params.supported_rates_len =
2053 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2054 }
2055
2056 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2057 params.listen_interval =
2058 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2059
Jouni Malinen36aedc902008-08-25 11:58:58 +03002060 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2061 params.ht_capa =
2062 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2063
Johannes Bergeccb8e82009-05-11 21:57:56 +03002064 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002065 return -EINVAL;
2066
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002067 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2068 params.plink_action =
2069 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2070
Johannes Berg463d0182009-07-14 00:33:35 +02002071 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002072 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002073 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002074
2075 /* validate settings */
2076 err = 0;
2077
2078 switch (dev->ieee80211_ptr->iftype) {
2079 case NL80211_IFTYPE_AP:
2080 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002081 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002082 /* disallow mesh-specific things */
2083 if (params.plink_action)
2084 err = -EINVAL;
2085 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002086 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002087 case NL80211_IFTYPE_STATION:
2088 /* disallow everything but AUTHORIZED flag */
2089 if (params.plink_action)
2090 err = -EINVAL;
2091 if (params.vlan)
2092 err = -EINVAL;
2093 if (params.supported_rates)
2094 err = -EINVAL;
2095 if (params.ht_capa)
2096 err = -EINVAL;
2097 if (params.listen_interval >= 0)
2098 err = -EINVAL;
2099 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2100 err = -EINVAL;
2101 break;
2102 case NL80211_IFTYPE_MESH_POINT:
2103 /* disallow things mesh doesn't support */
2104 if (params.vlan)
2105 err = -EINVAL;
2106 if (params.ht_capa)
2107 err = -EINVAL;
2108 if (params.listen_interval >= 0)
2109 err = -EINVAL;
2110 if (params.supported_rates)
2111 err = -EINVAL;
2112 if (params.sta_flags_mask)
2113 err = -EINVAL;
2114 break;
2115 default:
2116 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002117 }
2118
Johannes Berg5727ef12007-12-19 02:03:34 +01002119 if (err)
2120 goto out;
2121
Johannes Berg79c97e92009-07-07 03:56:12 +02002122 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002123 err = -EOPNOTSUPP;
2124 goto out;
2125 }
2126
Johannes Berg79c97e92009-07-07 03:56:12 +02002127 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002128
2129 out:
2130 if (params.vlan)
2131 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002132
Johannes Berg5727ef12007-12-19 02:03:34 +01002133 return err;
2134}
2135
2136static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2137{
Johannes Berg4c476992010-10-04 21:36:35 +02002138 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002139 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002140 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002141 struct station_parameters params;
2142 u8 *mac_addr = NULL;
2143
2144 memset(&params, 0, sizeof(params));
2145
2146 if (!info->attrs[NL80211_ATTR_MAC])
2147 return -EINVAL;
2148
Johannes Berg5727ef12007-12-19 02:03:34 +01002149 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2150 return -EINVAL;
2151
2152 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2153 return -EINVAL;
2154
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002155 if (!info->attrs[NL80211_ATTR_STA_AID])
2156 return -EINVAL;
2157
Johannes Berg5727ef12007-12-19 02:03:34 +01002158 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2159 params.supported_rates =
2160 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2161 params.supported_rates_len =
2162 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2163 params.listen_interval =
2164 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002165
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002166 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2167 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2168 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002169
Jouni Malinen36aedc902008-08-25 11:58:58 +03002170 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2171 params.ht_capa =
2172 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002173
Johannes Bergeccb8e82009-05-11 21:57:56 +03002174 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002175 return -EINVAL;
2176
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002177 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002178 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02002179 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2180 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002181
Johannes Berg463d0182009-07-14 00:33:35 +02002182 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002183 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002184 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002185
2186 /* validate settings */
2187 err = 0;
2188
Johannes Berg79c97e92009-07-07 03:56:12 +02002189 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002190 err = -EOPNOTSUPP;
2191 goto out;
2192 }
2193
Johannes Berg79c97e92009-07-07 03:56:12 +02002194 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002195
2196 out:
2197 if (params.vlan)
2198 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002199 return err;
2200}
2201
2202static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2203{
Johannes Berg4c476992010-10-04 21:36:35 +02002204 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2205 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002206 u8 *mac_addr = NULL;
2207
2208 if (info->attrs[NL80211_ATTR_MAC])
2209 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2210
Johannes Berge80cf852009-05-11 14:43:13 +02002211 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002212 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002213 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002214 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2215 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002216
Johannes Berg4c476992010-10-04 21:36:35 +02002217 if (!rdev->ops->del_station)
2218 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002219
Johannes Berg4c476992010-10-04 21:36:35 +02002220 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002221}
2222
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002223static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2224 int flags, struct net_device *dev,
2225 u8 *dst, u8 *next_hop,
2226 struct mpath_info *pinfo)
2227{
2228 void *hdr;
2229 struct nlattr *pinfoattr;
2230
2231 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2232 if (!hdr)
2233 return -1;
2234
2235 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2236 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2237 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2238
Johannes Bergf5ea9122009-08-07 16:17:38 +02002239 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2240
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002241 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2242 if (!pinfoattr)
2243 goto nla_put_failure;
2244 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2245 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2246 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002247 if (pinfo->filled & MPATH_INFO_SN)
2248 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2249 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002250 if (pinfo->filled & MPATH_INFO_METRIC)
2251 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2252 pinfo->metric);
2253 if (pinfo->filled & MPATH_INFO_EXPTIME)
2254 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2255 pinfo->exptime);
2256 if (pinfo->filled & MPATH_INFO_FLAGS)
2257 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2258 pinfo->flags);
2259 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2260 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2261 pinfo->discovery_timeout);
2262 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2263 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2264 pinfo->discovery_retries);
2265
2266 nla_nest_end(msg, pinfoattr);
2267
2268 return genlmsg_end(msg, hdr);
2269
2270 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002271 genlmsg_cancel(msg, hdr);
2272 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002273}
2274
2275static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002276 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002277{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002278 struct mpath_info pinfo;
2279 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002280 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002281 u8 dst[ETH_ALEN];
2282 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002283 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002284 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002285
Johannes Berg67748892010-10-04 21:14:06 +02002286 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2287 if (err)
2288 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002289
2290 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002291 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002292 goto out_err;
2293 }
2294
Jouni Malineneec60b02009-03-20 21:21:19 +02002295 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2296 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002297 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002298 }
2299
Johannes Bergbba95fe2008-07-29 13:22:51 +02002300 while (1) {
2301 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2302 dst, next_hop, &pinfo);
2303 if (err == -ENOENT)
2304 break;
2305 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002306 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002307
2308 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2309 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2310 netdev, dst, next_hop,
2311 &pinfo) < 0)
2312 goto out;
2313
2314 path_idx++;
2315 }
2316
2317
2318 out:
2319 cb->args[1] = path_idx;
2320 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002321 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002322 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002323 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002324}
2325
2326static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2327{
Johannes Berg4c476992010-10-04 21:36:35 +02002328 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002329 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002330 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002331 struct mpath_info pinfo;
2332 struct sk_buff *msg;
2333 u8 *dst = NULL;
2334 u8 next_hop[ETH_ALEN];
2335
2336 memset(&pinfo, 0, sizeof(pinfo));
2337
2338 if (!info->attrs[NL80211_ATTR_MAC])
2339 return -EINVAL;
2340
2341 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2342
Johannes Berg4c476992010-10-04 21:36:35 +02002343 if (!rdev->ops->get_mpath)
2344 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002345
Johannes Berg4c476992010-10-04 21:36:35 +02002346 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2347 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002348
Johannes Berg79c97e92009-07-07 03:56:12 +02002349 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002350 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002351 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002352
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002353 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002354 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002355 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002356
2357 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002358 dev, dst, next_hop, &pinfo) < 0) {
2359 nlmsg_free(msg);
2360 return -ENOBUFS;
2361 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002362
Johannes Berg4c476992010-10-04 21:36:35 +02002363 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002364}
2365
2366static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2367{
Johannes Berg4c476992010-10-04 21:36:35 +02002368 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2369 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002370 u8 *dst = NULL;
2371 u8 *next_hop = NULL;
2372
2373 if (!info->attrs[NL80211_ATTR_MAC])
2374 return -EINVAL;
2375
2376 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2377 return -EINVAL;
2378
2379 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2380 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2381
Johannes Berg4c476992010-10-04 21:36:35 +02002382 if (!rdev->ops->change_mpath)
2383 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002384
Johannes Berg4c476992010-10-04 21:36:35 +02002385 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2386 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002387
Johannes Berg4c476992010-10-04 21:36:35 +02002388 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002389}
Johannes Berg4c476992010-10-04 21:36:35 +02002390
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002391static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2392{
Johannes Berg4c476992010-10-04 21:36:35 +02002393 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2394 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002395 u8 *dst = NULL;
2396 u8 *next_hop = NULL;
2397
2398 if (!info->attrs[NL80211_ATTR_MAC])
2399 return -EINVAL;
2400
2401 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2402 return -EINVAL;
2403
2404 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2405 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2406
Johannes Berg4c476992010-10-04 21:36:35 +02002407 if (!rdev->ops->add_mpath)
2408 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002409
Johannes Berg4c476992010-10-04 21:36:35 +02002410 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2411 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002412
Johannes Berg4c476992010-10-04 21:36:35 +02002413 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002414}
2415
2416static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2417{
Johannes Berg4c476992010-10-04 21:36:35 +02002418 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2419 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002420 u8 *dst = NULL;
2421
2422 if (info->attrs[NL80211_ATTR_MAC])
2423 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2424
Johannes Berg4c476992010-10-04 21:36:35 +02002425 if (!rdev->ops->del_mpath)
2426 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002427
Johannes Berg4c476992010-10-04 21:36:35 +02002428 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002429}
2430
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002431static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2432{
Johannes Berg4c476992010-10-04 21:36:35 +02002433 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2434 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002435 struct bss_parameters params;
2436
2437 memset(&params, 0, sizeof(params));
2438 /* default to not changing parameters */
2439 params.use_cts_prot = -1;
2440 params.use_short_preamble = -1;
2441 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002442 params.ap_isolate = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002443
2444 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2445 params.use_cts_prot =
2446 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2447 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2448 params.use_short_preamble =
2449 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2450 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2451 params.use_short_slot_time =
2452 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002453 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2454 params.basic_rates =
2455 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2456 params.basic_rates_len =
2457 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2458 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002459 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2460 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002461
Johannes Berg4c476992010-10-04 21:36:35 +02002462 if (!rdev->ops->change_bss)
2463 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002464
Johannes Berg074ac8d2010-09-16 14:58:22 +02002465 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002466 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2467 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002468
Johannes Berg4c476992010-10-04 21:36:35 +02002469 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002470}
2471
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002472static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002473 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2474 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2475 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2476 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2477 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2478 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2479};
2480
2481static int parse_reg_rule(struct nlattr *tb[],
2482 struct ieee80211_reg_rule *reg_rule)
2483{
2484 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2485 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2486
2487 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2488 return -EINVAL;
2489 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2490 return -EINVAL;
2491 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2492 return -EINVAL;
2493 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2494 return -EINVAL;
2495 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2496 return -EINVAL;
2497
2498 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2499
2500 freq_range->start_freq_khz =
2501 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2502 freq_range->end_freq_khz =
2503 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2504 freq_range->max_bandwidth_khz =
2505 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2506
2507 power_rule->max_eirp =
2508 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2509
2510 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2511 power_rule->max_antenna_gain =
2512 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2513
2514 return 0;
2515}
2516
2517static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2518{
2519 int r;
2520 char *data = NULL;
2521
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002522 /*
2523 * You should only get this when cfg80211 hasn't yet initialized
2524 * completely when built-in to the kernel right between the time
2525 * window between nl80211_init() and regulatory_init(), if that is
2526 * even possible.
2527 */
2528 mutex_lock(&cfg80211_mutex);
2529 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002530 mutex_unlock(&cfg80211_mutex);
2531 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002532 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002533 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002534
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002535 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2536 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002537
2538 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2539
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002540 r = regulatory_hint_user(data);
2541
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002542 return r;
2543}
2544
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002545static int nl80211_get_mesh_params(struct sk_buff *skb,
2546 struct genl_info *info)
2547{
Johannes Berg4c476992010-10-04 21:36:35 +02002548 struct cfg80211_registered_device *rdev = info->user_ptr[0];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002549 struct mesh_config cur_params;
2550 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002551 struct net_device *dev = info->user_ptr[1];
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002552 void *hdr;
2553 struct nlattr *pinfoattr;
2554 struct sk_buff *msg;
2555
Johannes Berg4c476992010-10-04 21:36:35 +02002556 if (!rdev->ops->get_mesh_params)
2557 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002558
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002559 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002560 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002561 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002562 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002563
2564 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002565 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002566 if (!msg)
2567 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002568 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2569 NL80211_CMD_GET_MESH_PARAMS);
2570 if (!hdr)
2571 goto nla_put_failure;
2572 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2573 if (!pinfoattr)
2574 goto nla_put_failure;
2575 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2576 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2577 cur_params.dot11MeshRetryTimeout);
2578 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2579 cur_params.dot11MeshConfirmTimeout);
2580 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2581 cur_params.dot11MeshHoldingTimeout);
2582 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2583 cur_params.dot11MeshMaxPeerLinks);
2584 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2585 cur_params.dot11MeshMaxRetries);
2586 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2587 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01002588 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
2589 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002590 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2591 cur_params.auto_open_plinks);
2592 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2593 cur_params.dot11MeshHWMPmaxPREQretries);
2594 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2595 cur_params.path_refresh_time);
2596 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2597 cur_params.min_discovery_timeout);
2598 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2599 cur_params.dot11MeshHWMPactivePathTimeout);
2600 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2601 cur_params.dot11MeshHWMPpreqMinInterval);
2602 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2603 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002604 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2605 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002606 nla_nest_end(msg, pinfoattr);
2607 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002608 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002609
Johannes Berg3b858752009-03-12 09:55:09 +01002610 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002611 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002612 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002613 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002614}
2615
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002616static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002617 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2618 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2619 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2620 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2621 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2622 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01002623 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002624 [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
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002634static int nl80211_parse_mesh_params(struct genl_info *info,
2635 struct mesh_config *cfg,
2636 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002637{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002638 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002639 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002640
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002641#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2642do {\
2643 if (table[attr_num]) {\
2644 cfg->param = nla_fn(table[attr_num]); \
2645 mask |= (1 << (attr_num - 1)); \
2646 } \
2647} while (0);\
2648
2649
2650 if (!info->attrs[NL80211_ATTR_MESH_PARAMS])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002651 return -EINVAL;
2652 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002653 info->attrs[NL80211_ATTR_MESH_PARAMS],
2654 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002655 return -EINVAL;
2656
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002657 /* This makes sure that there aren't more than 32 mesh config
2658 * parameters (otherwise our bitfield scheme would not work.) */
2659 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2660
2661 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002662 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2663 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2664 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2665 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2666 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2667 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2668 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2669 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2670 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2671 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2672 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2673 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01002674 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
2675 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002676 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2677 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2678 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2679 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2680 nla_get_u8);
2681 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2682 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2683 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2684 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2685 nla_get_u16);
2686 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2687 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2688 nla_get_u32);
2689 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2690 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2691 nla_get_u16);
2692 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2693 dot11MeshHWMPnetDiameterTraversalTime,
2694 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2695 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002696 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2697 dot11MeshHWMPRootMode, mask,
2698 NL80211_MESHCONF_HWMP_ROOTMODE,
2699 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002700
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002701 if (mask_out)
2702 *mask_out = mask;
2703 return 0;
2704
2705#undef FILL_IN_MESH_PARAM_IF_SET
2706}
2707
2708static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2709{
2710 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2711 struct net_device *dev = info->user_ptr[1];
2712 struct mesh_config cfg;
2713 u32 mask;
2714 int err;
2715
2716 if (!rdev->ops->set_mesh_params)
2717 return -EOPNOTSUPP;
2718
2719 err = nl80211_parse_mesh_params(info, &cfg, &mask);
2720 if (err)
2721 return err;
2722
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002723 /* Apply changes */
Johannes Berg4c476992010-10-04 21:36:35 +02002724 return rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002725}
2726
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002727static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2728{
2729 struct sk_buff *msg;
2730 void *hdr = NULL;
2731 struct nlattr *nl_reg_rules;
2732 unsigned int i;
2733 int err = -EINVAL;
2734
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002735 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002736
2737 if (!cfg80211_regdomain)
2738 goto out;
2739
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002740 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002741 if (!msg) {
2742 err = -ENOBUFS;
2743 goto out;
2744 }
2745
2746 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2747 NL80211_CMD_GET_REG);
2748 if (!hdr)
2749 goto nla_put_failure;
2750
2751 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2752 cfg80211_regdomain->alpha2);
2753
2754 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2755 if (!nl_reg_rules)
2756 goto nla_put_failure;
2757
2758 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2759 struct nlattr *nl_reg_rule;
2760 const struct ieee80211_reg_rule *reg_rule;
2761 const struct ieee80211_freq_range *freq_range;
2762 const struct ieee80211_power_rule *power_rule;
2763
2764 reg_rule = &cfg80211_regdomain->reg_rules[i];
2765 freq_range = &reg_rule->freq_range;
2766 power_rule = &reg_rule->power_rule;
2767
2768 nl_reg_rule = nla_nest_start(msg, i);
2769 if (!nl_reg_rule)
2770 goto nla_put_failure;
2771
2772 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2773 reg_rule->flags);
2774 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2775 freq_range->start_freq_khz);
2776 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2777 freq_range->end_freq_khz);
2778 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2779 freq_range->max_bandwidth_khz);
2780 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2781 power_rule->max_antenna_gain);
2782 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2783 power_rule->max_eirp);
2784
2785 nla_nest_end(msg, nl_reg_rule);
2786 }
2787
2788 nla_nest_end(msg, nl_reg_rules);
2789
2790 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002791 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002792 goto out;
2793
2794nla_put_failure:
2795 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002796 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002797 err = -EMSGSIZE;
2798out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002799 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002800 return err;
2801}
2802
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002803static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2804{
2805 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2806 struct nlattr *nl_reg_rule;
2807 char *alpha2 = NULL;
2808 int rem_reg_rules = 0, r = 0;
2809 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2810 struct ieee80211_regdomain *rd = NULL;
2811
2812 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2813 return -EINVAL;
2814
2815 if (!info->attrs[NL80211_ATTR_REG_RULES])
2816 return -EINVAL;
2817
2818 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2819
2820 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2821 rem_reg_rules) {
2822 num_rules++;
2823 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002824 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002825 }
2826
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002827 mutex_lock(&cfg80211_mutex);
2828
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002829 if (!reg_is_valid_request(alpha2)) {
2830 r = -EINVAL;
2831 goto bad_reg;
2832 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002833
2834 size_of_regd = sizeof(struct ieee80211_regdomain) +
2835 (num_rules * sizeof(struct ieee80211_reg_rule));
2836
2837 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002838 if (!rd) {
2839 r = -ENOMEM;
2840 goto bad_reg;
2841 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002842
2843 rd->n_reg_rules = num_rules;
2844 rd->alpha2[0] = alpha2[0];
2845 rd->alpha2[1] = alpha2[1];
2846
2847 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2848 rem_reg_rules) {
2849 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2850 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2851 reg_rule_policy);
2852 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2853 if (r)
2854 goto bad_reg;
2855
2856 rule_idx++;
2857
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002858 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2859 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002860 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002861 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002862 }
2863
2864 BUG_ON(rule_idx != num_rules);
2865
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002866 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002867
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002868 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002869
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002870 return r;
2871
Johannes Bergd2372b32008-10-24 20:32:20 +02002872 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002873 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002874 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002875 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002876}
2877
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002878static int validate_scan_freqs(struct nlattr *freqs)
2879{
2880 struct nlattr *attr1, *attr2;
2881 int n_channels = 0, tmp1, tmp2;
2882
2883 nla_for_each_nested(attr1, freqs, tmp1) {
2884 n_channels++;
2885 /*
2886 * Some hardware has a limited channel list for
2887 * scanning, and it is pretty much nonsensical
2888 * to scan for a channel twice, so disallow that
2889 * and don't require drivers to check that the
2890 * channel list they get isn't longer than what
2891 * they can scan, as long as they can scan all
2892 * the channels they registered at once.
2893 */
2894 nla_for_each_nested(attr2, freqs, tmp2)
2895 if (attr1 != attr2 &&
2896 nla_get_u32(attr1) == nla_get_u32(attr2))
2897 return 0;
2898 }
2899
2900 return n_channels;
2901}
2902
Johannes Berg2a519312009-02-10 21:25:55 +01002903static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2904{
Johannes Berg4c476992010-10-04 21:36:35 +02002905 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2906 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01002907 struct cfg80211_scan_request *request;
2908 struct cfg80211_ssid *ssid;
2909 struct ieee80211_channel *channel;
2910 struct nlattr *attr;
2911 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002912 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002913 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002914 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002915
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002916 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2917 return -EINVAL;
2918
Johannes Berg79c97e92009-07-07 03:56:12 +02002919 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002920
Johannes Berg4c476992010-10-04 21:36:35 +02002921 if (!rdev->ops->scan)
2922 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01002923
Johannes Berg4c476992010-10-04 21:36:35 +02002924 if (rdev->scan_req)
2925 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01002926
2927 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002928 n_channels = validate_scan_freqs(
2929 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02002930 if (!n_channels)
2931 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002932 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002933 n_channels = 0;
2934
Johannes Berg2a519312009-02-10 21:25:55 +01002935 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2936 if (wiphy->bands[band])
2937 n_channels += wiphy->bands[band]->n_channels;
2938 }
2939
2940 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2941 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2942 n_ssids++;
2943
Johannes Berg4c476992010-10-04 21:36:35 +02002944 if (n_ssids > wiphy->max_scan_ssids)
2945 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002946
Jouni Malinen70692ad2009-02-16 19:39:13 +02002947 if (info->attrs[NL80211_ATTR_IE])
2948 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2949 else
2950 ie_len = 0;
2951
Johannes Berg4c476992010-10-04 21:36:35 +02002952 if (ie_len > wiphy->max_scan_ie_len)
2953 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02002954
Johannes Berg2a519312009-02-10 21:25:55 +01002955 request = kzalloc(sizeof(*request)
2956 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002957 + sizeof(channel) * n_channels
2958 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002959 if (!request)
2960 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01002961
Johannes Berg2a519312009-02-10 21:25:55 +01002962 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02002963 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01002964 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002965 if (ie_len) {
2966 if (request->ssids)
2967 request->ie = (void *)(request->ssids + n_ssids);
2968 else
2969 request->ie = (void *)(request->channels + n_channels);
2970 }
Johannes Berg2a519312009-02-10 21:25:55 +01002971
Johannes Berg584991d2009-11-02 13:32:03 +01002972 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01002973 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2974 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01002975 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01002976 struct ieee80211_channel *chan;
2977
2978 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2979
2980 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002981 err = -EINVAL;
2982 goto out_free;
2983 }
Johannes Berg584991d2009-11-02 13:32:03 +01002984
2985 /* ignore disabled channels */
2986 if (chan->flags & IEEE80211_CHAN_DISABLED)
2987 continue;
2988
2989 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01002990 i++;
2991 }
2992 } else {
2993 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01002994 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2995 int j;
2996 if (!wiphy->bands[band])
2997 continue;
2998 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01002999 struct ieee80211_channel *chan;
3000
3001 chan = &wiphy->bands[band]->channels[j];
3002
3003 if (chan->flags & IEEE80211_CHAN_DISABLED)
3004 continue;
3005
3006 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003007 i++;
3008 }
3009 }
3010 }
3011
Johannes Berg584991d2009-11-02 13:32:03 +01003012 if (!i) {
3013 err = -EINVAL;
3014 goto out_free;
3015 }
3016
3017 request->n_channels = i;
3018
Johannes Berg2a519312009-02-10 21:25:55 +01003019 i = 0;
3020 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3021 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3022 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3023 err = -EINVAL;
3024 goto out_free;
3025 }
3026 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3027 request->ssids[i].ssid_len = nla_len(attr);
3028 i++;
3029 }
3030 }
3031
Jouni Malinen70692ad2009-02-16 19:39:13 +02003032 if (info->attrs[NL80211_ATTR_IE]) {
3033 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003034 memcpy((void *)request->ie,
3035 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003036 request->ie_len);
3037 }
3038
Johannes Berg463d0182009-07-14 00:33:35 +02003039 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003040 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003041
Johannes Berg79c97e92009-07-07 03:56:12 +02003042 rdev->scan_req = request;
3043 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003044
Johannes Berg463d0182009-07-14 00:33:35 +02003045 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003046 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003047 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003048 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003049 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003050 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003051 kfree(request);
3052 }
Johannes Berg3b858752009-03-12 09:55:09 +01003053
Johannes Berg2a519312009-02-10 21:25:55 +01003054 return err;
3055}
3056
3057static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3058 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003059 struct wireless_dev *wdev,
3060 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003061{
Johannes Berg48ab9052009-07-10 18:42:31 +02003062 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003063 void *hdr;
3064 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003065 int i;
3066
3067 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003068
3069 hdr = nl80211hdr_put(msg, pid, seq, flags,
3070 NL80211_CMD_NEW_SCAN_RESULTS);
3071 if (!hdr)
3072 return -1;
3073
Johannes Bergf5ea9122009-08-07 16:17:38 +02003074 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003075 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003076
3077 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3078 if (!bss)
3079 goto nla_put_failure;
3080 if (!is_zero_ether_addr(res->bssid))
3081 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3082 if (res->information_elements && res->len_information_elements)
3083 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3084 res->len_information_elements,
3085 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003086 if (res->beacon_ies && res->len_beacon_ies &&
3087 res->beacon_ies != res->information_elements)
3088 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3089 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003090 if (res->tsf)
3091 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3092 if (res->beacon_interval)
3093 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3094 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3095 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003096 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3097 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003098
Johannes Berg77965c92009-02-18 18:45:06 +01003099 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003100 case CFG80211_SIGNAL_TYPE_MBM:
3101 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3102 break;
3103 case CFG80211_SIGNAL_TYPE_UNSPEC:
3104 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3105 break;
3106 default:
3107 break;
3108 }
3109
Johannes Berg48ab9052009-07-10 18:42:31 +02003110 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003111 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003112 case NL80211_IFTYPE_STATION:
3113 if (intbss == wdev->current_bss)
3114 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3115 NL80211_BSS_STATUS_ASSOCIATED);
3116 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3117 if (intbss != wdev->auth_bsses[i])
3118 continue;
3119 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3120 NL80211_BSS_STATUS_AUTHENTICATED);
3121 break;
3122 }
3123 break;
3124 case NL80211_IFTYPE_ADHOC:
3125 if (intbss == wdev->current_bss)
3126 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3127 NL80211_BSS_STATUS_IBSS_JOINED);
3128 break;
3129 default:
3130 break;
3131 }
3132
Johannes Berg2a519312009-02-10 21:25:55 +01003133 nla_nest_end(msg, bss);
3134
3135 return genlmsg_end(msg, hdr);
3136
3137 nla_put_failure:
3138 genlmsg_cancel(msg, hdr);
3139 return -EMSGSIZE;
3140}
3141
3142static int nl80211_dump_scan(struct sk_buff *skb,
3143 struct netlink_callback *cb)
3144{
Johannes Berg48ab9052009-07-10 18:42:31 +02003145 struct cfg80211_registered_device *rdev;
3146 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003147 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003148 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003149 int start = cb->args[1], idx = 0;
3150 int err;
3151
Johannes Berg67748892010-10-04 21:14:06 +02003152 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3153 if (err)
3154 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003155
Johannes Berg48ab9052009-07-10 18:42:31 +02003156 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003157
Johannes Berg48ab9052009-07-10 18:42:31 +02003158 wdev_lock(wdev);
3159 spin_lock_bh(&rdev->bss_lock);
3160 cfg80211_bss_expire(rdev);
3161
3162 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003163 if (++idx <= start)
3164 continue;
3165 if (nl80211_send_bss(skb,
3166 NETLINK_CB(cb->skb).pid,
3167 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003168 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003169 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003170 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003171 }
3172 }
3173
Johannes Berg48ab9052009-07-10 18:42:31 +02003174 spin_unlock_bh(&rdev->bss_lock);
3175 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003176
3177 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003178 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003179
Johannes Berg67748892010-10-04 21:14:06 +02003180 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003181}
3182
Holger Schurig61fa7132009-11-11 12:25:40 +01003183static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3184 int flags, struct net_device *dev,
3185 struct survey_info *survey)
3186{
3187 void *hdr;
3188 struct nlattr *infoattr;
3189
3190 /* Survey without a channel doesn't make sense */
3191 if (!survey->channel)
3192 return -EINVAL;
3193
3194 hdr = nl80211hdr_put(msg, pid, seq, flags,
3195 NL80211_CMD_NEW_SURVEY_RESULTS);
3196 if (!hdr)
3197 return -ENOMEM;
3198
3199 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3200
3201 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3202 if (!infoattr)
3203 goto nla_put_failure;
3204
3205 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3206 survey->channel->center_freq);
3207 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3208 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3209 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003210 if (survey->filled & SURVEY_INFO_IN_USE)
3211 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02003212 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3213 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3214 survey->channel_time);
3215 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3216 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3217 survey->channel_time_busy);
3218 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3219 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3220 survey->channel_time_ext_busy);
3221 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3222 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3223 survey->channel_time_rx);
3224 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3225 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3226 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003227
3228 nla_nest_end(msg, infoattr);
3229
3230 return genlmsg_end(msg, hdr);
3231
3232 nla_put_failure:
3233 genlmsg_cancel(msg, hdr);
3234 return -EMSGSIZE;
3235}
3236
3237static int nl80211_dump_survey(struct sk_buff *skb,
3238 struct netlink_callback *cb)
3239{
3240 struct survey_info survey;
3241 struct cfg80211_registered_device *dev;
3242 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003243 int survey_idx = cb->args[1];
3244 int res;
3245
Johannes Berg67748892010-10-04 21:14:06 +02003246 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3247 if (res)
3248 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003249
3250 if (!dev->ops->dump_survey) {
3251 res = -EOPNOTSUPP;
3252 goto out_err;
3253 }
3254
3255 while (1) {
3256 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3257 &survey);
3258 if (res == -ENOENT)
3259 break;
3260 if (res)
3261 goto out_err;
3262
3263 if (nl80211_send_survey(skb,
3264 NETLINK_CB(cb->skb).pid,
3265 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3266 netdev,
3267 &survey) < 0)
3268 goto out;
3269 survey_idx++;
3270 }
3271
3272 out:
3273 cb->args[1] = survey_idx;
3274 res = skb->len;
3275 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003276 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003277 return res;
3278}
3279
Jouni Malinen255e7372009-03-20 21:21:17 +02003280static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3281{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003282 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003283}
3284
Samuel Ortizb23aa672009-07-01 21:26:54 +02003285static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3286{
3287 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3288 NL80211_WPA_VERSION_2));
3289}
3290
3291static bool nl80211_valid_akm_suite(u32 akm)
3292{
3293 return akm == WLAN_AKM_SUITE_8021X ||
3294 akm == WLAN_AKM_SUITE_PSK;
3295}
3296
3297static bool nl80211_valid_cipher_suite(u32 cipher)
3298{
3299 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3300 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3301 cipher == WLAN_CIPHER_SUITE_TKIP ||
3302 cipher == WLAN_CIPHER_SUITE_CCMP ||
3303 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3304}
3305
3306
Jouni Malinen636a5d32009-03-19 13:39:22 +02003307static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3308{
Johannes Berg4c476992010-10-04 21:36:35 +02003309 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3310 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003311 struct ieee80211_channel *chan;
3312 const u8 *bssid, *ssid, *ie = NULL;
3313 int err, ssid_len, ie_len = 0;
3314 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003315 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003316 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003317
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003318 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3319 return -EINVAL;
3320
3321 if (!info->attrs[NL80211_ATTR_MAC])
3322 return -EINVAL;
3323
Jouni Malinen17780922009-03-27 20:52:47 +02003324 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3325 return -EINVAL;
3326
Johannes Berg19957bb2009-07-02 17:20:43 +02003327 if (!info->attrs[NL80211_ATTR_SSID])
3328 return -EINVAL;
3329
3330 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3331 return -EINVAL;
3332
Johannes Bergfffd0932009-07-08 14:22:54 +02003333 err = nl80211_parse_key(info, &key);
3334 if (err)
3335 return err;
3336
3337 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003338 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3339 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003340 if (!key.p.key || !key.p.key_len)
3341 return -EINVAL;
3342 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3343 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3344 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3345 key.p.key_len != WLAN_KEY_LEN_WEP104))
3346 return -EINVAL;
3347 if (key.idx > 4)
3348 return -EINVAL;
3349 } else {
3350 key.p.key_len = 0;
3351 key.p.key = NULL;
3352 }
3353
Johannes Bergafea0b72010-08-10 09:46:42 +02003354 if (key.idx >= 0) {
3355 int i;
3356 bool ok = false;
3357 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3358 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3359 ok = true;
3360 break;
3361 }
3362 }
Johannes Berg4c476992010-10-04 21:36:35 +02003363 if (!ok)
3364 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003365 }
3366
Johannes Berg4c476992010-10-04 21:36:35 +02003367 if (!rdev->ops->auth)
3368 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003369
Johannes Berg074ac8d2010-09-16 14:58:22 +02003370 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003371 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3372 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003373
Johannes Berg19957bb2009-07-02 17:20:43 +02003374 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003375 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003376 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003377 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3378 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003379
Johannes Berg19957bb2009-07-02 17:20:43 +02003380 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3381 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3382
3383 if (info->attrs[NL80211_ATTR_IE]) {
3384 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3385 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3386 }
3387
3388 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003389 if (!nl80211_valid_auth_type(auth_type))
3390 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003391
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003392 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3393
Johannes Berg4c476992010-10-04 21:36:35 +02003394 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3395 ssid, ssid_len, ie, ie_len,
3396 key.p.key, key.p.key_len, key.idx,
3397 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003398}
3399
Johannes Bergc0692b82010-08-27 14:26:53 +03003400static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3401 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003402 struct cfg80211_crypto_settings *settings,
3403 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003404{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003405 memset(settings, 0, sizeof(*settings));
3406
Samuel Ortizb23aa672009-07-01 21:26:54 +02003407 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3408
Johannes Bergc0692b82010-08-27 14:26:53 +03003409 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3410 u16 proto;
3411 proto = nla_get_u16(
3412 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3413 settings->control_port_ethertype = cpu_to_be16(proto);
3414 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3415 proto != ETH_P_PAE)
3416 return -EINVAL;
3417 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3418 settings->control_port_no_encrypt = true;
3419 } else
3420 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3421
Samuel Ortizb23aa672009-07-01 21:26:54 +02003422 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3423 void *data;
3424 int len, i;
3425
3426 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3427 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3428 settings->n_ciphers_pairwise = len / sizeof(u32);
3429
3430 if (len % sizeof(u32))
3431 return -EINVAL;
3432
Johannes Berg3dc27d22009-07-02 21:36:37 +02003433 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003434 return -EINVAL;
3435
3436 memcpy(settings->ciphers_pairwise, data, len);
3437
3438 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3439 if (!nl80211_valid_cipher_suite(
3440 settings->ciphers_pairwise[i]))
3441 return -EINVAL;
3442 }
3443
3444 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3445 settings->cipher_group =
3446 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3447 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3448 return -EINVAL;
3449 }
3450
3451 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3452 settings->wpa_versions =
3453 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3454 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3455 return -EINVAL;
3456 }
3457
3458 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3459 void *data;
3460 int len, i;
3461
3462 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3463 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3464 settings->n_akm_suites = len / sizeof(u32);
3465
3466 if (len % sizeof(u32))
3467 return -EINVAL;
3468
3469 memcpy(settings->akm_suites, data, len);
3470
3471 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3472 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3473 return -EINVAL;
3474 }
3475
3476 return 0;
3477}
3478
Jouni Malinen636a5d32009-03-19 13:39:22 +02003479static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3480{
Johannes Berg4c476992010-10-04 21:36:35 +02003481 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3482 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003483 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003484 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003485 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003486 int err, ssid_len, ie_len = 0;
3487 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003488
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003489 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3490 return -EINVAL;
3491
3492 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003493 !info->attrs[NL80211_ATTR_SSID] ||
3494 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003495 return -EINVAL;
3496
Johannes Berg4c476992010-10-04 21:36:35 +02003497 if (!rdev->ops->assoc)
3498 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003499
Johannes Berg074ac8d2010-09-16 14:58:22 +02003500 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003501 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3502 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003503
Johannes Berg19957bb2009-07-02 17:20:43 +02003504 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003505
Johannes Berg19957bb2009-07-02 17:20:43 +02003506 chan = ieee80211_get_channel(&rdev->wiphy,
3507 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003508 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3509 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003510
Johannes Berg19957bb2009-07-02 17:20:43 +02003511 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3512 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003513
3514 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003515 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3516 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003517 }
3518
Jouni Malinendc6382c2009-05-06 22:09:37 +03003519 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003520 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03003521 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003522 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003523 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02003524 else if (mfp != NL80211_MFP_NO)
3525 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03003526 }
3527
Johannes Berg3e5d7642009-07-07 14:37:26 +02003528 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3529 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3530
Johannes Bergc0692b82010-08-27 14:26:53 +03003531 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003532 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003533 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3534 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003535 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003536
Jouni Malinen636a5d32009-03-19 13:39:22 +02003537 return err;
3538}
3539
3540static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3541{
Johannes Berg4c476992010-10-04 21:36:35 +02003542 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3543 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003544 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003545 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003546 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003547 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003548
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003549 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3550 return -EINVAL;
3551
3552 if (!info->attrs[NL80211_ATTR_MAC])
3553 return -EINVAL;
3554
3555 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3556 return -EINVAL;
3557
Johannes Berg4c476992010-10-04 21:36:35 +02003558 if (!rdev->ops->deauth)
3559 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003560
Johannes Berg074ac8d2010-09-16 14:58:22 +02003561 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003562 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3563 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003564
Johannes Berg19957bb2009-07-02 17:20:43 +02003565 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003566
Johannes Berg19957bb2009-07-02 17:20:43 +02003567 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3568 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003569 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003570 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003571 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003572
3573 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003574 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3575 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003576 }
3577
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003578 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3579
Johannes Berg4c476992010-10-04 21:36:35 +02003580 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3581 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003582}
3583
3584static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3585{
Johannes Berg4c476992010-10-04 21:36:35 +02003586 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3587 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003588 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003589 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003590 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003591 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003592
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003593 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3594 return -EINVAL;
3595
3596 if (!info->attrs[NL80211_ATTR_MAC])
3597 return -EINVAL;
3598
3599 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3600 return -EINVAL;
3601
Johannes Berg4c476992010-10-04 21:36:35 +02003602 if (!rdev->ops->disassoc)
3603 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003604
Johannes Berg074ac8d2010-09-16 14:58:22 +02003605 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003606 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3607 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003608
Johannes Berg19957bb2009-07-02 17:20:43 +02003609 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003610
Johannes Berg19957bb2009-07-02 17:20:43 +02003611 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3612 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003613 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003614 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003615 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003616
3617 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003618 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3619 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003620 }
3621
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003622 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3623
Johannes Berg4c476992010-10-04 21:36:35 +02003624 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3625 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003626}
3627
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003628static bool
3629nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
3630 int mcast_rate[IEEE80211_NUM_BANDS],
3631 int rateval)
3632{
3633 struct wiphy *wiphy = &rdev->wiphy;
3634 bool found = false;
3635 int band, i;
3636
3637 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3638 struct ieee80211_supported_band *sband;
3639
3640 sband = wiphy->bands[band];
3641 if (!sband)
3642 continue;
3643
3644 for (i = 0; i < sband->n_bitrates; i++) {
3645 if (sband->bitrates[i].bitrate == rateval) {
3646 mcast_rate[band] = i + 1;
3647 found = true;
3648 break;
3649 }
3650 }
3651 }
3652
3653 return found;
3654}
3655
Johannes Berg04a773a2009-04-19 21:24:32 +02003656static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3657{
Johannes Berg4c476992010-10-04 21:36:35 +02003658 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3659 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003660 struct cfg80211_ibss_params ibss;
3661 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003662 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003663 int err;
3664
Johannes Berg8e30bc52009-04-22 17:45:38 +02003665 memset(&ibss, 0, sizeof(ibss));
3666
Johannes Berg04a773a2009-04-19 21:24:32 +02003667 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3668 return -EINVAL;
3669
3670 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3671 !info->attrs[NL80211_ATTR_SSID] ||
3672 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3673 return -EINVAL;
3674
Johannes Berg8e30bc52009-04-22 17:45:38 +02003675 ibss.beacon_interval = 100;
3676
3677 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3678 ibss.beacon_interval =
3679 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3680 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3681 return -EINVAL;
3682 }
3683
Johannes Berg4c476992010-10-04 21:36:35 +02003684 if (!rdev->ops->join_ibss)
3685 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003686
Johannes Berg4c476992010-10-04 21:36:35 +02003687 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3688 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003689
Johannes Berg79c97e92009-07-07 03:56:12 +02003690 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003691
3692 if (info->attrs[NL80211_ATTR_MAC])
3693 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3694 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3695 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3696
3697 if (info->attrs[NL80211_ATTR_IE]) {
3698 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3699 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3700 }
3701
3702 ibss.channel = ieee80211_get_channel(wiphy,
3703 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3704 if (!ibss.channel ||
3705 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02003706 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3707 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003708
3709 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003710 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003711
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003712 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3713 u8 *rates =
3714 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3715 int n_rates =
3716 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3717 struct ieee80211_supported_band *sband =
3718 wiphy->bands[ibss.channel->band];
3719 int i, j;
3720
Johannes Berg4c476992010-10-04 21:36:35 +02003721 if (n_rates == 0)
3722 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003723
3724 for (i = 0; i < n_rates; i++) {
3725 int rate = (rates[i] & 0x7f) * 5;
3726 bool found = false;
3727
3728 for (j = 0; j < sband->n_bitrates; j++) {
3729 if (sband->bitrates[j].bitrate == rate) {
3730 found = true;
3731 ibss.basic_rates |= BIT(j);
3732 break;
3733 }
3734 }
Johannes Berg4c476992010-10-04 21:36:35 +02003735 if (!found)
3736 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003737 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003738 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003739
3740 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
3741 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
3742 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
3743 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003744
Johannes Berg4c476992010-10-04 21:36:35 +02003745 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3746 connkeys = nl80211_parse_connkeys(rdev,
3747 info->attrs[NL80211_ATTR_KEYS]);
3748 if (IS_ERR(connkeys))
3749 return PTR_ERR(connkeys);
3750 }
Johannes Berg04a773a2009-04-19 21:24:32 +02003751
Johannes Berg4c476992010-10-04 21:36:35 +02003752 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003753 if (err)
3754 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003755 return err;
3756}
3757
3758static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3759{
Johannes Berg4c476992010-10-04 21:36:35 +02003760 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3761 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003762
Johannes Berg4c476992010-10-04 21:36:35 +02003763 if (!rdev->ops->leave_ibss)
3764 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003765
Johannes Berg4c476992010-10-04 21:36:35 +02003766 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3767 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003768
Johannes Berg4c476992010-10-04 21:36:35 +02003769 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003770}
3771
Johannes Bergaff89a92009-07-01 21:26:51 +02003772#ifdef CONFIG_NL80211_TESTMODE
3773static struct genl_multicast_group nl80211_testmode_mcgrp = {
3774 .name = "testmode",
3775};
3776
3777static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3778{
Johannes Berg4c476992010-10-04 21:36:35 +02003779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02003780 int err;
3781
3782 if (!info->attrs[NL80211_ATTR_TESTDATA])
3783 return -EINVAL;
3784
Johannes Bergaff89a92009-07-01 21:26:51 +02003785 err = -EOPNOTSUPP;
3786 if (rdev->ops->testmode_cmd) {
3787 rdev->testmode_info = info;
3788 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3789 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3790 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3791 rdev->testmode_info = NULL;
3792 }
3793
Johannes Bergaff89a92009-07-01 21:26:51 +02003794 return err;
3795}
3796
3797static struct sk_buff *
3798__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3799 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3800{
3801 struct sk_buff *skb;
3802 void *hdr;
3803 struct nlattr *data;
3804
3805 skb = nlmsg_new(approxlen + 100, gfp);
3806 if (!skb)
3807 return NULL;
3808
3809 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3810 if (!hdr) {
3811 kfree_skb(skb);
3812 return NULL;
3813 }
3814
3815 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3816 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3817
3818 ((void **)skb->cb)[0] = rdev;
3819 ((void **)skb->cb)[1] = hdr;
3820 ((void **)skb->cb)[2] = data;
3821
3822 return skb;
3823
3824 nla_put_failure:
3825 kfree_skb(skb);
3826 return NULL;
3827}
3828
3829struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3830 int approxlen)
3831{
3832 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3833
3834 if (WARN_ON(!rdev->testmode_info))
3835 return NULL;
3836
3837 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3838 rdev->testmode_info->snd_pid,
3839 rdev->testmode_info->snd_seq,
3840 GFP_KERNEL);
3841}
3842EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3843
3844int cfg80211_testmode_reply(struct sk_buff *skb)
3845{
3846 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3847 void *hdr = ((void **)skb->cb)[1];
3848 struct nlattr *data = ((void **)skb->cb)[2];
3849
3850 if (WARN_ON(!rdev->testmode_info)) {
3851 kfree_skb(skb);
3852 return -EINVAL;
3853 }
3854
3855 nla_nest_end(skb, data);
3856 genlmsg_end(skb, hdr);
3857 return genlmsg_reply(skb, rdev->testmode_info);
3858}
3859EXPORT_SYMBOL(cfg80211_testmode_reply);
3860
3861struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3862 int approxlen, gfp_t gfp)
3863{
3864 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3865
3866 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3867}
3868EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3869
3870void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3871{
3872 void *hdr = ((void **)skb->cb)[1];
3873 struct nlattr *data = ((void **)skb->cb)[2];
3874
3875 nla_nest_end(skb, data);
3876 genlmsg_end(skb, hdr);
3877 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3878}
3879EXPORT_SYMBOL(cfg80211_testmode_event);
3880#endif
3881
Samuel Ortizb23aa672009-07-01 21:26:54 +02003882static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3883{
Johannes Berg4c476992010-10-04 21:36:35 +02003884 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3885 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003886 struct cfg80211_connect_params connect;
3887 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003888 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003889 int err;
3890
3891 memset(&connect, 0, sizeof(connect));
3892
3893 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3894 return -EINVAL;
3895
3896 if (!info->attrs[NL80211_ATTR_SSID] ||
3897 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3898 return -EINVAL;
3899
3900 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3901 connect.auth_type =
3902 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3903 if (!nl80211_valid_auth_type(connect.auth_type))
3904 return -EINVAL;
3905 } else
3906 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3907
3908 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3909
Johannes Bergc0692b82010-08-27 14:26:53 +03003910 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003911 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003912 if (err)
3913 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003914
Johannes Berg074ac8d2010-09-16 14:58:22 +02003915 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003916 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3917 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003918
Johannes Berg79c97e92009-07-07 03:56:12 +02003919 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003920
Samuel Ortizb23aa672009-07-01 21:26:54 +02003921 if (info->attrs[NL80211_ATTR_MAC])
3922 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3923 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3924 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3925
3926 if (info->attrs[NL80211_ATTR_IE]) {
3927 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3928 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3929 }
3930
3931 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3932 connect.channel =
3933 ieee80211_get_channel(wiphy,
3934 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3935 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02003936 connect.channel->flags & IEEE80211_CHAN_DISABLED)
3937 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003938 }
3939
Johannes Bergfffd0932009-07-08 14:22:54 +02003940 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3941 connkeys = nl80211_parse_connkeys(rdev,
3942 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02003943 if (IS_ERR(connkeys))
3944 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003945 }
3946
3947 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003948 if (err)
3949 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003950 return err;
3951}
3952
3953static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3954{
Johannes Berg4c476992010-10-04 21:36:35 +02003955 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3956 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003957 u16 reason;
3958
3959 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3960 reason = WLAN_REASON_DEAUTH_LEAVING;
3961 else
3962 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3963
3964 if (reason == 0)
3965 return -EINVAL;
3966
Johannes Berg074ac8d2010-09-16 14:58:22 +02003967 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003968 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3969 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003970
Johannes Berg4c476992010-10-04 21:36:35 +02003971 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003972}
3973
Johannes Berg463d0182009-07-14 00:33:35 +02003974static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
3975{
Johannes Berg4c476992010-10-04 21:36:35 +02003976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02003977 struct net *net;
3978 int err;
3979 u32 pid;
3980
3981 if (!info->attrs[NL80211_ATTR_PID])
3982 return -EINVAL;
3983
3984 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
3985
Johannes Berg463d0182009-07-14 00:33:35 +02003986 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02003987 if (IS_ERR(net))
3988 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02003989
3990 err = 0;
3991
3992 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02003993 if (!net_eq(wiphy_net(&rdev->wiphy), net))
3994 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02003995
Johannes Berg463d0182009-07-14 00:33:35 +02003996 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02003997 return err;
3998}
3999
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004000static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4001{
Johannes Berg4c476992010-10-04 21:36:35 +02004002 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004003 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4004 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004005 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004006 struct cfg80211_pmksa pmksa;
4007
4008 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4009
4010 if (!info->attrs[NL80211_ATTR_MAC])
4011 return -EINVAL;
4012
4013 if (!info->attrs[NL80211_ATTR_PMKID])
4014 return -EINVAL;
4015
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004016 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4017 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4018
Johannes Berg074ac8d2010-09-16 14:58:22 +02004019 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004020 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4021 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004022
4023 switch (info->genlhdr->cmd) {
4024 case NL80211_CMD_SET_PMKSA:
4025 rdev_ops = rdev->ops->set_pmksa;
4026 break;
4027 case NL80211_CMD_DEL_PMKSA:
4028 rdev_ops = rdev->ops->del_pmksa;
4029 break;
4030 default:
4031 WARN_ON(1);
4032 break;
4033 }
4034
Johannes Berg4c476992010-10-04 21:36:35 +02004035 if (!rdev_ops)
4036 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004037
Johannes Berg4c476992010-10-04 21:36:35 +02004038 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004039}
4040
4041static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4042{
Johannes Berg4c476992010-10-04 21:36:35 +02004043 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4044 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004045
Johannes Berg074ac8d2010-09-16 14:58:22 +02004046 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004047 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4048 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004049
Johannes Berg4c476992010-10-04 21:36:35 +02004050 if (!rdev->ops->flush_pmksa)
4051 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004052
Johannes Berg4c476992010-10-04 21:36:35 +02004053 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004054}
4055
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004056static int nl80211_remain_on_channel(struct sk_buff *skb,
4057 struct genl_info *info)
4058{
Johannes Berg4c476992010-10-04 21:36:35 +02004059 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4060 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004061 struct ieee80211_channel *chan;
4062 struct sk_buff *msg;
4063 void *hdr;
4064 u64 cookie;
4065 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4066 u32 freq, duration;
4067 int err;
4068
4069 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4070 !info->attrs[NL80211_ATTR_DURATION])
4071 return -EINVAL;
4072
4073 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4074
4075 /*
4076 * We should be on that channel for at least one jiffie,
4077 * and more than 5 seconds seems excessive.
4078 */
4079 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4080 return -EINVAL;
4081
Johannes Berg4c476992010-10-04 21:36:35 +02004082 if (!rdev->ops->remain_on_channel)
4083 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004084
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004085 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4086 channel_type = nla_get_u32(
4087 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4088 if (channel_type != NL80211_CHAN_NO_HT &&
4089 channel_type != NL80211_CHAN_HT20 &&
4090 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004091 channel_type != NL80211_CHAN_HT40MINUS)
4092 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004093 }
4094
4095 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4096 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004097 if (chan == NULL)
4098 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004099
4100 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004101 if (!msg)
4102 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004103
4104 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4105 NL80211_CMD_REMAIN_ON_CHANNEL);
4106
4107 if (IS_ERR(hdr)) {
4108 err = PTR_ERR(hdr);
4109 goto free_msg;
4110 }
4111
4112 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4113 channel_type, duration, &cookie);
4114
4115 if (err)
4116 goto free_msg;
4117
4118 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4119
4120 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004121
4122 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004123
4124 nla_put_failure:
4125 err = -ENOBUFS;
4126 free_msg:
4127 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004128 return err;
4129}
4130
4131static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4132 struct genl_info *info)
4133{
Johannes Berg4c476992010-10-04 21:36:35 +02004134 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4135 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004136 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004137
4138 if (!info->attrs[NL80211_ATTR_COOKIE])
4139 return -EINVAL;
4140
Johannes Berg4c476992010-10-04 21:36:35 +02004141 if (!rdev->ops->cancel_remain_on_channel)
4142 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004143
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004144 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4145
Johannes Berg4c476992010-10-04 21:36:35 +02004146 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004147}
4148
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004149static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4150 u8 *rates, u8 rates_len)
4151{
4152 u8 i;
4153 u32 mask = 0;
4154
4155 for (i = 0; i < rates_len; i++) {
4156 int rate = (rates[i] & 0x7f) * 5;
4157 int ridx;
4158 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4159 struct ieee80211_rate *srate =
4160 &sband->bitrates[ridx];
4161 if (rate == srate->bitrate) {
4162 mask |= 1 << ridx;
4163 break;
4164 }
4165 }
4166 if (ridx == sband->n_bitrates)
4167 return 0; /* rate not found */
4168 }
4169
4170 return mask;
4171}
4172
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004173static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004174 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4175 .len = NL80211_MAX_SUPP_RATES },
4176};
4177
4178static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4179 struct genl_info *info)
4180{
4181 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004183 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004184 int rem, i;
4185 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004186 struct nlattr *tx_rates;
4187 struct ieee80211_supported_band *sband;
4188
4189 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4190 return -EINVAL;
4191
Johannes Berg4c476992010-10-04 21:36:35 +02004192 if (!rdev->ops->set_bitrate_mask)
4193 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004194
4195 memset(&mask, 0, sizeof(mask));
4196 /* Default to all rates enabled */
4197 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4198 sband = rdev->wiphy.bands[i];
4199 mask.control[i].legacy =
4200 sband ? (1 << sband->n_bitrates) - 1 : 0;
4201 }
4202
4203 /*
4204 * The nested attribute uses enum nl80211_band as the index. This maps
4205 * directly to the enum ieee80211_band values used in cfg80211.
4206 */
4207 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4208 {
4209 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004210 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4211 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004212 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004213 if (sband == NULL)
4214 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004215 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4216 nla_len(tx_rates), nl80211_txattr_policy);
4217 if (tb[NL80211_TXRATE_LEGACY]) {
4218 mask.control[band].legacy = rateset_to_mask(
4219 sband,
4220 nla_data(tb[NL80211_TXRATE_LEGACY]),
4221 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004222 if (mask.control[band].legacy == 0)
4223 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004224 }
4225 }
4226
Johannes Berg4c476992010-10-04 21:36:35 +02004227 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004228}
4229
Johannes Berg2e161f72010-08-12 15:38:38 +02004230static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004231{
Johannes Berg4c476992010-10-04 21:36:35 +02004232 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4233 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004234 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004235
4236 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4237 return -EINVAL;
4238
Johannes Berg2e161f72010-08-12 15:38:38 +02004239 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4240 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004241
Johannes Berg9d38d852010-06-09 17:20:33 +02004242 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004243 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004244 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4245 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4246 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004247 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4248 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004249
4250 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004251 if (!rdev->ops->mgmt_tx)
4252 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004253
Johannes Berg4c476992010-10-04 21:36:35 +02004254 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004255 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004256 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4257 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004258}
4259
Johannes Berg2e161f72010-08-12 15:38:38 +02004260static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004261{
Johannes Berg4c476992010-10-04 21:36:35 +02004262 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4263 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004264 struct ieee80211_channel *chan;
4265 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004266 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004267 u32 freq;
4268 int err;
4269 void *hdr;
4270 u64 cookie;
4271 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004272 unsigned int wait = 0;
4273 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02004274
4275 if (!info->attrs[NL80211_ATTR_FRAME] ||
4276 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4277 return -EINVAL;
4278
Johannes Berg4c476992010-10-04 21:36:35 +02004279 if (!rdev->ops->mgmt_tx)
4280 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004281
Johannes Berg9d38d852010-06-09 17:20:33 +02004282 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004283 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004284 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4285 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4286 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004287 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4288 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004289
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004290 if (info->attrs[NL80211_ATTR_DURATION]) {
4291 if (!rdev->ops->mgmt_tx_cancel_wait)
4292 return -EINVAL;
4293 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4294 }
4295
Jouni Malinen026331c2010-02-15 12:53:10 +02004296 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4297 channel_type = nla_get_u32(
4298 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4299 if (channel_type != NL80211_CHAN_NO_HT &&
4300 channel_type != NL80211_CHAN_HT20 &&
4301 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004302 channel_type != NL80211_CHAN_HT40MINUS)
4303 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004304 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004305 }
4306
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004307 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
4308
Jouni Malinen026331c2010-02-15 12:53:10 +02004309 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4310 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004311 if (chan == NULL)
4312 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004313
4314 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004315 if (!msg)
4316 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004317
4318 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004319 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004320
4321 if (IS_ERR(hdr)) {
4322 err = PTR_ERR(hdr);
4323 goto free_msg;
4324 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004325 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
4326 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02004327 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4328 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4329 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004330 if (err)
4331 goto free_msg;
4332
4333 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4334
4335 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004336 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004337
4338 nla_put_failure:
4339 err = -ENOBUFS;
4340 free_msg:
4341 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004342 return err;
4343}
4344
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004345static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
4346{
4347 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4348 struct net_device *dev = info->user_ptr[1];
4349 u64 cookie;
4350
4351 if (!info->attrs[NL80211_ATTR_COOKIE])
4352 return -EINVAL;
4353
4354 if (!rdev->ops->mgmt_tx_cancel_wait)
4355 return -EOPNOTSUPP;
4356
4357 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4358 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4359 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4360 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4361 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4362 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4363 return -EOPNOTSUPP;
4364
4365 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4366
4367 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
4368}
4369
Kalle Valoffb9eb32010-02-17 17:58:10 +02004370static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4371{
Johannes Berg4c476992010-10-04 21:36:35 +02004372 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004373 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004374 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004375 u8 ps_state;
4376 bool state;
4377 int err;
4378
Johannes Berg4c476992010-10-04 21:36:35 +02004379 if (!info->attrs[NL80211_ATTR_PS_STATE])
4380 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004381
4382 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4383
Johannes Berg4c476992010-10-04 21:36:35 +02004384 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4385 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004386
4387 wdev = dev->ieee80211_ptr;
4388
Johannes Berg4c476992010-10-04 21:36:35 +02004389 if (!rdev->ops->set_power_mgmt)
4390 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004391
4392 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4393
4394 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004395 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004396
Johannes Berg4c476992010-10-04 21:36:35 +02004397 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4398 wdev->ps_timeout);
4399 if (!err)
4400 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004401 return err;
4402}
4403
4404static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4405{
Johannes Berg4c476992010-10-04 21:36:35 +02004406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004407 enum nl80211_ps_state ps_state;
4408 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004409 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004410 struct sk_buff *msg;
4411 void *hdr;
4412 int err;
4413
Kalle Valoffb9eb32010-02-17 17:58:10 +02004414 wdev = dev->ieee80211_ptr;
4415
Johannes Berg4c476992010-10-04 21:36:35 +02004416 if (!rdev->ops->set_power_mgmt)
4417 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004418
4419 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004420 if (!msg)
4421 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004422
4423 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4424 NL80211_CMD_GET_POWER_SAVE);
4425 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004426 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004427 goto free_msg;
4428 }
4429
4430 if (wdev->ps)
4431 ps_state = NL80211_PS_ENABLED;
4432 else
4433 ps_state = NL80211_PS_DISABLED;
4434
4435 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4436
4437 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004438 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004439
Johannes Berg4c476992010-10-04 21:36:35 +02004440 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004441 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004442 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004443 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004444 return err;
4445}
4446
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004447static struct nla_policy
4448nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4449 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4450 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4451 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4452};
4453
4454static int nl80211_set_cqm_rssi(struct genl_info *info,
4455 s32 threshold, u32 hysteresis)
4456{
Johannes Berg4c476992010-10-04 21:36:35 +02004457 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004458 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004459 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004460
4461 if (threshold > 0)
4462 return -EINVAL;
4463
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004464 wdev = dev->ieee80211_ptr;
4465
Johannes Berg4c476992010-10-04 21:36:35 +02004466 if (!rdev->ops->set_cqm_rssi_config)
4467 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004468
Johannes Berg074ac8d2010-09-16 14:58:22 +02004469 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004470 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4471 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004472
Johannes Berg4c476992010-10-04 21:36:35 +02004473 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4474 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004475}
4476
4477static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4478{
4479 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4480 struct nlattr *cqm;
4481 int err;
4482
4483 cqm = info->attrs[NL80211_ATTR_CQM];
4484 if (!cqm) {
4485 err = -EINVAL;
4486 goto out;
4487 }
4488
4489 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4490 nl80211_attr_cqm_policy);
4491 if (err)
4492 goto out;
4493
4494 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4495 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4496 s32 threshold;
4497 u32 hysteresis;
4498 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4499 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4500 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4501 } else
4502 err = -EINVAL;
4503
4504out:
4505 return err;
4506}
4507
Johannes Berg4c476992010-10-04 21:36:35 +02004508#define NL80211_FLAG_NEED_WIPHY 0x01
4509#define NL80211_FLAG_NEED_NETDEV 0x02
4510#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02004511#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
4512#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
4513 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02004514
4515static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4516 struct genl_info *info)
4517{
4518 struct cfg80211_registered_device *rdev;
4519 struct net_device *dev;
4520 int err;
4521 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4522
4523 if (rtnl)
4524 rtnl_lock();
4525
4526 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4527 rdev = cfg80211_get_dev_from_info(info);
4528 if (IS_ERR(rdev)) {
4529 if (rtnl)
4530 rtnl_unlock();
4531 return PTR_ERR(rdev);
4532 }
4533 info->user_ptr[0] = rdev;
4534 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4535 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4536 if (err) {
4537 if (rtnl)
4538 rtnl_unlock();
4539 return err;
4540 }
Johannes Berg41265712010-10-04 21:14:05 +02004541 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4542 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02004543 cfg80211_unlock_rdev(rdev);
4544 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02004545 if (rtnl)
4546 rtnl_unlock();
4547 return -ENETDOWN;
4548 }
Johannes Berg4c476992010-10-04 21:36:35 +02004549 info->user_ptr[0] = rdev;
4550 info->user_ptr[1] = dev;
4551 }
4552
4553 return 0;
4554}
4555
4556static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4557 struct genl_info *info)
4558{
4559 if (info->user_ptr[0])
4560 cfg80211_unlock_rdev(info->user_ptr[0]);
4561 if (info->user_ptr[1])
4562 dev_put(info->user_ptr[1]);
4563 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4564 rtnl_unlock();
4565}
4566
Johannes Berg55682962007-09-20 13:09:35 -04004567static struct genl_ops nl80211_ops[] = {
4568 {
4569 .cmd = NL80211_CMD_GET_WIPHY,
4570 .doit = nl80211_get_wiphy,
4571 .dumpit = nl80211_dump_wiphy,
4572 .policy = nl80211_policy,
4573 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004574 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04004575 },
4576 {
4577 .cmd = NL80211_CMD_SET_WIPHY,
4578 .doit = nl80211_set_wiphy,
4579 .policy = nl80211_policy,
4580 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004581 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004582 },
4583 {
4584 .cmd = NL80211_CMD_GET_INTERFACE,
4585 .doit = nl80211_get_interface,
4586 .dumpit = nl80211_dump_interface,
4587 .policy = nl80211_policy,
4588 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004589 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04004590 },
4591 {
4592 .cmd = NL80211_CMD_SET_INTERFACE,
4593 .doit = nl80211_set_interface,
4594 .policy = nl80211_policy,
4595 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004596 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4597 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004598 },
4599 {
4600 .cmd = NL80211_CMD_NEW_INTERFACE,
4601 .doit = nl80211_new_interface,
4602 .policy = nl80211_policy,
4603 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004604 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4605 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004606 },
4607 {
4608 .cmd = NL80211_CMD_DEL_INTERFACE,
4609 .doit = nl80211_del_interface,
4610 .policy = nl80211_policy,
4611 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004612 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4613 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004614 },
Johannes Berg41ade002007-12-19 02:03:29 +01004615 {
4616 .cmd = NL80211_CMD_GET_KEY,
4617 .doit = nl80211_get_key,
4618 .policy = nl80211_policy,
4619 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004620 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4621 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004622 },
4623 {
4624 .cmd = NL80211_CMD_SET_KEY,
4625 .doit = nl80211_set_key,
4626 .policy = nl80211_policy,
4627 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004628 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004629 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004630 },
4631 {
4632 .cmd = NL80211_CMD_NEW_KEY,
4633 .doit = nl80211_new_key,
4634 .policy = nl80211_policy,
4635 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004636 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004637 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004638 },
4639 {
4640 .cmd = NL80211_CMD_DEL_KEY,
4641 .doit = nl80211_del_key,
4642 .policy = nl80211_policy,
4643 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004644 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004645 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004646 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004647 {
4648 .cmd = NL80211_CMD_SET_BEACON,
4649 .policy = nl80211_policy,
4650 .flags = GENL_ADMIN_PERM,
4651 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004652 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4653 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004654 },
4655 {
4656 .cmd = NL80211_CMD_NEW_BEACON,
4657 .policy = nl80211_policy,
4658 .flags = GENL_ADMIN_PERM,
4659 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004660 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4661 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004662 },
4663 {
4664 .cmd = NL80211_CMD_DEL_BEACON,
4665 .policy = nl80211_policy,
4666 .flags = GENL_ADMIN_PERM,
4667 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004668 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4669 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004670 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004671 {
4672 .cmd = NL80211_CMD_GET_STATION,
4673 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004674 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004675 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02004676 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4677 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004678 },
4679 {
4680 .cmd = NL80211_CMD_SET_STATION,
4681 .doit = nl80211_set_station,
4682 .policy = nl80211_policy,
4683 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004684 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4685 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004686 },
4687 {
4688 .cmd = NL80211_CMD_NEW_STATION,
4689 .doit = nl80211_new_station,
4690 .policy = nl80211_policy,
4691 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004692 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004693 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004694 },
4695 {
4696 .cmd = NL80211_CMD_DEL_STATION,
4697 .doit = nl80211_del_station,
4698 .policy = nl80211_policy,
4699 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004700 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4701 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004702 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004703 {
4704 .cmd = NL80211_CMD_GET_MPATH,
4705 .doit = nl80211_get_mpath,
4706 .dumpit = nl80211_dump_mpath,
4707 .policy = nl80211_policy,
4708 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004709 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004710 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004711 },
4712 {
4713 .cmd = NL80211_CMD_SET_MPATH,
4714 .doit = nl80211_set_mpath,
4715 .policy = nl80211_policy,
4716 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004717 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004718 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004719 },
4720 {
4721 .cmd = NL80211_CMD_NEW_MPATH,
4722 .doit = nl80211_new_mpath,
4723 .policy = nl80211_policy,
4724 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004725 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004726 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004727 },
4728 {
4729 .cmd = NL80211_CMD_DEL_MPATH,
4730 .doit = nl80211_del_mpath,
4731 .policy = nl80211_policy,
4732 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004733 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4734 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004735 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004736 {
4737 .cmd = NL80211_CMD_SET_BSS,
4738 .doit = nl80211_set_bss,
4739 .policy = nl80211_policy,
4740 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004741 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4742 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004743 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004744 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004745 .cmd = NL80211_CMD_GET_REG,
4746 .doit = nl80211_get_reg,
4747 .policy = nl80211_policy,
4748 /* can be retrieved by unprivileged users */
4749 },
4750 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004751 .cmd = NL80211_CMD_SET_REG,
4752 .doit = nl80211_set_reg,
4753 .policy = nl80211_policy,
4754 .flags = GENL_ADMIN_PERM,
4755 },
4756 {
4757 .cmd = NL80211_CMD_REQ_SET_REG,
4758 .doit = nl80211_req_set_reg,
4759 .policy = nl80211_policy,
4760 .flags = GENL_ADMIN_PERM,
4761 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004762 {
4763 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4764 .doit = nl80211_get_mesh_params,
4765 .policy = nl80211_policy,
4766 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004767 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4768 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004769 },
4770 {
4771 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4772 .doit = nl80211_set_mesh_params,
4773 .policy = nl80211_policy,
4774 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004775 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4776 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004777 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004778 {
Johannes Berg2a519312009-02-10 21:25:55 +01004779 .cmd = NL80211_CMD_TRIGGER_SCAN,
4780 .doit = nl80211_trigger_scan,
4781 .policy = nl80211_policy,
4782 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004783 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004784 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01004785 },
4786 {
4787 .cmd = NL80211_CMD_GET_SCAN,
4788 .policy = nl80211_policy,
4789 .dumpit = nl80211_dump_scan,
4790 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004791 {
4792 .cmd = NL80211_CMD_AUTHENTICATE,
4793 .doit = nl80211_authenticate,
4794 .policy = nl80211_policy,
4795 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004796 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004797 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004798 },
4799 {
4800 .cmd = NL80211_CMD_ASSOCIATE,
4801 .doit = nl80211_associate,
4802 .policy = nl80211_policy,
4803 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004804 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004805 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004806 },
4807 {
4808 .cmd = NL80211_CMD_DEAUTHENTICATE,
4809 .doit = nl80211_deauthenticate,
4810 .policy = nl80211_policy,
4811 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004812 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004813 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004814 },
4815 {
4816 .cmd = NL80211_CMD_DISASSOCIATE,
4817 .doit = nl80211_disassociate,
4818 .policy = nl80211_policy,
4819 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004820 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004821 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004822 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004823 {
4824 .cmd = NL80211_CMD_JOIN_IBSS,
4825 .doit = nl80211_join_ibss,
4826 .policy = nl80211_policy,
4827 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004828 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004829 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004830 },
4831 {
4832 .cmd = NL80211_CMD_LEAVE_IBSS,
4833 .doit = nl80211_leave_ibss,
4834 .policy = nl80211_policy,
4835 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004836 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004837 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004838 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004839#ifdef CONFIG_NL80211_TESTMODE
4840 {
4841 .cmd = NL80211_CMD_TESTMODE,
4842 .doit = nl80211_testmode_do,
4843 .policy = nl80211_policy,
4844 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004845 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4846 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02004847 },
4848#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004849 {
4850 .cmd = NL80211_CMD_CONNECT,
4851 .doit = nl80211_connect,
4852 .policy = nl80211_policy,
4853 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004854 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004855 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004856 },
4857 {
4858 .cmd = NL80211_CMD_DISCONNECT,
4859 .doit = nl80211_disconnect,
4860 .policy = nl80211_policy,
4861 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004862 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004863 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004864 },
Johannes Berg463d0182009-07-14 00:33:35 +02004865 {
4866 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4867 .doit = nl80211_wiphy_netns,
4868 .policy = nl80211_policy,
4869 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004870 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4871 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02004872 },
Holger Schurig61fa7132009-11-11 12:25:40 +01004873 {
4874 .cmd = NL80211_CMD_GET_SURVEY,
4875 .policy = nl80211_policy,
4876 .dumpit = nl80211_dump_survey,
4877 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004878 {
4879 .cmd = NL80211_CMD_SET_PMKSA,
4880 .doit = nl80211_setdel_pmksa,
4881 .policy = nl80211_policy,
4882 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004883 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4884 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004885 },
4886 {
4887 .cmd = NL80211_CMD_DEL_PMKSA,
4888 .doit = nl80211_setdel_pmksa,
4889 .policy = nl80211_policy,
4890 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004891 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4892 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004893 },
4894 {
4895 .cmd = NL80211_CMD_FLUSH_PMKSA,
4896 .doit = nl80211_flush_pmksa,
4897 .policy = nl80211_policy,
4898 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004899 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4900 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004901 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004902 {
4903 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
4904 .doit = nl80211_remain_on_channel,
4905 .policy = nl80211_policy,
4906 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004907 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004908 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004909 },
4910 {
4911 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
4912 .doit = nl80211_cancel_remain_on_channel,
4913 .policy = nl80211_policy,
4914 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004915 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004916 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004917 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004918 {
4919 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
4920 .doit = nl80211_set_tx_bitrate_mask,
4921 .policy = nl80211_policy,
4922 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004923 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4924 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004925 },
Jouni Malinen026331c2010-02-15 12:53:10 +02004926 {
Johannes Berg2e161f72010-08-12 15:38:38 +02004927 .cmd = NL80211_CMD_REGISTER_FRAME,
4928 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02004929 .policy = nl80211_policy,
4930 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004931 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4932 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02004933 },
4934 {
Johannes Berg2e161f72010-08-12 15:38:38 +02004935 .cmd = NL80211_CMD_FRAME,
4936 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02004937 .policy = nl80211_policy,
4938 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004939 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004940 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02004941 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02004942 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004943 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
4944 .doit = nl80211_tx_mgmt_cancel_wait,
4945 .policy = nl80211_policy,
4946 .flags = GENL_ADMIN_PERM,
4947 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4948 NL80211_FLAG_NEED_RTNL,
4949 },
4950 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02004951 .cmd = NL80211_CMD_SET_POWER_SAVE,
4952 .doit = nl80211_set_power_save,
4953 .policy = nl80211_policy,
4954 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004955 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4956 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02004957 },
4958 {
4959 .cmd = NL80211_CMD_GET_POWER_SAVE,
4960 .doit = nl80211_get_power_save,
4961 .policy = nl80211_policy,
4962 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004963 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4964 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02004965 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004966 {
4967 .cmd = NL80211_CMD_SET_CQM,
4968 .doit = nl80211_set_cqm,
4969 .policy = nl80211_policy,
4970 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004971 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4972 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004973 },
Johannes Bergf444de02010-05-05 15:25:02 +02004974 {
4975 .cmd = NL80211_CMD_SET_CHANNEL,
4976 .doit = nl80211_set_channel,
4977 .policy = nl80211_policy,
4978 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004979 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4980 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02004981 },
Bill Jordane8347eb2010-10-01 13:54:28 -04004982 {
4983 .cmd = NL80211_CMD_SET_WDS_PEER,
4984 .doit = nl80211_set_wds_peer,
4985 .policy = nl80211_policy,
4986 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02004987 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4988 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04004989 },
Johannes Berg55682962007-09-20 13:09:35 -04004990};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004991
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004992static struct genl_multicast_group nl80211_mlme_mcgrp = {
4993 .name = "mlme",
4994};
Johannes Berg55682962007-09-20 13:09:35 -04004995
4996/* multicast groups */
4997static struct genl_multicast_group nl80211_config_mcgrp = {
4998 .name = "config",
4999};
Johannes Berg2a519312009-02-10 21:25:55 +01005000static struct genl_multicast_group nl80211_scan_mcgrp = {
5001 .name = "scan",
5002};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005003static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5004 .name = "regulatory",
5005};
Johannes Berg55682962007-09-20 13:09:35 -04005006
5007/* notification functions */
5008
5009void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5010{
5011 struct sk_buff *msg;
5012
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005013 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005014 if (!msg)
5015 return;
5016
5017 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5018 nlmsg_free(msg);
5019 return;
5020 }
5021
Johannes Berg463d0182009-07-14 00:33:35 +02005022 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5023 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005024}
5025
Johannes Berg362a4152009-05-24 16:43:15 +02005026static int nl80211_add_scan_req(struct sk_buff *msg,
5027 struct cfg80211_registered_device *rdev)
5028{
5029 struct cfg80211_scan_request *req = rdev->scan_req;
5030 struct nlattr *nest;
5031 int i;
5032
Johannes Berg667503dd2009-07-07 03:56:11 +02005033 ASSERT_RDEV_LOCK(rdev);
5034
Johannes Berg362a4152009-05-24 16:43:15 +02005035 if (WARN_ON(!req))
5036 return 0;
5037
5038 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5039 if (!nest)
5040 goto nla_put_failure;
5041 for (i = 0; i < req->n_ssids; i++)
5042 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5043 nla_nest_end(msg, nest);
5044
5045 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5046 if (!nest)
5047 goto nla_put_failure;
5048 for (i = 0; i < req->n_channels; i++)
5049 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5050 nla_nest_end(msg, nest);
5051
5052 if (req->ie)
5053 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5054
5055 return 0;
5056 nla_put_failure:
5057 return -ENOBUFS;
5058}
5059
Johannes Berga538e2d2009-06-16 19:56:42 +02005060static int nl80211_send_scan_msg(struct sk_buff *msg,
5061 struct cfg80211_registered_device *rdev,
5062 struct net_device *netdev,
5063 u32 pid, u32 seq, int flags,
5064 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005065{
5066 void *hdr;
5067
5068 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5069 if (!hdr)
5070 return -1;
5071
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005072 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005073 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5074
Johannes Berg362a4152009-05-24 16:43:15 +02005075 /* ignore errors and send incomplete event anyway */
5076 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005077
5078 return genlmsg_end(msg, hdr);
5079
5080 nla_put_failure:
5081 genlmsg_cancel(msg, hdr);
5082 return -EMSGSIZE;
5083}
5084
Johannes Berga538e2d2009-06-16 19:56:42 +02005085void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5086 struct net_device *netdev)
5087{
5088 struct sk_buff *msg;
5089
5090 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5091 if (!msg)
5092 return;
5093
5094 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5095 NL80211_CMD_TRIGGER_SCAN) < 0) {
5096 nlmsg_free(msg);
5097 return;
5098 }
5099
Johannes Berg463d0182009-07-14 00:33:35 +02005100 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5101 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005102}
5103
Johannes Berg2a519312009-02-10 21:25:55 +01005104void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5105 struct net_device *netdev)
5106{
5107 struct sk_buff *msg;
5108
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005109 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005110 if (!msg)
5111 return;
5112
Johannes Berga538e2d2009-06-16 19:56:42 +02005113 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5114 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005115 nlmsg_free(msg);
5116 return;
5117 }
5118
Johannes Berg463d0182009-07-14 00:33:35 +02005119 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5120 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005121}
5122
5123void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5124 struct net_device *netdev)
5125{
5126 struct sk_buff *msg;
5127
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005128 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005129 if (!msg)
5130 return;
5131
Johannes Berga538e2d2009-06-16 19:56:42 +02005132 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5133 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005134 nlmsg_free(msg);
5135 return;
5136 }
5137
Johannes Berg463d0182009-07-14 00:33:35 +02005138 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5139 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005140}
5141
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005142/*
5143 * This can happen on global regulatory changes or device specific settings
5144 * based on custom world regulatory domains.
5145 */
5146void nl80211_send_reg_change_event(struct regulatory_request *request)
5147{
5148 struct sk_buff *msg;
5149 void *hdr;
5150
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005151 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005152 if (!msg)
5153 return;
5154
5155 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5156 if (!hdr) {
5157 nlmsg_free(msg);
5158 return;
5159 }
5160
5161 /* Userspace can always count this one always being set */
5162 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5163
5164 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5165 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5166 NL80211_REGDOM_TYPE_WORLD);
5167 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5168 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5169 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5170 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5171 request->intersect)
5172 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5173 NL80211_REGDOM_TYPE_INTERSECTION);
5174 else {
5175 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5176 NL80211_REGDOM_TYPE_COUNTRY);
5177 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5178 }
5179
5180 if (wiphy_idx_valid(request->wiphy_idx))
5181 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5182
5183 if (genlmsg_end(msg, hdr) < 0) {
5184 nlmsg_free(msg);
5185 return;
5186 }
5187
Johannes Bergbc43b282009-07-25 10:54:13 +02005188 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005189 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005190 GFP_ATOMIC);
5191 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005192
5193 return;
5194
5195nla_put_failure:
5196 genlmsg_cancel(msg, hdr);
5197 nlmsg_free(msg);
5198}
5199
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005200static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5201 struct net_device *netdev,
5202 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005203 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005204{
5205 struct sk_buff *msg;
5206 void *hdr;
5207
Johannes Berge6d6e342009-07-01 21:26:47 +02005208 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005209 if (!msg)
5210 return;
5211
5212 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5213 if (!hdr) {
5214 nlmsg_free(msg);
5215 return;
5216 }
5217
5218 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5219 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5220 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5221
5222 if (genlmsg_end(msg, hdr) < 0) {
5223 nlmsg_free(msg);
5224 return;
5225 }
5226
Johannes Berg463d0182009-07-14 00:33:35 +02005227 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5228 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005229 return;
5230
5231 nla_put_failure:
5232 genlmsg_cancel(msg, hdr);
5233 nlmsg_free(msg);
5234}
5235
5236void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005237 struct net_device *netdev, const u8 *buf,
5238 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005239{
5240 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005241 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005242}
5243
5244void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5245 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005246 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005247{
Johannes Berge6d6e342009-07-01 21:26:47 +02005248 nl80211_send_mlme_event(rdev, netdev, buf, len,
5249 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005250}
5251
Jouni Malinen53b46b82009-03-27 20:53:56 +02005252void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005253 struct net_device *netdev, const u8 *buf,
5254 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005255{
5256 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005257 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005258}
5259
Jouni Malinen53b46b82009-03-27 20:53:56 +02005260void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5261 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005262 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005263{
5264 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005265 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005266}
5267
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005268static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5269 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005270 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005271{
5272 struct sk_buff *msg;
5273 void *hdr;
5274
Johannes Berge6d6e342009-07-01 21:26:47 +02005275 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005276 if (!msg)
5277 return;
5278
5279 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5280 if (!hdr) {
5281 nlmsg_free(msg);
5282 return;
5283 }
5284
5285 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5286 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5287 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5288 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5289
5290 if (genlmsg_end(msg, hdr) < 0) {
5291 nlmsg_free(msg);
5292 return;
5293 }
5294
Johannes Berg463d0182009-07-14 00:33:35 +02005295 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5296 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005297 return;
5298
5299 nla_put_failure:
5300 genlmsg_cancel(msg, hdr);
5301 nlmsg_free(msg);
5302}
5303
5304void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005305 struct net_device *netdev, const u8 *addr,
5306 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005307{
5308 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005309 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005310}
5311
5312void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005313 struct net_device *netdev, const u8 *addr,
5314 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005315{
Johannes Berge6d6e342009-07-01 21:26:47 +02005316 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5317 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005318}
5319
Samuel Ortizb23aa672009-07-01 21:26:54 +02005320void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5321 struct net_device *netdev, const u8 *bssid,
5322 const u8 *req_ie, size_t req_ie_len,
5323 const u8 *resp_ie, size_t resp_ie_len,
5324 u16 status, gfp_t gfp)
5325{
5326 struct sk_buff *msg;
5327 void *hdr;
5328
5329 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5330 if (!msg)
5331 return;
5332
5333 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5334 if (!hdr) {
5335 nlmsg_free(msg);
5336 return;
5337 }
5338
5339 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5340 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5341 if (bssid)
5342 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5343 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5344 if (req_ie)
5345 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5346 if (resp_ie)
5347 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5348
5349 if (genlmsg_end(msg, hdr) < 0) {
5350 nlmsg_free(msg);
5351 return;
5352 }
5353
Johannes Berg463d0182009-07-14 00:33:35 +02005354 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5355 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005356 return;
5357
5358 nla_put_failure:
5359 genlmsg_cancel(msg, hdr);
5360 nlmsg_free(msg);
5361
5362}
5363
5364void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5365 struct net_device *netdev, const u8 *bssid,
5366 const u8 *req_ie, size_t req_ie_len,
5367 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5368{
5369 struct sk_buff *msg;
5370 void *hdr;
5371
5372 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5373 if (!msg)
5374 return;
5375
5376 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5377 if (!hdr) {
5378 nlmsg_free(msg);
5379 return;
5380 }
5381
5382 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5383 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5384 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5385 if (req_ie)
5386 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5387 if (resp_ie)
5388 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5389
5390 if (genlmsg_end(msg, hdr) < 0) {
5391 nlmsg_free(msg);
5392 return;
5393 }
5394
Johannes Berg463d0182009-07-14 00:33:35 +02005395 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5396 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005397 return;
5398
5399 nla_put_failure:
5400 genlmsg_cancel(msg, hdr);
5401 nlmsg_free(msg);
5402
5403}
5404
5405void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5406 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02005407 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005408{
5409 struct sk_buff *msg;
5410 void *hdr;
5411
Johannes Berg667503dd2009-07-07 03:56:11 +02005412 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005413 if (!msg)
5414 return;
5415
5416 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5417 if (!hdr) {
5418 nlmsg_free(msg);
5419 return;
5420 }
5421
5422 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5423 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5424 if (from_ap && reason)
5425 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5426 if (from_ap)
5427 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5428 if (ie)
5429 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5430
5431 if (genlmsg_end(msg, hdr) < 0) {
5432 nlmsg_free(msg);
5433 return;
5434 }
5435
Johannes Berg463d0182009-07-14 00:33:35 +02005436 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5437 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005438 return;
5439
5440 nla_put_failure:
5441 genlmsg_cancel(msg, hdr);
5442 nlmsg_free(msg);
5443
5444}
5445
Johannes Berg04a773a2009-04-19 21:24:32 +02005446void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5447 struct net_device *netdev, const u8 *bssid,
5448 gfp_t gfp)
5449{
5450 struct sk_buff *msg;
5451 void *hdr;
5452
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005453 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005454 if (!msg)
5455 return;
5456
5457 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5458 if (!hdr) {
5459 nlmsg_free(msg);
5460 return;
5461 }
5462
5463 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5464 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5465 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5466
5467 if (genlmsg_end(msg, hdr) < 0) {
5468 nlmsg_free(msg);
5469 return;
5470 }
5471
Johannes Berg463d0182009-07-14 00:33:35 +02005472 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5473 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005474 return;
5475
5476 nla_put_failure:
5477 genlmsg_cancel(msg, hdr);
5478 nlmsg_free(msg);
5479}
5480
Jouni Malinena3b8b052009-03-27 21:59:49 +02005481void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5482 struct net_device *netdev, const u8 *addr,
5483 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005484 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005485{
5486 struct sk_buff *msg;
5487 void *hdr;
5488
Johannes Berge6d6e342009-07-01 21:26:47 +02005489 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005490 if (!msg)
5491 return;
5492
5493 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5494 if (!hdr) {
5495 nlmsg_free(msg);
5496 return;
5497 }
5498
5499 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5500 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5501 if (addr)
5502 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5503 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5504 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5505 if (tsc)
5506 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5507
5508 if (genlmsg_end(msg, hdr) < 0) {
5509 nlmsg_free(msg);
5510 return;
5511 }
5512
Johannes Berg463d0182009-07-14 00:33:35 +02005513 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5514 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005515 return;
5516
5517 nla_put_failure:
5518 genlmsg_cancel(msg, hdr);
5519 nlmsg_free(msg);
5520}
5521
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005522void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5523 struct ieee80211_channel *channel_before,
5524 struct ieee80211_channel *channel_after)
5525{
5526 struct sk_buff *msg;
5527 void *hdr;
5528 struct nlattr *nl_freq;
5529
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005530 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005531 if (!msg)
5532 return;
5533
5534 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5535 if (!hdr) {
5536 nlmsg_free(msg);
5537 return;
5538 }
5539
5540 /*
5541 * Since we are applying the beacon hint to a wiphy we know its
5542 * wiphy_idx is valid
5543 */
5544 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5545
5546 /* Before */
5547 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5548 if (!nl_freq)
5549 goto nla_put_failure;
5550 if (nl80211_msg_put_channel(msg, channel_before))
5551 goto nla_put_failure;
5552 nla_nest_end(msg, nl_freq);
5553
5554 /* After */
5555 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5556 if (!nl_freq)
5557 goto nla_put_failure;
5558 if (nl80211_msg_put_channel(msg, channel_after))
5559 goto nla_put_failure;
5560 nla_nest_end(msg, nl_freq);
5561
5562 if (genlmsg_end(msg, hdr) < 0) {
5563 nlmsg_free(msg);
5564 return;
5565 }
5566
Johannes Berg463d0182009-07-14 00:33:35 +02005567 rcu_read_lock();
5568 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5569 GFP_ATOMIC);
5570 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005571
5572 return;
5573
5574nla_put_failure:
5575 genlmsg_cancel(msg, hdr);
5576 nlmsg_free(msg);
5577}
5578
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005579static void nl80211_send_remain_on_chan_event(
5580 int cmd, struct cfg80211_registered_device *rdev,
5581 struct net_device *netdev, u64 cookie,
5582 struct ieee80211_channel *chan,
5583 enum nl80211_channel_type channel_type,
5584 unsigned int duration, gfp_t gfp)
5585{
5586 struct sk_buff *msg;
5587 void *hdr;
5588
5589 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5590 if (!msg)
5591 return;
5592
5593 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5594 if (!hdr) {
5595 nlmsg_free(msg);
5596 return;
5597 }
5598
5599 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5600 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5601 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5602 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5603 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5604
5605 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5606 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5607
5608 if (genlmsg_end(msg, hdr) < 0) {
5609 nlmsg_free(msg);
5610 return;
5611 }
5612
5613 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5614 nl80211_mlme_mcgrp.id, gfp);
5615 return;
5616
5617 nla_put_failure:
5618 genlmsg_cancel(msg, hdr);
5619 nlmsg_free(msg);
5620}
5621
5622void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5623 struct net_device *netdev, u64 cookie,
5624 struct ieee80211_channel *chan,
5625 enum nl80211_channel_type channel_type,
5626 unsigned int duration, gfp_t gfp)
5627{
5628 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5629 rdev, netdev, cookie, chan,
5630 channel_type, duration, gfp);
5631}
5632
5633void nl80211_send_remain_on_channel_cancel(
5634 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5635 u64 cookie, struct ieee80211_channel *chan,
5636 enum nl80211_channel_type channel_type, gfp_t gfp)
5637{
5638 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5639 rdev, netdev, cookie, chan,
5640 channel_type, 0, gfp);
5641}
5642
Johannes Berg98b62182009-12-23 13:15:44 +01005643void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5644 struct net_device *dev, const u8 *mac_addr,
5645 struct station_info *sinfo, gfp_t gfp)
5646{
5647 struct sk_buff *msg;
5648
5649 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5650 if (!msg)
5651 return;
5652
5653 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5654 nlmsg_free(msg);
5655 return;
5656 }
5657
5658 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5659 nl80211_mlme_mcgrp.id, gfp);
5660}
5661
Johannes Berg2e161f72010-08-12 15:38:38 +02005662int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5663 struct net_device *netdev, u32 nlpid,
5664 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005665{
5666 struct sk_buff *msg;
5667 void *hdr;
5668 int err;
5669
5670 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5671 if (!msg)
5672 return -ENOMEM;
5673
Johannes Berg2e161f72010-08-12 15:38:38 +02005674 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005675 if (!hdr) {
5676 nlmsg_free(msg);
5677 return -ENOMEM;
5678 }
5679
5680 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5681 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5682 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5683 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5684
5685 err = genlmsg_end(msg, hdr);
5686 if (err < 0) {
5687 nlmsg_free(msg);
5688 return err;
5689 }
5690
5691 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5692 if (err < 0)
5693 return err;
5694 return 0;
5695
5696 nla_put_failure:
5697 genlmsg_cancel(msg, hdr);
5698 nlmsg_free(msg);
5699 return -ENOBUFS;
5700}
5701
Johannes Berg2e161f72010-08-12 15:38:38 +02005702void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5703 struct net_device *netdev, u64 cookie,
5704 const u8 *buf, size_t len, bool ack,
5705 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005706{
5707 struct sk_buff *msg;
5708 void *hdr;
5709
5710 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5711 if (!msg)
5712 return;
5713
Johannes Berg2e161f72010-08-12 15:38:38 +02005714 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02005715 if (!hdr) {
5716 nlmsg_free(msg);
5717 return;
5718 }
5719
5720 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5721 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5722 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5723 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5724 if (ack)
5725 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5726
5727 if (genlmsg_end(msg, hdr) < 0) {
5728 nlmsg_free(msg);
5729 return;
5730 }
5731
5732 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5733 return;
5734
5735 nla_put_failure:
5736 genlmsg_cancel(msg, hdr);
5737 nlmsg_free(msg);
5738}
5739
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005740void
5741nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5742 struct net_device *netdev,
5743 enum nl80211_cqm_rssi_threshold_event rssi_event,
5744 gfp_t gfp)
5745{
5746 struct sk_buff *msg;
5747 struct nlattr *pinfoattr;
5748 void *hdr;
5749
5750 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5751 if (!msg)
5752 return;
5753
5754 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5755 if (!hdr) {
5756 nlmsg_free(msg);
5757 return;
5758 }
5759
5760 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5761 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5762
5763 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5764 if (!pinfoattr)
5765 goto nla_put_failure;
5766
5767 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5768 rssi_event);
5769
5770 nla_nest_end(msg, pinfoattr);
5771
5772 if (genlmsg_end(msg, hdr) < 0) {
5773 nlmsg_free(msg);
5774 return;
5775 }
5776
5777 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5778 nl80211_mlme_mcgrp.id, gfp);
5779 return;
5780
5781 nla_put_failure:
5782 genlmsg_cancel(msg, hdr);
5783 nlmsg_free(msg);
5784}
5785
Johannes Bergc063dbf2010-11-24 08:10:05 +01005786void
5787nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
5788 struct net_device *netdev, const u8 *peer,
5789 u32 num_packets, gfp_t gfp)
5790{
5791 struct sk_buff *msg;
5792 struct nlattr *pinfoattr;
5793 void *hdr;
5794
5795 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5796 if (!msg)
5797 return;
5798
5799 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5800 if (!hdr) {
5801 nlmsg_free(msg);
5802 return;
5803 }
5804
5805 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5806 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5807 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
5808
5809 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5810 if (!pinfoattr)
5811 goto nla_put_failure;
5812
5813 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
5814
5815 nla_nest_end(msg, pinfoattr);
5816
5817 if (genlmsg_end(msg, hdr) < 0) {
5818 nlmsg_free(msg);
5819 return;
5820 }
5821
5822 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5823 nl80211_mlme_mcgrp.id, gfp);
5824 return;
5825
5826 nla_put_failure:
5827 genlmsg_cancel(msg, hdr);
5828 nlmsg_free(msg);
5829}
5830
Jouni Malinen026331c2010-02-15 12:53:10 +02005831static int nl80211_netlink_notify(struct notifier_block * nb,
5832 unsigned long state,
5833 void *_notify)
5834{
5835 struct netlink_notify *notify = _notify;
5836 struct cfg80211_registered_device *rdev;
5837 struct wireless_dev *wdev;
5838
5839 if (state != NETLINK_URELEASE)
5840 return NOTIFY_DONE;
5841
5842 rcu_read_lock();
5843
5844 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5845 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02005846 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02005847
5848 rcu_read_unlock();
5849
5850 return NOTIFY_DONE;
5851}
5852
5853static struct notifier_block nl80211_netlink_notifier = {
5854 .notifier_call = nl80211_netlink_notify,
5855};
5856
Johannes Berg55682962007-09-20 13:09:35 -04005857/* initialisation/exit functions */
5858
5859int nl80211_init(void)
5860{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005861 int err;
Johannes Berg55682962007-09-20 13:09:35 -04005862
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005863 err = genl_register_family_with_ops(&nl80211_fam,
5864 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04005865 if (err)
5866 return err;
5867
Johannes Berg55682962007-09-20 13:09:35 -04005868 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5869 if (err)
5870 goto err_out;
5871
Johannes Berg2a519312009-02-10 21:25:55 +01005872 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5873 if (err)
5874 goto err_out;
5875
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005876 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5877 if (err)
5878 goto err_out;
5879
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005880 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5881 if (err)
5882 goto err_out;
5883
Johannes Bergaff89a92009-07-01 21:26:51 +02005884#ifdef CONFIG_NL80211_TESTMODE
5885 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5886 if (err)
5887 goto err_out;
5888#endif
5889
Jouni Malinen026331c2010-02-15 12:53:10 +02005890 err = netlink_register_notifier(&nl80211_netlink_notifier);
5891 if (err)
5892 goto err_out;
5893
Johannes Berg55682962007-09-20 13:09:35 -04005894 return 0;
5895 err_out:
5896 genl_unregister_family(&nl80211_fam);
5897 return err;
5898}
5899
5900void nl80211_exit(void)
5901{
Jouni Malinen026331c2010-02-15 12:53:10 +02005902 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04005903 genl_unregister_family(&nl80211_fam);
5904}