blob: 4fac370284c047d331bf2255aba505cf7364840e [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 },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100124 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300125
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800126 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700127 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700128
Jouni Malinen36aedc92008-08-25 11:58:58 +0300129 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
130 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200131
132 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
133 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
134 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100135 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
136 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200137
138 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
139 .len = IEEE80211_MAX_SSID_LEN },
140 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
141 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200142 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300143 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300144 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300145 [NL80211_ATTR_STA_FLAGS2] = {
146 .len = sizeof(struct nl80211_sta_flag_update),
147 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300148 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300149 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
150 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200151 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
152 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
153 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200154 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100155 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100156 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
157 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100158 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
159 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200160 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200161 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
162 .len = IEEE80211_MAX_DATA_LEN },
163 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200164 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200165 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300166 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200167 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300168 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
169 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200170 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900171 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
172 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100173 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100174 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100175 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200176 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700177 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400178};
179
Johannes Berge31b8212010-10-05 19:39:30 +0200180/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000181static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200182 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200183 [NL80211_KEY_IDX] = { .type = NLA_U8 },
184 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
185 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
186 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
187 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200188 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100189 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
190};
191
192/* policy for the key default flags */
193static const struct nla_policy
194nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
195 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
196 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200197};
198
Johannes Bergff1b6e62011-05-04 15:37:28 +0200199/* policy for WoWLAN attributes */
200static const struct nla_policy
201nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
202 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
203 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
204 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
205 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
206};
207
Holger Schuriga0438972009-11-11 11:30:02 +0100208/* ifidx get helper */
209static int nl80211_get_ifidx(struct netlink_callback *cb)
210{
211 int res;
212
213 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
214 nl80211_fam.attrbuf, nl80211_fam.maxattr,
215 nl80211_policy);
216 if (res)
217 return res;
218
219 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
220 return -EINVAL;
221
222 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
223 if (!res)
224 return -EINVAL;
225 return res;
226}
227
Johannes Berg67748892010-10-04 21:14:06 +0200228static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
229 struct netlink_callback *cb,
230 struct cfg80211_registered_device **rdev,
231 struct net_device **dev)
232{
233 int ifidx = cb->args[0];
234 int err;
235
236 if (!ifidx)
237 ifidx = nl80211_get_ifidx(cb);
238 if (ifidx < 0)
239 return ifidx;
240
241 cb->args[0] = ifidx;
242
243 rtnl_lock();
244
245 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
246 if (!*dev) {
247 err = -ENODEV;
248 goto out_rtnl;
249 }
250
251 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100252 if (IS_ERR(*rdev)) {
253 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200254 goto out_rtnl;
255 }
256
257 return 0;
258 out_rtnl:
259 rtnl_unlock();
260 return err;
261}
262
263static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
264{
265 cfg80211_unlock_rdev(rdev);
266 rtnl_unlock();
267}
268
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100269/* IE validation */
270static bool is_valid_ie_attr(const struct nlattr *attr)
271{
272 const u8 *pos;
273 int len;
274
275 if (!attr)
276 return true;
277
278 pos = nla_data(attr);
279 len = nla_len(attr);
280
281 while (len) {
282 u8 elemlen;
283
284 if (len < 2)
285 return false;
286 len -= 2;
287
288 elemlen = pos[1];
289 if (elemlen > len)
290 return false;
291
292 len -= elemlen;
293 pos += 2 + elemlen;
294 }
295
296 return true;
297}
298
Johannes Berg55682962007-09-20 13:09:35 -0400299/* message building helper */
300static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
301 int flags, u8 cmd)
302{
303 /* since there is no private header just add the generic one */
304 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
305}
306
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400307static int nl80211_msg_put_channel(struct sk_buff *msg,
308 struct ieee80211_channel *chan)
309{
310 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
311 chan->center_freq);
312
313 if (chan->flags & IEEE80211_CHAN_DISABLED)
314 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
315 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
316 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
317 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
318 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
319 if (chan->flags & IEEE80211_CHAN_RADAR)
320 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
321
322 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
323 DBM_TO_MBM(chan->max_power));
324
325 return 0;
326
327 nla_put_failure:
328 return -ENOBUFS;
329}
330
Johannes Berg55682962007-09-20 13:09:35 -0400331/* netlink command implementations */
332
Johannes Bergb9454e82009-07-08 13:29:08 +0200333struct key_parse {
334 struct key_params p;
335 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200336 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200337 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100338 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200339};
340
341static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
342{
343 struct nlattr *tb[NL80211_KEY_MAX + 1];
344 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
345 nl80211_key_policy);
346 if (err)
347 return err;
348
349 k->def = !!tb[NL80211_KEY_DEFAULT];
350 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
351
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100352 if (k->def) {
353 k->def_uni = true;
354 k->def_multi = true;
355 }
356 if (k->defmgmt)
357 k->def_multi = true;
358
Johannes Bergb9454e82009-07-08 13:29:08 +0200359 if (tb[NL80211_KEY_IDX])
360 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
361
362 if (tb[NL80211_KEY_DATA]) {
363 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
364 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
365 }
366
367 if (tb[NL80211_KEY_SEQ]) {
368 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
369 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
370 }
371
372 if (tb[NL80211_KEY_CIPHER])
373 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
374
Johannes Berge31b8212010-10-05 19:39:30 +0200375 if (tb[NL80211_KEY_TYPE]) {
376 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
377 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
378 return -EINVAL;
379 }
380
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100381 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
382 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
383 int err = nla_parse_nested(kdt,
384 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
385 tb[NL80211_KEY_DEFAULT_TYPES],
386 nl80211_key_default_policy);
387 if (err)
388 return err;
389
390 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
391 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
392 }
393
Johannes Bergb9454e82009-07-08 13:29:08 +0200394 return 0;
395}
396
397static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
398{
399 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
400 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
401 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
402 }
403
404 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
405 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
406 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
407 }
408
409 if (info->attrs[NL80211_ATTR_KEY_IDX])
410 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
411
412 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
413 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
414
415 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
416 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
417
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100418 if (k->def) {
419 k->def_uni = true;
420 k->def_multi = true;
421 }
422 if (k->defmgmt)
423 k->def_multi = true;
424
Johannes Berge31b8212010-10-05 19:39:30 +0200425 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
426 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
427 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
428 return -EINVAL;
429 }
430
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100431 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
432 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
433 int err = nla_parse_nested(
434 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
435 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
436 nl80211_key_default_policy);
437 if (err)
438 return err;
439
440 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
441 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
442 }
443
Johannes Bergb9454e82009-07-08 13:29:08 +0200444 return 0;
445}
446
447static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
448{
449 int err;
450
451 memset(k, 0, sizeof(*k));
452 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200453 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200454
455 if (info->attrs[NL80211_ATTR_KEY])
456 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
457 else
458 err = nl80211_parse_key_old(info, k);
459
460 if (err)
461 return err;
462
463 if (k->def && k->defmgmt)
464 return -EINVAL;
465
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100466 if (k->defmgmt) {
467 if (k->def_uni || !k->def_multi)
468 return -EINVAL;
469 }
470
Johannes Bergb9454e82009-07-08 13:29:08 +0200471 if (k->idx != -1) {
472 if (k->defmgmt) {
473 if (k->idx < 4 || k->idx > 5)
474 return -EINVAL;
475 } else if (k->def) {
476 if (k->idx < 0 || k->idx > 3)
477 return -EINVAL;
478 } else {
479 if (k->idx < 0 || k->idx > 5)
480 return -EINVAL;
481 }
482 }
483
484 return 0;
485}
486
Johannes Bergfffd0932009-07-08 14:22:54 +0200487static struct cfg80211_cached_keys *
488nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
489 struct nlattr *keys)
490{
491 struct key_parse parse;
492 struct nlattr *key;
493 struct cfg80211_cached_keys *result;
494 int rem, err, def = 0;
495
496 result = kzalloc(sizeof(*result), GFP_KERNEL);
497 if (!result)
498 return ERR_PTR(-ENOMEM);
499
500 result->def = -1;
501 result->defmgmt = -1;
502
503 nla_for_each_nested(key, keys, rem) {
504 memset(&parse, 0, sizeof(parse));
505 parse.idx = -1;
506
507 err = nl80211_parse_key_new(key, &parse);
508 if (err)
509 goto error;
510 err = -EINVAL;
511 if (!parse.p.key)
512 goto error;
513 if (parse.idx < 0 || parse.idx > 4)
514 goto error;
515 if (parse.def) {
516 if (def)
517 goto error;
518 def = 1;
519 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100520 if (!parse.def_uni || !parse.def_multi)
521 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200522 } else if (parse.defmgmt)
523 goto error;
524 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200525 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200526 if (err)
527 goto error;
528 result->params[parse.idx].cipher = parse.p.cipher;
529 result->params[parse.idx].key_len = parse.p.key_len;
530 result->params[parse.idx].key = result->data[parse.idx];
531 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
532 }
533
534 return result;
535 error:
536 kfree(result);
537 return ERR_PTR(err);
538}
539
540static int nl80211_key_allowed(struct wireless_dev *wdev)
541{
542 ASSERT_WDEV_LOCK(wdev);
543
Johannes Bergfffd0932009-07-08 14:22:54 +0200544 switch (wdev->iftype) {
545 case NL80211_IFTYPE_AP:
546 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200547 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700548 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200549 break;
550 case NL80211_IFTYPE_ADHOC:
551 if (!wdev->current_bss)
552 return -ENOLINK;
553 break;
554 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200555 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200556 if (wdev->sme_state != CFG80211_SME_CONNECTED)
557 return -ENOLINK;
558 break;
559 default:
560 return -EINVAL;
561 }
562
563 return 0;
564}
565
Johannes Berg55682962007-09-20 13:09:35 -0400566static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
567 struct cfg80211_registered_device *dev)
568{
569 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100570 struct nlattr *nl_bands, *nl_band;
571 struct nlattr *nl_freqs, *nl_freq;
572 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700573 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100574 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100575 enum ieee80211_band band;
576 struct ieee80211_channel *chan;
577 struct ieee80211_rate *rate;
578 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700579 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg2e161f72010-08-12 15:38:38 +0200580 const struct ieee80211_txrx_stypes *mgmt_stypes =
581 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400582
583 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
584 if (!hdr)
585 return -1;
586
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500587 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400588 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200589
Johannes Bergf5ea9122009-08-07 16:17:38 +0200590 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
591 cfg80211_rdev_list_generation);
592
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200593 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
594 dev->wiphy.retry_short);
595 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
596 dev->wiphy.retry_long);
597 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
598 dev->wiphy.frag_threshold);
599 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
600 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100601 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
602 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100603 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
604 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200605 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
606 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100607
Johannes Berge31b8212010-10-05 19:39:30 +0200608 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
609 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700610 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
611 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Johannes Berge31b8212010-10-05 19:39:30 +0200612
Johannes Berg25e47c182009-04-02 20:14:06 +0200613 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
614 sizeof(u32) * dev->wiphy.n_cipher_suites,
615 dev->wiphy.cipher_suites);
616
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100617 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
618 dev->wiphy.max_num_pmkids);
619
Johannes Bergc0692b82010-08-27 14:26:53 +0300620 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
621 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
622
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900623 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
624 dev->wiphy.available_antennas_tx);
625 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
626 dev->wiphy.available_antennas_rx);
627
Bruno Randolf7f531e02010-12-16 11:30:22 +0900628 if ((dev->wiphy.available_antennas_tx ||
629 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900630 u32 tx_ant = 0, rx_ant = 0;
631 int res;
632 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
633 if (!res) {
634 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
635 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
636 }
637 }
638
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700639 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
640 if (!nl_modes)
641 goto nla_put_failure;
642
643 i = 0;
644 while (ifmodes) {
645 if (ifmodes & 1)
646 NLA_PUT_FLAG(msg, i);
647 ifmodes >>= 1;
648 i++;
649 }
650
651 nla_nest_end(msg, nl_modes);
652
Johannes Bergee688b002008-01-24 19:38:39 +0100653 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
654 if (!nl_bands)
655 goto nla_put_failure;
656
657 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
658 if (!dev->wiphy.bands[band])
659 continue;
660
661 nl_band = nla_nest_start(msg, band);
662 if (!nl_band)
663 goto nla_put_failure;
664
Johannes Bergd51626d2008-10-09 12:20:13 +0200665 /* add HT info */
666 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
667 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
668 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
669 &dev->wiphy.bands[band]->ht_cap.mcs);
670 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
671 dev->wiphy.bands[band]->ht_cap.cap);
672 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
673 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
674 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
675 dev->wiphy.bands[band]->ht_cap.ampdu_density);
676 }
677
Johannes Bergee688b002008-01-24 19:38:39 +0100678 /* add frequencies */
679 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
680 if (!nl_freqs)
681 goto nla_put_failure;
682
683 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
684 nl_freq = nla_nest_start(msg, i);
685 if (!nl_freq)
686 goto nla_put_failure;
687
688 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100689
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400690 if (nl80211_msg_put_channel(msg, chan))
691 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200692
Johannes Bergee688b002008-01-24 19:38:39 +0100693 nla_nest_end(msg, nl_freq);
694 }
695
696 nla_nest_end(msg, nl_freqs);
697
698 /* add bitrates */
699 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
700 if (!nl_rates)
701 goto nla_put_failure;
702
703 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
704 nl_rate = nla_nest_start(msg, i);
705 if (!nl_rate)
706 goto nla_put_failure;
707
708 rate = &dev->wiphy.bands[band]->bitrates[i];
709 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
710 rate->bitrate);
711 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
712 NLA_PUT_FLAG(msg,
713 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
714
715 nla_nest_end(msg, nl_rate);
716 }
717
718 nla_nest_end(msg, nl_rates);
719
720 nla_nest_end(msg, nl_band);
721 }
722 nla_nest_end(msg, nl_bands);
723
Johannes Berg8fdc6212009-03-14 09:34:01 +0100724 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
725 if (!nl_cmds)
726 goto nla_put_failure;
727
728 i = 0;
729#define CMD(op, n) \
730 do { \
731 if (dev->ops->op) { \
732 i++; \
733 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
734 } \
735 } while (0)
736
737 CMD(add_virtual_intf, NEW_INTERFACE);
738 CMD(change_virtual_intf, SET_INTERFACE);
739 CMD(add_key, NEW_KEY);
740 CMD(add_beacon, NEW_BEACON);
741 CMD(add_station, NEW_STATION);
742 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800743 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100744 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200745 CMD(auth, AUTHENTICATE);
746 CMD(assoc, ASSOCIATE);
747 CMD(deauth, DEAUTHENTICATE);
748 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200749 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100750 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100751 CMD(set_pmksa, SET_PMKSA);
752 CMD(del_pmksa, DEL_PMKSA);
753 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100754 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200755 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200756 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100757 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100758 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200759 i++;
760 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
761 }
Johannes Bergf444de02010-05-05 15:25:02 +0200762 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400763 CMD(set_wds_peer, SET_WDS_PEER);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300764 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
765 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100766
767#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200768
Johannes Berg6829c872009-07-02 09:13:27 +0200769 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200770 i++;
771 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
772 }
773
Johannes Berg6829c872009-07-02 09:13:27 +0200774 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200775 i++;
776 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
777 }
778
Johannes Berg8fdc6212009-03-14 09:34:01 +0100779 nla_nest_end(msg, nl_cmds);
780
Johannes Berga2939112010-12-14 17:54:28 +0100781 if (dev->ops->remain_on_channel)
782 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
783 dev->wiphy.max_remain_on_channel_duration);
784
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100785 /* for now at least assume all drivers have it */
786 if (dev->ops->mgmt_tx)
787 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
788
Johannes Berg2e161f72010-08-12 15:38:38 +0200789 if (mgmt_stypes) {
790 u16 stypes;
791 struct nlattr *nl_ftypes, *nl_ifs;
792 enum nl80211_iftype ift;
793
794 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
795 if (!nl_ifs)
796 goto nla_put_failure;
797
798 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
799 nl_ftypes = nla_nest_start(msg, ift);
800 if (!nl_ftypes)
801 goto nla_put_failure;
802 i = 0;
803 stypes = mgmt_stypes[ift].tx;
804 while (stypes) {
805 if (stypes & 1)
806 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
807 (i << 4) | IEEE80211_FTYPE_MGMT);
808 stypes >>= 1;
809 i++;
810 }
811 nla_nest_end(msg, nl_ftypes);
812 }
813
Johannes Berg74b70a42010-08-24 12:15:53 +0200814 nla_nest_end(msg, nl_ifs);
815
Johannes Berg2e161f72010-08-12 15:38:38 +0200816 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
817 if (!nl_ifs)
818 goto nla_put_failure;
819
820 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
821 nl_ftypes = nla_nest_start(msg, ift);
822 if (!nl_ftypes)
823 goto nla_put_failure;
824 i = 0;
825 stypes = mgmt_stypes[ift].rx;
826 while (stypes) {
827 if (stypes & 1)
828 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
829 (i << 4) | IEEE80211_FTYPE_MGMT);
830 stypes >>= 1;
831 i++;
832 }
833 nla_nest_end(msg, nl_ftypes);
834 }
835 nla_nest_end(msg, nl_ifs);
836 }
837
Johannes Bergff1b6e62011-05-04 15:37:28 +0200838 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
839 struct nlattr *nl_wowlan;
840
841 nl_wowlan = nla_nest_start(msg,
842 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
843 if (!nl_wowlan)
844 goto nla_put_failure;
845
846 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
847 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
848 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
849 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
850 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
851 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
852 if (dev->wiphy.wowlan.n_patterns) {
853 struct nl80211_wowlan_pattern_support pat = {
854 .max_patterns = dev->wiphy.wowlan.n_patterns,
855 .min_pattern_len =
856 dev->wiphy.wowlan.pattern_min_len,
857 .max_pattern_len =
858 dev->wiphy.wowlan.pattern_max_len,
859 };
860 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
861 sizeof(pat), &pat);
862 }
863
864 nla_nest_end(msg, nl_wowlan);
865 }
866
Johannes Berg55682962007-09-20 13:09:35 -0400867 return genlmsg_end(msg, hdr);
868
869 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700870 genlmsg_cancel(msg, hdr);
871 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400872}
873
874static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
875{
876 int idx = 0;
877 int start = cb->args[0];
878 struct cfg80211_registered_device *dev;
879
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500880 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200881 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200882 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
883 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200884 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400885 continue;
886 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
887 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200888 dev) < 0) {
889 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400890 break;
Julius Volzb4637272008-07-08 14:02:19 +0200891 }
Johannes Berg55682962007-09-20 13:09:35 -0400892 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500893 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400894
895 cb->args[0] = idx;
896
897 return skb->len;
898}
899
900static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
901{
902 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200903 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400904
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700905 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400906 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200907 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400908
Johannes Berg4c476992010-10-04 21:36:35 +0200909 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
910 nlmsg_free(msg);
911 return -ENOBUFS;
912 }
Johannes Berg55682962007-09-20 13:09:35 -0400913
Johannes Berg134e6372009-07-10 09:51:34 +0000914 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400915}
916
Jouni Malinen31888482008-10-30 16:59:24 +0200917static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
918 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
919 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
920 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
921 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
922 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
923};
924
925static int parse_txq_params(struct nlattr *tb[],
926 struct ieee80211_txq_params *txq_params)
927{
928 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
929 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
930 !tb[NL80211_TXQ_ATTR_AIFS])
931 return -EINVAL;
932
933 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
934 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
935 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
936 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
937 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
938
939 return 0;
940}
941
Johannes Bergf444de02010-05-05 15:25:02 +0200942static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
943{
944 /*
945 * You can only set the channel explicitly for AP, mesh
946 * and WDS type interfaces; all others have their channel
947 * managed via their respective "establish a connection"
948 * command (connect, join, ...)
949 *
950 * Monitors are special as they are normally slaved to
951 * whatever else is going on, so they behave as though
952 * you tried setting the wiphy channel itself.
953 */
954 return !wdev ||
955 wdev->iftype == NL80211_IFTYPE_AP ||
956 wdev->iftype == NL80211_IFTYPE_WDS ||
957 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200958 wdev->iftype == NL80211_IFTYPE_MONITOR ||
959 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200960}
961
962static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
963 struct wireless_dev *wdev,
964 struct genl_info *info)
965{
966 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
967 u32 freq;
968 int result;
969
970 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
971 return -EINVAL;
972
973 if (!nl80211_can_set_dev_channel(wdev))
974 return -EOPNOTSUPP;
975
976 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
977 channel_type = nla_get_u32(info->attrs[
978 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
979 if (channel_type != NL80211_CHAN_NO_HT &&
980 channel_type != NL80211_CHAN_HT20 &&
981 channel_type != NL80211_CHAN_HT40PLUS &&
982 channel_type != NL80211_CHAN_HT40MINUS)
983 return -EINVAL;
984 }
985
986 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
987
988 mutex_lock(&rdev->devlist_mtx);
989 if (wdev) {
990 wdev_lock(wdev);
991 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
992 wdev_unlock(wdev);
993 } else {
994 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
995 }
996 mutex_unlock(&rdev->devlist_mtx);
997
998 return result;
999}
1000
1001static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1002{
Johannes Berg4c476992010-10-04 21:36:35 +02001003 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1004 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001005
Johannes Berg4c476992010-10-04 21:36:35 +02001006 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001007}
1008
Bill Jordane8347eb2010-10-01 13:54:28 -04001009static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1010{
Johannes Berg43b19952010-10-07 13:10:30 +02001011 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1012 struct net_device *dev = info->user_ptr[1];
1013 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001014 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001015
1016 if (!info->attrs[NL80211_ATTR_MAC])
1017 return -EINVAL;
1018
Johannes Berg43b19952010-10-07 13:10:30 +02001019 if (netif_running(dev))
1020 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001021
Johannes Berg43b19952010-10-07 13:10:30 +02001022 if (!rdev->ops->set_wds_peer)
1023 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001024
Johannes Berg43b19952010-10-07 13:10:30 +02001025 if (wdev->iftype != NL80211_IFTYPE_WDS)
1026 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001027
1028 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001029 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001030}
1031
1032
Johannes Berg55682962007-09-20 13:09:35 -04001033static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1034{
1035 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001036 struct net_device *netdev = NULL;
1037 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001038 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001039 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001040 u32 changed;
1041 u8 retry_short = 0, retry_long = 0;
1042 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001043 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001044
Johannes Bergf444de02010-05-05 15:25:02 +02001045 /*
1046 * Try to find the wiphy and netdev. Normally this
1047 * function shouldn't need the netdev, but this is
1048 * done for backward compatibility -- previously
1049 * setting the channel was done per wiphy, but now
1050 * it is per netdev. Previous userland like hostapd
1051 * also passed a netdev to set_wiphy, so that it is
1052 * possible to let that go to the right netdev!
1053 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001054 mutex_lock(&cfg80211_mutex);
1055
Johannes Bergf444de02010-05-05 15:25:02 +02001056 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1057 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1058
1059 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1060 if (netdev && netdev->ieee80211_ptr) {
1061 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1062 mutex_lock(&rdev->mtx);
1063 } else
1064 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001065 }
1066
Johannes Bergf444de02010-05-05 15:25:02 +02001067 if (!netdev) {
1068 rdev = __cfg80211_rdev_from_info(info);
1069 if (IS_ERR(rdev)) {
1070 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001071 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001072 }
1073 wdev = NULL;
1074 netdev = NULL;
1075 result = 0;
1076
1077 mutex_lock(&rdev->mtx);
1078 } else if (netif_running(netdev) &&
1079 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1080 wdev = netdev->ieee80211_ptr;
1081 else
1082 wdev = NULL;
1083
1084 /*
1085 * end workaround code, by now the rdev is available
1086 * and locked, and wdev may or may not be NULL.
1087 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001088
1089 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001090 result = cfg80211_dev_rename(
1091 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001092
1093 mutex_unlock(&cfg80211_mutex);
1094
1095 if (result)
1096 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001097
Jouni Malinen31888482008-10-30 16:59:24 +02001098 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1099 struct ieee80211_txq_params txq_params;
1100 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1101
1102 if (!rdev->ops->set_txq_params) {
1103 result = -EOPNOTSUPP;
1104 goto bad_res;
1105 }
1106
1107 nla_for_each_nested(nl_txq_params,
1108 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1109 rem_txq_params) {
1110 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1111 nla_data(nl_txq_params),
1112 nla_len(nl_txq_params),
1113 txq_params_policy);
1114 result = parse_txq_params(tb, &txq_params);
1115 if (result)
1116 goto bad_res;
1117
1118 result = rdev->ops->set_txq_params(&rdev->wiphy,
1119 &txq_params);
1120 if (result)
1121 goto bad_res;
1122 }
1123 }
1124
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001125 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001126 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001127 if (result)
1128 goto bad_res;
1129 }
1130
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001131 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1132 enum nl80211_tx_power_setting type;
1133 int idx, mbm = 0;
1134
1135 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001136 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001137 goto bad_res;
1138 }
1139
1140 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1141 type = nla_get_u32(info->attrs[idx]);
1142
1143 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1144 (type != NL80211_TX_POWER_AUTOMATIC)) {
1145 result = -EINVAL;
1146 goto bad_res;
1147 }
1148
1149 if (type != NL80211_TX_POWER_AUTOMATIC) {
1150 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1151 mbm = nla_get_u32(info->attrs[idx]);
1152 }
1153
1154 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1155 if (result)
1156 goto bad_res;
1157 }
1158
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001159 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1160 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1161 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001162 if ((!rdev->wiphy.available_antennas_tx &&
1163 !rdev->wiphy.available_antennas_rx) ||
1164 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001165 result = -EOPNOTSUPP;
1166 goto bad_res;
1167 }
1168
1169 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1170 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1171
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001172 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001173 * available antenna masks, except for the "all" mask */
1174 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1175 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001176 result = -EINVAL;
1177 goto bad_res;
1178 }
1179
Bruno Randolf7f531e02010-12-16 11:30:22 +09001180 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1181 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001182
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001183 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1184 if (result)
1185 goto bad_res;
1186 }
1187
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001188 changed = 0;
1189
1190 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1191 retry_short = nla_get_u8(
1192 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1193 if (retry_short == 0) {
1194 result = -EINVAL;
1195 goto bad_res;
1196 }
1197 changed |= WIPHY_PARAM_RETRY_SHORT;
1198 }
1199
1200 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1201 retry_long = nla_get_u8(
1202 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1203 if (retry_long == 0) {
1204 result = -EINVAL;
1205 goto bad_res;
1206 }
1207 changed |= WIPHY_PARAM_RETRY_LONG;
1208 }
1209
1210 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1211 frag_threshold = nla_get_u32(
1212 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1213 if (frag_threshold < 256) {
1214 result = -EINVAL;
1215 goto bad_res;
1216 }
1217 if (frag_threshold != (u32) -1) {
1218 /*
1219 * Fragments (apart from the last one) are required to
1220 * have even length. Make the fragmentation code
1221 * simpler by stripping LSB should someone try to use
1222 * odd threshold value.
1223 */
1224 frag_threshold &= ~0x1;
1225 }
1226 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1227 }
1228
1229 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1230 rts_threshold = nla_get_u32(
1231 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1232 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1233 }
1234
Lukáš Turek81077e82009-12-21 22:50:47 +01001235 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1236 coverage_class = nla_get_u8(
1237 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1238 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1239 }
1240
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001241 if (changed) {
1242 u8 old_retry_short, old_retry_long;
1243 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001244 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001245
1246 if (!rdev->ops->set_wiphy_params) {
1247 result = -EOPNOTSUPP;
1248 goto bad_res;
1249 }
1250
1251 old_retry_short = rdev->wiphy.retry_short;
1252 old_retry_long = rdev->wiphy.retry_long;
1253 old_frag_threshold = rdev->wiphy.frag_threshold;
1254 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001255 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001256
1257 if (changed & WIPHY_PARAM_RETRY_SHORT)
1258 rdev->wiphy.retry_short = retry_short;
1259 if (changed & WIPHY_PARAM_RETRY_LONG)
1260 rdev->wiphy.retry_long = retry_long;
1261 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1262 rdev->wiphy.frag_threshold = frag_threshold;
1263 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1264 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001265 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1266 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001267
1268 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1269 if (result) {
1270 rdev->wiphy.retry_short = old_retry_short;
1271 rdev->wiphy.retry_long = old_retry_long;
1272 rdev->wiphy.frag_threshold = old_frag_threshold;
1273 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001274 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001275 }
1276 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001277
Johannes Berg306d6112008-12-08 12:39:04 +01001278 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001279 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001280 if (netdev)
1281 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001282 return result;
1283}
1284
1285
1286static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001287 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001288 struct net_device *dev)
1289{
1290 void *hdr;
1291
1292 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1293 if (!hdr)
1294 return -1;
1295
1296 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001297 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001298 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001299 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001300
1301 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1302 rdev->devlist_generation ^
1303 (cfg80211_rdev_list_generation << 2));
1304
Johannes Berg55682962007-09-20 13:09:35 -04001305 return genlmsg_end(msg, hdr);
1306
1307 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001308 genlmsg_cancel(msg, hdr);
1309 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001310}
1311
1312static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1313{
1314 int wp_idx = 0;
1315 int if_idx = 0;
1316 int wp_start = cb->args[0];
1317 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001318 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001319 struct wireless_dev *wdev;
1320
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001321 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001322 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1323 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001324 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001325 if (wp_idx < wp_start) {
1326 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001327 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001328 }
Johannes Berg55682962007-09-20 13:09:35 -04001329 if_idx = 0;
1330
Johannes Bergf5ea9122009-08-07 16:17:38 +02001331 mutex_lock(&rdev->devlist_mtx);
1332 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001333 if (if_idx < if_start) {
1334 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001335 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001336 }
Johannes Berg55682962007-09-20 13:09:35 -04001337 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1338 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001339 rdev, wdev->netdev) < 0) {
1340 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001341 goto out;
1342 }
1343 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001344 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001345 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001346
1347 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001348 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001349 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001350 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001351
1352 cb->args[0] = wp_idx;
1353 cb->args[1] = if_idx;
1354
1355 return skb->len;
1356}
1357
1358static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1359{
1360 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001361 struct cfg80211_registered_device *dev = info->user_ptr[0];
1362 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001363
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001364 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001365 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001366 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001367
Johannes Bergd7264052009-04-19 16:23:20 +02001368 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001369 dev, netdev) < 0) {
1370 nlmsg_free(msg);
1371 return -ENOBUFS;
1372 }
Johannes Berg55682962007-09-20 13:09:35 -04001373
Johannes Berg134e6372009-07-10 09:51:34 +00001374 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001375}
1376
Michael Wu66f7ac52008-01-31 19:48:22 +01001377static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1378 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1379 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1380 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1381 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1382 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1383};
1384
1385static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1386{
1387 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1388 int flag;
1389
1390 *mntrflags = 0;
1391
1392 if (!nla)
1393 return -EINVAL;
1394
1395 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1396 nla, mntr_flags_policy))
1397 return -EINVAL;
1398
1399 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1400 if (flags[flag])
1401 *mntrflags |= (1<<flag);
1402
1403 return 0;
1404}
1405
Johannes Berg9bc383d2009-11-19 11:55:19 +01001406static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001407 struct net_device *netdev, u8 use_4addr,
1408 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001409{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001410 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001411 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001412 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001413 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001414 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001415
1416 switch (iftype) {
1417 case NL80211_IFTYPE_AP_VLAN:
1418 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1419 return 0;
1420 break;
1421 case NL80211_IFTYPE_STATION:
1422 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1423 return 0;
1424 break;
1425 default:
1426 break;
1427 }
1428
1429 return -EOPNOTSUPP;
1430}
1431
Johannes Berg55682962007-09-20 13:09:35 -04001432static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1433{
Johannes Berg4c476992010-10-04 21:36:35 +02001434 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001435 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001436 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001437 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001438 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001439 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001440 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001441
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001442 memset(&params, 0, sizeof(params));
1443
Johannes Berg04a773a2009-04-19 21:24:32 +02001444 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001445
Johannes Berg723b0382008-09-16 20:22:09 +02001446 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001447 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001448 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001449 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001450 if (ntype > NL80211_IFTYPE_MAX)
1451 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001452 }
1453
Johannes Berg92ffe052008-09-16 20:39:36 +02001454 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001455 struct wireless_dev *wdev = dev->ieee80211_ptr;
1456
Johannes Berg4c476992010-10-04 21:36:35 +02001457 if (ntype != NL80211_IFTYPE_MESH_POINT)
1458 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001459 if (netif_running(dev))
1460 return -EBUSY;
1461
1462 wdev_lock(wdev);
1463 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1464 IEEE80211_MAX_MESH_ID_LEN);
1465 wdev->mesh_id_up_len =
1466 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1467 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1468 wdev->mesh_id_up_len);
1469 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001470 }
1471
Felix Fietkau8b787642009-11-10 18:53:10 +01001472 if (info->attrs[NL80211_ATTR_4ADDR]) {
1473 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1474 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001475 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001476 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001477 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001478 } else {
1479 params.use_4addr = -1;
1480 }
1481
Johannes Berg92ffe052008-09-16 20:39:36 +02001482 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001483 if (ntype != NL80211_IFTYPE_MONITOR)
1484 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001485 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1486 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001487 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001488 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001489
1490 flags = &_flags;
1491 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001492 }
Johannes Berg3b858752009-03-12 09:55:09 +01001493
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001494 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001495 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001496 else
1497 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001498
Johannes Berg9bc383d2009-11-19 11:55:19 +01001499 if (!err && params.use_4addr != -1)
1500 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1501
Johannes Berg55682962007-09-20 13:09:35 -04001502 return err;
1503}
1504
1505static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1506{
Johannes Berg4c476992010-10-04 21:36:35 +02001507 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001508 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001509 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001510 int err;
1511 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001512 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001513
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001514 memset(&params, 0, sizeof(params));
1515
Johannes Berg55682962007-09-20 13:09:35 -04001516 if (!info->attrs[NL80211_ATTR_IFNAME])
1517 return -EINVAL;
1518
1519 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1520 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1521 if (type > NL80211_IFTYPE_MAX)
1522 return -EINVAL;
1523 }
1524
Johannes Berg79c97e92009-07-07 03:56:12 +02001525 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001526 !(rdev->wiphy.interface_modes & (1 << type)))
1527 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001528
Johannes Berg9bc383d2009-11-19 11:55:19 +01001529 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001530 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001531 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001532 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001533 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001534 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001535
Michael Wu66f7ac52008-01-31 19:48:22 +01001536 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1537 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1538 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001539 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001540 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001541 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001542 if (IS_ERR(dev))
1543 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001544
Johannes Berg29cbe682010-12-03 09:20:44 +01001545 if (type == NL80211_IFTYPE_MESH_POINT &&
1546 info->attrs[NL80211_ATTR_MESH_ID]) {
1547 struct wireless_dev *wdev = dev->ieee80211_ptr;
1548
1549 wdev_lock(wdev);
1550 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1551 IEEE80211_MAX_MESH_ID_LEN);
1552 wdev->mesh_id_up_len =
1553 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1554 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1555 wdev->mesh_id_up_len);
1556 wdev_unlock(wdev);
1557 }
1558
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001559 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001560}
1561
1562static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1563{
Johannes Berg4c476992010-10-04 21:36:35 +02001564 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1565 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001566
Johannes Berg4c476992010-10-04 21:36:35 +02001567 if (!rdev->ops->del_virtual_intf)
1568 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001569
Johannes Berg4c476992010-10-04 21:36:35 +02001570 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001571}
1572
Johannes Berg41ade002007-12-19 02:03:29 +01001573struct get_key_cookie {
1574 struct sk_buff *msg;
1575 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001576 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001577};
1578
1579static void get_key_callback(void *c, struct key_params *params)
1580{
Johannes Bergb9454e82009-07-08 13:29:08 +02001581 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001582 struct get_key_cookie *cookie = c;
1583
1584 if (params->key)
1585 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1586 params->key_len, params->key);
1587
1588 if (params->seq)
1589 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1590 params->seq_len, params->seq);
1591
1592 if (params->cipher)
1593 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1594 params->cipher);
1595
Johannes Bergb9454e82009-07-08 13:29:08 +02001596 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1597 if (!key)
1598 goto nla_put_failure;
1599
1600 if (params->key)
1601 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1602 params->key_len, params->key);
1603
1604 if (params->seq)
1605 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1606 params->seq_len, params->seq);
1607
1608 if (params->cipher)
1609 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1610 params->cipher);
1611
1612 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1613
1614 nla_nest_end(cookie->msg, key);
1615
Johannes Berg41ade002007-12-19 02:03:29 +01001616 return;
1617 nla_put_failure:
1618 cookie->error = 1;
1619}
1620
1621static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1622{
Johannes Berg4c476992010-10-04 21:36:35 +02001623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001624 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001625 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001626 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001627 const u8 *mac_addr = NULL;
1628 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001629 struct get_key_cookie cookie = {
1630 .error = 0,
1631 };
1632 void *hdr;
1633 struct sk_buff *msg;
1634
1635 if (info->attrs[NL80211_ATTR_KEY_IDX])
1636 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1637
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001638 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001639 return -EINVAL;
1640
1641 if (info->attrs[NL80211_ATTR_MAC])
1642 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1643
Johannes Berge31b8212010-10-05 19:39:30 +02001644 pairwise = !!mac_addr;
1645 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1646 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1647 if (kt >= NUM_NL80211_KEYTYPES)
1648 return -EINVAL;
1649 if (kt != NL80211_KEYTYPE_GROUP &&
1650 kt != NL80211_KEYTYPE_PAIRWISE)
1651 return -EINVAL;
1652 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1653 }
1654
Johannes Berg4c476992010-10-04 21:36:35 +02001655 if (!rdev->ops->get_key)
1656 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001657
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001658 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001659 if (!msg)
1660 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001661
1662 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1663 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001664 if (IS_ERR(hdr))
1665 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001666
1667 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001668 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001669
1670 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1671 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1672 if (mac_addr)
1673 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1674
Johannes Berge31b8212010-10-05 19:39:30 +02001675 if (pairwise && mac_addr &&
1676 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1677 return -ENOENT;
1678
1679 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1680 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001681
1682 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001683 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001684
1685 if (cookie.error)
1686 goto nla_put_failure;
1687
1688 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001689 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001690
1691 nla_put_failure:
1692 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001693 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001694 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001695 return err;
1696}
1697
1698static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1699{
Johannes Berg4c476992010-10-04 21:36:35 +02001700 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001701 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001702 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001703 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001704
Johannes Bergb9454e82009-07-08 13:29:08 +02001705 err = nl80211_parse_key(info, &key);
1706 if (err)
1707 return err;
1708
1709 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001710 return -EINVAL;
1711
Johannes Bergb9454e82009-07-08 13:29:08 +02001712 /* only support setting default key */
1713 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001714 return -EINVAL;
1715
Johannes Bergfffd0932009-07-08 14:22:54 +02001716 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001717
1718 if (key.def) {
1719 if (!rdev->ops->set_default_key) {
1720 err = -EOPNOTSUPP;
1721 goto out;
1722 }
1723
1724 err = nl80211_key_allowed(dev->ieee80211_ptr);
1725 if (err)
1726 goto out;
1727
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001728 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1729 key.def_uni, key.def_multi);
1730
1731 if (err)
1732 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001733
Johannes Berg3d23e342009-09-29 23:27:28 +02001734#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001735 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001736#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001737 } else {
1738 if (key.def_uni || !key.def_multi) {
1739 err = -EINVAL;
1740 goto out;
1741 }
1742
1743 if (!rdev->ops->set_default_mgmt_key) {
1744 err = -EOPNOTSUPP;
1745 goto out;
1746 }
1747
1748 err = nl80211_key_allowed(dev->ieee80211_ptr);
1749 if (err)
1750 goto out;
1751
1752 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1753 dev, key.idx);
1754 if (err)
1755 goto out;
1756
1757#ifdef CONFIG_CFG80211_WEXT
1758 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1759#endif
1760 }
1761
1762 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001763 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001764
Johannes Berg41ade002007-12-19 02:03:29 +01001765 return err;
1766}
1767
1768static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1769{
Johannes Berg4c476992010-10-04 21:36:35 +02001770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001771 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001772 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001773 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001774 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001775
Johannes Bergb9454e82009-07-08 13:29:08 +02001776 err = nl80211_parse_key(info, &key);
1777 if (err)
1778 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001779
Johannes Bergb9454e82009-07-08 13:29:08 +02001780 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001781 return -EINVAL;
1782
Johannes Berg41ade002007-12-19 02:03:29 +01001783 if (info->attrs[NL80211_ATTR_MAC])
1784 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1785
Johannes Berge31b8212010-10-05 19:39:30 +02001786 if (key.type == -1) {
1787 if (mac_addr)
1788 key.type = NL80211_KEYTYPE_PAIRWISE;
1789 else
1790 key.type = NL80211_KEYTYPE_GROUP;
1791 }
1792
1793 /* for now */
1794 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1795 key.type != NL80211_KEYTYPE_GROUP)
1796 return -EINVAL;
1797
Johannes Berg4c476992010-10-04 21:36:35 +02001798 if (!rdev->ops->add_key)
1799 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001800
Johannes Berge31b8212010-10-05 19:39:30 +02001801 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1802 key.type == NL80211_KEYTYPE_PAIRWISE,
1803 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001804 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001805
1806 wdev_lock(dev->ieee80211_ptr);
1807 err = nl80211_key_allowed(dev->ieee80211_ptr);
1808 if (!err)
1809 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001810 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001811 mac_addr, &key.p);
1812 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001813
Johannes Berg41ade002007-12-19 02:03:29 +01001814 return err;
1815}
1816
1817static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1818{
Johannes Berg4c476992010-10-04 21:36:35 +02001819 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001820 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001821 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001822 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001823 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001824
Johannes Bergb9454e82009-07-08 13:29:08 +02001825 err = nl80211_parse_key(info, &key);
1826 if (err)
1827 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001828
1829 if (info->attrs[NL80211_ATTR_MAC])
1830 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1831
Johannes Berge31b8212010-10-05 19:39:30 +02001832 if (key.type == -1) {
1833 if (mac_addr)
1834 key.type = NL80211_KEYTYPE_PAIRWISE;
1835 else
1836 key.type = NL80211_KEYTYPE_GROUP;
1837 }
1838
1839 /* for now */
1840 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1841 key.type != NL80211_KEYTYPE_GROUP)
1842 return -EINVAL;
1843
Johannes Berg4c476992010-10-04 21:36:35 +02001844 if (!rdev->ops->del_key)
1845 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001846
Johannes Bergfffd0932009-07-08 14:22:54 +02001847 wdev_lock(dev->ieee80211_ptr);
1848 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001849
1850 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1851 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1852 err = -ENOENT;
1853
Johannes Bergfffd0932009-07-08 14:22:54 +02001854 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001855 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1856 key.type == NL80211_KEYTYPE_PAIRWISE,
1857 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001858
Johannes Berg3d23e342009-09-29 23:27:28 +02001859#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001860 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001861 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001862 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001863 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001864 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1865 }
1866#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001867 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001868
Johannes Berg41ade002007-12-19 02:03:29 +01001869 return err;
1870}
1871
Johannes Berged1b6cc2007-12-19 02:03:32 +01001872static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1873{
1874 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1875 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001876 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1877 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001878 struct beacon_parameters params;
1879 int haveinfo = 0;
1880
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001881 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1882 return -EINVAL;
1883
Johannes Berg074ac8d2010-09-16 14:58:22 +02001884 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001885 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1886 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001887
Johannes Berged1b6cc2007-12-19 02:03:32 +01001888 switch (info->genlhdr->cmd) {
1889 case NL80211_CMD_NEW_BEACON:
1890 /* these are required for NEW_BEACON */
1891 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1892 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001893 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1894 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001895
Johannes Berg79c97e92009-07-07 03:56:12 +02001896 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001897 break;
1898 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001899 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001900 break;
1901 default:
1902 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001903 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001904 }
1905
Johannes Berg4c476992010-10-04 21:36:35 +02001906 if (!call)
1907 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001908
1909 memset(&params, 0, sizeof(params));
1910
1911 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1912 params.interval =
1913 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1914 haveinfo = 1;
1915 }
1916
1917 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1918 params.dtim_period =
1919 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1920 haveinfo = 1;
1921 }
1922
1923 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1924 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1925 params.head_len =
1926 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1927 haveinfo = 1;
1928 }
1929
1930 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1931 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1932 params.tail_len =
1933 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1934 haveinfo = 1;
1935 }
1936
Johannes Berg4c476992010-10-04 21:36:35 +02001937 if (!haveinfo)
1938 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001939
Johannes Berg4c476992010-10-04 21:36:35 +02001940 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001941}
1942
1943static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1944{
Johannes Berg4c476992010-10-04 21:36:35 +02001945 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1946 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001947
Johannes Berg4c476992010-10-04 21:36:35 +02001948 if (!rdev->ops->del_beacon)
1949 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001950
Johannes Berg074ac8d2010-09-16 14:58:22 +02001951 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001952 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1953 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001954
Johannes Berg4c476992010-10-04 21:36:35 +02001955 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001956}
1957
Johannes Berg5727ef12007-12-19 02:03:34 +01001958static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1959 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1960 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1961 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001962 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07001963 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001964};
1965
Johannes Bergeccb8e82009-05-11 21:57:56 +03001966static int parse_station_flags(struct genl_info *info,
1967 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001968{
1969 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001970 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001971 int flag;
1972
Johannes Bergeccb8e82009-05-11 21:57:56 +03001973 /*
1974 * Try parsing the new attribute first so userspace
1975 * can specify both for older kernels.
1976 */
1977 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1978 if (nla) {
1979 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001980
Johannes Bergeccb8e82009-05-11 21:57:56 +03001981 sta_flags = nla_data(nla);
1982 params->sta_flags_mask = sta_flags->mask;
1983 params->sta_flags_set = sta_flags->set;
1984 if ((params->sta_flags_mask |
1985 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1986 return -EINVAL;
1987 return 0;
1988 }
1989
1990 /* if present, parse the old attribute */
1991
1992 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001993 if (!nla)
1994 return 0;
1995
1996 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1997 nla, sta_flags_policy))
1998 return -EINVAL;
1999
Johannes Bergeccb8e82009-05-11 21:57:56 +03002000 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2001 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002002
2003 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2004 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002005 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002006
2007 return 0;
2008}
2009
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002010static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2011 int attr)
2012{
2013 struct nlattr *rate;
2014 u16 bitrate;
2015
2016 rate = nla_nest_start(msg, attr);
2017 if (!rate)
2018 goto nla_put_failure;
2019
2020 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2021 bitrate = cfg80211_calculate_bitrate(info);
2022 if (bitrate > 0)
2023 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2024
2025 if (info->flags & RATE_INFO_FLAGS_MCS)
2026 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2027 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2028 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2029 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2030 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2031
2032 nla_nest_end(msg, rate);
2033 return true;
2034
2035nla_put_failure:
2036 return false;
2037}
2038
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002039static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2040 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002041 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002042{
2043 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002044 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002045
2046 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2047 if (!hdr)
2048 return -1;
2049
2050 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2051 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2052
Johannes Bergf5ea9122009-08-07 16:17:38 +02002053 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2054
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002055 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2056 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002057 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302058 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2059 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2060 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002061 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2062 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2063 sinfo->inactive_time);
2064 if (sinfo->filled & STATION_INFO_RX_BYTES)
2065 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2066 sinfo->rx_bytes);
2067 if (sinfo->filled & STATION_INFO_TX_BYTES)
2068 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2069 sinfo->tx_bytes);
2070 if (sinfo->filled & STATION_INFO_LLID)
2071 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2072 sinfo->llid);
2073 if (sinfo->filled & STATION_INFO_PLID)
2074 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2075 sinfo->plid);
2076 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2077 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2078 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002079 if (sinfo->filled & STATION_INFO_SIGNAL)
2080 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2081 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002082 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2083 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2084 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002085 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002086 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2087 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002088 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002089 }
2090 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2091 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2092 NL80211_STA_INFO_RX_BITRATE))
2093 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002094 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002095 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2096 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2097 sinfo->rx_packets);
2098 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2099 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2100 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002101 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2102 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2103 sinfo->tx_retries);
2104 if (sinfo->filled & STATION_INFO_TX_FAILED)
2105 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2106 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002107 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2108 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2109 if (!bss_param)
2110 goto nla_put_failure;
2111
2112 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2113 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2114 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2115 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2116 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2117 NLA_PUT_FLAG(msg,
2118 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2119 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2120 sinfo->bss_param.dtim_period);
2121 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2122 sinfo->bss_param.beacon_interval);
2123
2124 nla_nest_end(msg, bss_param);
2125 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002126 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002127
2128 return genlmsg_end(msg, hdr);
2129
2130 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002131 genlmsg_cancel(msg, hdr);
2132 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002133}
2134
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002135static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002136 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002137{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002138 struct station_info sinfo;
2139 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002140 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002141 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002142 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002143 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002144
Johannes Berg67748892010-10-04 21:14:06 +02002145 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2146 if (err)
2147 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002148
2149 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002150 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002151 goto out_err;
2152 }
2153
Johannes Bergbba95fe2008-07-29 13:22:51 +02002154 while (1) {
2155 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2156 mac_addr, &sinfo);
2157 if (err == -ENOENT)
2158 break;
2159 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002160 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002161
2162 if (nl80211_send_station(skb,
2163 NETLINK_CB(cb->skb).pid,
2164 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2165 netdev, mac_addr,
2166 &sinfo) < 0)
2167 goto out;
2168
2169 sta_idx++;
2170 }
2171
2172
2173 out:
2174 cb->args[1] = sta_idx;
2175 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002176 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002177 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002178
2179 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002180}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002181
Johannes Berg5727ef12007-12-19 02:03:34 +01002182static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2183{
Johannes Berg4c476992010-10-04 21:36:35 +02002184 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2185 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002186 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002187 struct sk_buff *msg;
2188 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002189 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002190
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002191 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002192
2193 if (!info->attrs[NL80211_ATTR_MAC])
2194 return -EINVAL;
2195
2196 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2197
Johannes Berg4c476992010-10-04 21:36:35 +02002198 if (!rdev->ops->get_station)
2199 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002200
Johannes Berg79c97e92009-07-07 03:56:12 +02002201 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002202 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002203 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002204
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002205 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002206 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002207 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002208
2209 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002210 dev, mac_addr, &sinfo) < 0) {
2211 nlmsg_free(msg);
2212 return -ENOBUFS;
2213 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002214
Johannes Berg4c476992010-10-04 21:36:35 +02002215 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002216}
2217
2218/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002219 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002220 */
Johannes Berg463d0182009-07-14 00:33:35 +02002221static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002222 struct cfg80211_registered_device *rdev,
2223 struct net_device **vlan)
2224{
Johannes Berg463d0182009-07-14 00:33:35 +02002225 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002226 *vlan = NULL;
2227
2228 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002229 *vlan = dev_get_by_index(genl_info_net(info),
2230 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002231 if (!*vlan)
2232 return -ENODEV;
2233 if (!(*vlan)->ieee80211_ptr)
2234 return -EINVAL;
2235 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2236 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002237 if (!netif_running(*vlan))
2238 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002239 }
2240 return 0;
2241}
2242
2243static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2244{
Johannes Berg4c476992010-10-04 21:36:35 +02002245 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002246 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002247 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002248 struct station_parameters params;
2249 u8 *mac_addr = NULL;
2250
2251 memset(&params, 0, sizeof(params));
2252
2253 params.listen_interval = -1;
Javier Cardona9c3990a2011-05-03 16:57:11 -07002254 params.plink_state = PLINK_INVALID;
Johannes Berg5727ef12007-12-19 02:03:34 +01002255
2256 if (info->attrs[NL80211_ATTR_STA_AID])
2257 return -EINVAL;
2258
2259 if (!info->attrs[NL80211_ATTR_MAC])
2260 return -EINVAL;
2261
2262 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2263
2264 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2265 params.supported_rates =
2266 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2267 params.supported_rates_len =
2268 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2269 }
2270
2271 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2272 params.listen_interval =
2273 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2274
Jouni Malinen36aedc92008-08-25 11:58:58 +03002275 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2276 params.ht_capa =
2277 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2278
Johannes Bergeccb8e82009-05-11 21:57:56 +03002279 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002280 return -EINVAL;
2281
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002282 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2283 params.plink_action =
2284 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2285
Javier Cardona9c3990a2011-05-03 16:57:11 -07002286 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2287 params.plink_state =
2288 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2289
Johannes Berg463d0182009-07-14 00:33:35 +02002290 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002291 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002292 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002293
2294 /* validate settings */
2295 err = 0;
2296
2297 switch (dev->ieee80211_ptr->iftype) {
2298 case NL80211_IFTYPE_AP:
2299 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002300 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002301 /* disallow mesh-specific things */
2302 if (params.plink_action)
2303 err = -EINVAL;
2304 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002305 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002306 case NL80211_IFTYPE_STATION:
2307 /* disallow everything but AUTHORIZED flag */
2308 if (params.plink_action)
2309 err = -EINVAL;
2310 if (params.vlan)
2311 err = -EINVAL;
2312 if (params.supported_rates)
2313 err = -EINVAL;
2314 if (params.ht_capa)
2315 err = -EINVAL;
2316 if (params.listen_interval >= 0)
2317 err = -EINVAL;
2318 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2319 err = -EINVAL;
2320 break;
2321 case NL80211_IFTYPE_MESH_POINT:
2322 /* disallow things mesh doesn't support */
2323 if (params.vlan)
2324 err = -EINVAL;
2325 if (params.ht_capa)
2326 err = -EINVAL;
2327 if (params.listen_interval >= 0)
2328 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002329 if (params.sta_flags_mask &
2330 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002331 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002332 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002333 err = -EINVAL;
2334 break;
2335 default:
2336 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002337 }
2338
Johannes Berg5727ef12007-12-19 02:03:34 +01002339 if (err)
2340 goto out;
2341
Johannes Berg79c97e92009-07-07 03:56:12 +02002342 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002343 err = -EOPNOTSUPP;
2344 goto out;
2345 }
2346
Johannes Berg79c97e92009-07-07 03:56:12 +02002347 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002348
2349 out:
2350 if (params.vlan)
2351 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002352
Johannes Berg5727ef12007-12-19 02:03:34 +01002353 return err;
2354}
2355
2356static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2357{
Johannes Berg4c476992010-10-04 21:36:35 +02002358 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002359 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002360 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002361 struct station_parameters params;
2362 u8 *mac_addr = NULL;
2363
2364 memset(&params, 0, sizeof(params));
2365
2366 if (!info->attrs[NL80211_ATTR_MAC])
2367 return -EINVAL;
2368
Johannes Berg5727ef12007-12-19 02:03:34 +01002369 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2370 return -EINVAL;
2371
2372 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2373 return -EINVAL;
2374
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002375 if (!info->attrs[NL80211_ATTR_STA_AID])
2376 return -EINVAL;
2377
Johannes Berg5727ef12007-12-19 02:03:34 +01002378 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2379 params.supported_rates =
2380 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2381 params.supported_rates_len =
2382 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2383 params.listen_interval =
2384 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002385
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002386 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2387 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2388 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002389
Jouni Malinen36aedc92008-08-25 11:58:58 +03002390 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2391 params.ht_capa =
2392 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002393
Javier Cardona96b78df2011-04-07 15:08:33 -07002394 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2395 params.plink_action =
2396 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2397
Johannes Bergeccb8e82009-05-11 21:57:56 +03002398 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002399 return -EINVAL;
2400
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002401 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002402 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002403 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002404 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2405 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002406
Johannes Berg463d0182009-07-14 00:33:35 +02002407 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002408 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002409 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002410
2411 /* validate settings */
2412 err = 0;
2413
Johannes Berg79c97e92009-07-07 03:56:12 +02002414 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002415 err = -EOPNOTSUPP;
2416 goto out;
2417 }
2418
Johannes Berg79c97e92009-07-07 03:56:12 +02002419 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002420
2421 out:
2422 if (params.vlan)
2423 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002424 return err;
2425}
2426
2427static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2428{
Johannes Berg4c476992010-10-04 21:36:35 +02002429 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2430 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002431 u8 *mac_addr = NULL;
2432
2433 if (info->attrs[NL80211_ATTR_MAC])
2434 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2435
Johannes Berge80cf852009-05-11 14:43:13 +02002436 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002437 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002438 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002439 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2440 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002441
Johannes Berg4c476992010-10-04 21:36:35 +02002442 if (!rdev->ops->del_station)
2443 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002444
Johannes Berg4c476992010-10-04 21:36:35 +02002445 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002446}
2447
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002448static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2449 int flags, struct net_device *dev,
2450 u8 *dst, u8 *next_hop,
2451 struct mpath_info *pinfo)
2452{
2453 void *hdr;
2454 struct nlattr *pinfoattr;
2455
2456 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2457 if (!hdr)
2458 return -1;
2459
2460 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2461 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2462 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2463
Johannes Bergf5ea9122009-08-07 16:17:38 +02002464 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2465
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002466 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2467 if (!pinfoattr)
2468 goto nla_put_failure;
2469 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2470 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2471 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002472 if (pinfo->filled & MPATH_INFO_SN)
2473 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2474 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002475 if (pinfo->filled & MPATH_INFO_METRIC)
2476 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2477 pinfo->metric);
2478 if (pinfo->filled & MPATH_INFO_EXPTIME)
2479 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2480 pinfo->exptime);
2481 if (pinfo->filled & MPATH_INFO_FLAGS)
2482 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2483 pinfo->flags);
2484 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2485 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2486 pinfo->discovery_timeout);
2487 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2488 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2489 pinfo->discovery_retries);
2490
2491 nla_nest_end(msg, pinfoattr);
2492
2493 return genlmsg_end(msg, hdr);
2494
2495 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002496 genlmsg_cancel(msg, hdr);
2497 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002498}
2499
2500static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002501 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002502{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002503 struct mpath_info pinfo;
2504 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002505 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002506 u8 dst[ETH_ALEN];
2507 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002508 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002509 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002510
Johannes Berg67748892010-10-04 21:14:06 +02002511 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2512 if (err)
2513 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002514
2515 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002516 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002517 goto out_err;
2518 }
2519
Jouni Malineneec60b02009-03-20 21:21:19 +02002520 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2521 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002522 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002523 }
2524
Johannes Bergbba95fe2008-07-29 13:22:51 +02002525 while (1) {
2526 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2527 dst, next_hop, &pinfo);
2528 if (err == -ENOENT)
2529 break;
2530 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002531 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002532
2533 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2534 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2535 netdev, dst, next_hop,
2536 &pinfo) < 0)
2537 goto out;
2538
2539 path_idx++;
2540 }
2541
2542
2543 out:
2544 cb->args[1] = path_idx;
2545 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002546 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002547 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002548 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002549}
2550
2551static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2552{
Johannes Berg4c476992010-10-04 21:36:35 +02002553 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002554 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002555 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002556 struct mpath_info pinfo;
2557 struct sk_buff *msg;
2558 u8 *dst = NULL;
2559 u8 next_hop[ETH_ALEN];
2560
2561 memset(&pinfo, 0, sizeof(pinfo));
2562
2563 if (!info->attrs[NL80211_ATTR_MAC])
2564 return -EINVAL;
2565
2566 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2567
Johannes Berg4c476992010-10-04 21:36:35 +02002568 if (!rdev->ops->get_mpath)
2569 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002570
Johannes Berg4c476992010-10-04 21:36:35 +02002571 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2572 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002573
Johannes Berg79c97e92009-07-07 03:56:12 +02002574 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002575 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002576 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002577
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002578 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002579 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002580 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002581
2582 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002583 dev, dst, next_hop, &pinfo) < 0) {
2584 nlmsg_free(msg);
2585 return -ENOBUFS;
2586 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002587
Johannes Berg4c476992010-10-04 21:36:35 +02002588 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002589}
2590
2591static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2592{
Johannes Berg4c476992010-10-04 21:36:35 +02002593 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2594 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002595 u8 *dst = NULL;
2596 u8 *next_hop = NULL;
2597
2598 if (!info->attrs[NL80211_ATTR_MAC])
2599 return -EINVAL;
2600
2601 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2602 return -EINVAL;
2603
2604 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2605 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2606
Johannes Berg4c476992010-10-04 21:36:35 +02002607 if (!rdev->ops->change_mpath)
2608 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002609
Johannes Berg4c476992010-10-04 21:36:35 +02002610 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2611 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002612
Johannes Berg4c476992010-10-04 21:36:35 +02002613 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002614}
Johannes Berg4c476992010-10-04 21:36:35 +02002615
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002616static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2617{
Johannes Berg4c476992010-10-04 21:36:35 +02002618 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2619 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002620 u8 *dst = NULL;
2621 u8 *next_hop = NULL;
2622
2623 if (!info->attrs[NL80211_ATTR_MAC])
2624 return -EINVAL;
2625
2626 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2627 return -EINVAL;
2628
2629 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2630 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2631
Johannes Berg4c476992010-10-04 21:36:35 +02002632 if (!rdev->ops->add_mpath)
2633 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002634
Johannes Berg4c476992010-10-04 21:36:35 +02002635 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2636 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002637
Johannes Berg4c476992010-10-04 21:36:35 +02002638 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002639}
2640
2641static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2642{
Johannes Berg4c476992010-10-04 21:36:35 +02002643 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2644 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002645 u8 *dst = NULL;
2646
2647 if (info->attrs[NL80211_ATTR_MAC])
2648 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2649
Johannes Berg4c476992010-10-04 21:36:35 +02002650 if (!rdev->ops->del_mpath)
2651 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002652
Johannes Berg4c476992010-10-04 21:36:35 +02002653 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002654}
2655
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002656static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2657{
Johannes Berg4c476992010-10-04 21:36:35 +02002658 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2659 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002660 struct bss_parameters params;
2661
2662 memset(&params, 0, sizeof(params));
2663 /* default to not changing parameters */
2664 params.use_cts_prot = -1;
2665 params.use_short_preamble = -1;
2666 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002667 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002668 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002669
2670 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2671 params.use_cts_prot =
2672 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2673 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2674 params.use_short_preamble =
2675 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2676 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2677 params.use_short_slot_time =
2678 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002679 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2680 params.basic_rates =
2681 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2682 params.basic_rates_len =
2683 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2684 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002685 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2686 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002687 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2688 params.ht_opmode =
2689 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002690
Johannes Berg4c476992010-10-04 21:36:35 +02002691 if (!rdev->ops->change_bss)
2692 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002693
Johannes Berg074ac8d2010-09-16 14:58:22 +02002694 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002695 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2696 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002697
Johannes Berg4c476992010-10-04 21:36:35 +02002698 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002699}
2700
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002701static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002702 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2703 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2704 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2705 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2706 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2707 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2708};
2709
2710static int parse_reg_rule(struct nlattr *tb[],
2711 struct ieee80211_reg_rule *reg_rule)
2712{
2713 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2714 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2715
2716 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2717 return -EINVAL;
2718 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2719 return -EINVAL;
2720 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2721 return -EINVAL;
2722 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2723 return -EINVAL;
2724 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2725 return -EINVAL;
2726
2727 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2728
2729 freq_range->start_freq_khz =
2730 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2731 freq_range->end_freq_khz =
2732 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2733 freq_range->max_bandwidth_khz =
2734 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2735
2736 power_rule->max_eirp =
2737 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2738
2739 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2740 power_rule->max_antenna_gain =
2741 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2742
2743 return 0;
2744}
2745
2746static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2747{
2748 int r;
2749 char *data = NULL;
2750
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002751 /*
2752 * You should only get this when cfg80211 hasn't yet initialized
2753 * completely when built-in to the kernel right between the time
2754 * window between nl80211_init() and regulatory_init(), if that is
2755 * even possible.
2756 */
2757 mutex_lock(&cfg80211_mutex);
2758 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002759 mutex_unlock(&cfg80211_mutex);
2760 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002761 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002762 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002763
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002764 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2765 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002766
2767 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2768
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002769 r = regulatory_hint_user(data);
2770
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002771 return r;
2772}
2773
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002774static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002775 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002776{
Johannes Berg4c476992010-10-04 21:36:35 +02002777 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02002778 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002779 struct wireless_dev *wdev = dev->ieee80211_ptr;
2780 struct mesh_config cur_params;
2781 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002782 void *hdr;
2783 struct nlattr *pinfoattr;
2784 struct sk_buff *msg;
2785
Johannes Berg29cbe682010-12-03 09:20:44 +01002786 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2787 return -EOPNOTSUPP;
2788
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002789 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02002790 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002791
Johannes Berg29cbe682010-12-03 09:20:44 +01002792 wdev_lock(wdev);
2793 /* If not connected, get default parameters */
2794 if (!wdev->mesh_id_len)
2795 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
2796 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002797 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01002798 &cur_params);
2799 wdev_unlock(wdev);
2800
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002801 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002802 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002803
2804 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002805 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002806 if (!msg)
2807 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002808 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002809 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002810 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01002811 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002812 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002813 if (!pinfoattr)
2814 goto nla_put_failure;
2815 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2816 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2817 cur_params.dot11MeshRetryTimeout);
2818 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2819 cur_params.dot11MeshConfirmTimeout);
2820 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2821 cur_params.dot11MeshHoldingTimeout);
2822 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2823 cur_params.dot11MeshMaxPeerLinks);
2824 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2825 cur_params.dot11MeshMaxRetries);
2826 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2827 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01002828 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
2829 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002830 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2831 cur_params.auto_open_plinks);
2832 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2833 cur_params.dot11MeshHWMPmaxPREQretries);
2834 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2835 cur_params.path_refresh_time);
2836 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2837 cur_params.min_discovery_timeout);
2838 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2839 cur_params.dot11MeshHWMPactivePathTimeout);
2840 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2841 cur_params.dot11MeshHWMPpreqMinInterval);
2842 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2843 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002844 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2845 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002846 nla_nest_end(msg, pinfoattr);
2847 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002848 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002849
Johannes Berg3b858752009-03-12 09:55:09 +01002850 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002851 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01002852 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04002853 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002854 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002855}
2856
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002857static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002858 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2859 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2860 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2861 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2862 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2863 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01002864 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002865 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2866
2867 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2868 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2869 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2870 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2871 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2872 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2873};
2874
Javier Cardonac80d5452010-12-16 17:37:49 -08002875static const struct nla_policy
2876 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
2877 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
2878 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07002879 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07002880 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08002881 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07002882 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08002883};
2884
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002885static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002886 struct mesh_config *cfg,
2887 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002888{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002889 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002890 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002891
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002892#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2893do {\
2894 if (table[attr_num]) {\
2895 cfg->param = nla_fn(table[attr_num]); \
2896 mask |= (1 << (attr_num - 1)); \
2897 } \
2898} while (0);\
2899
2900
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002901 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002902 return -EINVAL;
2903 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002904 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002905 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002906 return -EINVAL;
2907
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002908 /* This makes sure that there aren't more than 32 mesh config
2909 * parameters (otherwise our bitfield scheme would not work.) */
2910 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2911
2912 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2914 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2915 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2916 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2917 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2918 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2920 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2921 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2922 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2923 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2924 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01002925 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
2926 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002927 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2928 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2929 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2930 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2931 nla_get_u8);
2932 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2933 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2934 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2935 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2936 nla_get_u16);
2937 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2938 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2939 nla_get_u32);
2940 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2941 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2942 nla_get_u16);
2943 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2944 dot11MeshHWMPnetDiameterTraversalTime,
2945 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2946 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002947 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2948 dot11MeshHWMPRootMode, mask,
2949 NL80211_MESHCONF_HWMP_ROOTMODE,
2950 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002951 if (mask_out)
2952 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08002953
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002954 return 0;
2955
2956#undef FILL_IN_MESH_PARAM_IF_SET
2957}
2958
Javier Cardonac80d5452010-12-16 17:37:49 -08002959static int nl80211_parse_mesh_setup(struct genl_info *info,
2960 struct mesh_setup *setup)
2961{
2962 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
2963
2964 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
2965 return -EINVAL;
2966 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
2967 info->attrs[NL80211_ATTR_MESH_SETUP],
2968 nl80211_mesh_setup_params_policy))
2969 return -EINVAL;
2970
2971 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
2972 setup->path_sel_proto =
2973 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
2974 IEEE80211_PATH_PROTOCOL_VENDOR :
2975 IEEE80211_PATH_PROTOCOL_HWMP;
2976
2977 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
2978 setup->path_metric =
2979 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
2980 IEEE80211_PATH_METRIC_VENDOR :
2981 IEEE80211_PATH_METRIC_AIRTIME;
2982
Javier Cardona581a8b02011-04-07 15:08:27 -07002983
2984 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08002985 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07002986 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08002987 if (!is_valid_ie_attr(ieattr))
2988 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07002989 setup->ie = nla_data(ieattr);
2990 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08002991 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07002992 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
2993 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08002994
2995 return 0;
2996}
2997
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002998static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002999 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003000{
3001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3002 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003003 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003004 struct mesh_config cfg;
3005 u32 mask;
3006 int err;
3007
Johannes Berg29cbe682010-12-03 09:20:44 +01003008 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3009 return -EOPNOTSUPP;
3010
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003011 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003012 return -EOPNOTSUPP;
3013
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003014 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003015 if (err)
3016 return err;
3017
Johannes Berg29cbe682010-12-03 09:20:44 +01003018 wdev_lock(wdev);
3019 if (!wdev->mesh_id_len)
3020 err = -ENOLINK;
3021
3022 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003023 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003024 mask, &cfg);
3025
3026 wdev_unlock(wdev);
3027
3028 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003029}
3030
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003031static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3032{
3033 struct sk_buff *msg;
3034 void *hdr = NULL;
3035 struct nlattr *nl_reg_rules;
3036 unsigned int i;
3037 int err = -EINVAL;
3038
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003039 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003040
3041 if (!cfg80211_regdomain)
3042 goto out;
3043
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003044 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003045 if (!msg) {
3046 err = -ENOBUFS;
3047 goto out;
3048 }
3049
3050 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3051 NL80211_CMD_GET_REG);
3052 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003053 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003054
3055 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3056 cfg80211_regdomain->alpha2);
3057
3058 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3059 if (!nl_reg_rules)
3060 goto nla_put_failure;
3061
3062 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3063 struct nlattr *nl_reg_rule;
3064 const struct ieee80211_reg_rule *reg_rule;
3065 const struct ieee80211_freq_range *freq_range;
3066 const struct ieee80211_power_rule *power_rule;
3067
3068 reg_rule = &cfg80211_regdomain->reg_rules[i];
3069 freq_range = &reg_rule->freq_range;
3070 power_rule = &reg_rule->power_rule;
3071
3072 nl_reg_rule = nla_nest_start(msg, i);
3073 if (!nl_reg_rule)
3074 goto nla_put_failure;
3075
3076 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3077 reg_rule->flags);
3078 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3079 freq_range->start_freq_khz);
3080 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3081 freq_range->end_freq_khz);
3082 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3083 freq_range->max_bandwidth_khz);
3084 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3085 power_rule->max_antenna_gain);
3086 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3087 power_rule->max_eirp);
3088
3089 nla_nest_end(msg, nl_reg_rule);
3090 }
3091
3092 nla_nest_end(msg, nl_reg_rules);
3093
3094 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003095 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003096 goto out;
3097
3098nla_put_failure:
3099 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003100put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003101 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003102 err = -EMSGSIZE;
3103out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003104 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003105 return err;
3106}
3107
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003108static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3109{
3110 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3111 struct nlattr *nl_reg_rule;
3112 char *alpha2 = NULL;
3113 int rem_reg_rules = 0, r = 0;
3114 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3115 struct ieee80211_regdomain *rd = NULL;
3116
3117 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3118 return -EINVAL;
3119
3120 if (!info->attrs[NL80211_ATTR_REG_RULES])
3121 return -EINVAL;
3122
3123 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3124
3125 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3126 rem_reg_rules) {
3127 num_rules++;
3128 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003129 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003130 }
3131
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003132 mutex_lock(&cfg80211_mutex);
3133
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003134 if (!reg_is_valid_request(alpha2)) {
3135 r = -EINVAL;
3136 goto bad_reg;
3137 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003138
3139 size_of_regd = sizeof(struct ieee80211_regdomain) +
3140 (num_rules * sizeof(struct ieee80211_reg_rule));
3141
3142 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003143 if (!rd) {
3144 r = -ENOMEM;
3145 goto bad_reg;
3146 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003147
3148 rd->n_reg_rules = num_rules;
3149 rd->alpha2[0] = alpha2[0];
3150 rd->alpha2[1] = alpha2[1];
3151
3152 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3153 rem_reg_rules) {
3154 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3155 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3156 reg_rule_policy);
3157 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3158 if (r)
3159 goto bad_reg;
3160
3161 rule_idx++;
3162
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003163 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3164 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003165 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003166 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003167 }
3168
3169 BUG_ON(rule_idx != num_rules);
3170
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003171 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003172
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003173 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003174
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003175 return r;
3176
Johannes Bergd2372b32008-10-24 20:32:20 +02003177 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003178 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003179 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003180 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003181}
3182
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003183static int validate_scan_freqs(struct nlattr *freqs)
3184{
3185 struct nlattr *attr1, *attr2;
3186 int n_channels = 0, tmp1, tmp2;
3187
3188 nla_for_each_nested(attr1, freqs, tmp1) {
3189 n_channels++;
3190 /*
3191 * Some hardware has a limited channel list for
3192 * scanning, and it is pretty much nonsensical
3193 * to scan for a channel twice, so disallow that
3194 * and don't require drivers to check that the
3195 * channel list they get isn't longer than what
3196 * they can scan, as long as they can scan all
3197 * the channels they registered at once.
3198 */
3199 nla_for_each_nested(attr2, freqs, tmp2)
3200 if (attr1 != attr2 &&
3201 nla_get_u32(attr1) == nla_get_u32(attr2))
3202 return 0;
3203 }
3204
3205 return n_channels;
3206}
3207
Johannes Berg2a519312009-02-10 21:25:55 +01003208static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3209{
Johannes Berg4c476992010-10-04 21:36:35 +02003210 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3211 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003212 struct cfg80211_scan_request *request;
3213 struct cfg80211_ssid *ssid;
3214 struct ieee80211_channel *channel;
3215 struct nlattr *attr;
3216 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003217 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01003218 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003219 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003220
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003221 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3222 return -EINVAL;
3223
Johannes Berg79c97e92009-07-07 03:56:12 +02003224 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003225
Johannes Berg4c476992010-10-04 21:36:35 +02003226 if (!rdev->ops->scan)
3227 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003228
Johannes Berg4c476992010-10-04 21:36:35 +02003229 if (rdev->scan_req)
3230 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003231
3232 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003233 n_channels = validate_scan_freqs(
3234 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003235 if (!n_channels)
3236 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003237 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003238 n_channels = 0;
3239
Johannes Berg2a519312009-02-10 21:25:55 +01003240 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3241 if (wiphy->bands[band])
3242 n_channels += wiphy->bands[band]->n_channels;
3243 }
3244
3245 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3246 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3247 n_ssids++;
3248
Johannes Berg4c476992010-10-04 21:36:35 +02003249 if (n_ssids > wiphy->max_scan_ssids)
3250 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003251
Jouni Malinen70692ad2009-02-16 19:39:13 +02003252 if (info->attrs[NL80211_ATTR_IE])
3253 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3254 else
3255 ie_len = 0;
3256
Johannes Berg4c476992010-10-04 21:36:35 +02003257 if (ie_len > wiphy->max_scan_ie_len)
3258 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003259
Johannes Berg2a519312009-02-10 21:25:55 +01003260 request = kzalloc(sizeof(*request)
3261 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003262 + sizeof(channel) * n_channels
3263 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003264 if (!request)
3265 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003266
Johannes Berg2a519312009-02-10 21:25:55 +01003267 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003268 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003269 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003270 if (ie_len) {
3271 if (request->ssids)
3272 request->ie = (void *)(request->ssids + n_ssids);
3273 else
3274 request->ie = (void *)(request->channels + n_channels);
3275 }
Johannes Berg2a519312009-02-10 21:25:55 +01003276
Johannes Berg584991d2009-11-02 13:32:03 +01003277 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003278 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3279 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003280 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003281 struct ieee80211_channel *chan;
3282
3283 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3284
3285 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003286 err = -EINVAL;
3287 goto out_free;
3288 }
Johannes Berg584991d2009-11-02 13:32:03 +01003289
3290 /* ignore disabled channels */
3291 if (chan->flags & IEEE80211_CHAN_DISABLED)
3292 continue;
3293
3294 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003295 i++;
3296 }
3297 } else {
3298 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003299 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3300 int j;
3301 if (!wiphy->bands[band])
3302 continue;
3303 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003304 struct ieee80211_channel *chan;
3305
3306 chan = &wiphy->bands[band]->channels[j];
3307
3308 if (chan->flags & IEEE80211_CHAN_DISABLED)
3309 continue;
3310
3311 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003312 i++;
3313 }
3314 }
3315 }
3316
Johannes Berg584991d2009-11-02 13:32:03 +01003317 if (!i) {
3318 err = -EINVAL;
3319 goto out_free;
3320 }
3321
3322 request->n_channels = i;
3323
Johannes Berg2a519312009-02-10 21:25:55 +01003324 i = 0;
3325 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3326 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3327 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3328 err = -EINVAL;
3329 goto out_free;
3330 }
3331 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3332 request->ssids[i].ssid_len = nla_len(attr);
3333 i++;
3334 }
3335 }
3336
Jouni Malinen70692ad2009-02-16 19:39:13 +02003337 if (info->attrs[NL80211_ATTR_IE]) {
3338 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003339 memcpy((void *)request->ie,
3340 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003341 request->ie_len);
3342 }
3343
Johannes Berg463d0182009-07-14 00:33:35 +02003344 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003345 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003346
Johannes Berg79c97e92009-07-07 03:56:12 +02003347 rdev->scan_req = request;
3348 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003349
Johannes Berg463d0182009-07-14 00:33:35 +02003350 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003351 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003352 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003353 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003354 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003355 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003356 kfree(request);
3357 }
Johannes Berg3b858752009-03-12 09:55:09 +01003358
Johannes Berg2a519312009-02-10 21:25:55 +01003359 return err;
3360}
3361
Luciano Coelho807f8a82011-05-11 17:09:35 +03003362static int nl80211_start_sched_scan(struct sk_buff *skb,
3363 struct genl_info *info)
3364{
3365 struct cfg80211_sched_scan_request *request;
3366 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3367 struct net_device *dev = info->user_ptr[1];
3368 struct cfg80211_ssid *ssid;
3369 struct ieee80211_channel *channel;
3370 struct nlattr *attr;
3371 struct wiphy *wiphy;
3372 int err, tmp, n_ssids = 0, n_channels, i;
3373 enum ieee80211_band band;
3374 size_t ie_len;
3375
3376 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3377 !rdev->ops->sched_scan_start)
3378 return -EOPNOTSUPP;
3379
3380 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3381 return -EINVAL;
3382
3383 if (rdev->sched_scan_req)
3384 return -EINPROGRESS;
3385
3386 wiphy = &rdev->wiphy;
3387
3388 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3389 n_channels = validate_scan_freqs(
3390 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3391 if (!n_channels)
3392 return -EINVAL;
3393 } else {
3394 n_channels = 0;
3395
3396 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3397 if (wiphy->bands[band])
3398 n_channels += wiphy->bands[band]->n_channels;
3399 }
3400
3401 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3402 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3403 tmp)
3404 n_ssids++;
3405
3406 if (n_ssids > wiphy->max_scan_ssids)
3407 return -EINVAL;
3408
3409 if (info->attrs[NL80211_ATTR_IE])
3410 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3411 else
3412 ie_len = 0;
3413
3414 if (ie_len > wiphy->max_scan_ie_len)
3415 return -EINVAL;
3416
3417 request = kzalloc(sizeof(*request)
3418 + sizeof(*ssid) * n_ssids
3419 + sizeof(channel) * n_channels
3420 + ie_len, GFP_KERNEL);
3421 if (!request)
3422 return -ENOMEM;
3423
3424 if (n_ssids)
3425 request->ssids = (void *)&request->channels[n_channels];
3426 request->n_ssids = n_ssids;
3427 if (ie_len) {
3428 if (request->ssids)
3429 request->ie = (void *)(request->ssids + n_ssids);
3430 else
3431 request->ie = (void *)(request->channels + n_channels);
3432 }
3433
3434 i = 0;
3435 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3436 /* user specified, bail out if channel not found */
3437 nla_for_each_nested(attr,
3438 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3439 tmp) {
3440 struct ieee80211_channel *chan;
3441
3442 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3443
3444 if (!chan) {
3445 err = -EINVAL;
3446 goto out_free;
3447 }
3448
3449 /* ignore disabled channels */
3450 if (chan->flags & IEEE80211_CHAN_DISABLED)
3451 continue;
3452
3453 request->channels[i] = chan;
3454 i++;
3455 }
3456 } else {
3457 /* all channels */
3458 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3459 int j;
3460 if (!wiphy->bands[band])
3461 continue;
3462 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3463 struct ieee80211_channel *chan;
3464
3465 chan = &wiphy->bands[band]->channels[j];
3466
3467 if (chan->flags & IEEE80211_CHAN_DISABLED)
3468 continue;
3469
3470 request->channels[i] = chan;
3471 i++;
3472 }
3473 }
3474 }
3475
3476 if (!i) {
3477 err = -EINVAL;
3478 goto out_free;
3479 }
3480
3481 request->n_channels = i;
3482
3483 i = 0;
3484 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3485 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3486 tmp) {
3487 if (request->ssids[i].ssid_len >
3488 IEEE80211_MAX_SSID_LEN) {
3489 err = -EINVAL;
3490 goto out_free;
3491 }
3492 memcpy(request->ssids[i].ssid, nla_data(attr),
3493 nla_len(attr));
3494 request->ssids[i].ssid_len = nla_len(attr);
3495 i++;
3496 }
3497 }
3498
3499 if (info->attrs[NL80211_ATTR_IE]) {
3500 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3501 memcpy((void *)request->ie,
3502 nla_data(info->attrs[NL80211_ATTR_IE]),
3503 request->ie_len);
3504 }
3505
3506 request->dev = dev;
3507 request->wiphy = &rdev->wiphy;
3508
3509 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3510 if (!err) {
3511 rdev->sched_scan_req = request;
3512 nl80211_send_sched_scan(rdev, dev,
3513 NL80211_CMD_START_SCHED_SCAN);
3514 goto out;
3515 }
3516
3517out_free:
3518 kfree(request);
3519out:
3520 return err;
3521}
3522
3523static int nl80211_stop_sched_scan(struct sk_buff *skb,
3524 struct genl_info *info)
3525{
3526 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3527
3528 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3529 !rdev->ops->sched_scan_stop)
3530 return -EOPNOTSUPP;
3531
3532 return __cfg80211_stop_sched_scan(rdev, false);
3533}
3534
Johannes Berg2a519312009-02-10 21:25:55 +01003535static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3536 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003537 struct wireless_dev *wdev,
3538 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003539{
Johannes Berg48ab9052009-07-10 18:42:31 +02003540 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003541 void *hdr;
3542 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003543 int i;
3544
3545 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003546
3547 hdr = nl80211hdr_put(msg, pid, seq, flags,
3548 NL80211_CMD_NEW_SCAN_RESULTS);
3549 if (!hdr)
3550 return -1;
3551
Johannes Bergf5ea9122009-08-07 16:17:38 +02003552 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003553 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003554
3555 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3556 if (!bss)
3557 goto nla_put_failure;
3558 if (!is_zero_ether_addr(res->bssid))
3559 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3560 if (res->information_elements && res->len_information_elements)
3561 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3562 res->len_information_elements,
3563 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003564 if (res->beacon_ies && res->len_beacon_ies &&
3565 res->beacon_ies != res->information_elements)
3566 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3567 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003568 if (res->tsf)
3569 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3570 if (res->beacon_interval)
3571 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3572 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3573 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003574 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3575 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003576
Johannes Berg77965c92009-02-18 18:45:06 +01003577 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003578 case CFG80211_SIGNAL_TYPE_MBM:
3579 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3580 break;
3581 case CFG80211_SIGNAL_TYPE_UNSPEC:
3582 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3583 break;
3584 default:
3585 break;
3586 }
3587
Johannes Berg48ab9052009-07-10 18:42:31 +02003588 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003589 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003590 case NL80211_IFTYPE_STATION:
3591 if (intbss == wdev->current_bss)
3592 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3593 NL80211_BSS_STATUS_ASSOCIATED);
3594 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3595 if (intbss != wdev->auth_bsses[i])
3596 continue;
3597 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3598 NL80211_BSS_STATUS_AUTHENTICATED);
3599 break;
3600 }
3601 break;
3602 case NL80211_IFTYPE_ADHOC:
3603 if (intbss == wdev->current_bss)
3604 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3605 NL80211_BSS_STATUS_IBSS_JOINED);
3606 break;
3607 default:
3608 break;
3609 }
3610
Johannes Berg2a519312009-02-10 21:25:55 +01003611 nla_nest_end(msg, bss);
3612
3613 return genlmsg_end(msg, hdr);
3614
3615 nla_put_failure:
3616 genlmsg_cancel(msg, hdr);
3617 return -EMSGSIZE;
3618}
3619
3620static int nl80211_dump_scan(struct sk_buff *skb,
3621 struct netlink_callback *cb)
3622{
Johannes Berg48ab9052009-07-10 18:42:31 +02003623 struct cfg80211_registered_device *rdev;
3624 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003625 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003626 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003627 int start = cb->args[1], idx = 0;
3628 int err;
3629
Johannes Berg67748892010-10-04 21:14:06 +02003630 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3631 if (err)
3632 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003633
Johannes Berg48ab9052009-07-10 18:42:31 +02003634 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003635
Johannes Berg48ab9052009-07-10 18:42:31 +02003636 wdev_lock(wdev);
3637 spin_lock_bh(&rdev->bss_lock);
3638 cfg80211_bss_expire(rdev);
3639
3640 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003641 if (++idx <= start)
3642 continue;
3643 if (nl80211_send_bss(skb,
3644 NETLINK_CB(cb->skb).pid,
3645 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003646 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003647 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003648 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003649 }
3650 }
3651
Johannes Berg48ab9052009-07-10 18:42:31 +02003652 spin_unlock_bh(&rdev->bss_lock);
3653 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003654
3655 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003656 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003657
Johannes Berg67748892010-10-04 21:14:06 +02003658 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003659}
3660
Holger Schurig61fa7132009-11-11 12:25:40 +01003661static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3662 int flags, struct net_device *dev,
3663 struct survey_info *survey)
3664{
3665 void *hdr;
3666 struct nlattr *infoattr;
3667
3668 /* Survey without a channel doesn't make sense */
3669 if (!survey->channel)
3670 return -EINVAL;
3671
3672 hdr = nl80211hdr_put(msg, pid, seq, flags,
3673 NL80211_CMD_NEW_SURVEY_RESULTS);
3674 if (!hdr)
3675 return -ENOMEM;
3676
3677 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3678
3679 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3680 if (!infoattr)
3681 goto nla_put_failure;
3682
3683 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3684 survey->channel->center_freq);
3685 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3686 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3687 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003688 if (survey->filled & SURVEY_INFO_IN_USE)
3689 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02003690 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3691 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3692 survey->channel_time);
3693 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3694 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3695 survey->channel_time_busy);
3696 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3697 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3698 survey->channel_time_ext_busy);
3699 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3700 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3701 survey->channel_time_rx);
3702 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3703 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3704 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003705
3706 nla_nest_end(msg, infoattr);
3707
3708 return genlmsg_end(msg, hdr);
3709
3710 nla_put_failure:
3711 genlmsg_cancel(msg, hdr);
3712 return -EMSGSIZE;
3713}
3714
3715static int nl80211_dump_survey(struct sk_buff *skb,
3716 struct netlink_callback *cb)
3717{
3718 struct survey_info survey;
3719 struct cfg80211_registered_device *dev;
3720 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003721 int survey_idx = cb->args[1];
3722 int res;
3723
Johannes Berg67748892010-10-04 21:14:06 +02003724 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3725 if (res)
3726 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003727
3728 if (!dev->ops->dump_survey) {
3729 res = -EOPNOTSUPP;
3730 goto out_err;
3731 }
3732
3733 while (1) {
3734 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3735 &survey);
3736 if (res == -ENOENT)
3737 break;
3738 if (res)
3739 goto out_err;
3740
3741 if (nl80211_send_survey(skb,
3742 NETLINK_CB(cb->skb).pid,
3743 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3744 netdev,
3745 &survey) < 0)
3746 goto out;
3747 survey_idx++;
3748 }
3749
3750 out:
3751 cb->args[1] = survey_idx;
3752 res = skb->len;
3753 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003754 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003755 return res;
3756}
3757
Jouni Malinen255e7372009-03-20 21:21:17 +02003758static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3759{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003760 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003761}
3762
Samuel Ortizb23aa672009-07-01 21:26:54 +02003763static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3764{
3765 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3766 NL80211_WPA_VERSION_2));
3767}
3768
3769static bool nl80211_valid_akm_suite(u32 akm)
3770{
3771 return akm == WLAN_AKM_SUITE_8021X ||
3772 akm == WLAN_AKM_SUITE_PSK;
3773}
3774
3775static bool nl80211_valid_cipher_suite(u32 cipher)
3776{
3777 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3778 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3779 cipher == WLAN_CIPHER_SUITE_TKIP ||
3780 cipher == WLAN_CIPHER_SUITE_CCMP ||
3781 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3782}
3783
3784
Jouni Malinen636a5d32009-03-19 13:39:22 +02003785static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3786{
Johannes Berg4c476992010-10-04 21:36:35 +02003787 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3788 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003789 struct ieee80211_channel *chan;
3790 const u8 *bssid, *ssid, *ie = NULL;
3791 int err, ssid_len, ie_len = 0;
3792 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003793 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003794 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003795
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003796 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3797 return -EINVAL;
3798
3799 if (!info->attrs[NL80211_ATTR_MAC])
3800 return -EINVAL;
3801
Jouni Malinen17780922009-03-27 20:52:47 +02003802 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3803 return -EINVAL;
3804
Johannes Berg19957bb2009-07-02 17:20:43 +02003805 if (!info->attrs[NL80211_ATTR_SSID])
3806 return -EINVAL;
3807
3808 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3809 return -EINVAL;
3810
Johannes Bergfffd0932009-07-08 14:22:54 +02003811 err = nl80211_parse_key(info, &key);
3812 if (err)
3813 return err;
3814
3815 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003816 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3817 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003818 if (!key.p.key || !key.p.key_len)
3819 return -EINVAL;
3820 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3821 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3822 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3823 key.p.key_len != WLAN_KEY_LEN_WEP104))
3824 return -EINVAL;
3825 if (key.idx > 4)
3826 return -EINVAL;
3827 } else {
3828 key.p.key_len = 0;
3829 key.p.key = NULL;
3830 }
3831
Johannes Bergafea0b72010-08-10 09:46:42 +02003832 if (key.idx >= 0) {
3833 int i;
3834 bool ok = false;
3835 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3836 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3837 ok = true;
3838 break;
3839 }
3840 }
Johannes Berg4c476992010-10-04 21:36:35 +02003841 if (!ok)
3842 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003843 }
3844
Johannes Berg4c476992010-10-04 21:36:35 +02003845 if (!rdev->ops->auth)
3846 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003847
Johannes Berg074ac8d2010-09-16 14:58:22 +02003848 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003849 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3850 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003851
Johannes Berg19957bb2009-07-02 17:20:43 +02003852 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003853 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003854 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003855 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3856 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003857
Johannes Berg19957bb2009-07-02 17:20:43 +02003858 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3859 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3860
3861 if (info->attrs[NL80211_ATTR_IE]) {
3862 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3863 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3864 }
3865
3866 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003867 if (!nl80211_valid_auth_type(auth_type))
3868 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003869
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003870 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3871
Johannes Berg4c476992010-10-04 21:36:35 +02003872 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3873 ssid, ssid_len, ie, ie_len,
3874 key.p.key, key.p.key_len, key.idx,
3875 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003876}
3877
Johannes Bergc0692b82010-08-27 14:26:53 +03003878static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3879 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003880 struct cfg80211_crypto_settings *settings,
3881 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003882{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003883 memset(settings, 0, sizeof(*settings));
3884
Samuel Ortizb23aa672009-07-01 21:26:54 +02003885 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3886
Johannes Bergc0692b82010-08-27 14:26:53 +03003887 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3888 u16 proto;
3889 proto = nla_get_u16(
3890 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3891 settings->control_port_ethertype = cpu_to_be16(proto);
3892 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3893 proto != ETH_P_PAE)
3894 return -EINVAL;
3895 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3896 settings->control_port_no_encrypt = true;
3897 } else
3898 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3899
Samuel Ortizb23aa672009-07-01 21:26:54 +02003900 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3901 void *data;
3902 int len, i;
3903
3904 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3905 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3906 settings->n_ciphers_pairwise = len / sizeof(u32);
3907
3908 if (len % sizeof(u32))
3909 return -EINVAL;
3910
Johannes Berg3dc27d22009-07-02 21:36:37 +02003911 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003912 return -EINVAL;
3913
3914 memcpy(settings->ciphers_pairwise, data, len);
3915
3916 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3917 if (!nl80211_valid_cipher_suite(
3918 settings->ciphers_pairwise[i]))
3919 return -EINVAL;
3920 }
3921
3922 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3923 settings->cipher_group =
3924 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3925 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3926 return -EINVAL;
3927 }
3928
3929 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3930 settings->wpa_versions =
3931 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3932 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3933 return -EINVAL;
3934 }
3935
3936 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3937 void *data;
3938 int len, i;
3939
3940 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3941 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3942 settings->n_akm_suites = len / sizeof(u32);
3943
3944 if (len % sizeof(u32))
3945 return -EINVAL;
3946
3947 memcpy(settings->akm_suites, data, len);
3948
3949 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3950 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3951 return -EINVAL;
3952 }
3953
3954 return 0;
3955}
3956
Jouni Malinen636a5d32009-03-19 13:39:22 +02003957static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3958{
Johannes Berg4c476992010-10-04 21:36:35 +02003959 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3960 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003961 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003962 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003963 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003964 int err, ssid_len, ie_len = 0;
3965 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003966
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003967 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3968 return -EINVAL;
3969
3970 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003971 !info->attrs[NL80211_ATTR_SSID] ||
3972 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003973 return -EINVAL;
3974
Johannes Berg4c476992010-10-04 21:36:35 +02003975 if (!rdev->ops->assoc)
3976 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003977
Johannes Berg074ac8d2010-09-16 14:58:22 +02003978 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003979 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3980 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003981
Johannes Berg19957bb2009-07-02 17:20:43 +02003982 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003983
Johannes Berg19957bb2009-07-02 17:20:43 +02003984 chan = ieee80211_get_channel(&rdev->wiphy,
3985 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003986 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3987 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003988
Johannes Berg19957bb2009-07-02 17:20:43 +02003989 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3990 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003991
3992 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003993 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3994 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003995 }
3996
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003997 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003998 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003999 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004000 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004001 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004002 else if (mfp != NL80211_MFP_NO)
4003 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004004 }
4005
Johannes Berg3e5d7642009-07-07 14:37:26 +02004006 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4007 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4008
Johannes Bergc0692b82010-08-27 14:26:53 +03004009 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004010 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004011 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4012 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004013 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004014
Jouni Malinen636a5d32009-03-19 13:39:22 +02004015 return err;
4016}
4017
4018static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4019{
Johannes Berg4c476992010-10-04 21:36:35 +02004020 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4021 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004022 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004023 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004024 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004025 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004026
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004027 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4028 return -EINVAL;
4029
4030 if (!info->attrs[NL80211_ATTR_MAC])
4031 return -EINVAL;
4032
4033 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4034 return -EINVAL;
4035
Johannes Berg4c476992010-10-04 21:36:35 +02004036 if (!rdev->ops->deauth)
4037 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004038
Johannes Berg074ac8d2010-09-16 14:58:22 +02004039 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004040 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4041 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004042
Johannes Berg19957bb2009-07-02 17:20:43 +02004043 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004044
Johannes Berg19957bb2009-07-02 17:20:43 +02004045 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4046 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004047 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004048 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004049 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004050
4051 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004052 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4053 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004054 }
4055
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004056 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4057
Johannes Berg4c476992010-10-04 21:36:35 +02004058 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4059 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004060}
4061
4062static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4063{
Johannes Berg4c476992010-10-04 21:36:35 +02004064 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4065 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004066 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004067 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004068 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004069 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004070
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004071 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4072 return -EINVAL;
4073
4074 if (!info->attrs[NL80211_ATTR_MAC])
4075 return -EINVAL;
4076
4077 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4078 return -EINVAL;
4079
Johannes Berg4c476992010-10-04 21:36:35 +02004080 if (!rdev->ops->disassoc)
4081 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004082
Johannes Berg074ac8d2010-09-16 14:58:22 +02004083 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004084 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4085 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004086
Johannes Berg19957bb2009-07-02 17:20:43 +02004087 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004088
Johannes Berg19957bb2009-07-02 17:20:43 +02004089 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4090 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004091 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004092 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004093 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004094
4095 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004096 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4097 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004098 }
4099
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004100 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4101
Johannes Berg4c476992010-10-04 21:36:35 +02004102 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4103 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004104}
4105
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004106static bool
4107nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4108 int mcast_rate[IEEE80211_NUM_BANDS],
4109 int rateval)
4110{
4111 struct wiphy *wiphy = &rdev->wiphy;
4112 bool found = false;
4113 int band, i;
4114
4115 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4116 struct ieee80211_supported_band *sband;
4117
4118 sband = wiphy->bands[band];
4119 if (!sband)
4120 continue;
4121
4122 for (i = 0; i < sband->n_bitrates; i++) {
4123 if (sband->bitrates[i].bitrate == rateval) {
4124 mcast_rate[band] = i + 1;
4125 found = true;
4126 break;
4127 }
4128 }
4129 }
4130
4131 return found;
4132}
4133
Johannes Berg04a773a2009-04-19 21:24:32 +02004134static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4135{
Johannes Berg4c476992010-10-04 21:36:35 +02004136 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4137 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004138 struct cfg80211_ibss_params ibss;
4139 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004140 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004141 int err;
4142
Johannes Berg8e30bc52009-04-22 17:45:38 +02004143 memset(&ibss, 0, sizeof(ibss));
4144
Johannes Berg04a773a2009-04-19 21:24:32 +02004145 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4146 return -EINVAL;
4147
4148 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4149 !info->attrs[NL80211_ATTR_SSID] ||
4150 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4151 return -EINVAL;
4152
Johannes Berg8e30bc52009-04-22 17:45:38 +02004153 ibss.beacon_interval = 100;
4154
4155 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4156 ibss.beacon_interval =
4157 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4158 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4159 return -EINVAL;
4160 }
4161
Johannes Berg4c476992010-10-04 21:36:35 +02004162 if (!rdev->ops->join_ibss)
4163 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004164
Johannes Berg4c476992010-10-04 21:36:35 +02004165 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4166 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004167
Johannes Berg79c97e92009-07-07 03:56:12 +02004168 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004169
4170 if (info->attrs[NL80211_ATTR_MAC])
4171 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4172 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4173 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4174
4175 if (info->attrs[NL80211_ATTR_IE]) {
4176 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4177 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4178 }
4179
4180 ibss.channel = ieee80211_get_channel(wiphy,
4181 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4182 if (!ibss.channel ||
4183 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004184 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4185 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004186
4187 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004188 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004189
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004190 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4191 u8 *rates =
4192 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4193 int n_rates =
4194 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4195 struct ieee80211_supported_band *sband =
4196 wiphy->bands[ibss.channel->band];
4197 int i, j;
4198
Johannes Berg4c476992010-10-04 21:36:35 +02004199 if (n_rates == 0)
4200 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004201
4202 for (i = 0; i < n_rates; i++) {
4203 int rate = (rates[i] & 0x7f) * 5;
4204 bool found = false;
4205
4206 for (j = 0; j < sband->n_bitrates; j++) {
4207 if (sband->bitrates[j].bitrate == rate) {
4208 found = true;
4209 ibss.basic_rates |= BIT(j);
4210 break;
4211 }
4212 }
Johannes Berg4c476992010-10-04 21:36:35 +02004213 if (!found)
4214 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004215 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004216 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004217
4218 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4219 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4220 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4221 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004222
Johannes Berg4c476992010-10-04 21:36:35 +02004223 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4224 connkeys = nl80211_parse_connkeys(rdev,
4225 info->attrs[NL80211_ATTR_KEYS]);
4226 if (IS_ERR(connkeys))
4227 return PTR_ERR(connkeys);
4228 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004229
Johannes Berg4c476992010-10-04 21:36:35 +02004230 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004231 if (err)
4232 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004233 return err;
4234}
4235
4236static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4237{
Johannes Berg4c476992010-10-04 21:36:35 +02004238 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4239 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004240
Johannes Berg4c476992010-10-04 21:36:35 +02004241 if (!rdev->ops->leave_ibss)
4242 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004243
Johannes Berg4c476992010-10-04 21:36:35 +02004244 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4245 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004246
Johannes Berg4c476992010-10-04 21:36:35 +02004247 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004248}
4249
Johannes Bergaff89a92009-07-01 21:26:51 +02004250#ifdef CONFIG_NL80211_TESTMODE
4251static struct genl_multicast_group nl80211_testmode_mcgrp = {
4252 .name = "testmode",
4253};
4254
4255static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4256{
Johannes Berg4c476992010-10-04 21:36:35 +02004257 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004258 int err;
4259
4260 if (!info->attrs[NL80211_ATTR_TESTDATA])
4261 return -EINVAL;
4262
Johannes Bergaff89a92009-07-01 21:26:51 +02004263 err = -EOPNOTSUPP;
4264 if (rdev->ops->testmode_cmd) {
4265 rdev->testmode_info = info;
4266 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4267 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4268 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4269 rdev->testmode_info = NULL;
4270 }
4271
Johannes Bergaff89a92009-07-01 21:26:51 +02004272 return err;
4273}
4274
4275static struct sk_buff *
4276__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4277 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4278{
4279 struct sk_buff *skb;
4280 void *hdr;
4281 struct nlattr *data;
4282
4283 skb = nlmsg_new(approxlen + 100, gfp);
4284 if (!skb)
4285 return NULL;
4286
4287 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4288 if (!hdr) {
4289 kfree_skb(skb);
4290 return NULL;
4291 }
4292
4293 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4294 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4295
4296 ((void **)skb->cb)[0] = rdev;
4297 ((void **)skb->cb)[1] = hdr;
4298 ((void **)skb->cb)[2] = data;
4299
4300 return skb;
4301
4302 nla_put_failure:
4303 kfree_skb(skb);
4304 return NULL;
4305}
4306
4307struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4308 int approxlen)
4309{
4310 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4311
4312 if (WARN_ON(!rdev->testmode_info))
4313 return NULL;
4314
4315 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4316 rdev->testmode_info->snd_pid,
4317 rdev->testmode_info->snd_seq,
4318 GFP_KERNEL);
4319}
4320EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4321
4322int cfg80211_testmode_reply(struct sk_buff *skb)
4323{
4324 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4325 void *hdr = ((void **)skb->cb)[1];
4326 struct nlattr *data = ((void **)skb->cb)[2];
4327
4328 if (WARN_ON(!rdev->testmode_info)) {
4329 kfree_skb(skb);
4330 return -EINVAL;
4331 }
4332
4333 nla_nest_end(skb, data);
4334 genlmsg_end(skb, hdr);
4335 return genlmsg_reply(skb, rdev->testmode_info);
4336}
4337EXPORT_SYMBOL(cfg80211_testmode_reply);
4338
4339struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4340 int approxlen, gfp_t gfp)
4341{
4342 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4343
4344 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4345}
4346EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4347
4348void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4349{
4350 void *hdr = ((void **)skb->cb)[1];
4351 struct nlattr *data = ((void **)skb->cb)[2];
4352
4353 nla_nest_end(skb, data);
4354 genlmsg_end(skb, hdr);
4355 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4356}
4357EXPORT_SYMBOL(cfg80211_testmode_event);
4358#endif
4359
Samuel Ortizb23aa672009-07-01 21:26:54 +02004360static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4361{
Johannes Berg4c476992010-10-04 21:36:35 +02004362 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4363 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004364 struct cfg80211_connect_params connect;
4365 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004366 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004367 int err;
4368
4369 memset(&connect, 0, sizeof(connect));
4370
4371 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4372 return -EINVAL;
4373
4374 if (!info->attrs[NL80211_ATTR_SSID] ||
4375 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4376 return -EINVAL;
4377
4378 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4379 connect.auth_type =
4380 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4381 if (!nl80211_valid_auth_type(connect.auth_type))
4382 return -EINVAL;
4383 } else
4384 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4385
4386 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4387
Johannes Bergc0692b82010-08-27 14:26:53 +03004388 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004389 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004390 if (err)
4391 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004392
Johannes Berg074ac8d2010-09-16 14:58:22 +02004393 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004394 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4395 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004396
Johannes Berg79c97e92009-07-07 03:56:12 +02004397 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004398
Samuel Ortizb23aa672009-07-01 21:26:54 +02004399 if (info->attrs[NL80211_ATTR_MAC])
4400 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4401 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4402 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4403
4404 if (info->attrs[NL80211_ATTR_IE]) {
4405 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4406 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4407 }
4408
4409 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4410 connect.channel =
4411 ieee80211_get_channel(wiphy,
4412 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4413 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004414 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4415 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004416 }
4417
Johannes Bergfffd0932009-07-08 14:22:54 +02004418 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4419 connkeys = nl80211_parse_connkeys(rdev,
4420 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004421 if (IS_ERR(connkeys))
4422 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004423 }
4424
4425 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004426 if (err)
4427 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004428 return err;
4429}
4430
4431static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4432{
Johannes Berg4c476992010-10-04 21:36:35 +02004433 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4434 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004435 u16 reason;
4436
4437 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4438 reason = WLAN_REASON_DEAUTH_LEAVING;
4439 else
4440 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4441
4442 if (reason == 0)
4443 return -EINVAL;
4444
Johannes Berg074ac8d2010-09-16 14:58:22 +02004445 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004446 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4447 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004448
Johannes Berg4c476992010-10-04 21:36:35 +02004449 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004450}
4451
Johannes Berg463d0182009-07-14 00:33:35 +02004452static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4453{
Johannes Berg4c476992010-10-04 21:36:35 +02004454 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004455 struct net *net;
4456 int err;
4457 u32 pid;
4458
4459 if (!info->attrs[NL80211_ATTR_PID])
4460 return -EINVAL;
4461
4462 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4463
Johannes Berg463d0182009-07-14 00:33:35 +02004464 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004465 if (IS_ERR(net))
4466 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004467
4468 err = 0;
4469
4470 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004471 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4472 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004473
Johannes Berg463d0182009-07-14 00:33:35 +02004474 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004475 return err;
4476}
4477
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004478static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4479{
Johannes Berg4c476992010-10-04 21:36:35 +02004480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004481 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4482 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004483 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004484 struct cfg80211_pmksa pmksa;
4485
4486 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4487
4488 if (!info->attrs[NL80211_ATTR_MAC])
4489 return -EINVAL;
4490
4491 if (!info->attrs[NL80211_ATTR_PMKID])
4492 return -EINVAL;
4493
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004494 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4495 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4496
Johannes Berg074ac8d2010-09-16 14:58:22 +02004497 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004498 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4499 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004500
4501 switch (info->genlhdr->cmd) {
4502 case NL80211_CMD_SET_PMKSA:
4503 rdev_ops = rdev->ops->set_pmksa;
4504 break;
4505 case NL80211_CMD_DEL_PMKSA:
4506 rdev_ops = rdev->ops->del_pmksa;
4507 break;
4508 default:
4509 WARN_ON(1);
4510 break;
4511 }
4512
Johannes Berg4c476992010-10-04 21:36:35 +02004513 if (!rdev_ops)
4514 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004515
Johannes Berg4c476992010-10-04 21:36:35 +02004516 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004517}
4518
4519static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4520{
Johannes Berg4c476992010-10-04 21:36:35 +02004521 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4522 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004523
Johannes Berg074ac8d2010-09-16 14:58:22 +02004524 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004525 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4526 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004527
Johannes Berg4c476992010-10-04 21:36:35 +02004528 if (!rdev->ops->flush_pmksa)
4529 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004530
Johannes Berg4c476992010-10-04 21:36:35 +02004531 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004532}
4533
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004534static int nl80211_remain_on_channel(struct sk_buff *skb,
4535 struct genl_info *info)
4536{
Johannes Berg4c476992010-10-04 21:36:35 +02004537 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4538 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004539 struct ieee80211_channel *chan;
4540 struct sk_buff *msg;
4541 void *hdr;
4542 u64 cookie;
4543 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4544 u32 freq, duration;
4545 int err;
4546
4547 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4548 !info->attrs[NL80211_ATTR_DURATION])
4549 return -EINVAL;
4550
4551 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4552
4553 /*
4554 * We should be on that channel for at least one jiffie,
4555 * and more than 5 seconds seems excessive.
4556 */
Johannes Berga2939112010-12-14 17:54:28 +01004557 if (!duration || !msecs_to_jiffies(duration) ||
4558 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004559 return -EINVAL;
4560
Johannes Berg4c476992010-10-04 21:36:35 +02004561 if (!rdev->ops->remain_on_channel)
4562 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004563
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004564 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4565 channel_type = nla_get_u32(
4566 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4567 if (channel_type != NL80211_CHAN_NO_HT &&
4568 channel_type != NL80211_CHAN_HT20 &&
4569 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004570 channel_type != NL80211_CHAN_HT40MINUS)
4571 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004572 }
4573
4574 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4575 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004576 if (chan == NULL)
4577 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004578
4579 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004580 if (!msg)
4581 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004582
4583 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4584 NL80211_CMD_REMAIN_ON_CHANNEL);
4585
4586 if (IS_ERR(hdr)) {
4587 err = PTR_ERR(hdr);
4588 goto free_msg;
4589 }
4590
4591 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4592 channel_type, duration, &cookie);
4593
4594 if (err)
4595 goto free_msg;
4596
4597 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4598
4599 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004600
4601 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004602
4603 nla_put_failure:
4604 err = -ENOBUFS;
4605 free_msg:
4606 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004607 return err;
4608}
4609
4610static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4611 struct genl_info *info)
4612{
Johannes Berg4c476992010-10-04 21:36:35 +02004613 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4614 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004615 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004616
4617 if (!info->attrs[NL80211_ATTR_COOKIE])
4618 return -EINVAL;
4619
Johannes Berg4c476992010-10-04 21:36:35 +02004620 if (!rdev->ops->cancel_remain_on_channel)
4621 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004622
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004623 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4624
Johannes Berg4c476992010-10-04 21:36:35 +02004625 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004626}
4627
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004628static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4629 u8 *rates, u8 rates_len)
4630{
4631 u8 i;
4632 u32 mask = 0;
4633
4634 for (i = 0; i < rates_len; i++) {
4635 int rate = (rates[i] & 0x7f) * 5;
4636 int ridx;
4637 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4638 struct ieee80211_rate *srate =
4639 &sband->bitrates[ridx];
4640 if (rate == srate->bitrate) {
4641 mask |= 1 << ridx;
4642 break;
4643 }
4644 }
4645 if (ridx == sband->n_bitrates)
4646 return 0; /* rate not found */
4647 }
4648
4649 return mask;
4650}
4651
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004652static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004653 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4654 .len = NL80211_MAX_SUPP_RATES },
4655};
4656
4657static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4658 struct genl_info *info)
4659{
4660 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004661 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004662 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004663 int rem, i;
4664 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004665 struct nlattr *tx_rates;
4666 struct ieee80211_supported_band *sband;
4667
4668 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4669 return -EINVAL;
4670
Johannes Berg4c476992010-10-04 21:36:35 +02004671 if (!rdev->ops->set_bitrate_mask)
4672 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004673
4674 memset(&mask, 0, sizeof(mask));
4675 /* Default to all rates enabled */
4676 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4677 sband = rdev->wiphy.bands[i];
4678 mask.control[i].legacy =
4679 sband ? (1 << sband->n_bitrates) - 1 : 0;
4680 }
4681
4682 /*
4683 * The nested attribute uses enum nl80211_band as the index. This maps
4684 * directly to the enum ieee80211_band values used in cfg80211.
4685 */
4686 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4687 {
4688 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004689 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4690 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004691 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004692 if (sband == NULL)
4693 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004694 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4695 nla_len(tx_rates), nl80211_txattr_policy);
4696 if (tb[NL80211_TXRATE_LEGACY]) {
4697 mask.control[band].legacy = rateset_to_mask(
4698 sband,
4699 nla_data(tb[NL80211_TXRATE_LEGACY]),
4700 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004701 if (mask.control[band].legacy == 0)
4702 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004703 }
4704 }
4705
Johannes Berg4c476992010-10-04 21:36:35 +02004706 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004707}
4708
Johannes Berg2e161f72010-08-12 15:38:38 +02004709static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004710{
Johannes Berg4c476992010-10-04 21:36:35 +02004711 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4712 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004713 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004714
4715 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4716 return -EINVAL;
4717
Johannes Berg2e161f72010-08-12 15:38:38 +02004718 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4719 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004720
Johannes Berg9d38d852010-06-09 17:20:33 +02004721 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004722 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004723 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4724 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4725 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08004726 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004727 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4728 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004729
4730 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004731 if (!rdev->ops->mgmt_tx)
4732 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004733
Johannes Berg4c476992010-10-04 21:36:35 +02004734 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004735 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004736 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4737 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004738}
4739
Johannes Berg2e161f72010-08-12 15:38:38 +02004740static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004741{
Johannes Berg4c476992010-10-04 21:36:35 +02004742 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4743 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004744 struct ieee80211_channel *chan;
4745 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004746 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004747 u32 freq;
4748 int err;
4749 void *hdr;
4750 u64 cookie;
4751 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004752 unsigned int wait = 0;
4753 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02004754
4755 if (!info->attrs[NL80211_ATTR_FRAME] ||
4756 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4757 return -EINVAL;
4758
Johannes Berg4c476992010-10-04 21:36:35 +02004759 if (!rdev->ops->mgmt_tx)
4760 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004761
Johannes Berg9d38d852010-06-09 17:20:33 +02004762 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004763 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004764 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4765 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4766 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08004767 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004768 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4769 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004770
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004771 if (info->attrs[NL80211_ATTR_DURATION]) {
4772 if (!rdev->ops->mgmt_tx_cancel_wait)
4773 return -EINVAL;
4774 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4775 }
4776
Jouni Malinen026331c2010-02-15 12:53:10 +02004777 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4778 channel_type = nla_get_u32(
4779 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4780 if (channel_type != NL80211_CHAN_NO_HT &&
4781 channel_type != NL80211_CHAN_HT20 &&
4782 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004783 channel_type != NL80211_CHAN_HT40MINUS)
4784 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004785 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004786 }
4787
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004788 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
4789
Jouni Malinen026331c2010-02-15 12:53:10 +02004790 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4791 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004792 if (chan == NULL)
4793 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004794
4795 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004796 if (!msg)
4797 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004798
4799 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004800 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004801
4802 if (IS_ERR(hdr)) {
4803 err = PTR_ERR(hdr);
4804 goto free_msg;
4805 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004806 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
4807 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02004808 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4809 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4810 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004811 if (err)
4812 goto free_msg;
4813
4814 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4815
4816 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004817 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004818
4819 nla_put_failure:
4820 err = -ENOBUFS;
4821 free_msg:
4822 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004823 return err;
4824}
4825
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004826static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
4827{
4828 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4829 struct net_device *dev = info->user_ptr[1];
4830 u64 cookie;
4831
4832 if (!info->attrs[NL80211_ATTR_COOKIE])
4833 return -EINVAL;
4834
4835 if (!rdev->ops->mgmt_tx_cancel_wait)
4836 return -EOPNOTSUPP;
4837
4838 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4839 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4840 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4841 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4842 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4843 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4844 return -EOPNOTSUPP;
4845
4846 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4847
4848 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
4849}
4850
Kalle Valoffb9eb32010-02-17 17:58:10 +02004851static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4852{
Johannes Berg4c476992010-10-04 21:36:35 +02004853 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004854 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004855 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004856 u8 ps_state;
4857 bool state;
4858 int err;
4859
Johannes Berg4c476992010-10-04 21:36:35 +02004860 if (!info->attrs[NL80211_ATTR_PS_STATE])
4861 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004862
4863 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4864
Johannes Berg4c476992010-10-04 21:36:35 +02004865 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4866 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004867
4868 wdev = dev->ieee80211_ptr;
4869
Johannes Berg4c476992010-10-04 21:36:35 +02004870 if (!rdev->ops->set_power_mgmt)
4871 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004872
4873 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4874
4875 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004876 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004877
Johannes Berg4c476992010-10-04 21:36:35 +02004878 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4879 wdev->ps_timeout);
4880 if (!err)
4881 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004882 return err;
4883}
4884
4885static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4886{
Johannes Berg4c476992010-10-04 21:36:35 +02004887 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004888 enum nl80211_ps_state ps_state;
4889 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004890 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004891 struct sk_buff *msg;
4892 void *hdr;
4893 int err;
4894
Kalle Valoffb9eb32010-02-17 17:58:10 +02004895 wdev = dev->ieee80211_ptr;
4896
Johannes Berg4c476992010-10-04 21:36:35 +02004897 if (!rdev->ops->set_power_mgmt)
4898 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004899
4900 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004901 if (!msg)
4902 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004903
4904 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4905 NL80211_CMD_GET_POWER_SAVE);
4906 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004907 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004908 goto free_msg;
4909 }
4910
4911 if (wdev->ps)
4912 ps_state = NL80211_PS_ENABLED;
4913 else
4914 ps_state = NL80211_PS_DISABLED;
4915
4916 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4917
4918 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004919 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004920
Johannes Berg4c476992010-10-04 21:36:35 +02004921 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004922 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004923 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004924 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004925 return err;
4926}
4927
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004928static struct nla_policy
4929nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4930 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4931 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4932 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4933};
4934
4935static int nl80211_set_cqm_rssi(struct genl_info *info,
4936 s32 threshold, u32 hysteresis)
4937{
Johannes Berg4c476992010-10-04 21:36:35 +02004938 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004939 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004940 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004941
4942 if (threshold > 0)
4943 return -EINVAL;
4944
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004945 wdev = dev->ieee80211_ptr;
4946
Johannes Berg4c476992010-10-04 21:36:35 +02004947 if (!rdev->ops->set_cqm_rssi_config)
4948 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004949
Johannes Berg074ac8d2010-09-16 14:58:22 +02004950 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004951 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4952 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004953
Johannes Berg4c476992010-10-04 21:36:35 +02004954 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4955 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004956}
4957
4958static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4959{
4960 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4961 struct nlattr *cqm;
4962 int err;
4963
4964 cqm = info->attrs[NL80211_ATTR_CQM];
4965 if (!cqm) {
4966 err = -EINVAL;
4967 goto out;
4968 }
4969
4970 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4971 nl80211_attr_cqm_policy);
4972 if (err)
4973 goto out;
4974
4975 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4976 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4977 s32 threshold;
4978 u32 hysteresis;
4979 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4980 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4981 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4982 } else
4983 err = -EINVAL;
4984
4985out:
4986 return err;
4987}
4988
Johannes Berg29cbe682010-12-03 09:20:44 +01004989static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
4990{
4991 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4992 struct net_device *dev = info->user_ptr[1];
4993 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08004994 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01004995 int err;
4996
4997 /* start with default */
4998 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08004999 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005000
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005001 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005002 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005003 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005004 if (err)
5005 return err;
5006 }
5007
5008 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5009 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5010 return -EINVAL;
5011
Javier Cardonac80d5452010-12-16 17:37:49 -08005012 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5013 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5014
5015 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5016 /* parse additional setup parameters if given */
5017 err = nl80211_parse_mesh_setup(info, &setup);
5018 if (err)
5019 return err;
5020 }
5021
5022 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005023}
5024
5025static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5026{
5027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5028 struct net_device *dev = info->user_ptr[1];
5029
5030 return cfg80211_leave_mesh(rdev, dev);
5031}
5032
Johannes Bergff1b6e62011-05-04 15:37:28 +02005033static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5034{
5035 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5036 struct sk_buff *msg;
5037 void *hdr;
5038
5039 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5040 return -EOPNOTSUPP;
5041
5042 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5043 if (!msg)
5044 return -ENOMEM;
5045
5046 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5047 NL80211_CMD_GET_WOWLAN);
5048 if (!hdr)
5049 goto nla_put_failure;
5050
5051 if (rdev->wowlan) {
5052 struct nlattr *nl_wowlan;
5053
5054 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5055 if (!nl_wowlan)
5056 goto nla_put_failure;
5057
5058 if (rdev->wowlan->any)
5059 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5060 if (rdev->wowlan->disconnect)
5061 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5062 if (rdev->wowlan->magic_pkt)
5063 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
5064 if (rdev->wowlan->n_patterns) {
5065 struct nlattr *nl_pats, *nl_pat;
5066 int i, pat_len;
5067
5068 nl_pats = nla_nest_start(msg,
5069 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5070 if (!nl_pats)
5071 goto nla_put_failure;
5072
5073 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5074 nl_pat = nla_nest_start(msg, i + 1);
5075 if (!nl_pat)
5076 goto nla_put_failure;
5077 pat_len = rdev->wowlan->patterns[i].pattern_len;
5078 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5079 DIV_ROUND_UP(pat_len, 8),
5080 rdev->wowlan->patterns[i].mask);
5081 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5082 pat_len,
5083 rdev->wowlan->patterns[i].pattern);
5084 nla_nest_end(msg, nl_pat);
5085 }
5086 nla_nest_end(msg, nl_pats);
5087 }
5088
5089 nla_nest_end(msg, nl_wowlan);
5090 }
5091
5092 genlmsg_end(msg, hdr);
5093 return genlmsg_reply(msg, info);
5094
5095nla_put_failure:
5096 nlmsg_free(msg);
5097 return -ENOBUFS;
5098}
5099
5100static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5101{
5102 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5103 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5104 struct cfg80211_wowlan no_triggers = {};
5105 struct cfg80211_wowlan new_triggers = {};
5106 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5107 int err, i;
5108
5109 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5110 return -EOPNOTSUPP;
5111
5112 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5113 goto no_triggers;
5114
5115 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5116 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5117 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5118 nl80211_wowlan_policy);
5119 if (err)
5120 return err;
5121
5122 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5123 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5124 return -EINVAL;
5125 new_triggers.any = true;
5126 }
5127
5128 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5129 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5130 return -EINVAL;
5131 new_triggers.disconnect = true;
5132 }
5133
5134 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5135 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5136 return -EINVAL;
5137 new_triggers.magic_pkt = true;
5138 }
5139
5140 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5141 struct nlattr *pat;
5142 int n_patterns = 0;
5143 int rem, pat_len, mask_len;
5144 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5145
5146 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5147 rem)
5148 n_patterns++;
5149 if (n_patterns > wowlan->n_patterns)
5150 return -EINVAL;
5151
5152 new_triggers.patterns = kcalloc(n_patterns,
5153 sizeof(new_triggers.patterns[0]),
5154 GFP_KERNEL);
5155 if (!new_triggers.patterns)
5156 return -ENOMEM;
5157
5158 new_triggers.n_patterns = n_patterns;
5159 i = 0;
5160
5161 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5162 rem) {
5163 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5164 nla_data(pat), nla_len(pat), NULL);
5165 err = -EINVAL;
5166 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5167 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5168 goto error;
5169 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5170 mask_len = DIV_ROUND_UP(pat_len, 8);
5171 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5172 mask_len)
5173 goto error;
5174 if (pat_len > wowlan->pattern_max_len ||
5175 pat_len < wowlan->pattern_min_len)
5176 goto error;
5177
5178 new_triggers.patterns[i].mask =
5179 kmalloc(mask_len + pat_len, GFP_KERNEL);
5180 if (!new_triggers.patterns[i].mask) {
5181 err = -ENOMEM;
5182 goto error;
5183 }
5184 new_triggers.patterns[i].pattern =
5185 new_triggers.patterns[i].mask + mask_len;
5186 memcpy(new_triggers.patterns[i].mask,
5187 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5188 mask_len);
5189 new_triggers.patterns[i].pattern_len = pat_len;
5190 memcpy(new_triggers.patterns[i].pattern,
5191 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5192 pat_len);
5193 i++;
5194 }
5195 }
5196
5197 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5198 struct cfg80211_wowlan *ntrig;
5199 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5200 GFP_KERNEL);
5201 if (!ntrig) {
5202 err = -ENOMEM;
5203 goto error;
5204 }
5205 cfg80211_rdev_free_wowlan(rdev);
5206 rdev->wowlan = ntrig;
5207 } else {
5208 no_triggers:
5209 cfg80211_rdev_free_wowlan(rdev);
5210 rdev->wowlan = NULL;
5211 }
5212
5213 return 0;
5214 error:
5215 for (i = 0; i < new_triggers.n_patterns; i++)
5216 kfree(new_triggers.patterns[i].mask);
5217 kfree(new_triggers.patterns);
5218 return err;
5219}
5220
Johannes Berg4c476992010-10-04 21:36:35 +02005221#define NL80211_FLAG_NEED_WIPHY 0x01
5222#define NL80211_FLAG_NEED_NETDEV 0x02
5223#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005224#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5225#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5226 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005227
5228static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5229 struct genl_info *info)
5230{
5231 struct cfg80211_registered_device *rdev;
5232 struct net_device *dev;
5233 int err;
5234 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5235
5236 if (rtnl)
5237 rtnl_lock();
5238
5239 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5240 rdev = cfg80211_get_dev_from_info(info);
5241 if (IS_ERR(rdev)) {
5242 if (rtnl)
5243 rtnl_unlock();
5244 return PTR_ERR(rdev);
5245 }
5246 info->user_ptr[0] = rdev;
5247 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5248 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5249 if (err) {
5250 if (rtnl)
5251 rtnl_unlock();
5252 return err;
5253 }
Johannes Berg41265712010-10-04 21:14:05 +02005254 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5255 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005256 cfg80211_unlock_rdev(rdev);
5257 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005258 if (rtnl)
5259 rtnl_unlock();
5260 return -ENETDOWN;
5261 }
Johannes Berg4c476992010-10-04 21:36:35 +02005262 info->user_ptr[0] = rdev;
5263 info->user_ptr[1] = dev;
5264 }
5265
5266 return 0;
5267}
5268
5269static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5270 struct genl_info *info)
5271{
5272 if (info->user_ptr[0])
5273 cfg80211_unlock_rdev(info->user_ptr[0]);
5274 if (info->user_ptr[1])
5275 dev_put(info->user_ptr[1]);
5276 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5277 rtnl_unlock();
5278}
5279
Johannes Berg55682962007-09-20 13:09:35 -04005280static struct genl_ops nl80211_ops[] = {
5281 {
5282 .cmd = NL80211_CMD_GET_WIPHY,
5283 .doit = nl80211_get_wiphy,
5284 .dumpit = nl80211_dump_wiphy,
5285 .policy = nl80211_policy,
5286 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005287 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005288 },
5289 {
5290 .cmd = NL80211_CMD_SET_WIPHY,
5291 .doit = nl80211_set_wiphy,
5292 .policy = nl80211_policy,
5293 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005294 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005295 },
5296 {
5297 .cmd = NL80211_CMD_GET_INTERFACE,
5298 .doit = nl80211_get_interface,
5299 .dumpit = nl80211_dump_interface,
5300 .policy = nl80211_policy,
5301 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005302 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04005303 },
5304 {
5305 .cmd = NL80211_CMD_SET_INTERFACE,
5306 .doit = nl80211_set_interface,
5307 .policy = nl80211_policy,
5308 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005309 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5310 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005311 },
5312 {
5313 .cmd = NL80211_CMD_NEW_INTERFACE,
5314 .doit = nl80211_new_interface,
5315 .policy = nl80211_policy,
5316 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005317 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5318 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005319 },
5320 {
5321 .cmd = NL80211_CMD_DEL_INTERFACE,
5322 .doit = nl80211_del_interface,
5323 .policy = nl80211_policy,
5324 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005325 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5326 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005327 },
Johannes Berg41ade002007-12-19 02:03:29 +01005328 {
5329 .cmd = NL80211_CMD_GET_KEY,
5330 .doit = nl80211_get_key,
5331 .policy = nl80211_policy,
5332 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005333 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5334 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005335 },
5336 {
5337 .cmd = NL80211_CMD_SET_KEY,
5338 .doit = nl80211_set_key,
5339 .policy = nl80211_policy,
5340 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005341 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005342 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005343 },
5344 {
5345 .cmd = NL80211_CMD_NEW_KEY,
5346 .doit = nl80211_new_key,
5347 .policy = nl80211_policy,
5348 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005349 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005350 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005351 },
5352 {
5353 .cmd = NL80211_CMD_DEL_KEY,
5354 .doit = nl80211_del_key,
5355 .policy = nl80211_policy,
5356 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005357 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005358 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005359 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005360 {
5361 .cmd = NL80211_CMD_SET_BEACON,
5362 .policy = nl80211_policy,
5363 .flags = GENL_ADMIN_PERM,
5364 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005365 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5366 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005367 },
5368 {
5369 .cmd = NL80211_CMD_NEW_BEACON,
5370 .policy = nl80211_policy,
5371 .flags = GENL_ADMIN_PERM,
5372 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005373 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5374 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005375 },
5376 {
5377 .cmd = NL80211_CMD_DEL_BEACON,
5378 .policy = nl80211_policy,
5379 .flags = GENL_ADMIN_PERM,
5380 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005381 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5382 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005383 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005384 {
5385 .cmd = NL80211_CMD_GET_STATION,
5386 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005387 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005388 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02005389 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5390 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005391 },
5392 {
5393 .cmd = NL80211_CMD_SET_STATION,
5394 .doit = nl80211_set_station,
5395 .policy = nl80211_policy,
5396 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005397 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5398 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005399 },
5400 {
5401 .cmd = NL80211_CMD_NEW_STATION,
5402 .doit = nl80211_new_station,
5403 .policy = nl80211_policy,
5404 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005405 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005406 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005407 },
5408 {
5409 .cmd = NL80211_CMD_DEL_STATION,
5410 .doit = nl80211_del_station,
5411 .policy = nl80211_policy,
5412 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005413 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5414 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005415 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005416 {
5417 .cmd = NL80211_CMD_GET_MPATH,
5418 .doit = nl80211_get_mpath,
5419 .dumpit = nl80211_dump_mpath,
5420 .policy = nl80211_policy,
5421 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005422 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005423 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005424 },
5425 {
5426 .cmd = NL80211_CMD_SET_MPATH,
5427 .doit = nl80211_set_mpath,
5428 .policy = nl80211_policy,
5429 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005430 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005431 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005432 },
5433 {
5434 .cmd = NL80211_CMD_NEW_MPATH,
5435 .doit = nl80211_new_mpath,
5436 .policy = nl80211_policy,
5437 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005438 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005439 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005440 },
5441 {
5442 .cmd = NL80211_CMD_DEL_MPATH,
5443 .doit = nl80211_del_mpath,
5444 .policy = nl80211_policy,
5445 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005446 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5447 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005448 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005449 {
5450 .cmd = NL80211_CMD_SET_BSS,
5451 .doit = nl80211_set_bss,
5452 .policy = nl80211_policy,
5453 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005454 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5455 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005456 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005457 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005458 .cmd = NL80211_CMD_GET_REG,
5459 .doit = nl80211_get_reg,
5460 .policy = nl80211_policy,
5461 /* can be retrieved by unprivileged users */
5462 },
5463 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005464 .cmd = NL80211_CMD_SET_REG,
5465 .doit = nl80211_set_reg,
5466 .policy = nl80211_policy,
5467 .flags = GENL_ADMIN_PERM,
5468 },
5469 {
5470 .cmd = NL80211_CMD_REQ_SET_REG,
5471 .doit = nl80211_req_set_reg,
5472 .policy = nl80211_policy,
5473 .flags = GENL_ADMIN_PERM,
5474 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005475 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005476 .cmd = NL80211_CMD_GET_MESH_CONFIG,
5477 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005478 .policy = nl80211_policy,
5479 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005480 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5481 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005482 },
5483 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005484 .cmd = NL80211_CMD_SET_MESH_CONFIG,
5485 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005486 .policy = nl80211_policy,
5487 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01005488 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005489 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005490 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005491 {
Johannes Berg2a519312009-02-10 21:25:55 +01005492 .cmd = NL80211_CMD_TRIGGER_SCAN,
5493 .doit = nl80211_trigger_scan,
5494 .policy = nl80211_policy,
5495 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005496 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005497 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01005498 },
5499 {
5500 .cmd = NL80211_CMD_GET_SCAN,
5501 .policy = nl80211_policy,
5502 .dumpit = nl80211_dump_scan,
5503 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005504 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005505 .cmd = NL80211_CMD_START_SCHED_SCAN,
5506 .doit = nl80211_start_sched_scan,
5507 .policy = nl80211_policy,
5508 .flags = GENL_ADMIN_PERM,
5509 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5510 NL80211_FLAG_NEED_RTNL,
5511 },
5512 {
5513 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
5514 .doit = nl80211_stop_sched_scan,
5515 .policy = nl80211_policy,
5516 .flags = GENL_ADMIN_PERM,
5517 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5518 NL80211_FLAG_NEED_RTNL,
5519 },
5520 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02005521 .cmd = NL80211_CMD_AUTHENTICATE,
5522 .doit = nl80211_authenticate,
5523 .policy = nl80211_policy,
5524 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005525 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005526 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005527 },
5528 {
5529 .cmd = NL80211_CMD_ASSOCIATE,
5530 .doit = nl80211_associate,
5531 .policy = nl80211_policy,
5532 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005533 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005534 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005535 },
5536 {
5537 .cmd = NL80211_CMD_DEAUTHENTICATE,
5538 .doit = nl80211_deauthenticate,
5539 .policy = nl80211_policy,
5540 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005541 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005542 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005543 },
5544 {
5545 .cmd = NL80211_CMD_DISASSOCIATE,
5546 .doit = nl80211_disassociate,
5547 .policy = nl80211_policy,
5548 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005549 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005550 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005551 },
Johannes Berg04a773a2009-04-19 21:24:32 +02005552 {
5553 .cmd = NL80211_CMD_JOIN_IBSS,
5554 .doit = nl80211_join_ibss,
5555 .policy = nl80211_policy,
5556 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005557 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005558 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005559 },
5560 {
5561 .cmd = NL80211_CMD_LEAVE_IBSS,
5562 .doit = nl80211_leave_ibss,
5563 .policy = nl80211_policy,
5564 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005565 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005566 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005567 },
Johannes Bergaff89a92009-07-01 21:26:51 +02005568#ifdef CONFIG_NL80211_TESTMODE
5569 {
5570 .cmd = NL80211_CMD_TESTMODE,
5571 .doit = nl80211_testmode_do,
5572 .policy = nl80211_policy,
5573 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005574 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5575 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02005576 },
5577#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02005578 {
5579 .cmd = NL80211_CMD_CONNECT,
5580 .doit = nl80211_connect,
5581 .policy = nl80211_policy,
5582 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005583 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005584 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005585 },
5586 {
5587 .cmd = NL80211_CMD_DISCONNECT,
5588 .doit = nl80211_disconnect,
5589 .policy = nl80211_policy,
5590 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005591 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005592 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005593 },
Johannes Berg463d0182009-07-14 00:33:35 +02005594 {
5595 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5596 .doit = nl80211_wiphy_netns,
5597 .policy = nl80211_policy,
5598 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005599 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5600 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02005601 },
Holger Schurig61fa7132009-11-11 12:25:40 +01005602 {
5603 .cmd = NL80211_CMD_GET_SURVEY,
5604 .policy = nl80211_policy,
5605 .dumpit = nl80211_dump_survey,
5606 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005607 {
5608 .cmd = NL80211_CMD_SET_PMKSA,
5609 .doit = nl80211_setdel_pmksa,
5610 .policy = nl80211_policy,
5611 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005612 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5613 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005614 },
5615 {
5616 .cmd = NL80211_CMD_DEL_PMKSA,
5617 .doit = nl80211_setdel_pmksa,
5618 .policy = nl80211_policy,
5619 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005620 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5621 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005622 },
5623 {
5624 .cmd = NL80211_CMD_FLUSH_PMKSA,
5625 .doit = nl80211_flush_pmksa,
5626 .policy = nl80211_policy,
5627 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005628 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5629 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005630 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005631 {
5632 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5633 .doit = nl80211_remain_on_channel,
5634 .policy = nl80211_policy,
5635 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005636 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005637 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005638 },
5639 {
5640 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5641 .doit = nl80211_cancel_remain_on_channel,
5642 .policy = nl80211_policy,
5643 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005644 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005645 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005646 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005647 {
5648 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5649 .doit = nl80211_set_tx_bitrate_mask,
5650 .policy = nl80211_policy,
5651 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005652 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5653 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005654 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005655 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005656 .cmd = NL80211_CMD_REGISTER_FRAME,
5657 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005658 .policy = nl80211_policy,
5659 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005660 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5661 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005662 },
5663 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005664 .cmd = NL80211_CMD_FRAME,
5665 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005666 .policy = nl80211_policy,
5667 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005668 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005669 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005670 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005671 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005672 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
5673 .doit = nl80211_tx_mgmt_cancel_wait,
5674 .policy = nl80211_policy,
5675 .flags = GENL_ADMIN_PERM,
5676 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5677 NL80211_FLAG_NEED_RTNL,
5678 },
5679 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02005680 .cmd = NL80211_CMD_SET_POWER_SAVE,
5681 .doit = nl80211_set_power_save,
5682 .policy = nl80211_policy,
5683 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005684 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5685 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005686 },
5687 {
5688 .cmd = NL80211_CMD_GET_POWER_SAVE,
5689 .doit = nl80211_get_power_save,
5690 .policy = nl80211_policy,
5691 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005692 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5693 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005694 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005695 {
5696 .cmd = NL80211_CMD_SET_CQM,
5697 .doit = nl80211_set_cqm,
5698 .policy = nl80211_policy,
5699 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005700 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5701 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005702 },
Johannes Bergf444de02010-05-05 15:25:02 +02005703 {
5704 .cmd = NL80211_CMD_SET_CHANNEL,
5705 .doit = nl80211_set_channel,
5706 .policy = nl80211_policy,
5707 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005708 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5709 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02005710 },
Bill Jordane8347eb2010-10-01 13:54:28 -04005711 {
5712 .cmd = NL80211_CMD_SET_WDS_PEER,
5713 .doit = nl80211_set_wds_peer,
5714 .policy = nl80211_policy,
5715 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02005716 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5717 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04005718 },
Johannes Berg29cbe682010-12-03 09:20:44 +01005719 {
5720 .cmd = NL80211_CMD_JOIN_MESH,
5721 .doit = nl80211_join_mesh,
5722 .policy = nl80211_policy,
5723 .flags = GENL_ADMIN_PERM,
5724 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5725 NL80211_FLAG_NEED_RTNL,
5726 },
5727 {
5728 .cmd = NL80211_CMD_LEAVE_MESH,
5729 .doit = nl80211_leave_mesh,
5730 .policy = nl80211_policy,
5731 .flags = GENL_ADMIN_PERM,
5732 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5733 NL80211_FLAG_NEED_RTNL,
5734 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02005735 {
5736 .cmd = NL80211_CMD_GET_WOWLAN,
5737 .doit = nl80211_get_wowlan,
5738 .policy = nl80211_policy,
5739 /* can be retrieved by unprivileged users */
5740 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5741 NL80211_FLAG_NEED_RTNL,
5742 },
5743 {
5744 .cmd = NL80211_CMD_SET_WOWLAN,
5745 .doit = nl80211_set_wowlan,
5746 .policy = nl80211_policy,
5747 .flags = GENL_ADMIN_PERM,
5748 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5749 NL80211_FLAG_NEED_RTNL,
5750 },
Johannes Berg55682962007-09-20 13:09:35 -04005751};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005752
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005753static struct genl_multicast_group nl80211_mlme_mcgrp = {
5754 .name = "mlme",
5755};
Johannes Berg55682962007-09-20 13:09:35 -04005756
5757/* multicast groups */
5758static struct genl_multicast_group nl80211_config_mcgrp = {
5759 .name = "config",
5760};
Johannes Berg2a519312009-02-10 21:25:55 +01005761static struct genl_multicast_group nl80211_scan_mcgrp = {
5762 .name = "scan",
5763};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005764static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5765 .name = "regulatory",
5766};
Johannes Berg55682962007-09-20 13:09:35 -04005767
5768/* notification functions */
5769
5770void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5771{
5772 struct sk_buff *msg;
5773
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005774 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005775 if (!msg)
5776 return;
5777
5778 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5779 nlmsg_free(msg);
5780 return;
5781 }
5782
Johannes Berg463d0182009-07-14 00:33:35 +02005783 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5784 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005785}
5786
Johannes Berg362a4152009-05-24 16:43:15 +02005787static int nl80211_add_scan_req(struct sk_buff *msg,
5788 struct cfg80211_registered_device *rdev)
5789{
5790 struct cfg80211_scan_request *req = rdev->scan_req;
5791 struct nlattr *nest;
5792 int i;
5793
Johannes Berg667503d2009-07-07 03:56:11 +02005794 ASSERT_RDEV_LOCK(rdev);
5795
Johannes Berg362a4152009-05-24 16:43:15 +02005796 if (WARN_ON(!req))
5797 return 0;
5798
5799 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5800 if (!nest)
5801 goto nla_put_failure;
5802 for (i = 0; i < req->n_ssids; i++)
5803 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5804 nla_nest_end(msg, nest);
5805
5806 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5807 if (!nest)
5808 goto nla_put_failure;
5809 for (i = 0; i < req->n_channels; i++)
5810 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5811 nla_nest_end(msg, nest);
5812
5813 if (req->ie)
5814 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5815
5816 return 0;
5817 nla_put_failure:
5818 return -ENOBUFS;
5819}
5820
Johannes Berga538e2d2009-06-16 19:56:42 +02005821static int nl80211_send_scan_msg(struct sk_buff *msg,
5822 struct cfg80211_registered_device *rdev,
5823 struct net_device *netdev,
5824 u32 pid, u32 seq, int flags,
5825 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005826{
5827 void *hdr;
5828
5829 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5830 if (!hdr)
5831 return -1;
5832
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005833 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005834 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5835
Johannes Berg362a4152009-05-24 16:43:15 +02005836 /* ignore errors and send incomplete event anyway */
5837 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005838
5839 return genlmsg_end(msg, hdr);
5840
5841 nla_put_failure:
5842 genlmsg_cancel(msg, hdr);
5843 return -EMSGSIZE;
5844}
5845
Luciano Coelho807f8a82011-05-11 17:09:35 +03005846static int
5847nl80211_send_sched_scan_msg(struct sk_buff *msg,
5848 struct cfg80211_registered_device *rdev,
5849 struct net_device *netdev,
5850 u32 pid, u32 seq, int flags, u32 cmd)
5851{
5852 void *hdr;
5853
5854 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5855 if (!hdr)
5856 return -1;
5857
5858 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5859 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5860
5861 return genlmsg_end(msg, hdr);
5862
5863 nla_put_failure:
5864 genlmsg_cancel(msg, hdr);
5865 return -EMSGSIZE;
5866}
5867
Johannes Berga538e2d2009-06-16 19:56:42 +02005868void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5869 struct net_device *netdev)
5870{
5871 struct sk_buff *msg;
5872
5873 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5874 if (!msg)
5875 return;
5876
5877 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5878 NL80211_CMD_TRIGGER_SCAN) < 0) {
5879 nlmsg_free(msg);
5880 return;
5881 }
5882
Johannes Berg463d0182009-07-14 00:33:35 +02005883 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5884 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005885}
5886
Johannes Berg2a519312009-02-10 21:25:55 +01005887void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5888 struct net_device *netdev)
5889{
5890 struct sk_buff *msg;
5891
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005892 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005893 if (!msg)
5894 return;
5895
Johannes Berga538e2d2009-06-16 19:56:42 +02005896 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5897 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005898 nlmsg_free(msg);
5899 return;
5900 }
5901
Johannes Berg463d0182009-07-14 00:33:35 +02005902 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5903 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005904}
5905
5906void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5907 struct net_device *netdev)
5908{
5909 struct sk_buff *msg;
5910
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005911 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005912 if (!msg)
5913 return;
5914
Johannes Berga538e2d2009-06-16 19:56:42 +02005915 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5916 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005917 nlmsg_free(msg);
5918 return;
5919 }
5920
Johannes Berg463d0182009-07-14 00:33:35 +02005921 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5922 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005923}
5924
Luciano Coelho807f8a82011-05-11 17:09:35 +03005925void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
5926 struct net_device *netdev)
5927{
5928 struct sk_buff *msg;
5929
5930 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5931 if (!msg)
5932 return;
5933
5934 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
5935 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
5936 nlmsg_free(msg);
5937 return;
5938 }
5939
5940 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5941 nl80211_scan_mcgrp.id, GFP_KERNEL);
5942}
5943
5944void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
5945 struct net_device *netdev, u32 cmd)
5946{
5947 struct sk_buff *msg;
5948
5949 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5950 if (!msg)
5951 return;
5952
5953 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
5954 nlmsg_free(msg);
5955 return;
5956 }
5957
5958 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5959 nl80211_scan_mcgrp.id, GFP_KERNEL);
5960}
5961
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005962/*
5963 * This can happen on global regulatory changes or device specific settings
5964 * based on custom world regulatory domains.
5965 */
5966void nl80211_send_reg_change_event(struct regulatory_request *request)
5967{
5968 struct sk_buff *msg;
5969 void *hdr;
5970
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005971 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005972 if (!msg)
5973 return;
5974
5975 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5976 if (!hdr) {
5977 nlmsg_free(msg);
5978 return;
5979 }
5980
5981 /* Userspace can always count this one always being set */
5982 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5983
5984 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5985 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5986 NL80211_REGDOM_TYPE_WORLD);
5987 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5988 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5989 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5990 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5991 request->intersect)
5992 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5993 NL80211_REGDOM_TYPE_INTERSECTION);
5994 else {
5995 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5996 NL80211_REGDOM_TYPE_COUNTRY);
5997 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5998 }
5999
6000 if (wiphy_idx_valid(request->wiphy_idx))
6001 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6002
6003 if (genlmsg_end(msg, hdr) < 0) {
6004 nlmsg_free(msg);
6005 return;
6006 }
6007
Johannes Bergbc43b282009-07-25 10:54:13 +02006008 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006009 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006010 GFP_ATOMIC);
6011 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006012
6013 return;
6014
6015nla_put_failure:
6016 genlmsg_cancel(msg, hdr);
6017 nlmsg_free(msg);
6018}
6019
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006020static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6021 struct net_device *netdev,
6022 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006023 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006024{
6025 struct sk_buff *msg;
6026 void *hdr;
6027
Johannes Berge6d6e342009-07-01 21:26:47 +02006028 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006029 if (!msg)
6030 return;
6031
6032 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6033 if (!hdr) {
6034 nlmsg_free(msg);
6035 return;
6036 }
6037
6038 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6039 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6040 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6041
6042 if (genlmsg_end(msg, hdr) < 0) {
6043 nlmsg_free(msg);
6044 return;
6045 }
6046
Johannes Berg463d0182009-07-14 00:33:35 +02006047 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6048 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006049 return;
6050
6051 nla_put_failure:
6052 genlmsg_cancel(msg, hdr);
6053 nlmsg_free(msg);
6054}
6055
6056void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006057 struct net_device *netdev, const u8 *buf,
6058 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006059{
6060 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006061 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006062}
6063
6064void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6065 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006066 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006067{
Johannes Berge6d6e342009-07-01 21:26:47 +02006068 nl80211_send_mlme_event(rdev, netdev, buf, len,
6069 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006070}
6071
Jouni Malinen53b46b82009-03-27 20:53:56 +02006072void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006073 struct net_device *netdev, const u8 *buf,
6074 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006075{
6076 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006077 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006078}
6079
Jouni Malinen53b46b82009-03-27 20:53:56 +02006080void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6081 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006082 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006083{
6084 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006085 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006086}
6087
Jouni Malinencf4e5942010-12-16 00:52:40 +02006088void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6089 struct net_device *netdev, const u8 *buf,
6090 size_t len, gfp_t gfp)
6091{
6092 nl80211_send_mlme_event(rdev, netdev, buf, len,
6093 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6094}
6095
6096void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6097 struct net_device *netdev, const u8 *buf,
6098 size_t len, gfp_t gfp)
6099{
6100 nl80211_send_mlme_event(rdev, netdev, buf, len,
6101 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6102}
6103
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006104static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6105 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006106 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006107{
6108 struct sk_buff *msg;
6109 void *hdr;
6110
Johannes Berge6d6e342009-07-01 21:26:47 +02006111 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006112 if (!msg)
6113 return;
6114
6115 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6116 if (!hdr) {
6117 nlmsg_free(msg);
6118 return;
6119 }
6120
6121 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6122 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6123 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6124 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6125
6126 if (genlmsg_end(msg, hdr) < 0) {
6127 nlmsg_free(msg);
6128 return;
6129 }
6130
Johannes Berg463d0182009-07-14 00:33:35 +02006131 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6132 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006133 return;
6134
6135 nla_put_failure:
6136 genlmsg_cancel(msg, hdr);
6137 nlmsg_free(msg);
6138}
6139
6140void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006141 struct net_device *netdev, const u8 *addr,
6142 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006143{
6144 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006145 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006146}
6147
6148void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006149 struct net_device *netdev, const u8 *addr,
6150 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006151{
Johannes Berge6d6e342009-07-01 21:26:47 +02006152 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6153 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006154}
6155
Samuel Ortizb23aa672009-07-01 21:26:54 +02006156void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6157 struct net_device *netdev, const u8 *bssid,
6158 const u8 *req_ie, size_t req_ie_len,
6159 const u8 *resp_ie, size_t resp_ie_len,
6160 u16 status, gfp_t gfp)
6161{
6162 struct sk_buff *msg;
6163 void *hdr;
6164
6165 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6166 if (!msg)
6167 return;
6168
6169 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6170 if (!hdr) {
6171 nlmsg_free(msg);
6172 return;
6173 }
6174
6175 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6176 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6177 if (bssid)
6178 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6179 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6180 if (req_ie)
6181 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6182 if (resp_ie)
6183 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6184
6185 if (genlmsg_end(msg, hdr) < 0) {
6186 nlmsg_free(msg);
6187 return;
6188 }
6189
Johannes Berg463d0182009-07-14 00:33:35 +02006190 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6191 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006192 return;
6193
6194 nla_put_failure:
6195 genlmsg_cancel(msg, hdr);
6196 nlmsg_free(msg);
6197
6198}
6199
6200void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6201 struct net_device *netdev, const u8 *bssid,
6202 const u8 *req_ie, size_t req_ie_len,
6203 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6204{
6205 struct sk_buff *msg;
6206 void *hdr;
6207
6208 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6209 if (!msg)
6210 return;
6211
6212 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6213 if (!hdr) {
6214 nlmsg_free(msg);
6215 return;
6216 }
6217
6218 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6219 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6220 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6221 if (req_ie)
6222 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6223 if (resp_ie)
6224 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6225
6226 if (genlmsg_end(msg, hdr) < 0) {
6227 nlmsg_free(msg);
6228 return;
6229 }
6230
Johannes Berg463d0182009-07-14 00:33:35 +02006231 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6232 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006233 return;
6234
6235 nla_put_failure:
6236 genlmsg_cancel(msg, hdr);
6237 nlmsg_free(msg);
6238
6239}
6240
6241void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6242 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02006243 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006244{
6245 struct sk_buff *msg;
6246 void *hdr;
6247
Johannes Berg667503d2009-07-07 03:56:11 +02006248 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006249 if (!msg)
6250 return;
6251
6252 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6253 if (!hdr) {
6254 nlmsg_free(msg);
6255 return;
6256 }
6257
6258 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6259 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6260 if (from_ap && reason)
6261 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6262 if (from_ap)
6263 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6264 if (ie)
6265 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6266
6267 if (genlmsg_end(msg, hdr) < 0) {
6268 nlmsg_free(msg);
6269 return;
6270 }
6271
Johannes Berg463d0182009-07-14 00:33:35 +02006272 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6273 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006274 return;
6275
6276 nla_put_failure:
6277 genlmsg_cancel(msg, hdr);
6278 nlmsg_free(msg);
6279
6280}
6281
Johannes Berg04a773a2009-04-19 21:24:32 +02006282void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6283 struct net_device *netdev, const u8 *bssid,
6284 gfp_t gfp)
6285{
6286 struct sk_buff *msg;
6287 void *hdr;
6288
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006289 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006290 if (!msg)
6291 return;
6292
6293 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6294 if (!hdr) {
6295 nlmsg_free(msg);
6296 return;
6297 }
6298
6299 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6300 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6301 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6302
6303 if (genlmsg_end(msg, hdr) < 0) {
6304 nlmsg_free(msg);
6305 return;
6306 }
6307
Johannes Berg463d0182009-07-14 00:33:35 +02006308 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6309 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006310 return;
6311
6312 nla_put_failure:
6313 genlmsg_cancel(msg, hdr);
6314 nlmsg_free(msg);
6315}
6316
Javier Cardonac93b5e72011-04-07 15:08:34 -07006317void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6318 struct net_device *netdev,
6319 const u8 *macaddr, const u8* ie, u8 ie_len,
6320 gfp_t gfp)
6321{
6322 struct sk_buff *msg;
6323 void *hdr;
6324
6325 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6326 if (!msg)
6327 return;
6328
6329 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6330 if (!hdr) {
6331 nlmsg_free(msg);
6332 return;
6333 }
6334
6335 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6336 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6337 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6338 if (ie_len && ie)
6339 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6340
6341 if (genlmsg_end(msg, hdr) < 0) {
6342 nlmsg_free(msg);
6343 return;
6344 }
6345
6346 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6347 nl80211_mlme_mcgrp.id, gfp);
6348 return;
6349
6350 nla_put_failure:
6351 genlmsg_cancel(msg, hdr);
6352 nlmsg_free(msg);
6353}
6354
Jouni Malinena3b8b052009-03-27 21:59:49 +02006355void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6356 struct net_device *netdev, const u8 *addr,
6357 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02006358 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02006359{
6360 struct sk_buff *msg;
6361 void *hdr;
6362
Johannes Berge6d6e342009-07-01 21:26:47 +02006363 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006364 if (!msg)
6365 return;
6366
6367 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
6368 if (!hdr) {
6369 nlmsg_free(msg);
6370 return;
6371 }
6372
6373 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6374 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6375 if (addr)
6376 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6377 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
6378 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
6379 if (tsc)
6380 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
6381
6382 if (genlmsg_end(msg, hdr) < 0) {
6383 nlmsg_free(msg);
6384 return;
6385 }
6386
Johannes Berg463d0182009-07-14 00:33:35 +02006387 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6388 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006389 return;
6390
6391 nla_put_failure:
6392 genlmsg_cancel(msg, hdr);
6393 nlmsg_free(msg);
6394}
6395
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006396void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
6397 struct ieee80211_channel *channel_before,
6398 struct ieee80211_channel *channel_after)
6399{
6400 struct sk_buff *msg;
6401 void *hdr;
6402 struct nlattr *nl_freq;
6403
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006405 if (!msg)
6406 return;
6407
6408 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
6409 if (!hdr) {
6410 nlmsg_free(msg);
6411 return;
6412 }
6413
6414 /*
6415 * Since we are applying the beacon hint to a wiphy we know its
6416 * wiphy_idx is valid
6417 */
6418 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
6419
6420 /* Before */
6421 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
6422 if (!nl_freq)
6423 goto nla_put_failure;
6424 if (nl80211_msg_put_channel(msg, channel_before))
6425 goto nla_put_failure;
6426 nla_nest_end(msg, nl_freq);
6427
6428 /* After */
6429 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
6430 if (!nl_freq)
6431 goto nla_put_failure;
6432 if (nl80211_msg_put_channel(msg, channel_after))
6433 goto nla_put_failure;
6434 nla_nest_end(msg, nl_freq);
6435
6436 if (genlmsg_end(msg, hdr) < 0) {
6437 nlmsg_free(msg);
6438 return;
6439 }
6440
Johannes Berg463d0182009-07-14 00:33:35 +02006441 rcu_read_lock();
6442 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6443 GFP_ATOMIC);
6444 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006445
6446 return;
6447
6448nla_put_failure:
6449 genlmsg_cancel(msg, hdr);
6450 nlmsg_free(msg);
6451}
6452
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006453static void nl80211_send_remain_on_chan_event(
6454 int cmd, struct cfg80211_registered_device *rdev,
6455 struct net_device *netdev, u64 cookie,
6456 struct ieee80211_channel *chan,
6457 enum nl80211_channel_type channel_type,
6458 unsigned int duration, gfp_t gfp)
6459{
6460 struct sk_buff *msg;
6461 void *hdr;
6462
6463 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6464 if (!msg)
6465 return;
6466
6467 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6468 if (!hdr) {
6469 nlmsg_free(msg);
6470 return;
6471 }
6472
6473 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6474 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6475 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
6476 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
6477 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6478
6479 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
6480 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
6481
6482 if (genlmsg_end(msg, hdr) < 0) {
6483 nlmsg_free(msg);
6484 return;
6485 }
6486
6487 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6488 nl80211_mlme_mcgrp.id, gfp);
6489 return;
6490
6491 nla_put_failure:
6492 genlmsg_cancel(msg, hdr);
6493 nlmsg_free(msg);
6494}
6495
6496void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
6497 struct net_device *netdev, u64 cookie,
6498 struct ieee80211_channel *chan,
6499 enum nl80211_channel_type channel_type,
6500 unsigned int duration, gfp_t gfp)
6501{
6502 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
6503 rdev, netdev, cookie, chan,
6504 channel_type, duration, gfp);
6505}
6506
6507void nl80211_send_remain_on_channel_cancel(
6508 struct cfg80211_registered_device *rdev, struct net_device *netdev,
6509 u64 cookie, struct ieee80211_channel *chan,
6510 enum nl80211_channel_type channel_type, gfp_t gfp)
6511{
6512 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6513 rdev, netdev, cookie, chan,
6514 channel_type, 0, gfp);
6515}
6516
Johannes Berg98b62182009-12-23 13:15:44 +01006517void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
6518 struct net_device *dev, const u8 *mac_addr,
6519 struct station_info *sinfo, gfp_t gfp)
6520{
6521 struct sk_buff *msg;
6522
6523 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6524 if (!msg)
6525 return;
6526
6527 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
6528 nlmsg_free(msg);
6529 return;
6530 }
6531
6532 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6533 nl80211_mlme_mcgrp.id, gfp);
6534}
6535
Jouni Malinenec15e682011-03-23 15:29:52 +02006536void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
6537 struct net_device *dev, const u8 *mac_addr,
6538 gfp_t gfp)
6539{
6540 struct sk_buff *msg;
6541 void *hdr;
6542
6543 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6544 if (!msg)
6545 return;
6546
6547 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
6548 if (!hdr) {
6549 nlmsg_free(msg);
6550 return;
6551 }
6552
6553 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
6554 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
6555
6556 if (genlmsg_end(msg, hdr) < 0) {
6557 nlmsg_free(msg);
6558 return;
6559 }
6560
6561 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6562 nl80211_mlme_mcgrp.id, gfp);
6563 return;
6564
6565 nla_put_failure:
6566 genlmsg_cancel(msg, hdr);
6567 nlmsg_free(msg);
6568}
6569
Johannes Berg2e161f72010-08-12 15:38:38 +02006570int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
6571 struct net_device *netdev, u32 nlpid,
6572 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006573{
6574 struct sk_buff *msg;
6575 void *hdr;
6576 int err;
6577
6578 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6579 if (!msg)
6580 return -ENOMEM;
6581
Johannes Berg2e161f72010-08-12 15:38:38 +02006582 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006583 if (!hdr) {
6584 nlmsg_free(msg);
6585 return -ENOMEM;
6586 }
6587
6588 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6589 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6590 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6591 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6592
6593 err = genlmsg_end(msg, hdr);
6594 if (err < 0) {
6595 nlmsg_free(msg);
6596 return err;
6597 }
6598
6599 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
6600 if (err < 0)
6601 return err;
6602 return 0;
6603
6604 nla_put_failure:
6605 genlmsg_cancel(msg, hdr);
6606 nlmsg_free(msg);
6607 return -ENOBUFS;
6608}
6609
Johannes Berg2e161f72010-08-12 15:38:38 +02006610void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
6611 struct net_device *netdev, u64 cookie,
6612 const u8 *buf, size_t len, bool ack,
6613 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006614{
6615 struct sk_buff *msg;
6616 void *hdr;
6617
6618 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6619 if (!msg)
6620 return;
6621
Johannes Berg2e161f72010-08-12 15:38:38 +02006622 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02006623 if (!hdr) {
6624 nlmsg_free(msg);
6625 return;
6626 }
6627
6628 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6629 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6630 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6631 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6632 if (ack)
6633 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
6634
6635 if (genlmsg_end(msg, hdr) < 0) {
6636 nlmsg_free(msg);
6637 return;
6638 }
6639
6640 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
6641 return;
6642
6643 nla_put_failure:
6644 genlmsg_cancel(msg, hdr);
6645 nlmsg_free(msg);
6646}
6647
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006648void
6649nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
6650 struct net_device *netdev,
6651 enum nl80211_cqm_rssi_threshold_event rssi_event,
6652 gfp_t gfp)
6653{
6654 struct sk_buff *msg;
6655 struct nlattr *pinfoattr;
6656 void *hdr;
6657
6658 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6659 if (!msg)
6660 return;
6661
6662 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6663 if (!hdr) {
6664 nlmsg_free(msg);
6665 return;
6666 }
6667
6668 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6669 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6670
6671 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6672 if (!pinfoattr)
6673 goto nla_put_failure;
6674
6675 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
6676 rssi_event);
6677
6678 nla_nest_end(msg, pinfoattr);
6679
6680 if (genlmsg_end(msg, hdr) < 0) {
6681 nlmsg_free(msg);
6682 return;
6683 }
6684
6685 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6686 nl80211_mlme_mcgrp.id, gfp);
6687 return;
6688
6689 nla_put_failure:
6690 genlmsg_cancel(msg, hdr);
6691 nlmsg_free(msg);
6692}
6693
Johannes Bergc063dbf2010-11-24 08:10:05 +01006694void
6695nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
6696 struct net_device *netdev, const u8 *peer,
6697 u32 num_packets, gfp_t gfp)
6698{
6699 struct sk_buff *msg;
6700 struct nlattr *pinfoattr;
6701 void *hdr;
6702
6703 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6704 if (!msg)
6705 return;
6706
6707 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6708 if (!hdr) {
6709 nlmsg_free(msg);
6710 return;
6711 }
6712
6713 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6714 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6715 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
6716
6717 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6718 if (!pinfoattr)
6719 goto nla_put_failure;
6720
6721 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
6722
6723 nla_nest_end(msg, pinfoattr);
6724
6725 if (genlmsg_end(msg, hdr) < 0) {
6726 nlmsg_free(msg);
6727 return;
6728 }
6729
6730 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6731 nl80211_mlme_mcgrp.id, gfp);
6732 return;
6733
6734 nla_put_failure:
6735 genlmsg_cancel(msg, hdr);
6736 nlmsg_free(msg);
6737}
6738
Jouni Malinen026331c2010-02-15 12:53:10 +02006739static int nl80211_netlink_notify(struct notifier_block * nb,
6740 unsigned long state,
6741 void *_notify)
6742{
6743 struct netlink_notify *notify = _notify;
6744 struct cfg80211_registered_device *rdev;
6745 struct wireless_dev *wdev;
6746
6747 if (state != NETLINK_URELEASE)
6748 return NOTIFY_DONE;
6749
6750 rcu_read_lock();
6751
6752 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
6753 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02006754 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02006755
6756 rcu_read_unlock();
6757
6758 return NOTIFY_DONE;
6759}
6760
6761static struct notifier_block nl80211_netlink_notifier = {
6762 .notifier_call = nl80211_netlink_notify,
6763};
6764
Johannes Berg55682962007-09-20 13:09:35 -04006765/* initialisation/exit functions */
6766
6767int nl80211_init(void)
6768{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006769 int err;
Johannes Berg55682962007-09-20 13:09:35 -04006770
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006771 err = genl_register_family_with_ops(&nl80211_fam,
6772 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04006773 if (err)
6774 return err;
6775
Johannes Berg55682962007-09-20 13:09:35 -04006776 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6777 if (err)
6778 goto err_out;
6779
Johannes Berg2a519312009-02-10 21:25:55 +01006780 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6781 if (err)
6782 goto err_out;
6783
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006784 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6785 if (err)
6786 goto err_out;
6787
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006788 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6789 if (err)
6790 goto err_out;
6791
Johannes Bergaff89a92009-07-01 21:26:51 +02006792#ifdef CONFIG_NL80211_TESTMODE
6793 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6794 if (err)
6795 goto err_out;
6796#endif
6797
Jouni Malinen026331c2010-02-15 12:53:10 +02006798 err = netlink_register_notifier(&nl80211_netlink_notifier);
6799 if (err)
6800 goto err_out;
6801
Johannes Berg55682962007-09-20 13:09:35 -04006802 return 0;
6803 err_out:
6804 genl_unregister_family(&nl80211_fam);
6805 return err;
6806}
6807
6808void nl80211_exit(void)
6809{
Jouni Malinen026331c2010-02-15 12:53:10 +02006810 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006811 genl_unregister_family(&nl80211_fam);
6812}