blob: 1ac9443b5265c11f1a22af4cac1e720688ba8d1a [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 Malinendc6382c2009-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 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300178 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400179};
180
Johannes Berge31b8212010-10-05 19:39:30 +0200181/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000182static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200183 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200184 [NL80211_KEY_IDX] = { .type = NLA_U8 },
185 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
186 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
187 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
188 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200189 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100190 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
191};
192
193/* policy for the key default flags */
194static const struct nla_policy
195nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
196 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
197 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200198};
199
Johannes Bergff1b6e62011-05-04 15:37:28 +0200200/* policy for WoWLAN attributes */
201static const struct nla_policy
202nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
203 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
204 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
205 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
206 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
207};
208
Holger Schuriga0438972009-11-11 11:30:02 +0100209/* ifidx get helper */
210static int nl80211_get_ifidx(struct netlink_callback *cb)
211{
212 int res;
213
214 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
215 nl80211_fam.attrbuf, nl80211_fam.maxattr,
216 nl80211_policy);
217 if (res)
218 return res;
219
220 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
221 return -EINVAL;
222
223 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
224 if (!res)
225 return -EINVAL;
226 return res;
227}
228
Johannes Berg67748892010-10-04 21:14:06 +0200229static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
230 struct netlink_callback *cb,
231 struct cfg80211_registered_device **rdev,
232 struct net_device **dev)
233{
234 int ifidx = cb->args[0];
235 int err;
236
237 if (!ifidx)
238 ifidx = nl80211_get_ifidx(cb);
239 if (ifidx < 0)
240 return ifidx;
241
242 cb->args[0] = ifidx;
243
244 rtnl_lock();
245
246 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
247 if (!*dev) {
248 err = -ENODEV;
249 goto out_rtnl;
250 }
251
252 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100253 if (IS_ERR(*rdev)) {
254 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200255 goto out_rtnl;
256 }
257
258 return 0;
259 out_rtnl:
260 rtnl_unlock();
261 return err;
262}
263
264static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
265{
266 cfg80211_unlock_rdev(rdev);
267 rtnl_unlock();
268}
269
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100270/* IE validation */
271static bool is_valid_ie_attr(const struct nlattr *attr)
272{
273 const u8 *pos;
274 int len;
275
276 if (!attr)
277 return true;
278
279 pos = nla_data(attr);
280 len = nla_len(attr);
281
282 while (len) {
283 u8 elemlen;
284
285 if (len < 2)
286 return false;
287 len -= 2;
288
289 elemlen = pos[1];
290 if (elemlen > len)
291 return false;
292
293 len -= elemlen;
294 pos += 2 + elemlen;
295 }
296
297 return true;
298}
299
Johannes Berg55682962007-09-20 13:09:35 -0400300/* message building helper */
301static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
302 int flags, u8 cmd)
303{
304 /* since there is no private header just add the generic one */
305 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
306}
307
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400308static int nl80211_msg_put_channel(struct sk_buff *msg,
309 struct ieee80211_channel *chan)
310{
311 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
312 chan->center_freq);
313
314 if (chan->flags & IEEE80211_CHAN_DISABLED)
315 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
316 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
317 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
318 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
319 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
320 if (chan->flags & IEEE80211_CHAN_RADAR)
321 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
322
323 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
324 DBM_TO_MBM(chan->max_power));
325
326 return 0;
327
328 nla_put_failure:
329 return -ENOBUFS;
330}
331
Johannes Berg55682962007-09-20 13:09:35 -0400332/* netlink command implementations */
333
Johannes Bergb9454e82009-07-08 13:29:08 +0200334struct key_parse {
335 struct key_params p;
336 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200337 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200338 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100339 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200340};
341
342static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
343{
344 struct nlattr *tb[NL80211_KEY_MAX + 1];
345 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
346 nl80211_key_policy);
347 if (err)
348 return err;
349
350 k->def = !!tb[NL80211_KEY_DEFAULT];
351 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
352
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100353 if (k->def) {
354 k->def_uni = true;
355 k->def_multi = true;
356 }
357 if (k->defmgmt)
358 k->def_multi = true;
359
Johannes Bergb9454e82009-07-08 13:29:08 +0200360 if (tb[NL80211_KEY_IDX])
361 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
362
363 if (tb[NL80211_KEY_DATA]) {
364 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
365 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
366 }
367
368 if (tb[NL80211_KEY_SEQ]) {
369 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
370 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
371 }
372
373 if (tb[NL80211_KEY_CIPHER])
374 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
375
Johannes Berge31b8212010-10-05 19:39:30 +0200376 if (tb[NL80211_KEY_TYPE]) {
377 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
378 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
379 return -EINVAL;
380 }
381
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100382 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
383 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
384 int err = nla_parse_nested(kdt,
385 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
386 tb[NL80211_KEY_DEFAULT_TYPES],
387 nl80211_key_default_policy);
388 if (err)
389 return err;
390
391 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
392 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
393 }
394
Johannes Bergb9454e82009-07-08 13:29:08 +0200395 return 0;
396}
397
398static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
399{
400 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
401 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
402 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
403 }
404
405 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
406 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
407 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
408 }
409
410 if (info->attrs[NL80211_ATTR_KEY_IDX])
411 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
412
413 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
414 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
415
416 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
417 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
418
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100419 if (k->def) {
420 k->def_uni = true;
421 k->def_multi = true;
422 }
423 if (k->defmgmt)
424 k->def_multi = true;
425
Johannes Berge31b8212010-10-05 19:39:30 +0200426 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
427 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
428 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
429 return -EINVAL;
430 }
431
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100432 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
433 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
434 int err = nla_parse_nested(
435 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
436 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
437 nl80211_key_default_policy);
438 if (err)
439 return err;
440
441 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
442 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
443 }
444
Johannes Bergb9454e82009-07-08 13:29:08 +0200445 return 0;
446}
447
448static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
449{
450 int err;
451
452 memset(k, 0, sizeof(*k));
453 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200454 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200455
456 if (info->attrs[NL80211_ATTR_KEY])
457 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
458 else
459 err = nl80211_parse_key_old(info, k);
460
461 if (err)
462 return err;
463
464 if (k->def && k->defmgmt)
465 return -EINVAL;
466
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100467 if (k->defmgmt) {
468 if (k->def_uni || !k->def_multi)
469 return -EINVAL;
470 }
471
Johannes Bergb9454e82009-07-08 13:29:08 +0200472 if (k->idx != -1) {
473 if (k->defmgmt) {
474 if (k->idx < 4 || k->idx > 5)
475 return -EINVAL;
476 } else if (k->def) {
477 if (k->idx < 0 || k->idx > 3)
478 return -EINVAL;
479 } else {
480 if (k->idx < 0 || k->idx > 5)
481 return -EINVAL;
482 }
483 }
484
485 return 0;
486}
487
Johannes Bergfffd0932009-07-08 14:22:54 +0200488static struct cfg80211_cached_keys *
489nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
490 struct nlattr *keys)
491{
492 struct key_parse parse;
493 struct nlattr *key;
494 struct cfg80211_cached_keys *result;
495 int rem, err, def = 0;
496
497 result = kzalloc(sizeof(*result), GFP_KERNEL);
498 if (!result)
499 return ERR_PTR(-ENOMEM);
500
501 result->def = -1;
502 result->defmgmt = -1;
503
504 nla_for_each_nested(key, keys, rem) {
505 memset(&parse, 0, sizeof(parse));
506 parse.idx = -1;
507
508 err = nl80211_parse_key_new(key, &parse);
509 if (err)
510 goto error;
511 err = -EINVAL;
512 if (!parse.p.key)
513 goto error;
514 if (parse.idx < 0 || parse.idx > 4)
515 goto error;
516 if (parse.def) {
517 if (def)
518 goto error;
519 def = 1;
520 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100521 if (!parse.def_uni || !parse.def_multi)
522 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200523 } else if (parse.defmgmt)
524 goto error;
525 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200526 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200527 if (err)
528 goto error;
529 result->params[parse.idx].cipher = parse.p.cipher;
530 result->params[parse.idx].key_len = parse.p.key_len;
531 result->params[parse.idx].key = result->data[parse.idx];
532 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
533 }
534
535 return result;
536 error:
537 kfree(result);
538 return ERR_PTR(err);
539}
540
541static int nl80211_key_allowed(struct wireless_dev *wdev)
542{
543 ASSERT_WDEV_LOCK(wdev);
544
Johannes Bergfffd0932009-07-08 14:22:54 +0200545 switch (wdev->iftype) {
546 case NL80211_IFTYPE_AP:
547 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200548 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700549 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200550 break;
551 case NL80211_IFTYPE_ADHOC:
552 if (!wdev->current_bss)
553 return -ENOLINK;
554 break;
555 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200556 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200557 if (wdev->sme_state != CFG80211_SME_CONNECTED)
558 return -ENOLINK;
559 break;
560 default:
561 return -EINVAL;
562 }
563
564 return 0;
565}
566
Johannes Berg7527a782011-05-13 10:58:57 +0200567static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
568{
569 struct nlattr *nl_modes = nla_nest_start(msg, attr);
570 int i;
571
572 if (!nl_modes)
573 goto nla_put_failure;
574
575 i = 0;
576 while (ifmodes) {
577 if (ifmodes & 1)
578 NLA_PUT_FLAG(msg, i);
579 ifmodes >>= 1;
580 i++;
581 }
582
583 nla_nest_end(msg, nl_modes);
584 return 0;
585
586nla_put_failure:
587 return -ENOBUFS;
588}
589
590static int nl80211_put_iface_combinations(struct wiphy *wiphy,
591 struct sk_buff *msg)
592{
593 struct nlattr *nl_combis;
594 int i, j;
595
596 nl_combis = nla_nest_start(msg,
597 NL80211_ATTR_INTERFACE_COMBINATIONS);
598 if (!nl_combis)
599 goto nla_put_failure;
600
601 for (i = 0; i < wiphy->n_iface_combinations; i++) {
602 const struct ieee80211_iface_combination *c;
603 struct nlattr *nl_combi, *nl_limits;
604
605 c = &wiphy->iface_combinations[i];
606
607 nl_combi = nla_nest_start(msg, i + 1);
608 if (!nl_combi)
609 goto nla_put_failure;
610
611 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
612 if (!nl_limits)
613 goto nla_put_failure;
614
615 for (j = 0; j < c->n_limits; j++) {
616 struct nlattr *nl_limit;
617
618 nl_limit = nla_nest_start(msg, j + 1);
619 if (!nl_limit)
620 goto nla_put_failure;
621 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
622 c->limits[j].max);
623 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
624 c->limits[j].types))
625 goto nla_put_failure;
626 nla_nest_end(msg, nl_limit);
627 }
628
629 nla_nest_end(msg, nl_limits);
630
631 if (c->beacon_int_infra_match)
632 NLA_PUT_FLAG(msg,
633 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
634 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
635 c->num_different_channels);
636 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
637 c->max_interfaces);
638
639 nla_nest_end(msg, nl_combi);
640 }
641
642 nla_nest_end(msg, nl_combis);
643
644 return 0;
645nla_put_failure:
646 return -ENOBUFS;
647}
648
Johannes Berg55682962007-09-20 13:09:35 -0400649static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
650 struct cfg80211_registered_device *dev)
651{
652 void *hdr;
Johannes Bergee688b02008-01-24 19:38:39 +0100653 struct nlattr *nl_bands, *nl_band;
654 struct nlattr *nl_freqs, *nl_freq;
655 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100656 struct nlattr *nl_cmds;
Johannes Bergee688b02008-01-24 19:38:39 +0100657 enum ieee80211_band band;
658 struct ieee80211_channel *chan;
659 struct ieee80211_rate *rate;
660 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200661 const struct ieee80211_txrx_stypes *mgmt_stypes =
662 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400663
664 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
665 if (!hdr)
666 return -1;
667
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500668 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400669 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200670
Johannes Bergf5ea9122009-08-07 16:17:38 +0200671 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
672 cfg80211_rdev_list_generation);
673
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200674 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
675 dev->wiphy.retry_short);
676 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
677 dev->wiphy.retry_long);
678 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
679 dev->wiphy.frag_threshold);
680 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
681 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100682 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
683 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100684 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
685 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200686 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
687 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b02008-01-24 19:38:39 +0100688
Johannes Berge31b8212010-10-05 19:39:30 +0200689 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
690 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700691 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
692 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Johannes Berge31b8212010-10-05 19:39:30 +0200693
Johannes Berg25e47c12009-04-02 20:14:06 +0200694 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
695 sizeof(u32) * dev->wiphy.n_cipher_suites,
696 dev->wiphy.cipher_suites);
697
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100698 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
699 dev->wiphy.max_num_pmkids);
700
Johannes Bergc0692b82010-08-27 14:26:53 +0300701 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
702 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
703
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900704 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
705 dev->wiphy.available_antennas_tx);
706 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
707 dev->wiphy.available_antennas_rx);
708
Bruno Randolf7f531e02010-12-16 11:30:22 +0900709 if ((dev->wiphy.available_antennas_tx ||
710 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900711 u32 tx_ant = 0, rx_ant = 0;
712 int res;
713 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
714 if (!res) {
715 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
716 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
717 }
718 }
719
Johannes Berg7527a782011-05-13 10:58:57 +0200720 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
721 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700722 goto nla_put_failure;
723
Johannes Bergee688b02008-01-24 19:38:39 +0100724 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
725 if (!nl_bands)
726 goto nla_put_failure;
727
728 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
729 if (!dev->wiphy.bands[band])
730 continue;
731
732 nl_band = nla_nest_start(msg, band);
733 if (!nl_band)
734 goto nla_put_failure;
735
Johannes Bergd51626d2008-10-09 12:20:13 +0200736 /* add HT info */
737 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
738 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
739 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
740 &dev->wiphy.bands[band]->ht_cap.mcs);
741 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
742 dev->wiphy.bands[band]->ht_cap.cap);
743 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
744 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
745 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
746 dev->wiphy.bands[band]->ht_cap.ampdu_density);
747 }
748
Johannes Bergee688b02008-01-24 19:38:39 +0100749 /* add frequencies */
750 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
751 if (!nl_freqs)
752 goto nla_put_failure;
753
754 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
755 nl_freq = nla_nest_start(msg, i);
756 if (!nl_freq)
757 goto nla_put_failure;
758
759 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b02008-01-24 19:38:39 +0100760
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400761 if (nl80211_msg_put_channel(msg, chan))
762 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200763
Johannes Bergee688b02008-01-24 19:38:39 +0100764 nla_nest_end(msg, nl_freq);
765 }
766
767 nla_nest_end(msg, nl_freqs);
768
769 /* add bitrates */
770 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
771 if (!nl_rates)
772 goto nla_put_failure;
773
774 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
775 nl_rate = nla_nest_start(msg, i);
776 if (!nl_rate)
777 goto nla_put_failure;
778
779 rate = &dev->wiphy.bands[band]->bitrates[i];
780 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
781 rate->bitrate);
782 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
783 NLA_PUT_FLAG(msg,
784 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
785
786 nla_nest_end(msg, nl_rate);
787 }
788
789 nla_nest_end(msg, nl_rates);
790
791 nla_nest_end(msg, nl_band);
792 }
793 nla_nest_end(msg, nl_bands);
794
Johannes Berg8fdc6212009-03-14 09:34:01 +0100795 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
796 if (!nl_cmds)
797 goto nla_put_failure;
798
799 i = 0;
800#define CMD(op, n) \
801 do { \
802 if (dev->ops->op) { \
803 i++; \
804 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
805 } \
806 } while (0)
807
808 CMD(add_virtual_intf, NEW_INTERFACE);
809 CMD(change_virtual_intf, SET_INTERFACE);
810 CMD(add_key, NEW_KEY);
811 CMD(add_beacon, NEW_BEACON);
812 CMD(add_station, NEW_STATION);
813 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800814 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100815 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200816 CMD(auth, AUTHENTICATE);
817 CMD(assoc, ASSOCIATE);
818 CMD(deauth, DEAUTHENTICATE);
819 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200820 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100821 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100822 CMD(set_pmksa, SET_PMKSA);
823 CMD(del_pmksa, DEL_PMKSA);
824 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100825 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200826 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200827 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100828 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100829 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200830 i++;
831 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
832 }
Johannes Bergf444de02010-05-05 15:25:02 +0200833 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400834 CMD(set_wds_peer, SET_WDS_PEER);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300835 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
836 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100837
838#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200839
Johannes Berg6829c872009-07-02 09:13:27 +0200840 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200841 i++;
842 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
843 }
844
Johannes Berg6829c872009-07-02 09:13:27 +0200845 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200846 i++;
847 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
848 }
849
Johannes Berg8fdc6212009-03-14 09:34:01 +0100850 nla_nest_end(msg, nl_cmds);
851
Johannes Berga2939112010-12-14 17:54:28 +0100852 if (dev->ops->remain_on_channel)
853 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
854 dev->wiphy.max_remain_on_channel_duration);
855
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100856 /* for now at least assume all drivers have it */
857 if (dev->ops->mgmt_tx)
858 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
859
Johannes Berg2e161f72010-08-12 15:38:38 +0200860 if (mgmt_stypes) {
861 u16 stypes;
862 struct nlattr *nl_ftypes, *nl_ifs;
863 enum nl80211_iftype ift;
864
865 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
866 if (!nl_ifs)
867 goto nla_put_failure;
868
869 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
870 nl_ftypes = nla_nest_start(msg, ift);
871 if (!nl_ftypes)
872 goto nla_put_failure;
873 i = 0;
874 stypes = mgmt_stypes[ift].tx;
875 while (stypes) {
876 if (stypes & 1)
877 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
878 (i << 4) | IEEE80211_FTYPE_MGMT);
879 stypes >>= 1;
880 i++;
881 }
882 nla_nest_end(msg, nl_ftypes);
883 }
884
Johannes Berg74b70a42010-08-24 12:15:53 +0200885 nla_nest_end(msg, nl_ifs);
886
Johannes Berg2e161f72010-08-12 15:38:38 +0200887 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
888 if (!nl_ifs)
889 goto nla_put_failure;
890
891 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
892 nl_ftypes = nla_nest_start(msg, ift);
893 if (!nl_ftypes)
894 goto nla_put_failure;
895 i = 0;
896 stypes = mgmt_stypes[ift].rx;
897 while (stypes) {
898 if (stypes & 1)
899 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
900 (i << 4) | IEEE80211_FTYPE_MGMT);
901 stypes >>= 1;
902 i++;
903 }
904 nla_nest_end(msg, nl_ftypes);
905 }
906 nla_nest_end(msg, nl_ifs);
907 }
908
Johannes Bergff1b6e62011-05-04 15:37:28 +0200909 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
910 struct nlattr *nl_wowlan;
911
912 nl_wowlan = nla_nest_start(msg,
913 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
914 if (!nl_wowlan)
915 goto nla_put_failure;
916
917 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
918 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
919 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
920 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
921 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
922 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
923 if (dev->wiphy.wowlan.n_patterns) {
924 struct nl80211_wowlan_pattern_support pat = {
925 .max_patterns = dev->wiphy.wowlan.n_patterns,
926 .min_pattern_len =
927 dev->wiphy.wowlan.pattern_min_len,
928 .max_pattern_len =
929 dev->wiphy.wowlan.pattern_max_len,
930 };
931 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
932 sizeof(pat), &pat);
933 }
934
935 nla_nest_end(msg, nl_wowlan);
936 }
937
Johannes Berg7527a782011-05-13 10:58:57 +0200938 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
939 dev->wiphy.software_iftypes))
940 goto nla_put_failure;
941
942 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
943 goto nla_put_failure;
944
Johannes Berg55682962007-09-20 13:09:35 -0400945 return genlmsg_end(msg, hdr);
946
947 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700948 genlmsg_cancel(msg, hdr);
949 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400950}
951
952static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
953{
954 int idx = 0;
955 int start = cb->args[0];
956 struct cfg80211_registered_device *dev;
957
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500958 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200959 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200960 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
961 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200962 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400963 continue;
964 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
965 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200966 dev) < 0) {
967 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400968 break;
Julius Volzb4637272008-07-08 14:02:19 +0200969 }
Johannes Berg55682962007-09-20 13:09:35 -0400970 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500971 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400972
973 cb->args[0] = idx;
974
975 return skb->len;
976}
977
978static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
979{
980 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200981 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400982
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700983 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400984 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200985 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400986
Johannes Berg4c476992010-10-04 21:36:35 +0200987 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
988 nlmsg_free(msg);
989 return -ENOBUFS;
990 }
Johannes Berg55682962007-09-20 13:09:35 -0400991
Johannes Berg134e6372009-07-10 09:51:34 +0000992 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400993}
994
Jouni Malinen31888482008-10-30 16:59:24 +0200995static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
996 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
997 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
998 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
999 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1000 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1001};
1002
1003static int parse_txq_params(struct nlattr *tb[],
1004 struct ieee80211_txq_params *txq_params)
1005{
1006 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1007 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1008 !tb[NL80211_TXQ_ATTR_AIFS])
1009 return -EINVAL;
1010
1011 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1012 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1013 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1014 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1015 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1016
1017 return 0;
1018}
1019
Johannes Bergf444de02010-05-05 15:25:02 +02001020static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1021{
1022 /*
1023 * You can only set the channel explicitly for AP, mesh
1024 * and WDS type interfaces; all others have their channel
1025 * managed via their respective "establish a connection"
1026 * command (connect, join, ...)
1027 *
1028 * Monitors are special as they are normally slaved to
1029 * whatever else is going on, so they behave as though
1030 * you tried setting the wiphy channel itself.
1031 */
1032 return !wdev ||
1033 wdev->iftype == NL80211_IFTYPE_AP ||
1034 wdev->iftype == NL80211_IFTYPE_WDS ||
1035 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001036 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1037 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001038}
1039
1040static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1041 struct wireless_dev *wdev,
1042 struct genl_info *info)
1043{
1044 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1045 u32 freq;
1046 int result;
1047
1048 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1049 return -EINVAL;
1050
1051 if (!nl80211_can_set_dev_channel(wdev))
1052 return -EOPNOTSUPP;
1053
1054 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1055 channel_type = nla_get_u32(info->attrs[
1056 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1057 if (channel_type != NL80211_CHAN_NO_HT &&
1058 channel_type != NL80211_CHAN_HT20 &&
1059 channel_type != NL80211_CHAN_HT40PLUS &&
1060 channel_type != NL80211_CHAN_HT40MINUS)
1061 return -EINVAL;
1062 }
1063
1064 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1065
1066 mutex_lock(&rdev->devlist_mtx);
1067 if (wdev) {
1068 wdev_lock(wdev);
1069 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1070 wdev_unlock(wdev);
1071 } else {
1072 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1073 }
1074 mutex_unlock(&rdev->devlist_mtx);
1075
1076 return result;
1077}
1078
1079static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1080{
Johannes Berg4c476992010-10-04 21:36:35 +02001081 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1082 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001083
Johannes Berg4c476992010-10-04 21:36:35 +02001084 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001085}
1086
Bill Jordane8347eb2010-10-01 13:54:28 -04001087static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1088{
Johannes Berg43b19952010-10-07 13:10:30 +02001089 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1090 struct net_device *dev = info->user_ptr[1];
1091 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001092 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001093
1094 if (!info->attrs[NL80211_ATTR_MAC])
1095 return -EINVAL;
1096
Johannes Berg43b19952010-10-07 13:10:30 +02001097 if (netif_running(dev))
1098 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001099
Johannes Berg43b19952010-10-07 13:10:30 +02001100 if (!rdev->ops->set_wds_peer)
1101 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001102
Johannes Berg43b19952010-10-07 13:10:30 +02001103 if (wdev->iftype != NL80211_IFTYPE_WDS)
1104 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001105
1106 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001107 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001108}
1109
1110
Johannes Berg55682962007-09-20 13:09:35 -04001111static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1112{
1113 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001114 struct net_device *netdev = NULL;
1115 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001116 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001117 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001118 u32 changed;
1119 u8 retry_short = 0, retry_long = 0;
1120 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001121 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001122
Johannes Bergf444de02010-05-05 15:25:02 +02001123 /*
1124 * Try to find the wiphy and netdev. Normally this
1125 * function shouldn't need the netdev, but this is
1126 * done for backward compatibility -- previously
1127 * setting the channel was done per wiphy, but now
1128 * it is per netdev. Previous userland like hostapd
1129 * also passed a netdev to set_wiphy, so that it is
1130 * possible to let that go to the right netdev!
1131 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001132 mutex_lock(&cfg80211_mutex);
1133
Johannes Bergf444de02010-05-05 15:25:02 +02001134 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1135 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1136
1137 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1138 if (netdev && netdev->ieee80211_ptr) {
1139 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1140 mutex_lock(&rdev->mtx);
1141 } else
1142 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001143 }
1144
Johannes Bergf444de02010-05-05 15:25:02 +02001145 if (!netdev) {
1146 rdev = __cfg80211_rdev_from_info(info);
1147 if (IS_ERR(rdev)) {
1148 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001149 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001150 }
1151 wdev = NULL;
1152 netdev = NULL;
1153 result = 0;
1154
1155 mutex_lock(&rdev->mtx);
1156 } else if (netif_running(netdev) &&
1157 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1158 wdev = netdev->ieee80211_ptr;
1159 else
1160 wdev = NULL;
1161
1162 /*
1163 * end workaround code, by now the rdev is available
1164 * and locked, and wdev may or may not be NULL.
1165 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001166
1167 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001168 result = cfg80211_dev_rename(
1169 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001170
1171 mutex_unlock(&cfg80211_mutex);
1172
1173 if (result)
1174 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001175
Jouni Malinen31888482008-10-30 16:59:24 +02001176 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1177 struct ieee80211_txq_params txq_params;
1178 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1179
1180 if (!rdev->ops->set_txq_params) {
1181 result = -EOPNOTSUPP;
1182 goto bad_res;
1183 }
1184
1185 nla_for_each_nested(nl_txq_params,
1186 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1187 rem_txq_params) {
1188 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1189 nla_data(nl_txq_params),
1190 nla_len(nl_txq_params),
1191 txq_params_policy);
1192 result = parse_txq_params(tb, &txq_params);
1193 if (result)
1194 goto bad_res;
1195
1196 result = rdev->ops->set_txq_params(&rdev->wiphy,
1197 &txq_params);
1198 if (result)
1199 goto bad_res;
1200 }
1201 }
1202
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001203 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001204 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001205 if (result)
1206 goto bad_res;
1207 }
1208
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001209 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1210 enum nl80211_tx_power_setting type;
1211 int idx, mbm = 0;
1212
1213 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001214 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001215 goto bad_res;
1216 }
1217
1218 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1219 type = nla_get_u32(info->attrs[idx]);
1220
1221 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1222 (type != NL80211_TX_POWER_AUTOMATIC)) {
1223 result = -EINVAL;
1224 goto bad_res;
1225 }
1226
1227 if (type != NL80211_TX_POWER_AUTOMATIC) {
1228 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1229 mbm = nla_get_u32(info->attrs[idx]);
1230 }
1231
1232 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1233 if (result)
1234 goto bad_res;
1235 }
1236
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001237 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1238 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1239 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001240 if ((!rdev->wiphy.available_antennas_tx &&
1241 !rdev->wiphy.available_antennas_rx) ||
1242 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001243 result = -EOPNOTSUPP;
1244 goto bad_res;
1245 }
1246
1247 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1248 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1249
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001250 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001251 * available antenna masks, except for the "all" mask */
1252 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1253 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001254 result = -EINVAL;
1255 goto bad_res;
1256 }
1257
Bruno Randolf7f531e02010-12-16 11:30:22 +09001258 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1259 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001260
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001261 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1262 if (result)
1263 goto bad_res;
1264 }
1265
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001266 changed = 0;
1267
1268 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1269 retry_short = nla_get_u8(
1270 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1271 if (retry_short == 0) {
1272 result = -EINVAL;
1273 goto bad_res;
1274 }
1275 changed |= WIPHY_PARAM_RETRY_SHORT;
1276 }
1277
1278 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1279 retry_long = nla_get_u8(
1280 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1281 if (retry_long == 0) {
1282 result = -EINVAL;
1283 goto bad_res;
1284 }
1285 changed |= WIPHY_PARAM_RETRY_LONG;
1286 }
1287
1288 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1289 frag_threshold = nla_get_u32(
1290 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1291 if (frag_threshold < 256) {
1292 result = -EINVAL;
1293 goto bad_res;
1294 }
1295 if (frag_threshold != (u32) -1) {
1296 /*
1297 * Fragments (apart from the last one) are required to
1298 * have even length. Make the fragmentation code
1299 * simpler by stripping LSB should someone try to use
1300 * odd threshold value.
1301 */
1302 frag_threshold &= ~0x1;
1303 }
1304 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1305 }
1306
1307 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1308 rts_threshold = nla_get_u32(
1309 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1310 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1311 }
1312
Lukáš Turek81077e82009-12-21 22:50:47 +01001313 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1314 coverage_class = nla_get_u8(
1315 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1316 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1317 }
1318
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001319 if (changed) {
1320 u8 old_retry_short, old_retry_long;
1321 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001322 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001323
1324 if (!rdev->ops->set_wiphy_params) {
1325 result = -EOPNOTSUPP;
1326 goto bad_res;
1327 }
1328
1329 old_retry_short = rdev->wiphy.retry_short;
1330 old_retry_long = rdev->wiphy.retry_long;
1331 old_frag_threshold = rdev->wiphy.frag_threshold;
1332 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001333 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001334
1335 if (changed & WIPHY_PARAM_RETRY_SHORT)
1336 rdev->wiphy.retry_short = retry_short;
1337 if (changed & WIPHY_PARAM_RETRY_LONG)
1338 rdev->wiphy.retry_long = retry_long;
1339 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1340 rdev->wiphy.frag_threshold = frag_threshold;
1341 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1342 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001343 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1344 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001345
1346 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1347 if (result) {
1348 rdev->wiphy.retry_short = old_retry_short;
1349 rdev->wiphy.retry_long = old_retry_long;
1350 rdev->wiphy.frag_threshold = old_frag_threshold;
1351 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001352 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001353 }
1354 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001355
Johannes Berg306d6112008-12-08 12:39:04 +01001356 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001357 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001358 if (netdev)
1359 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001360 return result;
1361}
1362
1363
1364static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001365 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001366 struct net_device *dev)
1367{
1368 void *hdr;
1369
1370 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1371 if (!hdr)
1372 return -1;
1373
1374 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001375 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001376 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001377 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001378
1379 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1380 rdev->devlist_generation ^
1381 (cfg80211_rdev_list_generation << 2));
1382
Johannes Berg55682962007-09-20 13:09:35 -04001383 return genlmsg_end(msg, hdr);
1384
1385 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001386 genlmsg_cancel(msg, hdr);
1387 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001388}
1389
1390static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1391{
1392 int wp_idx = 0;
1393 int if_idx = 0;
1394 int wp_start = cb->args[0];
1395 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001396 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001397 struct wireless_dev *wdev;
1398
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001399 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001400 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1401 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001402 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001403 if (wp_idx < wp_start) {
1404 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001405 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001406 }
Johannes Berg55682962007-09-20 13:09:35 -04001407 if_idx = 0;
1408
Johannes Bergf5ea9122009-08-07 16:17:38 +02001409 mutex_lock(&rdev->devlist_mtx);
1410 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001411 if (if_idx < if_start) {
1412 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001413 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001414 }
Johannes Berg55682962007-09-20 13:09:35 -04001415 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1416 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001417 rdev, wdev->netdev) < 0) {
1418 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001419 goto out;
1420 }
1421 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001422 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001423 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001424
1425 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001426 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001427 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001428 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001429
1430 cb->args[0] = wp_idx;
1431 cb->args[1] = if_idx;
1432
1433 return skb->len;
1434}
1435
1436static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1437{
1438 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001439 struct cfg80211_registered_device *dev = info->user_ptr[0];
1440 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001441
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001442 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001443 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001444 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001445
Johannes Bergd7264052009-04-19 16:23:20 +02001446 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001447 dev, netdev) < 0) {
1448 nlmsg_free(msg);
1449 return -ENOBUFS;
1450 }
Johannes Berg55682962007-09-20 13:09:35 -04001451
Johannes Berg134e6372009-07-10 09:51:34 +00001452 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001453}
1454
Michael Wu66f7ac52008-01-31 19:48:22 +01001455static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1456 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1457 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1458 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1459 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1460 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1461};
1462
1463static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1464{
1465 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1466 int flag;
1467
1468 *mntrflags = 0;
1469
1470 if (!nla)
1471 return -EINVAL;
1472
1473 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1474 nla, mntr_flags_policy))
1475 return -EINVAL;
1476
1477 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1478 if (flags[flag])
1479 *mntrflags |= (1<<flag);
1480
1481 return 0;
1482}
1483
Johannes Berg9bc383d2009-11-19 11:55:19 +01001484static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001485 struct net_device *netdev, u8 use_4addr,
1486 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001487{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001488 if (!use_4addr) {
Jiri Pirkof350a0a2010-06-15 06:50:45 +00001489 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001490 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001491 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001492 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001493
1494 switch (iftype) {
1495 case NL80211_IFTYPE_AP_VLAN:
1496 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1497 return 0;
1498 break;
1499 case NL80211_IFTYPE_STATION:
1500 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1501 return 0;
1502 break;
1503 default:
1504 break;
1505 }
1506
1507 return -EOPNOTSUPP;
1508}
1509
Johannes Berg55682962007-09-20 13:09:35 -04001510static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1511{
Johannes Berg4c476992010-10-04 21:36:35 +02001512 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001513 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001514 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001515 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001516 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001517 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001518 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001519
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001520 memset(&params, 0, sizeof(params));
1521
Johannes Berg04a773a2009-04-19 21:24:32 +02001522 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001523
Johannes Berg723b0382008-09-16 20:22:09 +02001524 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001525 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001526 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001527 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001528 if (ntype > NL80211_IFTYPE_MAX)
1529 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001530 }
1531
Johannes Berg92ffe052008-09-16 20:39:36 +02001532 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001533 struct wireless_dev *wdev = dev->ieee80211_ptr;
1534
Johannes Berg4c476992010-10-04 21:36:35 +02001535 if (ntype != NL80211_IFTYPE_MESH_POINT)
1536 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001537 if (netif_running(dev))
1538 return -EBUSY;
1539
1540 wdev_lock(wdev);
1541 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1542 IEEE80211_MAX_MESH_ID_LEN);
1543 wdev->mesh_id_up_len =
1544 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1545 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1546 wdev->mesh_id_up_len);
1547 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001548 }
1549
Felix Fietkau8b787642009-11-10 18:53:10 +01001550 if (info->attrs[NL80211_ATTR_4ADDR]) {
1551 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1552 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001553 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001554 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001555 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001556 } else {
1557 params.use_4addr = -1;
1558 }
1559
Johannes Berg92ffe052008-09-16 20:39:36 +02001560 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001561 if (ntype != NL80211_IFTYPE_MONITOR)
1562 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001563 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1564 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001565 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001566 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001567
1568 flags = &_flags;
1569 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001570 }
Johannes Berg3b858752009-03-12 09:55:09 +01001571
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001572 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001573 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001574 else
1575 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001576
Johannes Berg9bc383d2009-11-19 11:55:19 +01001577 if (!err && params.use_4addr != -1)
1578 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1579
Johannes Berg55682962007-09-20 13:09:35 -04001580 return err;
1581}
1582
1583static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1584{
Johannes Berg4c476992010-10-04 21:36:35 +02001585 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001586 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001587 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001588 int err;
1589 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001590 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001591
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001592 memset(&params, 0, sizeof(params));
1593
Johannes Berg55682962007-09-20 13:09:35 -04001594 if (!info->attrs[NL80211_ATTR_IFNAME])
1595 return -EINVAL;
1596
1597 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1598 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1599 if (type > NL80211_IFTYPE_MAX)
1600 return -EINVAL;
1601 }
1602
Johannes Berg79c97e92009-07-07 03:56:12 +02001603 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001604 !(rdev->wiphy.interface_modes & (1 << type)))
1605 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001606
Johannes Berg9bc383d2009-11-19 11:55:19 +01001607 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001608 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001609 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001610 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001611 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001612 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001613
Michael Wu66f7ac52008-01-31 19:48:22 +01001614 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1615 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1616 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001617 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001618 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001619 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001620 if (IS_ERR(dev))
1621 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001622
Johannes Berg29cbe682010-12-03 09:20:44 +01001623 if (type == NL80211_IFTYPE_MESH_POINT &&
1624 info->attrs[NL80211_ATTR_MESH_ID]) {
1625 struct wireless_dev *wdev = dev->ieee80211_ptr;
1626
1627 wdev_lock(wdev);
1628 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1629 IEEE80211_MAX_MESH_ID_LEN);
1630 wdev->mesh_id_up_len =
1631 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1632 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1633 wdev->mesh_id_up_len);
1634 wdev_unlock(wdev);
1635 }
1636
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001637 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001638}
1639
1640static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1641{
Johannes Berg4c476992010-10-04 21:36:35 +02001642 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1643 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001644
Johannes Berg4c476992010-10-04 21:36:35 +02001645 if (!rdev->ops->del_virtual_intf)
1646 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001647
Johannes Berg4c476992010-10-04 21:36:35 +02001648 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001649}
1650
Johannes Berg41ade002007-12-19 02:03:29 +01001651struct get_key_cookie {
1652 struct sk_buff *msg;
1653 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001654 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001655};
1656
1657static void get_key_callback(void *c, struct key_params *params)
1658{
Johannes Bergb9454e82009-07-08 13:29:08 +02001659 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001660 struct get_key_cookie *cookie = c;
1661
1662 if (params->key)
1663 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1664 params->key_len, params->key);
1665
1666 if (params->seq)
1667 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1668 params->seq_len, params->seq);
1669
1670 if (params->cipher)
1671 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1672 params->cipher);
1673
Johannes Bergb9454e82009-07-08 13:29:08 +02001674 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1675 if (!key)
1676 goto nla_put_failure;
1677
1678 if (params->key)
1679 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1680 params->key_len, params->key);
1681
1682 if (params->seq)
1683 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1684 params->seq_len, params->seq);
1685
1686 if (params->cipher)
1687 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1688 params->cipher);
1689
1690 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1691
1692 nla_nest_end(cookie->msg, key);
1693
Johannes Berg41ade002007-12-19 02:03:29 +01001694 return;
1695 nla_put_failure:
1696 cookie->error = 1;
1697}
1698
1699static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1700{
Johannes Berg4c476992010-10-04 21:36:35 +02001701 struct cfg80211_registered_device *rdev = info->user_ptr[0];
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 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001705 const u8 *mac_addr = NULL;
1706 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001707 struct get_key_cookie cookie = {
1708 .error = 0,
1709 };
1710 void *hdr;
1711 struct sk_buff *msg;
1712
1713 if (info->attrs[NL80211_ATTR_KEY_IDX])
1714 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1715
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +02001716 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001717 return -EINVAL;
1718
1719 if (info->attrs[NL80211_ATTR_MAC])
1720 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1721
Johannes Berge31b8212010-10-05 19:39:30 +02001722 pairwise = !!mac_addr;
1723 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1724 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1725 if (kt >= NUM_NL80211_KEYTYPES)
1726 return -EINVAL;
1727 if (kt != NL80211_KEYTYPE_GROUP &&
1728 kt != NL80211_KEYTYPE_PAIRWISE)
1729 return -EINVAL;
1730 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1731 }
1732
Johannes Berg4c476992010-10-04 21:36:35 +02001733 if (!rdev->ops->get_key)
1734 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001735
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001736 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001737 if (!msg)
1738 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001739
1740 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1741 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001742 if (IS_ERR(hdr))
1743 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001744
1745 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001746 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001747
1748 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1749 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1750 if (mac_addr)
1751 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1752
Johannes Berge31b8212010-10-05 19:39:30 +02001753 if (pairwise && mac_addr &&
1754 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1755 return -ENOENT;
1756
1757 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1758 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001759
1760 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001761 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001762
1763 if (cookie.error)
1764 goto nla_put_failure;
1765
1766 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001767 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001768
1769 nla_put_failure:
1770 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001771 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001772 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001773 return err;
1774}
1775
1776static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1777{
Johannes Berg4c476992010-10-04 21:36:35 +02001778 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001779 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001780 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001781 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001782
Johannes Bergb9454e82009-07-08 13:29:08 +02001783 err = nl80211_parse_key(info, &key);
1784 if (err)
1785 return err;
1786
1787 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001788 return -EINVAL;
1789
Johannes Bergb9454e82009-07-08 13:29:08 +02001790 /* only support setting default key */
1791 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001792 return -EINVAL;
1793
Johannes Bergfffd0932009-07-08 14:22:54 +02001794 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001795
1796 if (key.def) {
1797 if (!rdev->ops->set_default_key) {
1798 err = -EOPNOTSUPP;
1799 goto out;
1800 }
1801
1802 err = nl80211_key_allowed(dev->ieee80211_ptr);
1803 if (err)
1804 goto out;
1805
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001806 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1807 key.def_uni, key.def_multi);
1808
1809 if (err)
1810 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001811
Johannes Berg3d23e342009-09-29 23:27:28 +02001812#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001813 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001814#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001815 } else {
1816 if (key.def_uni || !key.def_multi) {
1817 err = -EINVAL;
1818 goto out;
1819 }
1820
1821 if (!rdev->ops->set_default_mgmt_key) {
1822 err = -EOPNOTSUPP;
1823 goto out;
1824 }
1825
1826 err = nl80211_key_allowed(dev->ieee80211_ptr);
1827 if (err)
1828 goto out;
1829
1830 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1831 dev, key.idx);
1832 if (err)
1833 goto out;
1834
1835#ifdef CONFIG_CFG80211_WEXT
1836 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1837#endif
1838 }
1839
1840 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001841 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001842
Johannes Berg41ade002007-12-19 02:03:29 +01001843 return err;
1844}
1845
1846static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1847{
Johannes Berg4c476992010-10-04 21:36:35 +02001848 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001849 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001850 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001851 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001852 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001853
Johannes Bergb9454e82009-07-08 13:29:08 +02001854 err = nl80211_parse_key(info, &key);
1855 if (err)
1856 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001857
Johannes Bergb9454e82009-07-08 13:29:08 +02001858 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001859 return -EINVAL;
1860
Johannes Berg41ade002007-12-19 02:03:29 +01001861 if (info->attrs[NL80211_ATTR_MAC])
1862 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1863
Johannes Berge31b8212010-10-05 19:39:30 +02001864 if (key.type == -1) {
1865 if (mac_addr)
1866 key.type = NL80211_KEYTYPE_PAIRWISE;
1867 else
1868 key.type = NL80211_KEYTYPE_GROUP;
1869 }
1870
1871 /* for now */
1872 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1873 key.type != NL80211_KEYTYPE_GROUP)
1874 return -EINVAL;
1875
Johannes Berg4c476992010-10-04 21:36:35 +02001876 if (!rdev->ops->add_key)
1877 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001878
Johannes Berge31b8212010-10-05 19:39:30 +02001879 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1880 key.type == NL80211_KEYTYPE_PAIRWISE,
1881 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001882 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001883
1884 wdev_lock(dev->ieee80211_ptr);
1885 err = nl80211_key_allowed(dev->ieee80211_ptr);
1886 if (!err)
1887 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001888 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001889 mac_addr, &key.p);
1890 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001891
Johannes Berg41ade002007-12-19 02:03:29 +01001892 return err;
1893}
1894
1895static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1896{
Johannes Berg4c476992010-10-04 21:36:35 +02001897 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001898 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001899 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001900 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001901 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001902
Johannes Bergb9454e82009-07-08 13:29:08 +02001903 err = nl80211_parse_key(info, &key);
1904 if (err)
1905 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001906
1907 if (info->attrs[NL80211_ATTR_MAC])
1908 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1909
Johannes Berge31b8212010-10-05 19:39:30 +02001910 if (key.type == -1) {
1911 if (mac_addr)
1912 key.type = NL80211_KEYTYPE_PAIRWISE;
1913 else
1914 key.type = NL80211_KEYTYPE_GROUP;
1915 }
1916
1917 /* for now */
1918 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1919 key.type != NL80211_KEYTYPE_GROUP)
1920 return -EINVAL;
1921
Johannes Berg4c476992010-10-04 21:36:35 +02001922 if (!rdev->ops->del_key)
1923 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001924
Johannes Bergfffd0932009-07-08 14:22:54 +02001925 wdev_lock(dev->ieee80211_ptr);
1926 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001927
1928 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1929 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1930 err = -ENOENT;
1931
Johannes Bergfffd0932009-07-08 14:22:54 +02001932 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001933 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1934 key.type == NL80211_KEYTYPE_PAIRWISE,
1935 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001936
Johannes Berg3d23e342009-09-29 23:27:28 +02001937#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001938 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001939 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001940 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001941 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001942 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1943 }
1944#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001945 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001946
Johannes Berg41ade002007-12-19 02:03:29 +01001947 return err;
1948}
1949
Johannes Berged1b6cc2007-12-19 02:03:32 +01001950static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1951{
1952 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1953 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001954 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1955 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02001956 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001957 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02001958 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001959
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001960 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1961 return -EINVAL;
1962
Johannes Berg074ac8d2010-09-16 14:58:22 +02001963 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001964 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1965 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001966
Johannes Berg56d18932011-05-09 18:41:15 +02001967 memset(&params, 0, sizeof(params));
1968
Johannes Berged1b6cc2007-12-19 02:03:32 +01001969 switch (info->genlhdr->cmd) {
1970 case NL80211_CMD_NEW_BEACON:
1971 /* these are required for NEW_BEACON */
1972 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1973 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001974 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1975 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001976
Johannes Berg56d18932011-05-09 18:41:15 +02001977 params.interval =
1978 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1979 params.dtim_period =
1980 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1981
1982 err = cfg80211_validate_beacon_int(rdev, params.interval);
1983 if (err)
1984 return err;
1985
Johannes Berg79c97e92009-07-07 03:56:12 +02001986 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001987 break;
1988 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001989 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001990 break;
1991 default:
1992 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001993 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001994 }
1995
Johannes Berg4c476992010-10-04 21:36:35 +02001996 if (!call)
1997 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001998
Johannes Berged1b6cc2007-12-19 02:03:32 +01001999 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2000 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2001 params.head_len =
2002 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2003 haveinfo = 1;
2004 }
2005
2006 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2007 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2008 params.tail_len =
2009 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2010 haveinfo = 1;
2011 }
2012
Johannes Berg4c476992010-10-04 21:36:35 +02002013 if (!haveinfo)
2014 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002015
Johannes Berg56d18932011-05-09 18:41:15 +02002016 err = call(&rdev->wiphy, dev, &params);
2017 if (!err && params.interval)
2018 wdev->beacon_interval = params.interval;
2019 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002020}
2021
2022static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2023{
Johannes Berg4c476992010-10-04 21:36:35 +02002024 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2025 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002026 struct wireless_dev *wdev = dev->ieee80211_ptr;
2027 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002028
Johannes Berg4c476992010-10-04 21:36:35 +02002029 if (!rdev->ops->del_beacon)
2030 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002031
Johannes Berg074ac8d2010-09-16 14:58:22 +02002032 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002033 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2034 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002035
Johannes Berg56d18932011-05-09 18:41:15 +02002036 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2037 if (!err)
2038 wdev->beacon_interval = 0;
2039 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002040}
2041
Johannes Berg5727ef12007-12-19 02:03:34 +01002042static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2043 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2044 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2045 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002046 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002047 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002048};
2049
Johannes Bergeccb8e82009-05-11 21:57:56 +03002050static int parse_station_flags(struct genl_info *info,
2051 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002052{
2053 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002054 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002055 int flag;
2056
Johannes Bergeccb8e82009-05-11 21:57:56 +03002057 /*
2058 * Try parsing the new attribute first so userspace
2059 * can specify both for older kernels.
2060 */
2061 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2062 if (nla) {
2063 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002064
Johannes Bergeccb8e82009-05-11 21:57:56 +03002065 sta_flags = nla_data(nla);
2066 params->sta_flags_mask = sta_flags->mask;
2067 params->sta_flags_set = sta_flags->set;
2068 if ((params->sta_flags_mask |
2069 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2070 return -EINVAL;
2071 return 0;
2072 }
2073
2074 /* if present, parse the old attribute */
2075
2076 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002077 if (!nla)
2078 return 0;
2079
2080 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2081 nla, sta_flags_policy))
2082 return -EINVAL;
2083
Johannes Bergeccb8e82009-05-11 21:57:56 +03002084 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2085 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002086
2087 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2088 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002089 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002090
2091 return 0;
2092}
2093
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002094static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2095 int attr)
2096{
2097 struct nlattr *rate;
2098 u16 bitrate;
2099
2100 rate = nla_nest_start(msg, attr);
2101 if (!rate)
2102 goto nla_put_failure;
2103
2104 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2105 bitrate = cfg80211_calculate_bitrate(info);
2106 if (bitrate > 0)
2107 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2108
2109 if (info->flags & RATE_INFO_FLAGS_MCS)
2110 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2111 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2112 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2113 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2114 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2115
2116 nla_nest_end(msg, rate);
2117 return true;
2118
2119nla_put_failure:
2120 return false;
2121}
2122
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002123static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2124 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002125 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002126{
2127 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002128 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002129
2130 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2131 if (!hdr)
2132 return -1;
2133
2134 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2135 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2136
Johannes Bergf5ea9122009-08-07 16:17:38 +02002137 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2138
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002139 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2140 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002141 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302142 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2143 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2144 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002145 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2146 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2147 sinfo->inactive_time);
2148 if (sinfo->filled & STATION_INFO_RX_BYTES)
2149 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2150 sinfo->rx_bytes);
2151 if (sinfo->filled & STATION_INFO_TX_BYTES)
2152 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2153 sinfo->tx_bytes);
2154 if (sinfo->filled & STATION_INFO_LLID)
2155 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2156 sinfo->llid);
2157 if (sinfo->filled & STATION_INFO_PLID)
2158 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2159 sinfo->plid);
2160 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2161 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2162 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002163 if (sinfo->filled & STATION_INFO_SIGNAL)
2164 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2165 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002166 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2167 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2168 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002169 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002170 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2171 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002172 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002173 }
2174 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2175 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2176 NL80211_STA_INFO_RX_BITRATE))
2177 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002178 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002179 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2180 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2181 sinfo->rx_packets);
2182 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2183 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2184 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002185 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2186 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2187 sinfo->tx_retries);
2188 if (sinfo->filled & STATION_INFO_TX_FAILED)
2189 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2190 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002191 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2192 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2193 if (!bss_param)
2194 goto nla_put_failure;
2195
2196 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2197 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2198 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2199 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2200 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2201 NLA_PUT_FLAG(msg,
2202 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2203 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2204 sinfo->bss_param.dtim_period);
2205 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2206 sinfo->bss_param.beacon_interval);
2207
2208 nla_nest_end(msg, bss_param);
2209 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002210 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002211
2212 return genlmsg_end(msg, hdr);
2213
2214 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002215 genlmsg_cancel(msg, hdr);
2216 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002217}
2218
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002219static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002220 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002221{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002222 struct station_info sinfo;
2223 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002224 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002225 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002226 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002227 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002228
Johannes Berg67748892010-10-04 21:14:06 +02002229 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2230 if (err)
2231 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002232
2233 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002234 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002235 goto out_err;
2236 }
2237
Johannes Bergbba95fe2008-07-29 13:22:51 +02002238 while (1) {
2239 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2240 mac_addr, &sinfo);
2241 if (err == -ENOENT)
2242 break;
2243 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002244 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002245
2246 if (nl80211_send_station(skb,
2247 NETLINK_CB(cb->skb).pid,
2248 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2249 netdev, mac_addr,
2250 &sinfo) < 0)
2251 goto out;
2252
2253 sta_idx++;
2254 }
2255
2256
2257 out:
2258 cb->args[1] = sta_idx;
2259 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002260 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002261 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002262
2263 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002264}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002265
Johannes Berg5727ef12007-12-19 02:03:34 +01002266static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2267{
Johannes Berg4c476992010-10-04 21:36:35 +02002268 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2269 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002270 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002271 struct sk_buff *msg;
2272 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002273 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002274
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002275 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002276
2277 if (!info->attrs[NL80211_ATTR_MAC])
2278 return -EINVAL;
2279
2280 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2281
Johannes Berg4c476992010-10-04 21:36:35 +02002282 if (!rdev->ops->get_station)
2283 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002284
Johannes Berg79c97e92009-07-07 03:56:12 +02002285 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002286 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002287 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002288
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002289 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002290 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002291 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002292
2293 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002294 dev, mac_addr, &sinfo) < 0) {
2295 nlmsg_free(msg);
2296 return -ENOBUFS;
2297 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002298
Johannes Berg4c476992010-10-04 21:36:35 +02002299 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002300}
2301
2302/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002303 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002304 */
Johannes Berg463d0182009-07-14 00:33:35 +02002305static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002306 struct cfg80211_registered_device *rdev,
2307 struct net_device **vlan)
2308{
Johannes Berg463d0182009-07-14 00:33:35 +02002309 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002310 *vlan = NULL;
2311
2312 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002313 *vlan = dev_get_by_index(genl_info_net(info),
2314 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002315 if (!*vlan)
2316 return -ENODEV;
2317 if (!(*vlan)->ieee80211_ptr)
2318 return -EINVAL;
2319 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2320 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002321 if (!netif_running(*vlan))
2322 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002323 }
2324 return 0;
2325}
2326
2327static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2328{
Johannes Berg4c476992010-10-04 21:36:35 +02002329 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002330 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002331 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002332 struct station_parameters params;
2333 u8 *mac_addr = NULL;
2334
2335 memset(&params, 0, sizeof(params));
2336
2337 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002338 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002339
2340 if (info->attrs[NL80211_ATTR_STA_AID])
2341 return -EINVAL;
2342
2343 if (!info->attrs[NL80211_ATTR_MAC])
2344 return -EINVAL;
2345
2346 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2347
2348 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2349 params.supported_rates =
2350 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2351 params.supported_rates_len =
2352 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2353 }
2354
2355 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2356 params.listen_interval =
2357 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2358
Jouni Malinen36aedc92008-08-25 11:58:58 +03002359 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2360 params.ht_capa =
2361 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2362
Johannes Bergeccb8e82009-05-11 21:57:56 +03002363 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002364 return -EINVAL;
2365
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002366 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2367 params.plink_action =
2368 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2369
Javier Cardona9c3990a2011-05-03 16:57:11 -07002370 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2371 params.plink_state =
2372 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2373
Johannes Berg463d0182009-07-14 00:33:35 +02002374 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002375 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002376 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002377
2378 /* validate settings */
2379 err = 0;
2380
2381 switch (dev->ieee80211_ptr->iftype) {
2382 case NL80211_IFTYPE_AP:
2383 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002384 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002385 /* disallow mesh-specific things */
2386 if (params.plink_action)
2387 err = -EINVAL;
2388 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002389 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002390 case NL80211_IFTYPE_STATION:
2391 /* disallow everything but AUTHORIZED flag */
2392 if (params.plink_action)
2393 err = -EINVAL;
2394 if (params.vlan)
2395 err = -EINVAL;
2396 if (params.supported_rates)
2397 err = -EINVAL;
2398 if (params.ht_capa)
2399 err = -EINVAL;
2400 if (params.listen_interval >= 0)
2401 err = -EINVAL;
2402 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2403 err = -EINVAL;
2404 break;
2405 case NL80211_IFTYPE_MESH_POINT:
2406 /* disallow things mesh doesn't support */
2407 if (params.vlan)
2408 err = -EINVAL;
2409 if (params.ht_capa)
2410 err = -EINVAL;
2411 if (params.listen_interval >= 0)
2412 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002413 if (params.sta_flags_mask &
2414 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002415 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002416 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002417 err = -EINVAL;
2418 break;
2419 default:
2420 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002421 }
2422
Johannes Berg5727ef12007-12-19 02:03:34 +01002423 if (err)
2424 goto out;
2425
Johannes Berg79c97e92009-07-07 03:56:12 +02002426 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002427 err = -EOPNOTSUPP;
2428 goto out;
2429 }
2430
Johannes Berg79c97e92009-07-07 03:56:12 +02002431 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002432
2433 out:
2434 if (params.vlan)
2435 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002436
Johannes Berg5727ef12007-12-19 02:03:34 +01002437 return err;
2438}
2439
2440static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2441{
Johannes Berg4c476992010-10-04 21:36:35 +02002442 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002443 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002444 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002445 struct station_parameters params;
2446 u8 *mac_addr = NULL;
2447
2448 memset(&params, 0, sizeof(params));
2449
2450 if (!info->attrs[NL80211_ATTR_MAC])
2451 return -EINVAL;
2452
Johannes Berg5727ef12007-12-19 02:03:34 +01002453 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2454 return -EINVAL;
2455
2456 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2457 return -EINVAL;
2458
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002459 if (!info->attrs[NL80211_ATTR_STA_AID])
2460 return -EINVAL;
2461
Johannes Berg5727ef12007-12-19 02:03:34 +01002462 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2463 params.supported_rates =
2464 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2465 params.supported_rates_len =
2466 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2467 params.listen_interval =
2468 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002469
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002470 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2471 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2472 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002473
Jouni Malinen36aedc92008-08-25 11:58:58 +03002474 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2475 params.ht_capa =
2476 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002477
Javier Cardona96b78df2011-04-07 15:08:33 -07002478 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2479 params.plink_action =
2480 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2481
Johannes Bergeccb8e82009-05-11 21:57:56 +03002482 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002483 return -EINVAL;
2484
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002485 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002486 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002487 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002488 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2489 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002490
Johannes Berg463d0182009-07-14 00:33:35 +02002491 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002492 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002493 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002494
2495 /* validate settings */
2496 err = 0;
2497
Johannes Berg79c97e92009-07-07 03:56:12 +02002498 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002499 err = -EOPNOTSUPP;
2500 goto out;
2501 }
2502
Johannes Berg79c97e92009-07-07 03:56:12 +02002503 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002504
2505 out:
2506 if (params.vlan)
2507 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002508 return err;
2509}
2510
2511static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2512{
Johannes Berg4c476992010-10-04 21:36:35 +02002513 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2514 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002515 u8 *mac_addr = NULL;
2516
2517 if (info->attrs[NL80211_ATTR_MAC])
2518 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2519
Johannes Berge80cf852009-05-11 14:43:13 +02002520 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002521 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002522 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002523 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2524 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002525
Johannes Berg4c476992010-10-04 21:36:35 +02002526 if (!rdev->ops->del_station)
2527 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002528
Johannes Berg4c476992010-10-04 21:36:35 +02002529 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002530}
2531
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002532static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2533 int flags, struct net_device *dev,
2534 u8 *dst, u8 *next_hop,
2535 struct mpath_info *pinfo)
2536{
2537 void *hdr;
2538 struct nlattr *pinfoattr;
2539
2540 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2541 if (!hdr)
2542 return -1;
2543
2544 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2545 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2546 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2547
Johannes Bergf5ea9122009-08-07 16:17:38 +02002548 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2549
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002550 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2551 if (!pinfoattr)
2552 goto nla_put_failure;
2553 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2554 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2555 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002556 if (pinfo->filled & MPATH_INFO_SN)
2557 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2558 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002559 if (pinfo->filled & MPATH_INFO_METRIC)
2560 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2561 pinfo->metric);
2562 if (pinfo->filled & MPATH_INFO_EXPTIME)
2563 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2564 pinfo->exptime);
2565 if (pinfo->filled & MPATH_INFO_FLAGS)
2566 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2567 pinfo->flags);
2568 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2569 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2570 pinfo->discovery_timeout);
2571 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2572 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2573 pinfo->discovery_retries);
2574
2575 nla_nest_end(msg, pinfoattr);
2576
2577 return genlmsg_end(msg, hdr);
2578
2579 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002580 genlmsg_cancel(msg, hdr);
2581 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002582}
2583
2584static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002585 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002586{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002587 struct mpath_info pinfo;
2588 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002589 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002590 u8 dst[ETH_ALEN];
2591 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002592 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002593 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002594
Johannes Berg67748892010-10-04 21:14:06 +02002595 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2596 if (err)
2597 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002598
2599 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002600 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002601 goto out_err;
2602 }
2603
Jouni Malineneec60b02009-03-20 21:21:19 +02002604 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2605 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002606 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002607 }
2608
Johannes Bergbba95fe2008-07-29 13:22:51 +02002609 while (1) {
2610 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2611 dst, next_hop, &pinfo);
2612 if (err == -ENOENT)
2613 break;
2614 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002615 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002616
2617 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2618 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2619 netdev, dst, next_hop,
2620 &pinfo) < 0)
2621 goto out;
2622
2623 path_idx++;
2624 }
2625
2626
2627 out:
2628 cb->args[1] = path_idx;
2629 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002630 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002631 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002632 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002633}
2634
2635static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2636{
Johannes Berg4c476992010-10-04 21:36:35 +02002637 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002638 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002639 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002640 struct mpath_info pinfo;
2641 struct sk_buff *msg;
2642 u8 *dst = NULL;
2643 u8 next_hop[ETH_ALEN];
2644
2645 memset(&pinfo, 0, sizeof(pinfo));
2646
2647 if (!info->attrs[NL80211_ATTR_MAC])
2648 return -EINVAL;
2649
2650 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2651
Johannes Berg4c476992010-10-04 21:36:35 +02002652 if (!rdev->ops->get_mpath)
2653 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002654
Johannes Berg4c476992010-10-04 21:36:35 +02002655 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2656 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002657
Johannes Berg79c97e92009-07-07 03:56:12 +02002658 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002659 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002660 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002661
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002662 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002663 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002664 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002665
2666 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002667 dev, dst, next_hop, &pinfo) < 0) {
2668 nlmsg_free(msg);
2669 return -ENOBUFS;
2670 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002671
Johannes Berg4c476992010-10-04 21:36:35 +02002672 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002673}
2674
2675static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2676{
Johannes Berg4c476992010-10-04 21:36:35 +02002677 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2678 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002679 u8 *dst = NULL;
2680 u8 *next_hop = NULL;
2681
2682 if (!info->attrs[NL80211_ATTR_MAC])
2683 return -EINVAL;
2684
2685 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2686 return -EINVAL;
2687
2688 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2689 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2690
Johannes Berg4c476992010-10-04 21:36:35 +02002691 if (!rdev->ops->change_mpath)
2692 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002693
Johannes Berg4c476992010-10-04 21:36:35 +02002694 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2695 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002696
Johannes Berg4c476992010-10-04 21:36:35 +02002697 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002698}
Johannes Berg4c476992010-10-04 21:36:35 +02002699
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002700static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2701{
Johannes Berg4c476992010-10-04 21:36:35 +02002702 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2703 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002704 u8 *dst = NULL;
2705 u8 *next_hop = NULL;
2706
2707 if (!info->attrs[NL80211_ATTR_MAC])
2708 return -EINVAL;
2709
2710 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2711 return -EINVAL;
2712
2713 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2714 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2715
Johannes Berg4c476992010-10-04 21:36:35 +02002716 if (!rdev->ops->add_mpath)
2717 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002718
Johannes Berg4c476992010-10-04 21:36:35 +02002719 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2720 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002721
Johannes Berg4c476992010-10-04 21:36:35 +02002722 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002723}
2724
2725static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2726{
Johannes Berg4c476992010-10-04 21:36:35 +02002727 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2728 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002729 u8 *dst = NULL;
2730
2731 if (info->attrs[NL80211_ATTR_MAC])
2732 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2733
Johannes Berg4c476992010-10-04 21:36:35 +02002734 if (!rdev->ops->del_mpath)
2735 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002736
Johannes Berg4c476992010-10-04 21:36:35 +02002737 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002738}
2739
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002740static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2741{
Johannes Berg4c476992010-10-04 21:36:35 +02002742 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2743 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002744 struct bss_parameters params;
2745
2746 memset(&params, 0, sizeof(params));
2747 /* default to not changing parameters */
2748 params.use_cts_prot = -1;
2749 params.use_short_preamble = -1;
2750 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002751 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002752 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002753
2754 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2755 params.use_cts_prot =
2756 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2757 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2758 params.use_short_preamble =
2759 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2760 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2761 params.use_short_slot_time =
2762 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002763 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2764 params.basic_rates =
2765 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2766 params.basic_rates_len =
2767 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2768 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002769 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2770 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002771 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2772 params.ht_opmode =
2773 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002774
Johannes Berg4c476992010-10-04 21:36:35 +02002775 if (!rdev->ops->change_bss)
2776 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002777
Johannes Berg074ac8d2010-09-16 14:58:22 +02002778 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002779 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2780 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002781
Johannes Berg4c476992010-10-04 21:36:35 +02002782 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002783}
2784
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002785static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002786 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2787 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2788 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2789 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2790 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2791 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2792};
2793
2794static int parse_reg_rule(struct nlattr *tb[],
2795 struct ieee80211_reg_rule *reg_rule)
2796{
2797 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2798 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2799
2800 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2801 return -EINVAL;
2802 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2803 return -EINVAL;
2804 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2805 return -EINVAL;
2806 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2807 return -EINVAL;
2808 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2809 return -EINVAL;
2810
2811 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2812
2813 freq_range->start_freq_khz =
2814 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2815 freq_range->end_freq_khz =
2816 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2817 freq_range->max_bandwidth_khz =
2818 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2819
2820 power_rule->max_eirp =
2821 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2822
2823 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2824 power_rule->max_antenna_gain =
2825 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2826
2827 return 0;
2828}
2829
2830static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2831{
2832 int r;
2833 char *data = NULL;
2834
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002835 /*
2836 * You should only get this when cfg80211 hasn't yet initialized
2837 * completely when built-in to the kernel right between the time
2838 * window between nl80211_init() and regulatory_init(), if that is
2839 * even possible.
2840 */
2841 mutex_lock(&cfg80211_mutex);
2842 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002843 mutex_unlock(&cfg80211_mutex);
2844 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002845 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002846 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002847
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002848 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2849 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002850
2851 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2852
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002853 r = regulatory_hint_user(data);
2854
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002855 return r;
2856}
2857
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002858static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002859 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002860{
Johannes Berg4c476992010-10-04 21:36:35 +02002861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02002862 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002863 struct wireless_dev *wdev = dev->ieee80211_ptr;
2864 struct mesh_config cur_params;
2865 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002866 void *hdr;
2867 struct nlattr *pinfoattr;
2868 struct sk_buff *msg;
2869
Johannes Berg29cbe682010-12-03 09:20:44 +01002870 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2871 return -EOPNOTSUPP;
2872
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002873 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02002874 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002875
Johannes Berg29cbe682010-12-03 09:20:44 +01002876 wdev_lock(wdev);
2877 /* If not connected, get default parameters */
2878 if (!wdev->mesh_id_len)
2879 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
2880 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002881 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01002882 &cur_params);
2883 wdev_unlock(wdev);
2884
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002885 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002886 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002887
2888 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002889 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002890 if (!msg)
2891 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002892 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002893 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002894 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01002895 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002896 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002897 if (!pinfoattr)
2898 goto nla_put_failure;
2899 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2900 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2901 cur_params.dot11MeshRetryTimeout);
2902 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2903 cur_params.dot11MeshConfirmTimeout);
2904 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2905 cur_params.dot11MeshHoldingTimeout);
2906 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2907 cur_params.dot11MeshMaxPeerLinks);
2908 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2909 cur_params.dot11MeshMaxRetries);
2910 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2911 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01002912 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
2913 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002914 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2915 cur_params.auto_open_plinks);
2916 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2917 cur_params.dot11MeshHWMPmaxPREQretries);
2918 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2919 cur_params.path_refresh_time);
2920 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2921 cur_params.min_discovery_timeout);
2922 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2923 cur_params.dot11MeshHWMPactivePathTimeout);
2924 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2925 cur_params.dot11MeshHWMPpreqMinInterval);
2926 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2927 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002928 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2929 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002930 nla_nest_end(msg, pinfoattr);
2931 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002932 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002933
Johannes Berg3b858752009-03-12 09:55:09 +01002934 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002935 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01002936 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04002937 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002938 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002939}
2940
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002941static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002942 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2943 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2944 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2945 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2946 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2947 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01002948 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002949 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2950
2951 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2952 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2953 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2954 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2955 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2956 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2957};
2958
Javier Cardonac80d5452010-12-16 17:37:49 -08002959static const struct nla_policy
2960 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
2961 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
2962 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07002963 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07002964 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08002965 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07002966 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08002967};
2968
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002969static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002970 struct mesh_config *cfg,
2971 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002972{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002973 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002974 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002975
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002976#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2977do {\
2978 if (table[attr_num]) {\
2979 cfg->param = nla_fn(table[attr_num]); \
2980 mask |= (1 << (attr_num - 1)); \
2981 } \
2982} while (0);\
2983
2984
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002985 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002986 return -EINVAL;
2987 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002988 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002989 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002990 return -EINVAL;
2991
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002992 /* This makes sure that there aren't more than 32 mesh config
2993 * parameters (otherwise our bitfield scheme would not work.) */
2994 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2995
2996 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002997 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2998 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2999 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3000 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3001 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3002 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3003 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3004 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3005 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3006 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3007 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3008 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003009 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3010 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003011 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3012 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3013 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3014 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3015 nla_get_u8);
3016 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3017 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3018 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3019 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3020 nla_get_u16);
3021 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3022 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3023 nla_get_u32);
3024 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3025 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3026 nla_get_u16);
3027 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3028 dot11MeshHWMPnetDiameterTraversalTime,
3029 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3030 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003031 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3032 dot11MeshHWMPRootMode, mask,
3033 NL80211_MESHCONF_HWMP_ROOTMODE,
3034 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003035 if (mask_out)
3036 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003037
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003038 return 0;
3039
3040#undef FILL_IN_MESH_PARAM_IF_SET
3041}
3042
Javier Cardonac80d5452010-12-16 17:37:49 -08003043static int nl80211_parse_mesh_setup(struct genl_info *info,
3044 struct mesh_setup *setup)
3045{
3046 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3047
3048 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3049 return -EINVAL;
3050 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3051 info->attrs[NL80211_ATTR_MESH_SETUP],
3052 nl80211_mesh_setup_params_policy))
3053 return -EINVAL;
3054
3055 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3056 setup->path_sel_proto =
3057 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3058 IEEE80211_PATH_PROTOCOL_VENDOR :
3059 IEEE80211_PATH_PROTOCOL_HWMP;
3060
3061 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3062 setup->path_metric =
3063 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3064 IEEE80211_PATH_METRIC_VENDOR :
3065 IEEE80211_PATH_METRIC_AIRTIME;
3066
Javier Cardona581a8b02011-04-07 15:08:27 -07003067
3068 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003069 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003070 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003071 if (!is_valid_ie_attr(ieattr))
3072 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003073 setup->ie = nla_data(ieattr);
3074 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003075 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003076 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3077 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003078
3079 return 0;
3080}
3081
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003082static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003083 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003084{
3085 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3086 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003087 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003088 struct mesh_config cfg;
3089 u32 mask;
3090 int err;
3091
Johannes Berg29cbe682010-12-03 09:20:44 +01003092 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3093 return -EOPNOTSUPP;
3094
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003095 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003096 return -EOPNOTSUPP;
3097
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003098 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003099 if (err)
3100 return err;
3101
Johannes Berg29cbe682010-12-03 09:20:44 +01003102 wdev_lock(wdev);
3103 if (!wdev->mesh_id_len)
3104 err = -ENOLINK;
3105
3106 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003107 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003108 mask, &cfg);
3109
3110 wdev_unlock(wdev);
3111
3112 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003113}
3114
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003115static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3116{
3117 struct sk_buff *msg;
3118 void *hdr = NULL;
3119 struct nlattr *nl_reg_rules;
3120 unsigned int i;
3121 int err = -EINVAL;
3122
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003123 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003124
3125 if (!cfg80211_regdomain)
3126 goto out;
3127
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003128 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003129 if (!msg) {
3130 err = -ENOBUFS;
3131 goto out;
3132 }
3133
3134 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3135 NL80211_CMD_GET_REG);
3136 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003137 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003138
3139 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3140 cfg80211_regdomain->alpha2);
3141
3142 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3143 if (!nl_reg_rules)
3144 goto nla_put_failure;
3145
3146 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3147 struct nlattr *nl_reg_rule;
3148 const struct ieee80211_reg_rule *reg_rule;
3149 const struct ieee80211_freq_range *freq_range;
3150 const struct ieee80211_power_rule *power_rule;
3151
3152 reg_rule = &cfg80211_regdomain->reg_rules[i];
3153 freq_range = &reg_rule->freq_range;
3154 power_rule = &reg_rule->power_rule;
3155
3156 nl_reg_rule = nla_nest_start(msg, i);
3157 if (!nl_reg_rule)
3158 goto nla_put_failure;
3159
3160 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3161 reg_rule->flags);
3162 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3163 freq_range->start_freq_khz);
3164 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3165 freq_range->end_freq_khz);
3166 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3167 freq_range->max_bandwidth_khz);
3168 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3169 power_rule->max_antenna_gain);
3170 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3171 power_rule->max_eirp);
3172
3173 nla_nest_end(msg, nl_reg_rule);
3174 }
3175
3176 nla_nest_end(msg, nl_reg_rules);
3177
3178 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003179 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003180 goto out;
3181
3182nla_put_failure:
3183 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003184put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003185 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003186 err = -EMSGSIZE;
3187out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003188 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003189 return err;
3190}
3191
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003192static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3193{
3194 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3195 struct nlattr *nl_reg_rule;
3196 char *alpha2 = NULL;
3197 int rem_reg_rules = 0, r = 0;
3198 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3199 struct ieee80211_regdomain *rd = NULL;
3200
3201 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3202 return -EINVAL;
3203
3204 if (!info->attrs[NL80211_ATTR_REG_RULES])
3205 return -EINVAL;
3206
3207 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3208
3209 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3210 rem_reg_rules) {
3211 num_rules++;
3212 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003213 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003214 }
3215
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003216 mutex_lock(&cfg80211_mutex);
3217
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003218 if (!reg_is_valid_request(alpha2)) {
3219 r = -EINVAL;
3220 goto bad_reg;
3221 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003222
3223 size_of_regd = sizeof(struct ieee80211_regdomain) +
3224 (num_rules * sizeof(struct ieee80211_reg_rule));
3225
3226 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003227 if (!rd) {
3228 r = -ENOMEM;
3229 goto bad_reg;
3230 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003231
3232 rd->n_reg_rules = num_rules;
3233 rd->alpha2[0] = alpha2[0];
3234 rd->alpha2[1] = alpha2[1];
3235
3236 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3237 rem_reg_rules) {
3238 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3239 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3240 reg_rule_policy);
3241 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3242 if (r)
3243 goto bad_reg;
3244
3245 rule_idx++;
3246
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003247 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3248 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003249 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003250 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003251 }
3252
3253 BUG_ON(rule_idx != num_rules);
3254
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003255 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003256
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003257 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003258
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003259 return r;
3260
Johannes Bergd2372b32008-10-24 20:32:20 +02003261 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003262 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003263 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003264 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003265}
3266
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003267static int validate_scan_freqs(struct nlattr *freqs)
3268{
3269 struct nlattr *attr1, *attr2;
3270 int n_channels = 0, tmp1, tmp2;
3271
3272 nla_for_each_nested(attr1, freqs, tmp1) {
3273 n_channels++;
3274 /*
3275 * Some hardware has a limited channel list for
3276 * scanning, and it is pretty much nonsensical
3277 * to scan for a channel twice, so disallow that
3278 * and don't require drivers to check that the
3279 * channel list they get isn't longer than what
3280 * they can scan, as long as they can scan all
3281 * the channels they registered at once.
3282 */
3283 nla_for_each_nested(attr2, freqs, tmp2)
3284 if (attr1 != attr2 &&
3285 nla_get_u32(attr1) == nla_get_u32(attr2))
3286 return 0;
3287 }
3288
3289 return n_channels;
3290}
3291
Johannes Berg2a519312009-02-10 21:25:55 +01003292static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3293{
Johannes Berg4c476992010-10-04 21:36:35 +02003294 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3295 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003296 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003297 struct nlattr *attr;
3298 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003299 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01003300 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003301 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003302
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003303 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3304 return -EINVAL;
3305
Johannes Berg79c97e92009-07-07 03:56:12 +02003306 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003307
Johannes Berg4c476992010-10-04 21:36:35 +02003308 if (!rdev->ops->scan)
3309 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003310
Johannes Berg4c476992010-10-04 21:36:35 +02003311 if (rdev->scan_req)
3312 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003313
3314 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003315 n_channels = validate_scan_freqs(
3316 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003317 if (!n_channels)
3318 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003319 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003320 n_channels = 0;
3321
Johannes Berg2a519312009-02-10 21:25:55 +01003322 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3323 if (wiphy->bands[band])
3324 n_channels += wiphy->bands[band]->n_channels;
3325 }
3326
3327 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3328 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3329 n_ssids++;
3330
Johannes Berg4c476992010-10-04 21:36:35 +02003331 if (n_ssids > wiphy->max_scan_ssids)
3332 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003333
Jouni Malinen70692ad2009-02-16 19:39:13 +02003334 if (info->attrs[NL80211_ATTR_IE])
3335 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3336 else
3337 ie_len = 0;
3338
Johannes Berg4c476992010-10-04 21:36:35 +02003339 if (ie_len > wiphy->max_scan_ie_len)
3340 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003341
Johannes Berg2a519312009-02-10 21:25:55 +01003342 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003343 + sizeof(*request->ssids) * n_ssids
3344 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003345 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003346 if (!request)
3347 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003348
Johannes Berg2a519312009-02-10 21:25:55 +01003349 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003350 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003351 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003352 if (ie_len) {
3353 if (request->ssids)
3354 request->ie = (void *)(request->ssids + n_ssids);
3355 else
3356 request->ie = (void *)(request->channels + n_channels);
3357 }
Johannes Berg2a519312009-02-10 21:25:55 +01003358
Johannes Berg584991d2009-11-02 13:32:03 +01003359 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003360 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3361 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003362 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003363 struct ieee80211_channel *chan;
3364
3365 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3366
3367 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003368 err = -EINVAL;
3369 goto out_free;
3370 }
Johannes Berg584991d2009-11-02 13:32:03 +01003371
3372 /* ignore disabled channels */
3373 if (chan->flags & IEEE80211_CHAN_DISABLED)
3374 continue;
3375
3376 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003377 i++;
3378 }
3379 } else {
3380 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003381 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3382 int j;
3383 if (!wiphy->bands[band])
3384 continue;
3385 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003386 struct ieee80211_channel *chan;
3387
3388 chan = &wiphy->bands[band]->channels[j];
3389
3390 if (chan->flags & IEEE80211_CHAN_DISABLED)
3391 continue;
3392
3393 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003394 i++;
3395 }
3396 }
3397 }
3398
Johannes Berg584991d2009-11-02 13:32:03 +01003399 if (!i) {
3400 err = -EINVAL;
3401 goto out_free;
3402 }
3403
3404 request->n_channels = i;
3405
Johannes Berg2a519312009-02-10 21:25:55 +01003406 i = 0;
3407 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3408 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003409 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003410 err = -EINVAL;
3411 goto out_free;
3412 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003413 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003414 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003415 i++;
3416 }
3417 }
3418
Jouni Malinen70692ad2009-02-16 19:39:13 +02003419 if (info->attrs[NL80211_ATTR_IE]) {
3420 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003421 memcpy((void *)request->ie,
3422 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003423 request->ie_len);
3424 }
3425
Johannes Berg463d0182009-07-14 00:33:35 +02003426 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003427 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003428
Johannes Berg79c97e92009-07-07 03:56:12 +02003429 rdev->scan_req = request;
3430 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003431
Johannes Berg463d0182009-07-14 00:33:35 +02003432 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003433 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003434 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003435 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003436 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003437 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003438 kfree(request);
3439 }
Johannes Berg3b858752009-03-12 09:55:09 +01003440
Johannes Berg2a519312009-02-10 21:25:55 +01003441 return err;
3442}
3443
Luciano Coelho807f8a82011-05-11 17:09:35 +03003444static int nl80211_start_sched_scan(struct sk_buff *skb,
3445 struct genl_info *info)
3446{
3447 struct cfg80211_sched_scan_request *request;
3448 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3449 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003450 struct nlattr *attr;
3451 struct wiphy *wiphy;
3452 int err, tmp, n_ssids = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003453 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003454 enum ieee80211_band band;
3455 size_t ie_len;
3456
3457 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3458 !rdev->ops->sched_scan_start)
3459 return -EOPNOTSUPP;
3460
3461 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3462 return -EINVAL;
3463
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003464 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3465 return -EINVAL;
3466
3467 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3468 if (interval == 0)
3469 return -EINVAL;
3470
Luciano Coelho807f8a82011-05-11 17:09:35 +03003471 wiphy = &rdev->wiphy;
3472
3473 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3474 n_channels = validate_scan_freqs(
3475 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3476 if (!n_channels)
3477 return -EINVAL;
3478 } else {
3479 n_channels = 0;
3480
3481 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3482 if (wiphy->bands[band])
3483 n_channels += wiphy->bands[band]->n_channels;
3484 }
3485
3486 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3487 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3488 tmp)
3489 n_ssids++;
3490
3491 if (n_ssids > wiphy->max_scan_ssids)
3492 return -EINVAL;
3493
3494 if (info->attrs[NL80211_ATTR_IE])
3495 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3496 else
3497 ie_len = 0;
3498
3499 if (ie_len > wiphy->max_scan_ie_len)
3500 return -EINVAL;
3501
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003502 mutex_lock(&rdev->sched_scan_mtx);
3503
3504 if (rdev->sched_scan_req) {
3505 err = -EINPROGRESS;
3506 goto out;
3507 }
3508
Luciano Coelho807f8a82011-05-11 17:09:35 +03003509 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003510 + sizeof(*request->ssids) * n_ssids
3511 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003512 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003513 if (!request) {
3514 err = -ENOMEM;
3515 goto out;
3516 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003517
3518 if (n_ssids)
3519 request->ssids = (void *)&request->channels[n_channels];
3520 request->n_ssids = n_ssids;
3521 if (ie_len) {
3522 if (request->ssids)
3523 request->ie = (void *)(request->ssids + n_ssids);
3524 else
3525 request->ie = (void *)(request->channels + n_channels);
3526 }
3527
3528 i = 0;
3529 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3530 /* user specified, bail out if channel not found */
3531 nla_for_each_nested(attr,
3532 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3533 tmp) {
3534 struct ieee80211_channel *chan;
3535
3536 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3537
3538 if (!chan) {
3539 err = -EINVAL;
3540 goto out_free;
3541 }
3542
3543 /* ignore disabled channels */
3544 if (chan->flags & IEEE80211_CHAN_DISABLED)
3545 continue;
3546
3547 request->channels[i] = chan;
3548 i++;
3549 }
3550 } else {
3551 /* all channels */
3552 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3553 int j;
3554 if (!wiphy->bands[band])
3555 continue;
3556 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3557 struct ieee80211_channel *chan;
3558
3559 chan = &wiphy->bands[band]->channels[j];
3560
3561 if (chan->flags & IEEE80211_CHAN_DISABLED)
3562 continue;
3563
3564 request->channels[i] = chan;
3565 i++;
3566 }
3567 }
3568 }
3569
3570 if (!i) {
3571 err = -EINVAL;
3572 goto out_free;
3573 }
3574
3575 request->n_channels = i;
3576
3577 i = 0;
3578 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3579 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3580 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003581 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003582 err = -EINVAL;
3583 goto out_free;
3584 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003585 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003586 memcpy(request->ssids[i].ssid, nla_data(attr),
3587 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003588 i++;
3589 }
3590 }
3591
3592 if (info->attrs[NL80211_ATTR_IE]) {
3593 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3594 memcpy((void *)request->ie,
3595 nla_data(info->attrs[NL80211_ATTR_IE]),
3596 request->ie_len);
3597 }
3598
3599 request->dev = dev;
3600 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003601 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003602
3603 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3604 if (!err) {
3605 rdev->sched_scan_req = request;
3606 nl80211_send_sched_scan(rdev, dev,
3607 NL80211_CMD_START_SCHED_SCAN);
3608 goto out;
3609 }
3610
3611out_free:
3612 kfree(request);
3613out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003614 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003615 return err;
3616}
3617
3618static int nl80211_stop_sched_scan(struct sk_buff *skb,
3619 struct genl_info *info)
3620{
3621 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003622 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003623
3624 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3625 !rdev->ops->sched_scan_stop)
3626 return -EOPNOTSUPP;
3627
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003628 mutex_lock(&rdev->sched_scan_mtx);
3629 err = __cfg80211_stop_sched_scan(rdev, false);
3630 mutex_unlock(&rdev->sched_scan_mtx);
3631
3632 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003633}
3634
Johannes Berg2a519312009-02-10 21:25:55 +01003635static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3636 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003637 struct wireless_dev *wdev,
3638 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003639{
Johannes Berg48ab9052009-07-10 18:42:31 +02003640 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003641 void *hdr;
3642 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003643 int i;
3644
3645 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003646
3647 hdr = nl80211hdr_put(msg, pid, seq, flags,
3648 NL80211_CMD_NEW_SCAN_RESULTS);
3649 if (!hdr)
3650 return -1;
3651
Johannes Bergf5ea9122009-08-07 16:17:38 +02003652 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003653 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003654
3655 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3656 if (!bss)
3657 goto nla_put_failure;
3658 if (!is_zero_ether_addr(res->bssid))
3659 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3660 if (res->information_elements && res->len_information_elements)
3661 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3662 res->len_information_elements,
3663 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003664 if (res->beacon_ies && res->len_beacon_ies &&
3665 res->beacon_ies != res->information_elements)
3666 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3667 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003668 if (res->tsf)
3669 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3670 if (res->beacon_interval)
3671 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3672 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3673 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003674 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3675 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003676
Johannes Berg77965c92009-02-18 18:45:06 +01003677 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003678 case CFG80211_SIGNAL_TYPE_MBM:
3679 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3680 break;
3681 case CFG80211_SIGNAL_TYPE_UNSPEC:
3682 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3683 break;
3684 default:
3685 break;
3686 }
3687
Johannes Berg48ab9052009-07-10 18:42:31 +02003688 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003689 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003690 case NL80211_IFTYPE_STATION:
3691 if (intbss == wdev->current_bss)
3692 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3693 NL80211_BSS_STATUS_ASSOCIATED);
3694 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3695 if (intbss != wdev->auth_bsses[i])
3696 continue;
3697 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3698 NL80211_BSS_STATUS_AUTHENTICATED);
3699 break;
3700 }
3701 break;
3702 case NL80211_IFTYPE_ADHOC:
3703 if (intbss == wdev->current_bss)
3704 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3705 NL80211_BSS_STATUS_IBSS_JOINED);
3706 break;
3707 default:
3708 break;
3709 }
3710
Johannes Berg2a519312009-02-10 21:25:55 +01003711 nla_nest_end(msg, bss);
3712
3713 return genlmsg_end(msg, hdr);
3714
3715 nla_put_failure:
3716 genlmsg_cancel(msg, hdr);
3717 return -EMSGSIZE;
3718}
3719
3720static int nl80211_dump_scan(struct sk_buff *skb,
3721 struct netlink_callback *cb)
3722{
Johannes Berg48ab9052009-07-10 18:42:31 +02003723 struct cfg80211_registered_device *rdev;
3724 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003725 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003726 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003727 int start = cb->args[1], idx = 0;
3728 int err;
3729
Johannes Berg67748892010-10-04 21:14:06 +02003730 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3731 if (err)
3732 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003733
Johannes Berg48ab9052009-07-10 18:42:31 +02003734 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003735
Johannes Berg48ab9052009-07-10 18:42:31 +02003736 wdev_lock(wdev);
3737 spin_lock_bh(&rdev->bss_lock);
3738 cfg80211_bss_expire(rdev);
3739
3740 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003741 if (++idx <= start)
3742 continue;
3743 if (nl80211_send_bss(skb,
3744 NETLINK_CB(cb->skb).pid,
3745 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003746 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003747 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003748 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003749 }
3750 }
3751
Johannes Berg48ab9052009-07-10 18:42:31 +02003752 spin_unlock_bh(&rdev->bss_lock);
3753 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003754
3755 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003756 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003757
Johannes Berg67748892010-10-04 21:14:06 +02003758 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003759}
3760
Holger Schurig61fa7132009-11-11 12:25:40 +01003761static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3762 int flags, struct net_device *dev,
3763 struct survey_info *survey)
3764{
3765 void *hdr;
3766 struct nlattr *infoattr;
3767
3768 /* Survey without a channel doesn't make sense */
3769 if (!survey->channel)
3770 return -EINVAL;
3771
3772 hdr = nl80211hdr_put(msg, pid, seq, flags,
3773 NL80211_CMD_NEW_SURVEY_RESULTS);
3774 if (!hdr)
3775 return -ENOMEM;
3776
3777 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3778
3779 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3780 if (!infoattr)
3781 goto nla_put_failure;
3782
3783 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3784 survey->channel->center_freq);
3785 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3786 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3787 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003788 if (survey->filled & SURVEY_INFO_IN_USE)
3789 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c29a2010-10-09 02:39:29 +02003790 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3791 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3792 survey->channel_time);
3793 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3794 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3795 survey->channel_time_busy);
3796 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3797 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3798 survey->channel_time_ext_busy);
3799 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3800 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3801 survey->channel_time_rx);
3802 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3803 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3804 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003805
3806 nla_nest_end(msg, infoattr);
3807
3808 return genlmsg_end(msg, hdr);
3809
3810 nla_put_failure:
3811 genlmsg_cancel(msg, hdr);
3812 return -EMSGSIZE;
3813}
3814
3815static int nl80211_dump_survey(struct sk_buff *skb,
3816 struct netlink_callback *cb)
3817{
3818 struct survey_info survey;
3819 struct cfg80211_registered_device *dev;
3820 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003821 int survey_idx = cb->args[1];
3822 int res;
3823
Johannes Berg67748892010-10-04 21:14:06 +02003824 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3825 if (res)
3826 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003827
3828 if (!dev->ops->dump_survey) {
3829 res = -EOPNOTSUPP;
3830 goto out_err;
3831 }
3832
3833 while (1) {
3834 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3835 &survey);
3836 if (res == -ENOENT)
3837 break;
3838 if (res)
3839 goto out_err;
3840
3841 if (nl80211_send_survey(skb,
3842 NETLINK_CB(cb->skb).pid,
3843 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3844 netdev,
3845 &survey) < 0)
3846 goto out;
3847 survey_idx++;
3848 }
3849
3850 out:
3851 cb->args[1] = survey_idx;
3852 res = skb->len;
3853 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003854 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003855 return res;
3856}
3857
Jouni Malinen255e7372009-03-20 21:21:17 +02003858static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3859{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003860 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003861}
3862
Samuel Ortizb23aa672009-07-01 21:26:54 +02003863static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3864{
3865 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3866 NL80211_WPA_VERSION_2));
3867}
3868
3869static bool nl80211_valid_akm_suite(u32 akm)
3870{
3871 return akm == WLAN_AKM_SUITE_8021X ||
3872 akm == WLAN_AKM_SUITE_PSK;
3873}
3874
3875static bool nl80211_valid_cipher_suite(u32 cipher)
3876{
3877 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3878 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3879 cipher == WLAN_CIPHER_SUITE_TKIP ||
3880 cipher == WLAN_CIPHER_SUITE_CCMP ||
3881 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3882}
3883
3884
Jouni Malinen636a5d32009-03-19 13:39:22 +02003885static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3886{
Johannes Berg4c476992010-10-04 21:36:35 +02003887 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3888 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003889 struct ieee80211_channel *chan;
3890 const u8 *bssid, *ssid, *ie = NULL;
3891 int err, ssid_len, ie_len = 0;
3892 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003893 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003894 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003895
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003896 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3897 return -EINVAL;
3898
3899 if (!info->attrs[NL80211_ATTR_MAC])
3900 return -EINVAL;
3901
Jouni Malinen17780922009-03-27 20:52:47 +02003902 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3903 return -EINVAL;
3904
Johannes Berg19957bb2009-07-02 17:20:43 +02003905 if (!info->attrs[NL80211_ATTR_SSID])
3906 return -EINVAL;
3907
3908 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3909 return -EINVAL;
3910
Johannes Bergfffd0932009-07-08 14:22:54 +02003911 err = nl80211_parse_key(info, &key);
3912 if (err)
3913 return err;
3914
3915 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003916 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3917 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003918 if (!key.p.key || !key.p.key_len)
3919 return -EINVAL;
3920 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3921 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3922 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3923 key.p.key_len != WLAN_KEY_LEN_WEP104))
3924 return -EINVAL;
3925 if (key.idx > 4)
3926 return -EINVAL;
3927 } else {
3928 key.p.key_len = 0;
3929 key.p.key = NULL;
3930 }
3931
Johannes Bergafea0b72010-08-10 09:46:42 +02003932 if (key.idx >= 0) {
3933 int i;
3934 bool ok = false;
3935 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3936 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3937 ok = true;
3938 break;
3939 }
3940 }
Johannes Berg4c476992010-10-04 21:36:35 +02003941 if (!ok)
3942 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003943 }
3944
Johannes Berg4c476992010-10-04 21:36:35 +02003945 if (!rdev->ops->auth)
3946 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003947
Johannes Berg074ac8d2010-09-16 14:58:22 +02003948 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003949 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3950 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003951
Johannes Berg19957bb2009-07-02 17:20:43 +02003952 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003953 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003954 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003955 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3956 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003957
Johannes Berg19957bb2009-07-02 17:20:43 +02003958 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3959 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3960
3961 if (info->attrs[NL80211_ATTR_IE]) {
3962 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3963 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3964 }
3965
3966 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003967 if (!nl80211_valid_auth_type(auth_type))
3968 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003969
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003970 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3971
Johannes Berg4c476992010-10-04 21:36:35 +02003972 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3973 ssid, ssid_len, ie, ie_len,
3974 key.p.key, key.p.key_len, key.idx,
3975 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003976}
3977
Johannes Bergc0692b82010-08-27 14:26:53 +03003978static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3979 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003980 struct cfg80211_crypto_settings *settings,
3981 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003982{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003983 memset(settings, 0, sizeof(*settings));
3984
Samuel Ortizb23aa672009-07-01 21:26:54 +02003985 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3986
Johannes Bergc0692b82010-08-27 14:26:53 +03003987 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3988 u16 proto;
3989 proto = nla_get_u16(
3990 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3991 settings->control_port_ethertype = cpu_to_be16(proto);
3992 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3993 proto != ETH_P_PAE)
3994 return -EINVAL;
3995 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3996 settings->control_port_no_encrypt = true;
3997 } else
3998 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3999
Samuel Ortizb23aa672009-07-01 21:26:54 +02004000 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4001 void *data;
4002 int len, i;
4003
4004 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4005 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4006 settings->n_ciphers_pairwise = len / sizeof(u32);
4007
4008 if (len % sizeof(u32))
4009 return -EINVAL;
4010
Johannes Berg3dc27d22009-07-02 21:36:37 +02004011 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004012 return -EINVAL;
4013
4014 memcpy(settings->ciphers_pairwise, data, len);
4015
4016 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4017 if (!nl80211_valid_cipher_suite(
4018 settings->ciphers_pairwise[i]))
4019 return -EINVAL;
4020 }
4021
4022 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4023 settings->cipher_group =
4024 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
4025 if (!nl80211_valid_cipher_suite(settings->cipher_group))
4026 return -EINVAL;
4027 }
4028
4029 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4030 settings->wpa_versions =
4031 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4032 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4033 return -EINVAL;
4034 }
4035
4036 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4037 void *data;
4038 int len, i;
4039
4040 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4041 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4042 settings->n_akm_suites = len / sizeof(u32);
4043
4044 if (len % sizeof(u32))
4045 return -EINVAL;
4046
Jouni Malinen508ed742011-09-21 16:13:07 +03004047 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4048 return -EINVAL;
4049
Samuel Ortizb23aa672009-07-01 21:26:54 +02004050 memcpy(settings->akm_suites, data, len);
4051
Jouni Malinen508ed742011-09-21 16:13:07 +03004052 for (i = 0; i < settings->n_akm_suites; i++)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004053 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
4054 return -EINVAL;
4055 }
4056
4057 return 0;
4058}
4059
Jouni Malinen636a5d32009-03-19 13:39:22 +02004060static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4061{
Johannes Berg4c476992010-10-04 21:36:35 +02004062 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4063 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004064 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004065 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004066 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004067 int err, ssid_len, ie_len = 0;
4068 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004069
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004070 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4071 return -EINVAL;
4072
4073 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004074 !info->attrs[NL80211_ATTR_SSID] ||
4075 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004076 return -EINVAL;
4077
Johannes Berg4c476992010-10-04 21:36:35 +02004078 if (!rdev->ops->assoc)
4079 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004080
Johannes Berg074ac8d2010-09-16 14:58:22 +02004081 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004082 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4083 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004084
Johannes Berg19957bb2009-07-02 17:20:43 +02004085 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004086
Johannes Berg19957bb2009-07-02 17:20:43 +02004087 chan = ieee80211_get_channel(&rdev->wiphy,
4088 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004089 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4090 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004091
Johannes Berg19957bb2009-07-02 17:20:43 +02004092 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4093 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
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 Malinendc6382c2009-05-06 22:09:37 +03004100 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004101 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03004102 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004103 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004104 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004105 else if (mfp != NL80211_MFP_NO)
4106 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03004107 }
4108
Johannes Berg3e5d7642009-07-07 14:37:26 +02004109 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4110 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4111
Johannes Bergc0692b82010-08-27 14:26:53 +03004112 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004113 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004114 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4115 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004116 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004117
Jouni Malinen636a5d32009-03-19 13:39:22 +02004118 return err;
4119}
4120
4121static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4122{
Johannes Berg4c476992010-10-04 21:36:35 +02004123 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4124 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004125 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004126 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004127 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004128 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004129
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004130 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4131 return -EINVAL;
4132
4133 if (!info->attrs[NL80211_ATTR_MAC])
4134 return -EINVAL;
4135
4136 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4137 return -EINVAL;
4138
Johannes Berg4c476992010-10-04 21:36:35 +02004139 if (!rdev->ops->deauth)
4140 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004141
Johannes Berg074ac8d2010-09-16 14:58:22 +02004142 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004143 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4144 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004145
Johannes Berg19957bb2009-07-02 17:20:43 +02004146 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004147
Johannes Berg19957bb2009-07-02 17:20:43 +02004148 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4149 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004150 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004151 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004152 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004153
4154 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004155 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4156 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004157 }
4158
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004159 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4160
Johannes Berg4c476992010-10-04 21:36:35 +02004161 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4162 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004163}
4164
4165static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4166{
Johannes Berg4c476992010-10-04 21:36:35 +02004167 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4168 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004169 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004170 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004171 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004172 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004173
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004174 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4175 return -EINVAL;
4176
4177 if (!info->attrs[NL80211_ATTR_MAC])
4178 return -EINVAL;
4179
4180 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4181 return -EINVAL;
4182
Johannes Berg4c476992010-10-04 21:36:35 +02004183 if (!rdev->ops->disassoc)
4184 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004185
Johannes Berg074ac8d2010-09-16 14:58:22 +02004186 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004187 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4188 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004189
Johannes Berg19957bb2009-07-02 17:20:43 +02004190 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004191
Johannes Berg19957bb2009-07-02 17:20:43 +02004192 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4193 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004194 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004195 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004196 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004197
4198 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004199 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4200 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004201 }
4202
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004203 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4204
Johannes Berg4c476992010-10-04 21:36:35 +02004205 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4206 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004207}
4208
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004209static bool
4210nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4211 int mcast_rate[IEEE80211_NUM_BANDS],
4212 int rateval)
4213{
4214 struct wiphy *wiphy = &rdev->wiphy;
4215 bool found = false;
4216 int band, i;
4217
4218 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4219 struct ieee80211_supported_band *sband;
4220
4221 sband = wiphy->bands[band];
4222 if (!sband)
4223 continue;
4224
4225 for (i = 0; i < sband->n_bitrates; i++) {
4226 if (sband->bitrates[i].bitrate == rateval) {
4227 mcast_rate[band] = i + 1;
4228 found = true;
4229 break;
4230 }
4231 }
4232 }
4233
4234 return found;
4235}
4236
Johannes Berg04a773a2009-04-19 21:24:32 +02004237static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4238{
Johannes Berg4c476992010-10-04 21:36:35 +02004239 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4240 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004241 struct cfg80211_ibss_params ibss;
4242 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004243 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004244 int err;
4245
Johannes Berg8e30bc52009-04-22 17:45:38 +02004246 memset(&ibss, 0, sizeof(ibss));
4247
Johannes Berg04a773a2009-04-19 21:24:32 +02004248 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4249 return -EINVAL;
4250
4251 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4252 !info->attrs[NL80211_ATTR_SSID] ||
4253 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4254 return -EINVAL;
4255
Johannes Berg8e30bc52009-04-22 17:45:38 +02004256 ibss.beacon_interval = 100;
4257
4258 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4259 ibss.beacon_interval =
4260 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4261 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4262 return -EINVAL;
4263 }
4264
Johannes Berg4c476992010-10-04 21:36:35 +02004265 if (!rdev->ops->join_ibss)
4266 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004267
Johannes Berg4c476992010-10-04 21:36:35 +02004268 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4269 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004270
Johannes Berg79c97e92009-07-07 03:56:12 +02004271 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004272
4273 if (info->attrs[NL80211_ATTR_MAC])
4274 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4275 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4276 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4277
4278 if (info->attrs[NL80211_ATTR_IE]) {
4279 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4280 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4281 }
4282
4283 ibss.channel = ieee80211_get_channel(wiphy,
4284 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4285 if (!ibss.channel ||
4286 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004287 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4288 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004289
4290 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004291 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004292
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004293 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4294 u8 *rates =
4295 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4296 int n_rates =
4297 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4298 struct ieee80211_supported_band *sband =
4299 wiphy->bands[ibss.channel->band];
4300 int i, j;
4301
Johannes Berg4c476992010-10-04 21:36:35 +02004302 if (n_rates == 0)
4303 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004304
4305 for (i = 0; i < n_rates; i++) {
4306 int rate = (rates[i] & 0x7f) * 5;
4307 bool found = false;
4308
4309 for (j = 0; j < sband->n_bitrates; j++) {
4310 if (sband->bitrates[j].bitrate == rate) {
4311 found = true;
4312 ibss.basic_rates |= BIT(j);
4313 break;
4314 }
4315 }
Johannes Berg4c476992010-10-04 21:36:35 +02004316 if (!found)
4317 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004318 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004319 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004320
4321 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4322 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4323 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4324 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004325
Johannes Berg4c476992010-10-04 21:36:35 +02004326 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4327 connkeys = nl80211_parse_connkeys(rdev,
4328 info->attrs[NL80211_ATTR_KEYS]);
4329 if (IS_ERR(connkeys))
4330 return PTR_ERR(connkeys);
4331 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004332
Johannes Berg4c476992010-10-04 21:36:35 +02004333 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004334 if (err)
4335 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004336 return err;
4337}
4338
4339static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4340{
Johannes Berg4c476992010-10-04 21:36:35 +02004341 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4342 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004343
Johannes Berg4c476992010-10-04 21:36:35 +02004344 if (!rdev->ops->leave_ibss)
4345 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004346
Johannes Berg4c476992010-10-04 21:36:35 +02004347 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4348 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004349
Johannes Berg4c476992010-10-04 21:36:35 +02004350 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004351}
4352
Johannes Bergaff89a92009-07-01 21:26:51 +02004353#ifdef CONFIG_NL80211_TESTMODE
4354static struct genl_multicast_group nl80211_testmode_mcgrp = {
4355 .name = "testmode",
4356};
4357
4358static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4359{
Johannes Berg4c476992010-10-04 21:36:35 +02004360 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004361 int err;
4362
4363 if (!info->attrs[NL80211_ATTR_TESTDATA])
4364 return -EINVAL;
4365
Johannes Bergaff89a92009-07-01 21:26:51 +02004366 err = -EOPNOTSUPP;
4367 if (rdev->ops->testmode_cmd) {
4368 rdev->testmode_info = info;
4369 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4370 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4371 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4372 rdev->testmode_info = NULL;
4373 }
4374
Johannes Bergaff89a92009-07-01 21:26:51 +02004375 return err;
4376}
4377
4378static struct sk_buff *
4379__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4380 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4381{
4382 struct sk_buff *skb;
4383 void *hdr;
4384 struct nlattr *data;
4385
4386 skb = nlmsg_new(approxlen + 100, gfp);
4387 if (!skb)
4388 return NULL;
4389
4390 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4391 if (!hdr) {
4392 kfree_skb(skb);
4393 return NULL;
4394 }
4395
4396 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4397 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4398
4399 ((void **)skb->cb)[0] = rdev;
4400 ((void **)skb->cb)[1] = hdr;
4401 ((void **)skb->cb)[2] = data;
4402
4403 return skb;
4404
4405 nla_put_failure:
4406 kfree_skb(skb);
4407 return NULL;
4408}
4409
4410struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4411 int approxlen)
4412{
4413 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4414
4415 if (WARN_ON(!rdev->testmode_info))
4416 return NULL;
4417
4418 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4419 rdev->testmode_info->snd_pid,
4420 rdev->testmode_info->snd_seq,
4421 GFP_KERNEL);
4422}
4423EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4424
4425int cfg80211_testmode_reply(struct sk_buff *skb)
4426{
4427 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4428 void *hdr = ((void **)skb->cb)[1];
4429 struct nlattr *data = ((void **)skb->cb)[2];
4430
4431 if (WARN_ON(!rdev->testmode_info)) {
4432 kfree_skb(skb);
4433 return -EINVAL;
4434 }
4435
4436 nla_nest_end(skb, data);
4437 genlmsg_end(skb, hdr);
4438 return genlmsg_reply(skb, rdev->testmode_info);
4439}
4440EXPORT_SYMBOL(cfg80211_testmode_reply);
4441
4442struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4443 int approxlen, gfp_t gfp)
4444{
4445 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4446
4447 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4448}
4449EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4450
4451void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4452{
4453 void *hdr = ((void **)skb->cb)[1];
4454 struct nlattr *data = ((void **)skb->cb)[2];
4455
4456 nla_nest_end(skb, data);
4457 genlmsg_end(skb, hdr);
4458 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4459}
4460EXPORT_SYMBOL(cfg80211_testmode_event);
4461#endif
4462
Samuel Ortizb23aa672009-07-01 21:26:54 +02004463static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4464{
Johannes Berg4c476992010-10-04 21:36:35 +02004465 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4466 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004467 struct cfg80211_connect_params connect;
4468 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004469 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004470 int err;
4471
4472 memset(&connect, 0, sizeof(connect));
4473
4474 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4475 return -EINVAL;
4476
4477 if (!info->attrs[NL80211_ATTR_SSID] ||
4478 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4479 return -EINVAL;
4480
4481 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4482 connect.auth_type =
4483 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4484 if (!nl80211_valid_auth_type(connect.auth_type))
4485 return -EINVAL;
4486 } else
4487 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4488
4489 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4490
Johannes Bergc0692b82010-08-27 14:26:53 +03004491 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004492 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004493 if (err)
4494 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004495
Johannes Berg074ac8d2010-09-16 14:58:22 +02004496 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004497 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4498 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004499
Johannes Berg79c97e92009-07-07 03:56:12 +02004500 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004501
Samuel Ortizb23aa672009-07-01 21:26:54 +02004502 if (info->attrs[NL80211_ATTR_MAC])
4503 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4504 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4505 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4506
4507 if (info->attrs[NL80211_ATTR_IE]) {
4508 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4509 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4510 }
4511
4512 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4513 connect.channel =
4514 ieee80211_get_channel(wiphy,
4515 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4516 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004517 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4518 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004519 }
4520
Johannes Bergfffd0932009-07-08 14:22:54 +02004521 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4522 connkeys = nl80211_parse_connkeys(rdev,
4523 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004524 if (IS_ERR(connkeys))
4525 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004526 }
4527
4528 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004529 if (err)
4530 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004531 return err;
4532}
4533
4534static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4535{
Johannes Berg4c476992010-10-04 21:36:35 +02004536 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4537 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004538 u16 reason;
4539
4540 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4541 reason = WLAN_REASON_DEAUTH_LEAVING;
4542 else
4543 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4544
4545 if (reason == 0)
4546 return -EINVAL;
4547
Johannes Berg074ac8d2010-09-16 14:58:22 +02004548 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004549 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4550 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004551
Johannes Berg4c476992010-10-04 21:36:35 +02004552 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004553}
4554
Johannes Berg463d0182009-07-14 00:33:35 +02004555static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4556{
Johannes Berg4c476992010-10-04 21:36:35 +02004557 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004558 struct net *net;
4559 int err;
4560 u32 pid;
4561
4562 if (!info->attrs[NL80211_ATTR_PID])
4563 return -EINVAL;
4564
4565 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4566
Johannes Berg463d0182009-07-14 00:33:35 +02004567 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004568 if (IS_ERR(net))
4569 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004570
4571 err = 0;
4572
4573 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004574 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4575 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004576
Johannes Berg463d0182009-07-14 00:33:35 +02004577 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004578 return err;
4579}
4580
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004581static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4582{
Johannes Berg4c476992010-10-04 21:36:35 +02004583 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004584 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4585 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004586 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004587 struct cfg80211_pmksa pmksa;
4588
4589 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4590
4591 if (!info->attrs[NL80211_ATTR_MAC])
4592 return -EINVAL;
4593
4594 if (!info->attrs[NL80211_ATTR_PMKID])
4595 return -EINVAL;
4596
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004597 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4598 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4599
Johannes Berg074ac8d2010-09-16 14:58:22 +02004600 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004601 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4602 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004603
4604 switch (info->genlhdr->cmd) {
4605 case NL80211_CMD_SET_PMKSA:
4606 rdev_ops = rdev->ops->set_pmksa;
4607 break;
4608 case NL80211_CMD_DEL_PMKSA:
4609 rdev_ops = rdev->ops->del_pmksa;
4610 break;
4611 default:
4612 WARN_ON(1);
4613 break;
4614 }
4615
Johannes Berg4c476992010-10-04 21:36:35 +02004616 if (!rdev_ops)
4617 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004618
Johannes Berg4c476992010-10-04 21:36:35 +02004619 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004620}
4621
4622static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4623{
Johannes Berg4c476992010-10-04 21:36:35 +02004624 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4625 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004626
Johannes Berg074ac8d2010-09-16 14:58:22 +02004627 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004628 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4629 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004630
Johannes Berg4c476992010-10-04 21:36:35 +02004631 if (!rdev->ops->flush_pmksa)
4632 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004633
Johannes Berg4c476992010-10-04 21:36:35 +02004634 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004635}
4636
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004637static int nl80211_remain_on_channel(struct sk_buff *skb,
4638 struct genl_info *info)
4639{
Johannes Berg4c476992010-10-04 21:36:35 +02004640 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4641 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004642 struct ieee80211_channel *chan;
4643 struct sk_buff *msg;
4644 void *hdr;
4645 u64 cookie;
4646 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4647 u32 freq, duration;
4648 int err;
4649
4650 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4651 !info->attrs[NL80211_ATTR_DURATION])
4652 return -EINVAL;
4653
4654 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4655
4656 /*
4657 * We should be on that channel for at least one jiffie,
4658 * and more than 5 seconds seems excessive.
4659 */
Johannes Berga2939112010-12-14 17:54:28 +01004660 if (!duration || !msecs_to_jiffies(duration) ||
4661 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004662 return -EINVAL;
4663
Johannes Berg4c476992010-10-04 21:36:35 +02004664 if (!rdev->ops->remain_on_channel)
4665 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004666
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004667 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4668 channel_type = nla_get_u32(
4669 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4670 if (channel_type != NL80211_CHAN_NO_HT &&
4671 channel_type != NL80211_CHAN_HT20 &&
4672 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004673 channel_type != NL80211_CHAN_HT40MINUS)
4674 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004675 }
4676
4677 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4678 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004679 if (chan == NULL)
4680 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004681
4682 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004683 if (!msg)
4684 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004685
4686 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4687 NL80211_CMD_REMAIN_ON_CHANNEL);
4688
4689 if (IS_ERR(hdr)) {
4690 err = PTR_ERR(hdr);
4691 goto free_msg;
4692 }
4693
4694 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4695 channel_type, duration, &cookie);
4696
4697 if (err)
4698 goto free_msg;
4699
4700 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4701
4702 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004703
4704 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004705
4706 nla_put_failure:
4707 err = -ENOBUFS;
4708 free_msg:
4709 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004710 return err;
4711}
4712
4713static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4714 struct genl_info *info)
4715{
Johannes Berg4c476992010-10-04 21:36:35 +02004716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4717 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004718 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004719
4720 if (!info->attrs[NL80211_ATTR_COOKIE])
4721 return -EINVAL;
4722
Johannes Berg4c476992010-10-04 21:36:35 +02004723 if (!rdev->ops->cancel_remain_on_channel)
4724 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004725
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004726 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4727
Johannes Berg4c476992010-10-04 21:36:35 +02004728 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004729}
4730
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004731static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4732 u8 *rates, u8 rates_len)
4733{
4734 u8 i;
4735 u32 mask = 0;
4736
4737 for (i = 0; i < rates_len; i++) {
4738 int rate = (rates[i] & 0x7f) * 5;
4739 int ridx;
4740 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4741 struct ieee80211_rate *srate =
4742 &sband->bitrates[ridx];
4743 if (rate == srate->bitrate) {
4744 mask |= 1 << ridx;
4745 break;
4746 }
4747 }
4748 if (ridx == sband->n_bitrates)
4749 return 0; /* rate not found */
4750 }
4751
4752 return mask;
4753}
4754
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004755static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004756 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4757 .len = NL80211_MAX_SUPP_RATES },
4758};
4759
4760static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4761 struct genl_info *info)
4762{
4763 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004764 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004765 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004766 int rem, i;
4767 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004768 struct nlattr *tx_rates;
4769 struct ieee80211_supported_band *sband;
4770
4771 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4772 return -EINVAL;
4773
Johannes Berg4c476992010-10-04 21:36:35 +02004774 if (!rdev->ops->set_bitrate_mask)
4775 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004776
4777 memset(&mask, 0, sizeof(mask));
4778 /* Default to all rates enabled */
4779 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4780 sband = rdev->wiphy.bands[i];
4781 mask.control[i].legacy =
4782 sband ? (1 << sband->n_bitrates) - 1 : 0;
4783 }
4784
4785 /*
4786 * The nested attribute uses enum nl80211_band as the index. This maps
4787 * directly to the enum ieee80211_band values used in cfg80211.
4788 */
4789 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4790 {
4791 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004792 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4793 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004794 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004795 if (sband == NULL)
4796 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004797 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4798 nla_len(tx_rates), nl80211_txattr_policy);
4799 if (tb[NL80211_TXRATE_LEGACY]) {
4800 mask.control[band].legacy = rateset_to_mask(
4801 sband,
4802 nla_data(tb[NL80211_TXRATE_LEGACY]),
4803 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004804 if (mask.control[band].legacy == 0)
4805 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004806 }
4807 }
4808
Johannes Berg4c476992010-10-04 21:36:35 +02004809 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004810}
4811
Johannes Berg2e161f72010-08-12 15:38:38 +02004812static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004813{
Johannes Berg4c476992010-10-04 21:36:35 +02004814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4815 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004816 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004817
4818 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4819 return -EINVAL;
4820
Johannes Berg2e161f72010-08-12 15:38:38 +02004821 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4822 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004823
Johannes Berg9d38d852010-06-09 17:20:33 +02004824 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004825 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004826 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4827 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4828 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08004829 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004830 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4831 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004832
4833 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004834 if (!rdev->ops->mgmt_tx)
4835 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004836
Johannes Berg4c476992010-10-04 21:36:35 +02004837 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004838 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004839 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4840 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004841}
4842
Johannes Berg2e161f72010-08-12 15:38:38 +02004843static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004844{
Johannes Berg4c476992010-10-04 21:36:35 +02004845 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4846 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004847 struct ieee80211_channel *chan;
4848 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004849 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004850 u32 freq;
4851 int err;
4852 void *hdr;
4853 u64 cookie;
4854 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004855 unsigned int wait = 0;
4856 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02004857
4858 if (!info->attrs[NL80211_ATTR_FRAME] ||
4859 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4860 return -EINVAL;
4861
Johannes Berg4c476992010-10-04 21:36:35 +02004862 if (!rdev->ops->mgmt_tx)
4863 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004864
Johannes Berg9d38d852010-06-09 17:20:33 +02004865 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004866 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004867 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4868 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4869 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08004870 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004871 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4872 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004873
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004874 if (info->attrs[NL80211_ATTR_DURATION]) {
4875 if (!rdev->ops->mgmt_tx_cancel_wait)
4876 return -EINVAL;
4877 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4878 }
4879
Jouni Malinen026331c2010-02-15 12:53:10 +02004880 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4881 channel_type = nla_get_u32(
4882 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4883 if (channel_type != NL80211_CHAN_NO_HT &&
4884 channel_type != NL80211_CHAN_HT20 &&
4885 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004886 channel_type != NL80211_CHAN_HT40MINUS)
4887 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004888 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004889 }
4890
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004891 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
4892
Jouni Malinen026331c2010-02-15 12:53:10 +02004893 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4894 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004895 if (chan == NULL)
4896 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004897
4898 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004899 if (!msg)
4900 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004901
4902 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004903 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004904
4905 if (IS_ERR(hdr)) {
4906 err = PTR_ERR(hdr);
4907 goto free_msg;
4908 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004909 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
4910 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02004911 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4912 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4913 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004914 if (err)
4915 goto free_msg;
4916
4917 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4918
4919 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004920 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004921
4922 nla_put_failure:
4923 err = -ENOBUFS;
4924 free_msg:
4925 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004926 return err;
4927}
4928
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004929static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
4930{
4931 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4932 struct net_device *dev = info->user_ptr[1];
4933 u64 cookie;
4934
4935 if (!info->attrs[NL80211_ATTR_COOKIE])
4936 return -EINVAL;
4937
4938 if (!rdev->ops->mgmt_tx_cancel_wait)
4939 return -EOPNOTSUPP;
4940
4941 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4942 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4943 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4944 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4945 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4946 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4947 return -EOPNOTSUPP;
4948
4949 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4950
4951 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
4952}
4953
Kalle Valoffb9eb32010-02-17 17:58:10 +02004954static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4955{
Johannes Berg4c476992010-10-04 21:36:35 +02004956 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004957 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004958 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004959 u8 ps_state;
4960 bool state;
4961 int err;
4962
Johannes Berg4c476992010-10-04 21:36:35 +02004963 if (!info->attrs[NL80211_ATTR_PS_STATE])
4964 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004965
4966 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4967
Johannes Berg4c476992010-10-04 21:36:35 +02004968 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4969 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004970
4971 wdev = dev->ieee80211_ptr;
4972
Johannes Berg4c476992010-10-04 21:36:35 +02004973 if (!rdev->ops->set_power_mgmt)
4974 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004975
4976 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4977
4978 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004979 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004980
Johannes Berg4c476992010-10-04 21:36:35 +02004981 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4982 wdev->ps_timeout);
4983 if (!err)
4984 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004985 return err;
4986}
4987
4988static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4989{
Johannes Berg4c476992010-10-04 21:36:35 +02004990 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004991 enum nl80211_ps_state ps_state;
4992 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004993 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004994 struct sk_buff *msg;
4995 void *hdr;
4996 int err;
4997
Kalle Valoffb9eb32010-02-17 17:58:10 +02004998 wdev = dev->ieee80211_ptr;
4999
Johannes Berg4c476992010-10-04 21:36:35 +02005000 if (!rdev->ops->set_power_mgmt)
5001 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005002
5003 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005004 if (!msg)
5005 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005006
5007 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5008 NL80211_CMD_GET_POWER_SAVE);
5009 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005010 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005011 goto free_msg;
5012 }
5013
5014 if (wdev->ps)
5015 ps_state = NL80211_PS_ENABLED;
5016 else
5017 ps_state = NL80211_PS_DISABLED;
5018
5019 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5020
5021 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005022 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005023
Johannes Berg4c476992010-10-04 21:36:35 +02005024 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005025 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005026 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005027 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005028 return err;
5029}
5030
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005031static struct nla_policy
5032nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5033 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5034 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5035 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5036};
5037
5038static int nl80211_set_cqm_rssi(struct genl_info *info,
5039 s32 threshold, u32 hysteresis)
5040{
Johannes Berg4c476992010-10-04 21:36:35 +02005041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005042 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005043 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005044
5045 if (threshold > 0)
5046 return -EINVAL;
5047
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005048 wdev = dev->ieee80211_ptr;
5049
Johannes Berg4c476992010-10-04 21:36:35 +02005050 if (!rdev->ops->set_cqm_rssi_config)
5051 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005052
Johannes Berg074ac8d2010-09-16 14:58:22 +02005053 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005054 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5055 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005056
Johannes Berg4c476992010-10-04 21:36:35 +02005057 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5058 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005059}
5060
5061static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5062{
5063 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5064 struct nlattr *cqm;
5065 int err;
5066
5067 cqm = info->attrs[NL80211_ATTR_CQM];
5068 if (!cqm) {
5069 err = -EINVAL;
5070 goto out;
5071 }
5072
5073 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5074 nl80211_attr_cqm_policy);
5075 if (err)
5076 goto out;
5077
5078 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5079 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5080 s32 threshold;
5081 u32 hysteresis;
5082 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5083 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5084 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5085 } else
5086 err = -EINVAL;
5087
5088out:
5089 return err;
5090}
5091
Johannes Berg29cbe682010-12-03 09:20:44 +01005092static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5093{
5094 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5095 struct net_device *dev = info->user_ptr[1];
5096 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005097 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005098 int err;
5099
5100 /* start with default */
5101 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005102 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005103
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005104 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005105 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005106 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005107 if (err)
5108 return err;
5109 }
5110
5111 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5112 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5113 return -EINVAL;
5114
Javier Cardonac80d5452010-12-16 17:37:49 -08005115 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5116 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5117
5118 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5119 /* parse additional setup parameters if given */
5120 err = nl80211_parse_mesh_setup(info, &setup);
5121 if (err)
5122 return err;
5123 }
5124
5125 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005126}
5127
5128static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5129{
5130 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5131 struct net_device *dev = info->user_ptr[1];
5132
5133 return cfg80211_leave_mesh(rdev, dev);
5134}
5135
Johannes Bergff1b6e62011-05-04 15:37:28 +02005136static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5137{
5138 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5139 struct sk_buff *msg;
5140 void *hdr;
5141
5142 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5143 return -EOPNOTSUPP;
5144
5145 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5146 if (!msg)
5147 return -ENOMEM;
5148
5149 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5150 NL80211_CMD_GET_WOWLAN);
5151 if (!hdr)
5152 goto nla_put_failure;
5153
5154 if (rdev->wowlan) {
5155 struct nlattr *nl_wowlan;
5156
5157 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5158 if (!nl_wowlan)
5159 goto nla_put_failure;
5160
5161 if (rdev->wowlan->any)
5162 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5163 if (rdev->wowlan->disconnect)
5164 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5165 if (rdev->wowlan->magic_pkt)
5166 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
5167 if (rdev->wowlan->n_patterns) {
5168 struct nlattr *nl_pats, *nl_pat;
5169 int i, pat_len;
5170
5171 nl_pats = nla_nest_start(msg,
5172 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5173 if (!nl_pats)
5174 goto nla_put_failure;
5175
5176 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5177 nl_pat = nla_nest_start(msg, i + 1);
5178 if (!nl_pat)
5179 goto nla_put_failure;
5180 pat_len = rdev->wowlan->patterns[i].pattern_len;
5181 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5182 DIV_ROUND_UP(pat_len, 8),
5183 rdev->wowlan->patterns[i].mask);
5184 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5185 pat_len,
5186 rdev->wowlan->patterns[i].pattern);
5187 nla_nest_end(msg, nl_pat);
5188 }
5189 nla_nest_end(msg, nl_pats);
5190 }
5191
5192 nla_nest_end(msg, nl_wowlan);
5193 }
5194
5195 genlmsg_end(msg, hdr);
5196 return genlmsg_reply(msg, info);
5197
5198nla_put_failure:
5199 nlmsg_free(msg);
5200 return -ENOBUFS;
5201}
5202
5203static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5204{
5205 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5206 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5207 struct cfg80211_wowlan no_triggers = {};
5208 struct cfg80211_wowlan new_triggers = {};
5209 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5210 int err, i;
5211
5212 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5213 return -EOPNOTSUPP;
5214
5215 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5216 goto no_triggers;
5217
5218 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5219 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5220 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5221 nl80211_wowlan_policy);
5222 if (err)
5223 return err;
5224
5225 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5226 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5227 return -EINVAL;
5228 new_triggers.any = true;
5229 }
5230
5231 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5232 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5233 return -EINVAL;
5234 new_triggers.disconnect = true;
5235 }
5236
5237 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5238 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5239 return -EINVAL;
5240 new_triggers.magic_pkt = true;
5241 }
5242
5243 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5244 struct nlattr *pat;
5245 int n_patterns = 0;
5246 int rem, pat_len, mask_len;
5247 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5248
5249 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5250 rem)
5251 n_patterns++;
5252 if (n_patterns > wowlan->n_patterns)
5253 return -EINVAL;
5254
5255 new_triggers.patterns = kcalloc(n_patterns,
5256 sizeof(new_triggers.patterns[0]),
5257 GFP_KERNEL);
5258 if (!new_triggers.patterns)
5259 return -ENOMEM;
5260
5261 new_triggers.n_patterns = n_patterns;
5262 i = 0;
5263
5264 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5265 rem) {
5266 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5267 nla_data(pat), nla_len(pat), NULL);
5268 err = -EINVAL;
5269 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5270 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5271 goto error;
5272 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5273 mask_len = DIV_ROUND_UP(pat_len, 8);
5274 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5275 mask_len)
5276 goto error;
5277 if (pat_len > wowlan->pattern_max_len ||
5278 pat_len < wowlan->pattern_min_len)
5279 goto error;
5280
5281 new_triggers.patterns[i].mask =
5282 kmalloc(mask_len + pat_len, GFP_KERNEL);
5283 if (!new_triggers.patterns[i].mask) {
5284 err = -ENOMEM;
5285 goto error;
5286 }
5287 new_triggers.patterns[i].pattern =
5288 new_triggers.patterns[i].mask + mask_len;
5289 memcpy(new_triggers.patterns[i].mask,
5290 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5291 mask_len);
5292 new_triggers.patterns[i].pattern_len = pat_len;
5293 memcpy(new_triggers.patterns[i].pattern,
5294 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5295 pat_len);
5296 i++;
5297 }
5298 }
5299
5300 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5301 struct cfg80211_wowlan *ntrig;
5302 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5303 GFP_KERNEL);
5304 if (!ntrig) {
5305 err = -ENOMEM;
5306 goto error;
5307 }
5308 cfg80211_rdev_free_wowlan(rdev);
5309 rdev->wowlan = ntrig;
5310 } else {
5311 no_triggers:
5312 cfg80211_rdev_free_wowlan(rdev);
5313 rdev->wowlan = NULL;
5314 }
5315
5316 return 0;
5317 error:
5318 for (i = 0; i < new_triggers.n_patterns; i++)
5319 kfree(new_triggers.patterns[i].mask);
5320 kfree(new_triggers.patterns);
5321 return err;
5322}
5323
Johannes Berg4c476992010-10-04 21:36:35 +02005324#define NL80211_FLAG_NEED_WIPHY 0x01
5325#define NL80211_FLAG_NEED_NETDEV 0x02
5326#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005327#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5328#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5329 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005330
5331static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5332 struct genl_info *info)
5333{
5334 struct cfg80211_registered_device *rdev;
5335 struct net_device *dev;
5336 int err;
5337 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5338
5339 if (rtnl)
5340 rtnl_lock();
5341
5342 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5343 rdev = cfg80211_get_dev_from_info(info);
5344 if (IS_ERR(rdev)) {
5345 if (rtnl)
5346 rtnl_unlock();
5347 return PTR_ERR(rdev);
5348 }
5349 info->user_ptr[0] = rdev;
5350 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5351 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5352 if (err) {
5353 if (rtnl)
5354 rtnl_unlock();
5355 return err;
5356 }
Johannes Berg41265712010-10-04 21:14:05 +02005357 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5358 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005359 cfg80211_unlock_rdev(rdev);
5360 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005361 if (rtnl)
5362 rtnl_unlock();
5363 return -ENETDOWN;
5364 }
Johannes Berg4c476992010-10-04 21:36:35 +02005365 info->user_ptr[0] = rdev;
5366 info->user_ptr[1] = dev;
5367 }
5368
5369 return 0;
5370}
5371
5372static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5373 struct genl_info *info)
5374{
5375 if (info->user_ptr[0])
5376 cfg80211_unlock_rdev(info->user_ptr[0]);
5377 if (info->user_ptr[1])
5378 dev_put(info->user_ptr[1]);
5379 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5380 rtnl_unlock();
5381}
5382
Johannes Berg55682962007-09-20 13:09:35 -04005383static struct genl_ops nl80211_ops[] = {
5384 {
5385 .cmd = NL80211_CMD_GET_WIPHY,
5386 .doit = nl80211_get_wiphy,
5387 .dumpit = nl80211_dump_wiphy,
5388 .policy = nl80211_policy,
5389 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005390 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005391 },
5392 {
5393 .cmd = NL80211_CMD_SET_WIPHY,
5394 .doit = nl80211_set_wiphy,
5395 .policy = nl80211_policy,
5396 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005397 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005398 },
5399 {
5400 .cmd = NL80211_CMD_GET_INTERFACE,
5401 .doit = nl80211_get_interface,
5402 .dumpit = nl80211_dump_interface,
5403 .policy = nl80211_policy,
5404 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005405 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04005406 },
5407 {
5408 .cmd = NL80211_CMD_SET_INTERFACE,
5409 .doit = nl80211_set_interface,
5410 .policy = nl80211_policy,
5411 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005412 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5413 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005414 },
5415 {
5416 .cmd = NL80211_CMD_NEW_INTERFACE,
5417 .doit = nl80211_new_interface,
5418 .policy = nl80211_policy,
5419 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005420 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5421 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005422 },
5423 {
5424 .cmd = NL80211_CMD_DEL_INTERFACE,
5425 .doit = nl80211_del_interface,
5426 .policy = nl80211_policy,
5427 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005428 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5429 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005430 },
Johannes Berg41ade002007-12-19 02:03:29 +01005431 {
5432 .cmd = NL80211_CMD_GET_KEY,
5433 .doit = nl80211_get_key,
5434 .policy = nl80211_policy,
5435 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005436 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5437 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005438 },
5439 {
5440 .cmd = NL80211_CMD_SET_KEY,
5441 .doit = nl80211_set_key,
5442 .policy = nl80211_policy,
5443 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005444 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005445 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005446 },
5447 {
5448 .cmd = NL80211_CMD_NEW_KEY,
5449 .doit = nl80211_new_key,
5450 .policy = nl80211_policy,
5451 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005452 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005453 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005454 },
5455 {
5456 .cmd = NL80211_CMD_DEL_KEY,
5457 .doit = nl80211_del_key,
5458 .policy = nl80211_policy,
5459 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005460 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005461 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005462 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005463 {
5464 .cmd = NL80211_CMD_SET_BEACON,
5465 .policy = nl80211_policy,
5466 .flags = GENL_ADMIN_PERM,
5467 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005468 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5469 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005470 },
5471 {
5472 .cmd = NL80211_CMD_NEW_BEACON,
5473 .policy = nl80211_policy,
5474 .flags = GENL_ADMIN_PERM,
5475 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005476 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5477 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005478 },
5479 {
5480 .cmd = NL80211_CMD_DEL_BEACON,
5481 .policy = nl80211_policy,
5482 .flags = GENL_ADMIN_PERM,
5483 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005484 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5485 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005486 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005487 {
5488 .cmd = NL80211_CMD_GET_STATION,
5489 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005490 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005491 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02005492 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5493 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005494 },
5495 {
5496 .cmd = NL80211_CMD_SET_STATION,
5497 .doit = nl80211_set_station,
5498 .policy = nl80211_policy,
5499 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005500 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5501 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005502 },
5503 {
5504 .cmd = NL80211_CMD_NEW_STATION,
5505 .doit = nl80211_new_station,
5506 .policy = nl80211_policy,
5507 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005508 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005509 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005510 },
5511 {
5512 .cmd = NL80211_CMD_DEL_STATION,
5513 .doit = nl80211_del_station,
5514 .policy = nl80211_policy,
5515 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005516 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5517 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005518 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005519 {
5520 .cmd = NL80211_CMD_GET_MPATH,
5521 .doit = nl80211_get_mpath,
5522 .dumpit = nl80211_dump_mpath,
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,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005527 },
5528 {
5529 .cmd = NL80211_CMD_SET_MPATH,
5530 .doit = nl80211_set_mpath,
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,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005535 },
5536 {
5537 .cmd = NL80211_CMD_NEW_MPATH,
5538 .doit = nl80211_new_mpath,
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,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005543 },
5544 {
5545 .cmd = NL80211_CMD_DEL_MPATH,
5546 .doit = nl80211_del_mpath,
5547 .policy = nl80211_policy,
5548 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005549 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5550 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005551 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005552 {
5553 .cmd = NL80211_CMD_SET_BSS,
5554 .doit = nl80211_set_bss,
5555 .policy = nl80211_policy,
5556 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005557 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5558 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005559 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005560 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005561 .cmd = NL80211_CMD_GET_REG,
5562 .doit = nl80211_get_reg,
5563 .policy = nl80211_policy,
5564 /* can be retrieved by unprivileged users */
5565 },
5566 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005567 .cmd = NL80211_CMD_SET_REG,
5568 .doit = nl80211_set_reg,
5569 .policy = nl80211_policy,
5570 .flags = GENL_ADMIN_PERM,
5571 },
5572 {
5573 .cmd = NL80211_CMD_REQ_SET_REG,
5574 .doit = nl80211_req_set_reg,
5575 .policy = nl80211_policy,
5576 .flags = GENL_ADMIN_PERM,
5577 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005578 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005579 .cmd = NL80211_CMD_GET_MESH_CONFIG,
5580 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005581 .policy = nl80211_policy,
5582 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005583 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5584 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005585 },
5586 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005587 .cmd = NL80211_CMD_SET_MESH_CONFIG,
5588 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005589 .policy = nl80211_policy,
5590 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01005591 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005592 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005593 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005594 {
Johannes Berg2a519312009-02-10 21:25:55 +01005595 .cmd = NL80211_CMD_TRIGGER_SCAN,
5596 .doit = nl80211_trigger_scan,
5597 .policy = nl80211_policy,
5598 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005599 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005600 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01005601 },
5602 {
5603 .cmd = NL80211_CMD_GET_SCAN,
5604 .policy = nl80211_policy,
5605 .dumpit = nl80211_dump_scan,
5606 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005607 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005608 .cmd = NL80211_CMD_START_SCHED_SCAN,
5609 .doit = nl80211_start_sched_scan,
5610 .policy = nl80211_policy,
5611 .flags = GENL_ADMIN_PERM,
5612 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5613 NL80211_FLAG_NEED_RTNL,
5614 },
5615 {
5616 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
5617 .doit = nl80211_stop_sched_scan,
5618 .policy = nl80211_policy,
5619 .flags = GENL_ADMIN_PERM,
5620 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5621 NL80211_FLAG_NEED_RTNL,
5622 },
5623 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02005624 .cmd = NL80211_CMD_AUTHENTICATE,
5625 .doit = nl80211_authenticate,
5626 .policy = nl80211_policy,
5627 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005628 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005629 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005630 },
5631 {
5632 .cmd = NL80211_CMD_ASSOCIATE,
5633 .doit = nl80211_associate,
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 Malinen636a5d32009-03-19 13:39:22 +02005638 },
5639 {
5640 .cmd = NL80211_CMD_DEAUTHENTICATE,
5641 .doit = nl80211_deauthenticate,
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 Malinen636a5d32009-03-19 13:39:22 +02005646 },
5647 {
5648 .cmd = NL80211_CMD_DISASSOCIATE,
5649 .doit = nl80211_disassociate,
5650 .policy = nl80211_policy,
5651 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005652 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005653 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005654 },
Johannes Berg04a773a2009-04-19 21:24:32 +02005655 {
5656 .cmd = NL80211_CMD_JOIN_IBSS,
5657 .doit = nl80211_join_ibss,
5658 .policy = nl80211_policy,
5659 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005660 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005661 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005662 },
5663 {
5664 .cmd = NL80211_CMD_LEAVE_IBSS,
5665 .doit = nl80211_leave_ibss,
5666 .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,
Johannes Berg04a773a2009-04-19 21:24:32 +02005670 },
Johannes Bergaff89a92009-07-01 21:26:51 +02005671#ifdef CONFIG_NL80211_TESTMODE
5672 {
5673 .cmd = NL80211_CMD_TESTMODE,
5674 .doit = nl80211_testmode_do,
5675 .policy = nl80211_policy,
5676 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005677 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5678 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02005679 },
5680#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02005681 {
5682 .cmd = NL80211_CMD_CONNECT,
5683 .doit = nl80211_connect,
5684 .policy = nl80211_policy,
5685 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005686 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005687 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005688 },
5689 {
5690 .cmd = NL80211_CMD_DISCONNECT,
5691 .doit = nl80211_disconnect,
5692 .policy = nl80211_policy,
5693 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005694 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005695 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005696 },
Johannes Berg463d0182009-07-14 00:33:35 +02005697 {
5698 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5699 .doit = nl80211_wiphy_netns,
5700 .policy = nl80211_policy,
5701 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005702 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5703 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02005704 },
Holger Schurig61fa7132009-11-11 12:25:40 +01005705 {
5706 .cmd = NL80211_CMD_GET_SURVEY,
5707 .policy = nl80211_policy,
5708 .dumpit = nl80211_dump_survey,
5709 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005710 {
5711 .cmd = NL80211_CMD_SET_PMKSA,
5712 .doit = nl80211_setdel_pmksa,
5713 .policy = nl80211_policy,
5714 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005715 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5716 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005717 },
5718 {
5719 .cmd = NL80211_CMD_DEL_PMKSA,
5720 .doit = nl80211_setdel_pmksa,
5721 .policy = nl80211_policy,
5722 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005723 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5724 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005725 },
5726 {
5727 .cmd = NL80211_CMD_FLUSH_PMKSA,
5728 .doit = nl80211_flush_pmksa,
5729 .policy = nl80211_policy,
5730 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005731 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5732 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005733 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005734 {
5735 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5736 .doit = nl80211_remain_on_channel,
5737 .policy = nl80211_policy,
5738 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005739 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005740 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005741 },
5742 {
5743 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5744 .doit = nl80211_cancel_remain_on_channel,
5745 .policy = nl80211_policy,
5746 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005747 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005748 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005749 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005750 {
5751 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5752 .doit = nl80211_set_tx_bitrate_mask,
5753 .policy = nl80211_policy,
5754 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005755 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5756 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005757 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005758 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005759 .cmd = NL80211_CMD_REGISTER_FRAME,
5760 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005761 .policy = nl80211_policy,
5762 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005763 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5764 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005765 },
5766 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005767 .cmd = NL80211_CMD_FRAME,
5768 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005769 .policy = nl80211_policy,
5770 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005771 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005772 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005773 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005774 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005775 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
5776 .doit = nl80211_tx_mgmt_cancel_wait,
5777 .policy = nl80211_policy,
5778 .flags = GENL_ADMIN_PERM,
5779 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5780 NL80211_FLAG_NEED_RTNL,
5781 },
5782 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02005783 .cmd = NL80211_CMD_SET_POWER_SAVE,
5784 .doit = nl80211_set_power_save,
5785 .policy = nl80211_policy,
5786 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005787 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5788 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005789 },
5790 {
5791 .cmd = NL80211_CMD_GET_POWER_SAVE,
5792 .doit = nl80211_get_power_save,
5793 .policy = nl80211_policy,
5794 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005795 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5796 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005797 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005798 {
5799 .cmd = NL80211_CMD_SET_CQM,
5800 .doit = nl80211_set_cqm,
5801 .policy = nl80211_policy,
5802 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005803 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5804 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005805 },
Johannes Bergf444de02010-05-05 15:25:02 +02005806 {
5807 .cmd = NL80211_CMD_SET_CHANNEL,
5808 .doit = nl80211_set_channel,
5809 .policy = nl80211_policy,
5810 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005811 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5812 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02005813 },
Bill Jordane8347eb2010-10-01 13:54:28 -04005814 {
5815 .cmd = NL80211_CMD_SET_WDS_PEER,
5816 .doit = nl80211_set_wds_peer,
5817 .policy = nl80211_policy,
5818 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02005819 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5820 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04005821 },
Johannes Berg29cbe682010-12-03 09:20:44 +01005822 {
5823 .cmd = NL80211_CMD_JOIN_MESH,
5824 .doit = nl80211_join_mesh,
5825 .policy = nl80211_policy,
5826 .flags = GENL_ADMIN_PERM,
5827 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5828 NL80211_FLAG_NEED_RTNL,
5829 },
5830 {
5831 .cmd = NL80211_CMD_LEAVE_MESH,
5832 .doit = nl80211_leave_mesh,
5833 .policy = nl80211_policy,
5834 .flags = GENL_ADMIN_PERM,
5835 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5836 NL80211_FLAG_NEED_RTNL,
5837 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02005838 {
5839 .cmd = NL80211_CMD_GET_WOWLAN,
5840 .doit = nl80211_get_wowlan,
5841 .policy = nl80211_policy,
5842 /* can be retrieved by unprivileged users */
5843 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5844 NL80211_FLAG_NEED_RTNL,
5845 },
5846 {
5847 .cmd = NL80211_CMD_SET_WOWLAN,
5848 .doit = nl80211_set_wowlan,
5849 .policy = nl80211_policy,
5850 .flags = GENL_ADMIN_PERM,
5851 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5852 NL80211_FLAG_NEED_RTNL,
5853 },
Johannes Berg55682962007-09-20 13:09:35 -04005854};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005855
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005856static struct genl_multicast_group nl80211_mlme_mcgrp = {
5857 .name = "mlme",
5858};
Johannes Berg55682962007-09-20 13:09:35 -04005859
5860/* multicast groups */
5861static struct genl_multicast_group nl80211_config_mcgrp = {
5862 .name = "config",
5863};
Johannes Berg2a519312009-02-10 21:25:55 +01005864static struct genl_multicast_group nl80211_scan_mcgrp = {
5865 .name = "scan",
5866};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005867static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5868 .name = "regulatory",
5869};
Johannes Berg55682962007-09-20 13:09:35 -04005870
5871/* notification functions */
5872
5873void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5874{
5875 struct sk_buff *msg;
5876
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005877 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005878 if (!msg)
5879 return;
5880
5881 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5882 nlmsg_free(msg);
5883 return;
5884 }
5885
Johannes Berg463d0182009-07-14 00:33:35 +02005886 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5887 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005888}
5889
Johannes Berg362a4152009-05-24 16:43:15 +02005890static int nl80211_add_scan_req(struct sk_buff *msg,
5891 struct cfg80211_registered_device *rdev)
5892{
5893 struct cfg80211_scan_request *req = rdev->scan_req;
5894 struct nlattr *nest;
5895 int i;
5896
Johannes Berg667503d2009-07-07 03:56:11 +02005897 ASSERT_RDEV_LOCK(rdev);
5898
Johannes Berg362a4152009-05-24 16:43:15 +02005899 if (WARN_ON(!req))
5900 return 0;
5901
5902 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5903 if (!nest)
5904 goto nla_put_failure;
5905 for (i = 0; i < req->n_ssids; i++)
5906 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5907 nla_nest_end(msg, nest);
5908
5909 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5910 if (!nest)
5911 goto nla_put_failure;
5912 for (i = 0; i < req->n_channels; i++)
5913 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5914 nla_nest_end(msg, nest);
5915
5916 if (req->ie)
5917 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5918
5919 return 0;
5920 nla_put_failure:
5921 return -ENOBUFS;
5922}
5923
Johannes Berga538e2d2009-06-16 19:56:42 +02005924static int nl80211_send_scan_msg(struct sk_buff *msg,
5925 struct cfg80211_registered_device *rdev,
5926 struct net_device *netdev,
5927 u32 pid, u32 seq, int flags,
5928 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005929{
5930 void *hdr;
5931
5932 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5933 if (!hdr)
5934 return -1;
5935
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005936 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005937 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5938
Johannes Berg362a4152009-05-24 16:43:15 +02005939 /* ignore errors and send incomplete event anyway */
5940 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005941
5942 return genlmsg_end(msg, hdr);
5943
5944 nla_put_failure:
5945 genlmsg_cancel(msg, hdr);
5946 return -EMSGSIZE;
5947}
5948
Luciano Coelho807f8a82011-05-11 17:09:35 +03005949static int
5950nl80211_send_sched_scan_msg(struct sk_buff *msg,
5951 struct cfg80211_registered_device *rdev,
5952 struct net_device *netdev,
5953 u32 pid, u32 seq, int flags, u32 cmd)
5954{
5955 void *hdr;
5956
5957 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5958 if (!hdr)
5959 return -1;
5960
5961 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5962 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5963
5964 return genlmsg_end(msg, hdr);
5965
5966 nla_put_failure:
5967 genlmsg_cancel(msg, hdr);
5968 return -EMSGSIZE;
5969}
5970
Johannes Berga538e2d2009-06-16 19:56:42 +02005971void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5972 struct net_device *netdev)
5973{
5974 struct sk_buff *msg;
5975
5976 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5977 if (!msg)
5978 return;
5979
5980 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5981 NL80211_CMD_TRIGGER_SCAN) < 0) {
5982 nlmsg_free(msg);
5983 return;
5984 }
5985
Johannes Berg463d0182009-07-14 00:33:35 +02005986 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5987 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005988}
5989
Johannes Berg2a519312009-02-10 21:25:55 +01005990void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5991 struct net_device *netdev)
5992{
5993 struct sk_buff *msg;
5994
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005995 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005996 if (!msg)
5997 return;
5998
Johannes Berga538e2d2009-06-16 19:56:42 +02005999 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6000 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006001 nlmsg_free(msg);
6002 return;
6003 }
6004
Johannes Berg463d0182009-07-14 00:33:35 +02006005 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6006 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006007}
6008
6009void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6010 struct net_device *netdev)
6011{
6012 struct sk_buff *msg;
6013
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006014 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006015 if (!msg)
6016 return;
6017
Johannes Berga538e2d2009-06-16 19:56:42 +02006018 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6019 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006020 nlmsg_free(msg);
6021 return;
6022 }
6023
Johannes Berg463d0182009-07-14 00:33:35 +02006024 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6025 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006026}
6027
Luciano Coelho807f8a82011-05-11 17:09:35 +03006028void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6029 struct net_device *netdev)
6030{
6031 struct sk_buff *msg;
6032
6033 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6034 if (!msg)
6035 return;
6036
6037 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6038 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6039 nlmsg_free(msg);
6040 return;
6041 }
6042
6043 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6044 nl80211_scan_mcgrp.id, GFP_KERNEL);
6045}
6046
6047void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6048 struct net_device *netdev, u32 cmd)
6049{
6050 struct sk_buff *msg;
6051
6052 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6053 if (!msg)
6054 return;
6055
6056 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6057 nlmsg_free(msg);
6058 return;
6059 }
6060
6061 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6062 nl80211_scan_mcgrp.id, GFP_KERNEL);
6063}
6064
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006065/*
6066 * This can happen on global regulatory changes or device specific settings
6067 * based on custom world regulatory domains.
6068 */
6069void nl80211_send_reg_change_event(struct regulatory_request *request)
6070{
6071 struct sk_buff *msg;
6072 void *hdr;
6073
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006074 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006075 if (!msg)
6076 return;
6077
6078 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6079 if (!hdr) {
6080 nlmsg_free(msg);
6081 return;
6082 }
6083
6084 /* Userspace can always count this one always being set */
6085 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6086
6087 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6088 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6089 NL80211_REGDOM_TYPE_WORLD);
6090 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6091 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6092 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6093 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6094 request->intersect)
6095 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6096 NL80211_REGDOM_TYPE_INTERSECTION);
6097 else {
6098 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6099 NL80211_REGDOM_TYPE_COUNTRY);
6100 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6101 }
6102
6103 if (wiphy_idx_valid(request->wiphy_idx))
6104 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6105
6106 if (genlmsg_end(msg, hdr) < 0) {
6107 nlmsg_free(msg);
6108 return;
6109 }
6110
Johannes Bergbc43b282009-07-25 10:54:13 +02006111 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006112 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006113 GFP_ATOMIC);
6114 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006115
6116 return;
6117
6118nla_put_failure:
6119 genlmsg_cancel(msg, hdr);
6120 nlmsg_free(msg);
6121}
6122
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006123static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6124 struct net_device *netdev,
6125 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006126 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006127{
6128 struct sk_buff *msg;
6129 void *hdr;
6130
Johannes Berge6d6e342009-07-01 21:26:47 +02006131 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006132 if (!msg)
6133 return;
6134
6135 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6136 if (!hdr) {
6137 nlmsg_free(msg);
6138 return;
6139 }
6140
6141 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6142 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6143 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6144
6145 if (genlmsg_end(msg, hdr) < 0) {
6146 nlmsg_free(msg);
6147 return;
6148 }
6149
Johannes Berg463d0182009-07-14 00:33:35 +02006150 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6151 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006152 return;
6153
6154 nla_put_failure:
6155 genlmsg_cancel(msg, hdr);
6156 nlmsg_free(msg);
6157}
6158
6159void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006160 struct net_device *netdev, const u8 *buf,
6161 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006162{
6163 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006164 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006165}
6166
6167void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6168 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006169 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006170{
Johannes Berge6d6e342009-07-01 21:26:47 +02006171 nl80211_send_mlme_event(rdev, netdev, buf, len,
6172 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006173}
6174
Jouni Malinen53b46b82009-03-27 20:53:56 +02006175void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006176 struct net_device *netdev, const u8 *buf,
6177 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006178{
6179 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006180 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006181}
6182
Jouni Malinen53b46b82009-03-27 20:53:56 +02006183void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6184 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006185 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006186{
6187 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006188 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006189}
6190
Jouni Malinencf4e5942010-12-16 00:52:40 +02006191void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6192 struct net_device *netdev, const u8 *buf,
6193 size_t len, gfp_t gfp)
6194{
6195 nl80211_send_mlme_event(rdev, netdev, buf, len,
6196 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6197}
6198
6199void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6200 struct net_device *netdev, const u8 *buf,
6201 size_t len, gfp_t gfp)
6202{
6203 nl80211_send_mlme_event(rdev, netdev, buf, len,
6204 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6205}
6206
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006207static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6208 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006209 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006210{
6211 struct sk_buff *msg;
6212 void *hdr;
6213
Johannes Berge6d6e342009-07-01 21:26:47 +02006214 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006215 if (!msg)
6216 return;
6217
6218 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6219 if (!hdr) {
6220 nlmsg_free(msg);
6221 return;
6222 }
6223
6224 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6225 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6226 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6227 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6228
6229 if (genlmsg_end(msg, hdr) < 0) {
6230 nlmsg_free(msg);
6231 return;
6232 }
6233
Johannes Berg463d0182009-07-14 00:33:35 +02006234 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6235 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006236 return;
6237
6238 nla_put_failure:
6239 genlmsg_cancel(msg, hdr);
6240 nlmsg_free(msg);
6241}
6242
6243void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006244 struct net_device *netdev, const u8 *addr,
6245 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006246{
6247 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006248 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006249}
6250
6251void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006252 struct net_device *netdev, const u8 *addr,
6253 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006254{
Johannes Berge6d6e342009-07-01 21:26:47 +02006255 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6256 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006257}
6258
Samuel Ortizb23aa672009-07-01 21:26:54 +02006259void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6260 struct net_device *netdev, const u8 *bssid,
6261 const u8 *req_ie, size_t req_ie_len,
6262 const u8 *resp_ie, size_t resp_ie_len,
6263 u16 status, gfp_t gfp)
6264{
6265 struct sk_buff *msg;
6266 void *hdr;
6267
6268 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6269 if (!msg)
6270 return;
6271
6272 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6273 if (!hdr) {
6274 nlmsg_free(msg);
6275 return;
6276 }
6277
6278 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6279 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6280 if (bssid)
6281 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6282 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6283 if (req_ie)
6284 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6285 if (resp_ie)
6286 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6287
6288 if (genlmsg_end(msg, hdr) < 0) {
6289 nlmsg_free(msg);
6290 return;
6291 }
6292
Johannes Berg463d0182009-07-14 00:33:35 +02006293 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6294 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006295 return;
6296
6297 nla_put_failure:
6298 genlmsg_cancel(msg, hdr);
6299 nlmsg_free(msg);
6300
6301}
6302
6303void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6304 struct net_device *netdev, const u8 *bssid,
6305 const u8 *req_ie, size_t req_ie_len,
6306 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6307{
6308 struct sk_buff *msg;
6309 void *hdr;
6310
6311 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6312 if (!msg)
6313 return;
6314
6315 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6316 if (!hdr) {
6317 nlmsg_free(msg);
6318 return;
6319 }
6320
6321 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6322 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6323 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6324 if (req_ie)
6325 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6326 if (resp_ie)
6327 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6328
6329 if (genlmsg_end(msg, hdr) < 0) {
6330 nlmsg_free(msg);
6331 return;
6332 }
6333
Johannes Berg463d0182009-07-14 00:33:35 +02006334 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6335 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006336 return;
6337
6338 nla_put_failure:
6339 genlmsg_cancel(msg, hdr);
6340 nlmsg_free(msg);
6341
6342}
6343
6344void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6345 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02006346 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006347{
6348 struct sk_buff *msg;
6349 void *hdr;
6350
Johannes Berg667503d2009-07-07 03:56:11 +02006351 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006352 if (!msg)
6353 return;
6354
6355 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6356 if (!hdr) {
6357 nlmsg_free(msg);
6358 return;
6359 }
6360
6361 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6362 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6363 if (from_ap && reason)
6364 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6365 if (from_ap)
6366 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6367 if (ie)
6368 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6369
6370 if (genlmsg_end(msg, hdr) < 0) {
6371 nlmsg_free(msg);
6372 return;
6373 }
6374
Johannes Berg463d0182009-07-14 00:33:35 +02006375 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6376 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006377 return;
6378
6379 nla_put_failure:
6380 genlmsg_cancel(msg, hdr);
6381 nlmsg_free(msg);
6382
6383}
6384
Johannes Berg04a773a2009-04-19 21:24:32 +02006385void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6386 struct net_device *netdev, const u8 *bssid,
6387 gfp_t gfp)
6388{
6389 struct sk_buff *msg;
6390 void *hdr;
6391
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006392 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006393 if (!msg)
6394 return;
6395
6396 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6397 if (!hdr) {
6398 nlmsg_free(msg);
6399 return;
6400 }
6401
6402 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6403 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6404 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6405
6406 if (genlmsg_end(msg, hdr) < 0) {
6407 nlmsg_free(msg);
6408 return;
6409 }
6410
Johannes Berg463d0182009-07-14 00:33:35 +02006411 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6412 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006413 return;
6414
6415 nla_put_failure:
6416 genlmsg_cancel(msg, hdr);
6417 nlmsg_free(msg);
6418}
6419
Javier Cardonac93b5e72011-04-07 15:08:34 -07006420void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6421 struct net_device *netdev,
6422 const u8 *macaddr, const u8* ie, u8 ie_len,
6423 gfp_t gfp)
6424{
6425 struct sk_buff *msg;
6426 void *hdr;
6427
6428 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6429 if (!msg)
6430 return;
6431
6432 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6433 if (!hdr) {
6434 nlmsg_free(msg);
6435 return;
6436 }
6437
6438 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6439 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6440 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6441 if (ie_len && ie)
6442 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6443
6444 if (genlmsg_end(msg, hdr) < 0) {
6445 nlmsg_free(msg);
6446 return;
6447 }
6448
6449 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6450 nl80211_mlme_mcgrp.id, gfp);
6451 return;
6452
6453 nla_put_failure:
6454 genlmsg_cancel(msg, hdr);
6455 nlmsg_free(msg);
6456}
6457
Jouni Malinena3b8b052009-03-27 21:59:49 +02006458void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6459 struct net_device *netdev, const u8 *addr,
6460 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02006461 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02006462{
6463 struct sk_buff *msg;
6464 void *hdr;
6465
Johannes Berge6d6e342009-07-01 21:26:47 +02006466 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006467 if (!msg)
6468 return;
6469
6470 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
6471 if (!hdr) {
6472 nlmsg_free(msg);
6473 return;
6474 }
6475
6476 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6477 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6478 if (addr)
6479 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6480 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03006481 if (key_id != -1)
6482 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006483 if (tsc)
6484 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
6485
6486 if (genlmsg_end(msg, hdr) < 0) {
6487 nlmsg_free(msg);
6488 return;
6489 }
6490
Johannes Berg463d0182009-07-14 00:33:35 +02006491 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6492 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006493 return;
6494
6495 nla_put_failure:
6496 genlmsg_cancel(msg, hdr);
6497 nlmsg_free(msg);
6498}
6499
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006500void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
6501 struct ieee80211_channel *channel_before,
6502 struct ieee80211_channel *channel_after)
6503{
6504 struct sk_buff *msg;
6505 void *hdr;
6506 struct nlattr *nl_freq;
6507
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006508 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006509 if (!msg)
6510 return;
6511
6512 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
6513 if (!hdr) {
6514 nlmsg_free(msg);
6515 return;
6516 }
6517
6518 /*
6519 * Since we are applying the beacon hint to a wiphy we know its
6520 * wiphy_idx is valid
6521 */
6522 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
6523
6524 /* Before */
6525 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
6526 if (!nl_freq)
6527 goto nla_put_failure;
6528 if (nl80211_msg_put_channel(msg, channel_before))
6529 goto nla_put_failure;
6530 nla_nest_end(msg, nl_freq);
6531
6532 /* After */
6533 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
6534 if (!nl_freq)
6535 goto nla_put_failure;
6536 if (nl80211_msg_put_channel(msg, channel_after))
6537 goto nla_put_failure;
6538 nla_nest_end(msg, nl_freq);
6539
6540 if (genlmsg_end(msg, hdr) < 0) {
6541 nlmsg_free(msg);
6542 return;
6543 }
6544
Johannes Berg463d0182009-07-14 00:33:35 +02006545 rcu_read_lock();
6546 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6547 GFP_ATOMIC);
6548 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006549
6550 return;
6551
6552nla_put_failure:
6553 genlmsg_cancel(msg, hdr);
6554 nlmsg_free(msg);
6555}
6556
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006557static void nl80211_send_remain_on_chan_event(
6558 int cmd, struct cfg80211_registered_device *rdev,
6559 struct net_device *netdev, u64 cookie,
6560 struct ieee80211_channel *chan,
6561 enum nl80211_channel_type channel_type,
6562 unsigned int duration, gfp_t gfp)
6563{
6564 struct sk_buff *msg;
6565 void *hdr;
6566
6567 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6568 if (!msg)
6569 return;
6570
6571 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6572 if (!hdr) {
6573 nlmsg_free(msg);
6574 return;
6575 }
6576
6577 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6578 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6579 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
6580 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
6581 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6582
6583 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
6584 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
6585
6586 if (genlmsg_end(msg, hdr) < 0) {
6587 nlmsg_free(msg);
6588 return;
6589 }
6590
6591 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6592 nl80211_mlme_mcgrp.id, gfp);
6593 return;
6594
6595 nla_put_failure:
6596 genlmsg_cancel(msg, hdr);
6597 nlmsg_free(msg);
6598}
6599
6600void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
6601 struct net_device *netdev, u64 cookie,
6602 struct ieee80211_channel *chan,
6603 enum nl80211_channel_type channel_type,
6604 unsigned int duration, gfp_t gfp)
6605{
6606 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
6607 rdev, netdev, cookie, chan,
6608 channel_type, duration, gfp);
6609}
6610
6611void nl80211_send_remain_on_channel_cancel(
6612 struct cfg80211_registered_device *rdev, struct net_device *netdev,
6613 u64 cookie, struct ieee80211_channel *chan,
6614 enum nl80211_channel_type channel_type, gfp_t gfp)
6615{
6616 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6617 rdev, netdev, cookie, chan,
6618 channel_type, 0, gfp);
6619}
6620
Johannes Berg98b62182009-12-23 13:15:44 +01006621void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
6622 struct net_device *dev, const u8 *mac_addr,
6623 struct station_info *sinfo, gfp_t gfp)
6624{
6625 struct sk_buff *msg;
6626
6627 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6628 if (!msg)
6629 return;
6630
6631 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
6632 nlmsg_free(msg);
6633 return;
6634 }
6635
6636 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6637 nl80211_mlme_mcgrp.id, gfp);
6638}
6639
Jouni Malinenec15e682011-03-23 15:29:52 +02006640void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
6641 struct net_device *dev, const u8 *mac_addr,
6642 gfp_t gfp)
6643{
6644 struct sk_buff *msg;
6645 void *hdr;
6646
6647 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6648 if (!msg)
6649 return;
6650
6651 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
6652 if (!hdr) {
6653 nlmsg_free(msg);
6654 return;
6655 }
6656
6657 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
6658 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
6659
6660 if (genlmsg_end(msg, hdr) < 0) {
6661 nlmsg_free(msg);
6662 return;
6663 }
6664
6665 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6666 nl80211_mlme_mcgrp.id, gfp);
6667 return;
6668
6669 nla_put_failure:
6670 genlmsg_cancel(msg, hdr);
6671 nlmsg_free(msg);
6672}
6673
Johannes Berg2e161f72010-08-12 15:38:38 +02006674int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
6675 struct net_device *netdev, u32 nlpid,
6676 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006677{
6678 struct sk_buff *msg;
6679 void *hdr;
6680 int err;
6681
6682 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6683 if (!msg)
6684 return -ENOMEM;
6685
Johannes Berg2e161f72010-08-12 15:38:38 +02006686 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006687 if (!hdr) {
6688 nlmsg_free(msg);
6689 return -ENOMEM;
6690 }
6691
6692 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6693 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6694 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6695 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6696
6697 err = genlmsg_end(msg, hdr);
6698 if (err < 0) {
6699 nlmsg_free(msg);
6700 return err;
6701 }
6702
6703 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
6704 if (err < 0)
6705 return err;
6706 return 0;
6707
6708 nla_put_failure:
6709 genlmsg_cancel(msg, hdr);
6710 nlmsg_free(msg);
6711 return -ENOBUFS;
6712}
6713
Johannes Berg2e161f72010-08-12 15:38:38 +02006714void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
6715 struct net_device *netdev, u64 cookie,
6716 const u8 *buf, size_t len, bool ack,
6717 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006718{
6719 struct sk_buff *msg;
6720 void *hdr;
6721
6722 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6723 if (!msg)
6724 return;
6725
Johannes Berg2e161f72010-08-12 15:38:38 +02006726 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02006727 if (!hdr) {
6728 nlmsg_free(msg);
6729 return;
6730 }
6731
6732 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6733 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6734 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6735 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6736 if (ack)
6737 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
6738
6739 if (genlmsg_end(msg, hdr) < 0) {
6740 nlmsg_free(msg);
6741 return;
6742 }
6743
6744 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
6745 return;
6746
6747 nla_put_failure:
6748 genlmsg_cancel(msg, hdr);
6749 nlmsg_free(msg);
6750}
6751
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006752void
6753nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
6754 struct net_device *netdev,
6755 enum nl80211_cqm_rssi_threshold_event rssi_event,
6756 gfp_t gfp)
6757{
6758 struct sk_buff *msg;
6759 struct nlattr *pinfoattr;
6760 void *hdr;
6761
6762 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6763 if (!msg)
6764 return;
6765
6766 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6767 if (!hdr) {
6768 nlmsg_free(msg);
6769 return;
6770 }
6771
6772 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6773 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6774
6775 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6776 if (!pinfoattr)
6777 goto nla_put_failure;
6778
6779 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
6780 rssi_event);
6781
6782 nla_nest_end(msg, pinfoattr);
6783
6784 if (genlmsg_end(msg, hdr) < 0) {
6785 nlmsg_free(msg);
6786 return;
6787 }
6788
6789 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6790 nl80211_mlme_mcgrp.id, gfp);
6791 return;
6792
6793 nla_put_failure:
6794 genlmsg_cancel(msg, hdr);
6795 nlmsg_free(msg);
6796}
6797
Johannes Bergc063dbf2010-11-24 08:10:05 +01006798void
6799nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
6800 struct net_device *netdev, const u8 *peer,
6801 u32 num_packets, gfp_t gfp)
6802{
6803 struct sk_buff *msg;
6804 struct nlattr *pinfoattr;
6805 void *hdr;
6806
6807 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6808 if (!msg)
6809 return;
6810
6811 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6812 if (!hdr) {
6813 nlmsg_free(msg);
6814 return;
6815 }
6816
6817 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6818 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6819 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
6820
6821 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6822 if (!pinfoattr)
6823 goto nla_put_failure;
6824
6825 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
6826
6827 nla_nest_end(msg, pinfoattr);
6828
6829 if (genlmsg_end(msg, hdr) < 0) {
6830 nlmsg_free(msg);
6831 return;
6832 }
6833
6834 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6835 nl80211_mlme_mcgrp.id, gfp);
6836 return;
6837
6838 nla_put_failure:
6839 genlmsg_cancel(msg, hdr);
6840 nlmsg_free(msg);
6841}
6842
Jouni Malinen026331c2010-02-15 12:53:10 +02006843static int nl80211_netlink_notify(struct notifier_block * nb,
6844 unsigned long state,
6845 void *_notify)
6846{
6847 struct netlink_notify *notify = _notify;
6848 struct cfg80211_registered_device *rdev;
6849 struct wireless_dev *wdev;
6850
6851 if (state != NETLINK_URELEASE)
6852 return NOTIFY_DONE;
6853
6854 rcu_read_lock();
6855
6856 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
6857 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02006858 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02006859
6860 rcu_read_unlock();
6861
6862 return NOTIFY_DONE;
6863}
6864
6865static struct notifier_block nl80211_netlink_notifier = {
6866 .notifier_call = nl80211_netlink_notify,
6867};
6868
Johannes Berg55682962007-09-20 13:09:35 -04006869/* initialisation/exit functions */
6870
6871int nl80211_init(void)
6872{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006873 int err;
Johannes Berg55682962007-09-20 13:09:35 -04006874
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006875 err = genl_register_family_with_ops(&nl80211_fam,
6876 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04006877 if (err)
6878 return err;
6879
Johannes Berg55682962007-09-20 13:09:35 -04006880 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6881 if (err)
6882 goto err_out;
6883
Johannes Berg2a519312009-02-10 21:25:55 +01006884 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6885 if (err)
6886 goto err_out;
6887
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006888 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6889 if (err)
6890 goto err_out;
6891
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006892 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6893 if (err)
6894 goto err_out;
6895
Johannes Bergaff89a92009-07-01 21:26:51 +02006896#ifdef CONFIG_NL80211_TESTMODE
6897 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6898 if (err)
6899 goto err_out;
6900#endif
6901
Jouni Malinen026331c2010-02-15 12:53:10 +02006902 err = netlink_register_notifier(&nl80211_netlink_notifier);
6903 if (err)
6904 goto err_out;
6905
Johannes Berg55682962007-09-20 13:09:35 -04006906 return 0;
6907 err_out:
6908 genl_unregister_family(&nl80211_fam);
6909 return err;
6910}
6911
6912void nl80211_exit(void)
6913{
Jouni Malinen026331c2010-02-15 12:53:10 +02006914 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006915 genl_unregister_family(&nl80211_fam);
6916}