blob: 14276af7964b88b41a2ea3f109965f54aaa61961 [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 = {
Marcel Holtmannfb4e1562013-04-28 16:22:06 -070040 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = NL80211_GENL_NAME, /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
Johannes Berg55682962007-09-20 13:09:35 -040044 .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 },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200371 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
372 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100373 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100374 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
375 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
376 .len = NL80211_VHT_CAPABILITY_LEN,
377 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200378 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
379 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
380 .len = IEEE80211_MAX_DATA_LEN },
Jouni Malinen5e4b6f52013-05-16 20:11:08 +0300381 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400382};
383
Johannes Berge31b8212010-10-05 19:39:30 +0200384/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000385static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200386 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200387 [NL80211_KEY_IDX] = { .type = NLA_U8 },
388 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200389 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200390 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
391 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200392 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100393 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
394};
395
396/* policy for the key default flags */
397static const struct nla_policy
398nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
399 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
400 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200401};
402
Johannes Bergff1b6e62011-05-04 15:37:28 +0200403/* policy for WoWLAN attributes */
404static const struct nla_policy
405nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
406 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
407 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
408 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
409 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb132011-07-13 10:48:55 +0200410 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
411 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
412 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
413 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100414 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
415};
416
417static const struct nla_policy
418nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
419 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
420 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
421 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
422 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
423 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
424 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
425 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
426 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
427 },
428 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
429 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
430 },
431 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
432 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
433 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200434};
435
Johannes Berge5497d72011-07-05 16:35:40 +0200436/* policy for GTK rekey offload attributes */
437static const struct nla_policy
438nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
439 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
440 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
441 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
442};
443
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300444static const struct nla_policy
445nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200446 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300447 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700448 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300449};
450
Johannes Berg97990a02013-04-19 01:02:55 +0200451static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
452 struct netlink_callback *cb,
453 struct cfg80211_registered_device **rdev,
454 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100455{
Johannes Berg67748892010-10-04 21:14:06 +0200456 int err;
457
Johannes Berg67748892010-10-04 21:14:06 +0200458 rtnl_lock();
Johannes Berg97990a02013-04-19 01:02:55 +0200459 mutex_lock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200460
Johannes Berg97990a02013-04-19 01:02:55 +0200461 if (!cb->args[0]) {
462 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
463 nl80211_fam.attrbuf, nl80211_fam.maxattr,
464 nl80211_policy);
465 if (err)
466 goto out_unlock;
467
468 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
469 nl80211_fam.attrbuf);
470 if (IS_ERR(*wdev)) {
471 err = PTR_ERR(*wdev);
472 goto out_unlock;
473 }
474 *rdev = wiphy_to_dev((*wdev)->wiphy);
475 cb->args[0] = (*rdev)->wiphy_idx;
476 cb->args[1] = (*wdev)->identifier;
477 } else {
478 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0]);
479 struct wireless_dev *tmp;
480
481 if (!wiphy) {
482 err = -ENODEV;
483 goto out_unlock;
484 }
485 *rdev = wiphy_to_dev(wiphy);
486 *wdev = NULL;
487
488 mutex_lock(&(*rdev)->devlist_mtx);
489 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
490 if (tmp->identifier == cb->args[1]) {
491 *wdev = tmp;
492 break;
493 }
494 }
495 mutex_unlock(&(*rdev)->devlist_mtx);
496
497 if (!*wdev) {
498 err = -ENODEV;
499 goto out_unlock;
500 }
Johannes Berg67748892010-10-04 21:14:06 +0200501 }
502
Johannes Berg97990a02013-04-19 01:02:55 +0200503 cfg80211_lock_rdev(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200504
Johannes Berg97990a02013-04-19 01:02:55 +0200505 mutex_unlock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200506 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200507 out_unlock:
508 mutex_unlock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200509 rtnl_unlock();
510 return err;
511}
512
Johannes Berg97990a02013-04-19 01:02:55 +0200513static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200514{
515 cfg80211_unlock_rdev(rdev);
516 rtnl_unlock();
517}
518
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100519/* IE validation */
520static bool is_valid_ie_attr(const struct nlattr *attr)
521{
522 const u8 *pos;
523 int len;
524
525 if (!attr)
526 return true;
527
528 pos = nla_data(attr);
529 len = nla_len(attr);
530
531 while (len) {
532 u8 elemlen;
533
534 if (len < 2)
535 return false;
536 len -= 2;
537
538 elemlen = pos[1];
539 if (elemlen > len)
540 return false;
541
542 len -= elemlen;
543 pos += 2 + elemlen;
544 }
545
546 return true;
547}
548
Johannes Berg55682962007-09-20 13:09:35 -0400549/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000550static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400551 int flags, u8 cmd)
552{
553 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000554 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400555}
556
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400557static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100558 struct ieee80211_channel *chan,
559 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400560{
David S. Miller9360ffd2012-03-29 04:41:26 -0400561 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
562 chan->center_freq))
563 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400564
David S. Miller9360ffd2012-03-29 04:41:26 -0400565 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
566 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
567 goto nla_put_failure;
568 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
569 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
570 goto nla_put_failure;
571 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
572 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
573 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100574 if (chan->flags & IEEE80211_CHAN_RADAR) {
575 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
576 goto nla_put_failure;
577 if (large) {
578 u32 time;
579
580 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
581
582 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
583 chan->dfs_state))
584 goto nla_put_failure;
585 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
586 time))
587 goto nla_put_failure;
588 }
589 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400590
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100591 if (large) {
592 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
593 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
594 goto nla_put_failure;
595 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
596 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
597 goto nla_put_failure;
598 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
599 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
600 goto nla_put_failure;
601 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
602 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
603 goto nla_put_failure;
604 }
605
David S. Miller9360ffd2012-03-29 04:41:26 -0400606 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
607 DBM_TO_MBM(chan->max_power)))
608 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400609
610 return 0;
611
612 nla_put_failure:
613 return -ENOBUFS;
614}
615
Johannes Berg55682962007-09-20 13:09:35 -0400616/* netlink command implementations */
617
Johannes Bergb9454e82009-07-08 13:29:08 +0200618struct key_parse {
619 struct key_params p;
620 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200621 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200622 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100623 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200624};
625
626static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
627{
628 struct nlattr *tb[NL80211_KEY_MAX + 1];
629 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
630 nl80211_key_policy);
631 if (err)
632 return err;
633
634 k->def = !!tb[NL80211_KEY_DEFAULT];
635 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
636
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100637 if (k->def) {
638 k->def_uni = true;
639 k->def_multi = true;
640 }
641 if (k->defmgmt)
642 k->def_multi = true;
643
Johannes Bergb9454e82009-07-08 13:29:08 +0200644 if (tb[NL80211_KEY_IDX])
645 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
646
647 if (tb[NL80211_KEY_DATA]) {
648 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
649 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
650 }
651
652 if (tb[NL80211_KEY_SEQ]) {
653 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
654 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
655 }
656
657 if (tb[NL80211_KEY_CIPHER])
658 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
659
Johannes Berge31b8212010-10-05 19:39:30 +0200660 if (tb[NL80211_KEY_TYPE]) {
661 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
662 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
663 return -EINVAL;
664 }
665
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100666 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
667 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100668 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
669 tb[NL80211_KEY_DEFAULT_TYPES],
670 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100671 if (err)
672 return err;
673
674 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
675 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
676 }
677
Johannes Bergb9454e82009-07-08 13:29:08 +0200678 return 0;
679}
680
681static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
682{
683 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
684 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
685 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
686 }
687
688 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
689 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
690 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
691 }
692
693 if (info->attrs[NL80211_ATTR_KEY_IDX])
694 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
695
696 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
697 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
698
699 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
700 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
701
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100702 if (k->def) {
703 k->def_uni = true;
704 k->def_multi = true;
705 }
706 if (k->defmgmt)
707 k->def_multi = true;
708
Johannes Berge31b8212010-10-05 19:39:30 +0200709 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
710 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
711 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
712 return -EINVAL;
713 }
714
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100715 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
716 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
717 int err = nla_parse_nested(
718 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
719 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
720 nl80211_key_default_policy);
721 if (err)
722 return err;
723
724 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
725 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
726 }
727
Johannes Bergb9454e82009-07-08 13:29:08 +0200728 return 0;
729}
730
731static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
732{
733 int err;
734
735 memset(k, 0, sizeof(*k));
736 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200737 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200738
739 if (info->attrs[NL80211_ATTR_KEY])
740 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
741 else
742 err = nl80211_parse_key_old(info, k);
743
744 if (err)
745 return err;
746
747 if (k->def && k->defmgmt)
748 return -EINVAL;
749
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100750 if (k->defmgmt) {
751 if (k->def_uni || !k->def_multi)
752 return -EINVAL;
753 }
754
Johannes Bergb9454e82009-07-08 13:29:08 +0200755 if (k->idx != -1) {
756 if (k->defmgmt) {
757 if (k->idx < 4 || k->idx > 5)
758 return -EINVAL;
759 } else if (k->def) {
760 if (k->idx < 0 || k->idx > 3)
761 return -EINVAL;
762 } else {
763 if (k->idx < 0 || k->idx > 5)
764 return -EINVAL;
765 }
766 }
767
768 return 0;
769}
770
Johannes Bergfffd0932009-07-08 14:22:54 +0200771static struct cfg80211_cached_keys *
772nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530773 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200774{
775 struct key_parse parse;
776 struct nlattr *key;
777 struct cfg80211_cached_keys *result;
778 int rem, err, def = 0;
779
780 result = kzalloc(sizeof(*result), GFP_KERNEL);
781 if (!result)
782 return ERR_PTR(-ENOMEM);
783
784 result->def = -1;
785 result->defmgmt = -1;
786
787 nla_for_each_nested(key, keys, rem) {
788 memset(&parse, 0, sizeof(parse));
789 parse.idx = -1;
790
791 err = nl80211_parse_key_new(key, &parse);
792 if (err)
793 goto error;
794 err = -EINVAL;
795 if (!parse.p.key)
796 goto error;
797 if (parse.idx < 0 || parse.idx > 4)
798 goto error;
799 if (parse.def) {
800 if (def)
801 goto error;
802 def = 1;
803 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100804 if (!parse.def_uni || !parse.def_multi)
805 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200806 } else if (parse.defmgmt)
807 goto error;
808 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200809 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200810 if (err)
811 goto error;
812 result->params[parse.idx].cipher = parse.p.cipher;
813 result->params[parse.idx].key_len = parse.p.key_len;
814 result->params[parse.idx].key = result->data[parse.idx];
815 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530816
817 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
818 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
819 if (no_ht)
820 *no_ht = true;
821 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200822 }
823
824 return result;
825 error:
826 kfree(result);
827 return ERR_PTR(err);
828}
829
830static int nl80211_key_allowed(struct wireless_dev *wdev)
831{
832 ASSERT_WDEV_LOCK(wdev);
833
Johannes Bergfffd0932009-07-08 14:22:54 +0200834 switch (wdev->iftype) {
835 case NL80211_IFTYPE_AP:
836 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200837 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700838 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200839 break;
840 case NL80211_IFTYPE_ADHOC:
841 if (!wdev->current_bss)
842 return -ENOLINK;
843 break;
844 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200845 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200846 if (wdev->sme_state != CFG80211_SME_CONNECTED)
847 return -ENOLINK;
848 break;
849 default:
850 return -EINVAL;
851 }
852
853 return 0;
854}
855
Johannes Berg7527a782011-05-13 10:58:57 +0200856static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
857{
858 struct nlattr *nl_modes = nla_nest_start(msg, attr);
859 int i;
860
861 if (!nl_modes)
862 goto nla_put_failure;
863
864 i = 0;
865 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400866 if ((ifmodes & 1) && nla_put_flag(msg, i))
867 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200868 ifmodes >>= 1;
869 i++;
870 }
871
872 nla_nest_end(msg, nl_modes);
873 return 0;
874
875nla_put_failure:
876 return -ENOBUFS;
877}
878
879static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100880 struct sk_buff *msg,
881 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200882{
883 struct nlattr *nl_combis;
884 int i, j;
885
886 nl_combis = nla_nest_start(msg,
887 NL80211_ATTR_INTERFACE_COMBINATIONS);
888 if (!nl_combis)
889 goto nla_put_failure;
890
891 for (i = 0; i < wiphy->n_iface_combinations; i++) {
892 const struct ieee80211_iface_combination *c;
893 struct nlattr *nl_combi, *nl_limits;
894
895 c = &wiphy->iface_combinations[i];
896
897 nl_combi = nla_nest_start(msg, i + 1);
898 if (!nl_combi)
899 goto nla_put_failure;
900
901 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
902 if (!nl_limits)
903 goto nla_put_failure;
904
905 for (j = 0; j < c->n_limits; j++) {
906 struct nlattr *nl_limit;
907
908 nl_limit = nla_nest_start(msg, j + 1);
909 if (!nl_limit)
910 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400911 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
912 c->limits[j].max))
913 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200914 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
915 c->limits[j].types))
916 goto nla_put_failure;
917 nla_nest_end(msg, nl_limit);
918 }
919
920 nla_nest_end(msg, nl_limits);
921
David S. Miller9360ffd2012-03-29 04:41:26 -0400922 if (c->beacon_int_infra_match &&
923 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
924 goto nla_put_failure;
925 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
926 c->num_different_channels) ||
927 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
928 c->max_interfaces))
929 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100930 if (large &&
931 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
932 c->radar_detect_widths))
933 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200934
935 nla_nest_end(msg, nl_combi);
936 }
937
938 nla_nest_end(msg, nl_combis);
939
940 return 0;
941nla_put_failure:
942 return -ENOBUFS;
943}
944
Johannes Berg3713b4e2013-02-14 16:19:38 +0100945#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100946static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
947 struct sk_buff *msg)
948{
949 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
950 struct nlattr *nl_tcp;
951
952 if (!tcp)
953 return 0;
954
955 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
956 if (!nl_tcp)
957 return -ENOBUFS;
958
959 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
960 tcp->data_payload_max))
961 return -ENOBUFS;
962
963 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
964 tcp->data_payload_max))
965 return -ENOBUFS;
966
967 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
968 return -ENOBUFS;
969
970 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
971 sizeof(*tcp->tok), tcp->tok))
972 return -ENOBUFS;
973
974 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
975 tcp->data_interval_max))
976 return -ENOBUFS;
977
978 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
979 tcp->wake_payload_max))
980 return -ENOBUFS;
981
982 nla_nest_end(msg, nl_tcp);
983 return 0;
984}
985
Johannes Berg3713b4e2013-02-14 16:19:38 +0100986static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +0100987 struct cfg80211_registered_device *dev,
988 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100989{
990 struct nlattr *nl_wowlan;
991
992 if (!dev->wiphy.wowlan.flags && !dev->wiphy.wowlan.n_patterns)
993 return 0;
994
995 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
996 if (!nl_wowlan)
997 return -ENOBUFS;
998
999 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1000 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1001 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1002 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1003 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1004 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1005 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1006 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1007 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1008 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1009 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1010 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1011 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1012 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1013 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1014 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1015 return -ENOBUFS;
1016
1017 if (dev->wiphy.wowlan.n_patterns) {
1018 struct nl80211_wowlan_pattern_support pat = {
1019 .max_patterns = dev->wiphy.wowlan.n_patterns,
1020 .min_pattern_len = dev->wiphy.wowlan.pattern_min_len,
1021 .max_pattern_len = dev->wiphy.wowlan.pattern_max_len,
1022 .max_pkt_offset = dev->wiphy.wowlan.max_pkt_offset,
1023 };
1024
1025 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1026 sizeof(pat), &pat))
1027 return -ENOBUFS;
1028 }
1029
Johannes Bergb56cf722013-02-20 01:02:38 +01001030 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1031 return -ENOBUFS;
1032
Johannes Berg3713b4e2013-02-14 16:19:38 +01001033 nla_nest_end(msg, nl_wowlan);
1034
1035 return 0;
1036}
1037#endif
1038
1039static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1040 struct ieee80211_supported_band *sband)
1041{
1042 struct nlattr *nl_rates, *nl_rate;
1043 struct ieee80211_rate *rate;
1044 int i;
1045
1046 /* add HT info */
1047 if (sband->ht_cap.ht_supported &&
1048 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1049 sizeof(sband->ht_cap.mcs),
1050 &sband->ht_cap.mcs) ||
1051 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1052 sband->ht_cap.cap) ||
1053 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1054 sband->ht_cap.ampdu_factor) ||
1055 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1056 sband->ht_cap.ampdu_density)))
1057 return -ENOBUFS;
1058
1059 /* add VHT info */
1060 if (sband->vht_cap.vht_supported &&
1061 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1062 sizeof(sband->vht_cap.vht_mcs),
1063 &sband->vht_cap.vht_mcs) ||
1064 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1065 sband->vht_cap.cap)))
1066 return -ENOBUFS;
1067
1068 /* add bitrates */
1069 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1070 if (!nl_rates)
1071 return -ENOBUFS;
1072
1073 for (i = 0; i < sband->n_bitrates; i++) {
1074 nl_rate = nla_nest_start(msg, i);
1075 if (!nl_rate)
1076 return -ENOBUFS;
1077
1078 rate = &sband->bitrates[i];
1079 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1080 rate->bitrate))
1081 return -ENOBUFS;
1082 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1083 nla_put_flag(msg,
1084 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1085 return -ENOBUFS;
1086
1087 nla_nest_end(msg, nl_rate);
1088 }
1089
1090 nla_nest_end(msg, nl_rates);
1091
1092 return 0;
1093}
1094
1095static int
1096nl80211_send_mgmt_stypes(struct sk_buff *msg,
1097 const struct ieee80211_txrx_stypes *mgmt_stypes)
1098{
1099 u16 stypes;
1100 struct nlattr *nl_ftypes, *nl_ifs;
1101 enum nl80211_iftype ift;
1102 int i;
1103
1104 if (!mgmt_stypes)
1105 return 0;
1106
1107 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1108 if (!nl_ifs)
1109 return -ENOBUFS;
1110
1111 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1112 nl_ftypes = nla_nest_start(msg, ift);
1113 if (!nl_ftypes)
1114 return -ENOBUFS;
1115 i = 0;
1116 stypes = mgmt_stypes[ift].tx;
1117 while (stypes) {
1118 if ((stypes & 1) &&
1119 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1120 (i << 4) | IEEE80211_FTYPE_MGMT))
1121 return -ENOBUFS;
1122 stypes >>= 1;
1123 i++;
1124 }
1125 nla_nest_end(msg, nl_ftypes);
1126 }
1127
1128 nla_nest_end(msg, nl_ifs);
1129
1130 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1131 if (!nl_ifs)
1132 return -ENOBUFS;
1133
1134 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1135 nl_ftypes = nla_nest_start(msg, ift);
1136 if (!nl_ftypes)
1137 return -ENOBUFS;
1138 i = 0;
1139 stypes = mgmt_stypes[ift].rx;
1140 while (stypes) {
1141 if ((stypes & 1) &&
1142 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1143 (i << 4) | IEEE80211_FTYPE_MGMT))
1144 return -ENOBUFS;
1145 stypes >>= 1;
1146 i++;
1147 }
1148 nla_nest_end(msg, nl_ftypes);
1149 }
1150 nla_nest_end(msg, nl_ifs);
1151
1152 return 0;
1153}
1154
1155static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1156 struct sk_buff *msg, u32 portid, u32 seq,
1157 int flags, bool split, long *split_start,
1158 long *band_start, long *chan_start)
Johannes Berg55682962007-09-20 13:09:35 -04001159{
1160 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001161 struct nlattr *nl_bands, *nl_band;
1162 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001163 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001164 enum ieee80211_band band;
1165 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001166 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001167 const struct ieee80211_txrx_stypes *mgmt_stypes =
1168 dev->wiphy.mgmt_stypes;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001169 long start = 0, start_chan = 0, start_band = 0;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001170 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001171
Eric W. Biederman15e47302012-09-07 20:12:54 +00001172 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001173 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001174 return -ENOBUFS;
1175
1176 /* allow always using the variables */
1177 if (!split) {
1178 split_start = &start;
1179 band_start = &start_band;
1180 chan_start = &start_chan;
1181 }
Johannes Berg55682962007-09-20 13:09:35 -04001182
David S. Miller9360ffd2012-03-29 04:41:26 -04001183 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001184 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1185 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001186 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001187 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001188 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001189
Johannes Berg3713b4e2013-02-14 16:19:38 +01001190 switch (*split_start) {
1191 case 0:
1192 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1193 dev->wiphy.retry_short) ||
1194 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1195 dev->wiphy.retry_long) ||
1196 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1197 dev->wiphy.frag_threshold) ||
1198 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1199 dev->wiphy.rts_threshold) ||
1200 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1201 dev->wiphy.coverage_class) ||
1202 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1203 dev->wiphy.max_scan_ssids) ||
1204 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1205 dev->wiphy.max_sched_scan_ssids) ||
1206 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1207 dev->wiphy.max_scan_ie_len) ||
1208 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1209 dev->wiphy.max_sched_scan_ie_len) ||
1210 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1211 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001212 goto nla_put_failure;
1213
Johannes Berg3713b4e2013-02-14 16:19:38 +01001214 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1215 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1216 goto nla_put_failure;
1217 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1218 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1219 goto nla_put_failure;
1220 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1221 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1222 goto nla_put_failure;
1223 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1224 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1225 goto nla_put_failure;
1226 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1227 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1228 goto nla_put_failure;
1229 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1230 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001231 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001232
Johannes Berg3713b4e2013-02-14 16:19:38 +01001233 (*split_start)++;
1234 if (split)
1235 break;
1236 case 1:
1237 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1238 sizeof(u32) * dev->wiphy.n_cipher_suites,
1239 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001240 goto nla_put_failure;
1241
Johannes Berg3713b4e2013-02-14 16:19:38 +01001242 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1243 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001244 goto nla_put_failure;
1245
Johannes Berg3713b4e2013-02-14 16:19:38 +01001246 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1247 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1248 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001249
Johannes Berg3713b4e2013-02-14 16:19:38 +01001250 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1251 dev->wiphy.available_antennas_tx) ||
1252 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1253 dev->wiphy.available_antennas_rx))
1254 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001255
Johannes Berg3713b4e2013-02-14 16:19:38 +01001256 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1257 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1258 dev->wiphy.probe_resp_offload))
1259 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001260
Johannes Berg3713b4e2013-02-14 16:19:38 +01001261 if ((dev->wiphy.available_antennas_tx ||
1262 dev->wiphy.available_antennas_rx) &&
1263 dev->ops->get_antenna) {
1264 u32 tx_ant = 0, rx_ant = 0;
1265 int res;
1266 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1267 if (!res) {
1268 if (nla_put_u32(msg,
1269 NL80211_ATTR_WIPHY_ANTENNA_TX,
1270 tx_ant) ||
1271 nla_put_u32(msg,
1272 NL80211_ATTR_WIPHY_ANTENNA_RX,
1273 rx_ant))
1274 goto nla_put_failure;
1275 }
Johannes Bergee688b002008-01-24 19:38:39 +01001276 }
1277
Johannes Berg3713b4e2013-02-14 16:19:38 +01001278 (*split_start)++;
1279 if (split)
1280 break;
1281 case 2:
1282 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1283 dev->wiphy.interface_modes))
1284 goto nla_put_failure;
1285 (*split_start)++;
1286 if (split)
1287 break;
1288 case 3:
1289 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1290 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001291 goto nla_put_failure;
1292
Johannes Berg3713b4e2013-02-14 16:19:38 +01001293 for (band = *band_start; band < IEEE80211_NUM_BANDS; band++) {
1294 struct ieee80211_supported_band *sband;
1295
1296 sband = dev->wiphy.bands[band];
1297
1298 if (!sband)
1299 continue;
1300
1301 nl_band = nla_nest_start(msg, band);
1302 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001303 goto nla_put_failure;
1304
Johannes Berg3713b4e2013-02-14 16:19:38 +01001305 switch (*chan_start) {
1306 case 0:
1307 if (nl80211_send_band_rateinfo(msg, sband))
1308 goto nla_put_failure;
1309 (*chan_start)++;
1310 if (split)
1311 break;
1312 default:
1313 /* add frequencies */
1314 nl_freqs = nla_nest_start(
1315 msg, NL80211_BAND_ATTR_FREQS);
1316 if (!nl_freqs)
1317 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001318
Johannes Berg3713b4e2013-02-14 16:19:38 +01001319 for (i = *chan_start - 1;
1320 i < sband->n_channels;
1321 i++) {
1322 nl_freq = nla_nest_start(msg, i);
1323 if (!nl_freq)
1324 goto nla_put_failure;
1325
1326 chan = &sband->channels[i];
1327
Johannes Bergcdc89b92013-02-18 23:54:36 +01001328 if (nl80211_msg_put_channel(msg, chan,
1329 split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001330 goto nla_put_failure;
1331
1332 nla_nest_end(msg, nl_freq);
1333 if (split)
1334 break;
1335 }
1336 if (i < sband->n_channels)
1337 *chan_start = i + 2;
1338 else
1339 *chan_start = 0;
1340 nla_nest_end(msg, nl_freqs);
1341 }
1342
1343 nla_nest_end(msg, nl_band);
1344
1345 if (split) {
1346 /* start again here */
1347 if (*chan_start)
1348 band--;
1349 break;
1350 }
Johannes Bergee688b002008-01-24 19:38:39 +01001351 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001352 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001353
Johannes Berg3713b4e2013-02-14 16:19:38 +01001354 if (band < IEEE80211_NUM_BANDS)
1355 *band_start = band + 1;
1356 else
1357 *band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001358
Johannes Berg3713b4e2013-02-14 16:19:38 +01001359 /* if bands & channels are done, continue outside */
1360 if (*band_start == 0 && *chan_start == 0)
1361 (*split_start)++;
1362 if (split)
1363 break;
1364 case 4:
1365 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1366 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001367 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001368
1369 i = 0;
1370#define CMD(op, n) \
1371 do { \
1372 if (dev->ops->op) { \
1373 i++; \
1374 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1375 goto nla_put_failure; \
1376 } \
1377 } while (0)
1378
1379 CMD(add_virtual_intf, NEW_INTERFACE);
1380 CMD(change_virtual_intf, SET_INTERFACE);
1381 CMD(add_key, NEW_KEY);
1382 CMD(start_ap, START_AP);
1383 CMD(add_station, NEW_STATION);
1384 CMD(add_mpath, NEW_MPATH);
1385 CMD(update_mesh_config, SET_MESH_CONFIG);
1386 CMD(change_bss, SET_BSS);
1387 CMD(auth, AUTHENTICATE);
1388 CMD(assoc, ASSOCIATE);
1389 CMD(deauth, DEAUTHENTICATE);
1390 CMD(disassoc, DISASSOCIATE);
1391 CMD(join_ibss, JOIN_IBSS);
1392 CMD(join_mesh, JOIN_MESH);
1393 CMD(set_pmksa, SET_PMKSA);
1394 CMD(del_pmksa, DEL_PMKSA);
1395 CMD(flush_pmksa, FLUSH_PMKSA);
1396 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1397 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1398 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1399 CMD(mgmt_tx, FRAME);
1400 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1401 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1402 i++;
1403 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1404 goto nla_put_failure;
1405 }
1406 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1407 dev->ops->join_mesh) {
1408 i++;
1409 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1410 goto nla_put_failure;
1411 }
1412 CMD(set_wds_peer, SET_WDS_PEER);
1413 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1414 CMD(tdls_mgmt, TDLS_MGMT);
1415 CMD(tdls_oper, TDLS_OPER);
1416 }
1417 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1418 CMD(sched_scan_start, START_SCHED_SCAN);
1419 CMD(probe_client, PROBE_CLIENT);
1420 CMD(set_noack_map, SET_NOACK_MAP);
1421 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1422 i++;
1423 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1424 goto nla_put_failure;
1425 }
1426 CMD(start_p2p_device, START_P2P_DEVICE);
1427 CMD(set_mcast_rate, SET_MCAST_RATE);
Arend van Spriel5de17982013-04-18 15:49:00 +02001428 if (split) {
1429 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1430 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1431 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001432
Kalle Valo4745fc02011-11-17 19:06:10 +02001433#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001434 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001435#endif
1436
Johannes Berg8fdc6212009-03-14 09:34:01 +01001437#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001438
Johannes Berg3713b4e2013-02-14 16:19:38 +01001439 if (dev->ops->connect || dev->ops->auth) {
1440 i++;
1441 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001442 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001443 }
1444
Johannes Berg3713b4e2013-02-14 16:19:38 +01001445 if (dev->ops->disconnect || dev->ops->deauth) {
1446 i++;
1447 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1448 goto nla_put_failure;
1449 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001450
Johannes Berg3713b4e2013-02-14 16:19:38 +01001451 nla_nest_end(msg, nl_cmds);
1452 (*split_start)++;
1453 if (split)
1454 break;
1455 case 5:
1456 if (dev->ops->remain_on_channel &&
1457 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1458 nla_put_u32(msg,
1459 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1460 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001461 goto nla_put_failure;
1462
Johannes Berg3713b4e2013-02-14 16:19:38 +01001463 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1464 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1465 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001466
Johannes Berg3713b4e2013-02-14 16:19:38 +01001467 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1468 goto nla_put_failure;
1469 (*split_start)++;
1470 if (split)
1471 break;
1472 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001473#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001474 if (nl80211_send_wowlan(msg, dev, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001475 goto nla_put_failure;
1476 (*split_start)++;
1477 if (split)
1478 break;
1479#else
1480 (*split_start)++;
1481#endif
1482 case 7:
1483 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1484 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001485 goto nla_put_failure;
1486
Johannes Bergcdc89b92013-02-18 23:54:36 +01001487 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001488 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001489
Johannes Berg3713b4e2013-02-14 16:19:38 +01001490 (*split_start)++;
1491 if (split)
1492 break;
1493 case 8:
1494 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1495 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1496 dev->wiphy.ap_sme_capa))
1497 goto nla_put_failure;
1498
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001499 features = dev->wiphy.features;
1500 /*
1501 * We can only add the per-channel limit information if the
1502 * dump is split, otherwise it makes it too big. Therefore
1503 * only advertise it in that case.
1504 */
1505 if (split)
1506 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1507 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001508 goto nla_put_failure;
1509
1510 if (dev->wiphy.ht_capa_mod_mask &&
1511 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1512 sizeof(*dev->wiphy.ht_capa_mod_mask),
1513 dev->wiphy.ht_capa_mod_mask))
1514 goto nla_put_failure;
1515
1516 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1517 dev->wiphy.max_acl_mac_addrs &&
1518 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1519 dev->wiphy.max_acl_mac_addrs))
1520 goto nla_put_failure;
1521
1522 /*
1523 * Any information below this point is only available to
1524 * applications that can deal with it being split. This
1525 * helps ensure that newly added capabilities don't break
1526 * older tools by overrunning their buffers.
1527 *
1528 * We still increment split_start so that in the split
1529 * case we'll continue with more data in the next round,
1530 * but break unconditionally so unsplit data stops here.
1531 */
1532 (*split_start)++;
1533 break;
1534 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001535 if (dev->wiphy.extended_capabilities &&
1536 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1537 dev->wiphy.extended_capabilities_len,
1538 dev->wiphy.extended_capabilities) ||
1539 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1540 dev->wiphy.extended_capabilities_len,
1541 dev->wiphy.extended_capabilities_mask)))
1542 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001543
Johannes Bergee2aca32013-02-21 17:36:01 +01001544 if (dev->wiphy.vht_capa_mod_mask &&
1545 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1546 sizeof(*dev->wiphy.vht_capa_mod_mask),
1547 dev->wiphy.vht_capa_mod_mask))
1548 goto nla_put_failure;
1549
Johannes Berg3713b4e2013-02-14 16:19:38 +01001550 /* done */
1551 *split_start = 0;
1552 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001553 }
Johannes Berg55682962007-09-20 13:09:35 -04001554 return genlmsg_end(msg, hdr);
1555
1556 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001557 genlmsg_cancel(msg, hdr);
1558 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001559}
1560
1561static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1562{
Johannes Berg645e77d2013-03-01 14:03:49 +01001563 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001564 int start = cb->args[0];
1565 struct cfg80211_registered_device *dev;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001566 s64 filter_wiphy = -1;
1567 bool split = false;
1568 struct nlattr **tb = nl80211_fam.attrbuf;
1569 int res;
Johannes Berg55682962007-09-20 13:09:35 -04001570
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001571 mutex_lock(&cfg80211_mutex);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001572 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1573 tb, nl80211_fam.maxattr, nl80211_policy);
1574 if (res == 0) {
1575 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1576 if (tb[NL80211_ATTR_WIPHY])
1577 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1578 if (tb[NL80211_ATTR_WDEV])
1579 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1580 if (tb[NL80211_ATTR_IFINDEX]) {
1581 struct net_device *netdev;
1582 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1583
1584 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1585 if (!netdev) {
1586 mutex_unlock(&cfg80211_mutex);
1587 return -ENODEV;
1588 }
1589 if (netdev->ieee80211_ptr) {
1590 dev = wiphy_to_dev(
1591 netdev->ieee80211_ptr->wiphy);
1592 filter_wiphy = dev->wiphy_idx;
1593 }
1594 dev_put(netdev);
1595 }
1596 }
1597
Johannes Berg79c97e92009-07-07 03:56:12 +02001598 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001599 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1600 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001601 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001602 continue;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001603 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1604 continue;
1605 /* attempt to fit multiple wiphy data chunks into the skb */
1606 do {
1607 ret = nl80211_send_wiphy(dev, skb,
1608 NETLINK_CB(cb->skb).portid,
1609 cb->nlh->nlmsg_seq,
1610 NLM_F_MULTI,
1611 split, &cb->args[1],
1612 &cb->args[2],
1613 &cb->args[3]);
1614 if (ret < 0) {
1615 /*
1616 * If sending the wiphy data didn't fit (ENOBUFS
1617 * or EMSGSIZE returned), this SKB is still
1618 * empty (so it's not too big because another
1619 * wiphy dataset is already in the skb) and
1620 * we've not tried to adjust the dump allocation
1621 * yet ... then adjust the alloc size to be
1622 * bigger, and return 1 but with the empty skb.
1623 * This results in an empty message being RX'ed
1624 * in userspace, but that is ignored.
1625 *
1626 * We can then retry with the larger buffer.
1627 */
1628 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1629 !skb->len &&
1630 cb->min_dump_alloc < 4096) {
1631 cb->min_dump_alloc = 4096;
1632 mutex_unlock(&cfg80211_mutex);
1633 return 1;
1634 }
1635 idx--;
1636 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001637 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001638 } while (cb->args[1] > 0);
1639 break;
Johannes Berg55682962007-09-20 13:09:35 -04001640 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001641 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001642
1643 cb->args[0] = idx;
1644
1645 return skb->len;
1646}
1647
1648static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1649{
1650 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001651 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001652
Johannes Berg645e77d2013-03-01 14:03:49 +01001653 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001654 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001655 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001656
Johannes Berg3713b4e2013-02-14 16:19:38 +01001657 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1658 false, NULL, NULL, NULL) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001659 nlmsg_free(msg);
1660 return -ENOBUFS;
1661 }
Johannes Berg55682962007-09-20 13:09:35 -04001662
Johannes Berg134e6372009-07-10 09:51:34 +00001663 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001664}
1665
Jouni Malinen31888482008-10-30 16:59:24 +02001666static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1667 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1668 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1669 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1670 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1671 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1672};
1673
1674static int parse_txq_params(struct nlattr *tb[],
1675 struct ieee80211_txq_params *txq_params)
1676{
Johannes Berga3304b02012-03-28 11:04:24 +02001677 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001678 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1679 !tb[NL80211_TXQ_ATTR_AIFS])
1680 return -EINVAL;
1681
Johannes Berga3304b02012-03-28 11:04:24 +02001682 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001683 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1684 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1685 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1686 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1687
Johannes Berga3304b02012-03-28 11:04:24 +02001688 if (txq_params->ac >= NL80211_NUM_ACS)
1689 return -EINVAL;
1690
Jouni Malinen31888482008-10-30 16:59:24 +02001691 return 0;
1692}
1693
Johannes Bergf444de02010-05-05 15:25:02 +02001694static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1695{
1696 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001697 * You can only set the channel explicitly for WDS interfaces,
1698 * all others have their channel managed via their respective
1699 * "establish a connection" command (connect, join, ...)
1700 *
1701 * For AP/GO and mesh mode, the channel can be set with the
1702 * channel userspace API, but is only stored and passed to the
1703 * low-level driver when the AP starts or the mesh is joined.
1704 * This is for backward compatibility, userspace can also give
1705 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001706 *
1707 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001708 * whatever else is going on, so they have their own special
1709 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001710 */
1711 return !wdev ||
1712 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001713 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001714 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1715 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001716}
1717
Johannes Berg683b6d32012-11-08 21:25:48 +01001718static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1719 struct genl_info *info,
1720 struct cfg80211_chan_def *chandef)
1721{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301722 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001723
1724 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1725 return -EINVAL;
1726
1727 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1728
1729 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001730 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1731 chandef->center_freq1 = control_freq;
1732 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001733
1734 /* Primary channel not allowed */
1735 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1736 return -EINVAL;
1737
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001738 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1739 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001740
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001741 chantype = nla_get_u32(
1742 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1743
1744 switch (chantype) {
1745 case NL80211_CHAN_NO_HT:
1746 case NL80211_CHAN_HT20:
1747 case NL80211_CHAN_HT40PLUS:
1748 case NL80211_CHAN_HT40MINUS:
1749 cfg80211_chandef_create(chandef, chandef->chan,
1750 chantype);
1751 break;
1752 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001753 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001754 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001755 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1756 chandef->width =
1757 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1758 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1759 chandef->center_freq1 =
1760 nla_get_u32(
1761 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1762 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1763 chandef->center_freq2 =
1764 nla_get_u32(
1765 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1766 }
1767
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001768 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001769 return -EINVAL;
1770
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001771 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1772 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001773 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001774
Johannes Berg683b6d32012-11-08 21:25:48 +01001775 return 0;
1776}
1777
Johannes Bergf444de02010-05-05 15:25:02 +02001778static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1779 struct wireless_dev *wdev,
1780 struct genl_info *info)
1781{
Johannes Berg683b6d32012-11-08 21:25:48 +01001782 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001783 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001784 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1785
1786 if (wdev)
1787 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001788
Johannes Bergf444de02010-05-05 15:25:02 +02001789 if (!nl80211_can_set_dev_channel(wdev))
1790 return -EOPNOTSUPP;
1791
Johannes Berg683b6d32012-11-08 21:25:48 +01001792 result = nl80211_parse_chandef(rdev, info, &chandef);
1793 if (result)
1794 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001795
1796 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001797 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001798 case NL80211_IFTYPE_AP:
1799 case NL80211_IFTYPE_P2P_GO:
1800 if (wdev->beacon_interval) {
1801 result = -EBUSY;
1802 break;
1803 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001804 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001805 result = -EINVAL;
1806 break;
1807 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001808 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001809 result = 0;
1810 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001811 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001812 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001813 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001814 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001815 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001816 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001817 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001818 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001819 }
1820 mutex_unlock(&rdev->devlist_mtx);
1821
1822 return result;
1823}
1824
1825static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1826{
Johannes Berg4c476992010-10-04 21:36:35 +02001827 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1828 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001829
Johannes Berg4c476992010-10-04 21:36:35 +02001830 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001831}
1832
Bill Jordane8347eb2010-10-01 13:54:28 -04001833static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1834{
Johannes Berg43b19952010-10-07 13:10:30 +02001835 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1836 struct net_device *dev = info->user_ptr[1];
1837 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001838 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001839
1840 if (!info->attrs[NL80211_ATTR_MAC])
1841 return -EINVAL;
1842
Johannes Berg43b19952010-10-07 13:10:30 +02001843 if (netif_running(dev))
1844 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001845
Johannes Berg43b19952010-10-07 13:10:30 +02001846 if (!rdev->ops->set_wds_peer)
1847 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001848
Johannes Berg43b19952010-10-07 13:10:30 +02001849 if (wdev->iftype != NL80211_IFTYPE_WDS)
1850 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001851
1852 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001853 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001854}
1855
1856
Johannes Berg55682962007-09-20 13:09:35 -04001857static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1858{
1859 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001860 struct net_device *netdev = NULL;
1861 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001862 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001863 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001864 u32 changed;
1865 u8 retry_short = 0, retry_long = 0;
1866 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001867 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001868
Johannes Bergf444de02010-05-05 15:25:02 +02001869 /*
1870 * Try to find the wiphy and netdev. Normally this
1871 * function shouldn't need the netdev, but this is
1872 * done for backward compatibility -- previously
1873 * setting the channel was done per wiphy, but now
1874 * it is per netdev. Previous userland like hostapd
1875 * also passed a netdev to set_wiphy, so that it is
1876 * possible to let that go to the right netdev!
1877 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001878 mutex_lock(&cfg80211_mutex);
1879
Johannes Bergf444de02010-05-05 15:25:02 +02001880 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1881 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1882
1883 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1884 if (netdev && netdev->ieee80211_ptr) {
1885 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1886 mutex_lock(&rdev->mtx);
1887 } else
1888 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001889 }
1890
Johannes Bergf444de02010-05-05 15:25:02 +02001891 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001892 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1893 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001894 if (IS_ERR(rdev)) {
1895 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001896 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001897 }
1898 wdev = NULL;
1899 netdev = NULL;
1900 result = 0;
1901
1902 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001903 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001904 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001905
1906 /*
1907 * end workaround code, by now the rdev is available
1908 * and locked, and wdev may or may not be NULL.
1909 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001910
1911 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001912 result = cfg80211_dev_rename(
1913 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001914
1915 mutex_unlock(&cfg80211_mutex);
1916
1917 if (result)
1918 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001919
Jouni Malinen31888482008-10-30 16:59:24 +02001920 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1921 struct ieee80211_txq_params txq_params;
1922 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1923
1924 if (!rdev->ops->set_txq_params) {
1925 result = -EOPNOTSUPP;
1926 goto bad_res;
1927 }
1928
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001929 if (!netdev) {
1930 result = -EINVAL;
1931 goto bad_res;
1932 }
1933
Johannes Berg133a3ff2011-11-03 14:50:13 +01001934 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1935 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1936 result = -EINVAL;
1937 goto bad_res;
1938 }
1939
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001940 if (!netif_running(netdev)) {
1941 result = -ENETDOWN;
1942 goto bad_res;
1943 }
1944
Jouni Malinen31888482008-10-30 16:59:24 +02001945 nla_for_each_nested(nl_txq_params,
1946 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1947 rem_txq_params) {
1948 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1949 nla_data(nl_txq_params),
1950 nla_len(nl_txq_params),
1951 txq_params_policy);
1952 result = parse_txq_params(tb, &txq_params);
1953 if (result)
1954 goto bad_res;
1955
Hila Gonene35e4d22012-06-27 17:19:42 +03001956 result = rdev_set_txq_params(rdev, netdev,
1957 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001958 if (result)
1959 goto bad_res;
1960 }
1961 }
1962
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001963 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001964 result = __nl80211_set_channel(rdev,
1965 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1966 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001967 if (result)
1968 goto bad_res;
1969 }
1970
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001971 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001972 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001973 enum nl80211_tx_power_setting type;
1974 int idx, mbm = 0;
1975
Johannes Bergc8442112012-10-24 10:17:18 +02001976 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1977 txp_wdev = NULL;
1978
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001979 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001980 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001981 goto bad_res;
1982 }
1983
1984 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1985 type = nla_get_u32(info->attrs[idx]);
1986
1987 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1988 (type != NL80211_TX_POWER_AUTOMATIC)) {
1989 result = -EINVAL;
1990 goto bad_res;
1991 }
1992
1993 if (type != NL80211_TX_POWER_AUTOMATIC) {
1994 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1995 mbm = nla_get_u32(info->attrs[idx]);
1996 }
1997
Johannes Bergc8442112012-10-24 10:17:18 +02001998 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001999 if (result)
2000 goto bad_res;
2001 }
2002
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002003 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2004 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2005 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002006 if ((!rdev->wiphy.available_antennas_tx &&
2007 !rdev->wiphy.available_antennas_rx) ||
2008 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002009 result = -EOPNOTSUPP;
2010 goto bad_res;
2011 }
2012
2013 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2014 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2015
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002016 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002017 * available antenna masks, except for the "all" mask */
2018 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2019 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002020 result = -EINVAL;
2021 goto bad_res;
2022 }
2023
Bruno Randolf7f531e02010-12-16 11:30:22 +09002024 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2025 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002026
Hila Gonene35e4d22012-06-27 17:19:42 +03002027 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002028 if (result)
2029 goto bad_res;
2030 }
2031
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002032 changed = 0;
2033
2034 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2035 retry_short = nla_get_u8(
2036 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2037 if (retry_short == 0) {
2038 result = -EINVAL;
2039 goto bad_res;
2040 }
2041 changed |= WIPHY_PARAM_RETRY_SHORT;
2042 }
2043
2044 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2045 retry_long = nla_get_u8(
2046 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2047 if (retry_long == 0) {
2048 result = -EINVAL;
2049 goto bad_res;
2050 }
2051 changed |= WIPHY_PARAM_RETRY_LONG;
2052 }
2053
2054 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2055 frag_threshold = nla_get_u32(
2056 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2057 if (frag_threshold < 256) {
2058 result = -EINVAL;
2059 goto bad_res;
2060 }
2061 if (frag_threshold != (u32) -1) {
2062 /*
2063 * Fragments (apart from the last one) are required to
2064 * have even length. Make the fragmentation code
2065 * simpler by stripping LSB should someone try to use
2066 * odd threshold value.
2067 */
2068 frag_threshold &= ~0x1;
2069 }
2070 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2071 }
2072
2073 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2074 rts_threshold = nla_get_u32(
2075 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2076 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2077 }
2078
Lukáš Turek81077e82009-12-21 22:50:47 +01002079 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2080 coverage_class = nla_get_u8(
2081 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2082 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2083 }
2084
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002085 if (changed) {
2086 u8 old_retry_short, old_retry_long;
2087 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002088 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002089
2090 if (!rdev->ops->set_wiphy_params) {
2091 result = -EOPNOTSUPP;
2092 goto bad_res;
2093 }
2094
2095 old_retry_short = rdev->wiphy.retry_short;
2096 old_retry_long = rdev->wiphy.retry_long;
2097 old_frag_threshold = rdev->wiphy.frag_threshold;
2098 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002099 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002100
2101 if (changed & WIPHY_PARAM_RETRY_SHORT)
2102 rdev->wiphy.retry_short = retry_short;
2103 if (changed & WIPHY_PARAM_RETRY_LONG)
2104 rdev->wiphy.retry_long = retry_long;
2105 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2106 rdev->wiphy.frag_threshold = frag_threshold;
2107 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2108 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002109 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2110 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002111
Hila Gonene35e4d22012-06-27 17:19:42 +03002112 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002113 if (result) {
2114 rdev->wiphy.retry_short = old_retry_short;
2115 rdev->wiphy.retry_long = old_retry_long;
2116 rdev->wiphy.frag_threshold = old_frag_threshold;
2117 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002118 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002119 }
2120 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002121
Johannes Berg306d6112008-12-08 12:39:04 +01002122 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002123 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02002124 if (netdev)
2125 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002126 return result;
2127}
2128
Johannes Berg71bbc992012-06-15 15:30:18 +02002129static inline u64 wdev_id(struct wireless_dev *wdev)
2130{
2131 return (u64)wdev->identifier |
2132 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2133}
Johannes Berg55682962007-09-20 13:09:35 -04002134
Johannes Berg683b6d32012-11-08 21:25:48 +01002135static int nl80211_send_chandef(struct sk_buff *msg,
2136 struct cfg80211_chan_def *chandef)
2137{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002138 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002139
Johannes Berg683b6d32012-11-08 21:25:48 +01002140 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2141 chandef->chan->center_freq))
2142 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002143 switch (chandef->width) {
2144 case NL80211_CHAN_WIDTH_20_NOHT:
2145 case NL80211_CHAN_WIDTH_20:
2146 case NL80211_CHAN_WIDTH_40:
2147 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2148 cfg80211_get_chandef_type(chandef)))
2149 return -ENOBUFS;
2150 break;
2151 default:
2152 break;
2153 }
2154 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2155 return -ENOBUFS;
2156 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2157 return -ENOBUFS;
2158 if (chandef->center_freq2 &&
2159 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002160 return -ENOBUFS;
2161 return 0;
2162}
2163
Eric W. Biederman15e47302012-09-07 20:12:54 +00002164static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002165 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002166 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002167{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002168 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002169 void *hdr;
2170
Eric W. Biederman15e47302012-09-07 20:12:54 +00002171 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002172 if (!hdr)
2173 return -1;
2174
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002175 if (dev &&
2176 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002177 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002178 goto nla_put_failure;
2179
2180 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2181 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002182 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002183 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002184 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2185 rdev->devlist_generation ^
2186 (cfg80211_rdev_list_generation << 2)))
2187 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002188
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002189 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002190 int ret;
2191 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002192
Johannes Berg683b6d32012-11-08 21:25:48 +01002193 ret = rdev_get_channel(rdev, wdev, &chandef);
2194 if (ret == 0) {
2195 if (nl80211_send_chandef(msg, &chandef))
2196 goto nla_put_failure;
2197 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002198 }
2199
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002200 if (wdev->ssid_len) {
2201 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2202 goto nla_put_failure;
2203 }
2204
Johannes Berg55682962007-09-20 13:09:35 -04002205 return genlmsg_end(msg, hdr);
2206
2207 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002208 genlmsg_cancel(msg, hdr);
2209 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002210}
2211
2212static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2213{
2214 int wp_idx = 0;
2215 int if_idx = 0;
2216 int wp_start = cb->args[0];
2217 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002218 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002219 struct wireless_dev *wdev;
2220
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002221 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02002222 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2223 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002224 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002225 if (wp_idx < wp_start) {
2226 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002227 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002228 }
Johannes Berg55682962007-09-20 13:09:35 -04002229 if_idx = 0;
2230
Johannes Bergf5ea9122009-08-07 16:17:38 +02002231 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02002232 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002233 if (if_idx < if_start) {
2234 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002235 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002236 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002237 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002238 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002239 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002240 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002241 goto out;
2242 }
2243 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002244 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002245 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002246
2247 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002248 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002249 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002250 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002251
2252 cb->args[0] = wp_idx;
2253 cb->args[1] = if_idx;
2254
2255 return skb->len;
2256}
2257
2258static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2259{
2260 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002261 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002262 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002263
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002264 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002265 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002266 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002267
Eric W. Biederman15e47302012-09-07 20:12:54 +00002268 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002269 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002270 nlmsg_free(msg);
2271 return -ENOBUFS;
2272 }
Johannes Berg55682962007-09-20 13:09:35 -04002273
Johannes Berg134e6372009-07-10 09:51:34 +00002274 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002275}
2276
Michael Wu66f7ac52008-01-31 19:48:22 +01002277static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2278 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2279 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2280 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2281 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2282 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2283};
2284
2285static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2286{
2287 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2288 int flag;
2289
2290 *mntrflags = 0;
2291
2292 if (!nla)
2293 return -EINVAL;
2294
2295 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2296 nla, mntr_flags_policy))
2297 return -EINVAL;
2298
2299 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2300 if (flags[flag])
2301 *mntrflags |= (1<<flag);
2302
2303 return 0;
2304}
2305
Johannes Berg9bc383d2009-11-19 11:55:19 +01002306static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002307 struct net_device *netdev, u8 use_4addr,
2308 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002309{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002310 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002311 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002312 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002313 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002314 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002315
2316 switch (iftype) {
2317 case NL80211_IFTYPE_AP_VLAN:
2318 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2319 return 0;
2320 break;
2321 case NL80211_IFTYPE_STATION:
2322 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2323 return 0;
2324 break;
2325 default:
2326 break;
2327 }
2328
2329 return -EOPNOTSUPP;
2330}
2331
Johannes Berg55682962007-09-20 13:09:35 -04002332static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2333{
Johannes Berg4c476992010-10-04 21:36:35 +02002334 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002335 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002336 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002337 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002338 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002339 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002340 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002341
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002342 memset(&params, 0, sizeof(params));
2343
Johannes Berg04a773a2009-04-19 21:24:32 +02002344 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002345
Johannes Berg723b0382008-09-16 20:22:09 +02002346 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002347 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002348 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002349 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002350 if (ntype > NL80211_IFTYPE_MAX)
2351 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002352 }
2353
Johannes Berg92ffe052008-09-16 20:39:36 +02002354 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002355 struct wireless_dev *wdev = dev->ieee80211_ptr;
2356
Johannes Berg4c476992010-10-04 21:36:35 +02002357 if (ntype != NL80211_IFTYPE_MESH_POINT)
2358 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002359 if (netif_running(dev))
2360 return -EBUSY;
2361
2362 wdev_lock(wdev);
2363 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2364 IEEE80211_MAX_MESH_ID_LEN);
2365 wdev->mesh_id_up_len =
2366 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2367 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2368 wdev->mesh_id_up_len);
2369 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002370 }
2371
Felix Fietkau8b787642009-11-10 18:53:10 +01002372 if (info->attrs[NL80211_ATTR_4ADDR]) {
2373 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2374 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002375 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002376 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002377 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002378 } else {
2379 params.use_4addr = -1;
2380 }
2381
Johannes Berg92ffe052008-09-16 20:39:36 +02002382 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002383 if (ntype != NL80211_IFTYPE_MONITOR)
2384 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002385 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2386 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002387 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002388 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002389
2390 flags = &_flags;
2391 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002392 }
Johannes Berg3b858752009-03-12 09:55:09 +01002393
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002394 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002395 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002396 else
2397 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002398
Johannes Berg9bc383d2009-11-19 11:55:19 +01002399 if (!err && params.use_4addr != -1)
2400 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2401
Johannes Berg55682962007-09-20 13:09:35 -04002402 return err;
2403}
2404
2405static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2406{
Johannes Berg4c476992010-10-04 21:36:35 +02002407 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002408 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002409 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002410 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002411 int err;
2412 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002413 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002414
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002415 memset(&params, 0, sizeof(params));
2416
Johannes Berg55682962007-09-20 13:09:35 -04002417 if (!info->attrs[NL80211_ATTR_IFNAME])
2418 return -EINVAL;
2419
2420 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2421 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2422 if (type > NL80211_IFTYPE_MAX)
2423 return -EINVAL;
2424 }
2425
Johannes Berg79c97e92009-07-07 03:56:12 +02002426 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002427 !(rdev->wiphy.interface_modes & (1 << type)))
2428 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002429
Arend van Spriel1c18f142013-01-08 10:17:27 +01002430 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2431 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2432 ETH_ALEN);
2433 if (!is_valid_ether_addr(params.macaddr))
2434 return -EADDRNOTAVAIL;
2435 }
2436
Johannes Berg9bc383d2009-11-19 11:55:19 +01002437 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002438 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002439 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002440 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002441 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002442 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002443
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002444 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2445 if (!msg)
2446 return -ENOMEM;
2447
Michael Wu66f7ac52008-01-31 19:48:22 +01002448 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2449 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2450 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002451 wdev = rdev_add_virtual_intf(rdev,
2452 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2453 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002454 if (IS_ERR(wdev)) {
2455 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002456 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002457 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002458
Johannes Berg98104fde2012-06-16 00:19:54 +02002459 switch (type) {
2460 case NL80211_IFTYPE_MESH_POINT:
2461 if (!info->attrs[NL80211_ATTR_MESH_ID])
2462 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002463 wdev_lock(wdev);
2464 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2465 IEEE80211_MAX_MESH_ID_LEN);
2466 wdev->mesh_id_up_len =
2467 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2468 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2469 wdev->mesh_id_up_len);
2470 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002471 break;
2472 case NL80211_IFTYPE_P2P_DEVICE:
2473 /*
2474 * P2P Device doesn't have a netdev, so doesn't go
2475 * through the netdev notifier and must be added here
2476 */
2477 mutex_init(&wdev->mtx);
2478 INIT_LIST_HEAD(&wdev->event_list);
2479 spin_lock_init(&wdev->event_lock);
2480 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2481 spin_lock_init(&wdev->mgmt_registrations_lock);
2482
2483 mutex_lock(&rdev->devlist_mtx);
2484 wdev->identifier = ++rdev->wdev_id;
2485 list_add_rcu(&wdev->list, &rdev->wdev_list);
2486 rdev->devlist_generation++;
2487 mutex_unlock(&rdev->devlist_mtx);
2488 break;
2489 default:
2490 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002491 }
2492
Eric W. Biederman15e47302012-09-07 20:12:54 +00002493 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002494 rdev, wdev) < 0) {
2495 nlmsg_free(msg);
2496 return -ENOBUFS;
2497 }
2498
2499 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002500}
2501
2502static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2503{
Johannes Berg4c476992010-10-04 21:36:35 +02002504 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002505 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002506
Johannes Berg4c476992010-10-04 21:36:35 +02002507 if (!rdev->ops->del_virtual_intf)
2508 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002509
Johannes Berg84efbb82012-06-16 00:00:26 +02002510 /*
2511 * If we remove a wireless device without a netdev then clear
2512 * user_ptr[1] so that nl80211_post_doit won't dereference it
2513 * to check if it needs to do dev_put(). Otherwise it crashes
2514 * since the wdev has been freed, unlike with a netdev where
2515 * we need the dev_put() for the netdev to really be freed.
2516 */
2517 if (!wdev->netdev)
2518 info->user_ptr[1] = NULL;
2519
Hila Gonene35e4d22012-06-27 17:19:42 +03002520 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002521}
2522
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002523static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2524{
2525 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2526 struct net_device *dev = info->user_ptr[1];
2527 u16 noack_map;
2528
2529 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2530 return -EINVAL;
2531
2532 if (!rdev->ops->set_noack_map)
2533 return -EOPNOTSUPP;
2534
2535 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2536
Hila Gonene35e4d22012-06-27 17:19:42 +03002537 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002538}
2539
Johannes Berg41ade002007-12-19 02:03:29 +01002540struct get_key_cookie {
2541 struct sk_buff *msg;
2542 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002543 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002544};
2545
2546static void get_key_callback(void *c, struct key_params *params)
2547{
Johannes Bergb9454e82009-07-08 13:29:08 +02002548 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002549 struct get_key_cookie *cookie = c;
2550
David S. Miller9360ffd2012-03-29 04:41:26 -04002551 if ((params->key &&
2552 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2553 params->key_len, params->key)) ||
2554 (params->seq &&
2555 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2556 params->seq_len, params->seq)) ||
2557 (params->cipher &&
2558 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2559 params->cipher)))
2560 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002561
Johannes Bergb9454e82009-07-08 13:29:08 +02002562 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2563 if (!key)
2564 goto nla_put_failure;
2565
David S. Miller9360ffd2012-03-29 04:41:26 -04002566 if ((params->key &&
2567 nla_put(cookie->msg, NL80211_KEY_DATA,
2568 params->key_len, params->key)) ||
2569 (params->seq &&
2570 nla_put(cookie->msg, NL80211_KEY_SEQ,
2571 params->seq_len, params->seq)) ||
2572 (params->cipher &&
2573 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2574 params->cipher)))
2575 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002576
David S. Miller9360ffd2012-03-29 04:41:26 -04002577 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2578 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002579
2580 nla_nest_end(cookie->msg, key);
2581
Johannes Berg41ade002007-12-19 02:03:29 +01002582 return;
2583 nla_put_failure:
2584 cookie->error = 1;
2585}
2586
2587static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2588{
Johannes Berg4c476992010-10-04 21:36:35 +02002589 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002590 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002591 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002592 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002593 const u8 *mac_addr = NULL;
2594 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002595 struct get_key_cookie cookie = {
2596 .error = 0,
2597 };
2598 void *hdr;
2599 struct sk_buff *msg;
2600
2601 if (info->attrs[NL80211_ATTR_KEY_IDX])
2602 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2603
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002604 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002605 return -EINVAL;
2606
2607 if (info->attrs[NL80211_ATTR_MAC])
2608 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2609
Johannes Berge31b8212010-10-05 19:39:30 +02002610 pairwise = !!mac_addr;
2611 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2612 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2613 if (kt >= NUM_NL80211_KEYTYPES)
2614 return -EINVAL;
2615 if (kt != NL80211_KEYTYPE_GROUP &&
2616 kt != NL80211_KEYTYPE_PAIRWISE)
2617 return -EINVAL;
2618 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2619 }
2620
Johannes Berg4c476992010-10-04 21:36:35 +02002621 if (!rdev->ops->get_key)
2622 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002623
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002624 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002625 if (!msg)
2626 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002627
Eric W. Biederman15e47302012-09-07 20:12:54 +00002628 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002629 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002630 if (IS_ERR(hdr))
2631 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002632
2633 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002634 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002635
David S. Miller9360ffd2012-03-29 04:41:26 -04002636 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2637 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2638 goto nla_put_failure;
2639 if (mac_addr &&
2640 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2641 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002642
Johannes Berge31b8212010-10-05 19:39:30 +02002643 if (pairwise && mac_addr &&
2644 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2645 return -ENOENT;
2646
Hila Gonene35e4d22012-06-27 17:19:42 +03002647 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2648 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002649
2650 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002651 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002652
2653 if (cookie.error)
2654 goto nla_put_failure;
2655
2656 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002657 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002658
2659 nla_put_failure:
2660 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002661 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002662 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002663 return err;
2664}
2665
2666static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2667{
Johannes Berg4c476992010-10-04 21:36:35 +02002668 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002669 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002670 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002671 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002672
Johannes Bergb9454e82009-07-08 13:29:08 +02002673 err = nl80211_parse_key(info, &key);
2674 if (err)
2675 return err;
2676
2677 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002678 return -EINVAL;
2679
Johannes Bergb9454e82009-07-08 13:29:08 +02002680 /* only support setting default key */
2681 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002682 return -EINVAL;
2683
Johannes Bergfffd0932009-07-08 14:22:54 +02002684 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002685
2686 if (key.def) {
2687 if (!rdev->ops->set_default_key) {
2688 err = -EOPNOTSUPP;
2689 goto out;
2690 }
2691
2692 err = nl80211_key_allowed(dev->ieee80211_ptr);
2693 if (err)
2694 goto out;
2695
Hila Gonene35e4d22012-06-27 17:19:42 +03002696 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002697 key.def_uni, key.def_multi);
2698
2699 if (err)
2700 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002701
Johannes Berg3d23e342009-09-29 23:27:28 +02002702#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002703 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002704#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002705 } else {
2706 if (key.def_uni || !key.def_multi) {
2707 err = -EINVAL;
2708 goto out;
2709 }
2710
2711 if (!rdev->ops->set_default_mgmt_key) {
2712 err = -EOPNOTSUPP;
2713 goto out;
2714 }
2715
2716 err = nl80211_key_allowed(dev->ieee80211_ptr);
2717 if (err)
2718 goto out;
2719
Hila Gonene35e4d22012-06-27 17:19:42 +03002720 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002721 if (err)
2722 goto out;
2723
2724#ifdef CONFIG_CFG80211_WEXT
2725 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2726#endif
2727 }
2728
2729 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002730 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002731
Johannes Berg41ade002007-12-19 02:03:29 +01002732 return err;
2733}
2734
2735static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2736{
Johannes Berg4c476992010-10-04 21:36:35 +02002737 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002738 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002739 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002740 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002741 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002742
Johannes Bergb9454e82009-07-08 13:29:08 +02002743 err = nl80211_parse_key(info, &key);
2744 if (err)
2745 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002746
Johannes Bergb9454e82009-07-08 13:29:08 +02002747 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002748 return -EINVAL;
2749
Johannes Berg41ade002007-12-19 02:03:29 +01002750 if (info->attrs[NL80211_ATTR_MAC])
2751 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2752
Johannes Berge31b8212010-10-05 19:39:30 +02002753 if (key.type == -1) {
2754 if (mac_addr)
2755 key.type = NL80211_KEYTYPE_PAIRWISE;
2756 else
2757 key.type = NL80211_KEYTYPE_GROUP;
2758 }
2759
2760 /* for now */
2761 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2762 key.type != NL80211_KEYTYPE_GROUP)
2763 return -EINVAL;
2764
Johannes Berg4c476992010-10-04 21:36:35 +02002765 if (!rdev->ops->add_key)
2766 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002767
Johannes Berge31b8212010-10-05 19:39:30 +02002768 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2769 key.type == NL80211_KEYTYPE_PAIRWISE,
2770 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002771 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002772
2773 wdev_lock(dev->ieee80211_ptr);
2774 err = nl80211_key_allowed(dev->ieee80211_ptr);
2775 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002776 err = rdev_add_key(rdev, dev, key.idx,
2777 key.type == NL80211_KEYTYPE_PAIRWISE,
2778 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002779 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002780
Johannes Berg41ade002007-12-19 02:03:29 +01002781 return err;
2782}
2783
2784static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2785{
Johannes Berg4c476992010-10-04 21:36:35 +02002786 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002787 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002788 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002789 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002790 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002791
Johannes Bergb9454e82009-07-08 13:29:08 +02002792 err = nl80211_parse_key(info, &key);
2793 if (err)
2794 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002795
2796 if (info->attrs[NL80211_ATTR_MAC])
2797 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2798
Johannes Berge31b8212010-10-05 19:39:30 +02002799 if (key.type == -1) {
2800 if (mac_addr)
2801 key.type = NL80211_KEYTYPE_PAIRWISE;
2802 else
2803 key.type = NL80211_KEYTYPE_GROUP;
2804 }
2805
2806 /* for now */
2807 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2808 key.type != NL80211_KEYTYPE_GROUP)
2809 return -EINVAL;
2810
Johannes Berg4c476992010-10-04 21:36:35 +02002811 if (!rdev->ops->del_key)
2812 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002813
Johannes Bergfffd0932009-07-08 14:22:54 +02002814 wdev_lock(dev->ieee80211_ptr);
2815 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002816
2817 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2818 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2819 err = -ENOENT;
2820
Johannes Bergfffd0932009-07-08 14:22:54 +02002821 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002822 err = rdev_del_key(rdev, dev, key.idx,
2823 key.type == NL80211_KEYTYPE_PAIRWISE,
2824 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002825
Johannes Berg3d23e342009-09-29 23:27:28 +02002826#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002827 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002828 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002829 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002830 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002831 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2832 }
2833#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002834 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002835
Johannes Berg41ade002007-12-19 02:03:29 +01002836 return err;
2837}
2838
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302839/* This function returns an error or the number of nested attributes */
2840static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2841{
2842 struct nlattr *attr;
2843 int n_entries = 0, tmp;
2844
2845 nla_for_each_nested(attr, nl_attr, tmp) {
2846 if (nla_len(attr) != ETH_ALEN)
2847 return -EINVAL;
2848
2849 n_entries++;
2850 }
2851
2852 return n_entries;
2853}
2854
2855/*
2856 * This function parses ACL information and allocates memory for ACL data.
2857 * On successful return, the calling function is responsible to free the
2858 * ACL buffer returned by this function.
2859 */
2860static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2861 struct genl_info *info)
2862{
2863 enum nl80211_acl_policy acl_policy;
2864 struct nlattr *attr;
2865 struct cfg80211_acl_data *acl;
2866 int i = 0, n_entries, tmp;
2867
2868 if (!wiphy->max_acl_mac_addrs)
2869 return ERR_PTR(-EOPNOTSUPP);
2870
2871 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2872 return ERR_PTR(-EINVAL);
2873
2874 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2875 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2876 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2877 return ERR_PTR(-EINVAL);
2878
2879 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2880 return ERR_PTR(-EINVAL);
2881
2882 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2883 if (n_entries < 0)
2884 return ERR_PTR(n_entries);
2885
2886 if (n_entries > wiphy->max_acl_mac_addrs)
2887 return ERR_PTR(-ENOTSUPP);
2888
2889 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2890 GFP_KERNEL);
2891 if (!acl)
2892 return ERR_PTR(-ENOMEM);
2893
2894 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2895 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2896 i++;
2897 }
2898
2899 acl->n_acl_entries = n_entries;
2900 acl->acl_policy = acl_policy;
2901
2902 return acl;
2903}
2904
2905static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2906{
2907 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2908 struct net_device *dev = info->user_ptr[1];
2909 struct cfg80211_acl_data *acl;
2910 int err;
2911
2912 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2913 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2914 return -EOPNOTSUPP;
2915
2916 if (!dev->ieee80211_ptr->beacon_interval)
2917 return -EINVAL;
2918
2919 acl = parse_acl_data(&rdev->wiphy, info);
2920 if (IS_ERR(acl))
2921 return PTR_ERR(acl);
2922
2923 err = rdev_set_mac_acl(rdev, dev, acl);
2924
2925 kfree(acl);
2926
2927 return err;
2928}
2929
Johannes Berg88600202012-02-13 15:17:18 +01002930static int nl80211_parse_beacon(struct genl_info *info,
2931 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002932{
Johannes Berg88600202012-02-13 15:17:18 +01002933 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002934
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002935 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2936 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2937 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2938 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002939 return -EINVAL;
2940
Johannes Berg88600202012-02-13 15:17:18 +01002941 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002942
Johannes Berged1b6cc2007-12-19 02:03:32 +01002943 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002944 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2945 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2946 if (!bcn->head_len)
2947 return -EINVAL;
2948 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002949 }
2950
2951 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002952 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2953 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002954 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002955 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002956 }
2957
Johannes Berg4c476992010-10-04 21:36:35 +02002958 if (!haveinfo)
2959 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002960
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002961 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002962 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2963 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002964 }
2965
2966 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002967 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002968 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002969 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002970 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2971 }
2972
2973 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002974 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002975 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002976 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002977 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2978 }
2979
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002980 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002981 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002982 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002983 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002984 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2985 }
2986
Johannes Berg88600202012-02-13 15:17:18 +01002987 return 0;
2988}
2989
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002990static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2991 struct cfg80211_ap_settings *params)
2992{
2993 struct wireless_dev *wdev;
2994 bool ret = false;
2995
2996 mutex_lock(&rdev->devlist_mtx);
2997
Johannes Berg89a54e42012-06-15 14:33:17 +02002998 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002999 if (wdev->iftype != NL80211_IFTYPE_AP &&
3000 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3001 continue;
3002
Johannes Berg683b6d32012-11-08 21:25:48 +01003003 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003004 continue;
3005
Johannes Berg683b6d32012-11-08 21:25:48 +01003006 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003007 ret = true;
3008 break;
3009 }
3010
3011 mutex_unlock(&rdev->devlist_mtx);
3012
3013 return ret;
3014}
3015
Jouni Malinene39e5b52012-09-30 19:29:39 +03003016static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3017 enum nl80211_auth_type auth_type,
3018 enum nl80211_commands cmd)
3019{
3020 if (auth_type > NL80211_AUTHTYPE_MAX)
3021 return false;
3022
3023 switch (cmd) {
3024 case NL80211_CMD_AUTHENTICATE:
3025 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3026 auth_type == NL80211_AUTHTYPE_SAE)
3027 return false;
3028 return true;
3029 case NL80211_CMD_CONNECT:
3030 case NL80211_CMD_START_AP:
3031 /* SAE not supported yet */
3032 if (auth_type == NL80211_AUTHTYPE_SAE)
3033 return false;
3034 return true;
3035 default:
3036 return false;
3037 }
3038}
3039
Johannes Berg88600202012-02-13 15:17:18 +01003040static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3041{
3042 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3043 struct net_device *dev = info->user_ptr[1];
3044 struct wireless_dev *wdev = dev->ieee80211_ptr;
3045 struct cfg80211_ap_settings params;
3046 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003047 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003048
3049 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3050 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3051 return -EOPNOTSUPP;
3052
3053 if (!rdev->ops->start_ap)
3054 return -EOPNOTSUPP;
3055
3056 if (wdev->beacon_interval)
3057 return -EALREADY;
3058
3059 memset(&params, 0, sizeof(params));
3060
3061 /* these are required for START_AP */
3062 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3063 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3064 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3065 return -EINVAL;
3066
3067 err = nl80211_parse_beacon(info, &params.beacon);
3068 if (err)
3069 return err;
3070
3071 params.beacon_interval =
3072 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3073 params.dtim_period =
3074 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3075
3076 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3077 if (err)
3078 return err;
3079
3080 /*
3081 * In theory, some of these attributes should be required here
3082 * but since they were not used when the command was originally
3083 * added, keep them optional for old user space programs to let
3084 * them continue to work with drivers that do not need the
3085 * additional information -- drivers must check!
3086 */
3087 if (info->attrs[NL80211_ATTR_SSID]) {
3088 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3089 params.ssid_len =
3090 nla_len(info->attrs[NL80211_ATTR_SSID]);
3091 if (params.ssid_len == 0 ||
3092 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3093 return -EINVAL;
3094 }
3095
3096 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3097 params.hidden_ssid = nla_get_u32(
3098 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3099 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3100 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3101 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3102 return -EINVAL;
3103 }
3104
3105 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3106
3107 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3108 params.auth_type = nla_get_u32(
3109 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003110 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3111 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003112 return -EINVAL;
3113 } else
3114 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3115
3116 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3117 NL80211_MAX_NR_CIPHER_SUITES);
3118 if (err)
3119 return err;
3120
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303121 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3122 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3123 return -EOPNOTSUPP;
3124 params.inactivity_timeout = nla_get_u16(
3125 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3126 }
3127
Johannes Berg53cabad2012-11-14 15:17:28 +01003128 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3129 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3130 return -EINVAL;
3131 params.p2p_ctwindow =
3132 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3133 if (params.p2p_ctwindow > 127)
3134 return -EINVAL;
3135 if (params.p2p_ctwindow != 0 &&
3136 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3137 return -EINVAL;
3138 }
3139
3140 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3141 u8 tmp;
3142
3143 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3144 return -EINVAL;
3145 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3146 if (tmp > 1)
3147 return -EINVAL;
3148 params.p2p_opp_ps = tmp;
3149 if (params.p2p_opp_ps != 0 &&
3150 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3151 return -EINVAL;
3152 }
3153
Johannes Bergaa430da2012-05-16 23:50:18 +02003154 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003155 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3156 if (err)
3157 return err;
3158 } else if (wdev->preset_chandef.chan) {
3159 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003160 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003161 return -EINVAL;
3162
Johannes Berg683b6d32012-11-08 21:25:48 +01003163 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003164 return -EINVAL;
3165
Simon Wunderlich04f39042013-02-08 18:16:19 +01003166 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3167 if (err < 0)
3168 return err;
3169 if (err) {
3170 radar_detect_width = BIT(params.chandef.width);
3171 params.radar_required = true;
3172 }
3173
Michal Kaziore4e32452012-06-29 12:47:08 +02003174 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003175 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3176 params.chandef.chan,
3177 CHAN_MODE_SHARED,
3178 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003179 mutex_unlock(&rdev->devlist_mtx);
3180
3181 if (err)
3182 return err;
3183
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303184 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3185 params.acl = parse_acl_data(&rdev->wiphy, info);
3186 if (IS_ERR(params.acl))
3187 return PTR_ERR(params.acl);
3188 }
3189
Hila Gonene35e4d22012-06-27 17:19:42 +03003190 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003191 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003192 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003193 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003194 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003195 wdev->ssid_len = params.ssid_len;
3196 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003197 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303198
3199 kfree(params.acl);
3200
Johannes Berg56d18932011-05-09 18:41:15 +02003201 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003202}
3203
Johannes Berg88600202012-02-13 15:17:18 +01003204static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3205{
3206 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3207 struct net_device *dev = info->user_ptr[1];
3208 struct wireless_dev *wdev = dev->ieee80211_ptr;
3209 struct cfg80211_beacon_data params;
3210 int err;
3211
3212 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3213 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3214 return -EOPNOTSUPP;
3215
3216 if (!rdev->ops->change_beacon)
3217 return -EOPNOTSUPP;
3218
3219 if (!wdev->beacon_interval)
3220 return -EINVAL;
3221
3222 err = nl80211_parse_beacon(info, &params);
3223 if (err)
3224 return err;
3225
Hila Gonene35e4d22012-06-27 17:19:42 +03003226 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003227}
3228
3229static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003230{
Johannes Berg4c476992010-10-04 21:36:35 +02003231 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3232 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003233
Michal Kazior60771782012-06-29 12:46:56 +02003234 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003235}
3236
Johannes Berg5727ef12007-12-19 02:03:34 +01003237static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3238 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3239 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3240 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003241 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003242 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003243 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003244};
3245
Johannes Bergeccb8e82009-05-11 21:57:56 +03003246static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003247 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003248 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003249{
3250 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003251 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003252 int flag;
3253
Johannes Bergeccb8e82009-05-11 21:57:56 +03003254 /*
3255 * Try parsing the new attribute first so userspace
3256 * can specify both for older kernels.
3257 */
3258 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3259 if (nla) {
3260 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003261
Johannes Bergeccb8e82009-05-11 21:57:56 +03003262 sta_flags = nla_data(nla);
3263 params->sta_flags_mask = sta_flags->mask;
3264 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003265 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003266 if ((params->sta_flags_mask |
3267 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3268 return -EINVAL;
3269 return 0;
3270 }
3271
3272 /* if present, parse the old attribute */
3273
3274 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003275 if (!nla)
3276 return 0;
3277
3278 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3279 nla, sta_flags_policy))
3280 return -EINVAL;
3281
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003282 /*
3283 * Only allow certain flags for interface types so that
3284 * other attributes are silently ignored. Remember that
3285 * this is backward compatibility code with old userspace
3286 * and shouldn't be hit in other cases anyway.
3287 */
3288 switch (iftype) {
3289 case NL80211_IFTYPE_AP:
3290 case NL80211_IFTYPE_AP_VLAN:
3291 case NL80211_IFTYPE_P2P_GO:
3292 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3293 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3294 BIT(NL80211_STA_FLAG_WME) |
3295 BIT(NL80211_STA_FLAG_MFP);
3296 break;
3297 case NL80211_IFTYPE_P2P_CLIENT:
3298 case NL80211_IFTYPE_STATION:
3299 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3300 BIT(NL80211_STA_FLAG_TDLS_PEER);
3301 break;
3302 case NL80211_IFTYPE_MESH_POINT:
3303 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3304 BIT(NL80211_STA_FLAG_MFP) |
3305 BIT(NL80211_STA_FLAG_AUTHORIZED);
3306 default:
3307 return -EINVAL;
3308 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003309
Johannes Berg3383b5a2012-05-10 20:14:43 +02003310 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3311 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003312 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003313
Johannes Berg3383b5a2012-05-10 20:14:43 +02003314 /* no longer support new API additions in old API */
3315 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3316 return -EINVAL;
3317 }
3318 }
3319
Johannes Berg5727ef12007-12-19 02:03:34 +01003320 return 0;
3321}
3322
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003323static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3324 int attr)
3325{
3326 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003327 u32 bitrate;
3328 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003329
3330 rate = nla_nest_start(msg, attr);
3331 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003332 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003333
3334 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3335 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003336 /* report 16-bit bitrate only if we can */
3337 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003338 if (bitrate > 0 &&
3339 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3340 return false;
3341 if (bitrate_compat > 0 &&
3342 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3343 return false;
3344
3345 if (info->flags & RATE_INFO_FLAGS_MCS) {
3346 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3347 return false;
3348 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3349 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3350 return false;
3351 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3352 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3353 return false;
3354 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3355 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3356 return false;
3357 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3358 return false;
3359 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3360 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3361 return false;
3362 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3363 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3364 return false;
3365 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3366 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3367 return false;
3368 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3369 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3370 return false;
3371 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3372 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3373 return false;
3374 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003375
3376 nla_nest_end(msg, rate);
3377 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003378}
3379
Felix Fietkau119363c2013-04-22 16:29:30 +02003380static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3381 int id)
3382{
3383 void *attr;
3384 int i = 0;
3385
3386 if (!mask)
3387 return true;
3388
3389 attr = nla_nest_start(msg, id);
3390 if (!attr)
3391 return false;
3392
3393 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3394 if (!(mask & BIT(i)))
3395 continue;
3396
3397 if (nla_put_u8(msg, i, signal[i]))
3398 return false;
3399 }
3400
3401 nla_nest_end(msg, attr);
3402
3403 return true;
3404}
3405
Eric W. Biederman15e47302012-09-07 20:12:54 +00003406static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003407 int flags,
3408 struct cfg80211_registered_device *rdev,
3409 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003410 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003411{
3412 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003413 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003414
Eric W. Biederman15e47302012-09-07 20:12:54 +00003415 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003416 if (!hdr)
3417 return -1;
3418
David S. Miller9360ffd2012-03-29 04:41:26 -04003419 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3420 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3421 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3422 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003423
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003424 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3425 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003426 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003427 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3428 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3429 sinfo->connected_time))
3430 goto nla_put_failure;
3431 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3432 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3433 sinfo->inactive_time))
3434 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003435 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3436 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003437 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003438 (u32)sinfo->rx_bytes))
3439 goto nla_put_failure;
3440 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3441 NL80211_STA_INFO_TX_BYTES64)) &&
3442 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3443 (u32)sinfo->tx_bytes))
3444 goto nla_put_failure;
3445 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3446 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003447 sinfo->rx_bytes))
3448 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003449 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3450 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003451 sinfo->tx_bytes))
3452 goto nla_put_failure;
3453 if ((sinfo->filled & STATION_INFO_LLID) &&
3454 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3455 goto nla_put_failure;
3456 if ((sinfo->filled & STATION_INFO_PLID) &&
3457 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3458 goto nla_put_failure;
3459 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3460 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3461 sinfo->plink_state))
3462 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003463 switch (rdev->wiphy.signal_type) {
3464 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003465 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3466 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3467 sinfo->signal))
3468 goto nla_put_failure;
3469 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3470 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3471 sinfo->signal_avg))
3472 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003473 break;
3474 default:
3475 break;
3476 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003477 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3478 if (!nl80211_put_signal(msg, sinfo->chains,
3479 sinfo->chain_signal,
3480 NL80211_STA_INFO_CHAIN_SIGNAL))
3481 goto nla_put_failure;
3482 }
3483 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3484 if (!nl80211_put_signal(msg, sinfo->chains,
3485 sinfo->chain_signal_avg,
3486 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3487 goto nla_put_failure;
3488 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003489 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003490 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3491 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003492 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003493 }
3494 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3495 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3496 NL80211_STA_INFO_RX_BITRATE))
3497 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003498 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003499 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3500 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3501 sinfo->rx_packets))
3502 goto nla_put_failure;
3503 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3504 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3505 sinfo->tx_packets))
3506 goto nla_put_failure;
3507 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3508 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3509 sinfo->tx_retries))
3510 goto nla_put_failure;
3511 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3512 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3513 sinfo->tx_failed))
3514 goto nla_put_failure;
3515 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3516 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3517 sinfo->beacon_loss_count))
3518 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003519 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3520 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3521 sinfo->local_pm))
3522 goto nla_put_failure;
3523 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3524 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3525 sinfo->peer_pm))
3526 goto nla_put_failure;
3527 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3528 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3529 sinfo->nonpeer_pm))
3530 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003531 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3532 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3533 if (!bss_param)
3534 goto nla_put_failure;
3535
David S. Miller9360ffd2012-03-29 04:41:26 -04003536 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3537 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3538 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3539 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3540 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3541 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3542 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3543 sinfo->bss_param.dtim_period) ||
3544 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3545 sinfo->bss_param.beacon_interval))
3546 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003547
3548 nla_nest_end(msg, bss_param);
3549 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003550 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3551 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3552 sizeof(struct nl80211_sta_flag_update),
3553 &sinfo->sta_flags))
3554 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003555 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3556 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3557 sinfo->t_offset))
3558 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003559 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003560
David S. Miller9360ffd2012-03-29 04:41:26 -04003561 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3562 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3563 sinfo->assoc_req_ies))
3564 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003565
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003566 return genlmsg_end(msg, hdr);
3567
3568 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003569 genlmsg_cancel(msg, hdr);
3570 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003571}
3572
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003573static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003574 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003575{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003576 struct station_info sinfo;
3577 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003578 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003579 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003580 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003581 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003582
Johannes Berg97990a02013-04-19 01:02:55 +02003583 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003584 if (err)
3585 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003586
Johannes Berg97990a02013-04-19 01:02:55 +02003587 if (!wdev->netdev) {
3588 err = -EINVAL;
3589 goto out_err;
3590 }
3591
Johannes Bergbba95fe2008-07-29 13:22:51 +02003592 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003593 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003594 goto out_err;
3595 }
3596
Johannes Bergbba95fe2008-07-29 13:22:51 +02003597 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003598 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003599 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003600 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003601 if (err == -ENOENT)
3602 break;
3603 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003604 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003605
3606 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003607 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003608 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003609 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003610 &sinfo) < 0)
3611 goto out;
3612
3613 sta_idx++;
3614 }
3615
3616
3617 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003618 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003619 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003620 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003621 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003622
3623 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003624}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003625
Johannes Berg5727ef12007-12-19 02:03:34 +01003626static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3627{
Johannes Berg4c476992010-10-04 21:36:35 +02003628 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3629 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003630 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003631 struct sk_buff *msg;
3632 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003633 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003634
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003635 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003636
3637 if (!info->attrs[NL80211_ATTR_MAC])
3638 return -EINVAL;
3639
3640 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3641
Johannes Berg4c476992010-10-04 21:36:35 +02003642 if (!rdev->ops->get_station)
3643 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003644
Hila Gonene35e4d22012-06-27 17:19:42 +03003645 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003646 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003647 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003648
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003649 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003650 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003651 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003652
Eric W. Biederman15e47302012-09-07 20:12:54 +00003653 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003654 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003655 nlmsg_free(msg);
3656 return -ENOBUFS;
3657 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003658
Johannes Berg4c476992010-10-04 21:36:35 +02003659 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003660}
3661
Johannes Berg77ee7c82013-02-15 00:48:33 +01003662int cfg80211_check_station_change(struct wiphy *wiphy,
3663 struct station_parameters *params,
3664 enum cfg80211_station_type statype)
3665{
3666 if (params->listen_interval != -1)
3667 return -EINVAL;
3668 if (params->aid)
3669 return -EINVAL;
3670
3671 /* When you run into this, adjust the code below for the new flag */
3672 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3673
3674 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003675 case CFG80211_STA_MESH_PEER_KERNEL:
3676 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003677 /*
3678 * No ignoring the TDLS flag here -- the userspace mesh
3679 * code doesn't have the bug of including TDLS in the
3680 * mask everywhere.
3681 */
3682 if (params->sta_flags_mask &
3683 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3684 BIT(NL80211_STA_FLAG_MFP) |
3685 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3686 return -EINVAL;
3687 break;
3688 case CFG80211_STA_TDLS_PEER_SETUP:
3689 case CFG80211_STA_TDLS_PEER_ACTIVE:
3690 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3691 return -EINVAL;
3692 /* ignore since it can't change */
3693 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3694 break;
3695 default:
3696 /* disallow mesh-specific things */
3697 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3698 return -EINVAL;
3699 if (params->local_pm)
3700 return -EINVAL;
3701 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3702 return -EINVAL;
3703 }
3704
3705 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3706 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3707 /* TDLS can't be set, ... */
3708 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3709 return -EINVAL;
3710 /*
3711 * ... but don't bother the driver with it. This works around
3712 * a hostapd/wpa_supplicant issue -- it always includes the
3713 * TLDS_PEER flag in the mask even for AP mode.
3714 */
3715 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3716 }
3717
3718 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3719 /* reject other things that can't change */
3720 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3721 return -EINVAL;
3722 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3723 return -EINVAL;
3724 if (params->supported_rates)
3725 return -EINVAL;
3726 if (params->ext_capab || params->ht_capa || params->vht_capa)
3727 return -EINVAL;
3728 }
3729
3730 if (statype != CFG80211_STA_AP_CLIENT) {
3731 if (params->vlan)
3732 return -EINVAL;
3733 }
3734
3735 switch (statype) {
3736 case CFG80211_STA_AP_MLME_CLIENT:
3737 /* Use this only for authorizing/unauthorizing a station */
3738 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3739 return -EOPNOTSUPP;
3740 break;
3741 case CFG80211_STA_AP_CLIENT:
3742 /* accept only the listed bits */
3743 if (params->sta_flags_mask &
3744 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3745 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3746 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3747 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3748 BIT(NL80211_STA_FLAG_WME) |
3749 BIT(NL80211_STA_FLAG_MFP)))
3750 return -EINVAL;
3751
3752 /* but authenticated/associated only if driver handles it */
3753 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3754 params->sta_flags_mask &
3755 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3756 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3757 return -EINVAL;
3758 break;
3759 case CFG80211_STA_IBSS:
3760 case CFG80211_STA_AP_STA:
3761 /* reject any changes other than AUTHORIZED */
3762 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3763 return -EINVAL;
3764 break;
3765 case CFG80211_STA_TDLS_PEER_SETUP:
3766 /* reject any changes other than AUTHORIZED or WME */
3767 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3768 BIT(NL80211_STA_FLAG_WME)))
3769 return -EINVAL;
3770 /* force (at least) rates when authorizing */
3771 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3772 !params->supported_rates)
3773 return -EINVAL;
3774 break;
3775 case CFG80211_STA_TDLS_PEER_ACTIVE:
3776 /* reject any changes */
3777 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003778 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003779 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3780 return -EINVAL;
3781 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003782 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003783 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3784 return -EINVAL;
3785 break;
3786 }
3787
3788 return 0;
3789}
3790EXPORT_SYMBOL(cfg80211_check_station_change);
3791
Johannes Berg5727ef12007-12-19 02:03:34 +01003792/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003793 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003794 */
Johannes Berg80b99892011-11-18 16:23:01 +01003795static struct net_device *get_vlan(struct genl_info *info,
3796 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003797{
Johannes Berg463d0182009-07-14 00:33:35 +02003798 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003799 struct net_device *v;
3800 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003801
Johannes Berg80b99892011-11-18 16:23:01 +01003802 if (!vlanattr)
3803 return NULL;
3804
3805 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3806 if (!v)
3807 return ERR_PTR(-ENODEV);
3808
3809 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3810 ret = -EINVAL;
3811 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003812 }
Johannes Berg80b99892011-11-18 16:23:01 +01003813
Johannes Berg77ee7c82013-02-15 00:48:33 +01003814 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3815 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3816 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3817 ret = -EINVAL;
3818 goto error;
3819 }
3820
Johannes Berg80b99892011-11-18 16:23:01 +01003821 if (!netif_running(v)) {
3822 ret = -ENETDOWN;
3823 goto error;
3824 }
3825
3826 return v;
3827 error:
3828 dev_put(v);
3829 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003830}
3831
Jouni Malinendf881292013-02-14 21:10:54 +02003832static struct nla_policy
3833nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3834 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3835 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3836};
3837
Johannes Bergff276692013-02-15 00:09:01 +01003838static int nl80211_parse_sta_wme(struct genl_info *info,
3839 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003840{
Jouni Malinendf881292013-02-14 21:10:54 +02003841 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3842 struct nlattr *nla;
3843 int err;
3844
Jouni Malinendf881292013-02-14 21:10:54 +02003845 /* parse WME attributes if present */
3846 if (!info->attrs[NL80211_ATTR_STA_WME])
3847 return 0;
3848
3849 nla = info->attrs[NL80211_ATTR_STA_WME];
3850 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3851 nl80211_sta_wme_policy);
3852 if (err)
3853 return err;
3854
3855 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3856 params->uapsd_queues = nla_get_u8(
3857 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3858 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3859 return -EINVAL;
3860
3861 if (tb[NL80211_STA_WME_MAX_SP])
3862 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3863
3864 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3865 return -EINVAL;
3866
3867 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3868
3869 return 0;
3870}
3871
Johannes Bergff276692013-02-15 00:09:01 +01003872static int nl80211_set_station_tdls(struct genl_info *info,
3873 struct station_parameters *params)
3874{
3875 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03003876 if (info->attrs[NL80211_ATTR_PEER_AID])
3877 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01003878 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3879 params->ht_capa =
3880 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3881 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3882 params->vht_capa =
3883 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3884
3885 return nl80211_parse_sta_wme(info, params);
3886}
3887
Johannes Berg5727ef12007-12-19 02:03:34 +01003888static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3889{
Johannes Berg4c476992010-10-04 21:36:35 +02003890 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003891 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003892 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003893 u8 *mac_addr;
3894 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003895
3896 memset(&params, 0, sizeof(params));
3897
3898 params.listen_interval = -1;
3899
Johannes Berg77ee7c82013-02-15 00:48:33 +01003900 if (!rdev->ops->change_station)
3901 return -EOPNOTSUPP;
3902
Johannes Berg5727ef12007-12-19 02:03:34 +01003903 if (info->attrs[NL80211_ATTR_STA_AID])
3904 return -EINVAL;
3905
3906 if (!info->attrs[NL80211_ATTR_MAC])
3907 return -EINVAL;
3908
3909 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3910
3911 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3912 params.supported_rates =
3913 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3914 params.supported_rates_len =
3915 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3916 }
3917
Jouni Malinen9d62a982013-02-14 21:10:13 +02003918 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3919 params.capability =
3920 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3921 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3922 }
3923
3924 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3925 params.ext_capab =
3926 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3927 params.ext_capab_len =
3928 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3929 }
3930
Jouni Malinendf881292013-02-14 21:10:54 +02003931 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003932 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003933
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003934 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003935 return -EINVAL;
3936
Johannes Bergf8bacc22013-02-14 23:27:01 +01003937 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003938 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003939 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3940 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3941 return -EINVAL;
3942 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003943
Johannes Bergf8bacc22013-02-14 23:27:01 +01003944 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003945 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003946 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3947 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3948 return -EINVAL;
3949 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3950 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003951
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003952 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3953 enum nl80211_mesh_power_mode pm = nla_get_u32(
3954 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3955
3956 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3957 pm > NL80211_MESH_POWER_MAX)
3958 return -EINVAL;
3959
3960 params.local_pm = pm;
3961 }
3962
Johannes Berg77ee7c82013-02-15 00:48:33 +01003963 /* Include parameters for TDLS peer (will check later) */
3964 err = nl80211_set_station_tdls(info, &params);
3965 if (err)
3966 return err;
3967
3968 params.vlan = get_vlan(info, rdev);
3969 if (IS_ERR(params.vlan))
3970 return PTR_ERR(params.vlan);
3971
Johannes Berga97f4422009-06-18 17:23:43 +02003972 switch (dev->ieee80211_ptr->iftype) {
3973 case NL80211_IFTYPE_AP:
3974 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003975 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003976 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003977 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01003978 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02003979 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02003980 break;
3981 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003982 err = -EOPNOTSUPP;
3983 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02003984 }
3985
Johannes Berg77ee7c82013-02-15 00:48:33 +01003986 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03003987 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003988
Johannes Berg77ee7c82013-02-15 00:48:33 +01003989 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01003990 if (params.vlan)
3991 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003992
Johannes Berg5727ef12007-12-19 02:03:34 +01003993 return err;
3994}
3995
3996static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3997{
Johannes Berg4c476992010-10-04 21:36:35 +02003998 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003999 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004000 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004001 struct station_parameters params;
4002 u8 *mac_addr = NULL;
4003
4004 memset(&params, 0, sizeof(params));
4005
Johannes Berg984c3112013-02-14 23:43:25 +01004006 if (!rdev->ops->add_station)
4007 return -EOPNOTSUPP;
4008
Johannes Berg5727ef12007-12-19 02:03:34 +01004009 if (!info->attrs[NL80211_ATTR_MAC])
4010 return -EINVAL;
4011
Johannes Berg5727ef12007-12-19 02:03:34 +01004012 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4013 return -EINVAL;
4014
4015 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4016 return -EINVAL;
4017
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004018 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4019 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004020 return -EINVAL;
4021
Johannes Berg5727ef12007-12-19 02:03:34 +01004022 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4023 params.supported_rates =
4024 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4025 params.supported_rates_len =
4026 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4027 params.listen_interval =
4028 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004029
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004030 if (info->attrs[NL80211_ATTR_STA_AID])
4031 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
4032 else
4033 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004034 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4035 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004036
Jouni Malinen9d62a982013-02-14 21:10:13 +02004037 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4038 params.capability =
4039 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4040 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4041 }
4042
4043 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4044 params.ext_capab =
4045 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4046 params.ext_capab_len =
4047 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4048 }
4049
Jouni Malinen36aedc92008-08-25 11:58:58 +03004050 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4051 params.ht_capa =
4052 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004053
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004054 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4055 params.vht_capa =
4056 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4057
Johannes Bergf8bacc22013-02-14 23:27:01 +01004058 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004059 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004060 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4061 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4062 return -EINVAL;
4063 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004064
Johannes Bergff276692013-02-15 00:09:01 +01004065 err = nl80211_parse_sta_wme(info, &params);
4066 if (err)
4067 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004068
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004069 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004070 return -EINVAL;
4071
Johannes Berg77ee7c82013-02-15 00:48:33 +01004072 /* When you run into this, adjust the code below for the new flag */
4073 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4074
Johannes Bergbdd90d52011-12-14 12:20:27 +01004075 switch (dev->ieee80211_ptr->iftype) {
4076 case NL80211_IFTYPE_AP:
4077 case NL80211_IFTYPE_AP_VLAN:
4078 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004079 /* ignore WME attributes if iface/sta is not capable */
4080 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4081 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4082 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004083
Johannes Bergbdd90d52011-12-14 12:20:27 +01004084 /* TDLS peers cannot be added */
4085 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004086 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004087 /* but don't bother the driver with it */
4088 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004089
Johannes Bergd582cff2012-10-26 17:53:44 +02004090 /* allow authenticated/associated only if driver handles it */
4091 if (!(rdev->wiphy.features &
4092 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4093 params.sta_flags_mask &
4094 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4095 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4096 return -EINVAL;
4097
Johannes Bergbdd90d52011-12-14 12:20:27 +01004098 /* must be last in here for error handling */
4099 params.vlan = get_vlan(info, rdev);
4100 if (IS_ERR(params.vlan))
4101 return PTR_ERR(params.vlan);
4102 break;
4103 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004104 /* ignore uAPSD data */
4105 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4106
Johannes Bergd582cff2012-10-26 17:53:44 +02004107 /* associated is disallowed */
4108 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4109 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004110 /* TDLS peers cannot be added */
4111 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004112 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004113 break;
4114 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004115 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004116 /* ignore uAPSD data */
4117 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4118
Johannes Berg77ee7c82013-02-15 00:48:33 +01004119 /* these are disallowed */
4120 if (params.sta_flags_mask &
4121 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4122 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004123 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004124 /* Only TDLS peers can be added */
4125 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4126 return -EINVAL;
4127 /* Can only add if TDLS ... */
4128 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4129 return -EOPNOTSUPP;
4130 /* ... with external setup is supported */
4131 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4132 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004133 /*
4134 * Older wpa_supplicant versions always mark the TDLS peer
4135 * as authorized, but it shouldn't yet be.
4136 */
4137 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004138 break;
4139 default:
4140 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004141 }
4142
Johannes Bergbdd90d52011-12-14 12:20:27 +01004143 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004144
Hila Gonene35e4d22012-06-27 17:19:42 +03004145 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004146
Johannes Berg5727ef12007-12-19 02:03:34 +01004147 if (params.vlan)
4148 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004149 return err;
4150}
4151
4152static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4153{
Johannes Berg4c476992010-10-04 21:36:35 +02004154 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4155 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004156 u8 *mac_addr = NULL;
4157
4158 if (info->attrs[NL80211_ATTR_MAC])
4159 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4160
Johannes Berge80cf852009-05-11 14:43:13 +02004161 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004162 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004163 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004164 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4165 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004166
Johannes Berg4c476992010-10-04 21:36:35 +02004167 if (!rdev->ops->del_station)
4168 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004169
Hila Gonene35e4d22012-06-27 17:19:42 +03004170 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004171}
4172
Eric W. Biederman15e47302012-09-07 20:12:54 +00004173static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004174 int flags, struct net_device *dev,
4175 u8 *dst, u8 *next_hop,
4176 struct mpath_info *pinfo)
4177{
4178 void *hdr;
4179 struct nlattr *pinfoattr;
4180
Eric W. Biederman15e47302012-09-07 20:12:54 +00004181 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004182 if (!hdr)
4183 return -1;
4184
David S. Miller9360ffd2012-03-29 04:41:26 -04004185 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4186 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4187 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4188 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4189 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004190
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004191 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4192 if (!pinfoattr)
4193 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004194 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4195 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4196 pinfo->frame_qlen))
4197 goto nla_put_failure;
4198 if (((pinfo->filled & MPATH_INFO_SN) &&
4199 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4200 ((pinfo->filled & MPATH_INFO_METRIC) &&
4201 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4202 pinfo->metric)) ||
4203 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4204 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4205 pinfo->exptime)) ||
4206 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4207 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4208 pinfo->flags)) ||
4209 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4210 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4211 pinfo->discovery_timeout)) ||
4212 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4213 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4214 pinfo->discovery_retries)))
4215 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004216
4217 nla_nest_end(msg, pinfoattr);
4218
4219 return genlmsg_end(msg, hdr);
4220
4221 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004222 genlmsg_cancel(msg, hdr);
4223 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004224}
4225
4226static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004227 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004228{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004229 struct mpath_info pinfo;
4230 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004231 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004232 u8 dst[ETH_ALEN];
4233 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004234 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004235 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004236
Johannes Berg97990a02013-04-19 01:02:55 +02004237 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004238 if (err)
4239 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004240
4241 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004242 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004243 goto out_err;
4244 }
4245
Johannes Berg97990a02013-04-19 01:02:55 +02004246 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004247 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004248 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004249 }
4250
Johannes Bergbba95fe2008-07-29 13:22:51 +02004251 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004252 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4253 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004254 if (err == -ENOENT)
4255 break;
4256 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004257 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004258
Eric W. Biederman15e47302012-09-07 20:12:54 +00004259 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004260 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004261 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004262 &pinfo) < 0)
4263 goto out;
4264
4265 path_idx++;
4266 }
4267
4268
4269 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004270 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004271 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004272 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004273 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004274 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004275}
4276
4277static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4278{
Johannes Berg4c476992010-10-04 21:36:35 +02004279 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004280 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004281 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004282 struct mpath_info pinfo;
4283 struct sk_buff *msg;
4284 u8 *dst = NULL;
4285 u8 next_hop[ETH_ALEN];
4286
4287 memset(&pinfo, 0, sizeof(pinfo));
4288
4289 if (!info->attrs[NL80211_ATTR_MAC])
4290 return -EINVAL;
4291
4292 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4293
Johannes Berg4c476992010-10-04 21:36:35 +02004294 if (!rdev->ops->get_mpath)
4295 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004296
Johannes Berg4c476992010-10-04 21:36:35 +02004297 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4298 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004299
Hila Gonene35e4d22012-06-27 17:19:42 +03004300 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004301 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004302 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004303
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004305 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004306 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004307
Eric W. Biederman15e47302012-09-07 20:12:54 +00004308 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004309 dev, dst, next_hop, &pinfo) < 0) {
4310 nlmsg_free(msg);
4311 return -ENOBUFS;
4312 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004313
Johannes Berg4c476992010-10-04 21:36:35 +02004314 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004315}
4316
4317static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4318{
Johannes Berg4c476992010-10-04 21:36:35 +02004319 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4320 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004321 u8 *dst = NULL;
4322 u8 *next_hop = NULL;
4323
4324 if (!info->attrs[NL80211_ATTR_MAC])
4325 return -EINVAL;
4326
4327 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4328 return -EINVAL;
4329
4330 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4331 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4332
Johannes Berg4c476992010-10-04 21:36:35 +02004333 if (!rdev->ops->change_mpath)
4334 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004335
Johannes Berg4c476992010-10-04 21:36:35 +02004336 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4337 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004338
Hila Gonene35e4d22012-06-27 17:19:42 +03004339 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004340}
Johannes Berg4c476992010-10-04 21:36:35 +02004341
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004342static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4343{
Johannes Berg4c476992010-10-04 21:36:35 +02004344 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4345 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004346 u8 *dst = NULL;
4347 u8 *next_hop = NULL;
4348
4349 if (!info->attrs[NL80211_ATTR_MAC])
4350 return -EINVAL;
4351
4352 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4353 return -EINVAL;
4354
4355 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4356 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4357
Johannes Berg4c476992010-10-04 21:36:35 +02004358 if (!rdev->ops->add_mpath)
4359 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004360
Johannes Berg4c476992010-10-04 21:36:35 +02004361 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4362 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004363
Hila Gonene35e4d22012-06-27 17:19:42 +03004364 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004365}
4366
4367static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4368{
Johannes Berg4c476992010-10-04 21:36:35 +02004369 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4370 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004371 u8 *dst = NULL;
4372
4373 if (info->attrs[NL80211_ATTR_MAC])
4374 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4375
Johannes Berg4c476992010-10-04 21:36:35 +02004376 if (!rdev->ops->del_mpath)
4377 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004378
Hila Gonene35e4d22012-06-27 17:19:42 +03004379 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004380}
4381
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004382static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4383{
Johannes Berg4c476992010-10-04 21:36:35 +02004384 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4385 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004386 struct bss_parameters params;
4387
4388 memset(&params, 0, sizeof(params));
4389 /* default to not changing parameters */
4390 params.use_cts_prot = -1;
4391 params.use_short_preamble = -1;
4392 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004393 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004394 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004395 params.p2p_ctwindow = -1;
4396 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004397
4398 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4399 params.use_cts_prot =
4400 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4401 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4402 params.use_short_preamble =
4403 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4404 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4405 params.use_short_slot_time =
4406 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004407 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4408 params.basic_rates =
4409 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4410 params.basic_rates_len =
4411 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4412 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004413 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4414 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004415 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4416 params.ht_opmode =
4417 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004418
Johannes Berg53cabad2012-11-14 15:17:28 +01004419 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4420 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4421 return -EINVAL;
4422 params.p2p_ctwindow =
4423 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4424 if (params.p2p_ctwindow < 0)
4425 return -EINVAL;
4426 if (params.p2p_ctwindow != 0 &&
4427 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4428 return -EINVAL;
4429 }
4430
4431 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4432 u8 tmp;
4433
4434 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4435 return -EINVAL;
4436 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4437 if (tmp > 1)
4438 return -EINVAL;
4439 params.p2p_opp_ps = tmp;
4440 if (params.p2p_opp_ps &&
4441 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4442 return -EINVAL;
4443 }
4444
Johannes Berg4c476992010-10-04 21:36:35 +02004445 if (!rdev->ops->change_bss)
4446 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004447
Johannes Berg074ac8d2010-09-16 14:58:22 +02004448 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004449 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4450 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004451
Hila Gonene35e4d22012-06-27 17:19:42 +03004452 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004453}
4454
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004455static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004456 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4457 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4458 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4459 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4460 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4461 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4462};
4463
4464static int parse_reg_rule(struct nlattr *tb[],
4465 struct ieee80211_reg_rule *reg_rule)
4466{
4467 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4468 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4469
4470 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4471 return -EINVAL;
4472 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4473 return -EINVAL;
4474 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4475 return -EINVAL;
4476 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4477 return -EINVAL;
4478 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4479 return -EINVAL;
4480
4481 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4482
4483 freq_range->start_freq_khz =
4484 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4485 freq_range->end_freq_khz =
4486 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4487 freq_range->max_bandwidth_khz =
4488 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4489
4490 power_rule->max_eirp =
4491 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4492
4493 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4494 power_rule->max_antenna_gain =
4495 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4496
4497 return 0;
4498}
4499
4500static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4501{
4502 int r;
4503 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004504 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004505
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004506 /*
4507 * You should only get this when cfg80211 hasn't yet initialized
4508 * completely when built-in to the kernel right between the time
4509 * window between nl80211_init() and regulatory_init(), if that is
4510 * even possible.
4511 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004512 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004513 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004514
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004515 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4516 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004517
4518 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4519
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004520 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4521 user_reg_hint_type =
4522 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4523 else
4524 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4525
4526 switch (user_reg_hint_type) {
4527 case NL80211_USER_REG_HINT_USER:
4528 case NL80211_USER_REG_HINT_CELL_BASE:
4529 break;
4530 default:
4531 return -EINVAL;
4532 }
4533
4534 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004535
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004536 return r;
4537}
4538
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004539static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004540 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004541{
Johannes Berg4c476992010-10-04 21:36:35 +02004542 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004543 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004544 struct wireless_dev *wdev = dev->ieee80211_ptr;
4545 struct mesh_config cur_params;
4546 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004547 void *hdr;
4548 struct nlattr *pinfoattr;
4549 struct sk_buff *msg;
4550
Johannes Berg29cbe682010-12-03 09:20:44 +01004551 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4552 return -EOPNOTSUPP;
4553
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004554 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004555 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004556
Johannes Berg29cbe682010-12-03 09:20:44 +01004557 wdev_lock(wdev);
4558 /* If not connected, get default parameters */
4559 if (!wdev->mesh_id_len)
4560 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4561 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004562 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004563 wdev_unlock(wdev);
4564
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004565 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004566 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004567
4568 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004569 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004570 if (!msg)
4571 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004572 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004573 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004574 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004575 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004576 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004577 if (!pinfoattr)
4578 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004579 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4580 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4581 cur_params.dot11MeshRetryTimeout) ||
4582 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4583 cur_params.dot11MeshConfirmTimeout) ||
4584 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4585 cur_params.dot11MeshHoldingTimeout) ||
4586 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4587 cur_params.dot11MeshMaxPeerLinks) ||
4588 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4589 cur_params.dot11MeshMaxRetries) ||
4590 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4591 cur_params.dot11MeshTTL) ||
4592 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4593 cur_params.element_ttl) ||
4594 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4595 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004596 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4597 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004598 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4599 cur_params.dot11MeshHWMPmaxPREQretries) ||
4600 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4601 cur_params.path_refresh_time) ||
4602 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4603 cur_params.min_discovery_timeout) ||
4604 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4605 cur_params.dot11MeshHWMPactivePathTimeout) ||
4606 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4607 cur_params.dot11MeshHWMPpreqMinInterval) ||
4608 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4609 cur_params.dot11MeshHWMPperrMinInterval) ||
4610 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4611 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4612 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4613 cur_params.dot11MeshHWMPRootMode) ||
4614 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4615 cur_params.dot11MeshHWMPRannInterval) ||
4616 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4617 cur_params.dot11MeshGateAnnouncementProtocol) ||
4618 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4619 cur_params.dot11MeshForwarding) ||
4620 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004621 cur_params.rssi_threshold) ||
4622 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004623 cur_params.ht_opmode) ||
4624 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4625 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4626 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004627 cur_params.dot11MeshHWMProotInterval) ||
4628 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004629 cur_params.dot11MeshHWMPconfirmationInterval) ||
4630 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4631 cur_params.power_mode) ||
4632 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4633 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004634 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004635 nla_nest_end(msg, pinfoattr);
4636 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004637 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004638
Johannes Berg3b858752009-03-12 09:55:09 +01004639 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004640 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004641 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004642 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004643 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004644}
4645
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004646static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004647 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4648 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4649 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4650 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4651 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4652 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004653 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004654 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004655 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004656 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4657 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4658 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4659 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4660 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004661 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004662 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004663 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004664 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004665 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004666 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004667 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4668 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004669 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4670 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004671 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004672 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4673 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004674};
4675
Javier Cardonac80d5452010-12-16 17:37:49 -08004676static const struct nla_policy
4677 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004678 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004679 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4680 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004681 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004682 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004683 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004684 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004685 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004686 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004687};
4688
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004689static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004690 struct mesh_config *cfg,
4691 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004692{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004693 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004694 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004695
Marco Porschea54fba2013-01-07 16:04:48 +01004696#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4697do { \
4698 if (tb[attr]) { \
4699 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4700 return -EINVAL; \
4701 cfg->param = fn(tb[attr]); \
4702 mask |= (1 << (attr - 1)); \
4703 } \
4704} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004705
4706
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004707 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004708 return -EINVAL;
4709 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004710 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004711 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004712 return -EINVAL;
4713
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004714 /* This makes sure that there aren't more than 32 mesh config
4715 * parameters (otherwise our bitfield scheme would not work.) */
4716 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4717
4718 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004719 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004720 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4721 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004722 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004723 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4724 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004725 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004726 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4727 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004728 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004729 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4730 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004731 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004732 mask, NL80211_MESHCONF_MAX_RETRIES,
4733 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004734 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004735 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004736 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004737 mask, NL80211_MESHCONF_ELEMENT_TTL,
4738 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004739 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004740 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4741 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004742 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4743 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004744 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4745 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004746 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004747 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4748 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004749 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004750 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4751 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004752 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004753 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4754 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004755 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4756 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004757 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4758 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004759 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004760 1, 65535, mask,
4761 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004762 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004763 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004764 1, 65535, mask,
4765 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004766 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004767 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004768 dot11MeshHWMPnetDiameterTraversalTime,
4769 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004770 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4771 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004772 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4773 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4774 nla_get_u8);
4775 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4776 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004777 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004778 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004779 dot11MeshGateAnnouncementProtocol, 0, 1,
4780 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004781 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004782 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004783 mask, NL80211_MESHCONF_FORWARDING,
4784 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004785 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004786 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4787 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004788 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004789 mask, NL80211_MESHCONF_HT_OPMODE,
4790 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004791 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004792 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004793 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4794 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004795 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004796 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4797 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004798 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004799 dot11MeshHWMPconfirmationInterval,
4800 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004801 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4802 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004803 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4804 NL80211_MESH_POWER_ACTIVE,
4805 NL80211_MESH_POWER_MAX,
4806 mask, NL80211_MESHCONF_POWER_MODE,
4807 nla_get_u32);
4808 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4809 0, 65535, mask,
4810 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004811 if (mask_out)
4812 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004813
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004814 return 0;
4815
4816#undef FILL_IN_MESH_PARAM_IF_SET
4817}
4818
Javier Cardonac80d5452010-12-16 17:37:49 -08004819static int nl80211_parse_mesh_setup(struct genl_info *info,
4820 struct mesh_setup *setup)
4821{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004823 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4824
4825 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4826 return -EINVAL;
4827 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4828 info->attrs[NL80211_ATTR_MESH_SETUP],
4829 nl80211_mesh_setup_params_policy))
4830 return -EINVAL;
4831
Javier Cardonad299a1f2012-03-31 11:31:33 -07004832 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4833 setup->sync_method =
4834 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4835 IEEE80211_SYNC_METHOD_VENDOR :
4836 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4837
Javier Cardonac80d5452010-12-16 17:37:49 -08004838 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4839 setup->path_sel_proto =
4840 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4841 IEEE80211_PATH_PROTOCOL_VENDOR :
4842 IEEE80211_PATH_PROTOCOL_HWMP;
4843
4844 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4845 setup->path_metric =
4846 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4847 IEEE80211_PATH_METRIC_VENDOR :
4848 IEEE80211_PATH_METRIC_AIRTIME;
4849
Javier Cardona581a8b02011-04-07 15:08:27 -07004850
4851 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004852 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004853 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004854 if (!is_valid_ie_attr(ieattr))
4855 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004856 setup->ie = nla_data(ieattr);
4857 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004858 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004859 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4860 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4861 return -EINVAL;
4862 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004863 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4864 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004865 if (setup->is_secure)
4866 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08004867
Colleen Twitty6e16d902013-05-08 11:45:59 -07004868 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
4869 if (!setup->user_mpm)
4870 return -EINVAL;
4871 setup->auth_id =
4872 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
4873 }
4874
Javier Cardonac80d5452010-12-16 17:37:49 -08004875 return 0;
4876}
4877
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004878static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004879 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004880{
4881 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4882 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004883 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004884 struct mesh_config cfg;
4885 u32 mask;
4886 int err;
4887
Johannes Berg29cbe682010-12-03 09:20:44 +01004888 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4889 return -EOPNOTSUPP;
4890
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004891 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004892 return -EOPNOTSUPP;
4893
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004894 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004895 if (err)
4896 return err;
4897
Johannes Berg29cbe682010-12-03 09:20:44 +01004898 wdev_lock(wdev);
4899 if (!wdev->mesh_id_len)
4900 err = -ENOLINK;
4901
4902 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004903 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004904
4905 wdev_unlock(wdev);
4906
4907 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004908}
4909
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004910static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4911{
Johannes Berg458f4f92012-12-06 15:47:38 +01004912 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004913 struct sk_buff *msg;
4914 void *hdr = NULL;
4915 struct nlattr *nl_reg_rules;
4916 unsigned int i;
4917 int err = -EINVAL;
4918
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004919 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004920
4921 if (!cfg80211_regdomain)
4922 goto out;
4923
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004924 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004925 if (!msg) {
4926 err = -ENOBUFS;
4927 goto out;
4928 }
4929
Eric W. Biederman15e47302012-09-07 20:12:54 +00004930 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004931 NL80211_CMD_GET_REG);
4932 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004933 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004934
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004935 if (reg_last_request_cell_base() &&
4936 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4937 NL80211_USER_REG_HINT_CELL_BASE))
4938 goto nla_put_failure;
4939
Johannes Berg458f4f92012-12-06 15:47:38 +01004940 rcu_read_lock();
4941 regdom = rcu_dereference(cfg80211_regdomain);
4942
4943 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4944 (regdom->dfs_region &&
4945 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4946 goto nla_put_failure_rcu;
4947
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004948 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4949 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004950 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004951
Johannes Berg458f4f92012-12-06 15:47:38 +01004952 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004953 struct nlattr *nl_reg_rule;
4954 const struct ieee80211_reg_rule *reg_rule;
4955 const struct ieee80211_freq_range *freq_range;
4956 const struct ieee80211_power_rule *power_rule;
4957
Johannes Berg458f4f92012-12-06 15:47:38 +01004958 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004959 freq_range = &reg_rule->freq_range;
4960 power_rule = &reg_rule->power_rule;
4961
4962 nl_reg_rule = nla_nest_start(msg, i);
4963 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004964 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004965
David S. Miller9360ffd2012-03-29 04:41:26 -04004966 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4967 reg_rule->flags) ||
4968 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4969 freq_range->start_freq_khz) ||
4970 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4971 freq_range->end_freq_khz) ||
4972 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4973 freq_range->max_bandwidth_khz) ||
4974 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4975 power_rule->max_antenna_gain) ||
4976 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4977 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004978 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004979
4980 nla_nest_end(msg, nl_reg_rule);
4981 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004982 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004983
4984 nla_nest_end(msg, nl_reg_rules);
4985
4986 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004987 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004988 goto out;
4989
Johannes Berg458f4f92012-12-06 15:47:38 +01004990nla_put_failure_rcu:
4991 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004992nla_put_failure:
4993 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004994put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004995 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004996 err = -EMSGSIZE;
4997out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004998 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004999 return err;
5000}
5001
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005002static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5003{
5004 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5005 struct nlattr *nl_reg_rule;
5006 char *alpha2 = NULL;
5007 int rem_reg_rules = 0, r = 0;
5008 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005009 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005010 struct ieee80211_regdomain *rd = NULL;
5011
5012 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5013 return -EINVAL;
5014
5015 if (!info->attrs[NL80211_ATTR_REG_RULES])
5016 return -EINVAL;
5017
5018 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5019
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005020 if (info->attrs[NL80211_ATTR_DFS_REGION])
5021 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5022
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005023 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005024 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005025 num_rules++;
5026 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005027 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005028 }
5029
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005030 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005031 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005032
5033 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005034 if (!rd)
5035 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005036
5037 rd->n_reg_rules = num_rules;
5038 rd->alpha2[0] = alpha2[0];
5039 rd->alpha2[1] = alpha2[1];
5040
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005041 /*
5042 * Disable DFS master mode if the DFS region was
5043 * not supported or known on this kernel.
5044 */
5045 if (reg_supported_dfs_region(dfs_region))
5046 rd->dfs_region = dfs_region;
5047
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005048 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005049 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005050 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01005051 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5052 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005053 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5054 if (r)
5055 goto bad_reg;
5056
5057 rule_idx++;
5058
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005059 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5060 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005061 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005062 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005063 }
5064
Johannes Berg6913b492012-12-04 00:48:59 +01005065 mutex_lock(&cfg80211_mutex);
5066
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005067 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005068 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005069 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01005070 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005071
Johannes Bergd2372b32008-10-24 20:32:20 +02005072 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005073 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005074 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005075}
5076
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005077static int validate_scan_freqs(struct nlattr *freqs)
5078{
5079 struct nlattr *attr1, *attr2;
5080 int n_channels = 0, tmp1, tmp2;
5081
5082 nla_for_each_nested(attr1, freqs, tmp1) {
5083 n_channels++;
5084 /*
5085 * Some hardware has a limited channel list for
5086 * scanning, and it is pretty much nonsensical
5087 * to scan for a channel twice, so disallow that
5088 * and don't require drivers to check that the
5089 * channel list they get isn't longer than what
5090 * they can scan, as long as they can scan all
5091 * the channels they registered at once.
5092 */
5093 nla_for_each_nested(attr2, freqs, tmp2)
5094 if (attr1 != attr2 &&
5095 nla_get_u32(attr1) == nla_get_u32(attr2))
5096 return 0;
5097 }
5098
5099 return n_channels;
5100}
5101
Johannes Berg2a519312009-02-10 21:25:55 +01005102static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5103{
Johannes Berg4c476992010-10-04 21:36:35 +02005104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005105 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005106 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005107 struct nlattr *attr;
5108 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005109 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005110 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005111
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005112 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5113 return -EINVAL;
5114
Johannes Berg79c97e92009-07-07 03:56:12 +02005115 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005116
Johannes Berg4c476992010-10-04 21:36:35 +02005117 if (!rdev->ops->scan)
5118 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005119
Johannes Bergf9f47522013-03-19 15:04:07 +01005120 mutex_lock(&rdev->sched_scan_mtx);
5121 if (rdev->scan_req) {
5122 err = -EBUSY;
5123 goto unlock;
5124 }
Johannes Berg2a519312009-02-10 21:25:55 +01005125
5126 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005127 n_channels = validate_scan_freqs(
5128 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005129 if (!n_channels) {
5130 err = -EINVAL;
5131 goto unlock;
5132 }
Johannes Berg2a519312009-02-10 21:25:55 +01005133 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005134 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005135 n_channels = 0;
5136
Johannes Berg2a519312009-02-10 21:25:55 +01005137 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5138 if (wiphy->bands[band])
5139 n_channels += wiphy->bands[band]->n_channels;
5140 }
5141
5142 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5143 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5144 n_ssids++;
5145
Johannes Bergf9f47522013-03-19 15:04:07 +01005146 if (n_ssids > wiphy->max_scan_ssids) {
5147 err = -EINVAL;
5148 goto unlock;
5149 }
Johannes Berg2a519312009-02-10 21:25:55 +01005150
Jouni Malinen70692ad2009-02-16 19:39:13 +02005151 if (info->attrs[NL80211_ATTR_IE])
5152 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5153 else
5154 ie_len = 0;
5155
Johannes Bergf9f47522013-03-19 15:04:07 +01005156 if (ie_len > wiphy->max_scan_ie_len) {
5157 err = -EINVAL;
5158 goto unlock;
5159 }
Johannes Berg18a83652009-03-31 12:12:05 +02005160
Johannes Berg2a519312009-02-10 21:25:55 +01005161 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005162 + sizeof(*request->ssids) * n_ssids
5163 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005164 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005165 if (!request) {
5166 err = -ENOMEM;
5167 goto unlock;
5168 }
Johannes Berg2a519312009-02-10 21:25:55 +01005169
Johannes Berg2a519312009-02-10 21:25:55 +01005170 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005171 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005172 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005173 if (ie_len) {
5174 if (request->ssids)
5175 request->ie = (void *)(request->ssids + n_ssids);
5176 else
5177 request->ie = (void *)(request->channels + n_channels);
5178 }
Johannes Berg2a519312009-02-10 21:25:55 +01005179
Johannes Berg584991d2009-11-02 13:32:03 +01005180 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005181 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5182 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005183 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005184 struct ieee80211_channel *chan;
5185
5186 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5187
5188 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005189 err = -EINVAL;
5190 goto out_free;
5191 }
Johannes Berg584991d2009-11-02 13:32:03 +01005192
5193 /* ignore disabled channels */
5194 if (chan->flags & IEEE80211_CHAN_DISABLED)
5195 continue;
5196
5197 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005198 i++;
5199 }
5200 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005201 enum ieee80211_band band;
5202
Johannes Berg2a519312009-02-10 21:25:55 +01005203 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005204 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5205 int j;
5206 if (!wiphy->bands[band])
5207 continue;
5208 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005209 struct ieee80211_channel *chan;
5210
5211 chan = &wiphy->bands[band]->channels[j];
5212
5213 if (chan->flags & IEEE80211_CHAN_DISABLED)
5214 continue;
5215
5216 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005217 i++;
5218 }
5219 }
5220 }
5221
Johannes Berg584991d2009-11-02 13:32:03 +01005222 if (!i) {
5223 err = -EINVAL;
5224 goto out_free;
5225 }
5226
5227 request->n_channels = i;
5228
Johannes Berg2a519312009-02-10 21:25:55 +01005229 i = 0;
5230 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5231 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005232 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005233 err = -EINVAL;
5234 goto out_free;
5235 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005236 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005237 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005238 i++;
5239 }
5240 }
5241
Jouni Malinen70692ad2009-02-16 19:39:13 +02005242 if (info->attrs[NL80211_ATTR_IE]) {
5243 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005244 memcpy((void *)request->ie,
5245 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005246 request->ie_len);
5247 }
5248
Johannes Berg34850ab2011-07-18 18:08:35 +02005249 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005250 if (wiphy->bands[i])
5251 request->rates[i] =
5252 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005253
5254 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5255 nla_for_each_nested(attr,
5256 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5257 tmp) {
5258 enum ieee80211_band band = nla_type(attr);
5259
Dan Carpenter84404622011-07-29 11:52:18 +03005260 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005261 err = -EINVAL;
5262 goto out_free;
5263 }
5264 err = ieee80211_get_ratemask(wiphy->bands[band],
5265 nla_data(attr),
5266 nla_len(attr),
5267 &request->rates[band]);
5268 if (err)
5269 goto out_free;
5270 }
5271 }
5272
Sam Leffler46856bb2012-10-11 21:03:32 -07005273 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005274 request->flags = nla_get_u32(
5275 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005276 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5277 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5278 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5279 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005280 err = -EOPNOTSUPP;
5281 goto out_free;
5282 }
5283 }
Sam Lefflered4737712012-10-11 21:03:31 -07005284
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305285 request->no_cck =
5286 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5287
Johannes Bergfd014282012-06-18 19:17:03 +02005288 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005289 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005290 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005291
Johannes Berg79c97e92009-07-07 03:56:12 +02005292 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005293 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005294
Johannes Berg463d0182009-07-14 00:33:35 +02005295 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005296 nl80211_send_scan_start(rdev, wdev);
5297 if (wdev->netdev)
5298 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005299 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005300 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005301 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005302 kfree(request);
5303 }
Johannes Berg3b858752009-03-12 09:55:09 +01005304
Johannes Bergf9f47522013-03-19 15:04:07 +01005305 unlock:
5306 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01005307 return err;
5308}
5309
Luciano Coelho807f8a82011-05-11 17:09:35 +03005310static int nl80211_start_sched_scan(struct sk_buff *skb,
5311 struct genl_info *info)
5312{
5313 struct cfg80211_sched_scan_request *request;
5314 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5315 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005316 struct nlattr *attr;
5317 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005318 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005319 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005320 enum ieee80211_band band;
5321 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005322 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005323
5324 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5325 !rdev->ops->sched_scan_start)
5326 return -EOPNOTSUPP;
5327
5328 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5329 return -EINVAL;
5330
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005331 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5332 return -EINVAL;
5333
5334 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5335 if (interval == 0)
5336 return -EINVAL;
5337
Luciano Coelho807f8a82011-05-11 17:09:35 +03005338 wiphy = &rdev->wiphy;
5339
5340 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5341 n_channels = validate_scan_freqs(
5342 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5343 if (!n_channels)
5344 return -EINVAL;
5345 } else {
5346 n_channels = 0;
5347
5348 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5349 if (wiphy->bands[band])
5350 n_channels += wiphy->bands[band]->n_channels;
5351 }
5352
5353 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5354 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5355 tmp)
5356 n_ssids++;
5357
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005358 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005359 return -EINVAL;
5360
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005361 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5362 nla_for_each_nested(attr,
5363 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5364 tmp)
5365 n_match_sets++;
5366
5367 if (n_match_sets > wiphy->max_match_sets)
5368 return -EINVAL;
5369
Luciano Coelho807f8a82011-05-11 17:09:35 +03005370 if (info->attrs[NL80211_ATTR_IE])
5371 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5372 else
5373 ie_len = 0;
5374
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005375 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005376 return -EINVAL;
5377
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005378 mutex_lock(&rdev->sched_scan_mtx);
5379
5380 if (rdev->sched_scan_req) {
5381 err = -EINPROGRESS;
5382 goto out;
5383 }
5384
Luciano Coelho807f8a82011-05-11 17:09:35 +03005385 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005386 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005387 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005388 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005389 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005390 if (!request) {
5391 err = -ENOMEM;
5392 goto out;
5393 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005394
5395 if (n_ssids)
5396 request->ssids = (void *)&request->channels[n_channels];
5397 request->n_ssids = n_ssids;
5398 if (ie_len) {
5399 if (request->ssids)
5400 request->ie = (void *)(request->ssids + n_ssids);
5401 else
5402 request->ie = (void *)(request->channels + n_channels);
5403 }
5404
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005405 if (n_match_sets) {
5406 if (request->ie)
5407 request->match_sets = (void *)(request->ie + ie_len);
5408 else if (request->ssids)
5409 request->match_sets =
5410 (void *)(request->ssids + n_ssids);
5411 else
5412 request->match_sets =
5413 (void *)(request->channels + n_channels);
5414 }
5415 request->n_match_sets = n_match_sets;
5416
Luciano Coelho807f8a82011-05-11 17:09:35 +03005417 i = 0;
5418 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5419 /* user specified, bail out if channel not found */
5420 nla_for_each_nested(attr,
5421 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5422 tmp) {
5423 struct ieee80211_channel *chan;
5424
5425 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5426
5427 if (!chan) {
5428 err = -EINVAL;
5429 goto out_free;
5430 }
5431
5432 /* ignore disabled channels */
5433 if (chan->flags & IEEE80211_CHAN_DISABLED)
5434 continue;
5435
5436 request->channels[i] = chan;
5437 i++;
5438 }
5439 } else {
5440 /* all channels */
5441 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5442 int j;
5443 if (!wiphy->bands[band])
5444 continue;
5445 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5446 struct ieee80211_channel *chan;
5447
5448 chan = &wiphy->bands[band]->channels[j];
5449
5450 if (chan->flags & IEEE80211_CHAN_DISABLED)
5451 continue;
5452
5453 request->channels[i] = chan;
5454 i++;
5455 }
5456 }
5457 }
5458
5459 if (!i) {
5460 err = -EINVAL;
5461 goto out_free;
5462 }
5463
5464 request->n_channels = i;
5465
5466 i = 0;
5467 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5468 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5469 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005470 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005471 err = -EINVAL;
5472 goto out_free;
5473 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005474 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005475 memcpy(request->ssids[i].ssid, nla_data(attr),
5476 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005477 i++;
5478 }
5479 }
5480
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005481 i = 0;
5482 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5483 nla_for_each_nested(attr,
5484 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5485 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005486 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005487
5488 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5489 nla_data(attr), nla_len(attr),
5490 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005491 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005492 if (ssid) {
5493 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5494 err = -EINVAL;
5495 goto out_free;
5496 }
5497 memcpy(request->match_sets[i].ssid.ssid,
5498 nla_data(ssid), nla_len(ssid));
5499 request->match_sets[i].ssid.ssid_len =
5500 nla_len(ssid);
5501 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005502 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5503 if (rssi)
5504 request->rssi_thold = nla_get_u32(rssi);
5505 else
5506 request->rssi_thold =
5507 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005508 i++;
5509 }
5510 }
5511
Luciano Coelho807f8a82011-05-11 17:09:35 +03005512 if (info->attrs[NL80211_ATTR_IE]) {
5513 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5514 memcpy((void *)request->ie,
5515 nla_data(info->attrs[NL80211_ATTR_IE]),
5516 request->ie_len);
5517 }
5518
Sam Leffler46856bb2012-10-11 21:03:32 -07005519 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005520 request->flags = nla_get_u32(
5521 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005522 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5523 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5524 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5525 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005526 err = -EOPNOTSUPP;
5527 goto out_free;
5528 }
5529 }
Sam Lefflered4737712012-10-11 21:03:31 -07005530
Luciano Coelho807f8a82011-05-11 17:09:35 +03005531 request->dev = dev;
5532 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005533 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005534 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005535
Hila Gonene35e4d22012-06-27 17:19:42 +03005536 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005537 if (!err) {
5538 rdev->sched_scan_req = request;
5539 nl80211_send_sched_scan(rdev, dev,
5540 NL80211_CMD_START_SCHED_SCAN);
5541 goto out;
5542 }
5543
5544out_free:
5545 kfree(request);
5546out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005547 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005548 return err;
5549}
5550
5551static int nl80211_stop_sched_scan(struct sk_buff *skb,
5552 struct genl_info *info)
5553{
5554 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005555 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005556
5557 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5558 !rdev->ops->sched_scan_stop)
5559 return -EOPNOTSUPP;
5560
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005561 mutex_lock(&rdev->sched_scan_mtx);
5562 err = __cfg80211_stop_sched_scan(rdev, false);
5563 mutex_unlock(&rdev->sched_scan_mtx);
5564
5565 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005566}
5567
Simon Wunderlich04f39042013-02-08 18:16:19 +01005568static int nl80211_start_radar_detection(struct sk_buff *skb,
5569 struct genl_info *info)
5570{
5571 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5572 struct net_device *dev = info->user_ptr[1];
5573 struct wireless_dev *wdev = dev->ieee80211_ptr;
5574 struct cfg80211_chan_def chandef;
5575 int err;
5576
5577 err = nl80211_parse_chandef(rdev, info, &chandef);
5578 if (err)
5579 return err;
5580
5581 if (wdev->cac_started)
5582 return -EBUSY;
5583
5584 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5585 if (err < 0)
5586 return err;
5587
5588 if (err == 0)
5589 return -EINVAL;
5590
5591 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5592 return -EINVAL;
5593
5594 if (!rdev->ops->start_radar_detection)
5595 return -EOPNOTSUPP;
5596
5597 mutex_lock(&rdev->devlist_mtx);
5598 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5599 chandef.chan, CHAN_MODE_SHARED,
5600 BIT(chandef.width));
5601 if (err)
5602 goto err_locked;
5603
5604 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5605 if (!err) {
5606 wdev->channel = chandef.chan;
5607 wdev->cac_started = true;
5608 wdev->cac_start_time = jiffies;
5609 }
5610err_locked:
5611 mutex_unlock(&rdev->devlist_mtx);
5612
5613 return err;
5614}
5615
Johannes Berg9720bb32011-06-21 09:45:33 +02005616static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5617 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005618 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005619 struct wireless_dev *wdev,
5620 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005621{
Johannes Berg48ab9052009-07-10 18:42:31 +02005622 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005623 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005624 void *hdr;
5625 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005626 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005627
5628 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005629
Eric W. Biederman15e47302012-09-07 20:12:54 +00005630 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005631 NL80211_CMD_NEW_SCAN_RESULTS);
5632 if (!hdr)
5633 return -1;
5634
Johannes Berg9720bb32011-06-21 09:45:33 +02005635 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5636
Johannes Berg97990a02013-04-19 01:02:55 +02005637 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5638 goto nla_put_failure;
5639 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005640 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5641 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005642 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5643 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005644
5645 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5646 if (!bss)
5647 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005648 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005649 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005650 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005651
5652 rcu_read_lock();
5653 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005654 if (ies) {
5655 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5656 goto fail_unlock_rcu;
5657 tsf = true;
5658 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5659 ies->len, ies->data))
5660 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005661 }
5662 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005663 if (ies) {
5664 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5665 goto fail_unlock_rcu;
5666 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5667 ies->len, ies->data))
5668 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005669 }
5670 rcu_read_unlock();
5671
David S. Miller9360ffd2012-03-29 04:41:26 -04005672 if (res->beacon_interval &&
5673 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5674 goto nla_put_failure;
5675 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5676 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5677 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5678 jiffies_to_msecs(jiffies - intbss->ts)))
5679 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005680
Johannes Berg77965c92009-02-18 18:45:06 +01005681 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005682 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005683 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5684 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005685 break;
5686 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005687 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5688 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005689 break;
5690 default:
5691 break;
5692 }
5693
Johannes Berg48ab9052009-07-10 18:42:31 +02005694 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005695 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005696 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005697 if (intbss == wdev->current_bss &&
5698 nla_put_u32(msg, NL80211_BSS_STATUS,
5699 NL80211_BSS_STATUS_ASSOCIATED))
5700 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005701 break;
5702 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005703 if (intbss == wdev->current_bss &&
5704 nla_put_u32(msg, NL80211_BSS_STATUS,
5705 NL80211_BSS_STATUS_IBSS_JOINED))
5706 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005707 break;
5708 default:
5709 break;
5710 }
5711
Johannes Berg2a519312009-02-10 21:25:55 +01005712 nla_nest_end(msg, bss);
5713
5714 return genlmsg_end(msg, hdr);
5715
Johannes Berg8cef2c92013-02-05 16:54:31 +01005716 fail_unlock_rcu:
5717 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005718 nla_put_failure:
5719 genlmsg_cancel(msg, hdr);
5720 return -EMSGSIZE;
5721}
5722
Johannes Berg97990a02013-04-19 01:02:55 +02005723static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005724{
Johannes Berg48ab9052009-07-10 18:42:31 +02005725 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005726 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005727 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005728 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005729 int err;
5730
Johannes Berg97990a02013-04-19 01:02:55 +02005731 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005732 if (err)
5733 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005734
Johannes Berg48ab9052009-07-10 18:42:31 +02005735 wdev_lock(wdev);
5736 spin_lock_bh(&rdev->bss_lock);
5737 cfg80211_bss_expire(rdev);
5738
Johannes Berg9720bb32011-06-21 09:45:33 +02005739 cb->seq = rdev->bss_generation;
5740
Johannes Berg48ab9052009-07-10 18:42:31 +02005741 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005742 if (++idx <= start)
5743 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005744 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005745 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005746 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005747 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005748 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005749 }
5750 }
5751
Johannes Berg48ab9052009-07-10 18:42:31 +02005752 spin_unlock_bh(&rdev->bss_lock);
5753 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005754
Johannes Berg97990a02013-04-19 01:02:55 +02005755 cb->args[2] = idx;
5756 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005757
Johannes Berg67748892010-10-04 21:14:06 +02005758 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005759}
5760
Eric W. Biederman15e47302012-09-07 20:12:54 +00005761static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005762 int flags, struct net_device *dev,
5763 struct survey_info *survey)
5764{
5765 void *hdr;
5766 struct nlattr *infoattr;
5767
Eric W. Biederman15e47302012-09-07 20:12:54 +00005768 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005769 NL80211_CMD_NEW_SURVEY_RESULTS);
5770 if (!hdr)
5771 return -ENOMEM;
5772
David S. Miller9360ffd2012-03-29 04:41:26 -04005773 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5774 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005775
5776 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5777 if (!infoattr)
5778 goto nla_put_failure;
5779
David S. Miller9360ffd2012-03-29 04:41:26 -04005780 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5781 survey->channel->center_freq))
5782 goto nla_put_failure;
5783
5784 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5785 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5786 goto nla_put_failure;
5787 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5788 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5789 goto nla_put_failure;
5790 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5791 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5792 survey->channel_time))
5793 goto nla_put_failure;
5794 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5795 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5796 survey->channel_time_busy))
5797 goto nla_put_failure;
5798 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5799 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5800 survey->channel_time_ext_busy))
5801 goto nla_put_failure;
5802 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5803 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5804 survey->channel_time_rx))
5805 goto nla_put_failure;
5806 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5807 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5808 survey->channel_time_tx))
5809 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005810
5811 nla_nest_end(msg, infoattr);
5812
5813 return genlmsg_end(msg, hdr);
5814
5815 nla_put_failure:
5816 genlmsg_cancel(msg, hdr);
5817 return -EMSGSIZE;
5818}
5819
5820static int nl80211_dump_survey(struct sk_buff *skb,
5821 struct netlink_callback *cb)
5822{
5823 struct survey_info survey;
5824 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02005825 struct wireless_dev *wdev;
5826 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01005827 int res;
5828
Johannes Berg97990a02013-04-19 01:02:55 +02005829 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005830 if (res)
5831 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005832
Johannes Berg97990a02013-04-19 01:02:55 +02005833 if (!wdev->netdev) {
5834 res = -EINVAL;
5835 goto out_err;
5836 }
5837
Holger Schurig61fa7132009-11-11 12:25:40 +01005838 if (!dev->ops->dump_survey) {
5839 res = -EOPNOTSUPP;
5840 goto out_err;
5841 }
5842
5843 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005844 struct ieee80211_channel *chan;
5845
Johannes Berg97990a02013-04-19 01:02:55 +02005846 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005847 if (res == -ENOENT)
5848 break;
5849 if (res)
5850 goto out_err;
5851
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005852 /* Survey without a channel doesn't make sense */
5853 if (!survey.channel) {
5854 res = -EINVAL;
5855 goto out;
5856 }
5857
5858 chan = ieee80211_get_channel(&dev->wiphy,
5859 survey.channel->center_freq);
5860 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5861 survey_idx++;
5862 continue;
5863 }
5864
Holger Schurig61fa7132009-11-11 12:25:40 +01005865 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005866 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005867 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02005868 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01005869 goto out;
5870 survey_idx++;
5871 }
5872
5873 out:
Johannes Berg97990a02013-04-19 01:02:55 +02005874 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01005875 res = skb->len;
5876 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02005877 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005878 return res;
5879}
5880
Samuel Ortizb23aa672009-07-01 21:26:54 +02005881static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5882{
5883 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5884 NL80211_WPA_VERSION_2));
5885}
5886
Jouni Malinen636a5d32009-03-19 13:39:22 +02005887static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5888{
Johannes Berg4c476992010-10-04 21:36:35 +02005889 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5890 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005891 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005892 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5893 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005894 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005895 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005896 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005897
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005898 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5899 return -EINVAL;
5900
5901 if (!info->attrs[NL80211_ATTR_MAC])
5902 return -EINVAL;
5903
Jouni Malinen17780922009-03-27 20:52:47 +02005904 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5905 return -EINVAL;
5906
Johannes Berg19957bb2009-07-02 17:20:43 +02005907 if (!info->attrs[NL80211_ATTR_SSID])
5908 return -EINVAL;
5909
5910 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5911 return -EINVAL;
5912
Johannes Bergfffd0932009-07-08 14:22:54 +02005913 err = nl80211_parse_key(info, &key);
5914 if (err)
5915 return err;
5916
5917 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005918 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5919 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005920 if (!key.p.key || !key.p.key_len)
5921 return -EINVAL;
5922 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5923 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5924 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5925 key.p.key_len != WLAN_KEY_LEN_WEP104))
5926 return -EINVAL;
5927 if (key.idx > 4)
5928 return -EINVAL;
5929 } else {
5930 key.p.key_len = 0;
5931 key.p.key = NULL;
5932 }
5933
Johannes Bergafea0b72010-08-10 09:46:42 +02005934 if (key.idx >= 0) {
5935 int i;
5936 bool ok = false;
5937 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5938 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5939 ok = true;
5940 break;
5941 }
5942 }
Johannes Berg4c476992010-10-04 21:36:35 +02005943 if (!ok)
5944 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005945 }
5946
Johannes Berg4c476992010-10-04 21:36:35 +02005947 if (!rdev->ops->auth)
5948 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005949
Johannes Berg074ac8d2010-09-16 14:58:22 +02005950 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005951 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5952 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005953
Johannes Berg19957bb2009-07-02 17:20:43 +02005954 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005955 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005956 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005957 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5958 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005959
Johannes Berg19957bb2009-07-02 17:20:43 +02005960 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5961 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5962
5963 if (info->attrs[NL80211_ATTR_IE]) {
5964 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5965 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5966 }
5967
5968 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005969 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005970 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005971
Jouni Malinene39e5b52012-09-30 19:29:39 +03005972 if (auth_type == NL80211_AUTHTYPE_SAE &&
5973 !info->attrs[NL80211_ATTR_SAE_DATA])
5974 return -EINVAL;
5975
5976 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5977 if (auth_type != NL80211_AUTHTYPE_SAE)
5978 return -EINVAL;
5979 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5980 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5981 /* need to include at least Auth Transaction and Status Code */
5982 if (sae_data_len < 4)
5983 return -EINVAL;
5984 }
5985
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005986 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5987
Johannes Berg95de8172012-01-20 13:55:25 +01005988 /*
5989 * Since we no longer track auth state, ignore
5990 * requests to only change local state.
5991 */
5992 if (local_state_change)
5993 return 0;
5994
Johannes Berg4c476992010-10-04 21:36:35 +02005995 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5996 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005997 key.p.key, key.p.key_len, key.idx,
5998 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005999}
6000
Johannes Bergc0692b82010-08-27 14:26:53 +03006001static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6002 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006003 struct cfg80211_crypto_settings *settings,
6004 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006005{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006006 memset(settings, 0, sizeof(*settings));
6007
Samuel Ortizb23aa672009-07-01 21:26:54 +02006008 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6009
Johannes Bergc0692b82010-08-27 14:26:53 +03006010 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6011 u16 proto;
6012 proto = nla_get_u16(
6013 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6014 settings->control_port_ethertype = cpu_to_be16(proto);
6015 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6016 proto != ETH_P_PAE)
6017 return -EINVAL;
6018 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6019 settings->control_port_no_encrypt = true;
6020 } else
6021 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6022
Samuel Ortizb23aa672009-07-01 21:26:54 +02006023 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6024 void *data;
6025 int len, i;
6026
6027 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6028 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6029 settings->n_ciphers_pairwise = len / sizeof(u32);
6030
6031 if (len % sizeof(u32))
6032 return -EINVAL;
6033
Johannes Berg3dc27d22009-07-02 21:36:37 +02006034 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006035 return -EINVAL;
6036
6037 memcpy(settings->ciphers_pairwise, data, len);
6038
6039 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006040 if (!cfg80211_supported_cipher_suite(
6041 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006042 settings->ciphers_pairwise[i]))
6043 return -EINVAL;
6044 }
6045
6046 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6047 settings->cipher_group =
6048 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006049 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6050 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006051 return -EINVAL;
6052 }
6053
6054 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6055 settings->wpa_versions =
6056 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6057 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6058 return -EINVAL;
6059 }
6060
6061 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6062 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006063 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006064
6065 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6066 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6067 settings->n_akm_suites = len / sizeof(u32);
6068
6069 if (len % sizeof(u32))
6070 return -EINVAL;
6071
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006072 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6073 return -EINVAL;
6074
Samuel Ortizb23aa672009-07-01 21:26:54 +02006075 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006076 }
6077
6078 return 0;
6079}
6080
Jouni Malinen636a5d32009-03-19 13:39:22 +02006081static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6082{
Johannes Berg4c476992010-10-04 21:36:35 +02006083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6084 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006085 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006086 struct cfg80211_assoc_request req = {};
6087 const u8 *bssid, *ssid;
6088 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006089
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006090 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6091 return -EINVAL;
6092
6093 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006094 !info->attrs[NL80211_ATTR_SSID] ||
6095 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006096 return -EINVAL;
6097
Johannes Berg4c476992010-10-04 21:36:35 +02006098 if (!rdev->ops->assoc)
6099 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006100
Johannes Berg074ac8d2010-09-16 14:58:22 +02006101 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006102 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6103 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006104
Johannes Berg19957bb2009-07-02 17:20:43 +02006105 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006106
Johannes Berg19957bb2009-07-02 17:20:43 +02006107 chan = ieee80211_get_channel(&rdev->wiphy,
6108 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006109 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6110 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006111
Johannes Berg19957bb2009-07-02 17:20:43 +02006112 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6113 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006114
6115 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006116 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6117 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006118 }
6119
Jouni Malinendc6382c2009-05-06 22:09:37 +03006120 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006121 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006122 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006123 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006124 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006125 else if (mfp != NL80211_MFP_NO)
6126 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006127 }
6128
Johannes Berg3e5d7642009-07-07 14:37:26 +02006129 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006130 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006131
Ben Greear7e7c8922011-11-18 11:31:59 -08006132 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006133 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006134
6135 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006136 memcpy(&req.ht_capa_mask,
6137 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6138 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006139
6140 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006141 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006142 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006143 memcpy(&req.ht_capa,
6144 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6145 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006146 }
6147
Johannes Bergee2aca32013-02-21 17:36:01 +01006148 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006149 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006150
6151 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006152 memcpy(&req.vht_capa_mask,
6153 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6154 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006155
6156 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006157 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006158 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006159 memcpy(&req.vht_capa,
6160 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6161 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006162 }
6163
Johannes Bergf62fab72013-02-21 20:09:09 +01006164 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006165 if (!err)
Johannes Bergf62fab72013-02-21 20:09:09 +01006166 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6167 ssid, ssid_len, &req);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006168
Jouni Malinen636a5d32009-03-19 13:39:22 +02006169 return err;
6170}
6171
6172static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6173{
Johannes Berg4c476992010-10-04 21:36:35 +02006174 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6175 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006176 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006177 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006178 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006179 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006180
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006181 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6182 return -EINVAL;
6183
6184 if (!info->attrs[NL80211_ATTR_MAC])
6185 return -EINVAL;
6186
6187 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6188 return -EINVAL;
6189
Johannes Berg4c476992010-10-04 21:36:35 +02006190 if (!rdev->ops->deauth)
6191 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +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;
Jouni Malineneec60b02009-03-20 21:21:19 +02006196
Johannes Berg19957bb2009-07-02 17:20:43 +02006197 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006198
Johannes Berg19957bb2009-07-02 17:20:43 +02006199 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6200 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006201 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006202 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006203 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006204
6205 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006206 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6207 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006208 }
6209
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006210 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6211
Johannes Berg4c476992010-10-04 21:36:35 +02006212 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6213 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006214}
6215
6216static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6217{
Johannes Berg4c476992010-10-04 21:36:35 +02006218 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6219 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006220 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006221 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006222 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006223 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006224
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006225 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6226 return -EINVAL;
6227
6228 if (!info->attrs[NL80211_ATTR_MAC])
6229 return -EINVAL;
6230
6231 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6232 return -EINVAL;
6233
Johannes Berg4c476992010-10-04 21:36:35 +02006234 if (!rdev->ops->disassoc)
6235 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006236
Johannes Berg074ac8d2010-09-16 14:58:22 +02006237 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006238 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6239 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006240
Johannes Berg19957bb2009-07-02 17:20:43 +02006241 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006242
Johannes Berg19957bb2009-07-02 17:20:43 +02006243 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6244 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006245 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006246 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006247 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006248
6249 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006250 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6251 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006252 }
6253
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006254 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6255
Johannes Berg4c476992010-10-04 21:36:35 +02006256 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6257 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006258}
6259
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006260static bool
6261nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6262 int mcast_rate[IEEE80211_NUM_BANDS],
6263 int rateval)
6264{
6265 struct wiphy *wiphy = &rdev->wiphy;
6266 bool found = false;
6267 int band, i;
6268
6269 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6270 struct ieee80211_supported_band *sband;
6271
6272 sband = wiphy->bands[band];
6273 if (!sband)
6274 continue;
6275
6276 for (i = 0; i < sband->n_bitrates; i++) {
6277 if (sband->bitrates[i].bitrate == rateval) {
6278 mcast_rate[band] = i + 1;
6279 found = true;
6280 break;
6281 }
6282 }
6283 }
6284
6285 return found;
6286}
6287
Johannes Berg04a773a2009-04-19 21:24:32 +02006288static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6289{
Johannes Berg4c476992010-10-04 21:36:35 +02006290 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6291 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006292 struct cfg80211_ibss_params ibss;
6293 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006294 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006295 int err;
6296
Johannes Berg8e30bc52009-04-22 17:45:38 +02006297 memset(&ibss, 0, sizeof(ibss));
6298
Johannes Berg04a773a2009-04-19 21:24:32 +02006299 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6300 return -EINVAL;
6301
Johannes Berg683b6d32012-11-08 21:25:48 +01006302 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006303 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6304 return -EINVAL;
6305
Johannes Berg8e30bc52009-04-22 17:45:38 +02006306 ibss.beacon_interval = 100;
6307
6308 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6309 ibss.beacon_interval =
6310 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6311 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6312 return -EINVAL;
6313 }
6314
Johannes Berg4c476992010-10-04 21:36:35 +02006315 if (!rdev->ops->join_ibss)
6316 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006317
Johannes Berg4c476992010-10-04 21:36:35 +02006318 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6319 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006320
Johannes Berg79c97e92009-07-07 03:56:12 +02006321 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006322
Johannes Berg39193492011-09-16 13:45:25 +02006323 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006324 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006325
6326 if (!is_valid_ether_addr(ibss.bssid))
6327 return -EINVAL;
6328 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006329 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6330 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6331
6332 if (info->attrs[NL80211_ATTR_IE]) {
6333 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6334 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6335 }
6336
Johannes Berg683b6d32012-11-08 21:25:48 +01006337 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6338 if (err)
6339 return err;
Alexander Simon54858ee2011-11-30 16:56:32 +01006340
Johannes Berg683b6d32012-11-08 21:25:48 +01006341 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee2011-11-30 16:56:32 +01006342 return -EINVAL;
6343
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006344 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6345 return -EINVAL;
6346 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6347 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01006348 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006349
Johannes Berg04a773a2009-04-19 21:24:32 +02006350 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006351 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006352
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006353 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6354 u8 *rates =
6355 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6356 int n_rates =
6357 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6358 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006359 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006360
Johannes Berg34850ab2011-07-18 18:08:35 +02006361 err = ieee80211_get_ratemask(sband, rates, n_rates,
6362 &ibss.basic_rates);
6363 if (err)
6364 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006365 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006366
6367 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6368 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6369 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6370 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006371
Johannes Berg4c476992010-10-04 21:36:35 +02006372 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306373 bool no_ht = false;
6374
Johannes Berg4c476992010-10-04 21:36:35 +02006375 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306376 info->attrs[NL80211_ATTR_KEYS],
6377 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006378 if (IS_ERR(connkeys))
6379 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306380
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006381 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6382 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306383 kfree(connkeys);
6384 return -EINVAL;
6385 }
Johannes Berg4c476992010-10-04 21:36:35 +02006386 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006387
Antonio Quartulli267335d2012-01-31 20:25:47 +01006388 ibss.control_port =
6389 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6390
Johannes Berg4c476992010-10-04 21:36:35 +02006391 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006392 if (err)
6393 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006394 return err;
6395}
6396
6397static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6398{
Johannes Berg4c476992010-10-04 21:36:35 +02006399 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6400 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006401
Johannes Berg4c476992010-10-04 21:36:35 +02006402 if (!rdev->ops->leave_ibss)
6403 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006404
Johannes Berg4c476992010-10-04 21:36:35 +02006405 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6406 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006407
Johannes Berg4c476992010-10-04 21:36:35 +02006408 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006409}
6410
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006411static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6412{
6413 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6414 struct net_device *dev = info->user_ptr[1];
6415 int mcast_rate[IEEE80211_NUM_BANDS];
6416 u32 nla_rate;
6417 int err;
6418
6419 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6420 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6421 return -EOPNOTSUPP;
6422
6423 if (!rdev->ops->set_mcast_rate)
6424 return -EOPNOTSUPP;
6425
6426 memset(mcast_rate, 0, sizeof(mcast_rate));
6427
6428 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6429 return -EINVAL;
6430
6431 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6432 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6433 return -EINVAL;
6434
6435 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6436
6437 return err;
6438}
6439
6440
Johannes Bergaff89a92009-07-01 21:26:51 +02006441#ifdef CONFIG_NL80211_TESTMODE
6442static struct genl_multicast_group nl80211_testmode_mcgrp = {
6443 .name = "testmode",
6444};
6445
6446static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6447{
Johannes Berg4c476992010-10-04 21:36:35 +02006448 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006449 int err;
6450
6451 if (!info->attrs[NL80211_ATTR_TESTDATA])
6452 return -EINVAL;
6453
Johannes Bergaff89a92009-07-01 21:26:51 +02006454 err = -EOPNOTSUPP;
6455 if (rdev->ops->testmode_cmd) {
6456 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006457 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006458 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6459 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6460 rdev->testmode_info = NULL;
6461 }
6462
Johannes Bergaff89a92009-07-01 21:26:51 +02006463 return err;
6464}
6465
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006466static int nl80211_testmode_dump(struct sk_buff *skb,
6467 struct netlink_callback *cb)
6468{
Johannes Berg00918d32011-12-13 17:22:05 +01006469 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006470 int err;
6471 long phy_idx;
6472 void *data = NULL;
6473 int data_len = 0;
6474
6475 if (cb->args[0]) {
6476 /*
6477 * 0 is a valid index, but not valid for args[0],
6478 * so we need to offset by 1.
6479 */
6480 phy_idx = cb->args[0] - 1;
6481 } else {
6482 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6483 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6484 nl80211_policy);
6485 if (err)
6486 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006487
Johannes Berg2bd7e352012-06-15 14:23:16 +02006488 mutex_lock(&cfg80211_mutex);
6489 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6490 nl80211_fam.attrbuf);
6491 if (IS_ERR(rdev)) {
6492 mutex_unlock(&cfg80211_mutex);
6493 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006494 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006495 phy_idx = rdev->wiphy_idx;
6496 rdev = NULL;
6497 mutex_unlock(&cfg80211_mutex);
6498
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006499 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6500 cb->args[1] =
6501 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6502 }
6503
6504 if (cb->args[1]) {
6505 data = nla_data((void *)cb->args[1]);
6506 data_len = nla_len((void *)cb->args[1]);
6507 }
6508
6509 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006510 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6511 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006512 mutex_unlock(&cfg80211_mutex);
6513 return -ENOENT;
6514 }
Johannes Berg00918d32011-12-13 17:22:05 +01006515 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006516 mutex_unlock(&cfg80211_mutex);
6517
Johannes Berg00918d32011-12-13 17:22:05 +01006518 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006519 err = -EOPNOTSUPP;
6520 goto out_err;
6521 }
6522
6523 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006524 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006525 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6526 NL80211_CMD_TESTMODE);
6527 struct nlattr *tmdata;
6528
David S. Miller9360ffd2012-03-29 04:41:26 -04006529 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006530 genlmsg_cancel(skb, hdr);
6531 break;
6532 }
6533
6534 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6535 if (!tmdata) {
6536 genlmsg_cancel(skb, hdr);
6537 break;
6538 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006539 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006540 nla_nest_end(skb, tmdata);
6541
6542 if (err == -ENOBUFS || err == -ENOENT) {
6543 genlmsg_cancel(skb, hdr);
6544 break;
6545 } else if (err) {
6546 genlmsg_cancel(skb, hdr);
6547 goto out_err;
6548 }
6549
6550 genlmsg_end(skb, hdr);
6551 }
6552
6553 err = skb->len;
6554 /* see above */
6555 cb->args[0] = phy_idx + 1;
6556 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006557 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006558 return err;
6559}
6560
Johannes Bergaff89a92009-07-01 21:26:51 +02006561static struct sk_buff *
6562__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006563 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006564{
6565 struct sk_buff *skb;
6566 void *hdr;
6567 struct nlattr *data;
6568
6569 skb = nlmsg_new(approxlen + 100, gfp);
6570 if (!skb)
6571 return NULL;
6572
Eric W. Biederman15e47302012-09-07 20:12:54 +00006573 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006574 if (!hdr) {
6575 kfree_skb(skb);
6576 return NULL;
6577 }
6578
David S. Miller9360ffd2012-03-29 04:41:26 -04006579 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6580 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006581 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6582
6583 ((void **)skb->cb)[0] = rdev;
6584 ((void **)skb->cb)[1] = hdr;
6585 ((void **)skb->cb)[2] = data;
6586
6587 return skb;
6588
6589 nla_put_failure:
6590 kfree_skb(skb);
6591 return NULL;
6592}
6593
6594struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6595 int approxlen)
6596{
6597 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6598
6599 if (WARN_ON(!rdev->testmode_info))
6600 return NULL;
6601
6602 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006603 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006604 rdev->testmode_info->snd_seq,
6605 GFP_KERNEL);
6606}
6607EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6608
6609int cfg80211_testmode_reply(struct sk_buff *skb)
6610{
6611 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6612 void *hdr = ((void **)skb->cb)[1];
6613 struct nlattr *data = ((void **)skb->cb)[2];
6614
6615 if (WARN_ON(!rdev->testmode_info)) {
6616 kfree_skb(skb);
6617 return -EINVAL;
6618 }
6619
6620 nla_nest_end(skb, data);
6621 genlmsg_end(skb, hdr);
6622 return genlmsg_reply(skb, rdev->testmode_info);
6623}
6624EXPORT_SYMBOL(cfg80211_testmode_reply);
6625
6626struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6627 int approxlen, gfp_t gfp)
6628{
6629 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6630
6631 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6632}
6633EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6634
6635void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6636{
6637 void *hdr = ((void **)skb->cb)[1];
6638 struct nlattr *data = ((void **)skb->cb)[2];
6639
6640 nla_nest_end(skb, data);
6641 genlmsg_end(skb, hdr);
6642 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6643}
6644EXPORT_SYMBOL(cfg80211_testmode_event);
6645#endif
6646
Samuel Ortizb23aa672009-07-01 21:26:54 +02006647static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6648{
Johannes Berg4c476992010-10-04 21:36:35 +02006649 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6650 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006651 struct cfg80211_connect_params connect;
6652 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006653 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006654 int err;
6655
6656 memset(&connect, 0, sizeof(connect));
6657
6658 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6659 return -EINVAL;
6660
6661 if (!info->attrs[NL80211_ATTR_SSID] ||
6662 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6663 return -EINVAL;
6664
6665 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6666 connect.auth_type =
6667 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006668 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6669 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006670 return -EINVAL;
6671 } else
6672 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6673
6674 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6675
Johannes Bergc0692b82010-08-27 14:26:53 +03006676 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006677 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006678 if (err)
6679 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006680
Johannes Berg074ac8d2010-09-16 14:58:22 +02006681 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006682 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6683 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006684
Johannes Berg79c97e92009-07-07 03:56:12 +02006685 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006686
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306687 connect.bg_scan_period = -1;
6688 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6689 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6690 connect.bg_scan_period =
6691 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6692 }
6693
Samuel Ortizb23aa672009-07-01 21:26:54 +02006694 if (info->attrs[NL80211_ATTR_MAC])
6695 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6696 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6697 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6698
6699 if (info->attrs[NL80211_ATTR_IE]) {
6700 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6701 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6702 }
6703
Jouni Malinencee00a92013-01-15 17:15:57 +02006704 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6705 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6706 if (connect.mfp != NL80211_MFP_REQUIRED &&
6707 connect.mfp != NL80211_MFP_NO)
6708 return -EINVAL;
6709 } else {
6710 connect.mfp = NL80211_MFP_NO;
6711 }
6712
Samuel Ortizb23aa672009-07-01 21:26:54 +02006713 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6714 connect.channel =
6715 ieee80211_get_channel(wiphy,
6716 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6717 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006718 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6719 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006720 }
6721
Johannes Bergfffd0932009-07-08 14:22:54 +02006722 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6723 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306724 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006725 if (IS_ERR(connkeys))
6726 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006727 }
6728
Ben Greear7e7c8922011-11-18 11:31:59 -08006729 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6730 connect.flags |= ASSOC_REQ_DISABLE_HT;
6731
6732 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6733 memcpy(&connect.ht_capa_mask,
6734 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6735 sizeof(connect.ht_capa_mask));
6736
6737 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006738 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6739 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006740 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006741 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006742 memcpy(&connect.ht_capa,
6743 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6744 sizeof(connect.ht_capa));
6745 }
6746
Johannes Bergee2aca32013-02-21 17:36:01 +01006747 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6748 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6749
6750 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6751 memcpy(&connect.vht_capa_mask,
6752 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6753 sizeof(connect.vht_capa_mask));
6754
6755 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6756 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
6757 kfree(connkeys);
6758 return -EINVAL;
6759 }
6760 memcpy(&connect.vht_capa,
6761 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6762 sizeof(connect.vht_capa));
6763 }
6764
Johannes Bergfffd0932009-07-08 14:22:54 +02006765 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006766 if (err)
6767 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006768 return err;
6769}
6770
6771static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6772{
Johannes Berg4c476992010-10-04 21:36:35 +02006773 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6774 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006775 u16 reason;
6776
6777 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6778 reason = WLAN_REASON_DEAUTH_LEAVING;
6779 else
6780 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6781
6782 if (reason == 0)
6783 return -EINVAL;
6784
Johannes Berg074ac8d2010-09-16 14:58:22 +02006785 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006786 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6787 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006788
Johannes Berg4c476992010-10-04 21:36:35 +02006789 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006790}
6791
Johannes Berg463d0182009-07-14 00:33:35 +02006792static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6793{
Johannes Berg4c476992010-10-04 21:36:35 +02006794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006795 struct net *net;
6796 int err;
6797 u32 pid;
6798
6799 if (!info->attrs[NL80211_ATTR_PID])
6800 return -EINVAL;
6801
6802 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6803
Johannes Berg463d0182009-07-14 00:33:35 +02006804 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006805 if (IS_ERR(net))
6806 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006807
6808 err = 0;
6809
6810 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006811 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6812 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006813
Johannes Berg463d0182009-07-14 00:33:35 +02006814 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006815 return err;
6816}
6817
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006818static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6819{
Johannes Berg4c476992010-10-04 21:36:35 +02006820 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006821 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6822 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006823 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006824 struct cfg80211_pmksa pmksa;
6825
6826 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6827
6828 if (!info->attrs[NL80211_ATTR_MAC])
6829 return -EINVAL;
6830
6831 if (!info->attrs[NL80211_ATTR_PMKID])
6832 return -EINVAL;
6833
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006834 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6835 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6836
Johannes Berg074ac8d2010-09-16 14:58:22 +02006837 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006838 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6839 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006840
6841 switch (info->genlhdr->cmd) {
6842 case NL80211_CMD_SET_PMKSA:
6843 rdev_ops = rdev->ops->set_pmksa;
6844 break;
6845 case NL80211_CMD_DEL_PMKSA:
6846 rdev_ops = rdev->ops->del_pmksa;
6847 break;
6848 default:
6849 WARN_ON(1);
6850 break;
6851 }
6852
Johannes Berg4c476992010-10-04 21:36:35 +02006853 if (!rdev_ops)
6854 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006855
Johannes Berg4c476992010-10-04 21:36:35 +02006856 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006857}
6858
6859static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6860{
Johannes Berg4c476992010-10-04 21:36:35 +02006861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6862 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006863
Johannes Berg074ac8d2010-09-16 14:58:22 +02006864 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006865 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6866 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006867
Johannes Berg4c476992010-10-04 21:36:35 +02006868 if (!rdev->ops->flush_pmksa)
6869 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006870
Hila Gonene35e4d22012-06-27 17:19:42 +03006871 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006872}
6873
Arik Nemtsov109086c2011-09-28 14:12:50 +03006874static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6875{
6876 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6877 struct net_device *dev = info->user_ptr[1];
6878 u8 action_code, dialog_token;
6879 u16 status_code;
6880 u8 *peer;
6881
6882 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6883 !rdev->ops->tdls_mgmt)
6884 return -EOPNOTSUPP;
6885
6886 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6887 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6888 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6889 !info->attrs[NL80211_ATTR_IE] ||
6890 !info->attrs[NL80211_ATTR_MAC])
6891 return -EINVAL;
6892
6893 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6894 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6895 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6896 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6897
Hila Gonene35e4d22012-06-27 17:19:42 +03006898 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6899 dialog_token, status_code,
6900 nla_data(info->attrs[NL80211_ATTR_IE]),
6901 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006902}
6903
6904static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6905{
6906 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6907 struct net_device *dev = info->user_ptr[1];
6908 enum nl80211_tdls_operation operation;
6909 u8 *peer;
6910
6911 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6912 !rdev->ops->tdls_oper)
6913 return -EOPNOTSUPP;
6914
6915 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6916 !info->attrs[NL80211_ATTR_MAC])
6917 return -EINVAL;
6918
6919 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6920 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6921
Hila Gonene35e4d22012-06-27 17:19:42 +03006922 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006923}
6924
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006925static int nl80211_remain_on_channel(struct sk_buff *skb,
6926 struct genl_info *info)
6927{
Johannes Berg4c476992010-10-04 21:36:35 +02006928 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006929 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006930 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006931 struct sk_buff *msg;
6932 void *hdr;
6933 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006934 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006935 int err;
6936
6937 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6938 !info->attrs[NL80211_ATTR_DURATION])
6939 return -EINVAL;
6940
6941 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6942
Johannes Berg7c4ef712011-11-18 15:33:48 +01006943 if (!rdev->ops->remain_on_channel ||
6944 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006945 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006946
Johannes Bergebf348f2012-06-01 12:50:54 +02006947 /*
6948 * We should be on that channel for at least a minimum amount of
6949 * time (10ms) but no longer than the driver supports.
6950 */
6951 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6952 duration > rdev->wiphy.max_remain_on_channel_duration)
6953 return -EINVAL;
6954
Johannes Berg683b6d32012-11-08 21:25:48 +01006955 err = nl80211_parse_chandef(rdev, info, &chandef);
6956 if (err)
6957 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006958
6959 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006960 if (!msg)
6961 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006962
Eric W. Biederman15e47302012-09-07 20:12:54 +00006963 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006964 NL80211_CMD_REMAIN_ON_CHANNEL);
6965
6966 if (IS_ERR(hdr)) {
6967 err = PTR_ERR(hdr);
6968 goto free_msg;
6969 }
6970
Johannes Berg683b6d32012-11-08 21:25:48 +01006971 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6972 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006973
6974 if (err)
6975 goto free_msg;
6976
David S. Miller9360ffd2012-03-29 04:41:26 -04006977 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6978 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006979
6980 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006981
6982 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006983
6984 nla_put_failure:
6985 err = -ENOBUFS;
6986 free_msg:
6987 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006988 return err;
6989}
6990
6991static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6992 struct genl_info *info)
6993{
Johannes Berg4c476992010-10-04 21:36:35 +02006994 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006995 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006996 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006997
6998 if (!info->attrs[NL80211_ATTR_COOKIE])
6999 return -EINVAL;
7000
Johannes Berg4c476992010-10-04 21:36:35 +02007001 if (!rdev->ops->cancel_remain_on_channel)
7002 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007003
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007004 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7005
Hila Gonene35e4d22012-06-27 17:19:42 +03007006 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007007}
7008
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007009static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7010 u8 *rates, u8 rates_len)
7011{
7012 u8 i;
7013 u32 mask = 0;
7014
7015 for (i = 0; i < rates_len; i++) {
7016 int rate = (rates[i] & 0x7f) * 5;
7017 int ridx;
7018 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7019 struct ieee80211_rate *srate =
7020 &sband->bitrates[ridx];
7021 if (rate == srate->bitrate) {
7022 mask |= 1 << ridx;
7023 break;
7024 }
7025 }
7026 if (ridx == sband->n_bitrates)
7027 return 0; /* rate not found */
7028 }
7029
7030 return mask;
7031}
7032
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007033static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7034 u8 *rates, u8 rates_len,
7035 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7036{
7037 u8 i;
7038
7039 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7040
7041 for (i = 0; i < rates_len; i++) {
7042 int ridx, rbit;
7043
7044 ridx = rates[i] / 8;
7045 rbit = BIT(rates[i] % 8);
7046
7047 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007048 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007049 return false;
7050
7051 /* check availability */
7052 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7053 mcs[ridx] |= rbit;
7054 else
7055 return false;
7056 }
7057
7058 return true;
7059}
7060
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007061static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007062 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7063 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007064 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
7065 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007066};
7067
7068static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7069 struct genl_info *info)
7070{
7071 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007072 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007073 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007074 int rem, i;
7075 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007076 struct nlattr *tx_rates;
7077 struct ieee80211_supported_band *sband;
7078
7079 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7080 return -EINVAL;
7081
Johannes Berg4c476992010-10-04 21:36:35 +02007082 if (!rdev->ops->set_bitrate_mask)
7083 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007084
7085 memset(&mask, 0, sizeof(mask));
7086 /* Default to all rates enabled */
7087 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7088 sband = rdev->wiphy.bands[i];
7089 mask.control[i].legacy =
7090 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007091 if (sband)
7092 memcpy(mask.control[i].mcs,
7093 sband->ht_cap.mcs.rx_mask,
7094 sizeof(mask.control[i].mcs));
7095 else
7096 memset(mask.control[i].mcs, 0,
7097 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007098 }
7099
7100 /*
7101 * The nested attribute uses enum nl80211_band as the index. This maps
7102 * directly to the enum ieee80211_band values used in cfg80211.
7103 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007104 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007105 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7106 {
7107 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007108 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7109 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007110 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007111 if (sband == NULL)
7112 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007113 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7114 nla_len(tx_rates), nl80211_txattr_policy);
7115 if (tb[NL80211_TXRATE_LEGACY]) {
7116 mask.control[band].legacy = rateset_to_mask(
7117 sband,
7118 nla_data(tb[NL80211_TXRATE_LEGACY]),
7119 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307120 if ((mask.control[band].legacy == 0) &&
7121 nla_len(tb[NL80211_TXRATE_LEGACY]))
7122 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007123 }
7124 if (tb[NL80211_TXRATE_MCS]) {
7125 if (!ht_rateset_to_mask(
7126 sband,
7127 nla_data(tb[NL80211_TXRATE_MCS]),
7128 nla_len(tb[NL80211_TXRATE_MCS]),
7129 mask.control[band].mcs))
7130 return -EINVAL;
7131 }
7132
7133 if (mask.control[band].legacy == 0) {
7134 /* don't allow empty legacy rates if HT
7135 * is not even supported. */
7136 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7137 return -EINVAL;
7138
7139 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7140 if (mask.control[band].mcs[i])
7141 break;
7142
7143 /* legacy and mcs rates may not be both empty */
7144 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02007145 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007146 }
7147 }
7148
Hila Gonene35e4d22012-06-27 17:19:42 +03007149 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007150}
7151
Johannes Berg2e161f72010-08-12 15:38:38 +02007152static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007153{
Johannes Berg4c476992010-10-04 21:36:35 +02007154 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007155 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007156 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007157
7158 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7159 return -EINVAL;
7160
Johannes Berg2e161f72010-08-12 15:38:38 +02007161 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7162 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007163
Johannes Berg71bbc992012-06-15 15:30:18 +02007164 switch (wdev->iftype) {
7165 case NL80211_IFTYPE_STATION:
7166 case NL80211_IFTYPE_ADHOC:
7167 case NL80211_IFTYPE_P2P_CLIENT:
7168 case NL80211_IFTYPE_AP:
7169 case NL80211_IFTYPE_AP_VLAN:
7170 case NL80211_IFTYPE_MESH_POINT:
7171 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007172 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007173 break;
7174 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007175 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007176 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007177
7178 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007179 if (!rdev->ops->mgmt_tx)
7180 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007181
Eric W. Biederman15e47302012-09-07 20:12:54 +00007182 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007183 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7184 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007185}
7186
Johannes Berg2e161f72010-08-12 15:38:38 +02007187static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007188{
Johannes Berg4c476992010-10-04 21:36:35 +02007189 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007190 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007191 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007192 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007193 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007194 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007195 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007196 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01007197 bool offchan, no_cck, dont_wait_for_ack;
7198
7199 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02007200
Johannes Berg683b6d32012-11-08 21:25:48 +01007201 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007202 return -EINVAL;
7203
Johannes Berg4c476992010-10-04 21:36:35 +02007204 if (!rdev->ops->mgmt_tx)
7205 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007206
Johannes Berg71bbc992012-06-15 15:30:18 +02007207 switch (wdev->iftype) {
7208 case NL80211_IFTYPE_STATION:
7209 case NL80211_IFTYPE_ADHOC:
7210 case NL80211_IFTYPE_P2P_CLIENT:
7211 case NL80211_IFTYPE_AP:
7212 case NL80211_IFTYPE_AP_VLAN:
7213 case NL80211_IFTYPE_MESH_POINT:
7214 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007215 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007216 break;
7217 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007218 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007219 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007220
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007221 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007222 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007223 return -EINVAL;
7224 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007225
7226 /*
7227 * We should wait on the channel for at least a minimum amount
7228 * of time (10ms) but no longer than the driver supports.
7229 */
7230 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7231 wait > rdev->wiphy.max_remain_on_channel_duration)
7232 return -EINVAL;
7233
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007234 }
7235
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007236 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7237
Johannes Berg7c4ef712011-11-18 15:33:48 +01007238 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7239 return -EINVAL;
7240
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307241 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7242
Johannes Berg683b6d32012-11-08 21:25:48 +01007243 err = nl80211_parse_chandef(rdev, info, &chandef);
7244 if (err)
7245 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02007246
Johannes Berge247bd902011-11-04 11:18:21 +01007247 if (!dont_wait_for_ack) {
7248 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7249 if (!msg)
7250 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007251
Eric W. Biederman15e47302012-09-07 20:12:54 +00007252 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007253 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007254
Johannes Berge247bd902011-11-04 11:18:21 +01007255 if (IS_ERR(hdr)) {
7256 err = PTR_ERR(hdr);
7257 goto free_msg;
7258 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007259 }
Johannes Berge247bd902011-11-04 11:18:21 +01007260
Johannes Berg683b6d32012-11-08 21:25:48 +01007261 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02007262 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7263 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01007264 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007265 if (err)
7266 goto free_msg;
7267
Johannes Berge247bd902011-11-04 11:18:21 +01007268 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007269 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7270 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007271
Johannes Berge247bd902011-11-04 11:18:21 +01007272 genlmsg_end(msg, hdr);
7273 return genlmsg_reply(msg, info);
7274 }
7275
7276 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007277
7278 nla_put_failure:
7279 err = -ENOBUFS;
7280 free_msg:
7281 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007282 return err;
7283}
7284
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007285static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7286{
7287 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007288 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007289 u64 cookie;
7290
7291 if (!info->attrs[NL80211_ATTR_COOKIE])
7292 return -EINVAL;
7293
7294 if (!rdev->ops->mgmt_tx_cancel_wait)
7295 return -EOPNOTSUPP;
7296
Johannes Berg71bbc992012-06-15 15:30:18 +02007297 switch (wdev->iftype) {
7298 case NL80211_IFTYPE_STATION:
7299 case NL80211_IFTYPE_ADHOC:
7300 case NL80211_IFTYPE_P2P_CLIENT:
7301 case NL80211_IFTYPE_AP:
7302 case NL80211_IFTYPE_AP_VLAN:
7303 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007304 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007305 break;
7306 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007307 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007308 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007309
7310 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7311
Hila Gonene35e4d22012-06-27 17:19:42 +03007312 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007313}
7314
Kalle Valoffb9eb32010-02-17 17:58:10 +02007315static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7316{
Johannes Berg4c476992010-10-04 21:36:35 +02007317 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007318 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007319 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007320 u8 ps_state;
7321 bool state;
7322 int err;
7323
Johannes Berg4c476992010-10-04 21:36:35 +02007324 if (!info->attrs[NL80211_ATTR_PS_STATE])
7325 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007326
7327 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7328
Johannes Berg4c476992010-10-04 21:36:35 +02007329 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7330 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007331
7332 wdev = dev->ieee80211_ptr;
7333
Johannes Berg4c476992010-10-04 21:36:35 +02007334 if (!rdev->ops->set_power_mgmt)
7335 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007336
7337 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7338
7339 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007340 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007341
Hila Gonene35e4d22012-06-27 17:19:42 +03007342 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007343 if (!err)
7344 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007345 return err;
7346}
7347
7348static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7349{
Johannes Berg4c476992010-10-04 21:36:35 +02007350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007351 enum nl80211_ps_state ps_state;
7352 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007353 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007354 struct sk_buff *msg;
7355 void *hdr;
7356 int err;
7357
Kalle Valoffb9eb32010-02-17 17:58:10 +02007358 wdev = dev->ieee80211_ptr;
7359
Johannes Berg4c476992010-10-04 21:36:35 +02007360 if (!rdev->ops->set_power_mgmt)
7361 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007362
7363 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007364 if (!msg)
7365 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007366
Eric W. Biederman15e47302012-09-07 20:12:54 +00007367 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007368 NL80211_CMD_GET_POWER_SAVE);
7369 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007370 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007371 goto free_msg;
7372 }
7373
7374 if (wdev->ps)
7375 ps_state = NL80211_PS_ENABLED;
7376 else
7377 ps_state = NL80211_PS_DISABLED;
7378
David S. Miller9360ffd2012-03-29 04:41:26 -04007379 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7380 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007381
7382 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007383 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007384
Johannes Berg4c476992010-10-04 21:36:35 +02007385 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007386 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007387 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007388 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007389 return err;
7390}
7391
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007392static struct nla_policy
7393nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7394 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7395 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7396 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007397 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7398 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7399 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007400};
7401
Thomas Pedersen84f10702012-07-12 16:17:33 -07007402static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007403 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007404{
7405 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7406 struct wireless_dev *wdev;
7407 struct net_device *dev = info->user_ptr[1];
7408
Johannes Bergd9d8b012012-11-26 12:51:52 +01007409 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007410 return -EINVAL;
7411
7412 wdev = dev->ieee80211_ptr;
7413
7414 if (!rdev->ops->set_cqm_txe_config)
7415 return -EOPNOTSUPP;
7416
7417 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7418 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7419 return -EOPNOTSUPP;
7420
Hila Gonene35e4d22012-06-27 17:19:42 +03007421 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007422}
7423
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007424static int nl80211_set_cqm_rssi(struct genl_info *info,
7425 s32 threshold, u32 hysteresis)
7426{
Johannes Berg4c476992010-10-04 21:36:35 +02007427 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007428 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007429 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007430
7431 if (threshold > 0)
7432 return -EINVAL;
7433
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007434 wdev = dev->ieee80211_ptr;
7435
Johannes Berg4c476992010-10-04 21:36:35 +02007436 if (!rdev->ops->set_cqm_rssi_config)
7437 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007438
Johannes Berg074ac8d2010-09-16 14:58:22 +02007439 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007440 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7441 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007442
Hila Gonene35e4d22012-06-27 17:19:42 +03007443 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007444}
7445
7446static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7447{
7448 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7449 struct nlattr *cqm;
7450 int err;
7451
7452 cqm = info->attrs[NL80211_ATTR_CQM];
7453 if (!cqm) {
7454 err = -EINVAL;
7455 goto out;
7456 }
7457
7458 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7459 nl80211_attr_cqm_policy);
7460 if (err)
7461 goto out;
7462
7463 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7464 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7465 s32 threshold;
7466 u32 hysteresis;
7467 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7468 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7469 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007470 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7471 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7472 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7473 u32 rate, pkts, intvl;
7474 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7475 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7476 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7477 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007478 } else
7479 err = -EINVAL;
7480
7481out:
7482 return err;
7483}
7484
Johannes Berg29cbe682010-12-03 09:20:44 +01007485static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7486{
7487 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7488 struct net_device *dev = info->user_ptr[1];
7489 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007490 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007491 int err;
7492
7493 /* start with default */
7494 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007495 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007496
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007497 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007498 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007499 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007500 if (err)
7501 return err;
7502 }
7503
7504 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7505 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7506 return -EINVAL;
7507
Javier Cardonac80d5452010-12-16 17:37:49 -08007508 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7509 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7510
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007511 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7512 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7513 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7514 return -EINVAL;
7515
Marco Porsch9bdbf042013-01-07 16:04:51 +01007516 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7517 setup.beacon_interval =
7518 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7519 if (setup.beacon_interval < 10 ||
7520 setup.beacon_interval > 10000)
7521 return -EINVAL;
7522 }
7523
7524 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7525 setup.dtim_period =
7526 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7527 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7528 return -EINVAL;
7529 }
7530
Javier Cardonac80d5452010-12-16 17:37:49 -08007531 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7532 /* parse additional setup parameters if given */
7533 err = nl80211_parse_mesh_setup(info, &setup);
7534 if (err)
7535 return err;
7536 }
7537
Thomas Pedersend37bb182013-03-04 13:06:13 -08007538 if (setup.user_mpm)
7539 cfg.auto_open_plinks = false;
7540
Johannes Bergcc1d2802012-05-16 23:50:20 +02007541 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007542 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7543 if (err)
7544 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007545 } else {
7546 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007547 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007548 }
7549
Javier Cardonac80d5452010-12-16 17:37:49 -08007550 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007551}
7552
7553static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7554{
7555 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7556 struct net_device *dev = info->user_ptr[1];
7557
7558 return cfg80211_leave_mesh(rdev, dev);
7559}
7560
Johannes Bergdfb89c52012-06-27 09:23:48 +02007561#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007562static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7563 struct cfg80211_registered_device *rdev)
7564{
7565 struct nlattr *nl_pats, *nl_pat;
7566 int i, pat_len;
7567
7568 if (!rdev->wowlan->n_patterns)
7569 return 0;
7570
7571 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7572 if (!nl_pats)
7573 return -ENOBUFS;
7574
7575 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7576 nl_pat = nla_nest_start(msg, i + 1);
7577 if (!nl_pat)
7578 return -ENOBUFS;
7579 pat_len = rdev->wowlan->patterns[i].pattern_len;
7580 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7581 DIV_ROUND_UP(pat_len, 8),
7582 rdev->wowlan->patterns[i].mask) ||
7583 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7584 pat_len, rdev->wowlan->patterns[i].pattern) ||
7585 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7586 rdev->wowlan->patterns[i].pkt_offset))
7587 return -ENOBUFS;
7588 nla_nest_end(msg, nl_pat);
7589 }
7590 nla_nest_end(msg, nl_pats);
7591
7592 return 0;
7593}
7594
Johannes Berg2a0e0472013-01-23 22:57:40 +01007595static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7596 struct cfg80211_wowlan_tcp *tcp)
7597{
7598 struct nlattr *nl_tcp;
7599
7600 if (!tcp)
7601 return 0;
7602
7603 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7604 if (!nl_tcp)
7605 return -ENOBUFS;
7606
7607 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7608 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7609 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7610 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7611 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7612 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7613 tcp->payload_len, tcp->payload) ||
7614 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7615 tcp->data_interval) ||
7616 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7617 tcp->wake_len, tcp->wake_data) ||
7618 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7619 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7620 return -ENOBUFS;
7621
7622 if (tcp->payload_seq.len &&
7623 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7624 sizeof(tcp->payload_seq), &tcp->payload_seq))
7625 return -ENOBUFS;
7626
7627 if (tcp->payload_tok.len &&
7628 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7629 sizeof(tcp->payload_tok) + tcp->tokens_size,
7630 &tcp->payload_tok))
7631 return -ENOBUFS;
7632
7633 return 0;
7634}
7635
Johannes Bergff1b6e62011-05-04 15:37:28 +02007636static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7637{
7638 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7639 struct sk_buff *msg;
7640 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007641 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007642
Johannes Berg2a0e0472013-01-23 22:57:40 +01007643 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7644 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007645 return -EOPNOTSUPP;
7646
Johannes Berg2a0e0472013-01-23 22:57:40 +01007647 if (rdev->wowlan && rdev->wowlan->tcp) {
7648 /* adjust size to have room for all the data */
7649 size += rdev->wowlan->tcp->tokens_size +
7650 rdev->wowlan->tcp->payload_len +
7651 rdev->wowlan->tcp->wake_len +
7652 rdev->wowlan->tcp->wake_len / 8;
7653 }
7654
7655 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007656 if (!msg)
7657 return -ENOMEM;
7658
Eric W. Biederman15e47302012-09-07 20:12:54 +00007659 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007660 NL80211_CMD_GET_WOWLAN);
7661 if (!hdr)
7662 goto nla_put_failure;
7663
7664 if (rdev->wowlan) {
7665 struct nlattr *nl_wowlan;
7666
7667 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7668 if (!nl_wowlan)
7669 goto nla_put_failure;
7670
David S. Miller9360ffd2012-03-29 04:41:26 -04007671 if ((rdev->wowlan->any &&
7672 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7673 (rdev->wowlan->disconnect &&
7674 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7675 (rdev->wowlan->magic_pkt &&
7676 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7677 (rdev->wowlan->gtk_rekey_failure &&
7678 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7679 (rdev->wowlan->eap_identity_req &&
7680 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7681 (rdev->wowlan->four_way_handshake &&
7682 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7683 (rdev->wowlan->rfkill_release &&
7684 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7685 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007686
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007687 if (nl80211_send_wowlan_patterns(msg, rdev))
7688 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007689
7690 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7691 goto nla_put_failure;
7692
Johannes Bergff1b6e62011-05-04 15:37:28 +02007693 nla_nest_end(msg, nl_wowlan);
7694 }
7695
7696 genlmsg_end(msg, hdr);
7697 return genlmsg_reply(msg, info);
7698
7699nla_put_failure:
7700 nlmsg_free(msg);
7701 return -ENOBUFS;
7702}
7703
Johannes Berg2a0e0472013-01-23 22:57:40 +01007704static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7705 struct nlattr *attr,
7706 struct cfg80211_wowlan *trig)
7707{
7708 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7709 struct cfg80211_wowlan_tcp *cfg;
7710 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7711 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7712 u32 size;
7713 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7714 int err, port;
7715
7716 if (!rdev->wiphy.wowlan.tcp)
7717 return -EINVAL;
7718
7719 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7720 nla_data(attr), nla_len(attr),
7721 nl80211_wowlan_tcp_policy);
7722 if (err)
7723 return err;
7724
7725 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7726 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7727 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7728 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7729 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7730 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7731 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7732 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7733 return -EINVAL;
7734
7735 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7736 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7737 return -EINVAL;
7738
7739 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg723d5682013-02-26 13:56:40 +01007740 rdev->wiphy.wowlan.tcp->data_interval_max ||
7741 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007742 return -EINVAL;
7743
7744 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7745 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7746 return -EINVAL;
7747
7748 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7749 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7750 return -EINVAL;
7751
7752 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7753 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7754
7755 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7756 tokens_size = tokln - sizeof(*tok);
7757
7758 if (!tok->len || tokens_size % tok->len)
7759 return -EINVAL;
7760 if (!rdev->wiphy.wowlan.tcp->tok)
7761 return -EINVAL;
7762 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7763 return -EINVAL;
7764 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7765 return -EINVAL;
7766 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7767 return -EINVAL;
7768 if (tok->offset + tok->len > data_size)
7769 return -EINVAL;
7770 }
7771
7772 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7773 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7774 if (!rdev->wiphy.wowlan.tcp->seq)
7775 return -EINVAL;
7776 if (seq->len == 0 || seq->len > 4)
7777 return -EINVAL;
7778 if (seq->len + seq->offset > data_size)
7779 return -EINVAL;
7780 }
7781
7782 size = sizeof(*cfg);
7783 size += data_size;
7784 size += wake_size + wake_mask_size;
7785 size += tokens_size;
7786
7787 cfg = kzalloc(size, GFP_KERNEL);
7788 if (!cfg)
7789 return -ENOMEM;
7790 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7791 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7792 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7793 ETH_ALEN);
7794 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7795 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7796 else
7797 port = 0;
7798#ifdef CONFIG_INET
7799 /* allocate a socket and port for it and use it */
7800 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7801 IPPROTO_TCP, &cfg->sock, 1);
7802 if (err) {
7803 kfree(cfg);
7804 return err;
7805 }
7806 if (inet_csk_get_port(cfg->sock->sk, port)) {
7807 sock_release(cfg->sock);
7808 kfree(cfg);
7809 return -EADDRINUSE;
7810 }
7811 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7812#else
7813 if (!port) {
7814 kfree(cfg);
7815 return -EINVAL;
7816 }
7817 cfg->src_port = port;
7818#endif
7819
7820 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7821 cfg->payload_len = data_size;
7822 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7823 memcpy((void *)cfg->payload,
7824 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7825 data_size);
7826 if (seq)
7827 cfg->payload_seq = *seq;
7828 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7829 cfg->wake_len = wake_size;
7830 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7831 memcpy((void *)cfg->wake_data,
7832 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7833 wake_size);
7834 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7835 data_size + wake_size;
7836 memcpy((void *)cfg->wake_mask,
7837 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7838 wake_mask_size);
7839 if (tok) {
7840 cfg->tokens_size = tokens_size;
7841 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7842 }
7843
7844 trig->tcp = cfg;
7845
7846 return 0;
7847}
7848
Johannes Bergff1b6e62011-05-04 15:37:28 +02007849static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7850{
7851 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7852 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007853 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007854 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007855 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7856 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007857 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007858
Johannes Berg2a0e0472013-01-23 22:57:40 +01007859 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7860 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007861 return -EOPNOTSUPP;
7862
Johannes Bergae33bd82012-07-12 16:25:02 +02007863 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7864 cfg80211_rdev_free_wowlan(rdev);
7865 rdev->wowlan = NULL;
7866 goto set_wakeup;
7867 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007868
7869 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7870 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7871 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7872 nl80211_wowlan_policy);
7873 if (err)
7874 return err;
7875
7876 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7877 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7878 return -EINVAL;
7879 new_triggers.any = true;
7880 }
7881
7882 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7883 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7884 return -EINVAL;
7885 new_triggers.disconnect = true;
7886 }
7887
7888 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7889 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7890 return -EINVAL;
7891 new_triggers.magic_pkt = true;
7892 }
7893
Johannes Berg77dbbb132011-07-13 10:48:55 +02007894 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7895 return -EINVAL;
7896
7897 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7898 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7899 return -EINVAL;
7900 new_triggers.gtk_rekey_failure = true;
7901 }
7902
7903 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7904 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7905 return -EINVAL;
7906 new_triggers.eap_identity_req = true;
7907 }
7908
7909 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7910 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7911 return -EINVAL;
7912 new_triggers.four_way_handshake = true;
7913 }
7914
7915 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7916 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7917 return -EINVAL;
7918 new_triggers.rfkill_release = true;
7919 }
7920
Johannes Bergff1b6e62011-05-04 15:37:28 +02007921 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7922 struct nlattr *pat;
7923 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007924 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007925 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7926
7927 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7928 rem)
7929 n_patterns++;
7930 if (n_patterns > wowlan->n_patterns)
7931 return -EINVAL;
7932
7933 new_triggers.patterns = kcalloc(n_patterns,
7934 sizeof(new_triggers.patterns[0]),
7935 GFP_KERNEL);
7936 if (!new_triggers.patterns)
7937 return -ENOMEM;
7938
7939 new_triggers.n_patterns = n_patterns;
7940 i = 0;
7941
7942 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7943 rem) {
7944 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7945 nla_data(pat), nla_len(pat), NULL);
7946 err = -EINVAL;
7947 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7948 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7949 goto error;
7950 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7951 mask_len = DIV_ROUND_UP(pat_len, 8);
7952 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7953 mask_len)
7954 goto error;
7955 if (pat_len > wowlan->pattern_max_len ||
7956 pat_len < wowlan->pattern_min_len)
7957 goto error;
7958
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007959 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7960 pkt_offset = 0;
7961 else
7962 pkt_offset = nla_get_u32(
7963 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7964 if (pkt_offset > wowlan->max_pkt_offset)
7965 goto error;
7966 new_triggers.patterns[i].pkt_offset = pkt_offset;
7967
Johannes Bergff1b6e62011-05-04 15:37:28 +02007968 new_triggers.patterns[i].mask =
7969 kmalloc(mask_len + pat_len, GFP_KERNEL);
7970 if (!new_triggers.patterns[i].mask) {
7971 err = -ENOMEM;
7972 goto error;
7973 }
7974 new_triggers.patterns[i].pattern =
7975 new_triggers.patterns[i].mask + mask_len;
7976 memcpy(new_triggers.patterns[i].mask,
7977 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7978 mask_len);
7979 new_triggers.patterns[i].pattern_len = pat_len;
7980 memcpy(new_triggers.patterns[i].pattern,
7981 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7982 pat_len);
7983 i++;
7984 }
7985 }
7986
Johannes Berg2a0e0472013-01-23 22:57:40 +01007987 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7988 err = nl80211_parse_wowlan_tcp(
7989 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7990 &new_triggers);
7991 if (err)
7992 goto error;
7993 }
7994
Johannes Bergae33bd82012-07-12 16:25:02 +02007995 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7996 if (!ntrig) {
7997 err = -ENOMEM;
7998 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007999 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008000 cfg80211_rdev_free_wowlan(rdev);
8001 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008002
Johannes Bergae33bd82012-07-12 16:25:02 +02008003 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02008004 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03008005 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02008006
Johannes Bergff1b6e62011-05-04 15:37:28 +02008007 return 0;
8008 error:
8009 for (i = 0; i < new_triggers.n_patterns; i++)
8010 kfree(new_triggers.patterns[i].mask);
8011 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008012 if (new_triggers.tcp && new_triggers.tcp->sock)
8013 sock_release(new_triggers.tcp->sock);
8014 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008015 return err;
8016}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008017#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008018
Johannes Berge5497d72011-07-05 16:35:40 +02008019static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8020{
8021 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8022 struct net_device *dev = info->user_ptr[1];
8023 struct wireless_dev *wdev = dev->ieee80211_ptr;
8024 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8025 struct cfg80211_gtk_rekey_data rekey_data;
8026 int err;
8027
8028 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8029 return -EINVAL;
8030
8031 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8032 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8033 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8034 nl80211_rekey_policy);
8035 if (err)
8036 return err;
8037
8038 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8039 return -ERANGE;
8040 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8041 return -ERANGE;
8042 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8043 return -ERANGE;
8044
8045 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8046 NL80211_KEK_LEN);
8047 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8048 NL80211_KCK_LEN);
8049 memcpy(rekey_data.replay_ctr,
8050 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8051 NL80211_REPLAY_CTR_LEN);
8052
8053 wdev_lock(wdev);
8054 if (!wdev->current_bss) {
8055 err = -ENOTCONN;
8056 goto out;
8057 }
8058
8059 if (!rdev->ops->set_rekey_data) {
8060 err = -EOPNOTSUPP;
8061 goto out;
8062 }
8063
Hila Gonene35e4d22012-06-27 17:19:42 +03008064 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008065 out:
8066 wdev_unlock(wdev);
8067 return err;
8068}
8069
Johannes Berg28946da2011-11-04 11:18:12 +01008070static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8071 struct genl_info *info)
8072{
8073 struct net_device *dev = info->user_ptr[1];
8074 struct wireless_dev *wdev = dev->ieee80211_ptr;
8075
8076 if (wdev->iftype != NL80211_IFTYPE_AP &&
8077 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8078 return -EINVAL;
8079
Eric W. Biederman15e47302012-09-07 20:12:54 +00008080 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008081 return -EBUSY;
8082
Eric W. Biederman15e47302012-09-07 20:12:54 +00008083 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008084 return 0;
8085}
8086
Johannes Berg7f6cf312011-11-04 11:18:15 +01008087static int nl80211_probe_client(struct sk_buff *skb,
8088 struct genl_info *info)
8089{
8090 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8091 struct net_device *dev = info->user_ptr[1];
8092 struct wireless_dev *wdev = dev->ieee80211_ptr;
8093 struct sk_buff *msg;
8094 void *hdr;
8095 const u8 *addr;
8096 u64 cookie;
8097 int err;
8098
8099 if (wdev->iftype != NL80211_IFTYPE_AP &&
8100 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8101 return -EOPNOTSUPP;
8102
8103 if (!info->attrs[NL80211_ATTR_MAC])
8104 return -EINVAL;
8105
8106 if (!rdev->ops->probe_client)
8107 return -EOPNOTSUPP;
8108
8109 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8110 if (!msg)
8111 return -ENOMEM;
8112
Eric W. Biederman15e47302012-09-07 20:12:54 +00008113 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008114 NL80211_CMD_PROBE_CLIENT);
8115
8116 if (IS_ERR(hdr)) {
8117 err = PTR_ERR(hdr);
8118 goto free_msg;
8119 }
8120
8121 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8122
Hila Gonene35e4d22012-06-27 17:19:42 +03008123 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008124 if (err)
8125 goto free_msg;
8126
David S. Miller9360ffd2012-03-29 04:41:26 -04008127 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8128 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008129
8130 genlmsg_end(msg, hdr);
8131
8132 return genlmsg_reply(msg, info);
8133
8134 nla_put_failure:
8135 err = -ENOBUFS;
8136 free_msg:
8137 nlmsg_free(msg);
8138 return err;
8139}
8140
Johannes Berg5e760232011-11-04 11:18:17 +01008141static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8142{
8143 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008144 struct cfg80211_beacon_registration *reg, *nreg;
8145 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008146
8147 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8148 return -EOPNOTSUPP;
8149
Ben Greear37c73b52012-10-26 14:49:25 -07008150 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8151 if (!nreg)
8152 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008153
Ben Greear37c73b52012-10-26 14:49:25 -07008154 /* First, check if already registered. */
8155 spin_lock_bh(&rdev->beacon_registrations_lock);
8156 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8157 if (reg->nlportid == info->snd_portid) {
8158 rv = -EALREADY;
8159 goto out_err;
8160 }
8161 }
8162 /* Add it to the list */
8163 nreg->nlportid = info->snd_portid;
8164 list_add(&nreg->list, &rdev->beacon_registrations);
8165
8166 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008167
8168 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008169out_err:
8170 spin_unlock_bh(&rdev->beacon_registrations_lock);
8171 kfree(nreg);
8172 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008173}
8174
Johannes Berg98104fde2012-06-16 00:19:54 +02008175static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8176{
8177 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8178 struct wireless_dev *wdev = info->user_ptr[1];
8179 int err;
8180
8181 if (!rdev->ops->start_p2p_device)
8182 return -EOPNOTSUPP;
8183
8184 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8185 return -EOPNOTSUPP;
8186
8187 if (wdev->p2p_started)
8188 return 0;
8189
8190 mutex_lock(&rdev->devlist_mtx);
8191 err = cfg80211_can_add_interface(rdev, wdev->iftype);
8192 mutex_unlock(&rdev->devlist_mtx);
8193 if (err)
8194 return err;
8195
Johannes Bergeeb126e2012-10-23 15:16:50 +02008196 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008197 if (err)
8198 return err;
8199
8200 wdev->p2p_started = true;
8201 mutex_lock(&rdev->devlist_mtx);
8202 rdev->opencount++;
8203 mutex_unlock(&rdev->devlist_mtx);
8204
8205 return 0;
8206}
8207
8208static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8209{
8210 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8211 struct wireless_dev *wdev = info->user_ptr[1];
8212
8213 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8214 return -EOPNOTSUPP;
8215
8216 if (!rdev->ops->stop_p2p_device)
8217 return -EOPNOTSUPP;
8218
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008219 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +01008220 mutex_lock(&rdev->sched_scan_mtx);
8221 cfg80211_stop_p2p_device(rdev, wdev);
8222 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008223 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg98104fde2012-06-16 00:19:54 +02008224
8225 return 0;
8226}
8227
Johannes Berg3713b4e2013-02-14 16:19:38 +01008228static int nl80211_get_protocol_features(struct sk_buff *skb,
8229 struct genl_info *info)
8230{
8231 void *hdr;
8232 struct sk_buff *msg;
8233
8234 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8235 if (!msg)
8236 return -ENOMEM;
8237
8238 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8239 NL80211_CMD_GET_PROTOCOL_FEATURES);
8240 if (!hdr)
8241 goto nla_put_failure;
8242
8243 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8244 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8245 goto nla_put_failure;
8246
8247 genlmsg_end(msg, hdr);
8248 return genlmsg_reply(msg, info);
8249
8250 nla_put_failure:
8251 kfree_skb(msg);
8252 return -ENOBUFS;
8253}
8254
Jouni Malinen355199e2013-02-27 17:14:27 +02008255static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8256{
8257 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8258 struct cfg80211_update_ft_ies_params ft_params;
8259 struct net_device *dev = info->user_ptr[1];
8260
8261 if (!rdev->ops->update_ft_ies)
8262 return -EOPNOTSUPP;
8263
8264 if (!info->attrs[NL80211_ATTR_MDID] ||
8265 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8266 return -EINVAL;
8267
8268 memset(&ft_params, 0, sizeof(ft_params));
8269 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8270 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8271 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8272
8273 return rdev_update_ft_ies(rdev, dev, &ft_params);
8274}
8275
Arend van Spriel5de17982013-04-18 15:49:00 +02008276static int nl80211_crit_protocol_start(struct sk_buff *skb,
8277 struct genl_info *info)
8278{
8279 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8280 struct wireless_dev *wdev = info->user_ptr[1];
8281 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8282 u16 duration;
8283 int ret;
8284
8285 if (!rdev->ops->crit_proto_start)
8286 return -EOPNOTSUPP;
8287
8288 if (WARN_ON(!rdev->ops->crit_proto_stop))
8289 return -EINVAL;
8290
8291 if (rdev->crit_proto_nlportid)
8292 return -EBUSY;
8293
8294 /* determine protocol if provided */
8295 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8296 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8297
8298 if (proto >= NUM_NL80211_CRIT_PROTO)
8299 return -EINVAL;
8300
8301 /* timeout must be provided */
8302 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8303 return -EINVAL;
8304
8305 duration =
8306 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8307
8308 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8309 return -ERANGE;
8310
8311 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8312 if (!ret)
8313 rdev->crit_proto_nlportid = info->snd_portid;
8314
8315 return ret;
8316}
8317
8318static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8319 struct genl_info *info)
8320{
8321 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8322 struct wireless_dev *wdev = info->user_ptr[1];
8323
8324 if (!rdev->ops->crit_proto_stop)
8325 return -EOPNOTSUPP;
8326
8327 if (rdev->crit_proto_nlportid) {
8328 rdev->crit_proto_nlportid = 0;
8329 rdev_crit_proto_stop(rdev, wdev);
8330 }
8331 return 0;
8332}
8333
Johannes Berg4c476992010-10-04 21:36:35 +02008334#define NL80211_FLAG_NEED_WIPHY 0x01
8335#define NL80211_FLAG_NEED_NETDEV 0x02
8336#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008337#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8338#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8339 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008340#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008341/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008342#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8343 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008344
8345static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8346 struct genl_info *info)
8347{
8348 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008349 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008350 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008351 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8352
8353 if (rtnl)
8354 rtnl_lock();
8355
8356 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008357 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008358 if (IS_ERR(rdev)) {
8359 if (rtnl)
8360 rtnl_unlock();
8361 return PTR_ERR(rdev);
8362 }
8363 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008364 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8365 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008366 mutex_lock(&cfg80211_mutex);
8367 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8368 info->attrs);
8369 if (IS_ERR(wdev)) {
8370 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02008371 if (rtnl)
8372 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02008373 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02008374 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008375
Johannes Berg89a54e42012-06-15 14:33:17 +02008376 dev = wdev->netdev;
8377 rdev = wiphy_to_dev(wdev->wiphy);
8378
Johannes Berg1bf614e2012-06-15 15:23:36 +02008379 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8380 if (!dev) {
8381 mutex_unlock(&cfg80211_mutex);
8382 if (rtnl)
8383 rtnl_unlock();
8384 return -EINVAL;
8385 }
8386
8387 info->user_ptr[1] = dev;
8388 } else {
8389 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02008390 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008391
Johannes Berg1bf614e2012-06-15 15:23:36 +02008392 if (dev) {
8393 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8394 !netif_running(dev)) {
8395 mutex_unlock(&cfg80211_mutex);
8396 if (rtnl)
8397 rtnl_unlock();
8398 return -ENETDOWN;
8399 }
8400
8401 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008402 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8403 if (!wdev->p2p_started) {
8404 mutex_unlock(&cfg80211_mutex);
8405 if (rtnl)
8406 rtnl_unlock();
8407 return -ENETDOWN;
8408 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02008409 }
8410
Johannes Berg89a54e42012-06-15 14:33:17 +02008411 cfg80211_lock_rdev(rdev);
8412
8413 mutex_unlock(&cfg80211_mutex);
8414
Johannes Berg4c476992010-10-04 21:36:35 +02008415 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008416 }
8417
8418 return 0;
8419}
8420
8421static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8422 struct genl_info *info)
8423{
8424 if (info->user_ptr[0])
8425 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02008426 if (info->user_ptr[1]) {
8427 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8428 struct wireless_dev *wdev = info->user_ptr[1];
8429
8430 if (wdev->netdev)
8431 dev_put(wdev->netdev);
8432 } else {
8433 dev_put(info->user_ptr[1]);
8434 }
8435 }
Johannes Berg4c476992010-10-04 21:36:35 +02008436 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8437 rtnl_unlock();
8438}
8439
Johannes Berg55682962007-09-20 13:09:35 -04008440static struct genl_ops nl80211_ops[] = {
8441 {
8442 .cmd = NL80211_CMD_GET_WIPHY,
8443 .doit = nl80211_get_wiphy,
8444 .dumpit = nl80211_dump_wiphy,
8445 .policy = nl80211_policy,
8446 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008447 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04008448 },
8449 {
8450 .cmd = NL80211_CMD_SET_WIPHY,
8451 .doit = nl80211_set_wiphy,
8452 .policy = nl80211_policy,
8453 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008454 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008455 },
8456 {
8457 .cmd = NL80211_CMD_GET_INTERFACE,
8458 .doit = nl80211_get_interface,
8459 .dumpit = nl80211_dump_interface,
8460 .policy = nl80211_policy,
8461 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02008462 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04008463 },
8464 {
8465 .cmd = NL80211_CMD_SET_INTERFACE,
8466 .doit = nl80211_set_interface,
8467 .policy = nl80211_policy,
8468 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008469 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8470 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008471 },
8472 {
8473 .cmd = NL80211_CMD_NEW_INTERFACE,
8474 .doit = nl80211_new_interface,
8475 .policy = nl80211_policy,
8476 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008477 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8478 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008479 },
8480 {
8481 .cmd = NL80211_CMD_DEL_INTERFACE,
8482 .doit = nl80211_del_interface,
8483 .policy = nl80211_policy,
8484 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02008485 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008486 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008487 },
Johannes Berg41ade002007-12-19 02:03:29 +01008488 {
8489 .cmd = NL80211_CMD_GET_KEY,
8490 .doit = nl80211_get_key,
8491 .policy = nl80211_policy,
8492 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008493 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008494 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008495 },
8496 {
8497 .cmd = NL80211_CMD_SET_KEY,
8498 .doit = nl80211_set_key,
8499 .policy = nl80211_policy,
8500 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008501 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008502 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008503 },
8504 {
8505 .cmd = NL80211_CMD_NEW_KEY,
8506 .doit = nl80211_new_key,
8507 .policy = nl80211_policy,
8508 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008509 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008510 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008511 },
8512 {
8513 .cmd = NL80211_CMD_DEL_KEY,
8514 .doit = nl80211_del_key,
8515 .policy = nl80211_policy,
8516 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008517 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008518 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008519 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008520 {
8521 .cmd = NL80211_CMD_SET_BEACON,
8522 .policy = nl80211_policy,
8523 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008524 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008525 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008526 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008527 },
8528 {
Johannes Berg88600202012-02-13 15:17:18 +01008529 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008530 .policy = nl80211_policy,
8531 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008532 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008533 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008534 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008535 },
8536 {
Johannes Berg88600202012-02-13 15:17:18 +01008537 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008538 .policy = nl80211_policy,
8539 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008540 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008541 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008542 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008543 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008544 {
8545 .cmd = NL80211_CMD_GET_STATION,
8546 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008547 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008548 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008549 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8550 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008551 },
8552 {
8553 .cmd = NL80211_CMD_SET_STATION,
8554 .doit = nl80211_set_station,
8555 .policy = nl80211_policy,
8556 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008557 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008558 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008559 },
8560 {
8561 .cmd = NL80211_CMD_NEW_STATION,
8562 .doit = nl80211_new_station,
8563 .policy = nl80211_policy,
8564 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008565 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008566 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008567 },
8568 {
8569 .cmd = NL80211_CMD_DEL_STATION,
8570 .doit = nl80211_del_station,
8571 .policy = nl80211_policy,
8572 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008573 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008574 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008575 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008576 {
8577 .cmd = NL80211_CMD_GET_MPATH,
8578 .doit = nl80211_get_mpath,
8579 .dumpit = nl80211_dump_mpath,
8580 .policy = nl80211_policy,
8581 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008582 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008583 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008584 },
8585 {
8586 .cmd = NL80211_CMD_SET_MPATH,
8587 .doit = nl80211_set_mpath,
8588 .policy = nl80211_policy,
8589 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008590 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008591 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008592 },
8593 {
8594 .cmd = NL80211_CMD_NEW_MPATH,
8595 .doit = nl80211_new_mpath,
8596 .policy = nl80211_policy,
8597 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008598 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008599 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008600 },
8601 {
8602 .cmd = NL80211_CMD_DEL_MPATH,
8603 .doit = nl80211_del_mpath,
8604 .policy = nl80211_policy,
8605 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008606 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008607 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008608 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008609 {
8610 .cmd = NL80211_CMD_SET_BSS,
8611 .doit = nl80211_set_bss,
8612 .policy = nl80211_policy,
8613 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008614 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008615 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008616 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008617 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008618 .cmd = NL80211_CMD_GET_REG,
8619 .doit = nl80211_get_reg,
8620 .policy = nl80211_policy,
8621 /* can be retrieved by unprivileged users */
8622 },
8623 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008624 .cmd = NL80211_CMD_SET_REG,
8625 .doit = nl80211_set_reg,
8626 .policy = nl80211_policy,
8627 .flags = GENL_ADMIN_PERM,
8628 },
8629 {
8630 .cmd = NL80211_CMD_REQ_SET_REG,
8631 .doit = nl80211_req_set_reg,
8632 .policy = nl80211_policy,
8633 .flags = GENL_ADMIN_PERM,
8634 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008635 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008636 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8637 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008638 .policy = nl80211_policy,
8639 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008640 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008641 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008642 },
8643 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008644 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8645 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008646 .policy = nl80211_policy,
8647 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008648 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008649 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008650 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008651 {
Johannes Berg2a519312009-02-10 21:25:55 +01008652 .cmd = NL80211_CMD_TRIGGER_SCAN,
8653 .doit = nl80211_trigger_scan,
8654 .policy = nl80211_policy,
8655 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008656 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008657 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008658 },
8659 {
8660 .cmd = NL80211_CMD_GET_SCAN,
8661 .policy = nl80211_policy,
8662 .dumpit = nl80211_dump_scan,
8663 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008664 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008665 .cmd = NL80211_CMD_START_SCHED_SCAN,
8666 .doit = nl80211_start_sched_scan,
8667 .policy = nl80211_policy,
8668 .flags = GENL_ADMIN_PERM,
8669 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8670 NL80211_FLAG_NEED_RTNL,
8671 },
8672 {
8673 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8674 .doit = nl80211_stop_sched_scan,
8675 .policy = nl80211_policy,
8676 .flags = GENL_ADMIN_PERM,
8677 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8678 NL80211_FLAG_NEED_RTNL,
8679 },
8680 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008681 .cmd = NL80211_CMD_AUTHENTICATE,
8682 .doit = nl80211_authenticate,
8683 .policy = nl80211_policy,
8684 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008685 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008686 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008687 },
8688 {
8689 .cmd = NL80211_CMD_ASSOCIATE,
8690 .doit = nl80211_associate,
8691 .policy = nl80211_policy,
8692 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008693 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008694 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008695 },
8696 {
8697 .cmd = NL80211_CMD_DEAUTHENTICATE,
8698 .doit = nl80211_deauthenticate,
8699 .policy = nl80211_policy,
8700 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008701 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008702 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008703 },
8704 {
8705 .cmd = NL80211_CMD_DISASSOCIATE,
8706 .doit = nl80211_disassociate,
8707 .policy = nl80211_policy,
8708 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008709 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008710 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008711 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008712 {
8713 .cmd = NL80211_CMD_JOIN_IBSS,
8714 .doit = nl80211_join_ibss,
8715 .policy = nl80211_policy,
8716 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008717 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008718 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008719 },
8720 {
8721 .cmd = NL80211_CMD_LEAVE_IBSS,
8722 .doit = nl80211_leave_ibss,
8723 .policy = nl80211_policy,
8724 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008725 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008726 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008727 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008728#ifdef CONFIG_NL80211_TESTMODE
8729 {
8730 .cmd = NL80211_CMD_TESTMODE,
8731 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008732 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008733 .policy = nl80211_policy,
8734 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008735 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8736 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008737 },
8738#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008739 {
8740 .cmd = NL80211_CMD_CONNECT,
8741 .doit = nl80211_connect,
8742 .policy = nl80211_policy,
8743 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008744 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008745 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008746 },
8747 {
8748 .cmd = NL80211_CMD_DISCONNECT,
8749 .doit = nl80211_disconnect,
8750 .policy = nl80211_policy,
8751 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008752 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008753 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008754 },
Johannes Berg463d0182009-07-14 00:33:35 +02008755 {
8756 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8757 .doit = nl80211_wiphy_netns,
8758 .policy = nl80211_policy,
8759 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008760 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8761 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008762 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008763 {
8764 .cmd = NL80211_CMD_GET_SURVEY,
8765 .policy = nl80211_policy,
8766 .dumpit = nl80211_dump_survey,
8767 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008768 {
8769 .cmd = NL80211_CMD_SET_PMKSA,
8770 .doit = nl80211_setdel_pmksa,
8771 .policy = nl80211_policy,
8772 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008773 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008774 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008775 },
8776 {
8777 .cmd = NL80211_CMD_DEL_PMKSA,
8778 .doit = nl80211_setdel_pmksa,
8779 .policy = nl80211_policy,
8780 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008781 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008782 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008783 },
8784 {
8785 .cmd = NL80211_CMD_FLUSH_PMKSA,
8786 .doit = nl80211_flush_pmksa,
8787 .policy = nl80211_policy,
8788 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008789 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008790 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008791 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008792 {
8793 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8794 .doit = nl80211_remain_on_channel,
8795 .policy = nl80211_policy,
8796 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008797 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008798 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008799 },
8800 {
8801 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8802 .doit = nl80211_cancel_remain_on_channel,
8803 .policy = nl80211_policy,
8804 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008805 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008806 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008807 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008808 {
8809 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8810 .doit = nl80211_set_tx_bitrate_mask,
8811 .policy = nl80211_policy,
8812 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008813 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8814 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008815 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008816 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008817 .cmd = NL80211_CMD_REGISTER_FRAME,
8818 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008819 .policy = nl80211_policy,
8820 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008821 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008822 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008823 },
8824 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008825 .cmd = NL80211_CMD_FRAME,
8826 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008827 .policy = nl80211_policy,
8828 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008829 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008830 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008831 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008832 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008833 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8834 .doit = nl80211_tx_mgmt_cancel_wait,
8835 .policy = nl80211_policy,
8836 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008837 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008838 NL80211_FLAG_NEED_RTNL,
8839 },
8840 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008841 .cmd = NL80211_CMD_SET_POWER_SAVE,
8842 .doit = nl80211_set_power_save,
8843 .policy = nl80211_policy,
8844 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008845 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8846 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008847 },
8848 {
8849 .cmd = NL80211_CMD_GET_POWER_SAVE,
8850 .doit = nl80211_get_power_save,
8851 .policy = nl80211_policy,
8852 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008853 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8854 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008855 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008856 {
8857 .cmd = NL80211_CMD_SET_CQM,
8858 .doit = nl80211_set_cqm,
8859 .policy = nl80211_policy,
8860 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008861 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8862 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008863 },
Johannes Bergf444de02010-05-05 15:25:02 +02008864 {
8865 .cmd = NL80211_CMD_SET_CHANNEL,
8866 .doit = nl80211_set_channel,
8867 .policy = nl80211_policy,
8868 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008869 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8870 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008871 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008872 {
8873 .cmd = NL80211_CMD_SET_WDS_PEER,
8874 .doit = nl80211_set_wds_peer,
8875 .policy = nl80211_policy,
8876 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008877 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8878 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008879 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008880 {
8881 .cmd = NL80211_CMD_JOIN_MESH,
8882 .doit = nl80211_join_mesh,
8883 .policy = nl80211_policy,
8884 .flags = GENL_ADMIN_PERM,
8885 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8886 NL80211_FLAG_NEED_RTNL,
8887 },
8888 {
8889 .cmd = NL80211_CMD_LEAVE_MESH,
8890 .doit = nl80211_leave_mesh,
8891 .policy = nl80211_policy,
8892 .flags = GENL_ADMIN_PERM,
8893 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8894 NL80211_FLAG_NEED_RTNL,
8895 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008896#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008897 {
8898 .cmd = NL80211_CMD_GET_WOWLAN,
8899 .doit = nl80211_get_wowlan,
8900 .policy = nl80211_policy,
8901 /* can be retrieved by unprivileged users */
8902 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8903 NL80211_FLAG_NEED_RTNL,
8904 },
8905 {
8906 .cmd = NL80211_CMD_SET_WOWLAN,
8907 .doit = nl80211_set_wowlan,
8908 .policy = nl80211_policy,
8909 .flags = GENL_ADMIN_PERM,
8910 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8911 NL80211_FLAG_NEED_RTNL,
8912 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008913#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008914 {
8915 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8916 .doit = nl80211_set_rekey_data,
8917 .policy = nl80211_policy,
8918 .flags = GENL_ADMIN_PERM,
8919 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8920 NL80211_FLAG_NEED_RTNL,
8921 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008922 {
8923 .cmd = NL80211_CMD_TDLS_MGMT,
8924 .doit = nl80211_tdls_mgmt,
8925 .policy = nl80211_policy,
8926 .flags = GENL_ADMIN_PERM,
8927 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8928 NL80211_FLAG_NEED_RTNL,
8929 },
8930 {
8931 .cmd = NL80211_CMD_TDLS_OPER,
8932 .doit = nl80211_tdls_oper,
8933 .policy = nl80211_policy,
8934 .flags = GENL_ADMIN_PERM,
8935 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8936 NL80211_FLAG_NEED_RTNL,
8937 },
Johannes Berg28946da2011-11-04 11:18:12 +01008938 {
8939 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8940 .doit = nl80211_register_unexpected_frame,
8941 .policy = nl80211_policy,
8942 .flags = GENL_ADMIN_PERM,
8943 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8944 NL80211_FLAG_NEED_RTNL,
8945 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008946 {
8947 .cmd = NL80211_CMD_PROBE_CLIENT,
8948 .doit = nl80211_probe_client,
8949 .policy = nl80211_policy,
8950 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008951 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008952 NL80211_FLAG_NEED_RTNL,
8953 },
Johannes Berg5e760232011-11-04 11:18:17 +01008954 {
8955 .cmd = NL80211_CMD_REGISTER_BEACONS,
8956 .doit = nl80211_register_beacons,
8957 .policy = nl80211_policy,
8958 .flags = GENL_ADMIN_PERM,
8959 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8960 NL80211_FLAG_NEED_RTNL,
8961 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008962 {
8963 .cmd = NL80211_CMD_SET_NOACK_MAP,
8964 .doit = nl80211_set_noack_map,
8965 .policy = nl80211_policy,
8966 .flags = GENL_ADMIN_PERM,
8967 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8968 NL80211_FLAG_NEED_RTNL,
8969 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008970 {
8971 .cmd = NL80211_CMD_START_P2P_DEVICE,
8972 .doit = nl80211_start_p2p_device,
8973 .policy = nl80211_policy,
8974 .flags = GENL_ADMIN_PERM,
8975 .internal_flags = NL80211_FLAG_NEED_WDEV |
8976 NL80211_FLAG_NEED_RTNL,
8977 },
8978 {
8979 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8980 .doit = nl80211_stop_p2p_device,
8981 .policy = nl80211_policy,
8982 .flags = GENL_ADMIN_PERM,
8983 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8984 NL80211_FLAG_NEED_RTNL,
8985 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008986 {
8987 .cmd = NL80211_CMD_SET_MCAST_RATE,
8988 .doit = nl80211_set_mcast_rate,
8989 .policy = nl80211_policy,
8990 .flags = GENL_ADMIN_PERM,
8991 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8992 NL80211_FLAG_NEED_RTNL,
8993 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308994 {
8995 .cmd = NL80211_CMD_SET_MAC_ACL,
8996 .doit = nl80211_set_mac_acl,
8997 .policy = nl80211_policy,
8998 .flags = GENL_ADMIN_PERM,
8999 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9000 NL80211_FLAG_NEED_RTNL,
9001 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009002 {
9003 .cmd = NL80211_CMD_RADAR_DETECT,
9004 .doit = nl80211_start_radar_detection,
9005 .policy = nl80211_policy,
9006 .flags = GENL_ADMIN_PERM,
9007 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9008 NL80211_FLAG_NEED_RTNL,
9009 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009010 {
9011 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9012 .doit = nl80211_get_protocol_features,
9013 .policy = nl80211_policy,
9014 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009015 {
9016 .cmd = NL80211_CMD_UPDATE_FT_IES,
9017 .doit = nl80211_update_ft_ies,
9018 .policy = nl80211_policy,
9019 .flags = GENL_ADMIN_PERM,
9020 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9021 NL80211_FLAG_NEED_RTNL,
9022 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009023 {
9024 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9025 .doit = nl80211_crit_protocol_start,
9026 .policy = nl80211_policy,
9027 .flags = GENL_ADMIN_PERM,
9028 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9029 NL80211_FLAG_NEED_RTNL,
9030 },
9031 {
9032 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9033 .doit = nl80211_crit_protocol_stop,
9034 .policy = nl80211_policy,
9035 .flags = GENL_ADMIN_PERM,
9036 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9037 NL80211_FLAG_NEED_RTNL,
9038 }
Johannes Berg55682962007-09-20 13:09:35 -04009039};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009040
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009041static struct genl_multicast_group nl80211_mlme_mcgrp = {
9042 .name = "mlme",
9043};
Johannes Berg55682962007-09-20 13:09:35 -04009044
9045/* multicast groups */
9046static struct genl_multicast_group nl80211_config_mcgrp = {
9047 .name = "config",
9048};
Johannes Berg2a519312009-02-10 21:25:55 +01009049static struct genl_multicast_group nl80211_scan_mcgrp = {
9050 .name = "scan",
9051};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009052static struct genl_multicast_group nl80211_regulatory_mcgrp = {
9053 .name = "regulatory",
9054};
Johannes Berg55682962007-09-20 13:09:35 -04009055
9056/* notification functions */
9057
9058void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9059{
9060 struct sk_buff *msg;
9061
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009062 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009063 if (!msg)
9064 return;
9065
Johannes Berg3713b4e2013-02-14 16:19:38 +01009066 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
9067 false, NULL, NULL, NULL) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04009068 nlmsg_free(msg);
9069 return;
9070 }
9071
Johannes Berg463d0182009-07-14 00:33:35 +02009072 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9073 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009074}
9075
Johannes Berg362a4152009-05-24 16:43:15 +02009076static int nl80211_add_scan_req(struct sk_buff *msg,
9077 struct cfg80211_registered_device *rdev)
9078{
9079 struct cfg80211_scan_request *req = rdev->scan_req;
9080 struct nlattr *nest;
9081 int i;
9082
Johannes Bergf9f47522013-03-19 15:04:07 +01009083 lockdep_assert_held(&rdev->sched_scan_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +02009084
Johannes Berg362a4152009-05-24 16:43:15 +02009085 if (WARN_ON(!req))
9086 return 0;
9087
9088 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9089 if (!nest)
9090 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009091 for (i = 0; i < req->n_ssids; i++) {
9092 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9093 goto nla_put_failure;
9094 }
Johannes Berg362a4152009-05-24 16:43:15 +02009095 nla_nest_end(msg, nest);
9096
9097 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9098 if (!nest)
9099 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009100 for (i = 0; i < req->n_channels; i++) {
9101 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9102 goto nla_put_failure;
9103 }
Johannes Berg362a4152009-05-24 16:43:15 +02009104 nla_nest_end(msg, nest);
9105
David S. Miller9360ffd2012-03-29 04:41:26 -04009106 if (req->ie &&
9107 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9108 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02009109
Sam Lefflered4737712012-10-11 21:03:31 -07009110 if (req->flags)
9111 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
9112
Johannes Berg362a4152009-05-24 16:43:15 +02009113 return 0;
9114 nla_put_failure:
9115 return -ENOBUFS;
9116}
9117
Johannes Berga538e2d2009-06-16 19:56:42 +02009118static int nl80211_send_scan_msg(struct sk_buff *msg,
9119 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009120 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009121 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02009122 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01009123{
9124 void *hdr;
9125
Eric W. Biederman15e47302012-09-07 20:12:54 +00009126 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01009127 if (!hdr)
9128 return -1;
9129
David S. Miller9360ffd2012-03-29 04:41:26 -04009130 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009131 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9132 wdev->netdev->ifindex)) ||
9133 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009134 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009135
Johannes Berg362a4152009-05-24 16:43:15 +02009136 /* ignore errors and send incomplete event anyway */
9137 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009138
9139 return genlmsg_end(msg, hdr);
9140
9141 nla_put_failure:
9142 genlmsg_cancel(msg, hdr);
9143 return -EMSGSIZE;
9144}
9145
Luciano Coelho807f8a82011-05-11 17:09:35 +03009146static int
9147nl80211_send_sched_scan_msg(struct sk_buff *msg,
9148 struct cfg80211_registered_device *rdev,
9149 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009150 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009151{
9152 void *hdr;
9153
Eric W. Biederman15e47302012-09-07 20:12:54 +00009154 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009155 if (!hdr)
9156 return -1;
9157
David S. Miller9360ffd2012-03-29 04:41:26 -04009158 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9159 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9160 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009161
9162 return genlmsg_end(msg, hdr);
9163
9164 nla_put_failure:
9165 genlmsg_cancel(msg, hdr);
9166 return -EMSGSIZE;
9167}
9168
Johannes Berga538e2d2009-06-16 19:56:42 +02009169void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009170 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009171{
9172 struct sk_buff *msg;
9173
Thomas Graf58050fc2012-06-28 03:57:45 +00009174 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009175 if (!msg)
9176 return;
9177
Johannes Bergfd014282012-06-18 19:17:03 +02009178 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009179 NL80211_CMD_TRIGGER_SCAN) < 0) {
9180 nlmsg_free(msg);
9181 return;
9182 }
9183
Johannes Berg463d0182009-07-14 00:33:35 +02009184 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9185 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009186}
9187
Johannes Berg2a519312009-02-10 21:25:55 +01009188void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009189 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009190{
9191 struct sk_buff *msg;
9192
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009193 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009194 if (!msg)
9195 return;
9196
Johannes Bergfd014282012-06-18 19:17:03 +02009197 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009198 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009199 nlmsg_free(msg);
9200 return;
9201 }
9202
Johannes Berg463d0182009-07-14 00:33:35 +02009203 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9204 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009205}
9206
9207void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009208 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009209{
9210 struct sk_buff *msg;
9211
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009212 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009213 if (!msg)
9214 return;
9215
Johannes Bergfd014282012-06-18 19:17:03 +02009216 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009217 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009218 nlmsg_free(msg);
9219 return;
9220 }
9221
Johannes Berg463d0182009-07-14 00:33:35 +02009222 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9223 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009224}
9225
Luciano Coelho807f8a82011-05-11 17:09:35 +03009226void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9227 struct net_device *netdev)
9228{
9229 struct sk_buff *msg;
9230
9231 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9232 if (!msg)
9233 return;
9234
9235 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9236 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9237 nlmsg_free(msg);
9238 return;
9239 }
9240
9241 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9242 nl80211_scan_mcgrp.id, GFP_KERNEL);
9243}
9244
9245void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9246 struct net_device *netdev, u32 cmd)
9247{
9248 struct sk_buff *msg;
9249
Thomas Graf58050fc2012-06-28 03:57:45 +00009250 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009251 if (!msg)
9252 return;
9253
9254 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9255 nlmsg_free(msg);
9256 return;
9257 }
9258
9259 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9260 nl80211_scan_mcgrp.id, GFP_KERNEL);
9261}
9262
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009263/*
9264 * This can happen on global regulatory changes or device specific settings
9265 * based on custom world regulatory domains.
9266 */
9267void nl80211_send_reg_change_event(struct regulatory_request *request)
9268{
9269 struct sk_buff *msg;
9270 void *hdr;
9271
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009272 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009273 if (!msg)
9274 return;
9275
9276 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9277 if (!hdr) {
9278 nlmsg_free(msg);
9279 return;
9280 }
9281
9282 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04009283 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9284 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009285
David S. Miller9360ffd2012-03-29 04:41:26 -04009286 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9287 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9288 NL80211_REGDOM_TYPE_WORLD))
9289 goto nla_put_failure;
9290 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9291 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9292 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9293 goto nla_put_failure;
9294 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9295 request->intersect) {
9296 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9297 NL80211_REGDOM_TYPE_INTERSECTION))
9298 goto nla_put_failure;
9299 } else {
9300 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9301 NL80211_REGDOM_TYPE_COUNTRY) ||
9302 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9303 request->alpha2))
9304 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009305 }
9306
Johannes Bergf4173762012-12-03 18:23:37 +01009307 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04009308 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9309 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009310
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009311 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009312
Johannes Bergbc43b282009-07-25 10:54:13 +02009313 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009314 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009315 GFP_ATOMIC);
9316 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009317
9318 return;
9319
9320nla_put_failure:
9321 genlmsg_cancel(msg, hdr);
9322 nlmsg_free(msg);
9323}
9324
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009325static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9326 struct net_device *netdev,
9327 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009328 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009329{
9330 struct sk_buff *msg;
9331 void *hdr;
9332
Johannes Berge6d6e342009-07-01 21:26:47 +02009333 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009334 if (!msg)
9335 return;
9336
9337 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9338 if (!hdr) {
9339 nlmsg_free(msg);
9340 return;
9341 }
9342
David S. Miller9360ffd2012-03-29 04:41:26 -04009343 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9344 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9345 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9346 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009347
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009348 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009349
Johannes Berg463d0182009-07-14 00:33:35 +02009350 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9351 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009352 return;
9353
9354 nla_put_failure:
9355 genlmsg_cancel(msg, hdr);
9356 nlmsg_free(msg);
9357}
9358
9359void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009360 struct net_device *netdev, const u8 *buf,
9361 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009362{
9363 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009364 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009365}
9366
9367void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9368 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009369 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009370{
Johannes Berge6d6e342009-07-01 21:26:47 +02009371 nl80211_send_mlme_event(rdev, netdev, buf, len,
9372 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009373}
9374
Jouni Malinen53b46b82009-03-27 20:53:56 +02009375void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009376 struct net_device *netdev, const u8 *buf,
9377 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009378{
9379 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009380 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009381}
9382
Jouni Malinen53b46b82009-03-27 20:53:56 +02009383void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9384 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009385 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009386{
9387 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009388 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009389}
9390
Johannes Berg947add32013-02-22 22:05:20 +01009391void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
9392 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +02009393{
Johannes Berg947add32013-02-22 22:05:20 +01009394 struct wireless_dev *wdev = dev->ieee80211_ptr;
9395 struct wiphy *wiphy = wdev->wiphy;
9396 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009397
Johannes Berg947add32013-02-22 22:05:20 +01009398 trace_cfg80211_send_unprot_deauth(dev);
9399 nl80211_send_mlme_event(rdev, dev, buf, len,
9400 NL80211_CMD_UNPROT_DEAUTHENTICATE, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009401}
Johannes Berg947add32013-02-22 22:05:20 +01009402EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
9403
9404void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
9405 size_t len)
9406{
9407 struct wireless_dev *wdev = dev->ieee80211_ptr;
9408 struct wiphy *wiphy = wdev->wiphy;
9409 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9410
9411 trace_cfg80211_send_unprot_disassoc(dev);
9412 nl80211_send_mlme_event(rdev, dev, buf, len,
9413 NL80211_CMD_UNPROT_DISASSOCIATE, GFP_ATOMIC);
9414}
9415EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009416
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04009417static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9418 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02009419 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009420{
9421 struct sk_buff *msg;
9422 void *hdr;
9423
Johannes Berge6d6e342009-07-01 21:26:47 +02009424 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009425 if (!msg)
9426 return;
9427
9428 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9429 if (!hdr) {
9430 nlmsg_free(msg);
9431 return;
9432 }
9433
David S. Miller9360ffd2012-03-29 04:41:26 -04009434 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9435 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9436 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9437 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9438 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03009439
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009440 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03009441
Johannes Berg463d0182009-07-14 00:33:35 +02009442 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9443 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009444 return;
9445
9446 nla_put_failure:
9447 genlmsg_cancel(msg, hdr);
9448 nlmsg_free(msg);
9449}
9450
9451void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009452 struct net_device *netdev, const u8 *addr,
9453 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009454{
9455 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02009456 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009457}
9458
9459void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009460 struct net_device *netdev, const u8 *addr,
9461 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009462{
Johannes Berge6d6e342009-07-01 21:26:47 +02009463 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9464 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009465}
9466
Samuel Ortizb23aa672009-07-01 21:26:54 +02009467void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9468 struct net_device *netdev, const u8 *bssid,
9469 const u8 *req_ie, size_t req_ie_len,
9470 const u8 *resp_ie, size_t resp_ie_len,
9471 u16 status, gfp_t gfp)
9472{
9473 struct sk_buff *msg;
9474 void *hdr;
9475
Thomas Graf58050fc2012-06-28 03:57:45 +00009476 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009477 if (!msg)
9478 return;
9479
9480 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9481 if (!hdr) {
9482 nlmsg_free(msg);
9483 return;
9484 }
9485
David S. Miller9360ffd2012-03-29 04:41:26 -04009486 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9487 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9488 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9489 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9490 (req_ie &&
9491 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9492 (resp_ie &&
9493 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9494 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009495
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009496 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009497
Johannes Berg463d0182009-07-14 00:33:35 +02009498 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9499 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009500 return;
9501
9502 nla_put_failure:
9503 genlmsg_cancel(msg, hdr);
9504 nlmsg_free(msg);
9505
9506}
9507
9508void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9509 struct net_device *netdev, const u8 *bssid,
9510 const u8 *req_ie, size_t req_ie_len,
9511 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9512{
9513 struct sk_buff *msg;
9514 void *hdr;
9515
Thomas Graf58050fc2012-06-28 03:57:45 +00009516 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009517 if (!msg)
9518 return;
9519
9520 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9521 if (!hdr) {
9522 nlmsg_free(msg);
9523 return;
9524 }
9525
David S. Miller9360ffd2012-03-29 04:41:26 -04009526 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9527 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9528 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9529 (req_ie &&
9530 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9531 (resp_ie &&
9532 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9533 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009534
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009535 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009536
Johannes Berg463d0182009-07-14 00:33:35 +02009537 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9538 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009539 return;
9540
9541 nla_put_failure:
9542 genlmsg_cancel(msg, hdr);
9543 nlmsg_free(msg);
9544
9545}
9546
9547void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9548 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02009549 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009550{
9551 struct sk_buff *msg;
9552 void *hdr;
9553
Thomas Graf58050fc2012-06-28 03:57:45 +00009554 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009555 if (!msg)
9556 return;
9557
9558 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9559 if (!hdr) {
9560 nlmsg_free(msg);
9561 return;
9562 }
9563
David S. Miller9360ffd2012-03-29 04:41:26 -04009564 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9565 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9566 (from_ap && reason &&
9567 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9568 (from_ap &&
9569 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9570 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9571 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009572
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009573 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009574
Johannes Berg463d0182009-07-14 00:33:35 +02009575 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9576 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009577 return;
9578
9579 nla_put_failure:
9580 genlmsg_cancel(msg, hdr);
9581 nlmsg_free(msg);
9582
9583}
9584
Johannes Berg04a773a2009-04-19 21:24:32 +02009585void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9586 struct net_device *netdev, const u8 *bssid,
9587 gfp_t gfp)
9588{
9589 struct sk_buff *msg;
9590 void *hdr;
9591
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009592 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009593 if (!msg)
9594 return;
9595
9596 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9597 if (!hdr) {
9598 nlmsg_free(msg);
9599 return;
9600 }
9601
David S. Miller9360ffd2012-03-29 04:41:26 -04009602 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9603 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9604 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9605 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009606
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009607 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009608
Johannes Berg463d0182009-07-14 00:33:35 +02009609 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9610 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009611 return;
9612
9613 nla_put_failure:
9614 genlmsg_cancel(msg, hdr);
9615 nlmsg_free(msg);
9616}
9617
Johannes Berg947add32013-02-22 22:05:20 +01009618void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
9619 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -07009620{
Johannes Berg947add32013-02-22 22:05:20 +01009621 struct wireless_dev *wdev = dev->ieee80211_ptr;
9622 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009623 struct sk_buff *msg;
9624 void *hdr;
9625
Johannes Berg947add32013-02-22 22:05:20 +01009626 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
9627 return;
9628
9629 trace_cfg80211_notify_new_peer_candidate(dev, addr);
9630
Javier Cardonac93b5e72011-04-07 15:08:34 -07009631 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9632 if (!msg)
9633 return;
9634
9635 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9636 if (!hdr) {
9637 nlmsg_free(msg);
9638 return;
9639 }
9640
David S. Miller9360ffd2012-03-29 04:41:26 -04009641 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +01009642 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9643 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009644 (ie_len && ie &&
9645 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9646 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009647
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009648 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009649
9650 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9651 nl80211_mlme_mcgrp.id, gfp);
9652 return;
9653
9654 nla_put_failure:
9655 genlmsg_cancel(msg, hdr);
9656 nlmsg_free(msg);
9657}
Johannes Berg947add32013-02-22 22:05:20 +01009658EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009659
Jouni Malinena3b8b052009-03-27 21:59:49 +02009660void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9661 struct net_device *netdev, const u8 *addr,
9662 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009663 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009664{
9665 struct sk_buff *msg;
9666 void *hdr;
9667
Johannes Berge6d6e342009-07-01 21:26:47 +02009668 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009669 if (!msg)
9670 return;
9671
9672 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9673 if (!hdr) {
9674 nlmsg_free(msg);
9675 return;
9676 }
9677
David S. Miller9360ffd2012-03-29 04:41:26 -04009678 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9679 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9680 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9681 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9682 (key_id != -1 &&
9683 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9684 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9685 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009686
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009687 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009688
Johannes Berg463d0182009-07-14 00:33:35 +02009689 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9690 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009691 return;
9692
9693 nla_put_failure:
9694 genlmsg_cancel(msg, hdr);
9695 nlmsg_free(msg);
9696}
9697
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009698void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9699 struct ieee80211_channel *channel_before,
9700 struct ieee80211_channel *channel_after)
9701{
9702 struct sk_buff *msg;
9703 void *hdr;
9704 struct nlattr *nl_freq;
9705
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009706 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009707 if (!msg)
9708 return;
9709
9710 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9711 if (!hdr) {
9712 nlmsg_free(msg);
9713 return;
9714 }
9715
9716 /*
9717 * Since we are applying the beacon hint to a wiphy we know its
9718 * wiphy_idx is valid
9719 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009720 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9721 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009722
9723 /* Before */
9724 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9725 if (!nl_freq)
9726 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009727 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009728 goto nla_put_failure;
9729 nla_nest_end(msg, nl_freq);
9730
9731 /* After */
9732 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9733 if (!nl_freq)
9734 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009735 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009736 goto nla_put_failure;
9737 nla_nest_end(msg, nl_freq);
9738
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009739 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009740
Johannes Berg463d0182009-07-14 00:33:35 +02009741 rcu_read_lock();
9742 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9743 GFP_ATOMIC);
9744 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009745
9746 return;
9747
9748nla_put_failure:
9749 genlmsg_cancel(msg, hdr);
9750 nlmsg_free(msg);
9751}
9752
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009753static void nl80211_send_remain_on_chan_event(
9754 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009755 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009756 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009757 unsigned int duration, gfp_t gfp)
9758{
9759 struct sk_buff *msg;
9760 void *hdr;
9761
9762 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9763 if (!msg)
9764 return;
9765
9766 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9767 if (!hdr) {
9768 nlmsg_free(msg);
9769 return;
9770 }
9771
David S. Miller9360ffd2012-03-29 04:41:26 -04009772 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009773 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9774 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009775 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009776 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009777 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9778 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009779 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9780 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009781
David S. Miller9360ffd2012-03-29 04:41:26 -04009782 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9783 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9784 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009785
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009786 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009787
9788 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9789 nl80211_mlme_mcgrp.id, gfp);
9790 return;
9791
9792 nla_put_failure:
9793 genlmsg_cancel(msg, hdr);
9794 nlmsg_free(msg);
9795}
9796
Johannes Berg947add32013-02-22 22:05:20 +01009797void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9798 struct ieee80211_channel *chan,
9799 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009800{
Johannes Berg947add32013-02-22 22:05:20 +01009801 struct wiphy *wiphy = wdev->wiphy;
9802 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9803
9804 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009805 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009806 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009807 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009808}
Johannes Berg947add32013-02-22 22:05:20 +01009809EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009810
Johannes Berg947add32013-02-22 22:05:20 +01009811void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9812 struct ieee80211_channel *chan,
9813 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009814{
Johannes Berg947add32013-02-22 22:05:20 +01009815 struct wiphy *wiphy = wdev->wiphy;
9816 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9817
9818 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009819 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009820 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009821}
Johannes Berg947add32013-02-22 22:05:20 +01009822EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009823
Johannes Berg947add32013-02-22 22:05:20 +01009824void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
9825 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +01009826{
Johannes Berg947add32013-02-22 22:05:20 +01009827 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9828 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +01009829 struct sk_buff *msg;
9830
Johannes Berg947add32013-02-22 22:05:20 +01009831 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
9832
Thomas Graf58050fc2012-06-28 03:57:45 +00009833 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009834 if (!msg)
9835 return;
9836
John W. Linville66266b32012-03-15 13:25:41 -04009837 if (nl80211_send_station(msg, 0, 0, 0,
9838 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009839 nlmsg_free(msg);
9840 return;
9841 }
9842
9843 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9844 nl80211_mlme_mcgrp.id, gfp);
9845}
Johannes Berg947add32013-02-22 22:05:20 +01009846EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +01009847
Johannes Berg947add32013-02-22 22:05:20 +01009848void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +02009849{
Johannes Berg947add32013-02-22 22:05:20 +01009850 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9851 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +02009852 struct sk_buff *msg;
9853 void *hdr;
9854
Johannes Berg947add32013-02-22 22:05:20 +01009855 trace_cfg80211_del_sta(dev, mac_addr);
9856
Thomas Graf58050fc2012-06-28 03:57:45 +00009857 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009858 if (!msg)
9859 return;
9860
9861 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9862 if (!hdr) {
9863 nlmsg_free(msg);
9864 return;
9865 }
9866
David S. Miller9360ffd2012-03-29 04:41:26 -04009867 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9868 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9869 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009870
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009871 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009872
9873 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9874 nl80211_mlme_mcgrp.id, gfp);
9875 return;
9876
9877 nla_put_failure:
9878 genlmsg_cancel(msg, hdr);
9879 nlmsg_free(msg);
9880}
Johannes Berg947add32013-02-22 22:05:20 +01009881EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +02009882
Johannes Berg947add32013-02-22 22:05:20 +01009883void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
9884 enum nl80211_connect_failed_reason reason,
9885 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309886{
Johannes Berg947add32013-02-22 22:05:20 +01009887 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9888 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309889 struct sk_buff *msg;
9890 void *hdr;
9891
9892 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9893 if (!msg)
9894 return;
9895
9896 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9897 if (!hdr) {
9898 nlmsg_free(msg);
9899 return;
9900 }
9901
9902 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9903 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9904 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9905 goto nla_put_failure;
9906
9907 genlmsg_end(msg, hdr);
9908
9909 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9910 nl80211_mlme_mcgrp.id, gfp);
9911 return;
9912
9913 nla_put_failure:
9914 genlmsg_cancel(msg, hdr);
9915 nlmsg_free(msg);
9916}
Johannes Berg947add32013-02-22 22:05:20 +01009917EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309918
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009919static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9920 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009921{
9922 struct wireless_dev *wdev = dev->ieee80211_ptr;
9923 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9924 struct sk_buff *msg;
9925 void *hdr;
9926 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009927 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009928
Eric W. Biederman15e47302012-09-07 20:12:54 +00009929 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009930 return false;
9931
9932 msg = nlmsg_new(100, gfp);
9933 if (!msg)
9934 return true;
9935
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009936 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009937 if (!hdr) {
9938 nlmsg_free(msg);
9939 return true;
9940 }
9941
David S. Miller9360ffd2012-03-29 04:41:26 -04009942 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9943 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9944 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9945 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009946
9947 err = genlmsg_end(msg, hdr);
9948 if (err < 0) {
9949 nlmsg_free(msg);
9950 return true;
9951 }
9952
Eric W. Biederman15e47302012-09-07 20:12:54 +00009953 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009954 return true;
9955
9956 nla_put_failure:
9957 genlmsg_cancel(msg, hdr);
9958 nlmsg_free(msg);
9959 return true;
9960}
9961
Johannes Berg947add32013-02-22 22:05:20 +01009962bool cfg80211_rx_spurious_frame(struct net_device *dev,
9963 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009964{
Johannes Berg947add32013-02-22 22:05:20 +01009965 struct wireless_dev *wdev = dev->ieee80211_ptr;
9966 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009967
Johannes Berg947add32013-02-22 22:05:20 +01009968 trace_cfg80211_rx_spurious_frame(dev, addr);
9969
9970 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9971 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
9972 trace_cfg80211_return_bool(false);
9973 return false;
9974 }
9975 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9976 addr, gfp);
9977 trace_cfg80211_return_bool(ret);
9978 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009979}
Johannes Berg947add32013-02-22 22:05:20 +01009980EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
9981
9982bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
9983 const u8 *addr, gfp_t gfp)
9984{
9985 struct wireless_dev *wdev = dev->ieee80211_ptr;
9986 bool ret;
9987
9988 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
9989
9990 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9991 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
9992 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
9993 trace_cfg80211_return_bool(false);
9994 return false;
9995 }
9996 ret = __nl80211_unexpected_frame(dev,
9997 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9998 addr, gfp);
9999 trace_cfg80211_return_bool(ret);
10000 return ret;
10001}
10002EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010003
Johannes Berg2e161f72010-08-12 15:38:38 +020010004int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010005 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010006 int freq, int sig_dbm,
10007 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010008{
Johannes Berg71bbc992012-06-15 15:30:18 +020010009 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010010 struct sk_buff *msg;
10011 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010012
10013 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10014 if (!msg)
10015 return -ENOMEM;
10016
Johannes Berg2e161f72010-08-12 15:38:38 +020010017 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010018 if (!hdr) {
10019 nlmsg_free(msg);
10020 return -ENOMEM;
10021 }
10022
David S. Miller9360ffd2012-03-29 04:41:26 -040010023 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010024 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10025 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010026 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10027 (sig_dbm &&
10028 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10029 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10030 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010031
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010032 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010033
Eric W. Biederman15e47302012-09-07 20:12:54 +000010034 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010035
10036 nla_put_failure:
10037 genlmsg_cancel(msg, hdr);
10038 nlmsg_free(msg);
10039 return -ENOBUFS;
10040}
10041
Johannes Berg947add32013-02-22 22:05:20 +010010042void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10043 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010044{
Johannes Berg947add32013-02-22 22:05:20 +010010045 struct wiphy *wiphy = wdev->wiphy;
10046 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010047 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010048 struct sk_buff *msg;
10049 void *hdr;
10050
Johannes Berg947add32013-02-22 22:05:20 +010010051 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10052
Jouni Malinen026331c2010-02-15 12:53:10 +020010053 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10054 if (!msg)
10055 return;
10056
Johannes Berg2e161f72010-08-12 15:38:38 +020010057 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010058 if (!hdr) {
10059 nlmsg_free(msg);
10060 return;
10061 }
10062
David S. Miller9360ffd2012-03-29 04:41:26 -040010063 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010064 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10065 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010066 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10067 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10068 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10069 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010070
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010071 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010072
10073 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
10074 return;
10075
10076 nla_put_failure:
10077 genlmsg_cancel(msg, hdr);
10078 nlmsg_free(msg);
10079}
Johannes Berg947add32013-02-22 22:05:20 +010010080EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020010081
Johannes Berg947add32013-02-22 22:05:20 +010010082void cfg80211_cqm_rssi_notify(struct net_device *dev,
10083 enum nl80211_cqm_rssi_threshold_event rssi_event,
10084 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010085{
Johannes Berg947add32013-02-22 22:05:20 +010010086 struct wireless_dev *wdev = dev->ieee80211_ptr;
10087 struct wiphy *wiphy = wdev->wiphy;
10088 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010089 struct sk_buff *msg;
10090 struct nlattr *pinfoattr;
10091 void *hdr;
10092
Johannes Berg947add32013-02-22 22:05:20 +010010093 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10094
Thomas Graf58050fc2012-06-28 03:57:45 +000010095 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010096 if (!msg)
10097 return;
10098
10099 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10100 if (!hdr) {
10101 nlmsg_free(msg);
10102 return;
10103 }
10104
David S. Miller9360ffd2012-03-29 04:41:26 -040010105 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010106 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040010107 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010108
10109 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10110 if (!pinfoattr)
10111 goto nla_put_failure;
10112
David S. Miller9360ffd2012-03-29 04:41:26 -040010113 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10114 rssi_event))
10115 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010116
10117 nla_nest_end(msg, pinfoattr);
10118
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010119 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010120
10121 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10122 nl80211_mlme_mcgrp.id, gfp);
10123 return;
10124
10125 nla_put_failure:
10126 genlmsg_cancel(msg, hdr);
10127 nlmsg_free(msg);
10128}
Johannes Berg947add32013-02-22 22:05:20 +010010129EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010130
Johannes Berg947add32013-02-22 22:05:20 +010010131static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10132 struct net_device *netdev, const u8 *bssid,
10133 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010134{
10135 struct sk_buff *msg;
10136 struct nlattr *rekey_attr;
10137 void *hdr;
10138
Thomas Graf58050fc2012-06-28 03:57:45 +000010139 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010140 if (!msg)
10141 return;
10142
10143 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10144 if (!hdr) {
10145 nlmsg_free(msg);
10146 return;
10147 }
10148
David S. Miller9360ffd2012-03-29 04:41:26 -040010149 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10150 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10151 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10152 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010153
10154 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10155 if (!rekey_attr)
10156 goto nla_put_failure;
10157
David S. Miller9360ffd2012-03-29 04:41:26 -040010158 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10159 NL80211_REPLAY_CTR_LEN, replay_ctr))
10160 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010161
10162 nla_nest_end(msg, rekey_attr);
10163
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010164 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010165
10166 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10167 nl80211_mlme_mcgrp.id, gfp);
10168 return;
10169
10170 nla_put_failure:
10171 genlmsg_cancel(msg, hdr);
10172 nlmsg_free(msg);
10173}
10174
Johannes Berg947add32013-02-22 22:05:20 +010010175void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10176 const u8 *replay_ctr, gfp_t gfp)
10177{
10178 struct wireless_dev *wdev = dev->ieee80211_ptr;
10179 struct wiphy *wiphy = wdev->wiphy;
10180 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10181
10182 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10183 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10184}
10185EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10186
10187static void
10188nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10189 struct net_device *netdev, int index,
10190 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010191{
10192 struct sk_buff *msg;
10193 struct nlattr *attr;
10194 void *hdr;
10195
Thomas Graf58050fc2012-06-28 03:57:45 +000010196 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010197 if (!msg)
10198 return;
10199
10200 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10201 if (!hdr) {
10202 nlmsg_free(msg);
10203 return;
10204 }
10205
David S. Miller9360ffd2012-03-29 04:41:26 -040010206 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10207 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10208 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010209
10210 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10211 if (!attr)
10212 goto nla_put_failure;
10213
David S. Miller9360ffd2012-03-29 04:41:26 -040010214 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10215 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10216 (preauth &&
10217 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10218 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010219
10220 nla_nest_end(msg, attr);
10221
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010222 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010223
10224 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10225 nl80211_mlme_mcgrp.id, gfp);
10226 return;
10227
10228 nla_put_failure:
10229 genlmsg_cancel(msg, hdr);
10230 nlmsg_free(msg);
10231}
10232
Johannes Berg947add32013-02-22 22:05:20 +010010233void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10234 const u8 *bssid, bool preauth, gfp_t gfp)
10235{
10236 struct wireless_dev *wdev = dev->ieee80211_ptr;
10237 struct wiphy *wiphy = wdev->wiphy;
10238 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10239
10240 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10241 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10242}
10243EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10244
10245static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10246 struct net_device *netdev,
10247 struct cfg80211_chan_def *chandef,
10248 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070010249{
10250 struct sk_buff *msg;
10251 void *hdr;
10252
Thomas Graf58050fc2012-06-28 03:57:45 +000010253 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070010254 if (!msg)
10255 return;
10256
10257 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10258 if (!hdr) {
10259 nlmsg_free(msg);
10260 return;
10261 }
10262
Johannes Berg683b6d32012-11-08 21:25:48 +010010263 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10264 goto nla_put_failure;
10265
10266 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040010267 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070010268
10269 genlmsg_end(msg, hdr);
10270
10271 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10272 nl80211_mlme_mcgrp.id, gfp);
10273 return;
10274
10275 nla_put_failure:
10276 genlmsg_cancel(msg, hdr);
10277 nlmsg_free(msg);
10278}
10279
Johannes Berg947add32013-02-22 22:05:20 +010010280void cfg80211_ch_switch_notify(struct net_device *dev,
10281 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070010282{
Johannes Berg947add32013-02-22 22:05:20 +010010283 struct wireless_dev *wdev = dev->ieee80211_ptr;
10284 struct wiphy *wiphy = wdev->wiphy;
10285 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10286
10287 trace_cfg80211_ch_switch_notify(dev, chandef);
10288
10289 wdev_lock(wdev);
10290
10291 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10292 wdev->iftype != NL80211_IFTYPE_P2P_GO))
10293 goto out;
10294
10295 wdev->channel = chandef->chan;
10296 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
10297out:
10298 wdev_unlock(wdev);
10299 return;
10300}
10301EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10302
10303void cfg80211_cqm_txe_notify(struct net_device *dev,
10304 const u8 *peer, u32 num_packets,
10305 u32 rate, u32 intvl, gfp_t gfp)
10306{
10307 struct wireless_dev *wdev = dev->ieee80211_ptr;
10308 struct wiphy *wiphy = wdev->wiphy;
10309 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010310 struct sk_buff *msg;
10311 struct nlattr *pinfoattr;
10312 void *hdr;
10313
10314 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10315 if (!msg)
10316 return;
10317
10318 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10319 if (!hdr) {
10320 nlmsg_free(msg);
10321 return;
10322 }
10323
10324 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010325 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070010326 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10327 goto nla_put_failure;
10328
10329 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10330 if (!pinfoattr)
10331 goto nla_put_failure;
10332
10333 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10334 goto nla_put_failure;
10335
10336 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10337 goto nla_put_failure;
10338
10339 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10340 goto nla_put_failure;
10341
10342 nla_nest_end(msg, pinfoattr);
10343
10344 genlmsg_end(msg, hdr);
10345
10346 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10347 nl80211_mlme_mcgrp.id, gfp);
10348 return;
10349
10350 nla_put_failure:
10351 genlmsg_cancel(msg, hdr);
10352 nlmsg_free(msg);
10353}
Johannes Berg947add32013-02-22 22:05:20 +010010354EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010355
10356void
Simon Wunderlich04f39042013-02-08 18:16:19 +010010357nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10358 struct cfg80211_chan_def *chandef,
10359 enum nl80211_radar_event event,
10360 struct net_device *netdev, gfp_t gfp)
10361{
10362 struct sk_buff *msg;
10363 void *hdr;
10364
10365 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10366 if (!msg)
10367 return;
10368
10369 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
10370 if (!hdr) {
10371 nlmsg_free(msg);
10372 return;
10373 }
10374
10375 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10376 goto nla_put_failure;
10377
10378 /* NOP and radar events don't need a netdev parameter */
10379 if (netdev) {
10380 struct wireless_dev *wdev = netdev->ieee80211_ptr;
10381
10382 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10383 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10384 goto nla_put_failure;
10385 }
10386
10387 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
10388 goto nla_put_failure;
10389
10390 if (nl80211_send_chandef(msg, chandef))
10391 goto nla_put_failure;
10392
10393 if (genlmsg_end(msg, hdr) < 0) {
10394 nlmsg_free(msg);
10395 return;
10396 }
10397
10398 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10399 nl80211_mlme_mcgrp.id, gfp);
10400 return;
10401
10402 nla_put_failure:
10403 genlmsg_cancel(msg, hdr);
10404 nlmsg_free(msg);
10405}
10406
Johannes Berg947add32013-02-22 22:05:20 +010010407void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10408 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010010409{
Johannes Berg947add32013-02-22 22:05:20 +010010410 struct wireless_dev *wdev = dev->ieee80211_ptr;
10411 struct wiphy *wiphy = wdev->wiphy;
10412 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010413 struct sk_buff *msg;
10414 struct nlattr *pinfoattr;
10415 void *hdr;
10416
Johannes Berg947add32013-02-22 22:05:20 +010010417 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
10418
Thomas Graf58050fc2012-06-28 03:57:45 +000010419 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010420 if (!msg)
10421 return;
10422
10423 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10424 if (!hdr) {
10425 nlmsg_free(msg);
10426 return;
10427 }
10428
David S. Miller9360ffd2012-03-29 04:41:26 -040010429 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010430 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010431 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10432 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010433
10434 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10435 if (!pinfoattr)
10436 goto nla_put_failure;
10437
David S. Miller9360ffd2012-03-29 04:41:26 -040010438 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10439 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010440
10441 nla_nest_end(msg, pinfoattr);
10442
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010443 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010444
10445 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10446 nl80211_mlme_mcgrp.id, gfp);
10447 return;
10448
10449 nla_put_failure:
10450 genlmsg_cancel(msg, hdr);
10451 nlmsg_free(msg);
10452}
Johannes Berg947add32013-02-22 22:05:20 +010010453EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010454
Johannes Berg7f6cf312011-11-04 11:18:15 +010010455void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10456 u64 cookie, bool acked, gfp_t gfp)
10457{
10458 struct wireless_dev *wdev = dev->ieee80211_ptr;
10459 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10460 struct sk_buff *msg;
10461 void *hdr;
10462 int err;
10463
Beni Lev4ee3e062012-08-27 12:49:39 +030010464 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10465
Thomas Graf58050fc2012-06-28 03:57:45 +000010466 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030010467
Johannes Berg7f6cf312011-11-04 11:18:15 +010010468 if (!msg)
10469 return;
10470
10471 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10472 if (!hdr) {
10473 nlmsg_free(msg);
10474 return;
10475 }
10476
David S. Miller9360ffd2012-03-29 04:41:26 -040010477 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10478 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10479 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10480 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10481 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10482 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010483
10484 err = genlmsg_end(msg, hdr);
10485 if (err < 0) {
10486 nlmsg_free(msg);
10487 return;
10488 }
10489
10490 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10491 nl80211_mlme_mcgrp.id, gfp);
10492 return;
10493
10494 nla_put_failure:
10495 genlmsg_cancel(msg, hdr);
10496 nlmsg_free(msg);
10497}
10498EXPORT_SYMBOL(cfg80211_probe_status);
10499
Johannes Berg5e760232011-11-04 11:18:17 +010010500void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10501 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070010502 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010010503{
10504 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10505 struct sk_buff *msg;
10506 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070010507 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010010508
Beni Lev4ee3e062012-08-27 12:49:39 +030010509 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10510
Ben Greear37c73b52012-10-26 14:49:25 -070010511 spin_lock_bh(&rdev->beacon_registrations_lock);
10512 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10513 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10514 if (!msg) {
10515 spin_unlock_bh(&rdev->beacon_registrations_lock);
10516 return;
10517 }
Johannes Berg5e760232011-11-04 11:18:17 +010010518
Ben Greear37c73b52012-10-26 14:49:25 -070010519 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10520 if (!hdr)
10521 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010010522
Ben Greear37c73b52012-10-26 14:49:25 -070010523 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10524 (freq &&
10525 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10526 (sig_dbm &&
10527 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10528 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10529 goto nla_put_failure;
10530
10531 genlmsg_end(msg, hdr);
10532
10533 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010010534 }
Ben Greear37c73b52012-10-26 14:49:25 -070010535 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010536 return;
10537
10538 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070010539 spin_unlock_bh(&rdev->beacon_registrations_lock);
10540 if (hdr)
10541 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010010542 nlmsg_free(msg);
10543}
10544EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10545
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010546#ifdef CONFIG_PM
10547void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10548 struct cfg80211_wowlan_wakeup *wakeup,
10549 gfp_t gfp)
10550{
10551 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10552 struct sk_buff *msg;
10553 void *hdr;
10554 int err, size = 200;
10555
10556 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10557
10558 if (wakeup)
10559 size += wakeup->packet_present_len;
10560
10561 msg = nlmsg_new(size, gfp);
10562 if (!msg)
10563 return;
10564
10565 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10566 if (!hdr)
10567 goto free_msg;
10568
10569 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10570 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10571 goto free_msg;
10572
10573 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10574 wdev->netdev->ifindex))
10575 goto free_msg;
10576
10577 if (wakeup) {
10578 struct nlattr *reasons;
10579
10580 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10581
10582 if (wakeup->disconnect &&
10583 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10584 goto free_msg;
10585 if (wakeup->magic_pkt &&
10586 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10587 goto free_msg;
10588 if (wakeup->gtk_rekey_failure &&
10589 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10590 goto free_msg;
10591 if (wakeup->eap_identity_req &&
10592 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10593 goto free_msg;
10594 if (wakeup->four_way_handshake &&
10595 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10596 goto free_msg;
10597 if (wakeup->rfkill_release &&
10598 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10599 goto free_msg;
10600
10601 if (wakeup->pattern_idx >= 0 &&
10602 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10603 wakeup->pattern_idx))
10604 goto free_msg;
10605
Johannes Berg2a0e0472013-01-23 22:57:40 +010010606 if (wakeup->tcp_match)
10607 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10608
10609 if (wakeup->tcp_connlost)
10610 nla_put_flag(msg,
10611 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10612
10613 if (wakeup->tcp_nomoretokens)
10614 nla_put_flag(msg,
10615 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10616
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010617 if (wakeup->packet) {
10618 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10619 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10620
10621 if (!wakeup->packet_80211) {
10622 pkt_attr =
10623 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10624 len_attr =
10625 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10626 }
10627
10628 if (wakeup->packet_len &&
10629 nla_put_u32(msg, len_attr, wakeup->packet_len))
10630 goto free_msg;
10631
10632 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10633 wakeup->packet))
10634 goto free_msg;
10635 }
10636
10637 nla_nest_end(msg, reasons);
10638 }
10639
10640 err = genlmsg_end(msg, hdr);
10641 if (err < 0)
10642 goto free_msg;
10643
10644 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10645 nl80211_mlme_mcgrp.id, gfp);
10646 return;
10647
10648 free_msg:
10649 nlmsg_free(msg);
10650}
10651EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10652#endif
10653
Jouni Malinen3475b092012-11-16 22:49:57 +020010654void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10655 enum nl80211_tdls_operation oper,
10656 u16 reason_code, gfp_t gfp)
10657{
10658 struct wireless_dev *wdev = dev->ieee80211_ptr;
10659 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10660 struct sk_buff *msg;
10661 void *hdr;
10662 int err;
10663
10664 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10665 reason_code);
10666
10667 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10668 if (!msg)
10669 return;
10670
10671 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10672 if (!hdr) {
10673 nlmsg_free(msg);
10674 return;
10675 }
10676
10677 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10678 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10679 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10680 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10681 (reason_code > 0 &&
10682 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10683 goto nla_put_failure;
10684
10685 err = genlmsg_end(msg, hdr);
10686 if (err < 0) {
10687 nlmsg_free(msg);
10688 return;
10689 }
10690
10691 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10692 nl80211_mlme_mcgrp.id, gfp);
10693 return;
10694
10695 nla_put_failure:
10696 genlmsg_cancel(msg, hdr);
10697 nlmsg_free(msg);
10698}
10699EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10700
Jouni Malinen026331c2010-02-15 12:53:10 +020010701static int nl80211_netlink_notify(struct notifier_block * nb,
10702 unsigned long state,
10703 void *_notify)
10704{
10705 struct netlink_notify *notify = _notify;
10706 struct cfg80211_registered_device *rdev;
10707 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010708 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010709
10710 if (state != NETLINK_URELEASE)
10711 return NOTIFY_DONE;
10712
10713 rcu_read_lock();
10714
Johannes Berg5e760232011-11-04 11:18:17 +010010715 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010716 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010717 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010718
10719 spin_lock_bh(&rdev->beacon_registrations_lock);
10720 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10721 list) {
10722 if (reg->nlportid == notify->portid) {
10723 list_del(&reg->list);
10724 kfree(reg);
10725 break;
10726 }
10727 }
10728 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010729 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010730
10731 rcu_read_unlock();
10732
10733 return NOTIFY_DONE;
10734}
10735
10736static struct notifier_block nl80211_netlink_notifier = {
10737 .notifier_call = nl80211_netlink_notify,
10738};
10739
Jouni Malinen355199e2013-02-27 17:14:27 +020010740void cfg80211_ft_event(struct net_device *netdev,
10741 struct cfg80211_ft_event_params *ft_event)
10742{
10743 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
10744 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10745 struct sk_buff *msg;
10746 void *hdr;
10747 int err;
10748
10749 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
10750
10751 if (!ft_event->target_ap)
10752 return;
10753
10754 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10755 if (!msg)
10756 return;
10757
10758 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
10759 if (!hdr) {
10760 nlmsg_free(msg);
10761 return;
10762 }
10763
10764 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
10765 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
10766 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
10767 if (ft_event->ies)
10768 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
10769 if (ft_event->ric_ies)
10770 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
10771 ft_event->ric_ies);
10772
10773 err = genlmsg_end(msg, hdr);
10774 if (err < 0) {
10775 nlmsg_free(msg);
10776 return;
10777 }
10778
10779 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10780 nl80211_mlme_mcgrp.id, GFP_KERNEL);
10781}
10782EXPORT_SYMBOL(cfg80211_ft_event);
10783
Arend van Spriel5de17982013-04-18 15:49:00 +020010784void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
10785{
10786 struct cfg80211_registered_device *rdev;
10787 struct sk_buff *msg;
10788 void *hdr;
10789 u32 nlportid;
10790
10791 rdev = wiphy_to_dev(wdev->wiphy);
10792 if (!rdev->crit_proto_nlportid)
10793 return;
10794
10795 nlportid = rdev->crit_proto_nlportid;
10796 rdev->crit_proto_nlportid = 0;
10797
10798 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10799 if (!msg)
10800 return;
10801
10802 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
10803 if (!hdr)
10804 goto nla_put_failure;
10805
10806 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10807 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10808 goto nla_put_failure;
10809
10810 genlmsg_end(msg, hdr);
10811
10812 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
10813 return;
10814
10815 nla_put_failure:
10816 if (hdr)
10817 genlmsg_cancel(msg, hdr);
10818 nlmsg_free(msg);
10819
10820}
10821EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
10822
Johannes Berg55682962007-09-20 13:09:35 -040010823/* initialisation/exit functions */
10824
10825int nl80211_init(void)
10826{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010827 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010828
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010829 err = genl_register_family_with_ops(&nl80211_fam,
10830 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010831 if (err)
10832 return err;
10833
Johannes Berg55682962007-09-20 13:09:35 -040010834 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10835 if (err)
10836 goto err_out;
10837
Johannes Berg2a519312009-02-10 21:25:55 +010010838 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10839 if (err)
10840 goto err_out;
10841
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010842 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10843 if (err)
10844 goto err_out;
10845
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010846 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10847 if (err)
10848 goto err_out;
10849
Johannes Bergaff89a92009-07-01 21:26:51 +020010850#ifdef CONFIG_NL80211_TESTMODE
10851 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10852 if (err)
10853 goto err_out;
10854#endif
10855
Jouni Malinen026331c2010-02-15 12:53:10 +020010856 err = netlink_register_notifier(&nl80211_netlink_notifier);
10857 if (err)
10858 goto err_out;
10859
Johannes Berg55682962007-09-20 13:09:35 -040010860 return 0;
10861 err_out:
10862 genl_unregister_family(&nl80211_fam);
10863 return err;
10864}
10865
10866void nl80211_exit(void)
10867{
Jouni Malinen026331c2010-02-15 12:53:10 +020010868 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010869 genl_unregister_family(&nl80211_fam);
10870}