blob: 1237431c3efae58582b2a811fd52c7a0a1883b83 [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 Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300283
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700286
Johannes Berg6c739412011-11-03 09:27:01 +0100287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300301 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300302 [NL80211_ATTR_STA_FLAGS2] = {
303 .len = sizeof(struct nl80211_sta_flag_update),
304 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300305 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300306 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
307 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200308 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
309 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
310 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200311 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100312 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100313 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
314 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100315 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
316 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200317 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200318 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
319 .len = IEEE80211_MAX_DATA_LEN },
320 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200321 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200322 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300323 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200324 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300325 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
326 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200327 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900328 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
329 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100330 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100331 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100332 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200333 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700334 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300335 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200336 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200337 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300338 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300339 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
340 .len = IEEE80211_MAX_DATA_LEN },
341 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530343 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300344 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530345 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300346 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
349 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100351 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200352 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
353 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700354 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800355 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
356 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
357 .len = NL80211_HT_CAPABILITY_LEN
358 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100359 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530360 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530361 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200362 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700363 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300364 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000365 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700366 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100367 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
368 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530369 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
370 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Johannes Berg55682962007-09-20 13:09:35 -0400371};
372
Johannes Berge31b8212010-10-05 19:39:30 +0200373/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000374static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200375 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200376 [NL80211_KEY_IDX] = { .type = NLA_U8 },
377 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200378 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200379 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
380 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200381 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100382 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
383};
384
385/* policy for the key default flags */
386static const struct nla_policy
387nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
388 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
389 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200390};
391
Johannes Bergff1b6e62011-05-04 15:37:28 +0200392/* policy for WoWLAN attributes */
393static const struct nla_policy
394nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
395 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
396 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
397 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
398 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200399 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
400 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
401 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
402 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100403 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
404};
405
406static const struct nla_policy
407nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
408 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
409 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
410 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
411 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
412 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
413 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
414 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
415 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
416 },
417 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
418 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
419 },
420 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
421 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
422 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200423};
424
Johannes Berge5497d72011-07-05 16:35:40 +0200425/* policy for GTK rekey offload attributes */
426static const struct nla_policy
427nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
428 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
429 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
430 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
431};
432
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300433static const struct nla_policy
434nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200435 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300436 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700437 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300438};
439
Holger Schuriga0438972009-11-11 11:30:02 +0100440/* ifidx get helper */
441static int nl80211_get_ifidx(struct netlink_callback *cb)
442{
443 int res;
444
445 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
446 nl80211_fam.attrbuf, nl80211_fam.maxattr,
447 nl80211_policy);
448 if (res)
449 return res;
450
451 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
452 return -EINVAL;
453
454 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
455 if (!res)
456 return -EINVAL;
457 return res;
458}
459
Johannes Berg67748892010-10-04 21:14:06 +0200460static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
461 struct netlink_callback *cb,
462 struct cfg80211_registered_device **rdev,
463 struct net_device **dev)
464{
465 int ifidx = cb->args[0];
466 int err;
467
468 if (!ifidx)
469 ifidx = nl80211_get_ifidx(cb);
470 if (ifidx < 0)
471 return ifidx;
472
473 cb->args[0] = ifidx;
474
475 rtnl_lock();
476
477 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
478 if (!*dev) {
479 err = -ENODEV;
480 goto out_rtnl;
481 }
482
483 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100484 if (IS_ERR(*rdev)) {
485 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200486 goto out_rtnl;
487 }
488
489 return 0;
490 out_rtnl:
491 rtnl_unlock();
492 return err;
493}
494
495static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
496{
497 cfg80211_unlock_rdev(rdev);
498 rtnl_unlock();
499}
500
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100501/* IE validation */
502static bool is_valid_ie_attr(const struct nlattr *attr)
503{
504 const u8 *pos;
505 int len;
506
507 if (!attr)
508 return true;
509
510 pos = nla_data(attr);
511 len = nla_len(attr);
512
513 while (len) {
514 u8 elemlen;
515
516 if (len < 2)
517 return false;
518 len -= 2;
519
520 elemlen = pos[1];
521 if (elemlen > len)
522 return false;
523
524 len -= elemlen;
525 pos += 2 + elemlen;
526 }
527
528 return true;
529}
530
Johannes Berg55682962007-09-20 13:09:35 -0400531/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000532static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400533 int flags, u8 cmd)
534{
535 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000536 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400537}
538
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400539static int nl80211_msg_put_channel(struct sk_buff *msg,
540 struct ieee80211_channel *chan)
541{
David S. Miller9360ffd2012-03-29 04:41:26 -0400542 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
543 chan->center_freq))
544 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400545
David S. Miller9360ffd2012-03-29 04:41:26 -0400546 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
547 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
548 goto nla_put_failure;
549 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
550 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
551 goto nla_put_failure;
552 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
553 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
554 goto nla_put_failure;
Simon Wunderlich04f39042013-02-08 18:16:19 +0100555 if (chan->flags & IEEE80211_CHAN_RADAR) {
556 u32 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
557 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
558 goto nla_put_failure;
559 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
560 chan->dfs_state))
561 goto nla_put_failure;
562 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME, time))
563 goto nla_put_failure;
564 }
Johannes Berg50640f12012-12-12 17:59:39 +0100565 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
566 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
567 goto nla_put_failure;
568 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
569 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
570 goto nla_put_failure;
571 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
572 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
573 goto nla_put_failure;
574 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
575 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
576 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400577
David S. Miller9360ffd2012-03-29 04:41:26 -0400578 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
579 DBM_TO_MBM(chan->max_power)))
580 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400581
582 return 0;
583
584 nla_put_failure:
585 return -ENOBUFS;
586}
587
Johannes Berg55682962007-09-20 13:09:35 -0400588/* netlink command implementations */
589
Johannes Bergb9454e82009-07-08 13:29:08 +0200590struct key_parse {
591 struct key_params p;
592 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200593 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200594 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100595 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200596};
597
598static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
599{
600 struct nlattr *tb[NL80211_KEY_MAX + 1];
601 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
602 nl80211_key_policy);
603 if (err)
604 return err;
605
606 k->def = !!tb[NL80211_KEY_DEFAULT];
607 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
608
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100609 if (k->def) {
610 k->def_uni = true;
611 k->def_multi = true;
612 }
613 if (k->defmgmt)
614 k->def_multi = true;
615
Johannes Bergb9454e82009-07-08 13:29:08 +0200616 if (tb[NL80211_KEY_IDX])
617 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
618
619 if (tb[NL80211_KEY_DATA]) {
620 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
621 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
622 }
623
624 if (tb[NL80211_KEY_SEQ]) {
625 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
626 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
627 }
628
629 if (tb[NL80211_KEY_CIPHER])
630 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
631
Johannes Berge31b8212010-10-05 19:39:30 +0200632 if (tb[NL80211_KEY_TYPE]) {
633 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
634 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
635 return -EINVAL;
636 }
637
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100638 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
639 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100640 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
641 tb[NL80211_KEY_DEFAULT_TYPES],
642 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100643 if (err)
644 return err;
645
646 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
647 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
648 }
649
Johannes Bergb9454e82009-07-08 13:29:08 +0200650 return 0;
651}
652
653static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
654{
655 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
656 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
657 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
658 }
659
660 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
661 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
662 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
663 }
664
665 if (info->attrs[NL80211_ATTR_KEY_IDX])
666 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
667
668 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
669 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
670
671 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
672 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
673
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100674 if (k->def) {
675 k->def_uni = true;
676 k->def_multi = true;
677 }
678 if (k->defmgmt)
679 k->def_multi = true;
680
Johannes Berge31b8212010-10-05 19:39:30 +0200681 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
682 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
683 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
684 return -EINVAL;
685 }
686
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100687 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
688 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
689 int err = nla_parse_nested(
690 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
691 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
692 nl80211_key_default_policy);
693 if (err)
694 return err;
695
696 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
697 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
698 }
699
Johannes Bergb9454e82009-07-08 13:29:08 +0200700 return 0;
701}
702
703static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
704{
705 int err;
706
707 memset(k, 0, sizeof(*k));
708 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200709 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200710
711 if (info->attrs[NL80211_ATTR_KEY])
712 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
713 else
714 err = nl80211_parse_key_old(info, k);
715
716 if (err)
717 return err;
718
719 if (k->def && k->defmgmt)
720 return -EINVAL;
721
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100722 if (k->defmgmt) {
723 if (k->def_uni || !k->def_multi)
724 return -EINVAL;
725 }
726
Johannes Bergb9454e82009-07-08 13:29:08 +0200727 if (k->idx != -1) {
728 if (k->defmgmt) {
729 if (k->idx < 4 || k->idx > 5)
730 return -EINVAL;
731 } else if (k->def) {
732 if (k->idx < 0 || k->idx > 3)
733 return -EINVAL;
734 } else {
735 if (k->idx < 0 || k->idx > 5)
736 return -EINVAL;
737 }
738 }
739
740 return 0;
741}
742
Johannes Bergfffd0932009-07-08 14:22:54 +0200743static struct cfg80211_cached_keys *
744nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530745 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200746{
747 struct key_parse parse;
748 struct nlattr *key;
749 struct cfg80211_cached_keys *result;
750 int rem, err, def = 0;
751
752 result = kzalloc(sizeof(*result), GFP_KERNEL);
753 if (!result)
754 return ERR_PTR(-ENOMEM);
755
756 result->def = -1;
757 result->defmgmt = -1;
758
759 nla_for_each_nested(key, keys, rem) {
760 memset(&parse, 0, sizeof(parse));
761 parse.idx = -1;
762
763 err = nl80211_parse_key_new(key, &parse);
764 if (err)
765 goto error;
766 err = -EINVAL;
767 if (!parse.p.key)
768 goto error;
769 if (parse.idx < 0 || parse.idx > 4)
770 goto error;
771 if (parse.def) {
772 if (def)
773 goto error;
774 def = 1;
775 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100776 if (!parse.def_uni || !parse.def_multi)
777 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200778 } else if (parse.defmgmt)
779 goto error;
780 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200781 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200782 if (err)
783 goto error;
784 result->params[parse.idx].cipher = parse.p.cipher;
785 result->params[parse.idx].key_len = parse.p.key_len;
786 result->params[parse.idx].key = result->data[parse.idx];
787 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530788
789 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
790 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
791 if (no_ht)
792 *no_ht = true;
793 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200794 }
795
796 return result;
797 error:
798 kfree(result);
799 return ERR_PTR(err);
800}
801
802static int nl80211_key_allowed(struct wireless_dev *wdev)
803{
804 ASSERT_WDEV_LOCK(wdev);
805
Johannes Bergfffd0932009-07-08 14:22:54 +0200806 switch (wdev->iftype) {
807 case NL80211_IFTYPE_AP:
808 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200809 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700810 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200811 break;
812 case NL80211_IFTYPE_ADHOC:
813 if (!wdev->current_bss)
814 return -ENOLINK;
815 break;
816 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200817 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200818 if (wdev->sme_state != CFG80211_SME_CONNECTED)
819 return -ENOLINK;
820 break;
821 default:
822 return -EINVAL;
823 }
824
825 return 0;
826}
827
Johannes Berg7527a782011-05-13 10:58:57 +0200828static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
829{
830 struct nlattr *nl_modes = nla_nest_start(msg, attr);
831 int i;
832
833 if (!nl_modes)
834 goto nla_put_failure;
835
836 i = 0;
837 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400838 if ((ifmodes & 1) && nla_put_flag(msg, i))
839 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200840 ifmodes >>= 1;
841 i++;
842 }
843
844 nla_nest_end(msg, nl_modes);
845 return 0;
846
847nla_put_failure:
848 return -ENOBUFS;
849}
850
851static int nl80211_put_iface_combinations(struct wiphy *wiphy,
852 struct sk_buff *msg)
853{
854 struct nlattr *nl_combis;
855 int i, j;
856
857 nl_combis = nla_nest_start(msg,
858 NL80211_ATTR_INTERFACE_COMBINATIONS);
859 if (!nl_combis)
860 goto nla_put_failure;
861
862 for (i = 0; i < wiphy->n_iface_combinations; i++) {
863 const struct ieee80211_iface_combination *c;
864 struct nlattr *nl_combi, *nl_limits;
865
866 c = &wiphy->iface_combinations[i];
867
868 nl_combi = nla_nest_start(msg, i + 1);
869 if (!nl_combi)
870 goto nla_put_failure;
871
872 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
873 if (!nl_limits)
874 goto nla_put_failure;
875
876 for (j = 0; j < c->n_limits; j++) {
877 struct nlattr *nl_limit;
878
879 nl_limit = nla_nest_start(msg, j + 1);
880 if (!nl_limit)
881 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400882 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
883 c->limits[j].max))
884 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200885 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
886 c->limits[j].types))
887 goto nla_put_failure;
888 nla_nest_end(msg, nl_limit);
889 }
890
891 nla_nest_end(msg, nl_limits);
892
David S. Miller9360ffd2012-03-29 04:41:26 -0400893 if (c->beacon_int_infra_match &&
894 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
895 goto nla_put_failure;
896 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
897 c->num_different_channels) ||
898 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
899 c->max_interfaces))
900 goto nla_put_failure;
Simon Wunderlich11c4a072013-01-08 14:04:07 +0100901 if (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
902 c->radar_detect_widths))
903 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200904
905 nla_nest_end(msg, nl_combi);
906 }
907
908 nla_nest_end(msg, nl_combis);
909
910 return 0;
911nla_put_failure:
912 return -ENOBUFS;
913}
914
Johannes Berg2a0e0472013-01-23 22:57:40 +0100915#ifdef CONFIG_PM
916static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
917 struct sk_buff *msg)
918{
919 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
920 struct nlattr *nl_tcp;
921
922 if (!tcp)
923 return 0;
924
925 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
926 if (!nl_tcp)
927 return -ENOBUFS;
928
929 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
930 tcp->data_payload_max))
931 return -ENOBUFS;
932
933 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
934 tcp->data_payload_max))
935 return -ENOBUFS;
936
937 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
938 return -ENOBUFS;
939
940 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
941 sizeof(*tcp->tok), tcp->tok))
942 return -ENOBUFS;
943
944 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
945 tcp->data_interval_max))
946 return -ENOBUFS;
947
948 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
949 tcp->wake_payload_max))
950 return -ENOBUFS;
951
952 nla_nest_end(msg, nl_tcp);
953 return 0;
954}
955#endif
956
Eric W. Biederman15e47302012-09-07 20:12:54 +0000957static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400958 struct cfg80211_registered_device *dev)
959{
960 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100961 struct nlattr *nl_bands, *nl_band;
962 struct nlattr *nl_freqs, *nl_freq;
963 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100964 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100965 enum ieee80211_band band;
966 struct ieee80211_channel *chan;
967 struct ieee80211_rate *rate;
968 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200969 const struct ieee80211_txrx_stypes *mgmt_stypes =
970 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400971
Eric W. Biederman15e47302012-09-07 20:12:54 +0000972 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400973 if (!hdr)
974 return -1;
975
David S. Miller9360ffd2012-03-29 04:41:26 -0400976 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
977 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
978 nla_put_u32(msg, NL80211_ATTR_GENERATION,
979 cfg80211_rdev_list_generation) ||
980 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
981 dev->wiphy.retry_short) ||
982 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
983 dev->wiphy.retry_long) ||
984 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
985 dev->wiphy.frag_threshold) ||
986 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
987 dev->wiphy.rts_threshold) ||
988 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
989 dev->wiphy.coverage_class) ||
990 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
991 dev->wiphy.max_scan_ssids) ||
992 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
993 dev->wiphy.max_sched_scan_ssids) ||
994 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
995 dev->wiphy.max_scan_ie_len) ||
996 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
997 dev->wiphy.max_sched_scan_ie_len) ||
998 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
999 dev->wiphy.max_match_sets))
1000 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001001
David S. Miller9360ffd2012-03-29 04:41:26 -04001002 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1003 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1004 goto nla_put_failure;
1005 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1006 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1007 goto nla_put_failure;
1008 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1009 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1010 goto nla_put_failure;
1011 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1012 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1013 goto nla_put_failure;
1014 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1015 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1016 goto nla_put_failure;
1017 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1018 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1019 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001020
David S. Miller9360ffd2012-03-29 04:41:26 -04001021 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1022 sizeof(u32) * dev->wiphy.n_cipher_suites,
1023 dev->wiphy.cipher_suites))
1024 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001025
David S. Miller9360ffd2012-03-29 04:41:26 -04001026 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1027 dev->wiphy.max_num_pmkids))
1028 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +05301029
David S. Miller9360ffd2012-03-29 04:41:26 -04001030 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1031 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1032 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +02001033
David S. Miller9360ffd2012-03-29 04:41:26 -04001034 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1035 dev->wiphy.available_antennas_tx) ||
1036 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1037 dev->wiphy.available_antennas_rx))
1038 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001039
David S. Miller9360ffd2012-03-29 04:41:26 -04001040 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1041 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1042 dev->wiphy.probe_resp_offload))
1043 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +02001044
Bruno Randolf7f531e02010-12-16 11:30:22 +09001045 if ((dev->wiphy.available_antennas_tx ||
1046 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001047 u32 tx_ant = 0, rx_ant = 0;
1048 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +03001049 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001050 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001051 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
1052 tx_ant) ||
1053 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
1054 rx_ant))
1055 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001056 }
1057 }
1058
Johannes Berg7527a782011-05-13 10:58:57 +02001059 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1060 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001061 goto nla_put_failure;
1062
Johannes Bergee688b002008-01-24 19:38:39 +01001063 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1064 if (!nl_bands)
1065 goto nla_put_failure;
1066
1067 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1068 if (!dev->wiphy.bands[band])
1069 continue;
1070
1071 nl_band = nla_nest_start(msg, band);
1072 if (!nl_band)
1073 goto nla_put_failure;
1074
Johannes Bergd51626d2008-10-09 12:20:13 +02001075 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -04001076 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
1077 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1078 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
1079 &dev->wiphy.bands[band]->ht_cap.mcs) ||
1080 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1081 dev->wiphy.bands[band]->ht_cap.cap) ||
1082 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1083 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
1084 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1085 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
1086 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001087
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001088 /* add VHT info */
1089 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1090 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1091 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1092 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1093 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1094 dev->wiphy.bands[band]->vht_cap.cap)))
1095 goto nla_put_failure;
1096
Johannes Bergee688b002008-01-24 19:38:39 +01001097 /* add frequencies */
1098 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1099 if (!nl_freqs)
1100 goto nla_put_failure;
1101
1102 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1103 nl_freq = nla_nest_start(msg, i);
1104 if (!nl_freq)
1105 goto nla_put_failure;
1106
1107 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001108
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001109 if (nl80211_msg_put_channel(msg, chan))
1110 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001111
Johannes Bergee688b002008-01-24 19:38:39 +01001112 nla_nest_end(msg, nl_freq);
1113 }
1114
1115 nla_nest_end(msg, nl_freqs);
1116
1117 /* add bitrates */
1118 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1119 if (!nl_rates)
1120 goto nla_put_failure;
1121
1122 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1123 nl_rate = nla_nest_start(msg, i);
1124 if (!nl_rate)
1125 goto nla_put_failure;
1126
1127 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001128 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1129 rate->bitrate))
1130 goto nla_put_failure;
1131 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1132 nla_put_flag(msg,
1133 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1134 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001135
1136 nla_nest_end(msg, nl_rate);
1137 }
1138
1139 nla_nest_end(msg, nl_rates);
1140
1141 nla_nest_end(msg, nl_band);
1142 }
1143 nla_nest_end(msg, nl_bands);
1144
Johannes Berg8fdc6212009-03-14 09:34:01 +01001145 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1146 if (!nl_cmds)
1147 goto nla_put_failure;
1148
1149 i = 0;
1150#define CMD(op, n) \
1151 do { \
1152 if (dev->ops->op) { \
1153 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001154 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1155 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001156 } \
1157 } while (0)
1158
1159 CMD(add_virtual_intf, NEW_INTERFACE);
1160 CMD(change_virtual_intf, SET_INTERFACE);
1161 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001162 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001163 CMD(add_station, NEW_STATION);
1164 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001165 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001166 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001167 CMD(auth, AUTHENTICATE);
1168 CMD(assoc, ASSOCIATE);
1169 CMD(deauth, DEAUTHENTICATE);
1170 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001171 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001172 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001173 CMD(set_pmksa, SET_PMKSA);
1174 CMD(del_pmksa, DEL_PMKSA);
1175 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001176 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1177 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001178 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001179 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001180 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001181 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001182 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001183 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1184 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001185 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001186 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001187 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001188 i++;
1189 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1190 goto nla_put_failure;
1191 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001192 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001193 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1194 CMD(tdls_mgmt, TDLS_MGMT);
1195 CMD(tdls_oper, TDLS_OPER);
1196 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001197 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1198 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001199 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001200 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001201 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1202 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001203 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1204 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001205 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001206 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001207 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001208
Kalle Valo4745fc02011-11-17 19:06:10 +02001209#ifdef CONFIG_NL80211_TESTMODE
1210 CMD(testmode_cmd, TESTMODE);
1211#endif
1212
Johannes Berg8fdc6212009-03-14 09:34:01 +01001213#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001214
Johannes Berg6829c872009-07-02 09:13:27 +02001215 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001216 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001217 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1218 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001219 }
1220
Johannes Berg6829c872009-07-02 09:13:27 +02001221 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001222 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001223 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1224 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001225 }
1226
Johannes Berg8fdc6212009-03-14 09:34:01 +01001227 nla_nest_end(msg, nl_cmds);
1228
Johannes Berg7c4ef712011-11-18 15:33:48 +01001229 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001230 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1231 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1232 dev->wiphy.max_remain_on_channel_duration))
1233 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001234
David S. Miller9360ffd2012-03-29 04:41:26 -04001235 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1236 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1237 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001238
Johannes Berg2e161f72010-08-12 15:38:38 +02001239 if (mgmt_stypes) {
1240 u16 stypes;
1241 struct nlattr *nl_ftypes, *nl_ifs;
1242 enum nl80211_iftype ift;
1243
1244 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1245 if (!nl_ifs)
1246 goto nla_put_failure;
1247
1248 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1249 nl_ftypes = nla_nest_start(msg, ift);
1250 if (!nl_ftypes)
1251 goto nla_put_failure;
1252 i = 0;
1253 stypes = mgmt_stypes[ift].tx;
1254 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001255 if ((stypes & 1) &&
1256 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1257 (i << 4) | IEEE80211_FTYPE_MGMT))
1258 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001259 stypes >>= 1;
1260 i++;
1261 }
1262 nla_nest_end(msg, nl_ftypes);
1263 }
1264
Johannes Berg74b70a42010-08-24 12:15:53 +02001265 nla_nest_end(msg, nl_ifs);
1266
Johannes Berg2e161f72010-08-12 15:38:38 +02001267 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1268 if (!nl_ifs)
1269 goto nla_put_failure;
1270
1271 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1272 nl_ftypes = nla_nest_start(msg, ift);
1273 if (!nl_ftypes)
1274 goto nla_put_failure;
1275 i = 0;
1276 stypes = mgmt_stypes[ift].rx;
1277 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001278 if ((stypes & 1) &&
1279 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1280 (i << 4) | IEEE80211_FTYPE_MGMT))
1281 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001282 stypes >>= 1;
1283 i++;
1284 }
1285 nla_nest_end(msg, nl_ftypes);
1286 }
1287 nla_nest_end(msg, nl_ifs);
1288 }
1289
Johannes Bergdfb89c52012-06-27 09:23:48 +02001290#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001291 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1292 struct nlattr *nl_wowlan;
1293
1294 nl_wowlan = nla_nest_start(msg,
1295 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1296 if (!nl_wowlan)
1297 goto nla_put_failure;
1298
David S. Miller9360ffd2012-03-29 04:41:26 -04001299 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1300 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1301 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1302 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1303 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1304 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1305 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1306 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1307 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1308 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1309 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1310 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1311 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1312 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1313 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1314 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1315 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001316 if (dev->wiphy.wowlan.n_patterns) {
1317 struct nl80211_wowlan_pattern_support pat = {
1318 .max_patterns = dev->wiphy.wowlan.n_patterns,
1319 .min_pattern_len =
1320 dev->wiphy.wowlan.pattern_min_len,
1321 .max_pattern_len =
1322 dev->wiphy.wowlan.pattern_max_len,
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08001323 .max_pkt_offset =
1324 dev->wiphy.wowlan.max_pkt_offset,
Johannes Bergff1b6e62011-05-04 15:37:28 +02001325 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001326 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1327 sizeof(pat), &pat))
1328 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001329 }
1330
Johannes Berg2a0e0472013-01-23 22:57:40 +01001331 if (nl80211_send_wowlan_tcp_caps(dev, msg))
1332 goto nla_put_failure;
1333
Johannes Bergff1b6e62011-05-04 15:37:28 +02001334 nla_nest_end(msg, nl_wowlan);
1335 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001336#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001337
Johannes Berg7527a782011-05-13 10:58:57 +02001338 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1339 dev->wiphy.software_iftypes))
1340 goto nla_put_failure;
1341
1342 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1343 goto nla_put_failure;
1344
David S. Miller9360ffd2012-03-29 04:41:26 -04001345 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1346 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1347 dev->wiphy.ap_sme_capa))
1348 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001349
David S. Miller9360ffd2012-03-29 04:41:26 -04001350 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1351 dev->wiphy.features))
1352 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001353
David S. Miller9360ffd2012-03-29 04:41:26 -04001354 if (dev->wiphy.ht_capa_mod_mask &&
1355 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1356 sizeof(*dev->wiphy.ht_capa_mod_mask),
1357 dev->wiphy.ht_capa_mod_mask))
1358 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001359
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05301360 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1361 dev->wiphy.max_acl_mac_addrs &&
1362 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1363 dev->wiphy.max_acl_mac_addrs))
1364 goto nla_put_failure;
1365
Johannes Berga50df0c2013-02-11 14:20:05 +01001366 if (dev->wiphy.extended_capabilities &&
1367 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1368 dev->wiphy.extended_capabilities_len,
1369 dev->wiphy.extended_capabilities) ||
1370 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1371 dev->wiphy.extended_capabilities_len,
1372 dev->wiphy.extended_capabilities_mask)))
1373 goto nla_put_failure;
1374
Johannes Berg55682962007-09-20 13:09:35 -04001375 return genlmsg_end(msg, hdr);
1376
1377 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001378 genlmsg_cancel(msg, hdr);
1379 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001380}
1381
1382static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1383{
1384 int idx = 0;
1385 int start = cb->args[0];
1386 struct cfg80211_registered_device *dev;
1387
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001388 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001389 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001390 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1391 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001392 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001393 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001394 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001395 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001396 dev) < 0) {
1397 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001398 break;
Julius Volzb4637272008-07-08 14:02:19 +02001399 }
Johannes Berg55682962007-09-20 13:09:35 -04001400 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001401 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001402
1403 cb->args[0] = idx;
1404
1405 return skb->len;
1406}
1407
1408static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1409{
1410 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001411 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001412
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001413 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001414 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001415 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001416
Eric W. Biederman15e47302012-09-07 20:12:54 +00001417 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001418 nlmsg_free(msg);
1419 return -ENOBUFS;
1420 }
Johannes Berg55682962007-09-20 13:09:35 -04001421
Johannes Berg134e6372009-07-10 09:51:34 +00001422 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001423}
1424
Jouni Malinen31888482008-10-30 16:59:24 +02001425static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1426 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1427 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1428 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1429 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1430 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1431};
1432
1433static int parse_txq_params(struct nlattr *tb[],
1434 struct ieee80211_txq_params *txq_params)
1435{
Johannes Berga3304b02012-03-28 11:04:24 +02001436 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001437 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1438 !tb[NL80211_TXQ_ATTR_AIFS])
1439 return -EINVAL;
1440
Johannes Berga3304b02012-03-28 11:04:24 +02001441 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001442 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1443 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1444 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1445 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1446
Johannes Berga3304b02012-03-28 11:04:24 +02001447 if (txq_params->ac >= NL80211_NUM_ACS)
1448 return -EINVAL;
1449
Jouni Malinen31888482008-10-30 16:59:24 +02001450 return 0;
1451}
1452
Johannes Bergf444de02010-05-05 15:25:02 +02001453static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1454{
1455 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001456 * You can only set the channel explicitly for WDS interfaces,
1457 * all others have their channel managed via their respective
1458 * "establish a connection" command (connect, join, ...)
1459 *
1460 * For AP/GO and mesh mode, the channel can be set with the
1461 * channel userspace API, but is only stored and passed to the
1462 * low-level driver when the AP starts or the mesh is joined.
1463 * This is for backward compatibility, userspace can also give
1464 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001465 *
1466 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001467 * whatever else is going on, so they have their own special
1468 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001469 */
1470 return !wdev ||
1471 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001472 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001473 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1474 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001475}
1476
Johannes Berg683b6d32012-11-08 21:25:48 +01001477static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1478 struct genl_info *info,
1479 struct cfg80211_chan_def *chandef)
1480{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301481 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001482
1483 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1484 return -EINVAL;
1485
1486 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1487
1488 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001489 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1490 chandef->center_freq1 = control_freq;
1491 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001492
1493 /* Primary channel not allowed */
1494 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1495 return -EINVAL;
1496
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001497 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1498 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001499
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001500 chantype = nla_get_u32(
1501 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1502
1503 switch (chantype) {
1504 case NL80211_CHAN_NO_HT:
1505 case NL80211_CHAN_HT20:
1506 case NL80211_CHAN_HT40PLUS:
1507 case NL80211_CHAN_HT40MINUS:
1508 cfg80211_chandef_create(chandef, chandef->chan,
1509 chantype);
1510 break;
1511 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001512 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001513 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001514 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1515 chandef->width =
1516 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1517 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1518 chandef->center_freq1 =
1519 nla_get_u32(
1520 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1521 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1522 chandef->center_freq2 =
1523 nla_get_u32(
1524 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1525 }
1526
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001527 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001528 return -EINVAL;
1529
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001530 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1531 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001532 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001533
Johannes Berg683b6d32012-11-08 21:25:48 +01001534 return 0;
1535}
1536
Johannes Bergf444de02010-05-05 15:25:02 +02001537static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1538 struct wireless_dev *wdev,
1539 struct genl_info *info)
1540{
Johannes Berg683b6d32012-11-08 21:25:48 +01001541 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001542 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001543 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1544
1545 if (wdev)
1546 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001547
Johannes Bergf444de02010-05-05 15:25:02 +02001548 if (!nl80211_can_set_dev_channel(wdev))
1549 return -EOPNOTSUPP;
1550
Johannes Berg683b6d32012-11-08 21:25:48 +01001551 result = nl80211_parse_chandef(rdev, info, &chandef);
1552 if (result)
1553 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001554
1555 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001556 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001557 case NL80211_IFTYPE_AP:
1558 case NL80211_IFTYPE_P2P_GO:
1559 if (wdev->beacon_interval) {
1560 result = -EBUSY;
1561 break;
1562 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001563 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001564 result = -EINVAL;
1565 break;
1566 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001567 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001568 result = 0;
1569 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001570 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001571 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001572 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001573 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001574 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001575 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001576 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001577 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001578 }
1579 mutex_unlock(&rdev->devlist_mtx);
1580
1581 return result;
1582}
1583
1584static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1585{
Johannes Berg4c476992010-10-04 21:36:35 +02001586 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1587 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001588
Johannes Berg4c476992010-10-04 21:36:35 +02001589 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001590}
1591
Bill Jordane8347eb2010-10-01 13:54:28 -04001592static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1593{
Johannes Berg43b19952010-10-07 13:10:30 +02001594 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1595 struct net_device *dev = info->user_ptr[1];
1596 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001597 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001598
1599 if (!info->attrs[NL80211_ATTR_MAC])
1600 return -EINVAL;
1601
Johannes Berg43b19952010-10-07 13:10:30 +02001602 if (netif_running(dev))
1603 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001604
Johannes Berg43b19952010-10-07 13:10:30 +02001605 if (!rdev->ops->set_wds_peer)
1606 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001607
Johannes Berg43b19952010-10-07 13:10:30 +02001608 if (wdev->iftype != NL80211_IFTYPE_WDS)
1609 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001610
1611 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001612 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001613}
1614
1615
Johannes Berg55682962007-09-20 13:09:35 -04001616static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1617{
1618 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001619 struct net_device *netdev = NULL;
1620 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001621 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001622 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001623 u32 changed;
1624 u8 retry_short = 0, retry_long = 0;
1625 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001626 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001627
Johannes Bergf444de02010-05-05 15:25:02 +02001628 /*
1629 * Try to find the wiphy and netdev. Normally this
1630 * function shouldn't need the netdev, but this is
1631 * done for backward compatibility -- previously
1632 * setting the channel was done per wiphy, but now
1633 * it is per netdev. Previous userland like hostapd
1634 * also passed a netdev to set_wiphy, so that it is
1635 * possible to let that go to the right netdev!
1636 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001637 mutex_lock(&cfg80211_mutex);
1638
Johannes Bergf444de02010-05-05 15:25:02 +02001639 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1640 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1641
1642 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1643 if (netdev && netdev->ieee80211_ptr) {
1644 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1645 mutex_lock(&rdev->mtx);
1646 } else
1647 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001648 }
1649
Johannes Bergf444de02010-05-05 15:25:02 +02001650 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001651 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1652 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001653 if (IS_ERR(rdev)) {
1654 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001655 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001656 }
1657 wdev = NULL;
1658 netdev = NULL;
1659 result = 0;
1660
1661 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001662 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001663 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001664
1665 /*
1666 * end workaround code, by now the rdev is available
1667 * and locked, and wdev may or may not be NULL.
1668 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001669
1670 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001671 result = cfg80211_dev_rename(
1672 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001673
1674 mutex_unlock(&cfg80211_mutex);
1675
1676 if (result)
1677 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001678
Jouni Malinen31888482008-10-30 16:59:24 +02001679 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1680 struct ieee80211_txq_params txq_params;
1681 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1682
1683 if (!rdev->ops->set_txq_params) {
1684 result = -EOPNOTSUPP;
1685 goto bad_res;
1686 }
1687
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001688 if (!netdev) {
1689 result = -EINVAL;
1690 goto bad_res;
1691 }
1692
Johannes Berg133a3ff2011-11-03 14:50:13 +01001693 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1694 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1695 result = -EINVAL;
1696 goto bad_res;
1697 }
1698
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001699 if (!netif_running(netdev)) {
1700 result = -ENETDOWN;
1701 goto bad_res;
1702 }
1703
Jouni Malinen31888482008-10-30 16:59:24 +02001704 nla_for_each_nested(nl_txq_params,
1705 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1706 rem_txq_params) {
1707 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1708 nla_data(nl_txq_params),
1709 nla_len(nl_txq_params),
1710 txq_params_policy);
1711 result = parse_txq_params(tb, &txq_params);
1712 if (result)
1713 goto bad_res;
1714
Hila Gonene35e4d22012-06-27 17:19:42 +03001715 result = rdev_set_txq_params(rdev, netdev,
1716 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001717 if (result)
1718 goto bad_res;
1719 }
1720 }
1721
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001722 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001723 result = __nl80211_set_channel(rdev,
1724 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1725 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001726 if (result)
1727 goto bad_res;
1728 }
1729
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001730 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001731 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001732 enum nl80211_tx_power_setting type;
1733 int idx, mbm = 0;
1734
Johannes Bergc8442112012-10-24 10:17:18 +02001735 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1736 txp_wdev = NULL;
1737
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001738 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001739 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001740 goto bad_res;
1741 }
1742
1743 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1744 type = nla_get_u32(info->attrs[idx]);
1745
1746 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1747 (type != NL80211_TX_POWER_AUTOMATIC)) {
1748 result = -EINVAL;
1749 goto bad_res;
1750 }
1751
1752 if (type != NL80211_TX_POWER_AUTOMATIC) {
1753 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1754 mbm = nla_get_u32(info->attrs[idx]);
1755 }
1756
Johannes Bergc8442112012-10-24 10:17:18 +02001757 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001758 if (result)
1759 goto bad_res;
1760 }
1761
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001762 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1763 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1764 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001765 if ((!rdev->wiphy.available_antennas_tx &&
1766 !rdev->wiphy.available_antennas_rx) ||
1767 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001768 result = -EOPNOTSUPP;
1769 goto bad_res;
1770 }
1771
1772 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1773 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1774
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001775 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001776 * available antenna masks, except for the "all" mask */
1777 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1778 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001779 result = -EINVAL;
1780 goto bad_res;
1781 }
1782
Bruno Randolf7f531e02010-12-16 11:30:22 +09001783 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1784 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001785
Hila Gonene35e4d22012-06-27 17:19:42 +03001786 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001787 if (result)
1788 goto bad_res;
1789 }
1790
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001791 changed = 0;
1792
1793 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1794 retry_short = nla_get_u8(
1795 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1796 if (retry_short == 0) {
1797 result = -EINVAL;
1798 goto bad_res;
1799 }
1800 changed |= WIPHY_PARAM_RETRY_SHORT;
1801 }
1802
1803 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1804 retry_long = nla_get_u8(
1805 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1806 if (retry_long == 0) {
1807 result = -EINVAL;
1808 goto bad_res;
1809 }
1810 changed |= WIPHY_PARAM_RETRY_LONG;
1811 }
1812
1813 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1814 frag_threshold = nla_get_u32(
1815 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1816 if (frag_threshold < 256) {
1817 result = -EINVAL;
1818 goto bad_res;
1819 }
1820 if (frag_threshold != (u32) -1) {
1821 /*
1822 * Fragments (apart from the last one) are required to
1823 * have even length. Make the fragmentation code
1824 * simpler by stripping LSB should someone try to use
1825 * odd threshold value.
1826 */
1827 frag_threshold &= ~0x1;
1828 }
1829 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1830 }
1831
1832 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1833 rts_threshold = nla_get_u32(
1834 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1835 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1836 }
1837
Lukáš Turek81077e82009-12-21 22:50:47 +01001838 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1839 coverage_class = nla_get_u8(
1840 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1841 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1842 }
1843
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001844 if (changed) {
1845 u8 old_retry_short, old_retry_long;
1846 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001847 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001848
1849 if (!rdev->ops->set_wiphy_params) {
1850 result = -EOPNOTSUPP;
1851 goto bad_res;
1852 }
1853
1854 old_retry_short = rdev->wiphy.retry_short;
1855 old_retry_long = rdev->wiphy.retry_long;
1856 old_frag_threshold = rdev->wiphy.frag_threshold;
1857 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001858 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001859
1860 if (changed & WIPHY_PARAM_RETRY_SHORT)
1861 rdev->wiphy.retry_short = retry_short;
1862 if (changed & WIPHY_PARAM_RETRY_LONG)
1863 rdev->wiphy.retry_long = retry_long;
1864 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1865 rdev->wiphy.frag_threshold = frag_threshold;
1866 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1867 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001868 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1869 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001870
Hila Gonene35e4d22012-06-27 17:19:42 +03001871 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001872 if (result) {
1873 rdev->wiphy.retry_short = old_retry_short;
1874 rdev->wiphy.retry_long = old_retry_long;
1875 rdev->wiphy.frag_threshold = old_frag_threshold;
1876 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001877 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001878 }
1879 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001880
Johannes Berg306d6112008-12-08 12:39:04 +01001881 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001882 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001883 if (netdev)
1884 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001885 return result;
1886}
1887
Johannes Berg71bbc992012-06-15 15:30:18 +02001888static inline u64 wdev_id(struct wireless_dev *wdev)
1889{
1890 return (u64)wdev->identifier |
1891 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1892}
Johannes Berg55682962007-09-20 13:09:35 -04001893
Johannes Berg683b6d32012-11-08 21:25:48 +01001894static int nl80211_send_chandef(struct sk_buff *msg,
1895 struct cfg80211_chan_def *chandef)
1896{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001897 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001898
Johannes Berg683b6d32012-11-08 21:25:48 +01001899 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1900 chandef->chan->center_freq))
1901 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001902 switch (chandef->width) {
1903 case NL80211_CHAN_WIDTH_20_NOHT:
1904 case NL80211_CHAN_WIDTH_20:
1905 case NL80211_CHAN_WIDTH_40:
1906 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1907 cfg80211_get_chandef_type(chandef)))
1908 return -ENOBUFS;
1909 break;
1910 default:
1911 break;
1912 }
1913 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1914 return -ENOBUFS;
1915 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1916 return -ENOBUFS;
1917 if (chandef->center_freq2 &&
1918 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001919 return -ENOBUFS;
1920 return 0;
1921}
1922
Eric W. Biederman15e47302012-09-07 20:12:54 +00001923static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001924 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001925 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001926{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001927 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001928 void *hdr;
1929
Eric W. Biederman15e47302012-09-07 20:12:54 +00001930 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001931 if (!hdr)
1932 return -1;
1933
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001934 if (dev &&
1935 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001936 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001937 goto nla_put_failure;
1938
1939 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1940 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001941 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001942 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001943 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1944 rdev->devlist_generation ^
1945 (cfg80211_rdev_list_generation << 2)))
1946 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001947
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001948 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001949 int ret;
1950 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001951
Johannes Berg683b6d32012-11-08 21:25:48 +01001952 ret = rdev_get_channel(rdev, wdev, &chandef);
1953 if (ret == 0) {
1954 if (nl80211_send_chandef(msg, &chandef))
1955 goto nla_put_failure;
1956 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001957 }
1958
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001959 if (wdev->ssid_len) {
1960 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1961 goto nla_put_failure;
1962 }
1963
Johannes Berg55682962007-09-20 13:09:35 -04001964 return genlmsg_end(msg, hdr);
1965
1966 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001967 genlmsg_cancel(msg, hdr);
1968 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001969}
1970
1971static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1972{
1973 int wp_idx = 0;
1974 int if_idx = 0;
1975 int wp_start = cb->args[0];
1976 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001977 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001978 struct wireless_dev *wdev;
1979
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001980 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001981 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1982 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001983 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001984 if (wp_idx < wp_start) {
1985 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001986 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001987 }
Johannes Berg55682962007-09-20 13:09:35 -04001988 if_idx = 0;
1989
Johannes Bergf5ea9122009-08-07 16:17:38 +02001990 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001991 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001992 if (if_idx < if_start) {
1993 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001994 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001995 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001996 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001997 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001998 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001999 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002000 goto out;
2001 }
2002 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002003 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002004 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002005
2006 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002007 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002008 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002009 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002010
2011 cb->args[0] = wp_idx;
2012 cb->args[1] = if_idx;
2013
2014 return skb->len;
2015}
2016
2017static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2018{
2019 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002020 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002021 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002022
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002023 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002024 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002025 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002026
Eric W. Biederman15e47302012-09-07 20:12:54 +00002027 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002028 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002029 nlmsg_free(msg);
2030 return -ENOBUFS;
2031 }
Johannes Berg55682962007-09-20 13:09:35 -04002032
Johannes Berg134e6372009-07-10 09:51:34 +00002033 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002034}
2035
Michael Wu66f7ac52008-01-31 19:48:22 +01002036static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2037 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2038 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2039 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2040 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2041 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2042};
2043
2044static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2045{
2046 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2047 int flag;
2048
2049 *mntrflags = 0;
2050
2051 if (!nla)
2052 return -EINVAL;
2053
2054 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2055 nla, mntr_flags_policy))
2056 return -EINVAL;
2057
2058 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2059 if (flags[flag])
2060 *mntrflags |= (1<<flag);
2061
2062 return 0;
2063}
2064
Johannes Berg9bc383d2009-11-19 11:55:19 +01002065static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002066 struct net_device *netdev, u8 use_4addr,
2067 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002068{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002069 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002070 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002071 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002072 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002073 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002074
2075 switch (iftype) {
2076 case NL80211_IFTYPE_AP_VLAN:
2077 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2078 return 0;
2079 break;
2080 case NL80211_IFTYPE_STATION:
2081 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2082 return 0;
2083 break;
2084 default:
2085 break;
2086 }
2087
2088 return -EOPNOTSUPP;
2089}
2090
Johannes Berg55682962007-09-20 13:09:35 -04002091static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2092{
Johannes Berg4c476992010-10-04 21:36:35 +02002093 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002094 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002095 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002096 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002097 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002098 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002099 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002100
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002101 memset(&params, 0, sizeof(params));
2102
Johannes Berg04a773a2009-04-19 21:24:32 +02002103 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002104
Johannes Berg723b0382008-09-16 20:22:09 +02002105 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002106 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002107 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002108 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002109 if (ntype > NL80211_IFTYPE_MAX)
2110 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002111 }
2112
Johannes Berg92ffe052008-09-16 20:39:36 +02002113 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002114 struct wireless_dev *wdev = dev->ieee80211_ptr;
2115
Johannes Berg4c476992010-10-04 21:36:35 +02002116 if (ntype != NL80211_IFTYPE_MESH_POINT)
2117 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002118 if (netif_running(dev))
2119 return -EBUSY;
2120
2121 wdev_lock(wdev);
2122 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2123 IEEE80211_MAX_MESH_ID_LEN);
2124 wdev->mesh_id_up_len =
2125 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2126 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2127 wdev->mesh_id_up_len);
2128 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002129 }
2130
Felix Fietkau8b787642009-11-10 18:53:10 +01002131 if (info->attrs[NL80211_ATTR_4ADDR]) {
2132 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2133 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002134 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002135 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002136 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002137 } else {
2138 params.use_4addr = -1;
2139 }
2140
Johannes Berg92ffe052008-09-16 20:39:36 +02002141 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002142 if (ntype != NL80211_IFTYPE_MONITOR)
2143 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002144 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2145 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002146 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002147 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002148
2149 flags = &_flags;
2150 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002151 }
Johannes Berg3b858752009-03-12 09:55:09 +01002152
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002153 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002154 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002155 else
2156 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002157
Johannes Berg9bc383d2009-11-19 11:55:19 +01002158 if (!err && params.use_4addr != -1)
2159 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2160
Johannes Berg55682962007-09-20 13:09:35 -04002161 return err;
2162}
2163
2164static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2165{
Johannes Berg4c476992010-10-04 21:36:35 +02002166 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002167 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002168 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002169 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002170 int err;
2171 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002172 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002173
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002174 memset(&params, 0, sizeof(params));
2175
Johannes Berg55682962007-09-20 13:09:35 -04002176 if (!info->attrs[NL80211_ATTR_IFNAME])
2177 return -EINVAL;
2178
2179 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2180 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2181 if (type > NL80211_IFTYPE_MAX)
2182 return -EINVAL;
2183 }
2184
Johannes Berg79c97e92009-07-07 03:56:12 +02002185 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002186 !(rdev->wiphy.interface_modes & (1 << type)))
2187 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002188
Arend van Spriel1c18f142013-01-08 10:17:27 +01002189 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2190 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2191 ETH_ALEN);
2192 if (!is_valid_ether_addr(params.macaddr))
2193 return -EADDRNOTAVAIL;
2194 }
2195
Johannes Berg9bc383d2009-11-19 11:55:19 +01002196 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002197 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002198 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002199 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002200 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002201 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002202
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002203 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2204 if (!msg)
2205 return -ENOMEM;
2206
Michael Wu66f7ac52008-01-31 19:48:22 +01002207 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2208 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2209 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002210 wdev = rdev_add_virtual_intf(rdev,
2211 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2212 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002213 if (IS_ERR(wdev)) {
2214 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002215 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002216 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002217
Johannes Berg98104fde2012-06-16 00:19:54 +02002218 switch (type) {
2219 case NL80211_IFTYPE_MESH_POINT:
2220 if (!info->attrs[NL80211_ATTR_MESH_ID])
2221 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002222 wdev_lock(wdev);
2223 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2224 IEEE80211_MAX_MESH_ID_LEN);
2225 wdev->mesh_id_up_len =
2226 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2227 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2228 wdev->mesh_id_up_len);
2229 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002230 break;
2231 case NL80211_IFTYPE_P2P_DEVICE:
2232 /*
2233 * P2P Device doesn't have a netdev, so doesn't go
2234 * through the netdev notifier and must be added here
2235 */
2236 mutex_init(&wdev->mtx);
2237 INIT_LIST_HEAD(&wdev->event_list);
2238 spin_lock_init(&wdev->event_lock);
2239 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2240 spin_lock_init(&wdev->mgmt_registrations_lock);
2241
2242 mutex_lock(&rdev->devlist_mtx);
2243 wdev->identifier = ++rdev->wdev_id;
2244 list_add_rcu(&wdev->list, &rdev->wdev_list);
2245 rdev->devlist_generation++;
2246 mutex_unlock(&rdev->devlist_mtx);
2247 break;
2248 default:
2249 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002250 }
2251
Eric W. Biederman15e47302012-09-07 20:12:54 +00002252 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002253 rdev, wdev) < 0) {
2254 nlmsg_free(msg);
2255 return -ENOBUFS;
2256 }
2257
2258 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002259}
2260
2261static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2262{
Johannes Berg4c476992010-10-04 21:36:35 +02002263 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002264 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002265
Johannes Berg4c476992010-10-04 21:36:35 +02002266 if (!rdev->ops->del_virtual_intf)
2267 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002268
Johannes Berg84efbb82012-06-16 00:00:26 +02002269 /*
2270 * If we remove a wireless device without a netdev then clear
2271 * user_ptr[1] so that nl80211_post_doit won't dereference it
2272 * to check if it needs to do dev_put(). Otherwise it crashes
2273 * since the wdev has been freed, unlike with a netdev where
2274 * we need the dev_put() for the netdev to really be freed.
2275 */
2276 if (!wdev->netdev)
2277 info->user_ptr[1] = NULL;
2278
Hila Gonene35e4d22012-06-27 17:19:42 +03002279 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002280}
2281
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002282static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2283{
2284 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2285 struct net_device *dev = info->user_ptr[1];
2286 u16 noack_map;
2287
2288 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2289 return -EINVAL;
2290
2291 if (!rdev->ops->set_noack_map)
2292 return -EOPNOTSUPP;
2293
2294 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2295
Hila Gonene35e4d22012-06-27 17:19:42 +03002296 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002297}
2298
Johannes Berg41ade002007-12-19 02:03:29 +01002299struct get_key_cookie {
2300 struct sk_buff *msg;
2301 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002302 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002303};
2304
2305static void get_key_callback(void *c, struct key_params *params)
2306{
Johannes Bergb9454e82009-07-08 13:29:08 +02002307 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002308 struct get_key_cookie *cookie = c;
2309
David S. Miller9360ffd2012-03-29 04:41:26 -04002310 if ((params->key &&
2311 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2312 params->key_len, params->key)) ||
2313 (params->seq &&
2314 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2315 params->seq_len, params->seq)) ||
2316 (params->cipher &&
2317 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2318 params->cipher)))
2319 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002320
Johannes Bergb9454e82009-07-08 13:29:08 +02002321 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2322 if (!key)
2323 goto nla_put_failure;
2324
David S. Miller9360ffd2012-03-29 04:41:26 -04002325 if ((params->key &&
2326 nla_put(cookie->msg, NL80211_KEY_DATA,
2327 params->key_len, params->key)) ||
2328 (params->seq &&
2329 nla_put(cookie->msg, NL80211_KEY_SEQ,
2330 params->seq_len, params->seq)) ||
2331 (params->cipher &&
2332 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2333 params->cipher)))
2334 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002335
David S. Miller9360ffd2012-03-29 04:41:26 -04002336 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2337 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002338
2339 nla_nest_end(cookie->msg, key);
2340
Johannes Berg41ade002007-12-19 02:03:29 +01002341 return;
2342 nla_put_failure:
2343 cookie->error = 1;
2344}
2345
2346static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2347{
Johannes Berg4c476992010-10-04 21:36:35 +02002348 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002349 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002350 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002351 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002352 const u8 *mac_addr = NULL;
2353 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002354 struct get_key_cookie cookie = {
2355 .error = 0,
2356 };
2357 void *hdr;
2358 struct sk_buff *msg;
2359
2360 if (info->attrs[NL80211_ATTR_KEY_IDX])
2361 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2362
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002363 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002364 return -EINVAL;
2365
2366 if (info->attrs[NL80211_ATTR_MAC])
2367 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2368
Johannes Berge31b8212010-10-05 19:39:30 +02002369 pairwise = !!mac_addr;
2370 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2371 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2372 if (kt >= NUM_NL80211_KEYTYPES)
2373 return -EINVAL;
2374 if (kt != NL80211_KEYTYPE_GROUP &&
2375 kt != NL80211_KEYTYPE_PAIRWISE)
2376 return -EINVAL;
2377 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2378 }
2379
Johannes Berg4c476992010-10-04 21:36:35 +02002380 if (!rdev->ops->get_key)
2381 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002382
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002383 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002384 if (!msg)
2385 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002386
Eric W. Biederman15e47302012-09-07 20:12:54 +00002387 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002388 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002389 if (IS_ERR(hdr))
2390 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002391
2392 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002393 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002394
David S. Miller9360ffd2012-03-29 04:41:26 -04002395 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2396 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2397 goto nla_put_failure;
2398 if (mac_addr &&
2399 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2400 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002401
Johannes Berge31b8212010-10-05 19:39:30 +02002402 if (pairwise && mac_addr &&
2403 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2404 return -ENOENT;
2405
Hila Gonene35e4d22012-06-27 17:19:42 +03002406 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2407 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002408
2409 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002410 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002411
2412 if (cookie.error)
2413 goto nla_put_failure;
2414
2415 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002416 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002417
2418 nla_put_failure:
2419 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002420 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002421 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002422 return err;
2423}
2424
2425static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2426{
Johannes Berg4c476992010-10-04 21:36:35 +02002427 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002428 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002429 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002430 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002431
Johannes Bergb9454e82009-07-08 13:29:08 +02002432 err = nl80211_parse_key(info, &key);
2433 if (err)
2434 return err;
2435
2436 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002437 return -EINVAL;
2438
Johannes Bergb9454e82009-07-08 13:29:08 +02002439 /* only support setting default key */
2440 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002441 return -EINVAL;
2442
Johannes Bergfffd0932009-07-08 14:22:54 +02002443 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002444
2445 if (key.def) {
2446 if (!rdev->ops->set_default_key) {
2447 err = -EOPNOTSUPP;
2448 goto out;
2449 }
2450
2451 err = nl80211_key_allowed(dev->ieee80211_ptr);
2452 if (err)
2453 goto out;
2454
Hila Gonene35e4d22012-06-27 17:19:42 +03002455 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002456 key.def_uni, key.def_multi);
2457
2458 if (err)
2459 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002460
Johannes Berg3d23e342009-09-29 23:27:28 +02002461#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002462 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002463#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002464 } else {
2465 if (key.def_uni || !key.def_multi) {
2466 err = -EINVAL;
2467 goto out;
2468 }
2469
2470 if (!rdev->ops->set_default_mgmt_key) {
2471 err = -EOPNOTSUPP;
2472 goto out;
2473 }
2474
2475 err = nl80211_key_allowed(dev->ieee80211_ptr);
2476 if (err)
2477 goto out;
2478
Hila Gonene35e4d22012-06-27 17:19:42 +03002479 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002480 if (err)
2481 goto out;
2482
2483#ifdef CONFIG_CFG80211_WEXT
2484 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2485#endif
2486 }
2487
2488 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002489 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002490
Johannes Berg41ade002007-12-19 02:03:29 +01002491 return err;
2492}
2493
2494static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2495{
Johannes Berg4c476992010-10-04 21:36:35 +02002496 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002497 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002498 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002499 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002500 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002501
Johannes Bergb9454e82009-07-08 13:29:08 +02002502 err = nl80211_parse_key(info, &key);
2503 if (err)
2504 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002505
Johannes Bergb9454e82009-07-08 13:29:08 +02002506 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002507 return -EINVAL;
2508
Johannes Berg41ade002007-12-19 02:03:29 +01002509 if (info->attrs[NL80211_ATTR_MAC])
2510 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2511
Johannes Berge31b8212010-10-05 19:39:30 +02002512 if (key.type == -1) {
2513 if (mac_addr)
2514 key.type = NL80211_KEYTYPE_PAIRWISE;
2515 else
2516 key.type = NL80211_KEYTYPE_GROUP;
2517 }
2518
2519 /* for now */
2520 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2521 key.type != NL80211_KEYTYPE_GROUP)
2522 return -EINVAL;
2523
Johannes Berg4c476992010-10-04 21:36:35 +02002524 if (!rdev->ops->add_key)
2525 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002526
Johannes Berge31b8212010-10-05 19:39:30 +02002527 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2528 key.type == NL80211_KEYTYPE_PAIRWISE,
2529 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002530 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002531
2532 wdev_lock(dev->ieee80211_ptr);
2533 err = nl80211_key_allowed(dev->ieee80211_ptr);
2534 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002535 err = rdev_add_key(rdev, dev, key.idx,
2536 key.type == NL80211_KEYTYPE_PAIRWISE,
2537 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002538 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002539
Johannes Berg41ade002007-12-19 02:03:29 +01002540 return err;
2541}
2542
2543static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2544{
Johannes Berg4c476992010-10-04 21:36:35 +02002545 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002546 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002547 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002548 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002549 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002550
Johannes Bergb9454e82009-07-08 13:29:08 +02002551 err = nl80211_parse_key(info, &key);
2552 if (err)
2553 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002554
2555 if (info->attrs[NL80211_ATTR_MAC])
2556 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2557
Johannes Berge31b8212010-10-05 19:39:30 +02002558 if (key.type == -1) {
2559 if (mac_addr)
2560 key.type = NL80211_KEYTYPE_PAIRWISE;
2561 else
2562 key.type = NL80211_KEYTYPE_GROUP;
2563 }
2564
2565 /* for now */
2566 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2567 key.type != NL80211_KEYTYPE_GROUP)
2568 return -EINVAL;
2569
Johannes Berg4c476992010-10-04 21:36:35 +02002570 if (!rdev->ops->del_key)
2571 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002572
Johannes Bergfffd0932009-07-08 14:22:54 +02002573 wdev_lock(dev->ieee80211_ptr);
2574 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002575
2576 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2577 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2578 err = -ENOENT;
2579
Johannes Bergfffd0932009-07-08 14:22:54 +02002580 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002581 err = rdev_del_key(rdev, dev, key.idx,
2582 key.type == NL80211_KEYTYPE_PAIRWISE,
2583 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002584
Johannes Berg3d23e342009-09-29 23:27:28 +02002585#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002586 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002587 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002588 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002589 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002590 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2591 }
2592#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002593 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002594
Johannes Berg41ade002007-12-19 02:03:29 +01002595 return err;
2596}
2597
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302598/* This function returns an error or the number of nested attributes */
2599static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2600{
2601 struct nlattr *attr;
2602 int n_entries = 0, tmp;
2603
2604 nla_for_each_nested(attr, nl_attr, tmp) {
2605 if (nla_len(attr) != ETH_ALEN)
2606 return -EINVAL;
2607
2608 n_entries++;
2609 }
2610
2611 return n_entries;
2612}
2613
2614/*
2615 * This function parses ACL information and allocates memory for ACL data.
2616 * On successful return, the calling function is responsible to free the
2617 * ACL buffer returned by this function.
2618 */
2619static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2620 struct genl_info *info)
2621{
2622 enum nl80211_acl_policy acl_policy;
2623 struct nlattr *attr;
2624 struct cfg80211_acl_data *acl;
2625 int i = 0, n_entries, tmp;
2626
2627 if (!wiphy->max_acl_mac_addrs)
2628 return ERR_PTR(-EOPNOTSUPP);
2629
2630 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2631 return ERR_PTR(-EINVAL);
2632
2633 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2634 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2635 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2636 return ERR_PTR(-EINVAL);
2637
2638 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2639 return ERR_PTR(-EINVAL);
2640
2641 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2642 if (n_entries < 0)
2643 return ERR_PTR(n_entries);
2644
2645 if (n_entries > wiphy->max_acl_mac_addrs)
2646 return ERR_PTR(-ENOTSUPP);
2647
2648 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2649 GFP_KERNEL);
2650 if (!acl)
2651 return ERR_PTR(-ENOMEM);
2652
2653 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2654 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2655 i++;
2656 }
2657
2658 acl->n_acl_entries = n_entries;
2659 acl->acl_policy = acl_policy;
2660
2661 return acl;
2662}
2663
2664static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2665{
2666 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2667 struct net_device *dev = info->user_ptr[1];
2668 struct cfg80211_acl_data *acl;
2669 int err;
2670
2671 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2672 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2673 return -EOPNOTSUPP;
2674
2675 if (!dev->ieee80211_ptr->beacon_interval)
2676 return -EINVAL;
2677
2678 acl = parse_acl_data(&rdev->wiphy, info);
2679 if (IS_ERR(acl))
2680 return PTR_ERR(acl);
2681
2682 err = rdev_set_mac_acl(rdev, dev, acl);
2683
2684 kfree(acl);
2685
2686 return err;
2687}
2688
Johannes Berg88600202012-02-13 15:17:18 +01002689static int nl80211_parse_beacon(struct genl_info *info,
2690 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002691{
Johannes Berg88600202012-02-13 15:17:18 +01002692 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002693
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002694 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2695 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2696 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2697 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002698 return -EINVAL;
2699
Johannes Berg88600202012-02-13 15:17:18 +01002700 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002701
Johannes Berged1b6cc2007-12-19 02:03:32 +01002702 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002703 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2704 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2705 if (!bcn->head_len)
2706 return -EINVAL;
2707 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002708 }
2709
2710 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002711 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2712 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002713 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002714 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002715 }
2716
Johannes Berg4c476992010-10-04 21:36:35 +02002717 if (!haveinfo)
2718 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002719
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002720 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002721 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2722 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002723 }
2724
2725 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002726 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002727 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002728 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002729 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2730 }
2731
2732 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002733 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002734 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002735 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002736 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2737 }
2738
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002739 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002740 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002741 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002742 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002743 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2744 }
2745
Johannes Berg88600202012-02-13 15:17:18 +01002746 return 0;
2747}
2748
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002749static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2750 struct cfg80211_ap_settings *params)
2751{
2752 struct wireless_dev *wdev;
2753 bool ret = false;
2754
2755 mutex_lock(&rdev->devlist_mtx);
2756
Johannes Berg89a54e42012-06-15 14:33:17 +02002757 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002758 if (wdev->iftype != NL80211_IFTYPE_AP &&
2759 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2760 continue;
2761
Johannes Berg683b6d32012-11-08 21:25:48 +01002762 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002763 continue;
2764
Johannes Berg683b6d32012-11-08 21:25:48 +01002765 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002766 ret = true;
2767 break;
2768 }
2769
2770 mutex_unlock(&rdev->devlist_mtx);
2771
2772 return ret;
2773}
2774
Jouni Malinene39e5b52012-09-30 19:29:39 +03002775static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2776 enum nl80211_auth_type auth_type,
2777 enum nl80211_commands cmd)
2778{
2779 if (auth_type > NL80211_AUTHTYPE_MAX)
2780 return false;
2781
2782 switch (cmd) {
2783 case NL80211_CMD_AUTHENTICATE:
2784 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2785 auth_type == NL80211_AUTHTYPE_SAE)
2786 return false;
2787 return true;
2788 case NL80211_CMD_CONNECT:
2789 case NL80211_CMD_START_AP:
2790 /* SAE not supported yet */
2791 if (auth_type == NL80211_AUTHTYPE_SAE)
2792 return false;
2793 return true;
2794 default:
2795 return false;
2796 }
2797}
2798
Johannes Berg88600202012-02-13 15:17:18 +01002799static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2800{
2801 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2802 struct net_device *dev = info->user_ptr[1];
2803 struct wireless_dev *wdev = dev->ieee80211_ptr;
2804 struct cfg80211_ap_settings params;
2805 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01002806 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01002807
2808 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2809 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2810 return -EOPNOTSUPP;
2811
2812 if (!rdev->ops->start_ap)
2813 return -EOPNOTSUPP;
2814
2815 if (wdev->beacon_interval)
2816 return -EALREADY;
2817
2818 memset(&params, 0, sizeof(params));
2819
2820 /* these are required for START_AP */
2821 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2822 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2823 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2824 return -EINVAL;
2825
2826 err = nl80211_parse_beacon(info, &params.beacon);
2827 if (err)
2828 return err;
2829
2830 params.beacon_interval =
2831 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2832 params.dtim_period =
2833 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2834
2835 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2836 if (err)
2837 return err;
2838
2839 /*
2840 * In theory, some of these attributes should be required here
2841 * but since they were not used when the command was originally
2842 * added, keep them optional for old user space programs to let
2843 * them continue to work with drivers that do not need the
2844 * additional information -- drivers must check!
2845 */
2846 if (info->attrs[NL80211_ATTR_SSID]) {
2847 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2848 params.ssid_len =
2849 nla_len(info->attrs[NL80211_ATTR_SSID]);
2850 if (params.ssid_len == 0 ||
2851 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2852 return -EINVAL;
2853 }
2854
2855 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2856 params.hidden_ssid = nla_get_u32(
2857 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2858 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2859 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2860 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2861 return -EINVAL;
2862 }
2863
2864 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2865
2866 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2867 params.auth_type = nla_get_u32(
2868 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002869 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2870 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002871 return -EINVAL;
2872 } else
2873 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2874
2875 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2876 NL80211_MAX_NR_CIPHER_SUITES);
2877 if (err)
2878 return err;
2879
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302880 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2881 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2882 return -EOPNOTSUPP;
2883 params.inactivity_timeout = nla_get_u16(
2884 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2885 }
2886
Johannes Berg53cabad2012-11-14 15:17:28 +01002887 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2888 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2889 return -EINVAL;
2890 params.p2p_ctwindow =
2891 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2892 if (params.p2p_ctwindow > 127)
2893 return -EINVAL;
2894 if (params.p2p_ctwindow != 0 &&
2895 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2896 return -EINVAL;
2897 }
2898
2899 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2900 u8 tmp;
2901
2902 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2903 return -EINVAL;
2904 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2905 if (tmp > 1)
2906 return -EINVAL;
2907 params.p2p_opp_ps = tmp;
2908 if (params.p2p_opp_ps != 0 &&
2909 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2910 return -EINVAL;
2911 }
2912
Johannes Bergaa430da2012-05-16 23:50:18 +02002913 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002914 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2915 if (err)
2916 return err;
2917 } else if (wdev->preset_chandef.chan) {
2918 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002919 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002920 return -EINVAL;
2921
Johannes Berg683b6d32012-11-08 21:25:48 +01002922 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002923 return -EINVAL;
2924
Simon Wunderlich04f39042013-02-08 18:16:19 +01002925 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
2926 if (err < 0)
2927 return err;
2928 if (err) {
2929 radar_detect_width = BIT(params.chandef.width);
2930 params.radar_required = true;
2931 }
2932
Michal Kaziore4e32452012-06-29 12:47:08 +02002933 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01002934 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
2935 params.chandef.chan,
2936 CHAN_MODE_SHARED,
2937 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02002938 mutex_unlock(&rdev->devlist_mtx);
2939
2940 if (err)
2941 return err;
2942
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302943 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
2944 params.acl = parse_acl_data(&rdev->wiphy, info);
2945 if (IS_ERR(params.acl))
2946 return PTR_ERR(params.acl);
2947 }
2948
Hila Gonene35e4d22012-06-27 17:19:42 +03002949 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002950 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002951 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002952 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002953 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002954 wdev->ssid_len = params.ssid_len;
2955 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002956 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302957
2958 kfree(params.acl);
2959
Johannes Berg56d18932011-05-09 18:41:15 +02002960 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002961}
2962
Johannes Berg88600202012-02-13 15:17:18 +01002963static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2964{
2965 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2966 struct net_device *dev = info->user_ptr[1];
2967 struct wireless_dev *wdev = dev->ieee80211_ptr;
2968 struct cfg80211_beacon_data params;
2969 int err;
2970
2971 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2972 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2973 return -EOPNOTSUPP;
2974
2975 if (!rdev->ops->change_beacon)
2976 return -EOPNOTSUPP;
2977
2978 if (!wdev->beacon_interval)
2979 return -EINVAL;
2980
2981 err = nl80211_parse_beacon(info, &params);
2982 if (err)
2983 return err;
2984
Hila Gonene35e4d22012-06-27 17:19:42 +03002985 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002986}
2987
2988static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002989{
Johannes Berg4c476992010-10-04 21:36:35 +02002990 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2991 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002992
Michal Kazior60771782012-06-29 12:46:56 +02002993 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002994}
2995
Johannes Berg5727ef12007-12-19 02:03:34 +01002996static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2997 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2998 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2999 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003000 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003001 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003002 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003003};
3004
Johannes Bergeccb8e82009-05-11 21:57:56 +03003005static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003006 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003007 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003008{
3009 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003010 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003011 int flag;
3012
Johannes Bergeccb8e82009-05-11 21:57:56 +03003013 /*
3014 * Try parsing the new attribute first so userspace
3015 * can specify both for older kernels.
3016 */
3017 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3018 if (nla) {
3019 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003020
Johannes Bergeccb8e82009-05-11 21:57:56 +03003021 sta_flags = nla_data(nla);
3022 params->sta_flags_mask = sta_flags->mask;
3023 params->sta_flags_set = sta_flags->set;
3024 if ((params->sta_flags_mask |
3025 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3026 return -EINVAL;
3027 return 0;
3028 }
3029
3030 /* if present, parse the old attribute */
3031
3032 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003033 if (!nla)
3034 return 0;
3035
3036 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3037 nla, sta_flags_policy))
3038 return -EINVAL;
3039
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003040 /*
3041 * Only allow certain flags for interface types so that
3042 * other attributes are silently ignored. Remember that
3043 * this is backward compatibility code with old userspace
3044 * and shouldn't be hit in other cases anyway.
3045 */
3046 switch (iftype) {
3047 case NL80211_IFTYPE_AP:
3048 case NL80211_IFTYPE_AP_VLAN:
3049 case NL80211_IFTYPE_P2P_GO:
3050 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3051 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3052 BIT(NL80211_STA_FLAG_WME) |
3053 BIT(NL80211_STA_FLAG_MFP);
3054 break;
3055 case NL80211_IFTYPE_P2P_CLIENT:
3056 case NL80211_IFTYPE_STATION:
3057 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3058 BIT(NL80211_STA_FLAG_TDLS_PEER);
3059 break;
3060 case NL80211_IFTYPE_MESH_POINT:
3061 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3062 BIT(NL80211_STA_FLAG_MFP) |
3063 BIT(NL80211_STA_FLAG_AUTHORIZED);
3064 default:
3065 return -EINVAL;
3066 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003067
Johannes Berg3383b5a2012-05-10 20:14:43 +02003068 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3069 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003070 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003071
Johannes Berg3383b5a2012-05-10 20:14:43 +02003072 /* no longer support new API additions in old API */
3073 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3074 return -EINVAL;
3075 }
3076 }
3077
Johannes Berg5727ef12007-12-19 02:03:34 +01003078 return 0;
3079}
3080
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003081static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3082 int attr)
3083{
3084 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003085 u32 bitrate;
3086 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003087
3088 rate = nla_nest_start(msg, attr);
3089 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003090 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003091
3092 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3093 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003094 /* report 16-bit bitrate only if we can */
3095 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003096 if (bitrate > 0 &&
3097 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3098 return false;
3099 if (bitrate_compat > 0 &&
3100 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3101 return false;
3102
3103 if (info->flags & RATE_INFO_FLAGS_MCS) {
3104 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3105 return false;
3106 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3107 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3108 return false;
3109 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3110 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3111 return false;
3112 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3113 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3114 return false;
3115 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3116 return false;
3117 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3118 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3119 return false;
3120 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3121 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3122 return false;
3123 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3124 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3125 return false;
3126 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3127 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3128 return false;
3129 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3130 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3131 return false;
3132 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003133
3134 nla_nest_end(msg, rate);
3135 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003136}
3137
Eric W. Biederman15e47302012-09-07 20:12:54 +00003138static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003139 int flags,
3140 struct cfg80211_registered_device *rdev,
3141 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003142 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003143{
3144 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003145 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003146
Eric W. Biederman15e47302012-09-07 20:12:54 +00003147 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003148 if (!hdr)
3149 return -1;
3150
David S. Miller9360ffd2012-03-29 04:41:26 -04003151 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3152 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3153 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3154 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003155
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003156 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3157 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003158 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003159 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3160 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3161 sinfo->connected_time))
3162 goto nla_put_failure;
3163 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3164 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3165 sinfo->inactive_time))
3166 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003167 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3168 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003169 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003170 (u32)sinfo->rx_bytes))
3171 goto nla_put_failure;
3172 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3173 NL80211_STA_INFO_TX_BYTES64)) &&
3174 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3175 (u32)sinfo->tx_bytes))
3176 goto nla_put_failure;
3177 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3178 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003179 sinfo->rx_bytes))
3180 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003181 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3182 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003183 sinfo->tx_bytes))
3184 goto nla_put_failure;
3185 if ((sinfo->filled & STATION_INFO_LLID) &&
3186 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3187 goto nla_put_failure;
3188 if ((sinfo->filled & STATION_INFO_PLID) &&
3189 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3190 goto nla_put_failure;
3191 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3192 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3193 sinfo->plink_state))
3194 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003195 switch (rdev->wiphy.signal_type) {
3196 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003197 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3198 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3199 sinfo->signal))
3200 goto nla_put_failure;
3201 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3202 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3203 sinfo->signal_avg))
3204 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003205 break;
3206 default:
3207 break;
3208 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003209 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003210 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3211 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003212 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003213 }
3214 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3215 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3216 NL80211_STA_INFO_RX_BITRATE))
3217 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003218 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003219 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3220 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3221 sinfo->rx_packets))
3222 goto nla_put_failure;
3223 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3224 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3225 sinfo->tx_packets))
3226 goto nla_put_failure;
3227 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3228 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3229 sinfo->tx_retries))
3230 goto nla_put_failure;
3231 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3232 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3233 sinfo->tx_failed))
3234 goto nla_put_failure;
3235 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3236 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3237 sinfo->beacon_loss_count))
3238 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003239 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3240 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3241 sinfo->local_pm))
3242 goto nla_put_failure;
3243 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3244 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3245 sinfo->peer_pm))
3246 goto nla_put_failure;
3247 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3248 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3249 sinfo->nonpeer_pm))
3250 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003251 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3252 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3253 if (!bss_param)
3254 goto nla_put_failure;
3255
David S. Miller9360ffd2012-03-29 04:41:26 -04003256 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3257 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3258 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3259 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3260 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3261 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3262 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3263 sinfo->bss_param.dtim_period) ||
3264 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3265 sinfo->bss_param.beacon_interval))
3266 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003267
3268 nla_nest_end(msg, bss_param);
3269 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003270 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3271 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3272 sizeof(struct nl80211_sta_flag_update),
3273 &sinfo->sta_flags))
3274 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003275 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3276 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3277 sinfo->t_offset))
3278 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003279 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003280
David S. Miller9360ffd2012-03-29 04:41:26 -04003281 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3282 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3283 sinfo->assoc_req_ies))
3284 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003285
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003286 return genlmsg_end(msg, hdr);
3287
3288 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003289 genlmsg_cancel(msg, hdr);
3290 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003291}
3292
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003293static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003294 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003295{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003296 struct station_info sinfo;
3297 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003298 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003299 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003300 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003301 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003302
Johannes Berg67748892010-10-04 21:14:06 +02003303 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3304 if (err)
3305 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003306
3307 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003308 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003309 goto out_err;
3310 }
3311
Johannes Bergbba95fe2008-07-29 13:22:51 +02003312 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003313 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003314 err = rdev_dump_station(dev, netdev, sta_idx,
3315 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003316 if (err == -ENOENT)
3317 break;
3318 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003319 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003320
3321 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003322 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003323 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003324 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003325 &sinfo) < 0)
3326 goto out;
3327
3328 sta_idx++;
3329 }
3330
3331
3332 out:
3333 cb->args[1] = sta_idx;
3334 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003335 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003336 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003337
3338 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003339}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003340
Johannes Berg5727ef12007-12-19 02:03:34 +01003341static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3342{
Johannes Berg4c476992010-10-04 21:36:35 +02003343 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3344 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003345 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003346 struct sk_buff *msg;
3347 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003348 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003349
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003350 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003351
3352 if (!info->attrs[NL80211_ATTR_MAC])
3353 return -EINVAL;
3354
3355 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3356
Johannes Berg4c476992010-10-04 21:36:35 +02003357 if (!rdev->ops->get_station)
3358 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003359
Hila Gonene35e4d22012-06-27 17:19:42 +03003360 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003361 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003362 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003363
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003364 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003365 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003366 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003367
Eric W. Biederman15e47302012-09-07 20:12:54 +00003368 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003369 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003370 nlmsg_free(msg);
3371 return -ENOBUFS;
3372 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003373
Johannes Berg4c476992010-10-04 21:36:35 +02003374 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003375}
3376
3377/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003378 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003379 */
Johannes Berg80b99892011-11-18 16:23:01 +01003380static struct net_device *get_vlan(struct genl_info *info,
3381 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003382{
Johannes Berg463d0182009-07-14 00:33:35 +02003383 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003384 struct net_device *v;
3385 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003386
Johannes Berg80b99892011-11-18 16:23:01 +01003387 if (!vlanattr)
3388 return NULL;
3389
3390 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3391 if (!v)
3392 return ERR_PTR(-ENODEV);
3393
3394 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3395 ret = -EINVAL;
3396 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003397 }
Johannes Berg80b99892011-11-18 16:23:01 +01003398
3399 if (!netif_running(v)) {
3400 ret = -ENETDOWN;
3401 goto error;
3402 }
3403
3404 return v;
3405 error:
3406 dev_put(v);
3407 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003408}
3409
3410static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3411{
Johannes Berg4c476992010-10-04 21:36:35 +02003412 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003413 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003414 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003415 struct station_parameters params;
3416 u8 *mac_addr = NULL;
3417
3418 memset(&params, 0, sizeof(params));
3419
3420 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003421 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003422
3423 if (info->attrs[NL80211_ATTR_STA_AID])
3424 return -EINVAL;
3425
3426 if (!info->attrs[NL80211_ATTR_MAC])
3427 return -EINVAL;
3428
3429 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3430
3431 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3432 params.supported_rates =
3433 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3434 params.supported_rates_len =
3435 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3436 }
3437
Johannes Bergba23d202012-12-27 17:32:09 +01003438 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL] ||
3439 info->attrs[NL80211_ATTR_HT_CAPABILITY])
3440 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003441
Johannes Bergbdd90d52011-12-14 12:20:27 +01003442 if (!rdev->ops->change_station)
3443 return -EOPNOTSUPP;
3444
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003445 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003446 return -EINVAL;
3447
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003448 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3449 params.plink_action =
3450 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3451
Javier Cardona9c3990a2011-05-03 16:57:11 -07003452 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3453 params.plink_state =
3454 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3455
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003456 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3457 enum nl80211_mesh_power_mode pm = nla_get_u32(
3458 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3459
3460 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3461 pm > NL80211_MESH_POWER_MAX)
3462 return -EINVAL;
3463
3464 params.local_pm = pm;
3465 }
3466
Johannes Berga97f4422009-06-18 17:23:43 +02003467 switch (dev->ieee80211_ptr->iftype) {
3468 case NL80211_IFTYPE_AP:
3469 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003470 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003471 /* disallow mesh-specific things */
3472 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003473 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003474 if (params.local_pm)
3475 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003476
3477 /* TDLS can't be set, ... */
3478 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3479 return -EINVAL;
3480 /*
3481 * ... but don't bother the driver with it. This works around
3482 * a hostapd/wpa_supplicant issue -- it always includes the
3483 * TLDS_PEER flag in the mask even for AP mode.
3484 */
3485 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3486
3487 /* accept only the listed bits */
3488 if (params.sta_flags_mask &
3489 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003490 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3491 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003492 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3493 BIT(NL80211_STA_FLAG_WME) |
3494 BIT(NL80211_STA_FLAG_MFP)))
3495 return -EINVAL;
3496
Johannes Bergd582cff2012-10-26 17:53:44 +02003497 /* but authenticated/associated only if driver handles it */
3498 if (!(rdev->wiphy.features &
3499 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3500 params.sta_flags_mask &
3501 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3502 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3503 return -EINVAL;
3504
Johannes Bergba23d202012-12-27 17:32:09 +01003505 /* reject other things that can't change */
3506 if (params.supported_rates)
3507 return -EINVAL;
3508
Johannes Bergbdd90d52011-12-14 12:20:27 +01003509 /* must be last in here for error handling */
3510 params.vlan = get_vlan(info, rdev);
3511 if (IS_ERR(params.vlan))
3512 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003513 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003514 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003515 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003516 /*
3517 * Don't allow userspace to change the TDLS_PEER flag,
3518 * but silently ignore attempts to change it since we
3519 * don't have state here to verify that it doesn't try
3520 * to change the flag.
3521 */
3522 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003523 /* fall through */
3524 case NL80211_IFTYPE_ADHOC:
3525 /* disallow things sta doesn't support */
3526 if (params.plink_action)
3527 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003528 if (params.local_pm)
3529 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003530 /* reject any changes other than AUTHORIZED */
3531 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3532 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003533 break;
3534 case NL80211_IFTYPE_MESH_POINT:
3535 /* disallow things mesh doesn't support */
3536 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003537 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003538 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003539 return -EINVAL;
3540 /*
3541 * No special handling for TDLS here -- the userspace
3542 * mesh code doesn't have this bug.
3543 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003544 if (params.sta_flags_mask &
3545 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003546 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003547 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003548 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003549 break;
3550 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003551 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003552 }
3553
Johannes Bergbdd90d52011-12-14 12:20:27 +01003554 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003555
Hila Gonene35e4d22012-06-27 17:19:42 +03003556 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003557
Johannes Berg5727ef12007-12-19 02:03:34 +01003558 if (params.vlan)
3559 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003560
Johannes Berg5727ef12007-12-19 02:03:34 +01003561 return err;
3562}
3563
Eliad Pellerc75786c2011-08-23 14:37:46 +03003564static struct nla_policy
3565nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3566 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3567 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3568};
3569
Johannes Berg5727ef12007-12-19 02:03:34 +01003570static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3571{
Johannes Berg4c476992010-10-04 21:36:35 +02003572 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003573 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003574 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003575 struct station_parameters params;
3576 u8 *mac_addr = NULL;
3577
3578 memset(&params, 0, sizeof(params));
3579
3580 if (!info->attrs[NL80211_ATTR_MAC])
3581 return -EINVAL;
3582
Johannes Berg5727ef12007-12-19 02:03:34 +01003583 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3584 return -EINVAL;
3585
3586 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3587 return -EINVAL;
3588
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003589 if (!info->attrs[NL80211_ATTR_STA_AID])
3590 return -EINVAL;
3591
Johannes Berg5727ef12007-12-19 02:03:34 +01003592 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3593 params.supported_rates =
3594 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3595 params.supported_rates_len =
3596 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3597 params.listen_interval =
3598 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003599
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003600 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3601 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3602 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003603
Jouni Malinen36aedc92008-08-25 11:58:58 +03003604 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3605 params.ht_capa =
3606 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003607
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003608 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3609 params.vht_capa =
3610 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3611
Javier Cardona96b78df2011-04-07 15:08:33 -07003612 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3613 params.plink_action =
3614 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3615
Johannes Bergbdd90d52011-12-14 12:20:27 +01003616 if (!rdev->ops->add_station)
3617 return -EOPNOTSUPP;
3618
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003619 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003620 return -EINVAL;
3621
Johannes Bergbdd90d52011-12-14 12:20:27 +01003622 switch (dev->ieee80211_ptr->iftype) {
3623 case NL80211_IFTYPE_AP:
3624 case NL80211_IFTYPE_AP_VLAN:
3625 case NL80211_IFTYPE_P2P_GO:
3626 /* parse WME attributes if sta is WME capable */
3627 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3628 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3629 info->attrs[NL80211_ATTR_STA_WME]) {
3630 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3631 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003632
Johannes Bergbdd90d52011-12-14 12:20:27 +01003633 nla = info->attrs[NL80211_ATTR_STA_WME];
3634 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3635 nl80211_sta_wme_policy);
3636 if (err)
3637 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003638
Johannes Bergbdd90d52011-12-14 12:20:27 +01003639 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3640 params.uapsd_queues =
3641 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3642 if (params.uapsd_queues &
3643 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3644 return -EINVAL;
3645
3646 if (tb[NL80211_STA_WME_MAX_SP])
3647 params.max_sp =
3648 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3649
3650 if (params.max_sp &
3651 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3652 return -EINVAL;
3653
3654 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3655 }
3656 /* TDLS peers cannot be added */
3657 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003658 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003659 /* but don't bother the driver with it */
3660 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003661
Johannes Bergd582cff2012-10-26 17:53:44 +02003662 /* allow authenticated/associated only if driver handles it */
3663 if (!(rdev->wiphy.features &
3664 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3665 params.sta_flags_mask &
3666 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3667 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3668 return -EINVAL;
3669
Johannes Bergbdd90d52011-12-14 12:20:27 +01003670 /* must be last in here for error handling */
3671 params.vlan = get_vlan(info, rdev);
3672 if (IS_ERR(params.vlan))
3673 return PTR_ERR(params.vlan);
3674 break;
3675 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergd582cff2012-10-26 17:53:44 +02003676 /* associated is disallowed */
3677 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3678 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003679 /* TDLS peers cannot be added */
3680 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003681 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003682 break;
3683 case NL80211_IFTYPE_STATION:
Johannes Bergd582cff2012-10-26 17:53:44 +02003684 /* associated is disallowed */
3685 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3686 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003687 /* Only TDLS peers can be added */
3688 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3689 return -EINVAL;
3690 /* Can only add if TDLS ... */
3691 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3692 return -EOPNOTSUPP;
3693 /* ... with external setup is supported */
3694 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3695 return -EOPNOTSUPP;
3696 break;
3697 default:
3698 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003699 }
3700
Johannes Bergbdd90d52011-12-14 12:20:27 +01003701 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003702
Hila Gonene35e4d22012-06-27 17:19:42 +03003703 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003704
Johannes Berg5727ef12007-12-19 02:03:34 +01003705 if (params.vlan)
3706 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003707 return err;
3708}
3709
3710static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3711{
Johannes Berg4c476992010-10-04 21:36:35 +02003712 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3713 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003714 u8 *mac_addr = NULL;
3715
3716 if (info->attrs[NL80211_ATTR_MAC])
3717 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3718
Johannes Berge80cf852009-05-11 14:43:13 +02003719 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003720 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003721 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003722 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3723 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003724
Johannes Berg4c476992010-10-04 21:36:35 +02003725 if (!rdev->ops->del_station)
3726 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003727
Hila Gonene35e4d22012-06-27 17:19:42 +03003728 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003729}
3730
Eric W. Biederman15e47302012-09-07 20:12:54 +00003731static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003732 int flags, struct net_device *dev,
3733 u8 *dst, u8 *next_hop,
3734 struct mpath_info *pinfo)
3735{
3736 void *hdr;
3737 struct nlattr *pinfoattr;
3738
Eric W. Biederman15e47302012-09-07 20:12:54 +00003739 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003740 if (!hdr)
3741 return -1;
3742
David S. Miller9360ffd2012-03-29 04:41:26 -04003743 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3744 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3745 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3746 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3747 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003748
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003749 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3750 if (!pinfoattr)
3751 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003752 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3753 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3754 pinfo->frame_qlen))
3755 goto nla_put_failure;
3756 if (((pinfo->filled & MPATH_INFO_SN) &&
3757 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3758 ((pinfo->filled & MPATH_INFO_METRIC) &&
3759 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3760 pinfo->metric)) ||
3761 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3762 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3763 pinfo->exptime)) ||
3764 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3765 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3766 pinfo->flags)) ||
3767 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3768 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3769 pinfo->discovery_timeout)) ||
3770 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3771 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3772 pinfo->discovery_retries)))
3773 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003774
3775 nla_nest_end(msg, pinfoattr);
3776
3777 return genlmsg_end(msg, hdr);
3778
3779 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003780 genlmsg_cancel(msg, hdr);
3781 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003782}
3783
3784static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003785 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003786{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003787 struct mpath_info pinfo;
3788 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003789 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003790 u8 dst[ETH_ALEN];
3791 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003792 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003793 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003794
Johannes Berg67748892010-10-04 21:14:06 +02003795 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3796 if (err)
3797 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003798
3799 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003800 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003801 goto out_err;
3802 }
3803
Jouni Malineneec60b02009-03-20 21:21:19 +02003804 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3805 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003806 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003807 }
3808
Johannes Bergbba95fe2008-07-29 13:22:51 +02003809 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003810 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3811 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003812 if (err == -ENOENT)
3813 break;
3814 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003815 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003816
Eric W. Biederman15e47302012-09-07 20:12:54 +00003817 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003818 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3819 netdev, dst, next_hop,
3820 &pinfo) < 0)
3821 goto out;
3822
3823 path_idx++;
3824 }
3825
3826
3827 out:
3828 cb->args[1] = path_idx;
3829 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003830 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003831 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003832 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003833}
3834
3835static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3836{
Johannes Berg4c476992010-10-04 21:36:35 +02003837 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003838 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003839 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003840 struct mpath_info pinfo;
3841 struct sk_buff *msg;
3842 u8 *dst = NULL;
3843 u8 next_hop[ETH_ALEN];
3844
3845 memset(&pinfo, 0, sizeof(pinfo));
3846
3847 if (!info->attrs[NL80211_ATTR_MAC])
3848 return -EINVAL;
3849
3850 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3851
Johannes Berg4c476992010-10-04 21:36:35 +02003852 if (!rdev->ops->get_mpath)
3853 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003854
Johannes Berg4c476992010-10-04 21:36:35 +02003855 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3856 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003857
Hila Gonene35e4d22012-06-27 17:19:42 +03003858 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003859 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003860 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003861
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003863 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003864 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003865
Eric W. Biederman15e47302012-09-07 20:12:54 +00003866 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003867 dev, dst, next_hop, &pinfo) < 0) {
3868 nlmsg_free(msg);
3869 return -ENOBUFS;
3870 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003871
Johannes Berg4c476992010-10-04 21:36:35 +02003872 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003873}
3874
3875static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3876{
Johannes Berg4c476992010-10-04 21:36:35 +02003877 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3878 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003879 u8 *dst = NULL;
3880 u8 *next_hop = NULL;
3881
3882 if (!info->attrs[NL80211_ATTR_MAC])
3883 return -EINVAL;
3884
3885 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3886 return -EINVAL;
3887
3888 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3889 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3890
Johannes Berg4c476992010-10-04 21:36:35 +02003891 if (!rdev->ops->change_mpath)
3892 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003893
Johannes Berg4c476992010-10-04 21:36:35 +02003894 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3895 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003896
Hila Gonene35e4d22012-06-27 17:19:42 +03003897 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003898}
Johannes Berg4c476992010-10-04 21:36:35 +02003899
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003900static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3901{
Johannes Berg4c476992010-10-04 21:36:35 +02003902 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3903 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003904 u8 *dst = NULL;
3905 u8 *next_hop = NULL;
3906
3907 if (!info->attrs[NL80211_ATTR_MAC])
3908 return -EINVAL;
3909
3910 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3911 return -EINVAL;
3912
3913 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3914 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3915
Johannes Berg4c476992010-10-04 21:36:35 +02003916 if (!rdev->ops->add_mpath)
3917 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003918
Johannes Berg4c476992010-10-04 21:36:35 +02003919 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3920 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003921
Hila Gonene35e4d22012-06-27 17:19:42 +03003922 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003923}
3924
3925static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3926{
Johannes Berg4c476992010-10-04 21:36:35 +02003927 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3928 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003929 u8 *dst = NULL;
3930
3931 if (info->attrs[NL80211_ATTR_MAC])
3932 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3933
Johannes Berg4c476992010-10-04 21:36:35 +02003934 if (!rdev->ops->del_mpath)
3935 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003936
Hila Gonene35e4d22012-06-27 17:19:42 +03003937 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003938}
3939
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003940static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3941{
Johannes Berg4c476992010-10-04 21:36:35 +02003942 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3943 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003944 struct bss_parameters params;
3945
3946 memset(&params, 0, sizeof(params));
3947 /* default to not changing parameters */
3948 params.use_cts_prot = -1;
3949 params.use_short_preamble = -1;
3950 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003951 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003952 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01003953 params.p2p_ctwindow = -1;
3954 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003955
3956 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3957 params.use_cts_prot =
3958 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3959 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3960 params.use_short_preamble =
3961 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3962 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3963 params.use_short_slot_time =
3964 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003965 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3966 params.basic_rates =
3967 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3968 params.basic_rates_len =
3969 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3970 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003971 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3972 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003973 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3974 params.ht_opmode =
3975 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003976
Johannes Berg53cabad2012-11-14 15:17:28 +01003977 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3978 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3979 return -EINVAL;
3980 params.p2p_ctwindow =
3981 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3982 if (params.p2p_ctwindow < 0)
3983 return -EINVAL;
3984 if (params.p2p_ctwindow != 0 &&
3985 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3986 return -EINVAL;
3987 }
3988
3989 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3990 u8 tmp;
3991
3992 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3993 return -EINVAL;
3994 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3995 if (tmp > 1)
3996 return -EINVAL;
3997 params.p2p_opp_ps = tmp;
3998 if (params.p2p_opp_ps &&
3999 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4000 return -EINVAL;
4001 }
4002
Johannes Berg4c476992010-10-04 21:36:35 +02004003 if (!rdev->ops->change_bss)
4004 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004005
Johannes Berg074ac8d2010-09-16 14:58:22 +02004006 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004007 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4008 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004009
Hila Gonene35e4d22012-06-27 17:19:42 +03004010 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004011}
4012
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004013static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004014 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4015 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4016 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4017 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4018 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4019 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4020};
4021
4022static int parse_reg_rule(struct nlattr *tb[],
4023 struct ieee80211_reg_rule *reg_rule)
4024{
4025 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4026 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4027
4028 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4029 return -EINVAL;
4030 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4031 return -EINVAL;
4032 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4033 return -EINVAL;
4034 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4035 return -EINVAL;
4036 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4037 return -EINVAL;
4038
4039 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4040
4041 freq_range->start_freq_khz =
4042 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4043 freq_range->end_freq_khz =
4044 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4045 freq_range->max_bandwidth_khz =
4046 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4047
4048 power_rule->max_eirp =
4049 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4050
4051 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4052 power_rule->max_antenna_gain =
4053 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4054
4055 return 0;
4056}
4057
4058static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4059{
4060 int r;
4061 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004062 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004063
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004064 /*
4065 * You should only get this when cfg80211 hasn't yet initialized
4066 * completely when built-in to the kernel right between the time
4067 * window between nl80211_init() and regulatory_init(), if that is
4068 * even possible.
4069 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004070 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004071 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004072
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004073 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4074 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004075
4076 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4077
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004078 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4079 user_reg_hint_type =
4080 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4081 else
4082 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4083
4084 switch (user_reg_hint_type) {
4085 case NL80211_USER_REG_HINT_USER:
4086 case NL80211_USER_REG_HINT_CELL_BASE:
4087 break;
4088 default:
4089 return -EINVAL;
4090 }
4091
4092 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004093
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004094 return r;
4095}
4096
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004097static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004098 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004099{
Johannes Berg4c476992010-10-04 21:36:35 +02004100 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004101 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004102 struct wireless_dev *wdev = dev->ieee80211_ptr;
4103 struct mesh_config cur_params;
4104 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004105 void *hdr;
4106 struct nlattr *pinfoattr;
4107 struct sk_buff *msg;
4108
Johannes Berg29cbe682010-12-03 09:20:44 +01004109 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4110 return -EOPNOTSUPP;
4111
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004112 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004113 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004114
Johannes Berg29cbe682010-12-03 09:20:44 +01004115 wdev_lock(wdev);
4116 /* If not connected, get default parameters */
4117 if (!wdev->mesh_id_len)
4118 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4119 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004120 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004121 wdev_unlock(wdev);
4122
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004123 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004124 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004125
4126 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004127 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004128 if (!msg)
4129 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004130 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004131 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004132 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004133 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004134 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004135 if (!pinfoattr)
4136 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004137 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4138 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4139 cur_params.dot11MeshRetryTimeout) ||
4140 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4141 cur_params.dot11MeshConfirmTimeout) ||
4142 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4143 cur_params.dot11MeshHoldingTimeout) ||
4144 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4145 cur_params.dot11MeshMaxPeerLinks) ||
4146 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4147 cur_params.dot11MeshMaxRetries) ||
4148 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4149 cur_params.dot11MeshTTL) ||
4150 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4151 cur_params.element_ttl) ||
4152 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4153 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004154 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4155 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004156 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4157 cur_params.dot11MeshHWMPmaxPREQretries) ||
4158 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4159 cur_params.path_refresh_time) ||
4160 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4161 cur_params.min_discovery_timeout) ||
4162 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4163 cur_params.dot11MeshHWMPactivePathTimeout) ||
4164 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4165 cur_params.dot11MeshHWMPpreqMinInterval) ||
4166 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4167 cur_params.dot11MeshHWMPperrMinInterval) ||
4168 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4169 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4170 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4171 cur_params.dot11MeshHWMPRootMode) ||
4172 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4173 cur_params.dot11MeshHWMPRannInterval) ||
4174 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4175 cur_params.dot11MeshGateAnnouncementProtocol) ||
4176 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4177 cur_params.dot11MeshForwarding) ||
4178 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004179 cur_params.rssi_threshold) ||
4180 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004181 cur_params.ht_opmode) ||
4182 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4183 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4184 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004185 cur_params.dot11MeshHWMProotInterval) ||
4186 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004187 cur_params.dot11MeshHWMPconfirmationInterval) ||
4188 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4189 cur_params.power_mode) ||
4190 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4191 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004192 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004193 nla_nest_end(msg, pinfoattr);
4194 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004195 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004196
Johannes Berg3b858752009-03-12 09:55:09 +01004197 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004198 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004199 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004200 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004201 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004202}
4203
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004204static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004205 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4206 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4207 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4208 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4209 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4210 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004211 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004212 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004213 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004214 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4215 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4216 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4217 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4218 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004219 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004220 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004221 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004222 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004223 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004224 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004225 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4226 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004227 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4228 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004229 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004230 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4231 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004232};
4233
Javier Cardonac80d5452010-12-16 17:37:49 -08004234static const struct nla_policy
4235 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004236 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004237 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4238 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004239 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004240 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004241 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004242 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004243};
4244
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004245static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004246 struct mesh_config *cfg,
4247 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004248{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004249 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004250 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004251
Marco Porschea54fba2013-01-07 16:04:48 +01004252#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4253do { \
4254 if (tb[attr]) { \
4255 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4256 return -EINVAL; \
4257 cfg->param = fn(tb[attr]); \
4258 mask |= (1 << (attr - 1)); \
4259 } \
4260} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004261
4262
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004263 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004264 return -EINVAL;
4265 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004266 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004267 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004268 return -EINVAL;
4269
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004270 /* This makes sure that there aren't more than 32 mesh config
4271 * parameters (otherwise our bitfield scheme would not work.) */
4272 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4273
4274 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004275 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004276 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4277 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004278 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004279 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4280 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004281 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004282 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4283 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004284 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004285 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4286 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004287 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004288 mask, NL80211_MESHCONF_MAX_RETRIES,
4289 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004290 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004291 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004292 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004293 mask, NL80211_MESHCONF_ELEMENT_TTL,
4294 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004295 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004296 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4297 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004298 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4299 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004300 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4301 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004302 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004303 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4304 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004305 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004306 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4307 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004308 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004309 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4310 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004311 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4312 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004313 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4314 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004315 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004316 1, 65535, mask,
4317 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004318 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004319 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004320 1, 65535, mask,
4321 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004322 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004323 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004324 dot11MeshHWMPnetDiameterTraversalTime,
4325 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004326 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4327 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004328 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4329 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4330 nla_get_u8);
4331 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4332 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004333 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004334 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004335 dot11MeshGateAnnouncementProtocol, 0, 1,
4336 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004337 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004338 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004339 mask, NL80211_MESHCONF_FORWARDING,
4340 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004341 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004342 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4343 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004344 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004345 mask, NL80211_MESHCONF_HT_OPMODE,
4346 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004347 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004348 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004349 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4350 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004351 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004352 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4353 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004354 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004355 dot11MeshHWMPconfirmationInterval,
4356 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004357 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4358 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004359 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4360 NL80211_MESH_POWER_ACTIVE,
4361 NL80211_MESH_POWER_MAX,
4362 mask, NL80211_MESHCONF_POWER_MODE,
4363 nla_get_u32);
4364 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4365 0, 65535, mask,
4366 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004367 if (mask_out)
4368 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004369
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004370 return 0;
4371
4372#undef FILL_IN_MESH_PARAM_IF_SET
4373}
4374
Javier Cardonac80d5452010-12-16 17:37:49 -08004375static int nl80211_parse_mesh_setup(struct genl_info *info,
4376 struct mesh_setup *setup)
4377{
4378 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4379
4380 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4381 return -EINVAL;
4382 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4383 info->attrs[NL80211_ATTR_MESH_SETUP],
4384 nl80211_mesh_setup_params_policy))
4385 return -EINVAL;
4386
Javier Cardonad299a1f2012-03-31 11:31:33 -07004387 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4388 setup->sync_method =
4389 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4390 IEEE80211_SYNC_METHOD_VENDOR :
4391 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4392
Javier Cardonac80d5452010-12-16 17:37:49 -08004393 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4394 setup->path_sel_proto =
4395 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4396 IEEE80211_PATH_PROTOCOL_VENDOR :
4397 IEEE80211_PATH_PROTOCOL_HWMP;
4398
4399 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4400 setup->path_metric =
4401 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4402 IEEE80211_PATH_METRIC_VENDOR :
4403 IEEE80211_PATH_METRIC_AIRTIME;
4404
Javier Cardona581a8b02011-04-07 15:08:27 -07004405
4406 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004407 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004408 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004409 if (!is_valid_ie_attr(ieattr))
4410 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004411 setup->ie = nla_data(ieattr);
4412 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004413 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004414 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4415 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004416
4417 return 0;
4418}
4419
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004420static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004421 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004422{
4423 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4424 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004425 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004426 struct mesh_config cfg;
4427 u32 mask;
4428 int err;
4429
Johannes Berg29cbe682010-12-03 09:20:44 +01004430 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4431 return -EOPNOTSUPP;
4432
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004433 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004434 return -EOPNOTSUPP;
4435
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004436 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004437 if (err)
4438 return err;
4439
Johannes Berg29cbe682010-12-03 09:20:44 +01004440 wdev_lock(wdev);
4441 if (!wdev->mesh_id_len)
4442 err = -ENOLINK;
4443
4444 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004445 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004446
4447 wdev_unlock(wdev);
4448
4449 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004450}
4451
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004452static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4453{
Johannes Berg458f4f92012-12-06 15:47:38 +01004454 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004455 struct sk_buff *msg;
4456 void *hdr = NULL;
4457 struct nlattr *nl_reg_rules;
4458 unsigned int i;
4459 int err = -EINVAL;
4460
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004461 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004462
4463 if (!cfg80211_regdomain)
4464 goto out;
4465
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004466 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004467 if (!msg) {
4468 err = -ENOBUFS;
4469 goto out;
4470 }
4471
Eric W. Biederman15e47302012-09-07 20:12:54 +00004472 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004473 NL80211_CMD_GET_REG);
4474 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004475 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004476
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004477 if (reg_last_request_cell_base() &&
4478 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4479 NL80211_USER_REG_HINT_CELL_BASE))
4480 goto nla_put_failure;
4481
Johannes Berg458f4f92012-12-06 15:47:38 +01004482 rcu_read_lock();
4483 regdom = rcu_dereference(cfg80211_regdomain);
4484
4485 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4486 (regdom->dfs_region &&
4487 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4488 goto nla_put_failure_rcu;
4489
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004490 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4491 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004492 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004493
Johannes Berg458f4f92012-12-06 15:47:38 +01004494 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004495 struct nlattr *nl_reg_rule;
4496 const struct ieee80211_reg_rule *reg_rule;
4497 const struct ieee80211_freq_range *freq_range;
4498 const struct ieee80211_power_rule *power_rule;
4499
Johannes Berg458f4f92012-12-06 15:47:38 +01004500 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004501 freq_range = &reg_rule->freq_range;
4502 power_rule = &reg_rule->power_rule;
4503
4504 nl_reg_rule = nla_nest_start(msg, i);
4505 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004506 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004507
David S. Miller9360ffd2012-03-29 04:41:26 -04004508 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4509 reg_rule->flags) ||
4510 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4511 freq_range->start_freq_khz) ||
4512 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4513 freq_range->end_freq_khz) ||
4514 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4515 freq_range->max_bandwidth_khz) ||
4516 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4517 power_rule->max_antenna_gain) ||
4518 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4519 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004520 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004521
4522 nla_nest_end(msg, nl_reg_rule);
4523 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004524 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004525
4526 nla_nest_end(msg, nl_reg_rules);
4527
4528 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004529 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004530 goto out;
4531
Johannes Berg458f4f92012-12-06 15:47:38 +01004532nla_put_failure_rcu:
4533 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004534nla_put_failure:
4535 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004536put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004537 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004538 err = -EMSGSIZE;
4539out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004540 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004541 return err;
4542}
4543
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004544static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4545{
4546 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4547 struct nlattr *nl_reg_rule;
4548 char *alpha2 = NULL;
4549 int rem_reg_rules = 0, r = 0;
4550 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004551 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004552 struct ieee80211_regdomain *rd = NULL;
4553
4554 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4555 return -EINVAL;
4556
4557 if (!info->attrs[NL80211_ATTR_REG_RULES])
4558 return -EINVAL;
4559
4560 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4561
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004562 if (info->attrs[NL80211_ATTR_DFS_REGION])
4563 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4564
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004565 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004566 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004567 num_rules++;
4568 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004569 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004570 }
4571
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004572 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004573 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004574
4575 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004576 if (!rd)
4577 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004578
4579 rd->n_reg_rules = num_rules;
4580 rd->alpha2[0] = alpha2[0];
4581 rd->alpha2[1] = alpha2[1];
4582
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004583 /*
4584 * Disable DFS master mode if the DFS region was
4585 * not supported or known on this kernel.
4586 */
4587 if (reg_supported_dfs_region(dfs_region))
4588 rd->dfs_region = dfs_region;
4589
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004590 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004591 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004592 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004593 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4594 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004595 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4596 if (r)
4597 goto bad_reg;
4598
4599 rule_idx++;
4600
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004601 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4602 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004603 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004604 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004605 }
4606
Johannes Berg6913b492012-12-04 00:48:59 +01004607 mutex_lock(&cfg80211_mutex);
4608
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004609 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004610 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004611 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004612 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004613
Johannes Bergd2372b32008-10-24 20:32:20 +02004614 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004615 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004616 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004617}
4618
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004619static int validate_scan_freqs(struct nlattr *freqs)
4620{
4621 struct nlattr *attr1, *attr2;
4622 int n_channels = 0, tmp1, tmp2;
4623
4624 nla_for_each_nested(attr1, freqs, tmp1) {
4625 n_channels++;
4626 /*
4627 * Some hardware has a limited channel list for
4628 * scanning, and it is pretty much nonsensical
4629 * to scan for a channel twice, so disallow that
4630 * and don't require drivers to check that the
4631 * channel list they get isn't longer than what
4632 * they can scan, as long as they can scan all
4633 * the channels they registered at once.
4634 */
4635 nla_for_each_nested(attr2, freqs, tmp2)
4636 if (attr1 != attr2 &&
4637 nla_get_u32(attr1) == nla_get_u32(attr2))
4638 return 0;
4639 }
4640
4641 return n_channels;
4642}
4643
Johannes Berg2a519312009-02-10 21:25:55 +01004644static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4645{
Johannes Berg4c476992010-10-04 21:36:35 +02004646 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004647 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004648 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004649 struct nlattr *attr;
4650 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004651 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004652 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004653
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004654 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4655 return -EINVAL;
4656
Johannes Berg79c97e92009-07-07 03:56:12 +02004657 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004658
Johannes Berg4c476992010-10-04 21:36:35 +02004659 if (!rdev->ops->scan)
4660 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004661
Johannes Berg4c476992010-10-04 21:36:35 +02004662 if (rdev->scan_req)
4663 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004664
4665 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004666 n_channels = validate_scan_freqs(
4667 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004668 if (!n_channels)
4669 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004670 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004671 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004672 n_channels = 0;
4673
Johannes Berg2a519312009-02-10 21:25:55 +01004674 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4675 if (wiphy->bands[band])
4676 n_channels += wiphy->bands[band]->n_channels;
4677 }
4678
4679 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4680 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4681 n_ssids++;
4682
Johannes Berg4c476992010-10-04 21:36:35 +02004683 if (n_ssids > wiphy->max_scan_ssids)
4684 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004685
Jouni Malinen70692ad2009-02-16 19:39:13 +02004686 if (info->attrs[NL80211_ATTR_IE])
4687 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4688 else
4689 ie_len = 0;
4690
Johannes Berg4c476992010-10-04 21:36:35 +02004691 if (ie_len > wiphy->max_scan_ie_len)
4692 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004693
Johannes Berg2a519312009-02-10 21:25:55 +01004694 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004695 + sizeof(*request->ssids) * n_ssids
4696 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004697 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004698 if (!request)
4699 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004700
Johannes Berg2a519312009-02-10 21:25:55 +01004701 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004702 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004703 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004704 if (ie_len) {
4705 if (request->ssids)
4706 request->ie = (void *)(request->ssids + n_ssids);
4707 else
4708 request->ie = (void *)(request->channels + n_channels);
4709 }
Johannes Berg2a519312009-02-10 21:25:55 +01004710
Johannes Berg584991d2009-11-02 13:32:03 +01004711 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004712 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4713 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004714 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004715 struct ieee80211_channel *chan;
4716
4717 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4718
4719 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004720 err = -EINVAL;
4721 goto out_free;
4722 }
Johannes Berg584991d2009-11-02 13:32:03 +01004723
4724 /* ignore disabled channels */
4725 if (chan->flags & IEEE80211_CHAN_DISABLED)
4726 continue;
4727
4728 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004729 i++;
4730 }
4731 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004732 enum ieee80211_band band;
4733
Johannes Berg2a519312009-02-10 21:25:55 +01004734 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004735 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4736 int j;
4737 if (!wiphy->bands[band])
4738 continue;
4739 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004740 struct ieee80211_channel *chan;
4741
4742 chan = &wiphy->bands[band]->channels[j];
4743
4744 if (chan->flags & IEEE80211_CHAN_DISABLED)
4745 continue;
4746
4747 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004748 i++;
4749 }
4750 }
4751 }
4752
Johannes Berg584991d2009-11-02 13:32:03 +01004753 if (!i) {
4754 err = -EINVAL;
4755 goto out_free;
4756 }
4757
4758 request->n_channels = i;
4759
Johannes Berg2a519312009-02-10 21:25:55 +01004760 i = 0;
4761 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4762 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004763 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004764 err = -EINVAL;
4765 goto out_free;
4766 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004767 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004768 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004769 i++;
4770 }
4771 }
4772
Jouni Malinen70692ad2009-02-16 19:39:13 +02004773 if (info->attrs[NL80211_ATTR_IE]) {
4774 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004775 memcpy((void *)request->ie,
4776 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004777 request->ie_len);
4778 }
4779
Johannes Berg34850ab2011-07-18 18:08:35 +02004780 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004781 if (wiphy->bands[i])
4782 request->rates[i] =
4783 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004784
4785 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4786 nla_for_each_nested(attr,
4787 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4788 tmp) {
4789 enum ieee80211_band band = nla_type(attr);
4790
Dan Carpenter84404622011-07-29 11:52:18 +03004791 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004792 err = -EINVAL;
4793 goto out_free;
4794 }
4795 err = ieee80211_get_ratemask(wiphy->bands[band],
4796 nla_data(attr),
4797 nla_len(attr),
4798 &request->rates[band]);
4799 if (err)
4800 goto out_free;
4801 }
4802 }
4803
Sam Leffler46856bb2012-10-11 21:03:32 -07004804 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004805 request->flags = nla_get_u32(
4806 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004807 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4808 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4809 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4810 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004811 err = -EOPNOTSUPP;
4812 goto out_free;
4813 }
4814 }
Sam Lefflered4737712012-10-11 21:03:31 -07004815
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304816 request->no_cck =
4817 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4818
Johannes Bergfd014282012-06-18 19:17:03 +02004819 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004820 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004821 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004822
Johannes Berg79c97e92009-07-07 03:56:12 +02004823 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004824 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004825
Johannes Berg463d0182009-07-14 00:33:35 +02004826 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004827 nl80211_send_scan_start(rdev, wdev);
4828 if (wdev->netdev)
4829 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004830 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004831 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004832 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004833 kfree(request);
4834 }
Johannes Berg3b858752009-03-12 09:55:09 +01004835
Johannes Berg2a519312009-02-10 21:25:55 +01004836 return err;
4837}
4838
Luciano Coelho807f8a82011-05-11 17:09:35 +03004839static int nl80211_start_sched_scan(struct sk_buff *skb,
4840 struct genl_info *info)
4841{
4842 struct cfg80211_sched_scan_request *request;
4843 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4844 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004845 struct nlattr *attr;
4846 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004847 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004848 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004849 enum ieee80211_band band;
4850 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004851 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004852
4853 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4854 !rdev->ops->sched_scan_start)
4855 return -EOPNOTSUPP;
4856
4857 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4858 return -EINVAL;
4859
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004860 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4861 return -EINVAL;
4862
4863 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4864 if (interval == 0)
4865 return -EINVAL;
4866
Luciano Coelho807f8a82011-05-11 17:09:35 +03004867 wiphy = &rdev->wiphy;
4868
4869 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4870 n_channels = validate_scan_freqs(
4871 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4872 if (!n_channels)
4873 return -EINVAL;
4874 } else {
4875 n_channels = 0;
4876
4877 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4878 if (wiphy->bands[band])
4879 n_channels += wiphy->bands[band]->n_channels;
4880 }
4881
4882 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4883 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4884 tmp)
4885 n_ssids++;
4886
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004887 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004888 return -EINVAL;
4889
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004890 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4891 nla_for_each_nested(attr,
4892 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4893 tmp)
4894 n_match_sets++;
4895
4896 if (n_match_sets > wiphy->max_match_sets)
4897 return -EINVAL;
4898
Luciano Coelho807f8a82011-05-11 17:09:35 +03004899 if (info->attrs[NL80211_ATTR_IE])
4900 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4901 else
4902 ie_len = 0;
4903
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004904 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004905 return -EINVAL;
4906
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004907 mutex_lock(&rdev->sched_scan_mtx);
4908
4909 if (rdev->sched_scan_req) {
4910 err = -EINPROGRESS;
4911 goto out;
4912 }
4913
Luciano Coelho807f8a82011-05-11 17:09:35 +03004914 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004915 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004916 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004917 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004918 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004919 if (!request) {
4920 err = -ENOMEM;
4921 goto out;
4922 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004923
4924 if (n_ssids)
4925 request->ssids = (void *)&request->channels[n_channels];
4926 request->n_ssids = n_ssids;
4927 if (ie_len) {
4928 if (request->ssids)
4929 request->ie = (void *)(request->ssids + n_ssids);
4930 else
4931 request->ie = (void *)(request->channels + n_channels);
4932 }
4933
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004934 if (n_match_sets) {
4935 if (request->ie)
4936 request->match_sets = (void *)(request->ie + ie_len);
4937 else if (request->ssids)
4938 request->match_sets =
4939 (void *)(request->ssids + n_ssids);
4940 else
4941 request->match_sets =
4942 (void *)(request->channels + n_channels);
4943 }
4944 request->n_match_sets = n_match_sets;
4945
Luciano Coelho807f8a82011-05-11 17:09:35 +03004946 i = 0;
4947 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4948 /* user specified, bail out if channel not found */
4949 nla_for_each_nested(attr,
4950 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4951 tmp) {
4952 struct ieee80211_channel *chan;
4953
4954 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4955
4956 if (!chan) {
4957 err = -EINVAL;
4958 goto out_free;
4959 }
4960
4961 /* ignore disabled channels */
4962 if (chan->flags & IEEE80211_CHAN_DISABLED)
4963 continue;
4964
4965 request->channels[i] = chan;
4966 i++;
4967 }
4968 } else {
4969 /* all channels */
4970 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4971 int j;
4972 if (!wiphy->bands[band])
4973 continue;
4974 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4975 struct ieee80211_channel *chan;
4976
4977 chan = &wiphy->bands[band]->channels[j];
4978
4979 if (chan->flags & IEEE80211_CHAN_DISABLED)
4980 continue;
4981
4982 request->channels[i] = chan;
4983 i++;
4984 }
4985 }
4986 }
4987
4988 if (!i) {
4989 err = -EINVAL;
4990 goto out_free;
4991 }
4992
4993 request->n_channels = i;
4994
4995 i = 0;
4996 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4997 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4998 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004999 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005000 err = -EINVAL;
5001 goto out_free;
5002 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005003 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005004 memcpy(request->ssids[i].ssid, nla_data(attr),
5005 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005006 i++;
5007 }
5008 }
5009
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005010 i = 0;
5011 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5012 nla_for_each_nested(attr,
5013 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5014 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005015 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005016
5017 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5018 nla_data(attr), nla_len(attr),
5019 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005020 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005021 if (ssid) {
5022 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5023 err = -EINVAL;
5024 goto out_free;
5025 }
5026 memcpy(request->match_sets[i].ssid.ssid,
5027 nla_data(ssid), nla_len(ssid));
5028 request->match_sets[i].ssid.ssid_len =
5029 nla_len(ssid);
5030 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005031 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5032 if (rssi)
5033 request->rssi_thold = nla_get_u32(rssi);
5034 else
5035 request->rssi_thold =
5036 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005037 i++;
5038 }
5039 }
5040
Luciano Coelho807f8a82011-05-11 17:09:35 +03005041 if (info->attrs[NL80211_ATTR_IE]) {
5042 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5043 memcpy((void *)request->ie,
5044 nla_data(info->attrs[NL80211_ATTR_IE]),
5045 request->ie_len);
5046 }
5047
Sam Leffler46856bb2012-10-11 21:03:32 -07005048 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005049 request->flags = nla_get_u32(
5050 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005051 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5052 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5053 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5054 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005055 err = -EOPNOTSUPP;
5056 goto out_free;
5057 }
5058 }
Sam Lefflered4737712012-10-11 21:03:31 -07005059
Luciano Coelho807f8a82011-05-11 17:09:35 +03005060 request->dev = dev;
5061 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005062 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005063 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005064
Hila Gonene35e4d22012-06-27 17:19:42 +03005065 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005066 if (!err) {
5067 rdev->sched_scan_req = request;
5068 nl80211_send_sched_scan(rdev, dev,
5069 NL80211_CMD_START_SCHED_SCAN);
5070 goto out;
5071 }
5072
5073out_free:
5074 kfree(request);
5075out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005076 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005077 return err;
5078}
5079
5080static int nl80211_stop_sched_scan(struct sk_buff *skb,
5081 struct genl_info *info)
5082{
5083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005084 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005085
5086 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5087 !rdev->ops->sched_scan_stop)
5088 return -EOPNOTSUPP;
5089
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005090 mutex_lock(&rdev->sched_scan_mtx);
5091 err = __cfg80211_stop_sched_scan(rdev, false);
5092 mutex_unlock(&rdev->sched_scan_mtx);
5093
5094 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005095}
5096
Simon Wunderlich04f39042013-02-08 18:16:19 +01005097static int nl80211_start_radar_detection(struct sk_buff *skb,
5098 struct genl_info *info)
5099{
5100 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5101 struct net_device *dev = info->user_ptr[1];
5102 struct wireless_dev *wdev = dev->ieee80211_ptr;
5103 struct cfg80211_chan_def chandef;
5104 int err;
5105
5106 err = nl80211_parse_chandef(rdev, info, &chandef);
5107 if (err)
5108 return err;
5109
5110 if (wdev->cac_started)
5111 return -EBUSY;
5112
5113 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5114 if (err < 0)
5115 return err;
5116
5117 if (err == 0)
5118 return -EINVAL;
5119
5120 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5121 return -EINVAL;
5122
5123 if (!rdev->ops->start_radar_detection)
5124 return -EOPNOTSUPP;
5125
5126 mutex_lock(&rdev->devlist_mtx);
5127 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5128 chandef.chan, CHAN_MODE_SHARED,
5129 BIT(chandef.width));
5130 if (err)
5131 goto err_locked;
5132
5133 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5134 if (!err) {
5135 wdev->channel = chandef.chan;
5136 wdev->cac_started = true;
5137 wdev->cac_start_time = jiffies;
5138 }
5139err_locked:
5140 mutex_unlock(&rdev->devlist_mtx);
5141
5142 return err;
5143}
5144
Johannes Berg9720bb32011-06-21 09:45:33 +02005145static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5146 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005147 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005148 struct wireless_dev *wdev,
5149 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005150{
Johannes Berg48ab9052009-07-10 18:42:31 +02005151 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005152 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005153 void *hdr;
5154 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005155 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005156
5157 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005158
Eric W. Biederman15e47302012-09-07 20:12:54 +00005159 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005160 NL80211_CMD_NEW_SCAN_RESULTS);
5161 if (!hdr)
5162 return -1;
5163
Johannes Berg9720bb32011-06-21 09:45:33 +02005164 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5165
David S. Miller9360ffd2012-03-29 04:41:26 -04005166 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5167 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5168 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005169
5170 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5171 if (!bss)
5172 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005173 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005174 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005175 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005176
5177 rcu_read_lock();
5178 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005179 if (ies) {
5180 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5181 goto fail_unlock_rcu;
5182 tsf = true;
5183 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5184 ies->len, ies->data))
5185 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005186 }
5187 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005188 if (ies) {
5189 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5190 goto fail_unlock_rcu;
5191 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5192 ies->len, ies->data))
5193 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005194 }
5195 rcu_read_unlock();
5196
David S. Miller9360ffd2012-03-29 04:41:26 -04005197 if (res->beacon_interval &&
5198 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5199 goto nla_put_failure;
5200 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5201 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5202 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5203 jiffies_to_msecs(jiffies - intbss->ts)))
5204 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005205
Johannes Berg77965c92009-02-18 18:45:06 +01005206 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005207 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005208 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5209 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005210 break;
5211 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005212 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5213 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005214 break;
5215 default:
5216 break;
5217 }
5218
Johannes Berg48ab9052009-07-10 18:42:31 +02005219 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005220 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005221 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005222 if (intbss == wdev->current_bss &&
5223 nla_put_u32(msg, NL80211_BSS_STATUS,
5224 NL80211_BSS_STATUS_ASSOCIATED))
5225 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005226 break;
5227 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005228 if (intbss == wdev->current_bss &&
5229 nla_put_u32(msg, NL80211_BSS_STATUS,
5230 NL80211_BSS_STATUS_IBSS_JOINED))
5231 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005232 break;
5233 default:
5234 break;
5235 }
5236
Johannes Berg2a519312009-02-10 21:25:55 +01005237 nla_nest_end(msg, bss);
5238
5239 return genlmsg_end(msg, hdr);
5240
Johannes Berg8cef2c92013-02-05 16:54:31 +01005241 fail_unlock_rcu:
5242 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005243 nla_put_failure:
5244 genlmsg_cancel(msg, hdr);
5245 return -EMSGSIZE;
5246}
5247
5248static int nl80211_dump_scan(struct sk_buff *skb,
5249 struct netlink_callback *cb)
5250{
Johannes Berg48ab9052009-07-10 18:42:31 +02005251 struct cfg80211_registered_device *rdev;
5252 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005253 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005254 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005255 int start = cb->args[1], idx = 0;
5256 int err;
5257
Johannes Berg67748892010-10-04 21:14:06 +02005258 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5259 if (err)
5260 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005261
Johannes Berg48ab9052009-07-10 18:42:31 +02005262 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005263
Johannes Berg48ab9052009-07-10 18:42:31 +02005264 wdev_lock(wdev);
5265 spin_lock_bh(&rdev->bss_lock);
5266 cfg80211_bss_expire(rdev);
5267
Johannes Berg9720bb32011-06-21 09:45:33 +02005268 cb->seq = rdev->bss_generation;
5269
Johannes Berg48ab9052009-07-10 18:42:31 +02005270 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005271 if (++idx <= start)
5272 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005273 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005274 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005275 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005276 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005277 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005278 }
5279 }
5280
Johannes Berg48ab9052009-07-10 18:42:31 +02005281 spin_unlock_bh(&rdev->bss_lock);
5282 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005283
5284 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005285 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005286
Johannes Berg67748892010-10-04 21:14:06 +02005287 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005288}
5289
Eric W. Biederman15e47302012-09-07 20:12:54 +00005290static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005291 int flags, struct net_device *dev,
5292 struct survey_info *survey)
5293{
5294 void *hdr;
5295 struct nlattr *infoattr;
5296
Eric W. Biederman15e47302012-09-07 20:12:54 +00005297 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005298 NL80211_CMD_NEW_SURVEY_RESULTS);
5299 if (!hdr)
5300 return -ENOMEM;
5301
David S. Miller9360ffd2012-03-29 04:41:26 -04005302 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5303 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005304
5305 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5306 if (!infoattr)
5307 goto nla_put_failure;
5308
David S. Miller9360ffd2012-03-29 04:41:26 -04005309 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5310 survey->channel->center_freq))
5311 goto nla_put_failure;
5312
5313 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5314 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5315 goto nla_put_failure;
5316 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5317 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5318 goto nla_put_failure;
5319 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5320 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5321 survey->channel_time))
5322 goto nla_put_failure;
5323 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5324 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5325 survey->channel_time_busy))
5326 goto nla_put_failure;
5327 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5328 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5329 survey->channel_time_ext_busy))
5330 goto nla_put_failure;
5331 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5332 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5333 survey->channel_time_rx))
5334 goto nla_put_failure;
5335 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5336 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5337 survey->channel_time_tx))
5338 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005339
5340 nla_nest_end(msg, infoattr);
5341
5342 return genlmsg_end(msg, hdr);
5343
5344 nla_put_failure:
5345 genlmsg_cancel(msg, hdr);
5346 return -EMSGSIZE;
5347}
5348
5349static int nl80211_dump_survey(struct sk_buff *skb,
5350 struct netlink_callback *cb)
5351{
5352 struct survey_info survey;
5353 struct cfg80211_registered_device *dev;
5354 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005355 int survey_idx = cb->args[1];
5356 int res;
5357
Johannes Berg67748892010-10-04 21:14:06 +02005358 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5359 if (res)
5360 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005361
5362 if (!dev->ops->dump_survey) {
5363 res = -EOPNOTSUPP;
5364 goto out_err;
5365 }
5366
5367 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005368 struct ieee80211_channel *chan;
5369
Hila Gonene35e4d22012-06-27 17:19:42 +03005370 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005371 if (res == -ENOENT)
5372 break;
5373 if (res)
5374 goto out_err;
5375
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005376 /* Survey without a channel doesn't make sense */
5377 if (!survey.channel) {
5378 res = -EINVAL;
5379 goto out;
5380 }
5381
5382 chan = ieee80211_get_channel(&dev->wiphy,
5383 survey.channel->center_freq);
5384 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5385 survey_idx++;
5386 continue;
5387 }
5388
Holger Schurig61fa7132009-11-11 12:25:40 +01005389 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005390 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005391 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5392 netdev,
5393 &survey) < 0)
5394 goto out;
5395 survey_idx++;
5396 }
5397
5398 out:
5399 cb->args[1] = survey_idx;
5400 res = skb->len;
5401 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005402 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005403 return res;
5404}
5405
Samuel Ortizb23aa672009-07-01 21:26:54 +02005406static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5407{
5408 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5409 NL80211_WPA_VERSION_2));
5410}
5411
Jouni Malinen636a5d32009-03-19 13:39:22 +02005412static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5413{
Johannes Berg4c476992010-10-04 21:36:35 +02005414 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5415 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005416 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005417 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5418 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005419 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005420 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005421 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005422
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005423 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5424 return -EINVAL;
5425
5426 if (!info->attrs[NL80211_ATTR_MAC])
5427 return -EINVAL;
5428
Jouni Malinen17780922009-03-27 20:52:47 +02005429 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5430 return -EINVAL;
5431
Johannes Berg19957bb2009-07-02 17:20:43 +02005432 if (!info->attrs[NL80211_ATTR_SSID])
5433 return -EINVAL;
5434
5435 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5436 return -EINVAL;
5437
Johannes Bergfffd0932009-07-08 14:22:54 +02005438 err = nl80211_parse_key(info, &key);
5439 if (err)
5440 return err;
5441
5442 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005443 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5444 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005445 if (!key.p.key || !key.p.key_len)
5446 return -EINVAL;
5447 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5448 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5449 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5450 key.p.key_len != WLAN_KEY_LEN_WEP104))
5451 return -EINVAL;
5452 if (key.idx > 4)
5453 return -EINVAL;
5454 } else {
5455 key.p.key_len = 0;
5456 key.p.key = NULL;
5457 }
5458
Johannes Bergafea0b72010-08-10 09:46:42 +02005459 if (key.idx >= 0) {
5460 int i;
5461 bool ok = false;
5462 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5463 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5464 ok = true;
5465 break;
5466 }
5467 }
Johannes Berg4c476992010-10-04 21:36:35 +02005468 if (!ok)
5469 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005470 }
5471
Johannes Berg4c476992010-10-04 21:36:35 +02005472 if (!rdev->ops->auth)
5473 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005474
Johannes Berg074ac8d2010-09-16 14:58:22 +02005475 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005476 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5477 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005478
Johannes Berg19957bb2009-07-02 17:20:43 +02005479 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005480 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005481 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005482 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5483 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005484
Johannes Berg19957bb2009-07-02 17:20:43 +02005485 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5486 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5487
5488 if (info->attrs[NL80211_ATTR_IE]) {
5489 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5490 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5491 }
5492
5493 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005494 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005495 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005496
Jouni Malinene39e5b52012-09-30 19:29:39 +03005497 if (auth_type == NL80211_AUTHTYPE_SAE &&
5498 !info->attrs[NL80211_ATTR_SAE_DATA])
5499 return -EINVAL;
5500
5501 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5502 if (auth_type != NL80211_AUTHTYPE_SAE)
5503 return -EINVAL;
5504 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5505 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5506 /* need to include at least Auth Transaction and Status Code */
5507 if (sae_data_len < 4)
5508 return -EINVAL;
5509 }
5510
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005511 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5512
Johannes Berg95de8172012-01-20 13:55:25 +01005513 /*
5514 * Since we no longer track auth state, ignore
5515 * requests to only change local state.
5516 */
5517 if (local_state_change)
5518 return 0;
5519
Johannes Berg4c476992010-10-04 21:36:35 +02005520 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5521 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005522 key.p.key, key.p.key_len, key.idx,
5523 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005524}
5525
Johannes Bergc0692b82010-08-27 14:26:53 +03005526static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5527 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005528 struct cfg80211_crypto_settings *settings,
5529 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005530{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005531 memset(settings, 0, sizeof(*settings));
5532
Samuel Ortizb23aa672009-07-01 21:26:54 +02005533 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5534
Johannes Bergc0692b82010-08-27 14:26:53 +03005535 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5536 u16 proto;
5537 proto = nla_get_u16(
5538 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5539 settings->control_port_ethertype = cpu_to_be16(proto);
5540 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5541 proto != ETH_P_PAE)
5542 return -EINVAL;
5543 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5544 settings->control_port_no_encrypt = true;
5545 } else
5546 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5547
Samuel Ortizb23aa672009-07-01 21:26:54 +02005548 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5549 void *data;
5550 int len, i;
5551
5552 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5553 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5554 settings->n_ciphers_pairwise = len / sizeof(u32);
5555
5556 if (len % sizeof(u32))
5557 return -EINVAL;
5558
Johannes Berg3dc27d22009-07-02 21:36:37 +02005559 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005560 return -EINVAL;
5561
5562 memcpy(settings->ciphers_pairwise, data, len);
5563
5564 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005565 if (!cfg80211_supported_cipher_suite(
5566 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005567 settings->ciphers_pairwise[i]))
5568 return -EINVAL;
5569 }
5570
5571 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5572 settings->cipher_group =
5573 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005574 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5575 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005576 return -EINVAL;
5577 }
5578
5579 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5580 settings->wpa_versions =
5581 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5582 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5583 return -EINVAL;
5584 }
5585
5586 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5587 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005588 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005589
5590 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5591 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5592 settings->n_akm_suites = len / sizeof(u32);
5593
5594 if (len % sizeof(u32))
5595 return -EINVAL;
5596
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005597 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5598 return -EINVAL;
5599
Samuel Ortizb23aa672009-07-01 21:26:54 +02005600 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005601 }
5602
5603 return 0;
5604}
5605
Jouni Malinen636a5d32009-03-19 13:39:22 +02005606static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5607{
Johannes Berg4c476992010-10-04 21:36:35 +02005608 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5609 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005610 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005611 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005612 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005613 int err, ssid_len, ie_len = 0;
5614 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005615 u32 flags = 0;
5616 struct ieee80211_ht_cap *ht_capa = NULL;
5617 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005618
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005619 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5620 return -EINVAL;
5621
5622 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005623 !info->attrs[NL80211_ATTR_SSID] ||
5624 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005625 return -EINVAL;
5626
Johannes Berg4c476992010-10-04 21:36:35 +02005627 if (!rdev->ops->assoc)
5628 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005629
Johannes Berg074ac8d2010-09-16 14:58:22 +02005630 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005631 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5632 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005633
Johannes Berg19957bb2009-07-02 17:20:43 +02005634 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005635
Johannes Berg19957bb2009-07-02 17:20:43 +02005636 chan = ieee80211_get_channel(&rdev->wiphy,
5637 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005638 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5639 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005640
Johannes Berg19957bb2009-07-02 17:20:43 +02005641 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5642 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005643
5644 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005645 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5646 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005647 }
5648
Jouni Malinendc6382c2009-05-06 22:09:37 +03005649 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005650 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005651 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005652 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005653 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005654 else if (mfp != NL80211_MFP_NO)
5655 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005656 }
5657
Johannes Berg3e5d7642009-07-07 14:37:26 +02005658 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5659 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5660
Ben Greear7e7c8922011-11-18 11:31:59 -08005661 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5662 flags |= ASSOC_REQ_DISABLE_HT;
5663
5664 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5665 ht_capa_mask =
5666 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5667
5668 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5669 if (!ht_capa_mask)
5670 return -EINVAL;
5671 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5672 }
5673
Johannes Bergc0692b82010-08-27 14:26:53 +03005674 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005675 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005676 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5677 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005678 &crypto, flags, ht_capa,
5679 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005680
Jouni Malinen636a5d32009-03-19 13:39:22 +02005681 return err;
5682}
5683
5684static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5685{
Johannes Berg4c476992010-10-04 21:36:35 +02005686 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5687 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005688 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005689 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005690 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005691 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005692
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005693 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5694 return -EINVAL;
5695
5696 if (!info->attrs[NL80211_ATTR_MAC])
5697 return -EINVAL;
5698
5699 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5700 return -EINVAL;
5701
Johannes Berg4c476992010-10-04 21:36:35 +02005702 if (!rdev->ops->deauth)
5703 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005704
Johannes Berg074ac8d2010-09-16 14:58:22 +02005705 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005706 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5707 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005708
Johannes Berg19957bb2009-07-02 17:20:43 +02005709 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005710
Johannes Berg19957bb2009-07-02 17:20:43 +02005711 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5712 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005713 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005714 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005715 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005716
5717 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005718 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5719 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005720 }
5721
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005722 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5723
Johannes Berg4c476992010-10-04 21:36:35 +02005724 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5725 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005726}
5727
5728static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5729{
Johannes Berg4c476992010-10-04 21:36:35 +02005730 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5731 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005732 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005733 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005734 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005735 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005736
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005737 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5738 return -EINVAL;
5739
5740 if (!info->attrs[NL80211_ATTR_MAC])
5741 return -EINVAL;
5742
5743 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5744 return -EINVAL;
5745
Johannes Berg4c476992010-10-04 21:36:35 +02005746 if (!rdev->ops->disassoc)
5747 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005748
Johannes Berg074ac8d2010-09-16 14:58:22 +02005749 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005750 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5751 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005752
Johannes Berg19957bb2009-07-02 17:20:43 +02005753 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005754
Johannes Berg19957bb2009-07-02 17:20:43 +02005755 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5756 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005757 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005758 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005759 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005760
5761 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005762 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5763 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005764 }
5765
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005766 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5767
Johannes Berg4c476992010-10-04 21:36:35 +02005768 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5769 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005770}
5771
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005772static bool
5773nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5774 int mcast_rate[IEEE80211_NUM_BANDS],
5775 int rateval)
5776{
5777 struct wiphy *wiphy = &rdev->wiphy;
5778 bool found = false;
5779 int band, i;
5780
5781 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5782 struct ieee80211_supported_band *sband;
5783
5784 sband = wiphy->bands[band];
5785 if (!sband)
5786 continue;
5787
5788 for (i = 0; i < sband->n_bitrates; i++) {
5789 if (sband->bitrates[i].bitrate == rateval) {
5790 mcast_rate[band] = i + 1;
5791 found = true;
5792 break;
5793 }
5794 }
5795 }
5796
5797 return found;
5798}
5799
Johannes Berg04a773a2009-04-19 21:24:32 +02005800static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5801{
Johannes Berg4c476992010-10-04 21:36:35 +02005802 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5803 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005804 struct cfg80211_ibss_params ibss;
5805 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005806 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005807 int err;
5808
Johannes Berg8e30bc52009-04-22 17:45:38 +02005809 memset(&ibss, 0, sizeof(ibss));
5810
Johannes Berg04a773a2009-04-19 21:24:32 +02005811 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5812 return -EINVAL;
5813
Johannes Berg683b6d32012-11-08 21:25:48 +01005814 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005815 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5816 return -EINVAL;
5817
Johannes Berg8e30bc52009-04-22 17:45:38 +02005818 ibss.beacon_interval = 100;
5819
5820 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5821 ibss.beacon_interval =
5822 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5823 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5824 return -EINVAL;
5825 }
5826
Johannes Berg4c476992010-10-04 21:36:35 +02005827 if (!rdev->ops->join_ibss)
5828 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005829
Johannes Berg4c476992010-10-04 21:36:35 +02005830 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5831 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005832
Johannes Berg79c97e92009-07-07 03:56:12 +02005833 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005834
Johannes Berg39193492011-09-16 13:45:25 +02005835 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005836 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005837
5838 if (!is_valid_ether_addr(ibss.bssid))
5839 return -EINVAL;
5840 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005841 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5842 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5843
5844 if (info->attrs[NL80211_ATTR_IE]) {
5845 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5846 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5847 }
5848
Johannes Berg683b6d32012-11-08 21:25:48 +01005849 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5850 if (err)
5851 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005852
Johannes Berg683b6d32012-11-08 21:25:48 +01005853 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005854 return -EINVAL;
5855
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005856 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5857 return -EINVAL;
5858 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5859 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005860 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005861
Johannes Berg04a773a2009-04-19 21:24:32 +02005862 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005863 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005864
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005865 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5866 u8 *rates =
5867 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5868 int n_rates =
5869 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5870 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005871 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005872
Johannes Berg34850ab2011-07-18 18:08:35 +02005873 err = ieee80211_get_ratemask(sband, rates, n_rates,
5874 &ibss.basic_rates);
5875 if (err)
5876 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005877 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005878
5879 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5880 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5881 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5882 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005883
Johannes Berg4c476992010-10-04 21:36:35 +02005884 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305885 bool no_ht = false;
5886
Johannes Berg4c476992010-10-04 21:36:35 +02005887 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305888 info->attrs[NL80211_ATTR_KEYS],
5889 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005890 if (IS_ERR(connkeys))
5891 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305892
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005893 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5894 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305895 kfree(connkeys);
5896 return -EINVAL;
5897 }
Johannes Berg4c476992010-10-04 21:36:35 +02005898 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005899
Antonio Quartulli267335d2012-01-31 20:25:47 +01005900 ibss.control_port =
5901 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5902
Johannes Berg4c476992010-10-04 21:36:35 +02005903 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005904 if (err)
5905 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005906 return err;
5907}
5908
5909static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5910{
Johannes Berg4c476992010-10-04 21:36:35 +02005911 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5912 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005913
Johannes Berg4c476992010-10-04 21:36:35 +02005914 if (!rdev->ops->leave_ibss)
5915 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005916
Johannes Berg4c476992010-10-04 21:36:35 +02005917 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5918 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005919
Johannes Berg4c476992010-10-04 21:36:35 +02005920 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005921}
5922
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005923static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5924{
5925 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5926 struct net_device *dev = info->user_ptr[1];
5927 int mcast_rate[IEEE80211_NUM_BANDS];
5928 u32 nla_rate;
5929 int err;
5930
5931 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5932 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5933 return -EOPNOTSUPP;
5934
5935 if (!rdev->ops->set_mcast_rate)
5936 return -EOPNOTSUPP;
5937
5938 memset(mcast_rate, 0, sizeof(mcast_rate));
5939
5940 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5941 return -EINVAL;
5942
5943 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5944 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5945 return -EINVAL;
5946
5947 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5948
5949 return err;
5950}
5951
5952
Johannes Bergaff89a92009-07-01 21:26:51 +02005953#ifdef CONFIG_NL80211_TESTMODE
5954static struct genl_multicast_group nl80211_testmode_mcgrp = {
5955 .name = "testmode",
5956};
5957
5958static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5959{
Johannes Berg4c476992010-10-04 21:36:35 +02005960 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005961 int err;
5962
5963 if (!info->attrs[NL80211_ATTR_TESTDATA])
5964 return -EINVAL;
5965
Johannes Bergaff89a92009-07-01 21:26:51 +02005966 err = -EOPNOTSUPP;
5967 if (rdev->ops->testmode_cmd) {
5968 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005969 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005970 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5971 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5972 rdev->testmode_info = NULL;
5973 }
5974
Johannes Bergaff89a92009-07-01 21:26:51 +02005975 return err;
5976}
5977
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005978static int nl80211_testmode_dump(struct sk_buff *skb,
5979 struct netlink_callback *cb)
5980{
Johannes Berg00918d32011-12-13 17:22:05 +01005981 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005982 int err;
5983 long phy_idx;
5984 void *data = NULL;
5985 int data_len = 0;
5986
5987 if (cb->args[0]) {
5988 /*
5989 * 0 is a valid index, but not valid for args[0],
5990 * so we need to offset by 1.
5991 */
5992 phy_idx = cb->args[0] - 1;
5993 } else {
5994 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5995 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5996 nl80211_policy);
5997 if (err)
5998 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005999
Johannes Berg2bd7e352012-06-15 14:23:16 +02006000 mutex_lock(&cfg80211_mutex);
6001 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6002 nl80211_fam.attrbuf);
6003 if (IS_ERR(rdev)) {
6004 mutex_unlock(&cfg80211_mutex);
6005 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006006 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006007 phy_idx = rdev->wiphy_idx;
6008 rdev = NULL;
6009 mutex_unlock(&cfg80211_mutex);
6010
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006011 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6012 cb->args[1] =
6013 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6014 }
6015
6016 if (cb->args[1]) {
6017 data = nla_data((void *)cb->args[1]);
6018 data_len = nla_len((void *)cb->args[1]);
6019 }
6020
6021 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006022 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6023 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006024 mutex_unlock(&cfg80211_mutex);
6025 return -ENOENT;
6026 }
Johannes Berg00918d32011-12-13 17:22:05 +01006027 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006028 mutex_unlock(&cfg80211_mutex);
6029
Johannes Berg00918d32011-12-13 17:22:05 +01006030 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006031 err = -EOPNOTSUPP;
6032 goto out_err;
6033 }
6034
6035 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006036 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006037 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6038 NL80211_CMD_TESTMODE);
6039 struct nlattr *tmdata;
6040
David S. Miller9360ffd2012-03-29 04:41:26 -04006041 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006042 genlmsg_cancel(skb, hdr);
6043 break;
6044 }
6045
6046 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6047 if (!tmdata) {
6048 genlmsg_cancel(skb, hdr);
6049 break;
6050 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006051 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006052 nla_nest_end(skb, tmdata);
6053
6054 if (err == -ENOBUFS || err == -ENOENT) {
6055 genlmsg_cancel(skb, hdr);
6056 break;
6057 } else if (err) {
6058 genlmsg_cancel(skb, hdr);
6059 goto out_err;
6060 }
6061
6062 genlmsg_end(skb, hdr);
6063 }
6064
6065 err = skb->len;
6066 /* see above */
6067 cb->args[0] = phy_idx + 1;
6068 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006069 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006070 return err;
6071}
6072
Johannes Bergaff89a92009-07-01 21:26:51 +02006073static struct sk_buff *
6074__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006075 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006076{
6077 struct sk_buff *skb;
6078 void *hdr;
6079 struct nlattr *data;
6080
6081 skb = nlmsg_new(approxlen + 100, gfp);
6082 if (!skb)
6083 return NULL;
6084
Eric W. Biederman15e47302012-09-07 20:12:54 +00006085 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006086 if (!hdr) {
6087 kfree_skb(skb);
6088 return NULL;
6089 }
6090
David S. Miller9360ffd2012-03-29 04:41:26 -04006091 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6092 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006093 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6094
6095 ((void **)skb->cb)[0] = rdev;
6096 ((void **)skb->cb)[1] = hdr;
6097 ((void **)skb->cb)[2] = data;
6098
6099 return skb;
6100
6101 nla_put_failure:
6102 kfree_skb(skb);
6103 return NULL;
6104}
6105
6106struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6107 int approxlen)
6108{
6109 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6110
6111 if (WARN_ON(!rdev->testmode_info))
6112 return NULL;
6113
6114 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006115 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006116 rdev->testmode_info->snd_seq,
6117 GFP_KERNEL);
6118}
6119EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6120
6121int cfg80211_testmode_reply(struct sk_buff *skb)
6122{
6123 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6124 void *hdr = ((void **)skb->cb)[1];
6125 struct nlattr *data = ((void **)skb->cb)[2];
6126
6127 if (WARN_ON(!rdev->testmode_info)) {
6128 kfree_skb(skb);
6129 return -EINVAL;
6130 }
6131
6132 nla_nest_end(skb, data);
6133 genlmsg_end(skb, hdr);
6134 return genlmsg_reply(skb, rdev->testmode_info);
6135}
6136EXPORT_SYMBOL(cfg80211_testmode_reply);
6137
6138struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6139 int approxlen, gfp_t gfp)
6140{
6141 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6142
6143 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6144}
6145EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6146
6147void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6148{
6149 void *hdr = ((void **)skb->cb)[1];
6150 struct nlattr *data = ((void **)skb->cb)[2];
6151
6152 nla_nest_end(skb, data);
6153 genlmsg_end(skb, hdr);
6154 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6155}
6156EXPORT_SYMBOL(cfg80211_testmode_event);
6157#endif
6158
Samuel Ortizb23aa672009-07-01 21:26:54 +02006159static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6160{
Johannes Berg4c476992010-10-04 21:36:35 +02006161 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6162 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006163 struct cfg80211_connect_params connect;
6164 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006165 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006166 int err;
6167
6168 memset(&connect, 0, sizeof(connect));
6169
6170 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6171 return -EINVAL;
6172
6173 if (!info->attrs[NL80211_ATTR_SSID] ||
6174 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6175 return -EINVAL;
6176
6177 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6178 connect.auth_type =
6179 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006180 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6181 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006182 return -EINVAL;
6183 } else
6184 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6185
6186 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6187
Johannes Bergc0692b82010-08-27 14:26:53 +03006188 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006189 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006190 if (err)
6191 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006192
Johannes Berg074ac8d2010-09-16 14:58:22 +02006193 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006194 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6195 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006196
Johannes Berg79c97e92009-07-07 03:56:12 +02006197 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006198
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306199 connect.bg_scan_period = -1;
6200 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6201 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6202 connect.bg_scan_period =
6203 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6204 }
6205
Samuel Ortizb23aa672009-07-01 21:26:54 +02006206 if (info->attrs[NL80211_ATTR_MAC])
6207 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6208 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6209 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6210
6211 if (info->attrs[NL80211_ATTR_IE]) {
6212 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6213 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6214 }
6215
Jouni Malinencee00a92013-01-15 17:15:57 +02006216 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6217 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6218 if (connect.mfp != NL80211_MFP_REQUIRED &&
6219 connect.mfp != NL80211_MFP_NO)
6220 return -EINVAL;
6221 } else {
6222 connect.mfp = NL80211_MFP_NO;
6223 }
6224
Samuel Ortizb23aa672009-07-01 21:26:54 +02006225 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6226 connect.channel =
6227 ieee80211_get_channel(wiphy,
6228 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6229 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006230 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6231 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006232 }
6233
Johannes Bergfffd0932009-07-08 14:22:54 +02006234 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6235 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306236 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006237 if (IS_ERR(connkeys))
6238 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006239 }
6240
Ben Greear7e7c8922011-11-18 11:31:59 -08006241 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6242 connect.flags |= ASSOC_REQ_DISABLE_HT;
6243
6244 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6245 memcpy(&connect.ht_capa_mask,
6246 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6247 sizeof(connect.ht_capa_mask));
6248
6249 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006250 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6251 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006252 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006253 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006254 memcpy(&connect.ht_capa,
6255 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6256 sizeof(connect.ht_capa));
6257 }
6258
Johannes Bergfffd0932009-07-08 14:22:54 +02006259 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006260 if (err)
6261 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006262 return err;
6263}
6264
6265static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6266{
Johannes Berg4c476992010-10-04 21:36:35 +02006267 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6268 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006269 u16 reason;
6270
6271 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6272 reason = WLAN_REASON_DEAUTH_LEAVING;
6273 else
6274 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6275
6276 if (reason == 0)
6277 return -EINVAL;
6278
Johannes Berg074ac8d2010-09-16 14:58:22 +02006279 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006280 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6281 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006282
Johannes Berg4c476992010-10-04 21:36:35 +02006283 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006284}
6285
Johannes Berg463d0182009-07-14 00:33:35 +02006286static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6287{
Johannes Berg4c476992010-10-04 21:36:35 +02006288 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006289 struct net *net;
6290 int err;
6291 u32 pid;
6292
6293 if (!info->attrs[NL80211_ATTR_PID])
6294 return -EINVAL;
6295
6296 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6297
Johannes Berg463d0182009-07-14 00:33:35 +02006298 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006299 if (IS_ERR(net))
6300 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006301
6302 err = 0;
6303
6304 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006305 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6306 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006307
Johannes Berg463d0182009-07-14 00:33:35 +02006308 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006309 return err;
6310}
6311
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006312static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6313{
Johannes Berg4c476992010-10-04 21:36:35 +02006314 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006315 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6316 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006317 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006318 struct cfg80211_pmksa pmksa;
6319
6320 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6321
6322 if (!info->attrs[NL80211_ATTR_MAC])
6323 return -EINVAL;
6324
6325 if (!info->attrs[NL80211_ATTR_PMKID])
6326 return -EINVAL;
6327
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006328 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6329 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6330
Johannes Berg074ac8d2010-09-16 14:58:22 +02006331 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006332 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6333 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006334
6335 switch (info->genlhdr->cmd) {
6336 case NL80211_CMD_SET_PMKSA:
6337 rdev_ops = rdev->ops->set_pmksa;
6338 break;
6339 case NL80211_CMD_DEL_PMKSA:
6340 rdev_ops = rdev->ops->del_pmksa;
6341 break;
6342 default:
6343 WARN_ON(1);
6344 break;
6345 }
6346
Johannes Berg4c476992010-10-04 21:36:35 +02006347 if (!rdev_ops)
6348 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006349
Johannes Berg4c476992010-10-04 21:36:35 +02006350 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006351}
6352
6353static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6354{
Johannes Berg4c476992010-10-04 21:36:35 +02006355 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6356 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006357
Johannes Berg074ac8d2010-09-16 14:58:22 +02006358 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006359 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6360 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006361
Johannes Berg4c476992010-10-04 21:36:35 +02006362 if (!rdev->ops->flush_pmksa)
6363 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006364
Hila Gonene35e4d22012-06-27 17:19:42 +03006365 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006366}
6367
Arik Nemtsov109086c2011-09-28 14:12:50 +03006368static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6369{
6370 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6371 struct net_device *dev = info->user_ptr[1];
6372 u8 action_code, dialog_token;
6373 u16 status_code;
6374 u8 *peer;
6375
6376 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6377 !rdev->ops->tdls_mgmt)
6378 return -EOPNOTSUPP;
6379
6380 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6381 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6382 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6383 !info->attrs[NL80211_ATTR_IE] ||
6384 !info->attrs[NL80211_ATTR_MAC])
6385 return -EINVAL;
6386
6387 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6388 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6389 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6390 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6391
Hila Gonene35e4d22012-06-27 17:19:42 +03006392 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6393 dialog_token, status_code,
6394 nla_data(info->attrs[NL80211_ATTR_IE]),
6395 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006396}
6397
6398static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6399{
6400 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6401 struct net_device *dev = info->user_ptr[1];
6402 enum nl80211_tdls_operation operation;
6403 u8 *peer;
6404
6405 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6406 !rdev->ops->tdls_oper)
6407 return -EOPNOTSUPP;
6408
6409 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6410 !info->attrs[NL80211_ATTR_MAC])
6411 return -EINVAL;
6412
6413 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6414 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6415
Hila Gonene35e4d22012-06-27 17:19:42 +03006416 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006417}
6418
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006419static int nl80211_remain_on_channel(struct sk_buff *skb,
6420 struct genl_info *info)
6421{
Johannes Berg4c476992010-10-04 21:36:35 +02006422 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006423 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006424 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006425 struct sk_buff *msg;
6426 void *hdr;
6427 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006428 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006429 int err;
6430
6431 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6432 !info->attrs[NL80211_ATTR_DURATION])
6433 return -EINVAL;
6434
6435 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6436
Johannes Berg7c4ef712011-11-18 15:33:48 +01006437 if (!rdev->ops->remain_on_channel ||
6438 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006439 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006440
Johannes Bergebf348f2012-06-01 12:50:54 +02006441 /*
6442 * We should be on that channel for at least a minimum amount of
6443 * time (10ms) but no longer than the driver supports.
6444 */
6445 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6446 duration > rdev->wiphy.max_remain_on_channel_duration)
6447 return -EINVAL;
6448
Johannes Berg683b6d32012-11-08 21:25:48 +01006449 err = nl80211_parse_chandef(rdev, info, &chandef);
6450 if (err)
6451 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006452
6453 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006454 if (!msg)
6455 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006456
Eric W. Biederman15e47302012-09-07 20:12:54 +00006457 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006458 NL80211_CMD_REMAIN_ON_CHANNEL);
6459
6460 if (IS_ERR(hdr)) {
6461 err = PTR_ERR(hdr);
6462 goto free_msg;
6463 }
6464
Johannes Berg683b6d32012-11-08 21:25:48 +01006465 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6466 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006467
6468 if (err)
6469 goto free_msg;
6470
David S. Miller9360ffd2012-03-29 04:41:26 -04006471 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6472 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006473
6474 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006475
6476 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006477
6478 nla_put_failure:
6479 err = -ENOBUFS;
6480 free_msg:
6481 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006482 return err;
6483}
6484
6485static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6486 struct genl_info *info)
6487{
Johannes Berg4c476992010-10-04 21:36:35 +02006488 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006489 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006490 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006491
6492 if (!info->attrs[NL80211_ATTR_COOKIE])
6493 return -EINVAL;
6494
Johannes Berg4c476992010-10-04 21:36:35 +02006495 if (!rdev->ops->cancel_remain_on_channel)
6496 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006497
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006498 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6499
Hila Gonene35e4d22012-06-27 17:19:42 +03006500 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006501}
6502
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006503static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6504 u8 *rates, u8 rates_len)
6505{
6506 u8 i;
6507 u32 mask = 0;
6508
6509 for (i = 0; i < rates_len; i++) {
6510 int rate = (rates[i] & 0x7f) * 5;
6511 int ridx;
6512 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6513 struct ieee80211_rate *srate =
6514 &sband->bitrates[ridx];
6515 if (rate == srate->bitrate) {
6516 mask |= 1 << ridx;
6517 break;
6518 }
6519 }
6520 if (ridx == sband->n_bitrates)
6521 return 0; /* rate not found */
6522 }
6523
6524 return mask;
6525}
6526
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006527static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6528 u8 *rates, u8 rates_len,
6529 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6530{
6531 u8 i;
6532
6533 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6534
6535 for (i = 0; i < rates_len; i++) {
6536 int ridx, rbit;
6537
6538 ridx = rates[i] / 8;
6539 rbit = BIT(rates[i] % 8);
6540
6541 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006542 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006543 return false;
6544
6545 /* check availability */
6546 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6547 mcs[ridx] |= rbit;
6548 else
6549 return false;
6550 }
6551
6552 return true;
6553}
6554
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006555static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006556 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6557 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006558 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6559 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006560};
6561
6562static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6563 struct genl_info *info)
6564{
6565 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006566 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006567 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006568 int rem, i;
6569 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006570 struct nlattr *tx_rates;
6571 struct ieee80211_supported_band *sband;
6572
6573 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6574 return -EINVAL;
6575
Johannes Berg4c476992010-10-04 21:36:35 +02006576 if (!rdev->ops->set_bitrate_mask)
6577 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006578
6579 memset(&mask, 0, sizeof(mask));
6580 /* Default to all rates enabled */
6581 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6582 sband = rdev->wiphy.bands[i];
6583 mask.control[i].legacy =
6584 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006585 if (sband)
6586 memcpy(mask.control[i].mcs,
6587 sband->ht_cap.mcs.rx_mask,
6588 sizeof(mask.control[i].mcs));
6589 else
6590 memset(mask.control[i].mcs, 0,
6591 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006592 }
6593
6594 /*
6595 * The nested attribute uses enum nl80211_band as the index. This maps
6596 * directly to the enum ieee80211_band values used in cfg80211.
6597 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006598 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006599 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6600 {
6601 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006602 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6603 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006604 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006605 if (sband == NULL)
6606 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006607 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6608 nla_len(tx_rates), nl80211_txattr_policy);
6609 if (tb[NL80211_TXRATE_LEGACY]) {
6610 mask.control[band].legacy = rateset_to_mask(
6611 sband,
6612 nla_data(tb[NL80211_TXRATE_LEGACY]),
6613 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306614 if ((mask.control[band].legacy == 0) &&
6615 nla_len(tb[NL80211_TXRATE_LEGACY]))
6616 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006617 }
6618 if (tb[NL80211_TXRATE_MCS]) {
6619 if (!ht_rateset_to_mask(
6620 sband,
6621 nla_data(tb[NL80211_TXRATE_MCS]),
6622 nla_len(tb[NL80211_TXRATE_MCS]),
6623 mask.control[band].mcs))
6624 return -EINVAL;
6625 }
6626
6627 if (mask.control[band].legacy == 0) {
6628 /* don't allow empty legacy rates if HT
6629 * is not even supported. */
6630 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6631 return -EINVAL;
6632
6633 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6634 if (mask.control[band].mcs[i])
6635 break;
6636
6637 /* legacy and mcs rates may not be both empty */
6638 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006639 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006640 }
6641 }
6642
Hila Gonene35e4d22012-06-27 17:19:42 +03006643 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006644}
6645
Johannes Berg2e161f72010-08-12 15:38:38 +02006646static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006647{
Johannes Berg4c476992010-10-04 21:36:35 +02006648 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006649 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006650 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006651
6652 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6653 return -EINVAL;
6654
Johannes Berg2e161f72010-08-12 15:38:38 +02006655 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6656 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006657
Johannes Berg71bbc992012-06-15 15:30:18 +02006658 switch (wdev->iftype) {
6659 case NL80211_IFTYPE_STATION:
6660 case NL80211_IFTYPE_ADHOC:
6661 case NL80211_IFTYPE_P2P_CLIENT:
6662 case NL80211_IFTYPE_AP:
6663 case NL80211_IFTYPE_AP_VLAN:
6664 case NL80211_IFTYPE_MESH_POINT:
6665 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006666 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006667 break;
6668 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006669 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006670 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006671
6672 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006673 if (!rdev->ops->mgmt_tx)
6674 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006675
Eric W. Biederman15e47302012-09-07 20:12:54 +00006676 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006677 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6678 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006679}
6680
Johannes Berg2e161f72010-08-12 15:38:38 +02006681static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006682{
Johannes Berg4c476992010-10-04 21:36:35 +02006683 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006684 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006685 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006686 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006687 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006688 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006689 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006690 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006691 bool offchan, no_cck, dont_wait_for_ack;
6692
6693 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006694
Johannes Berg683b6d32012-11-08 21:25:48 +01006695 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006696 return -EINVAL;
6697
Johannes Berg4c476992010-10-04 21:36:35 +02006698 if (!rdev->ops->mgmt_tx)
6699 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006700
Johannes Berg71bbc992012-06-15 15:30:18 +02006701 switch (wdev->iftype) {
6702 case NL80211_IFTYPE_STATION:
6703 case NL80211_IFTYPE_ADHOC:
6704 case NL80211_IFTYPE_P2P_CLIENT:
6705 case NL80211_IFTYPE_AP:
6706 case NL80211_IFTYPE_AP_VLAN:
6707 case NL80211_IFTYPE_MESH_POINT:
6708 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006709 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006710 break;
6711 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006712 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006713 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006714
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006715 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006716 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006717 return -EINVAL;
6718 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006719
6720 /*
6721 * We should wait on the channel for at least a minimum amount
6722 * of time (10ms) but no longer than the driver supports.
6723 */
6724 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6725 wait > rdev->wiphy.max_remain_on_channel_duration)
6726 return -EINVAL;
6727
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006728 }
6729
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006730 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6731
Johannes Berg7c4ef712011-11-18 15:33:48 +01006732 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6733 return -EINVAL;
6734
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306735 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6736
Johannes Berg683b6d32012-11-08 21:25:48 +01006737 err = nl80211_parse_chandef(rdev, info, &chandef);
6738 if (err)
6739 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006740
Johannes Berge247bd902011-11-04 11:18:21 +01006741 if (!dont_wait_for_ack) {
6742 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6743 if (!msg)
6744 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006745
Eric W. Biederman15e47302012-09-07 20:12:54 +00006746 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006747 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006748
Johannes Berge247bd902011-11-04 11:18:21 +01006749 if (IS_ERR(hdr)) {
6750 err = PTR_ERR(hdr);
6751 goto free_msg;
6752 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006753 }
Johannes Berge247bd902011-11-04 11:18:21 +01006754
Johannes Berg683b6d32012-11-08 21:25:48 +01006755 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006756 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6757 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006758 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006759 if (err)
6760 goto free_msg;
6761
Johannes Berge247bd902011-11-04 11:18:21 +01006762 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006763 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6764 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006765
Johannes Berge247bd902011-11-04 11:18:21 +01006766 genlmsg_end(msg, hdr);
6767 return genlmsg_reply(msg, info);
6768 }
6769
6770 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006771
6772 nla_put_failure:
6773 err = -ENOBUFS;
6774 free_msg:
6775 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006776 return err;
6777}
6778
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006779static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6780{
6781 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006782 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006783 u64 cookie;
6784
6785 if (!info->attrs[NL80211_ATTR_COOKIE])
6786 return -EINVAL;
6787
6788 if (!rdev->ops->mgmt_tx_cancel_wait)
6789 return -EOPNOTSUPP;
6790
Johannes Berg71bbc992012-06-15 15:30:18 +02006791 switch (wdev->iftype) {
6792 case NL80211_IFTYPE_STATION:
6793 case NL80211_IFTYPE_ADHOC:
6794 case NL80211_IFTYPE_P2P_CLIENT:
6795 case NL80211_IFTYPE_AP:
6796 case NL80211_IFTYPE_AP_VLAN:
6797 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006798 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006799 break;
6800 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006801 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006802 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006803
6804 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6805
Hila Gonene35e4d22012-06-27 17:19:42 +03006806 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006807}
6808
Kalle Valoffb9eb32010-02-17 17:58:10 +02006809static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6810{
Johannes Berg4c476992010-10-04 21:36:35 +02006811 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006812 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006813 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006814 u8 ps_state;
6815 bool state;
6816 int err;
6817
Johannes Berg4c476992010-10-04 21:36:35 +02006818 if (!info->attrs[NL80211_ATTR_PS_STATE])
6819 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006820
6821 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6822
Johannes Berg4c476992010-10-04 21:36:35 +02006823 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6824 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006825
6826 wdev = dev->ieee80211_ptr;
6827
Johannes Berg4c476992010-10-04 21:36:35 +02006828 if (!rdev->ops->set_power_mgmt)
6829 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006830
6831 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6832
6833 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006834 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006835
Hila Gonene35e4d22012-06-27 17:19:42 +03006836 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006837 if (!err)
6838 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006839 return err;
6840}
6841
6842static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6843{
Johannes Berg4c476992010-10-04 21:36:35 +02006844 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006845 enum nl80211_ps_state ps_state;
6846 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006847 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006848 struct sk_buff *msg;
6849 void *hdr;
6850 int err;
6851
Kalle Valoffb9eb32010-02-17 17:58:10 +02006852 wdev = dev->ieee80211_ptr;
6853
Johannes Berg4c476992010-10-04 21:36:35 +02006854 if (!rdev->ops->set_power_mgmt)
6855 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006856
6857 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006858 if (!msg)
6859 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006860
Eric W. Biederman15e47302012-09-07 20:12:54 +00006861 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006862 NL80211_CMD_GET_POWER_SAVE);
6863 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006864 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006865 goto free_msg;
6866 }
6867
6868 if (wdev->ps)
6869 ps_state = NL80211_PS_ENABLED;
6870 else
6871 ps_state = NL80211_PS_DISABLED;
6872
David S. Miller9360ffd2012-03-29 04:41:26 -04006873 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6874 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006875
6876 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006877 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006878
Johannes Berg4c476992010-10-04 21:36:35 +02006879 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006880 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006881 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006882 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006883 return err;
6884}
6885
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006886static struct nla_policy
6887nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6888 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6889 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6890 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006891 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6892 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6893 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006894};
6895
Thomas Pedersen84f10702012-07-12 16:17:33 -07006896static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006897 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006898{
6899 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6900 struct wireless_dev *wdev;
6901 struct net_device *dev = info->user_ptr[1];
6902
Johannes Bergd9d8b012012-11-26 12:51:52 +01006903 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006904 return -EINVAL;
6905
6906 wdev = dev->ieee80211_ptr;
6907
6908 if (!rdev->ops->set_cqm_txe_config)
6909 return -EOPNOTSUPP;
6910
6911 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6912 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6913 return -EOPNOTSUPP;
6914
Hila Gonene35e4d22012-06-27 17:19:42 +03006915 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006916}
6917
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006918static int nl80211_set_cqm_rssi(struct genl_info *info,
6919 s32 threshold, u32 hysteresis)
6920{
Johannes Berg4c476992010-10-04 21:36:35 +02006921 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006922 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006923 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006924
6925 if (threshold > 0)
6926 return -EINVAL;
6927
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006928 wdev = dev->ieee80211_ptr;
6929
Johannes Berg4c476992010-10-04 21:36:35 +02006930 if (!rdev->ops->set_cqm_rssi_config)
6931 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006932
Johannes Berg074ac8d2010-09-16 14:58:22 +02006933 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006934 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6935 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006936
Hila Gonene35e4d22012-06-27 17:19:42 +03006937 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006938}
6939
6940static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6941{
6942 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6943 struct nlattr *cqm;
6944 int err;
6945
6946 cqm = info->attrs[NL80211_ATTR_CQM];
6947 if (!cqm) {
6948 err = -EINVAL;
6949 goto out;
6950 }
6951
6952 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6953 nl80211_attr_cqm_policy);
6954 if (err)
6955 goto out;
6956
6957 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6958 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6959 s32 threshold;
6960 u32 hysteresis;
6961 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6962 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6963 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006964 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6965 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6966 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6967 u32 rate, pkts, intvl;
6968 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6969 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6970 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6971 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006972 } else
6973 err = -EINVAL;
6974
6975out:
6976 return err;
6977}
6978
Johannes Berg29cbe682010-12-03 09:20:44 +01006979static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6980{
6981 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6982 struct net_device *dev = info->user_ptr[1];
6983 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006984 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006985 int err;
6986
6987 /* start with default */
6988 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006989 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006990
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006991 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006992 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006993 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006994 if (err)
6995 return err;
6996 }
6997
6998 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6999 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7000 return -EINVAL;
7001
Javier Cardonac80d5452010-12-16 17:37:49 -08007002 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7003 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7004
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007005 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7006 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7007 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7008 return -EINVAL;
7009
Marco Porsch9bdbf042013-01-07 16:04:51 +01007010 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7011 setup.beacon_interval =
7012 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7013 if (setup.beacon_interval < 10 ||
7014 setup.beacon_interval > 10000)
7015 return -EINVAL;
7016 }
7017
7018 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7019 setup.dtim_period =
7020 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7021 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7022 return -EINVAL;
7023 }
7024
Javier Cardonac80d5452010-12-16 17:37:49 -08007025 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7026 /* parse additional setup parameters if given */
7027 err = nl80211_parse_mesh_setup(info, &setup);
7028 if (err)
7029 return err;
7030 }
7031
Johannes Bergcc1d2802012-05-16 23:50:20 +02007032 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007033 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7034 if (err)
7035 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007036 } else {
7037 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007038 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007039 }
7040
Javier Cardonac80d5452010-12-16 17:37:49 -08007041 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007042}
7043
7044static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7045{
7046 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7047 struct net_device *dev = info->user_ptr[1];
7048
7049 return cfg80211_leave_mesh(rdev, dev);
7050}
7051
Johannes Bergdfb89c52012-06-27 09:23:48 +02007052#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007053static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7054 struct cfg80211_registered_device *rdev)
7055{
7056 struct nlattr *nl_pats, *nl_pat;
7057 int i, pat_len;
7058
7059 if (!rdev->wowlan->n_patterns)
7060 return 0;
7061
7062 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7063 if (!nl_pats)
7064 return -ENOBUFS;
7065
7066 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7067 nl_pat = nla_nest_start(msg, i + 1);
7068 if (!nl_pat)
7069 return -ENOBUFS;
7070 pat_len = rdev->wowlan->patterns[i].pattern_len;
7071 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7072 DIV_ROUND_UP(pat_len, 8),
7073 rdev->wowlan->patterns[i].mask) ||
7074 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7075 pat_len, rdev->wowlan->patterns[i].pattern) ||
7076 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7077 rdev->wowlan->patterns[i].pkt_offset))
7078 return -ENOBUFS;
7079 nla_nest_end(msg, nl_pat);
7080 }
7081 nla_nest_end(msg, nl_pats);
7082
7083 return 0;
7084}
7085
Johannes Berg2a0e0472013-01-23 22:57:40 +01007086static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7087 struct cfg80211_wowlan_tcp *tcp)
7088{
7089 struct nlattr *nl_tcp;
7090
7091 if (!tcp)
7092 return 0;
7093
7094 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7095 if (!nl_tcp)
7096 return -ENOBUFS;
7097
7098 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7099 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7100 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7101 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7102 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7103 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7104 tcp->payload_len, tcp->payload) ||
7105 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7106 tcp->data_interval) ||
7107 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7108 tcp->wake_len, tcp->wake_data) ||
7109 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7110 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7111 return -ENOBUFS;
7112
7113 if (tcp->payload_seq.len &&
7114 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7115 sizeof(tcp->payload_seq), &tcp->payload_seq))
7116 return -ENOBUFS;
7117
7118 if (tcp->payload_tok.len &&
7119 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7120 sizeof(tcp->payload_tok) + tcp->tokens_size,
7121 &tcp->payload_tok))
7122 return -ENOBUFS;
7123
7124 return 0;
7125}
7126
Johannes Bergff1b6e62011-05-04 15:37:28 +02007127static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7128{
7129 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7130 struct sk_buff *msg;
7131 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007132 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007133
Johannes Berg2a0e0472013-01-23 22:57:40 +01007134 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7135 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007136 return -EOPNOTSUPP;
7137
Johannes Berg2a0e0472013-01-23 22:57:40 +01007138 if (rdev->wowlan && rdev->wowlan->tcp) {
7139 /* adjust size to have room for all the data */
7140 size += rdev->wowlan->tcp->tokens_size +
7141 rdev->wowlan->tcp->payload_len +
7142 rdev->wowlan->tcp->wake_len +
7143 rdev->wowlan->tcp->wake_len / 8;
7144 }
7145
7146 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007147 if (!msg)
7148 return -ENOMEM;
7149
Eric W. Biederman15e47302012-09-07 20:12:54 +00007150 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007151 NL80211_CMD_GET_WOWLAN);
7152 if (!hdr)
7153 goto nla_put_failure;
7154
7155 if (rdev->wowlan) {
7156 struct nlattr *nl_wowlan;
7157
7158 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7159 if (!nl_wowlan)
7160 goto nla_put_failure;
7161
David S. Miller9360ffd2012-03-29 04:41:26 -04007162 if ((rdev->wowlan->any &&
7163 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7164 (rdev->wowlan->disconnect &&
7165 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7166 (rdev->wowlan->magic_pkt &&
7167 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7168 (rdev->wowlan->gtk_rekey_failure &&
7169 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7170 (rdev->wowlan->eap_identity_req &&
7171 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7172 (rdev->wowlan->four_way_handshake &&
7173 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7174 (rdev->wowlan->rfkill_release &&
7175 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7176 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007177
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007178 if (nl80211_send_wowlan_patterns(msg, rdev))
7179 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007180
7181 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7182 goto nla_put_failure;
7183
Johannes Bergff1b6e62011-05-04 15:37:28 +02007184 nla_nest_end(msg, nl_wowlan);
7185 }
7186
7187 genlmsg_end(msg, hdr);
7188 return genlmsg_reply(msg, info);
7189
7190nla_put_failure:
7191 nlmsg_free(msg);
7192 return -ENOBUFS;
7193}
7194
Johannes Berg2a0e0472013-01-23 22:57:40 +01007195static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7196 struct nlattr *attr,
7197 struct cfg80211_wowlan *trig)
7198{
7199 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7200 struct cfg80211_wowlan_tcp *cfg;
7201 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7202 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7203 u32 size;
7204 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7205 int err, port;
7206
7207 if (!rdev->wiphy.wowlan.tcp)
7208 return -EINVAL;
7209
7210 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7211 nla_data(attr), nla_len(attr),
7212 nl80211_wowlan_tcp_policy);
7213 if (err)
7214 return err;
7215
7216 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7217 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7218 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7219 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7220 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7221 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7222 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7223 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7224 return -EINVAL;
7225
7226 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7227 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7228 return -EINVAL;
7229
7230 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7231 rdev->wiphy.wowlan.tcp->data_interval_max)
7232 return -EINVAL;
7233
7234 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7235 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7236 return -EINVAL;
7237
7238 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7239 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7240 return -EINVAL;
7241
7242 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7243 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7244
7245 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7246 tokens_size = tokln - sizeof(*tok);
7247
7248 if (!tok->len || tokens_size % tok->len)
7249 return -EINVAL;
7250 if (!rdev->wiphy.wowlan.tcp->tok)
7251 return -EINVAL;
7252 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7253 return -EINVAL;
7254 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7255 return -EINVAL;
7256 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7257 return -EINVAL;
7258 if (tok->offset + tok->len > data_size)
7259 return -EINVAL;
7260 }
7261
7262 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7263 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7264 if (!rdev->wiphy.wowlan.tcp->seq)
7265 return -EINVAL;
7266 if (seq->len == 0 || seq->len > 4)
7267 return -EINVAL;
7268 if (seq->len + seq->offset > data_size)
7269 return -EINVAL;
7270 }
7271
7272 size = sizeof(*cfg);
7273 size += data_size;
7274 size += wake_size + wake_mask_size;
7275 size += tokens_size;
7276
7277 cfg = kzalloc(size, GFP_KERNEL);
7278 if (!cfg)
7279 return -ENOMEM;
7280 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7281 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7282 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7283 ETH_ALEN);
7284 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7285 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7286 else
7287 port = 0;
7288#ifdef CONFIG_INET
7289 /* allocate a socket and port for it and use it */
7290 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7291 IPPROTO_TCP, &cfg->sock, 1);
7292 if (err) {
7293 kfree(cfg);
7294 return err;
7295 }
7296 if (inet_csk_get_port(cfg->sock->sk, port)) {
7297 sock_release(cfg->sock);
7298 kfree(cfg);
7299 return -EADDRINUSE;
7300 }
7301 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7302#else
7303 if (!port) {
7304 kfree(cfg);
7305 return -EINVAL;
7306 }
7307 cfg->src_port = port;
7308#endif
7309
7310 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7311 cfg->payload_len = data_size;
7312 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7313 memcpy((void *)cfg->payload,
7314 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7315 data_size);
7316 if (seq)
7317 cfg->payload_seq = *seq;
7318 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7319 cfg->wake_len = wake_size;
7320 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7321 memcpy((void *)cfg->wake_data,
7322 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7323 wake_size);
7324 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7325 data_size + wake_size;
7326 memcpy((void *)cfg->wake_mask,
7327 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7328 wake_mask_size);
7329 if (tok) {
7330 cfg->tokens_size = tokens_size;
7331 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7332 }
7333
7334 trig->tcp = cfg;
7335
7336 return 0;
7337}
7338
Johannes Bergff1b6e62011-05-04 15:37:28 +02007339static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7340{
7341 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7342 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007343 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007344 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007345 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7346 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007347 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007348
Johannes Berg2a0e0472013-01-23 22:57:40 +01007349 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7350 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007351 return -EOPNOTSUPP;
7352
Johannes Bergae33bd82012-07-12 16:25:02 +02007353 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7354 cfg80211_rdev_free_wowlan(rdev);
7355 rdev->wowlan = NULL;
7356 goto set_wakeup;
7357 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007358
7359 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7360 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7361 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7362 nl80211_wowlan_policy);
7363 if (err)
7364 return err;
7365
7366 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7367 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7368 return -EINVAL;
7369 new_triggers.any = true;
7370 }
7371
7372 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7373 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7374 return -EINVAL;
7375 new_triggers.disconnect = true;
7376 }
7377
7378 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7379 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7380 return -EINVAL;
7381 new_triggers.magic_pkt = true;
7382 }
7383
Johannes Berg77dbbb12011-07-13 10:48:55 +02007384 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7385 return -EINVAL;
7386
7387 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7388 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7389 return -EINVAL;
7390 new_triggers.gtk_rekey_failure = true;
7391 }
7392
7393 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7394 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7395 return -EINVAL;
7396 new_triggers.eap_identity_req = true;
7397 }
7398
7399 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7400 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7401 return -EINVAL;
7402 new_triggers.four_way_handshake = true;
7403 }
7404
7405 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7406 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7407 return -EINVAL;
7408 new_triggers.rfkill_release = true;
7409 }
7410
Johannes Bergff1b6e62011-05-04 15:37:28 +02007411 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7412 struct nlattr *pat;
7413 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007414 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007415 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7416
7417 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7418 rem)
7419 n_patterns++;
7420 if (n_patterns > wowlan->n_patterns)
7421 return -EINVAL;
7422
7423 new_triggers.patterns = kcalloc(n_patterns,
7424 sizeof(new_triggers.patterns[0]),
7425 GFP_KERNEL);
7426 if (!new_triggers.patterns)
7427 return -ENOMEM;
7428
7429 new_triggers.n_patterns = n_patterns;
7430 i = 0;
7431
7432 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7433 rem) {
7434 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7435 nla_data(pat), nla_len(pat), NULL);
7436 err = -EINVAL;
7437 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7438 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7439 goto error;
7440 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7441 mask_len = DIV_ROUND_UP(pat_len, 8);
7442 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7443 mask_len)
7444 goto error;
7445 if (pat_len > wowlan->pattern_max_len ||
7446 pat_len < wowlan->pattern_min_len)
7447 goto error;
7448
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007449 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7450 pkt_offset = 0;
7451 else
7452 pkt_offset = nla_get_u32(
7453 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7454 if (pkt_offset > wowlan->max_pkt_offset)
7455 goto error;
7456 new_triggers.patterns[i].pkt_offset = pkt_offset;
7457
Johannes Bergff1b6e62011-05-04 15:37:28 +02007458 new_triggers.patterns[i].mask =
7459 kmalloc(mask_len + pat_len, GFP_KERNEL);
7460 if (!new_triggers.patterns[i].mask) {
7461 err = -ENOMEM;
7462 goto error;
7463 }
7464 new_triggers.patterns[i].pattern =
7465 new_triggers.patterns[i].mask + mask_len;
7466 memcpy(new_triggers.patterns[i].mask,
7467 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7468 mask_len);
7469 new_triggers.patterns[i].pattern_len = pat_len;
7470 memcpy(new_triggers.patterns[i].pattern,
7471 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7472 pat_len);
7473 i++;
7474 }
7475 }
7476
Johannes Berg2a0e0472013-01-23 22:57:40 +01007477 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7478 err = nl80211_parse_wowlan_tcp(
7479 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7480 &new_triggers);
7481 if (err)
7482 goto error;
7483 }
7484
Johannes Bergae33bd82012-07-12 16:25:02 +02007485 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7486 if (!ntrig) {
7487 err = -ENOMEM;
7488 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007489 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007490 cfg80211_rdev_free_wowlan(rdev);
7491 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007492
Johannes Bergae33bd82012-07-12 16:25:02 +02007493 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007494 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007495 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007496
Johannes Bergff1b6e62011-05-04 15:37:28 +02007497 return 0;
7498 error:
7499 for (i = 0; i < new_triggers.n_patterns; i++)
7500 kfree(new_triggers.patterns[i].mask);
7501 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007502 if (new_triggers.tcp && new_triggers.tcp->sock)
7503 sock_release(new_triggers.tcp->sock);
7504 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007505 return err;
7506}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007507#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007508
Johannes Berge5497d72011-07-05 16:35:40 +02007509static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7510{
7511 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7512 struct net_device *dev = info->user_ptr[1];
7513 struct wireless_dev *wdev = dev->ieee80211_ptr;
7514 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7515 struct cfg80211_gtk_rekey_data rekey_data;
7516 int err;
7517
7518 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7519 return -EINVAL;
7520
7521 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7522 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7523 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7524 nl80211_rekey_policy);
7525 if (err)
7526 return err;
7527
7528 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7529 return -ERANGE;
7530 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7531 return -ERANGE;
7532 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7533 return -ERANGE;
7534
7535 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7536 NL80211_KEK_LEN);
7537 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7538 NL80211_KCK_LEN);
7539 memcpy(rekey_data.replay_ctr,
7540 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7541 NL80211_REPLAY_CTR_LEN);
7542
7543 wdev_lock(wdev);
7544 if (!wdev->current_bss) {
7545 err = -ENOTCONN;
7546 goto out;
7547 }
7548
7549 if (!rdev->ops->set_rekey_data) {
7550 err = -EOPNOTSUPP;
7551 goto out;
7552 }
7553
Hila Gonene35e4d22012-06-27 17:19:42 +03007554 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007555 out:
7556 wdev_unlock(wdev);
7557 return err;
7558}
7559
Johannes Berg28946da2011-11-04 11:18:12 +01007560static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7561 struct genl_info *info)
7562{
7563 struct net_device *dev = info->user_ptr[1];
7564 struct wireless_dev *wdev = dev->ieee80211_ptr;
7565
7566 if (wdev->iftype != NL80211_IFTYPE_AP &&
7567 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7568 return -EINVAL;
7569
Eric W. Biederman15e47302012-09-07 20:12:54 +00007570 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007571 return -EBUSY;
7572
Eric W. Biederman15e47302012-09-07 20:12:54 +00007573 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007574 return 0;
7575}
7576
Johannes Berg7f6cf312011-11-04 11:18:15 +01007577static int nl80211_probe_client(struct sk_buff *skb,
7578 struct genl_info *info)
7579{
7580 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7581 struct net_device *dev = info->user_ptr[1];
7582 struct wireless_dev *wdev = dev->ieee80211_ptr;
7583 struct sk_buff *msg;
7584 void *hdr;
7585 const u8 *addr;
7586 u64 cookie;
7587 int err;
7588
7589 if (wdev->iftype != NL80211_IFTYPE_AP &&
7590 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7591 return -EOPNOTSUPP;
7592
7593 if (!info->attrs[NL80211_ATTR_MAC])
7594 return -EINVAL;
7595
7596 if (!rdev->ops->probe_client)
7597 return -EOPNOTSUPP;
7598
7599 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7600 if (!msg)
7601 return -ENOMEM;
7602
Eric W. Biederman15e47302012-09-07 20:12:54 +00007603 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007604 NL80211_CMD_PROBE_CLIENT);
7605
7606 if (IS_ERR(hdr)) {
7607 err = PTR_ERR(hdr);
7608 goto free_msg;
7609 }
7610
7611 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7612
Hila Gonene35e4d22012-06-27 17:19:42 +03007613 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007614 if (err)
7615 goto free_msg;
7616
David S. Miller9360ffd2012-03-29 04:41:26 -04007617 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7618 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007619
7620 genlmsg_end(msg, hdr);
7621
7622 return genlmsg_reply(msg, info);
7623
7624 nla_put_failure:
7625 err = -ENOBUFS;
7626 free_msg:
7627 nlmsg_free(msg);
7628 return err;
7629}
7630
Johannes Berg5e760232011-11-04 11:18:17 +01007631static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7632{
7633 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007634 struct cfg80211_beacon_registration *reg, *nreg;
7635 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007636
7637 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7638 return -EOPNOTSUPP;
7639
Ben Greear37c73b52012-10-26 14:49:25 -07007640 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7641 if (!nreg)
7642 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007643
Ben Greear37c73b52012-10-26 14:49:25 -07007644 /* First, check if already registered. */
7645 spin_lock_bh(&rdev->beacon_registrations_lock);
7646 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7647 if (reg->nlportid == info->snd_portid) {
7648 rv = -EALREADY;
7649 goto out_err;
7650 }
7651 }
7652 /* Add it to the list */
7653 nreg->nlportid = info->snd_portid;
7654 list_add(&nreg->list, &rdev->beacon_registrations);
7655
7656 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007657
7658 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007659out_err:
7660 spin_unlock_bh(&rdev->beacon_registrations_lock);
7661 kfree(nreg);
7662 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007663}
7664
Johannes Berg98104fde2012-06-16 00:19:54 +02007665static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7666{
7667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7668 struct wireless_dev *wdev = info->user_ptr[1];
7669 int err;
7670
7671 if (!rdev->ops->start_p2p_device)
7672 return -EOPNOTSUPP;
7673
7674 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7675 return -EOPNOTSUPP;
7676
7677 if (wdev->p2p_started)
7678 return 0;
7679
7680 mutex_lock(&rdev->devlist_mtx);
7681 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7682 mutex_unlock(&rdev->devlist_mtx);
7683 if (err)
7684 return err;
7685
Johannes Bergeeb126e2012-10-23 15:16:50 +02007686 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007687 if (err)
7688 return err;
7689
7690 wdev->p2p_started = true;
7691 mutex_lock(&rdev->devlist_mtx);
7692 rdev->opencount++;
7693 mutex_unlock(&rdev->devlist_mtx);
7694
7695 return 0;
7696}
7697
7698static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7699{
7700 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7701 struct wireless_dev *wdev = info->user_ptr[1];
7702
7703 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7704 return -EOPNOTSUPP;
7705
7706 if (!rdev->ops->stop_p2p_device)
7707 return -EOPNOTSUPP;
7708
7709 if (!wdev->p2p_started)
7710 return 0;
7711
Johannes Bergeeb126e2012-10-23 15:16:50 +02007712 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007713 wdev->p2p_started = false;
7714
7715 mutex_lock(&rdev->devlist_mtx);
7716 rdev->opencount--;
7717 mutex_unlock(&rdev->devlist_mtx);
7718
7719 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7720 rdev->scan_req->aborted = true;
7721 ___cfg80211_scan_done(rdev, true);
7722 }
7723
7724 return 0;
7725}
7726
Johannes Berg4c476992010-10-04 21:36:35 +02007727#define NL80211_FLAG_NEED_WIPHY 0x01
7728#define NL80211_FLAG_NEED_NETDEV 0x02
7729#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007730#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7731#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7732 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007733#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007734/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007735#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7736 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007737
7738static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7739 struct genl_info *info)
7740{
7741 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007742 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007743 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007744 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7745
7746 if (rtnl)
7747 rtnl_lock();
7748
7749 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007750 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007751 if (IS_ERR(rdev)) {
7752 if (rtnl)
7753 rtnl_unlock();
7754 return PTR_ERR(rdev);
7755 }
7756 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007757 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7758 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007759 mutex_lock(&cfg80211_mutex);
7760 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7761 info->attrs);
7762 if (IS_ERR(wdev)) {
7763 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007764 if (rtnl)
7765 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007766 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007767 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007768
Johannes Berg89a54e42012-06-15 14:33:17 +02007769 dev = wdev->netdev;
7770 rdev = wiphy_to_dev(wdev->wiphy);
7771
Johannes Berg1bf614e2012-06-15 15:23:36 +02007772 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7773 if (!dev) {
7774 mutex_unlock(&cfg80211_mutex);
7775 if (rtnl)
7776 rtnl_unlock();
7777 return -EINVAL;
7778 }
7779
7780 info->user_ptr[1] = dev;
7781 } else {
7782 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007783 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007784
Johannes Berg1bf614e2012-06-15 15:23:36 +02007785 if (dev) {
7786 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7787 !netif_running(dev)) {
7788 mutex_unlock(&cfg80211_mutex);
7789 if (rtnl)
7790 rtnl_unlock();
7791 return -ENETDOWN;
7792 }
7793
7794 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007795 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7796 if (!wdev->p2p_started) {
7797 mutex_unlock(&cfg80211_mutex);
7798 if (rtnl)
7799 rtnl_unlock();
7800 return -ENETDOWN;
7801 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007802 }
7803
Johannes Berg89a54e42012-06-15 14:33:17 +02007804 cfg80211_lock_rdev(rdev);
7805
7806 mutex_unlock(&cfg80211_mutex);
7807
Johannes Berg4c476992010-10-04 21:36:35 +02007808 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007809 }
7810
7811 return 0;
7812}
7813
7814static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7815 struct genl_info *info)
7816{
7817 if (info->user_ptr[0])
7818 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007819 if (info->user_ptr[1]) {
7820 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7821 struct wireless_dev *wdev = info->user_ptr[1];
7822
7823 if (wdev->netdev)
7824 dev_put(wdev->netdev);
7825 } else {
7826 dev_put(info->user_ptr[1]);
7827 }
7828 }
Johannes Berg4c476992010-10-04 21:36:35 +02007829 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7830 rtnl_unlock();
7831}
7832
Johannes Berg55682962007-09-20 13:09:35 -04007833static struct genl_ops nl80211_ops[] = {
7834 {
7835 .cmd = NL80211_CMD_GET_WIPHY,
7836 .doit = nl80211_get_wiphy,
7837 .dumpit = nl80211_dump_wiphy,
7838 .policy = nl80211_policy,
7839 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007840 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007841 },
7842 {
7843 .cmd = NL80211_CMD_SET_WIPHY,
7844 .doit = nl80211_set_wiphy,
7845 .policy = nl80211_policy,
7846 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007847 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007848 },
7849 {
7850 .cmd = NL80211_CMD_GET_INTERFACE,
7851 .doit = nl80211_get_interface,
7852 .dumpit = nl80211_dump_interface,
7853 .policy = nl80211_policy,
7854 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007855 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007856 },
7857 {
7858 .cmd = NL80211_CMD_SET_INTERFACE,
7859 .doit = nl80211_set_interface,
7860 .policy = nl80211_policy,
7861 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007862 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7863 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007864 },
7865 {
7866 .cmd = NL80211_CMD_NEW_INTERFACE,
7867 .doit = nl80211_new_interface,
7868 .policy = nl80211_policy,
7869 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007870 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7871 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007872 },
7873 {
7874 .cmd = NL80211_CMD_DEL_INTERFACE,
7875 .doit = nl80211_del_interface,
7876 .policy = nl80211_policy,
7877 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007878 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007879 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007880 },
Johannes Berg41ade002007-12-19 02:03:29 +01007881 {
7882 .cmd = NL80211_CMD_GET_KEY,
7883 .doit = nl80211_get_key,
7884 .policy = nl80211_policy,
7885 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007886 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007887 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007888 },
7889 {
7890 .cmd = NL80211_CMD_SET_KEY,
7891 .doit = nl80211_set_key,
7892 .policy = nl80211_policy,
7893 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007894 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007895 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007896 },
7897 {
7898 .cmd = NL80211_CMD_NEW_KEY,
7899 .doit = nl80211_new_key,
7900 .policy = nl80211_policy,
7901 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007902 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007903 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007904 },
7905 {
7906 .cmd = NL80211_CMD_DEL_KEY,
7907 .doit = nl80211_del_key,
7908 .policy = nl80211_policy,
7909 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007910 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007911 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007912 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007913 {
7914 .cmd = NL80211_CMD_SET_BEACON,
7915 .policy = nl80211_policy,
7916 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007917 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007918 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007919 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007920 },
7921 {
Johannes Berg88600202012-02-13 15:17:18 +01007922 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007923 .policy = nl80211_policy,
7924 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007925 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007926 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007927 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007928 },
7929 {
Johannes Berg88600202012-02-13 15:17:18 +01007930 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007931 .policy = nl80211_policy,
7932 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007933 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007934 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007935 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007936 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007937 {
7938 .cmd = NL80211_CMD_GET_STATION,
7939 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007940 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007941 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007942 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7943 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007944 },
7945 {
7946 .cmd = NL80211_CMD_SET_STATION,
7947 .doit = nl80211_set_station,
7948 .policy = nl80211_policy,
7949 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007950 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007951 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007952 },
7953 {
7954 .cmd = NL80211_CMD_NEW_STATION,
7955 .doit = nl80211_new_station,
7956 .policy = nl80211_policy,
7957 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007958 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007959 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007960 },
7961 {
7962 .cmd = NL80211_CMD_DEL_STATION,
7963 .doit = nl80211_del_station,
7964 .policy = nl80211_policy,
7965 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007966 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007967 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007968 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007969 {
7970 .cmd = NL80211_CMD_GET_MPATH,
7971 .doit = nl80211_get_mpath,
7972 .dumpit = nl80211_dump_mpath,
7973 .policy = nl80211_policy,
7974 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007975 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007976 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007977 },
7978 {
7979 .cmd = NL80211_CMD_SET_MPATH,
7980 .doit = nl80211_set_mpath,
7981 .policy = nl80211_policy,
7982 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007983 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007984 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007985 },
7986 {
7987 .cmd = NL80211_CMD_NEW_MPATH,
7988 .doit = nl80211_new_mpath,
7989 .policy = nl80211_policy,
7990 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007991 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007992 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007993 },
7994 {
7995 .cmd = NL80211_CMD_DEL_MPATH,
7996 .doit = nl80211_del_mpath,
7997 .policy = nl80211_policy,
7998 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007999 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008000 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008001 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008002 {
8003 .cmd = NL80211_CMD_SET_BSS,
8004 .doit = nl80211_set_bss,
8005 .policy = nl80211_policy,
8006 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008007 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008008 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008009 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008010 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008011 .cmd = NL80211_CMD_GET_REG,
8012 .doit = nl80211_get_reg,
8013 .policy = nl80211_policy,
8014 /* can be retrieved by unprivileged users */
8015 },
8016 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008017 .cmd = NL80211_CMD_SET_REG,
8018 .doit = nl80211_set_reg,
8019 .policy = nl80211_policy,
8020 .flags = GENL_ADMIN_PERM,
8021 },
8022 {
8023 .cmd = NL80211_CMD_REQ_SET_REG,
8024 .doit = nl80211_req_set_reg,
8025 .policy = nl80211_policy,
8026 .flags = GENL_ADMIN_PERM,
8027 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008028 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008029 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8030 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008031 .policy = nl80211_policy,
8032 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008033 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008034 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008035 },
8036 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008037 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8038 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008039 .policy = nl80211_policy,
8040 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008041 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008042 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008043 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008044 {
Johannes Berg2a519312009-02-10 21:25:55 +01008045 .cmd = NL80211_CMD_TRIGGER_SCAN,
8046 .doit = nl80211_trigger_scan,
8047 .policy = nl80211_policy,
8048 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008049 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008050 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008051 },
8052 {
8053 .cmd = NL80211_CMD_GET_SCAN,
8054 .policy = nl80211_policy,
8055 .dumpit = nl80211_dump_scan,
8056 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008057 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008058 .cmd = NL80211_CMD_START_SCHED_SCAN,
8059 .doit = nl80211_start_sched_scan,
8060 .policy = nl80211_policy,
8061 .flags = GENL_ADMIN_PERM,
8062 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8063 NL80211_FLAG_NEED_RTNL,
8064 },
8065 {
8066 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8067 .doit = nl80211_stop_sched_scan,
8068 .policy = nl80211_policy,
8069 .flags = GENL_ADMIN_PERM,
8070 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8071 NL80211_FLAG_NEED_RTNL,
8072 },
8073 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008074 .cmd = NL80211_CMD_AUTHENTICATE,
8075 .doit = nl80211_authenticate,
8076 .policy = nl80211_policy,
8077 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008078 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008079 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008080 },
8081 {
8082 .cmd = NL80211_CMD_ASSOCIATE,
8083 .doit = nl80211_associate,
8084 .policy = nl80211_policy,
8085 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008086 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008087 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008088 },
8089 {
8090 .cmd = NL80211_CMD_DEAUTHENTICATE,
8091 .doit = nl80211_deauthenticate,
8092 .policy = nl80211_policy,
8093 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008094 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008095 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008096 },
8097 {
8098 .cmd = NL80211_CMD_DISASSOCIATE,
8099 .doit = nl80211_disassociate,
8100 .policy = nl80211_policy,
8101 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008102 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008103 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008104 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008105 {
8106 .cmd = NL80211_CMD_JOIN_IBSS,
8107 .doit = nl80211_join_ibss,
8108 .policy = nl80211_policy,
8109 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008110 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008111 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008112 },
8113 {
8114 .cmd = NL80211_CMD_LEAVE_IBSS,
8115 .doit = nl80211_leave_ibss,
8116 .policy = nl80211_policy,
8117 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008118 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008119 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008120 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008121#ifdef CONFIG_NL80211_TESTMODE
8122 {
8123 .cmd = NL80211_CMD_TESTMODE,
8124 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008125 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008126 .policy = nl80211_policy,
8127 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008128 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8129 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008130 },
8131#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008132 {
8133 .cmd = NL80211_CMD_CONNECT,
8134 .doit = nl80211_connect,
8135 .policy = nl80211_policy,
8136 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008137 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008138 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008139 },
8140 {
8141 .cmd = NL80211_CMD_DISCONNECT,
8142 .doit = nl80211_disconnect,
8143 .policy = nl80211_policy,
8144 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008145 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008146 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008147 },
Johannes Berg463d0182009-07-14 00:33:35 +02008148 {
8149 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8150 .doit = nl80211_wiphy_netns,
8151 .policy = nl80211_policy,
8152 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008153 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8154 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008155 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008156 {
8157 .cmd = NL80211_CMD_GET_SURVEY,
8158 .policy = nl80211_policy,
8159 .dumpit = nl80211_dump_survey,
8160 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008161 {
8162 .cmd = NL80211_CMD_SET_PMKSA,
8163 .doit = nl80211_setdel_pmksa,
8164 .policy = nl80211_policy,
8165 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008166 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008167 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008168 },
8169 {
8170 .cmd = NL80211_CMD_DEL_PMKSA,
8171 .doit = nl80211_setdel_pmksa,
8172 .policy = nl80211_policy,
8173 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008174 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008175 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008176 },
8177 {
8178 .cmd = NL80211_CMD_FLUSH_PMKSA,
8179 .doit = nl80211_flush_pmksa,
8180 .policy = nl80211_policy,
8181 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008182 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008183 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008184 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008185 {
8186 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8187 .doit = nl80211_remain_on_channel,
8188 .policy = nl80211_policy,
8189 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008190 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008191 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008192 },
8193 {
8194 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8195 .doit = nl80211_cancel_remain_on_channel,
8196 .policy = nl80211_policy,
8197 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008198 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008199 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008200 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008201 {
8202 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8203 .doit = nl80211_set_tx_bitrate_mask,
8204 .policy = nl80211_policy,
8205 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008206 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8207 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008208 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008209 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008210 .cmd = NL80211_CMD_REGISTER_FRAME,
8211 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008212 .policy = nl80211_policy,
8213 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008214 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008215 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008216 },
8217 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008218 .cmd = NL80211_CMD_FRAME,
8219 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008220 .policy = nl80211_policy,
8221 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008222 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008223 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008224 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008225 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008226 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8227 .doit = nl80211_tx_mgmt_cancel_wait,
8228 .policy = nl80211_policy,
8229 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008230 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008231 NL80211_FLAG_NEED_RTNL,
8232 },
8233 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008234 .cmd = NL80211_CMD_SET_POWER_SAVE,
8235 .doit = nl80211_set_power_save,
8236 .policy = nl80211_policy,
8237 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008238 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8239 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008240 },
8241 {
8242 .cmd = NL80211_CMD_GET_POWER_SAVE,
8243 .doit = nl80211_get_power_save,
8244 .policy = nl80211_policy,
8245 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008246 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8247 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008248 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008249 {
8250 .cmd = NL80211_CMD_SET_CQM,
8251 .doit = nl80211_set_cqm,
8252 .policy = nl80211_policy,
8253 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008254 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8255 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008256 },
Johannes Bergf444de02010-05-05 15:25:02 +02008257 {
8258 .cmd = NL80211_CMD_SET_CHANNEL,
8259 .doit = nl80211_set_channel,
8260 .policy = nl80211_policy,
8261 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008262 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8263 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008264 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008265 {
8266 .cmd = NL80211_CMD_SET_WDS_PEER,
8267 .doit = nl80211_set_wds_peer,
8268 .policy = nl80211_policy,
8269 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008270 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8271 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008272 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008273 {
8274 .cmd = NL80211_CMD_JOIN_MESH,
8275 .doit = nl80211_join_mesh,
8276 .policy = nl80211_policy,
8277 .flags = GENL_ADMIN_PERM,
8278 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8279 NL80211_FLAG_NEED_RTNL,
8280 },
8281 {
8282 .cmd = NL80211_CMD_LEAVE_MESH,
8283 .doit = nl80211_leave_mesh,
8284 .policy = nl80211_policy,
8285 .flags = GENL_ADMIN_PERM,
8286 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8287 NL80211_FLAG_NEED_RTNL,
8288 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008289#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008290 {
8291 .cmd = NL80211_CMD_GET_WOWLAN,
8292 .doit = nl80211_get_wowlan,
8293 .policy = nl80211_policy,
8294 /* can be retrieved by unprivileged users */
8295 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8296 NL80211_FLAG_NEED_RTNL,
8297 },
8298 {
8299 .cmd = NL80211_CMD_SET_WOWLAN,
8300 .doit = nl80211_set_wowlan,
8301 .policy = nl80211_policy,
8302 .flags = GENL_ADMIN_PERM,
8303 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8304 NL80211_FLAG_NEED_RTNL,
8305 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008306#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008307 {
8308 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8309 .doit = nl80211_set_rekey_data,
8310 .policy = nl80211_policy,
8311 .flags = GENL_ADMIN_PERM,
8312 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8313 NL80211_FLAG_NEED_RTNL,
8314 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008315 {
8316 .cmd = NL80211_CMD_TDLS_MGMT,
8317 .doit = nl80211_tdls_mgmt,
8318 .policy = nl80211_policy,
8319 .flags = GENL_ADMIN_PERM,
8320 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8321 NL80211_FLAG_NEED_RTNL,
8322 },
8323 {
8324 .cmd = NL80211_CMD_TDLS_OPER,
8325 .doit = nl80211_tdls_oper,
8326 .policy = nl80211_policy,
8327 .flags = GENL_ADMIN_PERM,
8328 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8329 NL80211_FLAG_NEED_RTNL,
8330 },
Johannes Berg28946da2011-11-04 11:18:12 +01008331 {
8332 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8333 .doit = nl80211_register_unexpected_frame,
8334 .policy = nl80211_policy,
8335 .flags = GENL_ADMIN_PERM,
8336 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8337 NL80211_FLAG_NEED_RTNL,
8338 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008339 {
8340 .cmd = NL80211_CMD_PROBE_CLIENT,
8341 .doit = nl80211_probe_client,
8342 .policy = nl80211_policy,
8343 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008344 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008345 NL80211_FLAG_NEED_RTNL,
8346 },
Johannes Berg5e760232011-11-04 11:18:17 +01008347 {
8348 .cmd = NL80211_CMD_REGISTER_BEACONS,
8349 .doit = nl80211_register_beacons,
8350 .policy = nl80211_policy,
8351 .flags = GENL_ADMIN_PERM,
8352 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8353 NL80211_FLAG_NEED_RTNL,
8354 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008355 {
8356 .cmd = NL80211_CMD_SET_NOACK_MAP,
8357 .doit = nl80211_set_noack_map,
8358 .policy = nl80211_policy,
8359 .flags = GENL_ADMIN_PERM,
8360 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8361 NL80211_FLAG_NEED_RTNL,
8362 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008363 {
8364 .cmd = NL80211_CMD_START_P2P_DEVICE,
8365 .doit = nl80211_start_p2p_device,
8366 .policy = nl80211_policy,
8367 .flags = GENL_ADMIN_PERM,
8368 .internal_flags = NL80211_FLAG_NEED_WDEV |
8369 NL80211_FLAG_NEED_RTNL,
8370 },
8371 {
8372 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8373 .doit = nl80211_stop_p2p_device,
8374 .policy = nl80211_policy,
8375 .flags = GENL_ADMIN_PERM,
8376 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8377 NL80211_FLAG_NEED_RTNL,
8378 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008379 {
8380 .cmd = NL80211_CMD_SET_MCAST_RATE,
8381 .doit = nl80211_set_mcast_rate,
8382 .policy = nl80211_policy,
8383 .flags = GENL_ADMIN_PERM,
8384 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8385 NL80211_FLAG_NEED_RTNL,
8386 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308387 {
8388 .cmd = NL80211_CMD_SET_MAC_ACL,
8389 .doit = nl80211_set_mac_acl,
8390 .policy = nl80211_policy,
8391 .flags = GENL_ADMIN_PERM,
8392 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8393 NL80211_FLAG_NEED_RTNL,
8394 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008395 {
8396 .cmd = NL80211_CMD_RADAR_DETECT,
8397 .doit = nl80211_start_radar_detection,
8398 .policy = nl80211_policy,
8399 .flags = GENL_ADMIN_PERM,
8400 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8401 NL80211_FLAG_NEED_RTNL,
8402 },
Johannes Berg55682962007-09-20 13:09:35 -04008403};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008404
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008405static struct genl_multicast_group nl80211_mlme_mcgrp = {
8406 .name = "mlme",
8407};
Johannes Berg55682962007-09-20 13:09:35 -04008408
8409/* multicast groups */
8410static struct genl_multicast_group nl80211_config_mcgrp = {
8411 .name = "config",
8412};
Johannes Berg2a519312009-02-10 21:25:55 +01008413static struct genl_multicast_group nl80211_scan_mcgrp = {
8414 .name = "scan",
8415};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008416static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8417 .name = "regulatory",
8418};
Johannes Berg55682962007-09-20 13:09:35 -04008419
8420/* notification functions */
8421
8422void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8423{
8424 struct sk_buff *msg;
8425
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008426 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008427 if (!msg)
8428 return;
8429
8430 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
8431 nlmsg_free(msg);
8432 return;
8433 }
8434
Johannes Berg463d0182009-07-14 00:33:35 +02008435 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8436 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008437}
8438
Johannes Berg362a4152009-05-24 16:43:15 +02008439static int nl80211_add_scan_req(struct sk_buff *msg,
8440 struct cfg80211_registered_device *rdev)
8441{
8442 struct cfg80211_scan_request *req = rdev->scan_req;
8443 struct nlattr *nest;
8444 int i;
8445
Johannes Berg667503dd2009-07-07 03:56:11 +02008446 ASSERT_RDEV_LOCK(rdev);
8447
Johannes Berg362a4152009-05-24 16:43:15 +02008448 if (WARN_ON(!req))
8449 return 0;
8450
8451 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8452 if (!nest)
8453 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008454 for (i = 0; i < req->n_ssids; i++) {
8455 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8456 goto nla_put_failure;
8457 }
Johannes Berg362a4152009-05-24 16:43:15 +02008458 nla_nest_end(msg, nest);
8459
8460 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8461 if (!nest)
8462 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008463 for (i = 0; i < req->n_channels; i++) {
8464 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8465 goto nla_put_failure;
8466 }
Johannes Berg362a4152009-05-24 16:43:15 +02008467 nla_nest_end(msg, nest);
8468
David S. Miller9360ffd2012-03-29 04:41:26 -04008469 if (req->ie &&
8470 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8471 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008472
Sam Lefflered4737712012-10-11 21:03:31 -07008473 if (req->flags)
8474 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8475
Johannes Berg362a4152009-05-24 16:43:15 +02008476 return 0;
8477 nla_put_failure:
8478 return -ENOBUFS;
8479}
8480
Johannes Berga538e2d2009-06-16 19:56:42 +02008481static int nl80211_send_scan_msg(struct sk_buff *msg,
8482 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008483 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008484 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008485 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008486{
8487 void *hdr;
8488
Eric W. Biederman15e47302012-09-07 20:12:54 +00008489 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008490 if (!hdr)
8491 return -1;
8492
David S. Miller9360ffd2012-03-29 04:41:26 -04008493 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008494 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8495 wdev->netdev->ifindex)) ||
8496 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008497 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008498
Johannes Berg362a4152009-05-24 16:43:15 +02008499 /* ignore errors and send incomplete event anyway */
8500 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008501
8502 return genlmsg_end(msg, hdr);
8503
8504 nla_put_failure:
8505 genlmsg_cancel(msg, hdr);
8506 return -EMSGSIZE;
8507}
8508
Luciano Coelho807f8a82011-05-11 17:09:35 +03008509static int
8510nl80211_send_sched_scan_msg(struct sk_buff *msg,
8511 struct cfg80211_registered_device *rdev,
8512 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008513 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008514{
8515 void *hdr;
8516
Eric W. Biederman15e47302012-09-07 20:12:54 +00008517 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008518 if (!hdr)
8519 return -1;
8520
David S. Miller9360ffd2012-03-29 04:41:26 -04008521 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8522 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8523 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008524
8525 return genlmsg_end(msg, hdr);
8526
8527 nla_put_failure:
8528 genlmsg_cancel(msg, hdr);
8529 return -EMSGSIZE;
8530}
8531
Johannes Berga538e2d2009-06-16 19:56:42 +02008532void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008533 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008534{
8535 struct sk_buff *msg;
8536
Thomas Graf58050fc2012-06-28 03:57:45 +00008537 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008538 if (!msg)
8539 return;
8540
Johannes Bergfd014282012-06-18 19:17:03 +02008541 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008542 NL80211_CMD_TRIGGER_SCAN) < 0) {
8543 nlmsg_free(msg);
8544 return;
8545 }
8546
Johannes Berg463d0182009-07-14 00:33:35 +02008547 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8548 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008549}
8550
Johannes Berg2a519312009-02-10 21:25:55 +01008551void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008552 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008553{
8554 struct sk_buff *msg;
8555
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008557 if (!msg)
8558 return;
8559
Johannes Bergfd014282012-06-18 19:17:03 +02008560 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008561 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008562 nlmsg_free(msg);
8563 return;
8564 }
8565
Johannes Berg463d0182009-07-14 00:33:35 +02008566 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8567 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008568}
8569
8570void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008571 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008572{
8573 struct sk_buff *msg;
8574
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008575 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008576 if (!msg)
8577 return;
8578
Johannes Bergfd014282012-06-18 19:17:03 +02008579 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008580 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008581 nlmsg_free(msg);
8582 return;
8583 }
8584
Johannes Berg463d0182009-07-14 00:33:35 +02008585 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8586 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008587}
8588
Luciano Coelho807f8a82011-05-11 17:09:35 +03008589void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8590 struct net_device *netdev)
8591{
8592 struct sk_buff *msg;
8593
8594 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8595 if (!msg)
8596 return;
8597
8598 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8599 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8600 nlmsg_free(msg);
8601 return;
8602 }
8603
8604 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8605 nl80211_scan_mcgrp.id, GFP_KERNEL);
8606}
8607
8608void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8609 struct net_device *netdev, u32 cmd)
8610{
8611 struct sk_buff *msg;
8612
Thomas Graf58050fc2012-06-28 03:57:45 +00008613 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008614 if (!msg)
8615 return;
8616
8617 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8618 nlmsg_free(msg);
8619 return;
8620 }
8621
8622 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8623 nl80211_scan_mcgrp.id, GFP_KERNEL);
8624}
8625
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008626/*
8627 * This can happen on global regulatory changes or device specific settings
8628 * based on custom world regulatory domains.
8629 */
8630void nl80211_send_reg_change_event(struct regulatory_request *request)
8631{
8632 struct sk_buff *msg;
8633 void *hdr;
8634
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008635 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008636 if (!msg)
8637 return;
8638
8639 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8640 if (!hdr) {
8641 nlmsg_free(msg);
8642 return;
8643 }
8644
8645 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008646 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8647 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008648
David S. Miller9360ffd2012-03-29 04:41:26 -04008649 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8650 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8651 NL80211_REGDOM_TYPE_WORLD))
8652 goto nla_put_failure;
8653 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8654 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8655 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8656 goto nla_put_failure;
8657 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8658 request->intersect) {
8659 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8660 NL80211_REGDOM_TYPE_INTERSECTION))
8661 goto nla_put_failure;
8662 } else {
8663 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8664 NL80211_REGDOM_TYPE_COUNTRY) ||
8665 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8666 request->alpha2))
8667 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008668 }
8669
Johannes Bergf4173762012-12-03 18:23:37 +01008670 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008671 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8672 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008673
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008674 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008675
Johannes Bergbc43b282009-07-25 10:54:13 +02008676 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008677 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008678 GFP_ATOMIC);
8679 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008680
8681 return;
8682
8683nla_put_failure:
8684 genlmsg_cancel(msg, hdr);
8685 nlmsg_free(msg);
8686}
8687
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008688static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8689 struct net_device *netdev,
8690 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008691 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008692{
8693 struct sk_buff *msg;
8694 void *hdr;
8695
Johannes Berge6d6e342009-07-01 21:26:47 +02008696 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008697 if (!msg)
8698 return;
8699
8700 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8701 if (!hdr) {
8702 nlmsg_free(msg);
8703 return;
8704 }
8705
David S. Miller9360ffd2012-03-29 04:41:26 -04008706 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8707 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8708 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8709 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008710
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008711 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008712
Johannes Berg463d0182009-07-14 00:33:35 +02008713 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8714 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008715 return;
8716
8717 nla_put_failure:
8718 genlmsg_cancel(msg, hdr);
8719 nlmsg_free(msg);
8720}
8721
8722void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008723 struct net_device *netdev, const u8 *buf,
8724 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008725{
8726 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008727 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008728}
8729
8730void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8731 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008732 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008733{
Johannes Berge6d6e342009-07-01 21:26:47 +02008734 nl80211_send_mlme_event(rdev, netdev, buf, len,
8735 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008736}
8737
Jouni Malinen53b46b82009-03-27 20:53:56 +02008738void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008739 struct net_device *netdev, const u8 *buf,
8740 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008741{
8742 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008743 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008744}
8745
Jouni Malinen53b46b82009-03-27 20:53:56 +02008746void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8747 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008748 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008749{
8750 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008751 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008752}
8753
Jouni Malinencf4e5942010-12-16 00:52:40 +02008754void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8755 struct net_device *netdev, const u8 *buf,
8756 size_t len, gfp_t gfp)
8757{
8758 nl80211_send_mlme_event(rdev, netdev, buf, len,
8759 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8760}
8761
8762void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8763 struct net_device *netdev, const u8 *buf,
8764 size_t len, gfp_t gfp)
8765{
8766 nl80211_send_mlme_event(rdev, netdev, buf, len,
8767 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8768}
8769
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008770static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8771 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008772 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008773{
8774 struct sk_buff *msg;
8775 void *hdr;
8776
Johannes Berge6d6e342009-07-01 21:26:47 +02008777 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008778 if (!msg)
8779 return;
8780
8781 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8782 if (!hdr) {
8783 nlmsg_free(msg);
8784 return;
8785 }
8786
David S. Miller9360ffd2012-03-29 04:41:26 -04008787 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8788 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8789 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8790 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8791 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008792
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008793 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008794
Johannes Berg463d0182009-07-14 00:33:35 +02008795 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8796 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008797 return;
8798
8799 nla_put_failure:
8800 genlmsg_cancel(msg, hdr);
8801 nlmsg_free(msg);
8802}
8803
8804void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008805 struct net_device *netdev, const u8 *addr,
8806 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008807{
8808 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008809 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008810}
8811
8812void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008813 struct net_device *netdev, const u8 *addr,
8814 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008815{
Johannes Berge6d6e342009-07-01 21:26:47 +02008816 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8817 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008818}
8819
Samuel Ortizb23aa672009-07-01 21:26:54 +02008820void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8821 struct net_device *netdev, const u8 *bssid,
8822 const u8 *req_ie, size_t req_ie_len,
8823 const u8 *resp_ie, size_t resp_ie_len,
8824 u16 status, gfp_t gfp)
8825{
8826 struct sk_buff *msg;
8827 void *hdr;
8828
Thomas Graf58050fc2012-06-28 03:57:45 +00008829 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008830 if (!msg)
8831 return;
8832
8833 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8834 if (!hdr) {
8835 nlmsg_free(msg);
8836 return;
8837 }
8838
David S. Miller9360ffd2012-03-29 04:41:26 -04008839 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8840 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8841 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8842 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8843 (req_ie &&
8844 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8845 (resp_ie &&
8846 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8847 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008848
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008849 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008850
Johannes Berg463d0182009-07-14 00:33:35 +02008851 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8852 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008853 return;
8854
8855 nla_put_failure:
8856 genlmsg_cancel(msg, hdr);
8857 nlmsg_free(msg);
8858
8859}
8860
8861void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8862 struct net_device *netdev, const u8 *bssid,
8863 const u8 *req_ie, size_t req_ie_len,
8864 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8865{
8866 struct sk_buff *msg;
8867 void *hdr;
8868
Thomas Graf58050fc2012-06-28 03:57:45 +00008869 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008870 if (!msg)
8871 return;
8872
8873 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8874 if (!hdr) {
8875 nlmsg_free(msg);
8876 return;
8877 }
8878
David S. Miller9360ffd2012-03-29 04:41:26 -04008879 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8880 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8881 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8882 (req_ie &&
8883 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8884 (resp_ie &&
8885 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8886 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008887
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008888 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008889
Johannes Berg463d0182009-07-14 00:33:35 +02008890 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8891 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008892 return;
8893
8894 nla_put_failure:
8895 genlmsg_cancel(msg, hdr);
8896 nlmsg_free(msg);
8897
8898}
8899
8900void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8901 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008902 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008903{
8904 struct sk_buff *msg;
8905 void *hdr;
8906
Thomas Graf58050fc2012-06-28 03:57:45 +00008907 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008908 if (!msg)
8909 return;
8910
8911 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8912 if (!hdr) {
8913 nlmsg_free(msg);
8914 return;
8915 }
8916
David S. Miller9360ffd2012-03-29 04:41:26 -04008917 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8918 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8919 (from_ap && reason &&
8920 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8921 (from_ap &&
8922 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8923 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8924 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008925
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008926 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008927
Johannes Berg463d0182009-07-14 00:33:35 +02008928 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8929 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008930 return;
8931
8932 nla_put_failure:
8933 genlmsg_cancel(msg, hdr);
8934 nlmsg_free(msg);
8935
8936}
8937
Johannes Berg04a773a2009-04-19 21:24:32 +02008938void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8939 struct net_device *netdev, const u8 *bssid,
8940 gfp_t gfp)
8941{
8942 struct sk_buff *msg;
8943 void *hdr;
8944
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008945 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008946 if (!msg)
8947 return;
8948
8949 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8950 if (!hdr) {
8951 nlmsg_free(msg);
8952 return;
8953 }
8954
David S. Miller9360ffd2012-03-29 04:41:26 -04008955 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8956 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8957 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8958 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008959
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008960 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008961
Johannes Berg463d0182009-07-14 00:33:35 +02008962 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8963 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008964 return;
8965
8966 nla_put_failure:
8967 genlmsg_cancel(msg, hdr);
8968 nlmsg_free(msg);
8969}
8970
Javier Cardonac93b5e72011-04-07 15:08:34 -07008971void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8972 struct net_device *netdev,
8973 const u8 *macaddr, const u8* ie, u8 ie_len,
8974 gfp_t gfp)
8975{
8976 struct sk_buff *msg;
8977 void *hdr;
8978
8979 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8980 if (!msg)
8981 return;
8982
8983 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8984 if (!hdr) {
8985 nlmsg_free(msg);
8986 return;
8987 }
8988
David S. Miller9360ffd2012-03-29 04:41:26 -04008989 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8990 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8991 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8992 (ie_len && ie &&
8993 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8994 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008995
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008996 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008997
8998 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8999 nl80211_mlme_mcgrp.id, gfp);
9000 return;
9001
9002 nla_put_failure:
9003 genlmsg_cancel(msg, hdr);
9004 nlmsg_free(msg);
9005}
9006
Jouni Malinena3b8b052009-03-27 21:59:49 +02009007void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9008 struct net_device *netdev, const u8 *addr,
9009 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009010 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009011{
9012 struct sk_buff *msg;
9013 void *hdr;
9014
Johannes Berge6d6e342009-07-01 21:26:47 +02009015 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009016 if (!msg)
9017 return;
9018
9019 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9020 if (!hdr) {
9021 nlmsg_free(msg);
9022 return;
9023 }
9024
David S. Miller9360ffd2012-03-29 04:41:26 -04009025 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9026 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9027 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9028 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9029 (key_id != -1 &&
9030 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9031 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9032 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009033
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009034 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009035
Johannes Berg463d0182009-07-14 00:33:35 +02009036 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9037 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009038 return;
9039
9040 nla_put_failure:
9041 genlmsg_cancel(msg, hdr);
9042 nlmsg_free(msg);
9043}
9044
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009045void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9046 struct ieee80211_channel *channel_before,
9047 struct ieee80211_channel *channel_after)
9048{
9049 struct sk_buff *msg;
9050 void *hdr;
9051 struct nlattr *nl_freq;
9052
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009053 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009054 if (!msg)
9055 return;
9056
9057 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9058 if (!hdr) {
9059 nlmsg_free(msg);
9060 return;
9061 }
9062
9063 /*
9064 * Since we are applying the beacon hint to a wiphy we know its
9065 * wiphy_idx is valid
9066 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009067 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9068 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009069
9070 /* Before */
9071 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9072 if (!nl_freq)
9073 goto nla_put_failure;
9074 if (nl80211_msg_put_channel(msg, channel_before))
9075 goto nla_put_failure;
9076 nla_nest_end(msg, nl_freq);
9077
9078 /* After */
9079 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9080 if (!nl_freq)
9081 goto nla_put_failure;
9082 if (nl80211_msg_put_channel(msg, channel_after))
9083 goto nla_put_failure;
9084 nla_nest_end(msg, nl_freq);
9085
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009086 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009087
Johannes Berg463d0182009-07-14 00:33:35 +02009088 rcu_read_lock();
9089 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9090 GFP_ATOMIC);
9091 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009092
9093 return;
9094
9095nla_put_failure:
9096 genlmsg_cancel(msg, hdr);
9097 nlmsg_free(msg);
9098}
9099
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009100static void nl80211_send_remain_on_chan_event(
9101 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009102 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009103 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009104 unsigned int duration, gfp_t gfp)
9105{
9106 struct sk_buff *msg;
9107 void *hdr;
9108
9109 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9110 if (!msg)
9111 return;
9112
9113 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9114 if (!hdr) {
9115 nlmsg_free(msg);
9116 return;
9117 }
9118
David S. Miller9360ffd2012-03-29 04:41:26 -04009119 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009120 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9121 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009122 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009123 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009124 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9125 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009126 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9127 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009128
David S. Miller9360ffd2012-03-29 04:41:26 -04009129 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9130 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9131 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009132
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009133 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009134
9135 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9136 nl80211_mlme_mcgrp.id, gfp);
9137 return;
9138
9139 nla_put_failure:
9140 genlmsg_cancel(msg, hdr);
9141 nlmsg_free(msg);
9142}
9143
9144void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009145 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009146 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009147 unsigned int duration, gfp_t gfp)
9148{
9149 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009150 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009151 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009152}
9153
9154void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02009155 struct cfg80211_registered_device *rdev,
9156 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01009157 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009158{
9159 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009160 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009161}
9162
Johannes Berg98b62182009-12-23 13:15:44 +01009163void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
9164 struct net_device *dev, const u8 *mac_addr,
9165 struct station_info *sinfo, gfp_t gfp)
9166{
9167 struct sk_buff *msg;
9168
Thomas Graf58050fc2012-06-28 03:57:45 +00009169 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009170 if (!msg)
9171 return;
9172
John W. Linville66266b32012-03-15 13:25:41 -04009173 if (nl80211_send_station(msg, 0, 0, 0,
9174 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009175 nlmsg_free(msg);
9176 return;
9177 }
9178
9179 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9180 nl80211_mlme_mcgrp.id, gfp);
9181}
9182
Jouni Malinenec15e682011-03-23 15:29:52 +02009183void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
9184 struct net_device *dev, const u8 *mac_addr,
9185 gfp_t gfp)
9186{
9187 struct sk_buff *msg;
9188 void *hdr;
9189
Thomas Graf58050fc2012-06-28 03:57:45 +00009190 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009191 if (!msg)
9192 return;
9193
9194 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9195 if (!hdr) {
9196 nlmsg_free(msg);
9197 return;
9198 }
9199
David S. Miller9360ffd2012-03-29 04:41:26 -04009200 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9201 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9202 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009203
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009204 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009205
9206 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9207 nl80211_mlme_mcgrp.id, gfp);
9208 return;
9209
9210 nla_put_failure:
9211 genlmsg_cancel(msg, hdr);
9212 nlmsg_free(msg);
9213}
9214
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309215void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
9216 struct net_device *dev, const u8 *mac_addr,
9217 enum nl80211_connect_failed_reason reason,
9218 gfp_t gfp)
9219{
9220 struct sk_buff *msg;
9221 void *hdr;
9222
9223 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9224 if (!msg)
9225 return;
9226
9227 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9228 if (!hdr) {
9229 nlmsg_free(msg);
9230 return;
9231 }
9232
9233 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9234 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9235 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9236 goto nla_put_failure;
9237
9238 genlmsg_end(msg, hdr);
9239
9240 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9241 nl80211_mlme_mcgrp.id, gfp);
9242 return;
9243
9244 nla_put_failure:
9245 genlmsg_cancel(msg, hdr);
9246 nlmsg_free(msg);
9247}
9248
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009249static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9250 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009251{
9252 struct wireless_dev *wdev = dev->ieee80211_ptr;
9253 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9254 struct sk_buff *msg;
9255 void *hdr;
9256 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009257 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009258
Eric W. Biederman15e47302012-09-07 20:12:54 +00009259 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009260 return false;
9261
9262 msg = nlmsg_new(100, gfp);
9263 if (!msg)
9264 return true;
9265
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009266 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009267 if (!hdr) {
9268 nlmsg_free(msg);
9269 return true;
9270 }
9271
David S. Miller9360ffd2012-03-29 04:41:26 -04009272 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9273 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9274 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9275 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009276
9277 err = genlmsg_end(msg, hdr);
9278 if (err < 0) {
9279 nlmsg_free(msg);
9280 return true;
9281 }
9282
Eric W. Biederman15e47302012-09-07 20:12:54 +00009283 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009284 return true;
9285
9286 nla_put_failure:
9287 genlmsg_cancel(msg, hdr);
9288 nlmsg_free(msg);
9289 return true;
9290}
9291
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009292bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
9293{
9294 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9295 addr, gfp);
9296}
9297
9298bool nl80211_unexpected_4addr_frame(struct net_device *dev,
9299 const u8 *addr, gfp_t gfp)
9300{
9301 return __nl80211_unexpected_frame(dev,
9302 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9303 addr, gfp);
9304}
9305
Johannes Berg2e161f72010-08-12 15:38:38 +02009306int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009307 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009308 int freq, int sig_dbm,
9309 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009310{
Johannes Berg71bbc992012-06-15 15:30:18 +02009311 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009312 struct sk_buff *msg;
9313 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009314
9315 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9316 if (!msg)
9317 return -ENOMEM;
9318
Johannes Berg2e161f72010-08-12 15:38:38 +02009319 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009320 if (!hdr) {
9321 nlmsg_free(msg);
9322 return -ENOMEM;
9323 }
9324
David S. Miller9360ffd2012-03-29 04:41:26 -04009325 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009326 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9327 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009328 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9329 (sig_dbm &&
9330 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9331 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9332 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009333
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009334 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009335
Eric W. Biederman15e47302012-09-07 20:12:54 +00009336 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009337
9338 nla_put_failure:
9339 genlmsg_cancel(msg, hdr);
9340 nlmsg_free(msg);
9341 return -ENOBUFS;
9342}
9343
Johannes Berg2e161f72010-08-12 15:38:38 +02009344void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009345 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02009346 const u8 *buf, size_t len, bool ack,
9347 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009348{
Johannes Berg71bbc992012-06-15 15:30:18 +02009349 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009350 struct sk_buff *msg;
9351 void *hdr;
9352
9353 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9354 if (!msg)
9355 return;
9356
Johannes Berg2e161f72010-08-12 15:38:38 +02009357 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009358 if (!hdr) {
9359 nlmsg_free(msg);
9360 return;
9361 }
9362
David S. Miller9360ffd2012-03-29 04:41:26 -04009363 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009364 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9365 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009366 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9367 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9368 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9369 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009370
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009371 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009372
9373 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9374 return;
9375
9376 nla_put_failure:
9377 genlmsg_cancel(msg, hdr);
9378 nlmsg_free(msg);
9379}
9380
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009381void
9382nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9383 struct net_device *netdev,
9384 enum nl80211_cqm_rssi_threshold_event rssi_event,
9385 gfp_t gfp)
9386{
9387 struct sk_buff *msg;
9388 struct nlattr *pinfoattr;
9389 void *hdr;
9390
Thomas Graf58050fc2012-06-28 03:57:45 +00009391 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009392 if (!msg)
9393 return;
9394
9395 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9396 if (!hdr) {
9397 nlmsg_free(msg);
9398 return;
9399 }
9400
David S. Miller9360ffd2012-03-29 04:41:26 -04009401 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9402 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9403 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009404
9405 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9406 if (!pinfoattr)
9407 goto nla_put_failure;
9408
David S. Miller9360ffd2012-03-29 04:41:26 -04009409 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9410 rssi_event))
9411 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009412
9413 nla_nest_end(msg, pinfoattr);
9414
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009415 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009416
9417 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9418 nl80211_mlme_mcgrp.id, gfp);
9419 return;
9420
9421 nla_put_failure:
9422 genlmsg_cancel(msg, hdr);
9423 nlmsg_free(msg);
9424}
9425
Johannes Berge5497d72011-07-05 16:35:40 +02009426void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9427 struct net_device *netdev, const u8 *bssid,
9428 const u8 *replay_ctr, gfp_t gfp)
9429{
9430 struct sk_buff *msg;
9431 struct nlattr *rekey_attr;
9432 void *hdr;
9433
Thomas Graf58050fc2012-06-28 03:57:45 +00009434 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009435 if (!msg)
9436 return;
9437
9438 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9439 if (!hdr) {
9440 nlmsg_free(msg);
9441 return;
9442 }
9443
David S. Miller9360ffd2012-03-29 04:41:26 -04009444 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9445 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9446 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9447 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009448
9449 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9450 if (!rekey_attr)
9451 goto nla_put_failure;
9452
David S. Miller9360ffd2012-03-29 04:41:26 -04009453 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9454 NL80211_REPLAY_CTR_LEN, replay_ctr))
9455 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009456
9457 nla_nest_end(msg, rekey_attr);
9458
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009459 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009460
9461 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9462 nl80211_mlme_mcgrp.id, gfp);
9463 return;
9464
9465 nla_put_failure:
9466 genlmsg_cancel(msg, hdr);
9467 nlmsg_free(msg);
9468}
9469
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009470void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9471 struct net_device *netdev, int index,
9472 const u8 *bssid, bool preauth, gfp_t gfp)
9473{
9474 struct sk_buff *msg;
9475 struct nlattr *attr;
9476 void *hdr;
9477
Thomas Graf58050fc2012-06-28 03:57:45 +00009478 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009479 if (!msg)
9480 return;
9481
9482 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
9483 if (!hdr) {
9484 nlmsg_free(msg);
9485 return;
9486 }
9487
David S. Miller9360ffd2012-03-29 04:41:26 -04009488 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9489 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9490 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009491
9492 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9493 if (!attr)
9494 goto nla_put_failure;
9495
David S. Miller9360ffd2012-03-29 04:41:26 -04009496 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9497 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9498 (preauth &&
9499 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9500 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009501
9502 nla_nest_end(msg, attr);
9503
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009504 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009505
9506 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9507 nl80211_mlme_mcgrp.id, gfp);
9508 return;
9509
9510 nla_put_failure:
9511 genlmsg_cancel(msg, hdr);
9512 nlmsg_free(msg);
9513}
9514
Thomas Pedersen53145262012-04-06 13:35:47 -07009515void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009516 struct net_device *netdev,
9517 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009518{
9519 struct sk_buff *msg;
9520 void *hdr;
9521
Thomas Graf58050fc2012-06-28 03:57:45 +00009522 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009523 if (!msg)
9524 return;
9525
9526 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9527 if (!hdr) {
9528 nlmsg_free(msg);
9529 return;
9530 }
9531
Johannes Berg683b6d32012-11-08 21:25:48 +01009532 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9533 goto nla_put_failure;
9534
9535 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009536 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009537
9538 genlmsg_end(msg, hdr);
9539
9540 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9541 nl80211_mlme_mcgrp.id, gfp);
9542 return;
9543
9544 nla_put_failure:
9545 genlmsg_cancel(msg, hdr);
9546 nlmsg_free(msg);
9547}
9548
Johannes Bergc063dbf2010-11-24 08:10:05 +01009549void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009550nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9551 struct net_device *netdev, const u8 *peer,
9552 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9553{
9554 struct sk_buff *msg;
9555 struct nlattr *pinfoattr;
9556 void *hdr;
9557
9558 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9559 if (!msg)
9560 return;
9561
9562 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9563 if (!hdr) {
9564 nlmsg_free(msg);
9565 return;
9566 }
9567
9568 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9569 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9570 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9571 goto nla_put_failure;
9572
9573 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9574 if (!pinfoattr)
9575 goto nla_put_failure;
9576
9577 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9578 goto nla_put_failure;
9579
9580 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9581 goto nla_put_failure;
9582
9583 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9584 goto nla_put_failure;
9585
9586 nla_nest_end(msg, pinfoattr);
9587
9588 genlmsg_end(msg, hdr);
9589
9590 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9591 nl80211_mlme_mcgrp.id, gfp);
9592 return;
9593
9594 nla_put_failure:
9595 genlmsg_cancel(msg, hdr);
9596 nlmsg_free(msg);
9597}
9598
9599void
Simon Wunderlich04f39042013-02-08 18:16:19 +01009600nl80211_radar_notify(struct cfg80211_registered_device *rdev,
9601 struct cfg80211_chan_def *chandef,
9602 enum nl80211_radar_event event,
9603 struct net_device *netdev, gfp_t gfp)
9604{
9605 struct sk_buff *msg;
9606 void *hdr;
9607
9608 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9609 if (!msg)
9610 return;
9611
9612 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
9613 if (!hdr) {
9614 nlmsg_free(msg);
9615 return;
9616 }
9617
9618 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9619 goto nla_put_failure;
9620
9621 /* NOP and radar events don't need a netdev parameter */
9622 if (netdev) {
9623 struct wireless_dev *wdev = netdev->ieee80211_ptr;
9624
9625 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9626 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9627 goto nla_put_failure;
9628 }
9629
9630 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
9631 goto nla_put_failure;
9632
9633 if (nl80211_send_chandef(msg, chandef))
9634 goto nla_put_failure;
9635
9636 if (genlmsg_end(msg, hdr) < 0) {
9637 nlmsg_free(msg);
9638 return;
9639 }
9640
9641 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9642 nl80211_mlme_mcgrp.id, gfp);
9643 return;
9644
9645 nla_put_failure:
9646 genlmsg_cancel(msg, hdr);
9647 nlmsg_free(msg);
9648}
9649
9650void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009651nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9652 struct net_device *netdev, const u8 *peer,
9653 u32 num_packets, gfp_t gfp)
9654{
9655 struct sk_buff *msg;
9656 struct nlattr *pinfoattr;
9657 void *hdr;
9658
Thomas Graf58050fc2012-06-28 03:57:45 +00009659 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009660 if (!msg)
9661 return;
9662
9663 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9664 if (!hdr) {
9665 nlmsg_free(msg);
9666 return;
9667 }
9668
David S. Miller9360ffd2012-03-29 04:41:26 -04009669 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9670 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9671 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9672 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009673
9674 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9675 if (!pinfoattr)
9676 goto nla_put_failure;
9677
David S. Miller9360ffd2012-03-29 04:41:26 -04009678 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9679 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009680
9681 nla_nest_end(msg, pinfoattr);
9682
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009683 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009684
9685 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9686 nl80211_mlme_mcgrp.id, gfp);
9687 return;
9688
9689 nla_put_failure:
9690 genlmsg_cancel(msg, hdr);
9691 nlmsg_free(msg);
9692}
9693
Johannes Berg7f6cf312011-11-04 11:18:15 +01009694void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9695 u64 cookie, bool acked, gfp_t gfp)
9696{
9697 struct wireless_dev *wdev = dev->ieee80211_ptr;
9698 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9699 struct sk_buff *msg;
9700 void *hdr;
9701 int err;
9702
Beni Lev4ee3e062012-08-27 12:49:39 +03009703 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9704
Thomas Graf58050fc2012-06-28 03:57:45 +00009705 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009706
Johannes Berg7f6cf312011-11-04 11:18:15 +01009707 if (!msg)
9708 return;
9709
9710 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9711 if (!hdr) {
9712 nlmsg_free(msg);
9713 return;
9714 }
9715
David S. Miller9360ffd2012-03-29 04:41:26 -04009716 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9717 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9718 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9719 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9720 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9721 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009722
9723 err = genlmsg_end(msg, hdr);
9724 if (err < 0) {
9725 nlmsg_free(msg);
9726 return;
9727 }
9728
9729 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9730 nl80211_mlme_mcgrp.id, gfp);
9731 return;
9732
9733 nla_put_failure:
9734 genlmsg_cancel(msg, hdr);
9735 nlmsg_free(msg);
9736}
9737EXPORT_SYMBOL(cfg80211_probe_status);
9738
Johannes Berg5e760232011-11-04 11:18:17 +01009739void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9740 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009741 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009742{
9743 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9744 struct sk_buff *msg;
9745 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009746 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009747
Beni Lev4ee3e062012-08-27 12:49:39 +03009748 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9749
Ben Greear37c73b52012-10-26 14:49:25 -07009750 spin_lock_bh(&rdev->beacon_registrations_lock);
9751 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9752 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9753 if (!msg) {
9754 spin_unlock_bh(&rdev->beacon_registrations_lock);
9755 return;
9756 }
Johannes Berg5e760232011-11-04 11:18:17 +01009757
Ben Greear37c73b52012-10-26 14:49:25 -07009758 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9759 if (!hdr)
9760 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009761
Ben Greear37c73b52012-10-26 14:49:25 -07009762 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9763 (freq &&
9764 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9765 (sig_dbm &&
9766 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9767 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9768 goto nla_put_failure;
9769
9770 genlmsg_end(msg, hdr);
9771
9772 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009773 }
Ben Greear37c73b52012-10-26 14:49:25 -07009774 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009775 return;
9776
9777 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009778 spin_unlock_bh(&rdev->beacon_registrations_lock);
9779 if (hdr)
9780 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009781 nlmsg_free(msg);
9782}
9783EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9784
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009785#ifdef CONFIG_PM
9786void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
9787 struct cfg80211_wowlan_wakeup *wakeup,
9788 gfp_t gfp)
9789{
9790 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9791 struct sk_buff *msg;
9792 void *hdr;
9793 int err, size = 200;
9794
9795 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
9796
9797 if (wakeup)
9798 size += wakeup->packet_present_len;
9799
9800 msg = nlmsg_new(size, gfp);
9801 if (!msg)
9802 return;
9803
9804 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
9805 if (!hdr)
9806 goto free_msg;
9807
9808 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9809 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9810 goto free_msg;
9811
9812 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9813 wdev->netdev->ifindex))
9814 goto free_msg;
9815
9816 if (wakeup) {
9817 struct nlattr *reasons;
9818
9819 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9820
9821 if (wakeup->disconnect &&
9822 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
9823 goto free_msg;
9824 if (wakeup->magic_pkt &&
9825 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
9826 goto free_msg;
9827 if (wakeup->gtk_rekey_failure &&
9828 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
9829 goto free_msg;
9830 if (wakeup->eap_identity_req &&
9831 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
9832 goto free_msg;
9833 if (wakeup->four_way_handshake &&
9834 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
9835 goto free_msg;
9836 if (wakeup->rfkill_release &&
9837 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
9838 goto free_msg;
9839
9840 if (wakeup->pattern_idx >= 0 &&
9841 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
9842 wakeup->pattern_idx))
9843 goto free_msg;
9844
Johannes Berg2a0e0472013-01-23 22:57:40 +01009845 if (wakeup->tcp_match)
9846 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
9847
9848 if (wakeup->tcp_connlost)
9849 nla_put_flag(msg,
9850 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
9851
9852 if (wakeup->tcp_nomoretokens)
9853 nla_put_flag(msg,
9854 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
9855
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009856 if (wakeup->packet) {
9857 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
9858 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
9859
9860 if (!wakeup->packet_80211) {
9861 pkt_attr =
9862 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
9863 len_attr =
9864 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
9865 }
9866
9867 if (wakeup->packet_len &&
9868 nla_put_u32(msg, len_attr, wakeup->packet_len))
9869 goto free_msg;
9870
9871 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
9872 wakeup->packet))
9873 goto free_msg;
9874 }
9875
9876 nla_nest_end(msg, reasons);
9877 }
9878
9879 err = genlmsg_end(msg, hdr);
9880 if (err < 0)
9881 goto free_msg;
9882
9883 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9884 nl80211_mlme_mcgrp.id, gfp);
9885 return;
9886
9887 free_msg:
9888 nlmsg_free(msg);
9889}
9890EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
9891#endif
9892
Jouni Malinen3475b092012-11-16 22:49:57 +02009893void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9894 enum nl80211_tdls_operation oper,
9895 u16 reason_code, gfp_t gfp)
9896{
9897 struct wireless_dev *wdev = dev->ieee80211_ptr;
9898 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9899 struct sk_buff *msg;
9900 void *hdr;
9901 int err;
9902
9903 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9904 reason_code);
9905
9906 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9907 if (!msg)
9908 return;
9909
9910 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9911 if (!hdr) {
9912 nlmsg_free(msg);
9913 return;
9914 }
9915
9916 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9917 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9918 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9919 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9920 (reason_code > 0 &&
9921 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9922 goto nla_put_failure;
9923
9924 err = genlmsg_end(msg, hdr);
9925 if (err < 0) {
9926 nlmsg_free(msg);
9927 return;
9928 }
9929
9930 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9931 nl80211_mlme_mcgrp.id, gfp);
9932 return;
9933
9934 nla_put_failure:
9935 genlmsg_cancel(msg, hdr);
9936 nlmsg_free(msg);
9937}
9938EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9939
Jouni Malinen026331c2010-02-15 12:53:10 +02009940static int nl80211_netlink_notify(struct notifier_block * nb,
9941 unsigned long state,
9942 void *_notify)
9943{
9944 struct netlink_notify *notify = _notify;
9945 struct cfg80211_registered_device *rdev;
9946 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009947 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009948
9949 if (state != NETLINK_URELEASE)
9950 return NOTIFY_DONE;
9951
9952 rcu_read_lock();
9953
Johannes Berg5e760232011-11-04 11:18:17 +01009954 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009955 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009956 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009957
9958 spin_lock_bh(&rdev->beacon_registrations_lock);
9959 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9960 list) {
9961 if (reg->nlportid == notify->portid) {
9962 list_del(&reg->list);
9963 kfree(reg);
9964 break;
9965 }
9966 }
9967 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009968 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009969
9970 rcu_read_unlock();
9971
9972 return NOTIFY_DONE;
9973}
9974
9975static struct notifier_block nl80211_netlink_notifier = {
9976 .notifier_call = nl80211_netlink_notify,
9977};
9978
Johannes Berg55682962007-09-20 13:09:35 -04009979/* initialisation/exit functions */
9980
9981int nl80211_init(void)
9982{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009983 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009984
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009985 err = genl_register_family_with_ops(&nl80211_fam,
9986 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009987 if (err)
9988 return err;
9989
Johannes Berg55682962007-09-20 13:09:35 -04009990 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9991 if (err)
9992 goto err_out;
9993
Johannes Berg2a519312009-02-10 21:25:55 +01009994 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9995 if (err)
9996 goto err_out;
9997
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009998 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9999 if (err)
10000 goto err_out;
10001
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010002 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10003 if (err)
10004 goto err_out;
10005
Johannes Bergaff89a92009-07-01 21:26:51 +020010006#ifdef CONFIG_NL80211_TESTMODE
10007 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10008 if (err)
10009 goto err_out;
10010#endif
10011
Jouni Malinen026331c2010-02-15 12:53:10 +020010012 err = netlink_register_notifier(&nl80211_netlink_notifier);
10013 if (err)
10014 goto err_out;
10015
Johannes Berg55682962007-09-20 13:09:35 -040010016 return 0;
10017 err_out:
10018 genl_unregister_family(&nl80211_fam);
10019 return err;
10020}
10021
10022void nl80211_exit(void)
10023{
Jouni Malinen026331c2010-02-15 12:53:10 +020010024 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010025 genl_unregister_family(&nl80211_fam);
10026}