blob: 8c8a57937b224093f8086ca830c96800d6f516ea [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc902008-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 },
Johannes Berg55682962007-09-20 13:09:35 -0400381};
382
Johannes Berge31b8212010-10-05 19:39:30 +0200383/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000384static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200385 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200386 [NL80211_KEY_IDX] = { .type = NLA_U8 },
387 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200388 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200389 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
390 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200391 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100392 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
393};
394
395/* policy for the key default flags */
396static const struct nla_policy
397nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
398 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
399 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200400};
401
Johannes Bergff1b6e62011-05-04 15:37:28 +0200402/* policy for WoWLAN attributes */
403static const struct nla_policy
404nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
405 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
406 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
407 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
408 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200409 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
410 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
411 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
412 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100413 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
414};
415
416static const struct nla_policy
417nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
418 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
419 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
420 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
421 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
422 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
423 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
424 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
425 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
426 },
427 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
428 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
429 },
430 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
431 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
432 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200433};
434
Johannes Berge5497d72011-07-05 16:35:40 +0200435/* policy for GTK rekey offload attributes */
436static const struct nla_policy
437nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
438 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
439 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
440 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
441};
442
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300443static const struct nla_policy
444nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200445 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300446 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700447 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300448};
449
Johannes Berg97990a02013-04-19 01:02:55 +0200450static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
451 struct netlink_callback *cb,
452 struct cfg80211_registered_device **rdev,
453 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100454{
Johannes Berg67748892010-10-04 21:14:06 +0200455 int err;
456
Johannes Berg67748892010-10-04 21:14:06 +0200457 rtnl_lock();
Johannes Berg97990a02013-04-19 01:02:55 +0200458 mutex_lock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200459
Johannes Berg97990a02013-04-19 01:02:55 +0200460 if (!cb->args[0]) {
461 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
462 nl80211_fam.attrbuf, nl80211_fam.maxattr,
463 nl80211_policy);
464 if (err)
465 goto out_unlock;
466
467 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
468 nl80211_fam.attrbuf);
469 if (IS_ERR(*wdev)) {
470 err = PTR_ERR(*wdev);
471 goto out_unlock;
472 }
473 *rdev = wiphy_to_dev((*wdev)->wiphy);
474 cb->args[0] = (*rdev)->wiphy_idx;
475 cb->args[1] = (*wdev)->identifier;
476 } else {
477 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0]);
478 struct wireless_dev *tmp;
479
480 if (!wiphy) {
481 err = -ENODEV;
482 goto out_unlock;
483 }
484 *rdev = wiphy_to_dev(wiphy);
485 *wdev = NULL;
486
487 mutex_lock(&(*rdev)->devlist_mtx);
488 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
489 if (tmp->identifier == cb->args[1]) {
490 *wdev = tmp;
491 break;
492 }
493 }
494 mutex_unlock(&(*rdev)->devlist_mtx);
495
496 if (!*wdev) {
497 err = -ENODEV;
498 goto out_unlock;
499 }
Johannes Berg67748892010-10-04 21:14:06 +0200500 }
501
Johannes Berg97990a02013-04-19 01:02:55 +0200502 cfg80211_lock_rdev(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200503
Johannes Berg97990a02013-04-19 01:02:55 +0200504 mutex_unlock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200505 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200506 out_unlock:
507 mutex_unlock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200508 rtnl_unlock();
509 return err;
510}
511
Johannes Berg97990a02013-04-19 01:02:55 +0200512static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200513{
514 cfg80211_unlock_rdev(rdev);
515 rtnl_unlock();
516}
517
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100518/* IE validation */
519static bool is_valid_ie_attr(const struct nlattr *attr)
520{
521 const u8 *pos;
522 int len;
523
524 if (!attr)
525 return true;
526
527 pos = nla_data(attr);
528 len = nla_len(attr);
529
530 while (len) {
531 u8 elemlen;
532
533 if (len < 2)
534 return false;
535 len -= 2;
536
537 elemlen = pos[1];
538 if (elemlen > len)
539 return false;
540
541 len -= elemlen;
542 pos += 2 + elemlen;
543 }
544
545 return true;
546}
547
Johannes Berg55682962007-09-20 13:09:35 -0400548/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000549static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400550 int flags, u8 cmd)
551{
552 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000553 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400554}
555
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400556static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100557 struct ieee80211_channel *chan,
558 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400559{
David S. Miller9360ffd2012-03-29 04:41:26 -0400560 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
561 chan->center_freq))
562 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400563
David S. Miller9360ffd2012-03-29 04:41:26 -0400564 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
565 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
566 goto nla_put_failure;
567 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
568 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
569 goto nla_put_failure;
570 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
571 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
572 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100573 if (chan->flags & IEEE80211_CHAN_RADAR) {
574 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
575 goto nla_put_failure;
576 if (large) {
577 u32 time;
578
579 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
580
581 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
582 chan->dfs_state))
583 goto nla_put_failure;
584 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
585 time))
586 goto nla_put_failure;
587 }
588 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400589
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100590 if (large) {
591 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
592 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
593 goto nla_put_failure;
594 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
595 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
596 goto nla_put_failure;
597 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
598 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
599 goto nla_put_failure;
600 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
601 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
602 goto nla_put_failure;
603 }
604
David S. Miller9360ffd2012-03-29 04:41:26 -0400605 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
606 DBM_TO_MBM(chan->max_power)))
607 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400608
609 return 0;
610
611 nla_put_failure:
612 return -ENOBUFS;
613}
614
Johannes Berg55682962007-09-20 13:09:35 -0400615/* netlink command implementations */
616
Johannes Bergb9454e82009-07-08 13:29:08 +0200617struct key_parse {
618 struct key_params p;
619 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200620 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200621 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100622 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200623};
624
625static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
626{
627 struct nlattr *tb[NL80211_KEY_MAX + 1];
628 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
629 nl80211_key_policy);
630 if (err)
631 return err;
632
633 k->def = !!tb[NL80211_KEY_DEFAULT];
634 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
635
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100636 if (k->def) {
637 k->def_uni = true;
638 k->def_multi = true;
639 }
640 if (k->defmgmt)
641 k->def_multi = true;
642
Johannes Bergb9454e82009-07-08 13:29:08 +0200643 if (tb[NL80211_KEY_IDX])
644 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
645
646 if (tb[NL80211_KEY_DATA]) {
647 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
648 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
649 }
650
651 if (tb[NL80211_KEY_SEQ]) {
652 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
653 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
654 }
655
656 if (tb[NL80211_KEY_CIPHER])
657 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
658
Johannes Berge31b8212010-10-05 19:39:30 +0200659 if (tb[NL80211_KEY_TYPE]) {
660 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
661 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
662 return -EINVAL;
663 }
664
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100665 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
666 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100667 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
668 tb[NL80211_KEY_DEFAULT_TYPES],
669 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100670 if (err)
671 return err;
672
673 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
674 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
675 }
676
Johannes Bergb9454e82009-07-08 13:29:08 +0200677 return 0;
678}
679
680static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
681{
682 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
683 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
684 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
685 }
686
687 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
688 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
689 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
690 }
691
692 if (info->attrs[NL80211_ATTR_KEY_IDX])
693 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
694
695 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
696 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
697
698 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
699 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
700
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100701 if (k->def) {
702 k->def_uni = true;
703 k->def_multi = true;
704 }
705 if (k->defmgmt)
706 k->def_multi = true;
707
Johannes Berge31b8212010-10-05 19:39:30 +0200708 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
709 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
710 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
711 return -EINVAL;
712 }
713
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100714 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
715 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
716 int err = nla_parse_nested(
717 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
718 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
719 nl80211_key_default_policy);
720 if (err)
721 return err;
722
723 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
724 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
725 }
726
Johannes Bergb9454e82009-07-08 13:29:08 +0200727 return 0;
728}
729
730static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
731{
732 int err;
733
734 memset(k, 0, sizeof(*k));
735 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200736 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200737
738 if (info->attrs[NL80211_ATTR_KEY])
739 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
740 else
741 err = nl80211_parse_key_old(info, k);
742
743 if (err)
744 return err;
745
746 if (k->def && k->defmgmt)
747 return -EINVAL;
748
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100749 if (k->defmgmt) {
750 if (k->def_uni || !k->def_multi)
751 return -EINVAL;
752 }
753
Johannes Bergb9454e82009-07-08 13:29:08 +0200754 if (k->idx != -1) {
755 if (k->defmgmt) {
756 if (k->idx < 4 || k->idx > 5)
757 return -EINVAL;
758 } else if (k->def) {
759 if (k->idx < 0 || k->idx > 3)
760 return -EINVAL;
761 } else {
762 if (k->idx < 0 || k->idx > 5)
763 return -EINVAL;
764 }
765 }
766
767 return 0;
768}
769
Johannes Bergfffd0932009-07-08 14:22:54 +0200770static struct cfg80211_cached_keys *
771nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530772 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200773{
774 struct key_parse parse;
775 struct nlattr *key;
776 struct cfg80211_cached_keys *result;
777 int rem, err, def = 0;
778
779 result = kzalloc(sizeof(*result), GFP_KERNEL);
780 if (!result)
781 return ERR_PTR(-ENOMEM);
782
783 result->def = -1;
784 result->defmgmt = -1;
785
786 nla_for_each_nested(key, keys, rem) {
787 memset(&parse, 0, sizeof(parse));
788 parse.idx = -1;
789
790 err = nl80211_parse_key_new(key, &parse);
791 if (err)
792 goto error;
793 err = -EINVAL;
794 if (!parse.p.key)
795 goto error;
796 if (parse.idx < 0 || parse.idx > 4)
797 goto error;
798 if (parse.def) {
799 if (def)
800 goto error;
801 def = 1;
802 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100803 if (!parse.def_uni || !parse.def_multi)
804 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200805 } else if (parse.defmgmt)
806 goto error;
807 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200808 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200809 if (err)
810 goto error;
811 result->params[parse.idx].cipher = parse.p.cipher;
812 result->params[parse.idx].key_len = parse.p.key_len;
813 result->params[parse.idx].key = result->data[parse.idx];
814 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530815
816 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
817 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
818 if (no_ht)
819 *no_ht = true;
820 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200821 }
822
823 return result;
824 error:
825 kfree(result);
826 return ERR_PTR(err);
827}
828
829static int nl80211_key_allowed(struct wireless_dev *wdev)
830{
831 ASSERT_WDEV_LOCK(wdev);
832
Johannes Bergfffd0932009-07-08 14:22:54 +0200833 switch (wdev->iftype) {
834 case NL80211_IFTYPE_AP:
835 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200836 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700837 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200838 break;
839 case NL80211_IFTYPE_ADHOC:
840 if (!wdev->current_bss)
841 return -ENOLINK;
842 break;
843 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200844 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200845 if (wdev->sme_state != CFG80211_SME_CONNECTED)
846 return -ENOLINK;
847 break;
848 default:
849 return -EINVAL;
850 }
851
852 return 0;
853}
854
Johannes Berg7527a782011-05-13 10:58:57 +0200855static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
856{
857 struct nlattr *nl_modes = nla_nest_start(msg, attr);
858 int i;
859
860 if (!nl_modes)
861 goto nla_put_failure;
862
863 i = 0;
864 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400865 if ((ifmodes & 1) && nla_put_flag(msg, i))
866 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200867 ifmodes >>= 1;
868 i++;
869 }
870
871 nla_nest_end(msg, nl_modes);
872 return 0;
873
874nla_put_failure:
875 return -ENOBUFS;
876}
877
878static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100879 struct sk_buff *msg,
880 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200881{
882 struct nlattr *nl_combis;
883 int i, j;
884
885 nl_combis = nla_nest_start(msg,
886 NL80211_ATTR_INTERFACE_COMBINATIONS);
887 if (!nl_combis)
888 goto nla_put_failure;
889
890 for (i = 0; i < wiphy->n_iface_combinations; i++) {
891 const struct ieee80211_iface_combination *c;
892 struct nlattr *nl_combi, *nl_limits;
893
894 c = &wiphy->iface_combinations[i];
895
896 nl_combi = nla_nest_start(msg, i + 1);
897 if (!nl_combi)
898 goto nla_put_failure;
899
900 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
901 if (!nl_limits)
902 goto nla_put_failure;
903
904 for (j = 0; j < c->n_limits; j++) {
905 struct nlattr *nl_limit;
906
907 nl_limit = nla_nest_start(msg, j + 1);
908 if (!nl_limit)
909 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400910 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
911 c->limits[j].max))
912 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200913 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
914 c->limits[j].types))
915 goto nla_put_failure;
916 nla_nest_end(msg, nl_limit);
917 }
918
919 nla_nest_end(msg, nl_limits);
920
David S. Miller9360ffd2012-03-29 04:41:26 -0400921 if (c->beacon_int_infra_match &&
922 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
923 goto nla_put_failure;
924 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
925 c->num_different_channels) ||
926 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
927 c->max_interfaces))
928 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100929 if (large &&
930 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
931 c->radar_detect_widths))
932 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200933
934 nla_nest_end(msg, nl_combi);
935 }
936
937 nla_nest_end(msg, nl_combis);
938
939 return 0;
940nla_put_failure:
941 return -ENOBUFS;
942}
943
Johannes Berg3713b4e2013-02-14 16:19:38 +0100944#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100945static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
946 struct sk_buff *msg)
947{
948 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
949 struct nlattr *nl_tcp;
950
951 if (!tcp)
952 return 0;
953
954 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
955 if (!nl_tcp)
956 return -ENOBUFS;
957
958 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
959 tcp->data_payload_max))
960 return -ENOBUFS;
961
962 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
963 tcp->data_payload_max))
964 return -ENOBUFS;
965
966 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
967 return -ENOBUFS;
968
969 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
970 sizeof(*tcp->tok), tcp->tok))
971 return -ENOBUFS;
972
973 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
974 tcp->data_interval_max))
975 return -ENOBUFS;
976
977 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
978 tcp->wake_payload_max))
979 return -ENOBUFS;
980
981 nla_nest_end(msg, nl_tcp);
982 return 0;
983}
984
Johannes Berg3713b4e2013-02-14 16:19:38 +0100985static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +0100986 struct cfg80211_registered_device *dev,
987 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100988{
989 struct nlattr *nl_wowlan;
990
991 if (!dev->wiphy.wowlan.flags && !dev->wiphy.wowlan.n_patterns)
992 return 0;
993
994 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
995 if (!nl_wowlan)
996 return -ENOBUFS;
997
998 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
999 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1000 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1001 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1002 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1003 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1004 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1005 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1006 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1007 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1008 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1009 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1010 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1011 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1012 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1013 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1014 return -ENOBUFS;
1015
1016 if (dev->wiphy.wowlan.n_patterns) {
1017 struct nl80211_wowlan_pattern_support pat = {
1018 .max_patterns = dev->wiphy.wowlan.n_patterns,
1019 .min_pattern_len = dev->wiphy.wowlan.pattern_min_len,
1020 .max_pattern_len = dev->wiphy.wowlan.pattern_max_len,
1021 .max_pkt_offset = dev->wiphy.wowlan.max_pkt_offset,
1022 };
1023
1024 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1025 sizeof(pat), &pat))
1026 return -ENOBUFS;
1027 }
1028
Johannes Bergb56cf722013-02-20 01:02:38 +01001029 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1030 return -ENOBUFS;
1031
Johannes Berg3713b4e2013-02-14 16:19:38 +01001032 nla_nest_end(msg, nl_wowlan);
1033
1034 return 0;
1035}
1036#endif
1037
1038static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1039 struct ieee80211_supported_band *sband)
1040{
1041 struct nlattr *nl_rates, *nl_rate;
1042 struct ieee80211_rate *rate;
1043 int i;
1044
1045 /* add HT info */
1046 if (sband->ht_cap.ht_supported &&
1047 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1048 sizeof(sband->ht_cap.mcs),
1049 &sband->ht_cap.mcs) ||
1050 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1051 sband->ht_cap.cap) ||
1052 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1053 sband->ht_cap.ampdu_factor) ||
1054 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1055 sband->ht_cap.ampdu_density)))
1056 return -ENOBUFS;
1057
1058 /* add VHT info */
1059 if (sband->vht_cap.vht_supported &&
1060 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1061 sizeof(sband->vht_cap.vht_mcs),
1062 &sband->vht_cap.vht_mcs) ||
1063 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1064 sband->vht_cap.cap)))
1065 return -ENOBUFS;
1066
1067 /* add bitrates */
1068 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1069 if (!nl_rates)
1070 return -ENOBUFS;
1071
1072 for (i = 0; i < sband->n_bitrates; i++) {
1073 nl_rate = nla_nest_start(msg, i);
1074 if (!nl_rate)
1075 return -ENOBUFS;
1076
1077 rate = &sband->bitrates[i];
1078 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1079 rate->bitrate))
1080 return -ENOBUFS;
1081 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1082 nla_put_flag(msg,
1083 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1084 return -ENOBUFS;
1085
1086 nla_nest_end(msg, nl_rate);
1087 }
1088
1089 nla_nest_end(msg, nl_rates);
1090
1091 return 0;
1092}
1093
1094static int
1095nl80211_send_mgmt_stypes(struct sk_buff *msg,
1096 const struct ieee80211_txrx_stypes *mgmt_stypes)
1097{
1098 u16 stypes;
1099 struct nlattr *nl_ftypes, *nl_ifs;
1100 enum nl80211_iftype ift;
1101 int i;
1102
1103 if (!mgmt_stypes)
1104 return 0;
1105
1106 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1107 if (!nl_ifs)
1108 return -ENOBUFS;
1109
1110 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1111 nl_ftypes = nla_nest_start(msg, ift);
1112 if (!nl_ftypes)
1113 return -ENOBUFS;
1114 i = 0;
1115 stypes = mgmt_stypes[ift].tx;
1116 while (stypes) {
1117 if ((stypes & 1) &&
1118 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1119 (i << 4) | IEEE80211_FTYPE_MGMT))
1120 return -ENOBUFS;
1121 stypes >>= 1;
1122 i++;
1123 }
1124 nla_nest_end(msg, nl_ftypes);
1125 }
1126
1127 nla_nest_end(msg, nl_ifs);
1128
1129 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1130 if (!nl_ifs)
1131 return -ENOBUFS;
1132
1133 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1134 nl_ftypes = nla_nest_start(msg, ift);
1135 if (!nl_ftypes)
1136 return -ENOBUFS;
1137 i = 0;
1138 stypes = mgmt_stypes[ift].rx;
1139 while (stypes) {
1140 if ((stypes & 1) &&
1141 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1142 (i << 4) | IEEE80211_FTYPE_MGMT))
1143 return -ENOBUFS;
1144 stypes >>= 1;
1145 i++;
1146 }
1147 nla_nest_end(msg, nl_ftypes);
1148 }
1149 nla_nest_end(msg, nl_ifs);
1150
1151 return 0;
1152}
1153
1154static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1155 struct sk_buff *msg, u32 portid, u32 seq,
1156 int flags, bool split, long *split_start,
1157 long *band_start, long *chan_start)
Johannes Berg55682962007-09-20 13:09:35 -04001158{
1159 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001160 struct nlattr *nl_bands, *nl_band;
1161 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001162 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001163 enum ieee80211_band band;
1164 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001165 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001166 const struct ieee80211_txrx_stypes *mgmt_stypes =
1167 dev->wiphy.mgmt_stypes;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001168 long start = 0, start_chan = 0, start_band = 0;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001169 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001170
Eric W. Biederman15e47302012-09-07 20:12:54 +00001171 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001172 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001173 return -ENOBUFS;
1174
1175 /* allow always using the variables */
1176 if (!split) {
1177 split_start = &start;
1178 band_start = &start_band;
1179 chan_start = &start_chan;
1180 }
Johannes Berg55682962007-09-20 13:09:35 -04001181
David S. Miller9360ffd2012-03-29 04:41:26 -04001182 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001183 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1184 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001185 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001186 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001187 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001188
Johannes Berg3713b4e2013-02-14 16:19:38 +01001189 switch (*split_start) {
1190 case 0:
1191 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1192 dev->wiphy.retry_short) ||
1193 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1194 dev->wiphy.retry_long) ||
1195 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1196 dev->wiphy.frag_threshold) ||
1197 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1198 dev->wiphy.rts_threshold) ||
1199 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1200 dev->wiphy.coverage_class) ||
1201 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1202 dev->wiphy.max_scan_ssids) ||
1203 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1204 dev->wiphy.max_sched_scan_ssids) ||
1205 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1206 dev->wiphy.max_scan_ie_len) ||
1207 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1208 dev->wiphy.max_sched_scan_ie_len) ||
1209 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1210 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001211 goto nla_put_failure;
1212
Johannes Berg3713b4e2013-02-14 16:19:38 +01001213 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1214 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1215 goto nla_put_failure;
1216 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1217 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1218 goto nla_put_failure;
1219 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1220 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1221 goto nla_put_failure;
1222 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1223 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1224 goto nla_put_failure;
1225 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1226 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1227 goto nla_put_failure;
1228 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1229 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001230 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001231
Johannes Berg3713b4e2013-02-14 16:19:38 +01001232 (*split_start)++;
1233 if (split)
1234 break;
1235 case 1:
1236 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1237 sizeof(u32) * dev->wiphy.n_cipher_suites,
1238 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001239 goto nla_put_failure;
1240
Johannes Berg3713b4e2013-02-14 16:19:38 +01001241 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1242 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001243 goto nla_put_failure;
1244
Johannes Berg3713b4e2013-02-14 16:19:38 +01001245 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1246 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1247 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001248
Johannes Berg3713b4e2013-02-14 16:19:38 +01001249 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1250 dev->wiphy.available_antennas_tx) ||
1251 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1252 dev->wiphy.available_antennas_rx))
1253 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001254
Johannes Berg3713b4e2013-02-14 16:19:38 +01001255 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1256 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1257 dev->wiphy.probe_resp_offload))
1258 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001259
Johannes Berg3713b4e2013-02-14 16:19:38 +01001260 if ((dev->wiphy.available_antennas_tx ||
1261 dev->wiphy.available_antennas_rx) &&
1262 dev->ops->get_antenna) {
1263 u32 tx_ant = 0, rx_ant = 0;
1264 int res;
1265 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1266 if (!res) {
1267 if (nla_put_u32(msg,
1268 NL80211_ATTR_WIPHY_ANTENNA_TX,
1269 tx_ant) ||
1270 nla_put_u32(msg,
1271 NL80211_ATTR_WIPHY_ANTENNA_RX,
1272 rx_ant))
1273 goto nla_put_failure;
1274 }
Johannes Bergee688b002008-01-24 19:38:39 +01001275 }
1276
Johannes Berg3713b4e2013-02-14 16:19:38 +01001277 (*split_start)++;
1278 if (split)
1279 break;
1280 case 2:
1281 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1282 dev->wiphy.interface_modes))
1283 goto nla_put_failure;
1284 (*split_start)++;
1285 if (split)
1286 break;
1287 case 3:
1288 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1289 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001290 goto nla_put_failure;
1291
Johannes Berg3713b4e2013-02-14 16:19:38 +01001292 for (band = *band_start; band < IEEE80211_NUM_BANDS; band++) {
1293 struct ieee80211_supported_band *sband;
1294
1295 sband = dev->wiphy.bands[band];
1296
1297 if (!sband)
1298 continue;
1299
1300 nl_band = nla_nest_start(msg, band);
1301 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001302 goto nla_put_failure;
1303
Johannes Berg3713b4e2013-02-14 16:19:38 +01001304 switch (*chan_start) {
1305 case 0:
1306 if (nl80211_send_band_rateinfo(msg, sband))
1307 goto nla_put_failure;
1308 (*chan_start)++;
1309 if (split)
1310 break;
1311 default:
1312 /* add frequencies */
1313 nl_freqs = nla_nest_start(
1314 msg, NL80211_BAND_ATTR_FREQS);
1315 if (!nl_freqs)
1316 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001317
Johannes Berg3713b4e2013-02-14 16:19:38 +01001318 for (i = *chan_start - 1;
1319 i < sband->n_channels;
1320 i++) {
1321 nl_freq = nla_nest_start(msg, i);
1322 if (!nl_freq)
1323 goto nla_put_failure;
1324
1325 chan = &sband->channels[i];
1326
Johannes Bergcdc89b92013-02-18 23:54:36 +01001327 if (nl80211_msg_put_channel(msg, chan,
1328 split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001329 goto nla_put_failure;
1330
1331 nla_nest_end(msg, nl_freq);
1332 if (split)
1333 break;
1334 }
1335 if (i < sband->n_channels)
1336 *chan_start = i + 2;
1337 else
1338 *chan_start = 0;
1339 nla_nest_end(msg, nl_freqs);
1340 }
1341
1342 nla_nest_end(msg, nl_band);
1343
1344 if (split) {
1345 /* start again here */
1346 if (*chan_start)
1347 band--;
1348 break;
1349 }
Johannes Bergee688b002008-01-24 19:38:39 +01001350 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001351 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001352
Johannes Berg3713b4e2013-02-14 16:19:38 +01001353 if (band < IEEE80211_NUM_BANDS)
1354 *band_start = band + 1;
1355 else
1356 *band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001357
Johannes Berg3713b4e2013-02-14 16:19:38 +01001358 /* if bands & channels are done, continue outside */
1359 if (*band_start == 0 && *chan_start == 0)
1360 (*split_start)++;
1361 if (split)
1362 break;
1363 case 4:
1364 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1365 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001366 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001367
1368 i = 0;
1369#define CMD(op, n) \
1370 do { \
1371 if (dev->ops->op) { \
1372 i++; \
1373 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1374 goto nla_put_failure; \
1375 } \
1376 } while (0)
1377
1378 CMD(add_virtual_intf, NEW_INTERFACE);
1379 CMD(change_virtual_intf, SET_INTERFACE);
1380 CMD(add_key, NEW_KEY);
1381 CMD(start_ap, START_AP);
1382 CMD(add_station, NEW_STATION);
1383 CMD(add_mpath, NEW_MPATH);
1384 CMD(update_mesh_config, SET_MESH_CONFIG);
1385 CMD(change_bss, SET_BSS);
1386 CMD(auth, AUTHENTICATE);
1387 CMD(assoc, ASSOCIATE);
1388 CMD(deauth, DEAUTHENTICATE);
1389 CMD(disassoc, DISASSOCIATE);
1390 CMD(join_ibss, JOIN_IBSS);
1391 CMD(join_mesh, JOIN_MESH);
1392 CMD(set_pmksa, SET_PMKSA);
1393 CMD(del_pmksa, DEL_PMKSA);
1394 CMD(flush_pmksa, FLUSH_PMKSA);
1395 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1396 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1397 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1398 CMD(mgmt_tx, FRAME);
1399 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1400 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1401 i++;
1402 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1403 goto nla_put_failure;
1404 }
1405 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1406 dev->ops->join_mesh) {
1407 i++;
1408 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1409 goto nla_put_failure;
1410 }
1411 CMD(set_wds_peer, SET_WDS_PEER);
1412 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1413 CMD(tdls_mgmt, TDLS_MGMT);
1414 CMD(tdls_oper, TDLS_OPER);
1415 }
1416 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1417 CMD(sched_scan_start, START_SCHED_SCAN);
1418 CMD(probe_client, PROBE_CLIENT);
1419 CMD(set_noack_map, SET_NOACK_MAP);
1420 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1421 i++;
1422 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1423 goto nla_put_failure;
1424 }
1425 CMD(start_p2p_device, START_P2P_DEVICE);
1426 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001427
Kalle Valo4745fc02011-11-17 19:06:10 +02001428#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001429 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001430#endif
1431
Johannes Berg8fdc6212009-03-14 09:34:01 +01001432#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001433
Johannes Berg3713b4e2013-02-14 16:19:38 +01001434 if (dev->ops->connect || dev->ops->auth) {
1435 i++;
1436 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001437 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001438 }
1439
Johannes Berg3713b4e2013-02-14 16:19:38 +01001440 if (dev->ops->disconnect || dev->ops->deauth) {
1441 i++;
1442 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1443 goto nla_put_failure;
1444 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001445
Johannes Berg3713b4e2013-02-14 16:19:38 +01001446 nla_nest_end(msg, nl_cmds);
1447 (*split_start)++;
1448 if (split)
1449 break;
1450 case 5:
1451 if (dev->ops->remain_on_channel &&
1452 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1453 nla_put_u32(msg,
1454 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1455 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001456 goto nla_put_failure;
1457
Johannes Berg3713b4e2013-02-14 16:19:38 +01001458 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1459 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1460 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001461
Johannes Berg3713b4e2013-02-14 16:19:38 +01001462 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1463 goto nla_put_failure;
1464 (*split_start)++;
1465 if (split)
1466 break;
1467 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001468#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001469 if (nl80211_send_wowlan(msg, dev, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001470 goto nla_put_failure;
1471 (*split_start)++;
1472 if (split)
1473 break;
1474#else
1475 (*split_start)++;
1476#endif
1477 case 7:
1478 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1479 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001480 goto nla_put_failure;
1481
Johannes Bergcdc89b92013-02-18 23:54:36 +01001482 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001483 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001484
Johannes Berg3713b4e2013-02-14 16:19:38 +01001485 (*split_start)++;
1486 if (split)
1487 break;
1488 case 8:
1489 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1490 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1491 dev->wiphy.ap_sme_capa))
1492 goto nla_put_failure;
1493
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001494 features = dev->wiphy.features;
1495 /*
1496 * We can only add the per-channel limit information if the
1497 * dump is split, otherwise it makes it too big. Therefore
1498 * only advertise it in that case.
1499 */
1500 if (split)
1501 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1502 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001503 goto nla_put_failure;
1504
1505 if (dev->wiphy.ht_capa_mod_mask &&
1506 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1507 sizeof(*dev->wiphy.ht_capa_mod_mask),
1508 dev->wiphy.ht_capa_mod_mask))
1509 goto nla_put_failure;
1510
1511 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1512 dev->wiphy.max_acl_mac_addrs &&
1513 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1514 dev->wiphy.max_acl_mac_addrs))
1515 goto nla_put_failure;
1516
1517 /*
1518 * Any information below this point is only available to
1519 * applications that can deal with it being split. This
1520 * helps ensure that newly added capabilities don't break
1521 * older tools by overrunning their buffers.
1522 *
1523 * We still increment split_start so that in the split
1524 * case we'll continue with more data in the next round,
1525 * but break unconditionally so unsplit data stops here.
1526 */
1527 (*split_start)++;
1528 break;
1529 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001530 if (dev->wiphy.extended_capabilities &&
1531 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1532 dev->wiphy.extended_capabilities_len,
1533 dev->wiphy.extended_capabilities) ||
1534 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1535 dev->wiphy.extended_capabilities_len,
1536 dev->wiphy.extended_capabilities_mask)))
1537 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001538
Johannes Bergee2aca32013-02-21 17:36:01 +01001539 if (dev->wiphy.vht_capa_mod_mask &&
1540 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1541 sizeof(*dev->wiphy.vht_capa_mod_mask),
1542 dev->wiphy.vht_capa_mod_mask))
1543 goto nla_put_failure;
1544
Johannes Berg3713b4e2013-02-14 16:19:38 +01001545 /* done */
1546 *split_start = 0;
1547 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001548 }
Johannes Berg55682962007-09-20 13:09:35 -04001549 return genlmsg_end(msg, hdr);
1550
1551 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001552 genlmsg_cancel(msg, hdr);
1553 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001554}
1555
1556static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1557{
Johannes Berg645e77d2013-03-01 14:03:49 +01001558 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001559 int start = cb->args[0];
1560 struct cfg80211_registered_device *dev;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001561 s64 filter_wiphy = -1;
1562 bool split = false;
1563 struct nlattr **tb = nl80211_fam.attrbuf;
1564 int res;
Johannes Berg55682962007-09-20 13:09:35 -04001565
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001566 mutex_lock(&cfg80211_mutex);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001567 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1568 tb, nl80211_fam.maxattr, nl80211_policy);
1569 if (res == 0) {
1570 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1571 if (tb[NL80211_ATTR_WIPHY])
1572 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1573 if (tb[NL80211_ATTR_WDEV])
1574 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1575 if (tb[NL80211_ATTR_IFINDEX]) {
1576 struct net_device *netdev;
1577 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1578
1579 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1580 if (!netdev) {
1581 mutex_unlock(&cfg80211_mutex);
1582 return -ENODEV;
1583 }
1584 if (netdev->ieee80211_ptr) {
1585 dev = wiphy_to_dev(
1586 netdev->ieee80211_ptr->wiphy);
1587 filter_wiphy = dev->wiphy_idx;
1588 }
1589 dev_put(netdev);
1590 }
1591 }
1592
Johannes Berg79c97e92009-07-07 03:56:12 +02001593 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001594 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1595 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001596 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001597 continue;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001598 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1599 continue;
1600 /* attempt to fit multiple wiphy data chunks into the skb */
1601 do {
1602 ret = nl80211_send_wiphy(dev, skb,
1603 NETLINK_CB(cb->skb).portid,
1604 cb->nlh->nlmsg_seq,
1605 NLM_F_MULTI,
1606 split, &cb->args[1],
1607 &cb->args[2],
1608 &cb->args[3]);
1609 if (ret < 0) {
1610 /*
1611 * If sending the wiphy data didn't fit (ENOBUFS
1612 * or EMSGSIZE returned), this SKB is still
1613 * empty (so it's not too big because another
1614 * wiphy dataset is already in the skb) and
1615 * we've not tried to adjust the dump allocation
1616 * yet ... then adjust the alloc size to be
1617 * bigger, and return 1 but with the empty skb.
1618 * This results in an empty message being RX'ed
1619 * in userspace, but that is ignored.
1620 *
1621 * We can then retry with the larger buffer.
1622 */
1623 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1624 !skb->len &&
1625 cb->min_dump_alloc < 4096) {
1626 cb->min_dump_alloc = 4096;
1627 mutex_unlock(&cfg80211_mutex);
1628 return 1;
1629 }
1630 idx--;
1631 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001632 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001633 } while (cb->args[1] > 0);
1634 break;
Johannes Berg55682962007-09-20 13:09:35 -04001635 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001636 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001637
1638 cb->args[0] = idx;
1639
1640 return skb->len;
1641}
1642
1643static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1644{
1645 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001646 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001647
Johannes Berg645e77d2013-03-01 14:03:49 +01001648 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001649 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001650 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001651
Johannes Berg3713b4e2013-02-14 16:19:38 +01001652 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1653 false, NULL, NULL, NULL) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001654 nlmsg_free(msg);
1655 return -ENOBUFS;
1656 }
Johannes Berg55682962007-09-20 13:09:35 -04001657
Johannes Berg134e6372009-07-10 09:51:34 +00001658 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001659}
1660
Jouni Malinen31888482008-10-30 16:59:24 +02001661static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1662 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1663 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1664 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1665 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1666 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1667};
1668
1669static int parse_txq_params(struct nlattr *tb[],
1670 struct ieee80211_txq_params *txq_params)
1671{
Johannes Berga3304b02012-03-28 11:04:24 +02001672 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001673 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1674 !tb[NL80211_TXQ_ATTR_AIFS])
1675 return -EINVAL;
1676
Johannes Berga3304b02012-03-28 11:04:24 +02001677 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001678 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1679 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1680 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1681 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1682
Johannes Berga3304b02012-03-28 11:04:24 +02001683 if (txq_params->ac >= NL80211_NUM_ACS)
1684 return -EINVAL;
1685
Jouni Malinen31888482008-10-30 16:59:24 +02001686 return 0;
1687}
1688
Johannes Bergf444de02010-05-05 15:25:02 +02001689static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1690{
1691 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001692 * You can only set the channel explicitly for WDS interfaces,
1693 * all others have their channel managed via their respective
1694 * "establish a connection" command (connect, join, ...)
1695 *
1696 * For AP/GO and mesh mode, the channel can be set with the
1697 * channel userspace API, but is only stored and passed to the
1698 * low-level driver when the AP starts or the mesh is joined.
1699 * This is for backward compatibility, userspace can also give
1700 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001701 *
1702 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001703 * whatever else is going on, so they have their own special
1704 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001705 */
1706 return !wdev ||
1707 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001708 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001709 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1710 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001711}
1712
Johannes Berg683b6d32012-11-08 21:25:48 +01001713static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1714 struct genl_info *info,
1715 struct cfg80211_chan_def *chandef)
1716{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301717 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001718
1719 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1720 return -EINVAL;
1721
1722 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1723
1724 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001725 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1726 chandef->center_freq1 = control_freq;
1727 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001728
1729 /* Primary channel not allowed */
1730 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1731 return -EINVAL;
1732
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001733 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1734 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001735
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001736 chantype = nla_get_u32(
1737 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1738
1739 switch (chantype) {
1740 case NL80211_CHAN_NO_HT:
1741 case NL80211_CHAN_HT20:
1742 case NL80211_CHAN_HT40PLUS:
1743 case NL80211_CHAN_HT40MINUS:
1744 cfg80211_chandef_create(chandef, chandef->chan,
1745 chantype);
1746 break;
1747 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001748 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001749 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001750 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1751 chandef->width =
1752 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1753 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1754 chandef->center_freq1 =
1755 nla_get_u32(
1756 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1757 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1758 chandef->center_freq2 =
1759 nla_get_u32(
1760 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1761 }
1762
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001763 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001764 return -EINVAL;
1765
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001766 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1767 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001768 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001769
Johannes Berg683b6d32012-11-08 21:25:48 +01001770 return 0;
1771}
1772
Johannes Bergf444de02010-05-05 15:25:02 +02001773static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1774 struct wireless_dev *wdev,
1775 struct genl_info *info)
1776{
Johannes Berg683b6d32012-11-08 21:25:48 +01001777 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001778 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001779 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1780
1781 if (wdev)
1782 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001783
Johannes Bergf444de02010-05-05 15:25:02 +02001784 if (!nl80211_can_set_dev_channel(wdev))
1785 return -EOPNOTSUPP;
1786
Johannes Berg683b6d32012-11-08 21:25:48 +01001787 result = nl80211_parse_chandef(rdev, info, &chandef);
1788 if (result)
1789 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001790
1791 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001792 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001793 case NL80211_IFTYPE_AP:
1794 case NL80211_IFTYPE_P2P_GO:
1795 if (wdev->beacon_interval) {
1796 result = -EBUSY;
1797 break;
1798 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001799 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001800 result = -EINVAL;
1801 break;
1802 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001803 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001804 result = 0;
1805 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001806 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001807 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001808 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001809 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001810 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001811 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001812 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001813 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001814 }
1815 mutex_unlock(&rdev->devlist_mtx);
1816
1817 return result;
1818}
1819
1820static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1821{
Johannes Berg4c476992010-10-04 21:36:35 +02001822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1823 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001824
Johannes Berg4c476992010-10-04 21:36:35 +02001825 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001826}
1827
Bill Jordane8347eb2010-10-01 13:54:28 -04001828static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1829{
Johannes Berg43b19952010-10-07 13:10:30 +02001830 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1831 struct net_device *dev = info->user_ptr[1];
1832 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001833 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001834
1835 if (!info->attrs[NL80211_ATTR_MAC])
1836 return -EINVAL;
1837
Johannes Berg43b19952010-10-07 13:10:30 +02001838 if (netif_running(dev))
1839 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001840
Johannes Berg43b19952010-10-07 13:10:30 +02001841 if (!rdev->ops->set_wds_peer)
1842 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001843
Johannes Berg43b19952010-10-07 13:10:30 +02001844 if (wdev->iftype != NL80211_IFTYPE_WDS)
1845 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001846
1847 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001848 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001849}
1850
1851
Johannes Berg55682962007-09-20 13:09:35 -04001852static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1853{
1854 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001855 struct net_device *netdev = NULL;
1856 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001857 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001858 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001859 u32 changed;
1860 u8 retry_short = 0, retry_long = 0;
1861 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001862 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001863
Johannes Bergf444de02010-05-05 15:25:02 +02001864 /*
1865 * Try to find the wiphy and netdev. Normally this
1866 * function shouldn't need the netdev, but this is
1867 * done for backward compatibility -- previously
1868 * setting the channel was done per wiphy, but now
1869 * it is per netdev. Previous userland like hostapd
1870 * also passed a netdev to set_wiphy, so that it is
1871 * possible to let that go to the right netdev!
1872 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001873 mutex_lock(&cfg80211_mutex);
1874
Johannes Bergf444de02010-05-05 15:25:02 +02001875 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1876 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1877
1878 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1879 if (netdev && netdev->ieee80211_ptr) {
1880 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1881 mutex_lock(&rdev->mtx);
1882 } else
1883 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001884 }
1885
Johannes Bergf444de02010-05-05 15:25:02 +02001886 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001887 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1888 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001889 if (IS_ERR(rdev)) {
1890 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001891 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001892 }
1893 wdev = NULL;
1894 netdev = NULL;
1895 result = 0;
1896
1897 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001898 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001899 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001900
1901 /*
1902 * end workaround code, by now the rdev is available
1903 * and locked, and wdev may or may not be NULL.
1904 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001905
1906 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001907 result = cfg80211_dev_rename(
1908 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001909
1910 mutex_unlock(&cfg80211_mutex);
1911
1912 if (result)
1913 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001914
Jouni Malinen31888482008-10-30 16:59:24 +02001915 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1916 struct ieee80211_txq_params txq_params;
1917 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1918
1919 if (!rdev->ops->set_txq_params) {
1920 result = -EOPNOTSUPP;
1921 goto bad_res;
1922 }
1923
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001924 if (!netdev) {
1925 result = -EINVAL;
1926 goto bad_res;
1927 }
1928
Johannes Berg133a3ff2011-11-03 14:50:13 +01001929 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1930 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1931 result = -EINVAL;
1932 goto bad_res;
1933 }
1934
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001935 if (!netif_running(netdev)) {
1936 result = -ENETDOWN;
1937 goto bad_res;
1938 }
1939
Jouni Malinen31888482008-10-30 16:59:24 +02001940 nla_for_each_nested(nl_txq_params,
1941 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1942 rem_txq_params) {
1943 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1944 nla_data(nl_txq_params),
1945 nla_len(nl_txq_params),
1946 txq_params_policy);
1947 result = parse_txq_params(tb, &txq_params);
1948 if (result)
1949 goto bad_res;
1950
Hila Gonene35e4d22012-06-27 17:19:42 +03001951 result = rdev_set_txq_params(rdev, netdev,
1952 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001953 if (result)
1954 goto bad_res;
1955 }
1956 }
1957
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001958 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001959 result = __nl80211_set_channel(rdev,
1960 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1961 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001962 if (result)
1963 goto bad_res;
1964 }
1965
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001966 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001967 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001968 enum nl80211_tx_power_setting type;
1969 int idx, mbm = 0;
1970
Johannes Bergc8442112012-10-24 10:17:18 +02001971 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1972 txp_wdev = NULL;
1973
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001974 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001975 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001976 goto bad_res;
1977 }
1978
1979 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1980 type = nla_get_u32(info->attrs[idx]);
1981
1982 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1983 (type != NL80211_TX_POWER_AUTOMATIC)) {
1984 result = -EINVAL;
1985 goto bad_res;
1986 }
1987
1988 if (type != NL80211_TX_POWER_AUTOMATIC) {
1989 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1990 mbm = nla_get_u32(info->attrs[idx]);
1991 }
1992
Johannes Bergc8442112012-10-24 10:17:18 +02001993 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001994 if (result)
1995 goto bad_res;
1996 }
1997
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001998 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1999 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2000 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002001 if ((!rdev->wiphy.available_antennas_tx &&
2002 !rdev->wiphy.available_antennas_rx) ||
2003 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002004 result = -EOPNOTSUPP;
2005 goto bad_res;
2006 }
2007
2008 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2009 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2010
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002011 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002012 * available antenna masks, except for the "all" mask */
2013 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2014 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002015 result = -EINVAL;
2016 goto bad_res;
2017 }
2018
Bruno Randolf7f531e02010-12-16 11:30:22 +09002019 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2020 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002021
Hila Gonene35e4d22012-06-27 17:19:42 +03002022 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002023 if (result)
2024 goto bad_res;
2025 }
2026
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002027 changed = 0;
2028
2029 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2030 retry_short = nla_get_u8(
2031 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2032 if (retry_short == 0) {
2033 result = -EINVAL;
2034 goto bad_res;
2035 }
2036 changed |= WIPHY_PARAM_RETRY_SHORT;
2037 }
2038
2039 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2040 retry_long = nla_get_u8(
2041 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2042 if (retry_long == 0) {
2043 result = -EINVAL;
2044 goto bad_res;
2045 }
2046 changed |= WIPHY_PARAM_RETRY_LONG;
2047 }
2048
2049 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2050 frag_threshold = nla_get_u32(
2051 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2052 if (frag_threshold < 256) {
2053 result = -EINVAL;
2054 goto bad_res;
2055 }
2056 if (frag_threshold != (u32) -1) {
2057 /*
2058 * Fragments (apart from the last one) are required to
2059 * have even length. Make the fragmentation code
2060 * simpler by stripping LSB should someone try to use
2061 * odd threshold value.
2062 */
2063 frag_threshold &= ~0x1;
2064 }
2065 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2066 }
2067
2068 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2069 rts_threshold = nla_get_u32(
2070 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2071 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2072 }
2073
Lukáš Turek81077e82009-12-21 22:50:47 +01002074 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2075 coverage_class = nla_get_u8(
2076 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2077 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2078 }
2079
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002080 if (changed) {
2081 u8 old_retry_short, old_retry_long;
2082 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002083 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002084
2085 if (!rdev->ops->set_wiphy_params) {
2086 result = -EOPNOTSUPP;
2087 goto bad_res;
2088 }
2089
2090 old_retry_short = rdev->wiphy.retry_short;
2091 old_retry_long = rdev->wiphy.retry_long;
2092 old_frag_threshold = rdev->wiphy.frag_threshold;
2093 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002094 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002095
2096 if (changed & WIPHY_PARAM_RETRY_SHORT)
2097 rdev->wiphy.retry_short = retry_short;
2098 if (changed & WIPHY_PARAM_RETRY_LONG)
2099 rdev->wiphy.retry_long = retry_long;
2100 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2101 rdev->wiphy.frag_threshold = frag_threshold;
2102 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2103 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002104 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2105 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002106
Hila Gonene35e4d22012-06-27 17:19:42 +03002107 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002108 if (result) {
2109 rdev->wiphy.retry_short = old_retry_short;
2110 rdev->wiphy.retry_long = old_retry_long;
2111 rdev->wiphy.frag_threshold = old_frag_threshold;
2112 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002113 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002114 }
2115 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002116
Johannes Berg306d6112008-12-08 12:39:04 +01002117 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002118 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02002119 if (netdev)
2120 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002121 return result;
2122}
2123
Johannes Berg71bbc992012-06-15 15:30:18 +02002124static inline u64 wdev_id(struct wireless_dev *wdev)
2125{
2126 return (u64)wdev->identifier |
2127 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2128}
Johannes Berg55682962007-09-20 13:09:35 -04002129
Johannes Berg683b6d32012-11-08 21:25:48 +01002130static int nl80211_send_chandef(struct sk_buff *msg,
2131 struct cfg80211_chan_def *chandef)
2132{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002133 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002134
Johannes Berg683b6d32012-11-08 21:25:48 +01002135 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2136 chandef->chan->center_freq))
2137 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002138 switch (chandef->width) {
2139 case NL80211_CHAN_WIDTH_20_NOHT:
2140 case NL80211_CHAN_WIDTH_20:
2141 case NL80211_CHAN_WIDTH_40:
2142 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2143 cfg80211_get_chandef_type(chandef)))
2144 return -ENOBUFS;
2145 break;
2146 default:
2147 break;
2148 }
2149 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2150 return -ENOBUFS;
2151 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2152 return -ENOBUFS;
2153 if (chandef->center_freq2 &&
2154 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002155 return -ENOBUFS;
2156 return 0;
2157}
2158
Eric W. Biederman15e47302012-09-07 20:12:54 +00002159static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002160 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002161 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002162{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002163 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002164 void *hdr;
2165
Eric W. Biederman15e47302012-09-07 20:12:54 +00002166 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002167 if (!hdr)
2168 return -1;
2169
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002170 if (dev &&
2171 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002172 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002173 goto nla_put_failure;
2174
2175 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2176 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002177 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002178 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002179 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2180 rdev->devlist_generation ^
2181 (cfg80211_rdev_list_generation << 2)))
2182 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002183
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002184 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002185 int ret;
2186 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002187
Johannes Berg683b6d32012-11-08 21:25:48 +01002188 ret = rdev_get_channel(rdev, wdev, &chandef);
2189 if (ret == 0) {
2190 if (nl80211_send_chandef(msg, &chandef))
2191 goto nla_put_failure;
2192 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002193 }
2194
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002195 if (wdev->ssid_len) {
2196 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2197 goto nla_put_failure;
2198 }
2199
Johannes Berg55682962007-09-20 13:09:35 -04002200 return genlmsg_end(msg, hdr);
2201
2202 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002203 genlmsg_cancel(msg, hdr);
2204 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002205}
2206
2207static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2208{
2209 int wp_idx = 0;
2210 int if_idx = 0;
2211 int wp_start = cb->args[0];
2212 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002213 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002214 struct wireless_dev *wdev;
2215
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002216 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02002217 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2218 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002219 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002220 if (wp_idx < wp_start) {
2221 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002222 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002223 }
Johannes Berg55682962007-09-20 13:09:35 -04002224 if_idx = 0;
2225
Johannes Bergf5ea9122009-08-07 16:17:38 +02002226 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02002227 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002228 if (if_idx < if_start) {
2229 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002230 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002231 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002232 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002233 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002234 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002235 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002236 goto out;
2237 }
2238 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002239 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002240 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002241
2242 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002243 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002244 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002245 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002246
2247 cb->args[0] = wp_idx;
2248 cb->args[1] = if_idx;
2249
2250 return skb->len;
2251}
2252
2253static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2254{
2255 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002256 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002257 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002258
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002259 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002260 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002261 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002262
Eric W. Biederman15e47302012-09-07 20:12:54 +00002263 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002264 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002265 nlmsg_free(msg);
2266 return -ENOBUFS;
2267 }
Johannes Berg55682962007-09-20 13:09:35 -04002268
Johannes Berg134e6372009-07-10 09:51:34 +00002269 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002270}
2271
Michael Wu66f7ac52008-01-31 19:48:22 +01002272static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2273 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2274 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2275 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2276 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2277 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2278};
2279
2280static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2281{
2282 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2283 int flag;
2284
2285 *mntrflags = 0;
2286
2287 if (!nla)
2288 return -EINVAL;
2289
2290 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2291 nla, mntr_flags_policy))
2292 return -EINVAL;
2293
2294 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2295 if (flags[flag])
2296 *mntrflags |= (1<<flag);
2297
2298 return 0;
2299}
2300
Johannes Berg9bc383d2009-11-19 11:55:19 +01002301static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002302 struct net_device *netdev, u8 use_4addr,
2303 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002304{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002305 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002306 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002307 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002308 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002309 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002310
2311 switch (iftype) {
2312 case NL80211_IFTYPE_AP_VLAN:
2313 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2314 return 0;
2315 break;
2316 case NL80211_IFTYPE_STATION:
2317 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2318 return 0;
2319 break;
2320 default:
2321 break;
2322 }
2323
2324 return -EOPNOTSUPP;
2325}
2326
Johannes Berg55682962007-09-20 13:09:35 -04002327static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2328{
Johannes Berg4c476992010-10-04 21:36:35 +02002329 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002330 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002331 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002332 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002333 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002334 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002335 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002336
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002337 memset(&params, 0, sizeof(params));
2338
Johannes Berg04a773a2009-04-19 21:24:32 +02002339 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002340
Johannes Berg723b0382008-09-16 20:22:09 +02002341 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002342 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002343 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002344 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002345 if (ntype > NL80211_IFTYPE_MAX)
2346 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002347 }
2348
Johannes Berg92ffe052008-09-16 20:39:36 +02002349 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002350 struct wireless_dev *wdev = dev->ieee80211_ptr;
2351
Johannes Berg4c476992010-10-04 21:36:35 +02002352 if (ntype != NL80211_IFTYPE_MESH_POINT)
2353 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002354 if (netif_running(dev))
2355 return -EBUSY;
2356
2357 wdev_lock(wdev);
2358 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2359 IEEE80211_MAX_MESH_ID_LEN);
2360 wdev->mesh_id_up_len =
2361 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2362 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2363 wdev->mesh_id_up_len);
2364 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002365 }
2366
Felix Fietkau8b787642009-11-10 18:53:10 +01002367 if (info->attrs[NL80211_ATTR_4ADDR]) {
2368 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2369 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002370 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002371 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002372 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002373 } else {
2374 params.use_4addr = -1;
2375 }
2376
Johannes Berg92ffe052008-09-16 20:39:36 +02002377 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002378 if (ntype != NL80211_IFTYPE_MONITOR)
2379 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002380 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2381 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002382 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002383 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002384
2385 flags = &_flags;
2386 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002387 }
Johannes Berg3b858752009-03-12 09:55:09 +01002388
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002389 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002390 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002391 else
2392 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002393
Johannes Berg9bc383d2009-11-19 11:55:19 +01002394 if (!err && params.use_4addr != -1)
2395 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2396
Johannes Berg55682962007-09-20 13:09:35 -04002397 return err;
2398}
2399
2400static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2401{
Johannes Berg4c476992010-10-04 21:36:35 +02002402 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002403 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002404 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002405 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002406 int err;
2407 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002408 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002409
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002410 memset(&params, 0, sizeof(params));
2411
Johannes Berg55682962007-09-20 13:09:35 -04002412 if (!info->attrs[NL80211_ATTR_IFNAME])
2413 return -EINVAL;
2414
2415 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2416 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2417 if (type > NL80211_IFTYPE_MAX)
2418 return -EINVAL;
2419 }
2420
Johannes Berg79c97e92009-07-07 03:56:12 +02002421 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002422 !(rdev->wiphy.interface_modes & (1 << type)))
2423 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002424
Arend van Spriel1c18f142013-01-08 10:17:27 +01002425 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2426 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2427 ETH_ALEN);
2428 if (!is_valid_ether_addr(params.macaddr))
2429 return -EADDRNOTAVAIL;
2430 }
2431
Johannes Berg9bc383d2009-11-19 11:55:19 +01002432 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002433 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002434 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002435 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002436 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002437 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002438
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002439 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2440 if (!msg)
2441 return -ENOMEM;
2442
Michael Wu66f7ac52008-01-31 19:48:22 +01002443 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2444 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2445 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002446 wdev = rdev_add_virtual_intf(rdev,
2447 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2448 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002449 if (IS_ERR(wdev)) {
2450 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002451 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002452 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002453
Johannes Berg98104fde2012-06-16 00:19:54 +02002454 switch (type) {
2455 case NL80211_IFTYPE_MESH_POINT:
2456 if (!info->attrs[NL80211_ATTR_MESH_ID])
2457 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002458 wdev_lock(wdev);
2459 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2460 IEEE80211_MAX_MESH_ID_LEN);
2461 wdev->mesh_id_up_len =
2462 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2463 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2464 wdev->mesh_id_up_len);
2465 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002466 break;
2467 case NL80211_IFTYPE_P2P_DEVICE:
2468 /*
2469 * P2P Device doesn't have a netdev, so doesn't go
2470 * through the netdev notifier and must be added here
2471 */
2472 mutex_init(&wdev->mtx);
2473 INIT_LIST_HEAD(&wdev->event_list);
2474 spin_lock_init(&wdev->event_lock);
2475 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2476 spin_lock_init(&wdev->mgmt_registrations_lock);
2477
2478 mutex_lock(&rdev->devlist_mtx);
2479 wdev->identifier = ++rdev->wdev_id;
2480 list_add_rcu(&wdev->list, &rdev->wdev_list);
2481 rdev->devlist_generation++;
2482 mutex_unlock(&rdev->devlist_mtx);
2483 break;
2484 default:
2485 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002486 }
2487
Eric W. Biederman15e47302012-09-07 20:12:54 +00002488 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002489 rdev, wdev) < 0) {
2490 nlmsg_free(msg);
2491 return -ENOBUFS;
2492 }
2493
2494 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002495}
2496
2497static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2498{
Johannes Berg4c476992010-10-04 21:36:35 +02002499 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002500 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002501
Johannes Berg4c476992010-10-04 21:36:35 +02002502 if (!rdev->ops->del_virtual_intf)
2503 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002504
Johannes Berg84efbb82012-06-16 00:00:26 +02002505 /*
2506 * If we remove a wireless device without a netdev then clear
2507 * user_ptr[1] so that nl80211_post_doit won't dereference it
2508 * to check if it needs to do dev_put(). Otherwise it crashes
2509 * since the wdev has been freed, unlike with a netdev where
2510 * we need the dev_put() for the netdev to really be freed.
2511 */
2512 if (!wdev->netdev)
2513 info->user_ptr[1] = NULL;
2514
Hila Gonene35e4d22012-06-27 17:19:42 +03002515 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002516}
2517
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002518static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2519{
2520 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2521 struct net_device *dev = info->user_ptr[1];
2522 u16 noack_map;
2523
2524 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2525 return -EINVAL;
2526
2527 if (!rdev->ops->set_noack_map)
2528 return -EOPNOTSUPP;
2529
2530 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2531
Hila Gonene35e4d22012-06-27 17:19:42 +03002532 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002533}
2534
Johannes Berg41ade002007-12-19 02:03:29 +01002535struct get_key_cookie {
2536 struct sk_buff *msg;
2537 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002538 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002539};
2540
2541static void get_key_callback(void *c, struct key_params *params)
2542{
Johannes Bergb9454e82009-07-08 13:29:08 +02002543 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002544 struct get_key_cookie *cookie = c;
2545
David S. Miller9360ffd2012-03-29 04:41:26 -04002546 if ((params->key &&
2547 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2548 params->key_len, params->key)) ||
2549 (params->seq &&
2550 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2551 params->seq_len, params->seq)) ||
2552 (params->cipher &&
2553 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2554 params->cipher)))
2555 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002556
Johannes Bergb9454e82009-07-08 13:29:08 +02002557 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2558 if (!key)
2559 goto nla_put_failure;
2560
David S. Miller9360ffd2012-03-29 04:41:26 -04002561 if ((params->key &&
2562 nla_put(cookie->msg, NL80211_KEY_DATA,
2563 params->key_len, params->key)) ||
2564 (params->seq &&
2565 nla_put(cookie->msg, NL80211_KEY_SEQ,
2566 params->seq_len, params->seq)) ||
2567 (params->cipher &&
2568 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2569 params->cipher)))
2570 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002571
David S. Miller9360ffd2012-03-29 04:41:26 -04002572 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2573 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002574
2575 nla_nest_end(cookie->msg, key);
2576
Johannes Berg41ade002007-12-19 02:03:29 +01002577 return;
2578 nla_put_failure:
2579 cookie->error = 1;
2580}
2581
2582static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2583{
Johannes Berg4c476992010-10-04 21:36:35 +02002584 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002585 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002586 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002587 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002588 const u8 *mac_addr = NULL;
2589 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002590 struct get_key_cookie cookie = {
2591 .error = 0,
2592 };
2593 void *hdr;
2594 struct sk_buff *msg;
2595
2596 if (info->attrs[NL80211_ATTR_KEY_IDX])
2597 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2598
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002599 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002600 return -EINVAL;
2601
2602 if (info->attrs[NL80211_ATTR_MAC])
2603 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2604
Johannes Berge31b8212010-10-05 19:39:30 +02002605 pairwise = !!mac_addr;
2606 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2607 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2608 if (kt >= NUM_NL80211_KEYTYPES)
2609 return -EINVAL;
2610 if (kt != NL80211_KEYTYPE_GROUP &&
2611 kt != NL80211_KEYTYPE_PAIRWISE)
2612 return -EINVAL;
2613 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2614 }
2615
Johannes Berg4c476992010-10-04 21:36:35 +02002616 if (!rdev->ops->get_key)
2617 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002618
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002619 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002620 if (!msg)
2621 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002622
Eric W. Biederman15e47302012-09-07 20:12:54 +00002623 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002624 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002625 if (IS_ERR(hdr))
2626 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002627
2628 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002629 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002630
David S. Miller9360ffd2012-03-29 04:41:26 -04002631 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2632 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2633 goto nla_put_failure;
2634 if (mac_addr &&
2635 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2636 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002637
Johannes Berge31b8212010-10-05 19:39:30 +02002638 if (pairwise && mac_addr &&
2639 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2640 return -ENOENT;
2641
Hila Gonene35e4d22012-06-27 17:19:42 +03002642 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2643 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002644
2645 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002646 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002647
2648 if (cookie.error)
2649 goto nla_put_failure;
2650
2651 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002652 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002653
2654 nla_put_failure:
2655 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002656 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002657 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002658 return err;
2659}
2660
2661static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2662{
Johannes Berg4c476992010-10-04 21:36:35 +02002663 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002664 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002665 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002666 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002667
Johannes Bergb9454e82009-07-08 13:29:08 +02002668 err = nl80211_parse_key(info, &key);
2669 if (err)
2670 return err;
2671
2672 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002673 return -EINVAL;
2674
Johannes Bergb9454e82009-07-08 13:29:08 +02002675 /* only support setting default key */
2676 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002677 return -EINVAL;
2678
Johannes Bergfffd0932009-07-08 14:22:54 +02002679 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002680
2681 if (key.def) {
2682 if (!rdev->ops->set_default_key) {
2683 err = -EOPNOTSUPP;
2684 goto out;
2685 }
2686
2687 err = nl80211_key_allowed(dev->ieee80211_ptr);
2688 if (err)
2689 goto out;
2690
Hila Gonene35e4d22012-06-27 17:19:42 +03002691 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002692 key.def_uni, key.def_multi);
2693
2694 if (err)
2695 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002696
Johannes Berg3d23e342009-09-29 23:27:28 +02002697#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002698 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002699#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002700 } else {
2701 if (key.def_uni || !key.def_multi) {
2702 err = -EINVAL;
2703 goto out;
2704 }
2705
2706 if (!rdev->ops->set_default_mgmt_key) {
2707 err = -EOPNOTSUPP;
2708 goto out;
2709 }
2710
2711 err = nl80211_key_allowed(dev->ieee80211_ptr);
2712 if (err)
2713 goto out;
2714
Hila Gonene35e4d22012-06-27 17:19:42 +03002715 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002716 if (err)
2717 goto out;
2718
2719#ifdef CONFIG_CFG80211_WEXT
2720 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2721#endif
2722 }
2723
2724 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002725 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002726
Johannes Berg41ade002007-12-19 02:03:29 +01002727 return err;
2728}
2729
2730static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2731{
Johannes Berg4c476992010-10-04 21:36:35 +02002732 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002733 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002734 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002735 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002736 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002737
Johannes Bergb9454e82009-07-08 13:29:08 +02002738 err = nl80211_parse_key(info, &key);
2739 if (err)
2740 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002741
Johannes Bergb9454e82009-07-08 13:29:08 +02002742 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002743 return -EINVAL;
2744
Johannes Berg41ade002007-12-19 02:03:29 +01002745 if (info->attrs[NL80211_ATTR_MAC])
2746 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2747
Johannes Berge31b8212010-10-05 19:39:30 +02002748 if (key.type == -1) {
2749 if (mac_addr)
2750 key.type = NL80211_KEYTYPE_PAIRWISE;
2751 else
2752 key.type = NL80211_KEYTYPE_GROUP;
2753 }
2754
2755 /* for now */
2756 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2757 key.type != NL80211_KEYTYPE_GROUP)
2758 return -EINVAL;
2759
Johannes Berg4c476992010-10-04 21:36:35 +02002760 if (!rdev->ops->add_key)
2761 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002762
Johannes Berge31b8212010-10-05 19:39:30 +02002763 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2764 key.type == NL80211_KEYTYPE_PAIRWISE,
2765 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002766 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002767
2768 wdev_lock(dev->ieee80211_ptr);
2769 err = nl80211_key_allowed(dev->ieee80211_ptr);
2770 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002771 err = rdev_add_key(rdev, dev, key.idx,
2772 key.type == NL80211_KEYTYPE_PAIRWISE,
2773 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002774 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002775
Johannes Berg41ade002007-12-19 02:03:29 +01002776 return err;
2777}
2778
2779static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2780{
Johannes Berg4c476992010-10-04 21:36:35 +02002781 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002782 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002783 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002784 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002785 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002786
Johannes Bergb9454e82009-07-08 13:29:08 +02002787 err = nl80211_parse_key(info, &key);
2788 if (err)
2789 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002790
2791 if (info->attrs[NL80211_ATTR_MAC])
2792 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2793
Johannes Berge31b8212010-10-05 19:39:30 +02002794 if (key.type == -1) {
2795 if (mac_addr)
2796 key.type = NL80211_KEYTYPE_PAIRWISE;
2797 else
2798 key.type = NL80211_KEYTYPE_GROUP;
2799 }
2800
2801 /* for now */
2802 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2803 key.type != NL80211_KEYTYPE_GROUP)
2804 return -EINVAL;
2805
Johannes Berg4c476992010-10-04 21:36:35 +02002806 if (!rdev->ops->del_key)
2807 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002808
Johannes Bergfffd0932009-07-08 14:22:54 +02002809 wdev_lock(dev->ieee80211_ptr);
2810 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002811
2812 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2813 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2814 err = -ENOENT;
2815
Johannes Bergfffd0932009-07-08 14:22:54 +02002816 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002817 err = rdev_del_key(rdev, dev, key.idx,
2818 key.type == NL80211_KEYTYPE_PAIRWISE,
2819 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002820
Johannes Berg3d23e342009-09-29 23:27:28 +02002821#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002822 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002823 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002824 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002825 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002826 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2827 }
2828#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002829 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002830
Johannes Berg41ade002007-12-19 02:03:29 +01002831 return err;
2832}
2833
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302834/* This function returns an error or the number of nested attributes */
2835static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2836{
2837 struct nlattr *attr;
2838 int n_entries = 0, tmp;
2839
2840 nla_for_each_nested(attr, nl_attr, tmp) {
2841 if (nla_len(attr) != ETH_ALEN)
2842 return -EINVAL;
2843
2844 n_entries++;
2845 }
2846
2847 return n_entries;
2848}
2849
2850/*
2851 * This function parses ACL information and allocates memory for ACL data.
2852 * On successful return, the calling function is responsible to free the
2853 * ACL buffer returned by this function.
2854 */
2855static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2856 struct genl_info *info)
2857{
2858 enum nl80211_acl_policy acl_policy;
2859 struct nlattr *attr;
2860 struct cfg80211_acl_data *acl;
2861 int i = 0, n_entries, tmp;
2862
2863 if (!wiphy->max_acl_mac_addrs)
2864 return ERR_PTR(-EOPNOTSUPP);
2865
2866 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2867 return ERR_PTR(-EINVAL);
2868
2869 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2870 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2871 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2872 return ERR_PTR(-EINVAL);
2873
2874 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2875 return ERR_PTR(-EINVAL);
2876
2877 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2878 if (n_entries < 0)
2879 return ERR_PTR(n_entries);
2880
2881 if (n_entries > wiphy->max_acl_mac_addrs)
2882 return ERR_PTR(-ENOTSUPP);
2883
2884 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2885 GFP_KERNEL);
2886 if (!acl)
2887 return ERR_PTR(-ENOMEM);
2888
2889 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2890 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2891 i++;
2892 }
2893
2894 acl->n_acl_entries = n_entries;
2895 acl->acl_policy = acl_policy;
2896
2897 return acl;
2898}
2899
2900static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2901{
2902 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2903 struct net_device *dev = info->user_ptr[1];
2904 struct cfg80211_acl_data *acl;
2905 int err;
2906
2907 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2908 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2909 return -EOPNOTSUPP;
2910
2911 if (!dev->ieee80211_ptr->beacon_interval)
2912 return -EINVAL;
2913
2914 acl = parse_acl_data(&rdev->wiphy, info);
2915 if (IS_ERR(acl))
2916 return PTR_ERR(acl);
2917
2918 err = rdev_set_mac_acl(rdev, dev, acl);
2919
2920 kfree(acl);
2921
2922 return err;
2923}
2924
Johannes Berg88600202012-02-13 15:17:18 +01002925static int nl80211_parse_beacon(struct genl_info *info,
2926 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002927{
Johannes Berg88600202012-02-13 15:17:18 +01002928 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002929
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002930 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2931 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2932 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2933 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002934 return -EINVAL;
2935
Johannes Berg88600202012-02-13 15:17:18 +01002936 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002937
Johannes Berged1b6cc2007-12-19 02:03:32 +01002938 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002939 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2940 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2941 if (!bcn->head_len)
2942 return -EINVAL;
2943 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002944 }
2945
2946 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002947 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2948 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002949 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002950 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002951 }
2952
Johannes Berg4c476992010-10-04 21:36:35 +02002953 if (!haveinfo)
2954 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002955
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002956 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002957 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2958 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002959 }
2960
2961 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002962 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002963 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002964 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002965 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2966 }
2967
2968 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002969 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002970 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002971 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002972 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2973 }
2974
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002975 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002976 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002977 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002978 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002979 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2980 }
2981
Johannes Berg88600202012-02-13 15:17:18 +01002982 return 0;
2983}
2984
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002985static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2986 struct cfg80211_ap_settings *params)
2987{
2988 struct wireless_dev *wdev;
2989 bool ret = false;
2990
2991 mutex_lock(&rdev->devlist_mtx);
2992
Johannes Berg89a54e42012-06-15 14:33:17 +02002993 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002994 if (wdev->iftype != NL80211_IFTYPE_AP &&
2995 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2996 continue;
2997
Johannes Berg683b6d32012-11-08 21:25:48 +01002998 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002999 continue;
3000
Johannes Berg683b6d32012-11-08 21:25:48 +01003001 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003002 ret = true;
3003 break;
3004 }
3005
3006 mutex_unlock(&rdev->devlist_mtx);
3007
3008 return ret;
3009}
3010
Jouni Malinene39e5b52012-09-30 19:29:39 +03003011static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3012 enum nl80211_auth_type auth_type,
3013 enum nl80211_commands cmd)
3014{
3015 if (auth_type > NL80211_AUTHTYPE_MAX)
3016 return false;
3017
3018 switch (cmd) {
3019 case NL80211_CMD_AUTHENTICATE:
3020 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3021 auth_type == NL80211_AUTHTYPE_SAE)
3022 return false;
3023 return true;
3024 case NL80211_CMD_CONNECT:
3025 case NL80211_CMD_START_AP:
3026 /* SAE not supported yet */
3027 if (auth_type == NL80211_AUTHTYPE_SAE)
3028 return false;
3029 return true;
3030 default:
3031 return false;
3032 }
3033}
3034
Johannes Berg88600202012-02-13 15:17:18 +01003035static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3036{
3037 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3038 struct net_device *dev = info->user_ptr[1];
3039 struct wireless_dev *wdev = dev->ieee80211_ptr;
3040 struct cfg80211_ap_settings params;
3041 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003042 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003043
3044 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3045 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3046 return -EOPNOTSUPP;
3047
3048 if (!rdev->ops->start_ap)
3049 return -EOPNOTSUPP;
3050
3051 if (wdev->beacon_interval)
3052 return -EALREADY;
3053
3054 memset(&params, 0, sizeof(params));
3055
3056 /* these are required for START_AP */
3057 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3058 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3059 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3060 return -EINVAL;
3061
3062 err = nl80211_parse_beacon(info, &params.beacon);
3063 if (err)
3064 return err;
3065
3066 params.beacon_interval =
3067 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3068 params.dtim_period =
3069 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3070
3071 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3072 if (err)
3073 return err;
3074
3075 /*
3076 * In theory, some of these attributes should be required here
3077 * but since they were not used when the command was originally
3078 * added, keep them optional for old user space programs to let
3079 * them continue to work with drivers that do not need the
3080 * additional information -- drivers must check!
3081 */
3082 if (info->attrs[NL80211_ATTR_SSID]) {
3083 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3084 params.ssid_len =
3085 nla_len(info->attrs[NL80211_ATTR_SSID]);
3086 if (params.ssid_len == 0 ||
3087 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3088 return -EINVAL;
3089 }
3090
3091 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3092 params.hidden_ssid = nla_get_u32(
3093 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3094 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3095 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3096 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3097 return -EINVAL;
3098 }
3099
3100 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3101
3102 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3103 params.auth_type = nla_get_u32(
3104 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003105 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3106 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003107 return -EINVAL;
3108 } else
3109 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3110
3111 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3112 NL80211_MAX_NR_CIPHER_SUITES);
3113 if (err)
3114 return err;
3115
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303116 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3117 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3118 return -EOPNOTSUPP;
3119 params.inactivity_timeout = nla_get_u16(
3120 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3121 }
3122
Johannes Berg53cabad2012-11-14 15:17:28 +01003123 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3124 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3125 return -EINVAL;
3126 params.p2p_ctwindow =
3127 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3128 if (params.p2p_ctwindow > 127)
3129 return -EINVAL;
3130 if (params.p2p_ctwindow != 0 &&
3131 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3132 return -EINVAL;
3133 }
3134
3135 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3136 u8 tmp;
3137
3138 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3139 return -EINVAL;
3140 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3141 if (tmp > 1)
3142 return -EINVAL;
3143 params.p2p_opp_ps = tmp;
3144 if (params.p2p_opp_ps != 0 &&
3145 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3146 return -EINVAL;
3147 }
3148
Johannes Bergaa430da2012-05-16 23:50:18 +02003149 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003150 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3151 if (err)
3152 return err;
3153 } else if (wdev->preset_chandef.chan) {
3154 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003155 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003156 return -EINVAL;
3157
Johannes Berg683b6d32012-11-08 21:25:48 +01003158 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003159 return -EINVAL;
3160
Simon Wunderlich04f39042013-02-08 18:16:19 +01003161 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3162 if (err < 0)
3163 return err;
3164 if (err) {
3165 radar_detect_width = BIT(params.chandef.width);
3166 params.radar_required = true;
3167 }
3168
Michal Kaziore4e32452012-06-29 12:47:08 +02003169 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003170 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3171 params.chandef.chan,
3172 CHAN_MODE_SHARED,
3173 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003174 mutex_unlock(&rdev->devlist_mtx);
3175
3176 if (err)
3177 return err;
3178
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303179 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3180 params.acl = parse_acl_data(&rdev->wiphy, info);
3181 if (IS_ERR(params.acl))
3182 return PTR_ERR(params.acl);
3183 }
3184
Hila Gonene35e4d22012-06-27 17:19:42 +03003185 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003186 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003187 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003188 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003189 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003190 wdev->ssid_len = params.ssid_len;
3191 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003192 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303193
3194 kfree(params.acl);
3195
Johannes Berg56d18932011-05-09 18:41:15 +02003196 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003197}
3198
Johannes Berg88600202012-02-13 15:17:18 +01003199static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3200{
3201 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3202 struct net_device *dev = info->user_ptr[1];
3203 struct wireless_dev *wdev = dev->ieee80211_ptr;
3204 struct cfg80211_beacon_data params;
3205 int err;
3206
3207 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3208 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3209 return -EOPNOTSUPP;
3210
3211 if (!rdev->ops->change_beacon)
3212 return -EOPNOTSUPP;
3213
3214 if (!wdev->beacon_interval)
3215 return -EINVAL;
3216
3217 err = nl80211_parse_beacon(info, &params);
3218 if (err)
3219 return err;
3220
Hila Gonene35e4d22012-06-27 17:19:42 +03003221 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003222}
3223
3224static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003225{
Johannes Berg4c476992010-10-04 21:36:35 +02003226 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3227 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003228
Michal Kazior60771782012-06-29 12:46:56 +02003229 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003230}
3231
Johannes Berg5727ef12007-12-19 02:03:34 +01003232static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3233 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3234 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3235 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003236 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003237 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003238 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003239};
3240
Johannes Bergeccb8e82009-05-11 21:57:56 +03003241static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003242 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003243 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003244{
3245 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003246 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003247 int flag;
3248
Johannes Bergeccb8e82009-05-11 21:57:56 +03003249 /*
3250 * Try parsing the new attribute first so userspace
3251 * can specify both for older kernels.
3252 */
3253 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3254 if (nla) {
3255 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003256
Johannes Bergeccb8e82009-05-11 21:57:56 +03003257 sta_flags = nla_data(nla);
3258 params->sta_flags_mask = sta_flags->mask;
3259 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003260 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003261 if ((params->sta_flags_mask |
3262 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3263 return -EINVAL;
3264 return 0;
3265 }
3266
3267 /* if present, parse the old attribute */
3268
3269 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003270 if (!nla)
3271 return 0;
3272
3273 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3274 nla, sta_flags_policy))
3275 return -EINVAL;
3276
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003277 /*
3278 * Only allow certain flags for interface types so that
3279 * other attributes are silently ignored. Remember that
3280 * this is backward compatibility code with old userspace
3281 * and shouldn't be hit in other cases anyway.
3282 */
3283 switch (iftype) {
3284 case NL80211_IFTYPE_AP:
3285 case NL80211_IFTYPE_AP_VLAN:
3286 case NL80211_IFTYPE_P2P_GO:
3287 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3288 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3289 BIT(NL80211_STA_FLAG_WME) |
3290 BIT(NL80211_STA_FLAG_MFP);
3291 break;
3292 case NL80211_IFTYPE_P2P_CLIENT:
3293 case NL80211_IFTYPE_STATION:
3294 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3295 BIT(NL80211_STA_FLAG_TDLS_PEER);
3296 break;
3297 case NL80211_IFTYPE_MESH_POINT:
3298 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3299 BIT(NL80211_STA_FLAG_MFP) |
3300 BIT(NL80211_STA_FLAG_AUTHORIZED);
3301 default:
3302 return -EINVAL;
3303 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003304
Johannes Berg3383b5a2012-05-10 20:14:43 +02003305 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3306 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003307 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003308
Johannes Berg3383b5a2012-05-10 20:14:43 +02003309 /* no longer support new API additions in old API */
3310 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3311 return -EINVAL;
3312 }
3313 }
3314
Johannes Berg5727ef12007-12-19 02:03:34 +01003315 return 0;
3316}
3317
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003318static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3319 int attr)
3320{
3321 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003322 u32 bitrate;
3323 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003324
3325 rate = nla_nest_start(msg, attr);
3326 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003327 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003328
3329 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3330 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003331 /* report 16-bit bitrate only if we can */
3332 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003333 if (bitrate > 0 &&
3334 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3335 return false;
3336 if (bitrate_compat > 0 &&
3337 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3338 return false;
3339
3340 if (info->flags & RATE_INFO_FLAGS_MCS) {
3341 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3342 return false;
3343 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3344 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3345 return false;
3346 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3347 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3348 return false;
3349 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3350 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3351 return false;
3352 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3353 return false;
3354 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3355 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3356 return false;
3357 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3358 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3359 return false;
3360 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3361 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3362 return false;
3363 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3364 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3365 return false;
3366 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3367 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3368 return false;
3369 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003370
3371 nla_nest_end(msg, rate);
3372 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003373}
3374
Eric W. Biederman15e47302012-09-07 20:12:54 +00003375static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003376 int flags,
3377 struct cfg80211_registered_device *rdev,
3378 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003379 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003380{
3381 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003382 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003383
Eric W. Biederman15e47302012-09-07 20:12:54 +00003384 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003385 if (!hdr)
3386 return -1;
3387
David S. Miller9360ffd2012-03-29 04:41:26 -04003388 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3389 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3390 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3391 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003392
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003393 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3394 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003395 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003396 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3397 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3398 sinfo->connected_time))
3399 goto nla_put_failure;
3400 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3401 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3402 sinfo->inactive_time))
3403 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003404 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3405 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003406 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003407 (u32)sinfo->rx_bytes))
3408 goto nla_put_failure;
3409 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3410 NL80211_STA_INFO_TX_BYTES64)) &&
3411 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3412 (u32)sinfo->tx_bytes))
3413 goto nla_put_failure;
3414 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3415 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003416 sinfo->rx_bytes))
3417 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003418 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3419 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003420 sinfo->tx_bytes))
3421 goto nla_put_failure;
3422 if ((sinfo->filled & STATION_INFO_LLID) &&
3423 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3424 goto nla_put_failure;
3425 if ((sinfo->filled & STATION_INFO_PLID) &&
3426 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3427 goto nla_put_failure;
3428 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3429 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3430 sinfo->plink_state))
3431 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003432 switch (rdev->wiphy.signal_type) {
3433 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003434 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3435 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3436 sinfo->signal))
3437 goto nla_put_failure;
3438 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3439 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3440 sinfo->signal_avg))
3441 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003442 break;
3443 default:
3444 break;
3445 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003446 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003447 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3448 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003449 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003450 }
3451 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3452 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3453 NL80211_STA_INFO_RX_BITRATE))
3454 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003455 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003456 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3457 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3458 sinfo->rx_packets))
3459 goto nla_put_failure;
3460 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3461 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3462 sinfo->tx_packets))
3463 goto nla_put_failure;
3464 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3465 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3466 sinfo->tx_retries))
3467 goto nla_put_failure;
3468 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3469 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3470 sinfo->tx_failed))
3471 goto nla_put_failure;
3472 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3473 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3474 sinfo->beacon_loss_count))
3475 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003476 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3477 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3478 sinfo->local_pm))
3479 goto nla_put_failure;
3480 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3481 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3482 sinfo->peer_pm))
3483 goto nla_put_failure;
3484 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3485 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3486 sinfo->nonpeer_pm))
3487 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003488 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3489 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3490 if (!bss_param)
3491 goto nla_put_failure;
3492
David S. Miller9360ffd2012-03-29 04:41:26 -04003493 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3494 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3495 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3496 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3497 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3498 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3499 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3500 sinfo->bss_param.dtim_period) ||
3501 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3502 sinfo->bss_param.beacon_interval))
3503 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003504
3505 nla_nest_end(msg, bss_param);
3506 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003507 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3508 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3509 sizeof(struct nl80211_sta_flag_update),
3510 &sinfo->sta_flags))
3511 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003512 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3513 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3514 sinfo->t_offset))
3515 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003516 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003517
David S. Miller9360ffd2012-03-29 04:41:26 -04003518 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3519 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3520 sinfo->assoc_req_ies))
3521 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003522
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003523 return genlmsg_end(msg, hdr);
3524
3525 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003526 genlmsg_cancel(msg, hdr);
3527 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003528}
3529
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003530static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003531 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003532{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003533 struct station_info sinfo;
3534 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003535 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003536 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003537 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003538 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003539
Johannes Berg97990a02013-04-19 01:02:55 +02003540 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003541 if (err)
3542 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003543
Johannes Berg97990a02013-04-19 01:02:55 +02003544 if (!wdev->netdev) {
3545 err = -EINVAL;
3546 goto out_err;
3547 }
3548
Johannes Bergbba95fe2008-07-29 13:22:51 +02003549 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003550 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003551 goto out_err;
3552 }
3553
Johannes Bergbba95fe2008-07-29 13:22:51 +02003554 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003555 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003556 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003557 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003558 if (err == -ENOENT)
3559 break;
3560 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003561 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003562
3563 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003564 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003565 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003566 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003567 &sinfo) < 0)
3568 goto out;
3569
3570 sta_idx++;
3571 }
3572
3573
3574 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003575 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003576 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003577 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003578 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003579
3580 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003581}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003582
Johannes Berg5727ef12007-12-19 02:03:34 +01003583static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3584{
Johannes Berg4c476992010-10-04 21:36:35 +02003585 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3586 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003587 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003588 struct sk_buff *msg;
3589 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003590 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003591
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003592 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003593
3594 if (!info->attrs[NL80211_ATTR_MAC])
3595 return -EINVAL;
3596
3597 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3598
Johannes Berg4c476992010-10-04 21:36:35 +02003599 if (!rdev->ops->get_station)
3600 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003601
Hila Gonene35e4d22012-06-27 17:19:42 +03003602 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003603 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003604 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003605
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003606 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003607 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003608 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003609
Eric W. Biederman15e47302012-09-07 20:12:54 +00003610 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003611 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003612 nlmsg_free(msg);
3613 return -ENOBUFS;
3614 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003615
Johannes Berg4c476992010-10-04 21:36:35 +02003616 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003617}
3618
Johannes Berg77ee7c82013-02-15 00:48:33 +01003619int cfg80211_check_station_change(struct wiphy *wiphy,
3620 struct station_parameters *params,
3621 enum cfg80211_station_type statype)
3622{
3623 if (params->listen_interval != -1)
3624 return -EINVAL;
3625 if (params->aid)
3626 return -EINVAL;
3627
3628 /* When you run into this, adjust the code below for the new flag */
3629 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3630
3631 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003632 case CFG80211_STA_MESH_PEER_KERNEL:
3633 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003634 /*
3635 * No ignoring the TDLS flag here -- the userspace mesh
3636 * code doesn't have the bug of including TDLS in the
3637 * mask everywhere.
3638 */
3639 if (params->sta_flags_mask &
3640 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3641 BIT(NL80211_STA_FLAG_MFP) |
3642 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3643 return -EINVAL;
3644 break;
3645 case CFG80211_STA_TDLS_PEER_SETUP:
3646 case CFG80211_STA_TDLS_PEER_ACTIVE:
3647 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3648 return -EINVAL;
3649 /* ignore since it can't change */
3650 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3651 break;
3652 default:
3653 /* disallow mesh-specific things */
3654 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3655 return -EINVAL;
3656 if (params->local_pm)
3657 return -EINVAL;
3658 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3659 return -EINVAL;
3660 }
3661
3662 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3663 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3664 /* TDLS can't be set, ... */
3665 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3666 return -EINVAL;
3667 /*
3668 * ... but don't bother the driver with it. This works around
3669 * a hostapd/wpa_supplicant issue -- it always includes the
3670 * TLDS_PEER flag in the mask even for AP mode.
3671 */
3672 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3673 }
3674
3675 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3676 /* reject other things that can't change */
3677 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3678 return -EINVAL;
3679 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3680 return -EINVAL;
3681 if (params->supported_rates)
3682 return -EINVAL;
3683 if (params->ext_capab || params->ht_capa || params->vht_capa)
3684 return -EINVAL;
3685 }
3686
3687 if (statype != CFG80211_STA_AP_CLIENT) {
3688 if (params->vlan)
3689 return -EINVAL;
3690 }
3691
3692 switch (statype) {
3693 case CFG80211_STA_AP_MLME_CLIENT:
3694 /* Use this only for authorizing/unauthorizing a station */
3695 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3696 return -EOPNOTSUPP;
3697 break;
3698 case CFG80211_STA_AP_CLIENT:
3699 /* accept only the listed bits */
3700 if (params->sta_flags_mask &
3701 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3702 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3703 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3704 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3705 BIT(NL80211_STA_FLAG_WME) |
3706 BIT(NL80211_STA_FLAG_MFP)))
3707 return -EINVAL;
3708
3709 /* but authenticated/associated only if driver handles it */
3710 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3711 params->sta_flags_mask &
3712 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3713 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3714 return -EINVAL;
3715 break;
3716 case CFG80211_STA_IBSS:
3717 case CFG80211_STA_AP_STA:
3718 /* reject any changes other than AUTHORIZED */
3719 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3720 return -EINVAL;
3721 break;
3722 case CFG80211_STA_TDLS_PEER_SETUP:
3723 /* reject any changes other than AUTHORIZED or WME */
3724 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3725 BIT(NL80211_STA_FLAG_WME)))
3726 return -EINVAL;
3727 /* force (at least) rates when authorizing */
3728 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3729 !params->supported_rates)
3730 return -EINVAL;
3731 break;
3732 case CFG80211_STA_TDLS_PEER_ACTIVE:
3733 /* reject any changes */
3734 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003735 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003736 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3737 return -EINVAL;
3738 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003739 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003740 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3741 return -EINVAL;
3742 break;
3743 }
3744
3745 return 0;
3746}
3747EXPORT_SYMBOL(cfg80211_check_station_change);
3748
Johannes Berg5727ef12007-12-19 02:03:34 +01003749/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003750 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003751 */
Johannes Berg80b99892011-11-18 16:23:01 +01003752static struct net_device *get_vlan(struct genl_info *info,
3753 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003754{
Johannes Berg463d0182009-07-14 00:33:35 +02003755 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003756 struct net_device *v;
3757 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003758
Johannes Berg80b99892011-11-18 16:23:01 +01003759 if (!vlanattr)
3760 return NULL;
3761
3762 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3763 if (!v)
3764 return ERR_PTR(-ENODEV);
3765
3766 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3767 ret = -EINVAL;
3768 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003769 }
Johannes Berg80b99892011-11-18 16:23:01 +01003770
Johannes Berg77ee7c82013-02-15 00:48:33 +01003771 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3772 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3773 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3774 ret = -EINVAL;
3775 goto error;
3776 }
3777
Johannes Berg80b99892011-11-18 16:23:01 +01003778 if (!netif_running(v)) {
3779 ret = -ENETDOWN;
3780 goto error;
3781 }
3782
3783 return v;
3784 error:
3785 dev_put(v);
3786 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003787}
3788
Jouni Malinendf881292013-02-14 21:10:54 +02003789static struct nla_policy
3790nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3791 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3792 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3793};
3794
Johannes Bergff276692013-02-15 00:09:01 +01003795static int nl80211_parse_sta_wme(struct genl_info *info,
3796 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003797{
Jouni Malinendf881292013-02-14 21:10:54 +02003798 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3799 struct nlattr *nla;
3800 int err;
3801
Jouni Malinendf881292013-02-14 21:10:54 +02003802 /* parse WME attributes if present */
3803 if (!info->attrs[NL80211_ATTR_STA_WME])
3804 return 0;
3805
3806 nla = info->attrs[NL80211_ATTR_STA_WME];
3807 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3808 nl80211_sta_wme_policy);
3809 if (err)
3810 return err;
3811
3812 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3813 params->uapsd_queues = nla_get_u8(
3814 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3815 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3816 return -EINVAL;
3817
3818 if (tb[NL80211_STA_WME_MAX_SP])
3819 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3820
3821 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3822 return -EINVAL;
3823
3824 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3825
3826 return 0;
3827}
3828
Johannes Bergff276692013-02-15 00:09:01 +01003829static int nl80211_set_station_tdls(struct genl_info *info,
3830 struct station_parameters *params)
3831{
3832 /* Dummy STA entry gets updated once the peer capabilities are known */
3833 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3834 params->ht_capa =
3835 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3836 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3837 params->vht_capa =
3838 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3839
3840 return nl80211_parse_sta_wme(info, params);
3841}
3842
Johannes Berg5727ef12007-12-19 02:03:34 +01003843static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3844{
Johannes Berg4c476992010-10-04 21:36:35 +02003845 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003846 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003847 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003848 u8 *mac_addr;
3849 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003850
3851 memset(&params, 0, sizeof(params));
3852
3853 params.listen_interval = -1;
3854
Johannes Berg77ee7c82013-02-15 00:48:33 +01003855 if (!rdev->ops->change_station)
3856 return -EOPNOTSUPP;
3857
Johannes Berg5727ef12007-12-19 02:03:34 +01003858 if (info->attrs[NL80211_ATTR_STA_AID])
3859 return -EINVAL;
3860
3861 if (!info->attrs[NL80211_ATTR_MAC])
3862 return -EINVAL;
3863
3864 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3865
3866 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3867 params.supported_rates =
3868 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3869 params.supported_rates_len =
3870 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3871 }
3872
Jouni Malinen9d62a982013-02-14 21:10:13 +02003873 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3874 params.capability =
3875 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3876 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3877 }
3878
3879 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3880 params.ext_capab =
3881 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3882 params.ext_capab_len =
3883 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3884 }
3885
Jouni Malinendf881292013-02-14 21:10:54 +02003886 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003887 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03003888
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003889 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003890 return -EINVAL;
3891
Johannes Bergf8bacc22013-02-14 23:27:01 +01003892 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003893 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003894 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3895 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3896 return -EINVAL;
3897 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003898
Johannes Bergf8bacc22013-02-14 23:27:01 +01003899 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003900 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003901 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3902 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3903 return -EINVAL;
3904 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3905 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003906
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003907 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3908 enum nl80211_mesh_power_mode pm = nla_get_u32(
3909 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3910
3911 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3912 pm > NL80211_MESH_POWER_MAX)
3913 return -EINVAL;
3914
3915 params.local_pm = pm;
3916 }
3917
Johannes Berg77ee7c82013-02-15 00:48:33 +01003918 /* Include parameters for TDLS peer (will check later) */
3919 err = nl80211_set_station_tdls(info, &params);
3920 if (err)
3921 return err;
3922
3923 params.vlan = get_vlan(info, rdev);
3924 if (IS_ERR(params.vlan))
3925 return PTR_ERR(params.vlan);
3926
Johannes Berga97f4422009-06-18 17:23:43 +02003927 switch (dev->ieee80211_ptr->iftype) {
3928 case NL80211_IFTYPE_AP:
3929 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003930 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003931 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003932 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01003933 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02003934 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02003935 break;
3936 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003937 err = -EOPNOTSUPP;
3938 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02003939 }
3940
Johannes Berg77ee7c82013-02-15 00:48:33 +01003941 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03003942 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003943
Johannes Berg77ee7c82013-02-15 00:48:33 +01003944 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01003945 if (params.vlan)
3946 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003947
Johannes Berg5727ef12007-12-19 02:03:34 +01003948 return err;
3949}
3950
3951static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3952{
Johannes Berg4c476992010-10-04 21:36:35 +02003953 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003954 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003955 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003956 struct station_parameters params;
3957 u8 *mac_addr = NULL;
3958
3959 memset(&params, 0, sizeof(params));
3960
Johannes Berg984c3112013-02-14 23:43:25 +01003961 if (!rdev->ops->add_station)
3962 return -EOPNOTSUPP;
3963
Johannes Berg5727ef12007-12-19 02:03:34 +01003964 if (!info->attrs[NL80211_ATTR_MAC])
3965 return -EINVAL;
3966
Johannes Berg5727ef12007-12-19 02:03:34 +01003967 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3968 return -EINVAL;
3969
3970 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3971 return -EINVAL;
3972
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003973 if (!info->attrs[NL80211_ATTR_STA_AID])
3974 return -EINVAL;
3975
Johannes Berg5727ef12007-12-19 02:03:34 +01003976 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3977 params.supported_rates =
3978 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3979 params.supported_rates_len =
3980 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3981 params.listen_interval =
3982 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003983
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003984 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3985 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3986 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003987
Jouni Malinen9d62a982013-02-14 21:10:13 +02003988 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3989 params.capability =
3990 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3991 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3992 }
3993
3994 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3995 params.ext_capab =
3996 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3997 params.ext_capab_len =
3998 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3999 }
4000
Jouni Malinen36aedc902008-08-25 11:58:58 +03004001 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4002 params.ht_capa =
4003 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004004
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004005 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4006 params.vht_capa =
4007 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4008
Johannes Bergf8bacc22013-02-14 23:27:01 +01004009 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004010 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004011 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4012 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4013 return -EINVAL;
4014 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004015
Johannes Bergff276692013-02-15 00:09:01 +01004016 err = nl80211_parse_sta_wme(info, &params);
4017 if (err)
4018 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004019
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004020 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004021 return -EINVAL;
4022
Johannes Berg77ee7c82013-02-15 00:48:33 +01004023 /* When you run into this, adjust the code below for the new flag */
4024 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4025
Johannes Bergbdd90d52011-12-14 12:20:27 +01004026 switch (dev->ieee80211_ptr->iftype) {
4027 case NL80211_IFTYPE_AP:
4028 case NL80211_IFTYPE_AP_VLAN:
4029 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004030 /* ignore WME attributes if iface/sta is not capable */
4031 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4032 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4033 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004034
Johannes Bergbdd90d52011-12-14 12:20:27 +01004035 /* TDLS peers cannot be added */
4036 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004037 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004038 /* but don't bother the driver with it */
4039 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004040
Johannes Bergd582cff2012-10-26 17:53:44 +02004041 /* allow authenticated/associated only if driver handles it */
4042 if (!(rdev->wiphy.features &
4043 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4044 params.sta_flags_mask &
4045 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4046 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4047 return -EINVAL;
4048
Johannes Bergbdd90d52011-12-14 12:20:27 +01004049 /* must be last in here for error handling */
4050 params.vlan = get_vlan(info, rdev);
4051 if (IS_ERR(params.vlan))
4052 return PTR_ERR(params.vlan);
4053 break;
4054 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004055 /* ignore uAPSD data */
4056 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4057
Johannes Bergd582cff2012-10-26 17:53:44 +02004058 /* associated is disallowed */
4059 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4060 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004061 /* TDLS peers cannot be added */
4062 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004063 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004064 break;
4065 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004066 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004067 /* ignore uAPSD data */
4068 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4069
Johannes Berg77ee7c82013-02-15 00:48:33 +01004070 /* these are disallowed */
4071 if (params.sta_flags_mask &
4072 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4073 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004074 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004075 /* Only TDLS peers can be added */
4076 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4077 return -EINVAL;
4078 /* Can only add if TDLS ... */
4079 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4080 return -EOPNOTSUPP;
4081 /* ... with external setup is supported */
4082 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4083 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004084 /*
4085 * Older wpa_supplicant versions always mark the TDLS peer
4086 * as authorized, but it shouldn't yet be.
4087 */
4088 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004089 break;
4090 default:
4091 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004092 }
4093
Johannes Bergbdd90d52011-12-14 12:20:27 +01004094 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004095
Hila Gonene35e4d22012-06-27 17:19:42 +03004096 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004097
Johannes Berg5727ef12007-12-19 02:03:34 +01004098 if (params.vlan)
4099 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004100 return err;
4101}
4102
4103static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4104{
Johannes Berg4c476992010-10-04 21:36:35 +02004105 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4106 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004107 u8 *mac_addr = NULL;
4108
4109 if (info->attrs[NL80211_ATTR_MAC])
4110 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4111
Johannes Berge80cf852009-05-11 14:43:13 +02004112 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004113 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004114 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004115 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4116 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004117
Johannes Berg4c476992010-10-04 21:36:35 +02004118 if (!rdev->ops->del_station)
4119 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004120
Hila Gonene35e4d22012-06-27 17:19:42 +03004121 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004122}
4123
Eric W. Biederman15e47302012-09-07 20:12:54 +00004124static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004125 int flags, struct net_device *dev,
4126 u8 *dst, u8 *next_hop,
4127 struct mpath_info *pinfo)
4128{
4129 void *hdr;
4130 struct nlattr *pinfoattr;
4131
Eric W. Biederman15e47302012-09-07 20:12:54 +00004132 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004133 if (!hdr)
4134 return -1;
4135
David S. Miller9360ffd2012-03-29 04:41:26 -04004136 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4137 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4138 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4139 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4140 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004141
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004142 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4143 if (!pinfoattr)
4144 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004145 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4146 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4147 pinfo->frame_qlen))
4148 goto nla_put_failure;
4149 if (((pinfo->filled & MPATH_INFO_SN) &&
4150 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4151 ((pinfo->filled & MPATH_INFO_METRIC) &&
4152 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4153 pinfo->metric)) ||
4154 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4155 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4156 pinfo->exptime)) ||
4157 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4158 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4159 pinfo->flags)) ||
4160 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4161 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4162 pinfo->discovery_timeout)) ||
4163 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4164 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4165 pinfo->discovery_retries)))
4166 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004167
4168 nla_nest_end(msg, pinfoattr);
4169
4170 return genlmsg_end(msg, hdr);
4171
4172 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004173 genlmsg_cancel(msg, hdr);
4174 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004175}
4176
4177static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004178 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004179{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004180 struct mpath_info pinfo;
4181 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004182 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004183 u8 dst[ETH_ALEN];
4184 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004185 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004186 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004187
Johannes Berg97990a02013-04-19 01:02:55 +02004188 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004189 if (err)
4190 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004191
4192 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004193 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004194 goto out_err;
4195 }
4196
Johannes Berg97990a02013-04-19 01:02:55 +02004197 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004198 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004199 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004200 }
4201
Johannes Bergbba95fe2008-07-29 13:22:51 +02004202 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004203 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4204 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004205 if (err == -ENOENT)
4206 break;
4207 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004208 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004209
Eric W. Biederman15e47302012-09-07 20:12:54 +00004210 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004211 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004212 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004213 &pinfo) < 0)
4214 goto out;
4215
4216 path_idx++;
4217 }
4218
4219
4220 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004221 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004222 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004223 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004224 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004225 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004226}
4227
4228static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4229{
Johannes Berg4c476992010-10-04 21:36:35 +02004230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004231 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004232 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004233 struct mpath_info pinfo;
4234 struct sk_buff *msg;
4235 u8 *dst = NULL;
4236 u8 next_hop[ETH_ALEN];
4237
4238 memset(&pinfo, 0, sizeof(pinfo));
4239
4240 if (!info->attrs[NL80211_ATTR_MAC])
4241 return -EINVAL;
4242
4243 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4244
Johannes Berg4c476992010-10-04 21:36:35 +02004245 if (!rdev->ops->get_mpath)
4246 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004247
Johannes Berg4c476992010-10-04 21:36:35 +02004248 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4249 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004250
Hila Gonene35e4d22012-06-27 17:19:42 +03004251 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004252 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004253 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004254
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004255 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004256 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004257 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004258
Eric W. Biederman15e47302012-09-07 20:12:54 +00004259 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004260 dev, dst, next_hop, &pinfo) < 0) {
4261 nlmsg_free(msg);
4262 return -ENOBUFS;
4263 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004264
Johannes Berg4c476992010-10-04 21:36:35 +02004265 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004266}
4267
4268static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4269{
Johannes Berg4c476992010-10-04 21:36:35 +02004270 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4271 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004272 u8 *dst = NULL;
4273 u8 *next_hop = NULL;
4274
4275 if (!info->attrs[NL80211_ATTR_MAC])
4276 return -EINVAL;
4277
4278 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4279 return -EINVAL;
4280
4281 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4282 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4283
Johannes Berg4c476992010-10-04 21:36:35 +02004284 if (!rdev->ops->change_mpath)
4285 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004286
Johannes Berg4c476992010-10-04 21:36:35 +02004287 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4288 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004289
Hila Gonene35e4d22012-06-27 17:19:42 +03004290 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004291}
Johannes Berg4c476992010-10-04 21:36:35 +02004292
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004293static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4294{
Johannes Berg4c476992010-10-04 21:36:35 +02004295 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4296 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004297 u8 *dst = NULL;
4298 u8 *next_hop = NULL;
4299
4300 if (!info->attrs[NL80211_ATTR_MAC])
4301 return -EINVAL;
4302
4303 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4304 return -EINVAL;
4305
4306 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4307 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4308
Johannes Berg4c476992010-10-04 21:36:35 +02004309 if (!rdev->ops->add_mpath)
4310 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004311
Johannes Berg4c476992010-10-04 21:36:35 +02004312 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4313 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004314
Hila Gonene35e4d22012-06-27 17:19:42 +03004315 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004316}
4317
4318static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4319{
Johannes Berg4c476992010-10-04 21:36:35 +02004320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4321 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004322 u8 *dst = NULL;
4323
4324 if (info->attrs[NL80211_ATTR_MAC])
4325 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4326
Johannes Berg4c476992010-10-04 21:36:35 +02004327 if (!rdev->ops->del_mpath)
4328 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004329
Hila Gonene35e4d22012-06-27 17:19:42 +03004330 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004331}
4332
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004333static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4334{
Johannes Berg4c476992010-10-04 21:36:35 +02004335 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4336 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004337 struct bss_parameters params;
4338
4339 memset(&params, 0, sizeof(params));
4340 /* default to not changing parameters */
4341 params.use_cts_prot = -1;
4342 params.use_short_preamble = -1;
4343 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004344 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004345 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004346 params.p2p_ctwindow = -1;
4347 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004348
4349 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4350 params.use_cts_prot =
4351 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4352 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4353 params.use_short_preamble =
4354 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4355 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4356 params.use_short_slot_time =
4357 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004358 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4359 params.basic_rates =
4360 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4361 params.basic_rates_len =
4362 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4363 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004364 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4365 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004366 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4367 params.ht_opmode =
4368 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004369
Johannes Berg53cabad2012-11-14 15:17:28 +01004370 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4371 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4372 return -EINVAL;
4373 params.p2p_ctwindow =
4374 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4375 if (params.p2p_ctwindow < 0)
4376 return -EINVAL;
4377 if (params.p2p_ctwindow != 0 &&
4378 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4379 return -EINVAL;
4380 }
4381
4382 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4383 u8 tmp;
4384
4385 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4386 return -EINVAL;
4387 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4388 if (tmp > 1)
4389 return -EINVAL;
4390 params.p2p_opp_ps = tmp;
4391 if (params.p2p_opp_ps &&
4392 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4393 return -EINVAL;
4394 }
4395
Johannes Berg4c476992010-10-04 21:36:35 +02004396 if (!rdev->ops->change_bss)
4397 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004398
Johannes Berg074ac8d2010-09-16 14:58:22 +02004399 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004400 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4401 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004402
Hila Gonene35e4d22012-06-27 17:19:42 +03004403 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004404}
4405
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004406static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004407 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4408 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4409 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4410 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4411 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4412 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4413};
4414
4415static int parse_reg_rule(struct nlattr *tb[],
4416 struct ieee80211_reg_rule *reg_rule)
4417{
4418 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4419 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4420
4421 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4422 return -EINVAL;
4423 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4424 return -EINVAL;
4425 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4426 return -EINVAL;
4427 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4428 return -EINVAL;
4429 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4430 return -EINVAL;
4431
4432 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4433
4434 freq_range->start_freq_khz =
4435 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4436 freq_range->end_freq_khz =
4437 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4438 freq_range->max_bandwidth_khz =
4439 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4440
4441 power_rule->max_eirp =
4442 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4443
4444 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4445 power_rule->max_antenna_gain =
4446 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4447
4448 return 0;
4449}
4450
4451static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4452{
4453 int r;
4454 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004455 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004456
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004457 /*
4458 * You should only get this when cfg80211 hasn't yet initialized
4459 * completely when built-in to the kernel right between the time
4460 * window between nl80211_init() and regulatory_init(), if that is
4461 * even possible.
4462 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004463 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004464 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004465
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004466 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4467 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004468
4469 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4470
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004471 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4472 user_reg_hint_type =
4473 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4474 else
4475 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4476
4477 switch (user_reg_hint_type) {
4478 case NL80211_USER_REG_HINT_USER:
4479 case NL80211_USER_REG_HINT_CELL_BASE:
4480 break;
4481 default:
4482 return -EINVAL;
4483 }
4484
4485 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004486
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004487 return r;
4488}
4489
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004490static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004491 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004492{
Johannes Berg4c476992010-10-04 21:36:35 +02004493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004494 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004495 struct wireless_dev *wdev = dev->ieee80211_ptr;
4496 struct mesh_config cur_params;
4497 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004498 void *hdr;
4499 struct nlattr *pinfoattr;
4500 struct sk_buff *msg;
4501
Johannes Berg29cbe682010-12-03 09:20:44 +01004502 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4503 return -EOPNOTSUPP;
4504
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004505 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004506 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004507
Johannes Berg29cbe682010-12-03 09:20:44 +01004508 wdev_lock(wdev);
4509 /* If not connected, get default parameters */
4510 if (!wdev->mesh_id_len)
4511 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4512 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004513 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004514 wdev_unlock(wdev);
4515
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004516 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004517 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004518
4519 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004520 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004521 if (!msg)
4522 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004523 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004524 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004525 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004526 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004527 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004528 if (!pinfoattr)
4529 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004530 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4531 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4532 cur_params.dot11MeshRetryTimeout) ||
4533 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4534 cur_params.dot11MeshConfirmTimeout) ||
4535 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4536 cur_params.dot11MeshHoldingTimeout) ||
4537 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4538 cur_params.dot11MeshMaxPeerLinks) ||
4539 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4540 cur_params.dot11MeshMaxRetries) ||
4541 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4542 cur_params.dot11MeshTTL) ||
4543 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4544 cur_params.element_ttl) ||
4545 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4546 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004547 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4548 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004549 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4550 cur_params.dot11MeshHWMPmaxPREQretries) ||
4551 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4552 cur_params.path_refresh_time) ||
4553 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4554 cur_params.min_discovery_timeout) ||
4555 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4556 cur_params.dot11MeshHWMPactivePathTimeout) ||
4557 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4558 cur_params.dot11MeshHWMPpreqMinInterval) ||
4559 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4560 cur_params.dot11MeshHWMPperrMinInterval) ||
4561 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4562 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4563 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4564 cur_params.dot11MeshHWMPRootMode) ||
4565 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4566 cur_params.dot11MeshHWMPRannInterval) ||
4567 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4568 cur_params.dot11MeshGateAnnouncementProtocol) ||
4569 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4570 cur_params.dot11MeshForwarding) ||
4571 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004572 cur_params.rssi_threshold) ||
4573 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004574 cur_params.ht_opmode) ||
4575 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4576 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4577 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004578 cur_params.dot11MeshHWMProotInterval) ||
4579 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004580 cur_params.dot11MeshHWMPconfirmationInterval) ||
4581 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4582 cur_params.power_mode) ||
4583 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4584 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004585 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004586 nla_nest_end(msg, pinfoattr);
4587 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004588 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004589
Johannes Berg3b858752009-03-12 09:55:09 +01004590 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004591 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004592 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004593 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004594 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004595}
4596
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004597static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004598 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4599 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4600 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4601 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4602 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4603 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004604 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004605 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004606 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004607 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4608 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4609 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4610 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4611 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004612 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004613 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004614 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004615 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004616 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004617 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004618 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4619 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004620 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4621 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004622 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004623 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4624 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004625};
4626
Javier Cardonac80d5452010-12-16 17:37:49 -08004627static const struct nla_policy
4628 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004629 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004630 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4631 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004632 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004633 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004634 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004635 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004636 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004637};
4638
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004639static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004640 struct mesh_config *cfg,
4641 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004642{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004643 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004644 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004645
Marco Porschea54fba2013-01-07 16:04:48 +01004646#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4647do { \
4648 if (tb[attr]) { \
4649 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4650 return -EINVAL; \
4651 cfg->param = fn(tb[attr]); \
4652 mask |= (1 << (attr - 1)); \
4653 } \
4654} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004655
4656
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004657 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004658 return -EINVAL;
4659 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004660 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004661 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004662 return -EINVAL;
4663
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004664 /* This makes sure that there aren't more than 32 mesh config
4665 * parameters (otherwise our bitfield scheme would not work.) */
4666 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4667
4668 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004669 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004670 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4671 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004672 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004673 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4674 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004675 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004676 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4677 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004678 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004679 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4680 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004681 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004682 mask, NL80211_MESHCONF_MAX_RETRIES,
4683 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004684 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004685 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004686 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004687 mask, NL80211_MESHCONF_ELEMENT_TTL,
4688 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004689 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004690 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4691 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004692 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4693 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004694 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4695 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004696 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004697 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4698 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004699 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004700 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4701 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004702 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004703 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4704 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004705 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4706 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004707 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4708 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004709 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004710 1, 65535, mask,
4711 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004712 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004713 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004714 1, 65535, mask,
4715 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004716 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004717 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004718 dot11MeshHWMPnetDiameterTraversalTime,
4719 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004720 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4721 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004722 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4723 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4724 nla_get_u8);
4725 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4726 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004727 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004728 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004729 dot11MeshGateAnnouncementProtocol, 0, 1,
4730 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004731 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004732 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004733 mask, NL80211_MESHCONF_FORWARDING,
4734 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004735 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004736 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4737 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004738 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004739 mask, NL80211_MESHCONF_HT_OPMODE,
4740 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004741 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004742 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004743 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4744 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004745 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004746 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4747 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004748 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004749 dot11MeshHWMPconfirmationInterval,
4750 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004751 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4752 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004753 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4754 NL80211_MESH_POWER_ACTIVE,
4755 NL80211_MESH_POWER_MAX,
4756 mask, NL80211_MESHCONF_POWER_MODE,
4757 nla_get_u32);
4758 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4759 0, 65535, mask,
4760 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004761 if (mask_out)
4762 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004763
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004764 return 0;
4765
4766#undef FILL_IN_MESH_PARAM_IF_SET
4767}
4768
Javier Cardonac80d5452010-12-16 17:37:49 -08004769static int nl80211_parse_mesh_setup(struct genl_info *info,
4770 struct mesh_setup *setup)
4771{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004772 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004773 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4774
4775 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4776 return -EINVAL;
4777 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4778 info->attrs[NL80211_ATTR_MESH_SETUP],
4779 nl80211_mesh_setup_params_policy))
4780 return -EINVAL;
4781
Javier Cardonad299a1f2012-03-31 11:31:33 -07004782 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4783 setup->sync_method =
4784 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4785 IEEE80211_SYNC_METHOD_VENDOR :
4786 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4787
Javier Cardonac80d5452010-12-16 17:37:49 -08004788 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4789 setup->path_sel_proto =
4790 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4791 IEEE80211_PATH_PROTOCOL_VENDOR :
4792 IEEE80211_PATH_PROTOCOL_HWMP;
4793
4794 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4795 setup->path_metric =
4796 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4797 IEEE80211_PATH_METRIC_VENDOR :
4798 IEEE80211_PATH_METRIC_AIRTIME;
4799
Javier Cardona581a8b02011-04-07 15:08:27 -07004800
4801 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004802 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004803 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004804 if (!is_valid_ie_attr(ieattr))
4805 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004806 setup->ie = nla_data(ieattr);
4807 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004808 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004809 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4810 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4811 return -EINVAL;
4812 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004813 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4814 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004815 if (setup->is_secure)
4816 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08004817
4818 return 0;
4819}
4820
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004821static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004822 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004823{
4824 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4825 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004826 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004827 struct mesh_config cfg;
4828 u32 mask;
4829 int err;
4830
Johannes Berg29cbe682010-12-03 09:20:44 +01004831 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4832 return -EOPNOTSUPP;
4833
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004834 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004835 return -EOPNOTSUPP;
4836
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004837 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004838 if (err)
4839 return err;
4840
Johannes Berg29cbe682010-12-03 09:20:44 +01004841 wdev_lock(wdev);
4842 if (!wdev->mesh_id_len)
4843 err = -ENOLINK;
4844
4845 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004846 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004847
4848 wdev_unlock(wdev);
4849
4850 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004851}
4852
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004853static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4854{
Johannes Berg458f4f92012-12-06 15:47:38 +01004855 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004856 struct sk_buff *msg;
4857 void *hdr = NULL;
4858 struct nlattr *nl_reg_rules;
4859 unsigned int i;
4860 int err = -EINVAL;
4861
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004862 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004863
4864 if (!cfg80211_regdomain)
4865 goto out;
4866
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004867 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004868 if (!msg) {
4869 err = -ENOBUFS;
4870 goto out;
4871 }
4872
Eric W. Biederman15e47302012-09-07 20:12:54 +00004873 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004874 NL80211_CMD_GET_REG);
4875 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004876 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004877
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004878 if (reg_last_request_cell_base() &&
4879 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4880 NL80211_USER_REG_HINT_CELL_BASE))
4881 goto nla_put_failure;
4882
Johannes Berg458f4f92012-12-06 15:47:38 +01004883 rcu_read_lock();
4884 regdom = rcu_dereference(cfg80211_regdomain);
4885
4886 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4887 (regdom->dfs_region &&
4888 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4889 goto nla_put_failure_rcu;
4890
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004891 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4892 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004893 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004894
Johannes Berg458f4f92012-12-06 15:47:38 +01004895 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004896 struct nlattr *nl_reg_rule;
4897 const struct ieee80211_reg_rule *reg_rule;
4898 const struct ieee80211_freq_range *freq_range;
4899 const struct ieee80211_power_rule *power_rule;
4900
Johannes Berg458f4f92012-12-06 15:47:38 +01004901 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004902 freq_range = &reg_rule->freq_range;
4903 power_rule = &reg_rule->power_rule;
4904
4905 nl_reg_rule = nla_nest_start(msg, i);
4906 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004907 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004908
David S. Miller9360ffd2012-03-29 04:41:26 -04004909 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4910 reg_rule->flags) ||
4911 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4912 freq_range->start_freq_khz) ||
4913 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4914 freq_range->end_freq_khz) ||
4915 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4916 freq_range->max_bandwidth_khz) ||
4917 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4918 power_rule->max_antenna_gain) ||
4919 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4920 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004921 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004922
4923 nla_nest_end(msg, nl_reg_rule);
4924 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004925 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004926
4927 nla_nest_end(msg, nl_reg_rules);
4928
4929 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004930 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004931 goto out;
4932
Johannes Berg458f4f92012-12-06 15:47:38 +01004933nla_put_failure_rcu:
4934 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004935nla_put_failure:
4936 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004937put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004938 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004939 err = -EMSGSIZE;
4940out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004941 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004942 return err;
4943}
4944
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004945static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4946{
4947 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4948 struct nlattr *nl_reg_rule;
4949 char *alpha2 = NULL;
4950 int rem_reg_rules = 0, r = 0;
4951 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004952 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004953 struct ieee80211_regdomain *rd = NULL;
4954
4955 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4956 return -EINVAL;
4957
4958 if (!info->attrs[NL80211_ATTR_REG_RULES])
4959 return -EINVAL;
4960
4961 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4962
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004963 if (info->attrs[NL80211_ATTR_DFS_REGION])
4964 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4965
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004966 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004967 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004968 num_rules++;
4969 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004970 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004971 }
4972
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004973 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004974 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004975
4976 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004977 if (!rd)
4978 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004979
4980 rd->n_reg_rules = num_rules;
4981 rd->alpha2[0] = alpha2[0];
4982 rd->alpha2[1] = alpha2[1];
4983
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004984 /*
4985 * Disable DFS master mode if the DFS region was
4986 * not supported or known on this kernel.
4987 */
4988 if (reg_supported_dfs_region(dfs_region))
4989 rd->dfs_region = dfs_region;
4990
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004991 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004992 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004993 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004994 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4995 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004996 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4997 if (r)
4998 goto bad_reg;
4999
5000 rule_idx++;
5001
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005002 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5003 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005004 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005005 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005006 }
5007
Johannes Berg6913b492012-12-04 00:48:59 +01005008 mutex_lock(&cfg80211_mutex);
5009
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005010 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005011 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005012 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01005013 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005014
Johannes Bergd2372b32008-10-24 20:32:20 +02005015 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005016 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005017 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005018}
5019
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005020static int validate_scan_freqs(struct nlattr *freqs)
5021{
5022 struct nlattr *attr1, *attr2;
5023 int n_channels = 0, tmp1, tmp2;
5024
5025 nla_for_each_nested(attr1, freqs, tmp1) {
5026 n_channels++;
5027 /*
5028 * Some hardware has a limited channel list for
5029 * scanning, and it is pretty much nonsensical
5030 * to scan for a channel twice, so disallow that
5031 * and don't require drivers to check that the
5032 * channel list they get isn't longer than what
5033 * they can scan, as long as they can scan all
5034 * the channels they registered at once.
5035 */
5036 nla_for_each_nested(attr2, freqs, tmp2)
5037 if (attr1 != attr2 &&
5038 nla_get_u32(attr1) == nla_get_u32(attr2))
5039 return 0;
5040 }
5041
5042 return n_channels;
5043}
5044
Johannes Berg2a519312009-02-10 21:25:55 +01005045static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5046{
Johannes Berg4c476992010-10-04 21:36:35 +02005047 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005048 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005049 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005050 struct nlattr *attr;
5051 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005052 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005053 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005054
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005055 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5056 return -EINVAL;
5057
Johannes Berg79c97e92009-07-07 03:56:12 +02005058 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005059
Johannes Berg4c476992010-10-04 21:36:35 +02005060 if (!rdev->ops->scan)
5061 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005062
Johannes Berg4c476992010-10-04 21:36:35 +02005063 if (rdev->scan_req)
5064 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01005065
5066 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005067 n_channels = validate_scan_freqs(
5068 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02005069 if (!n_channels)
5070 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01005071 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005072 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005073 n_channels = 0;
5074
Johannes Berg2a519312009-02-10 21:25:55 +01005075 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5076 if (wiphy->bands[band])
5077 n_channels += wiphy->bands[band]->n_channels;
5078 }
5079
5080 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5081 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5082 n_ssids++;
5083
Johannes Berg4c476992010-10-04 21:36:35 +02005084 if (n_ssids > wiphy->max_scan_ssids)
5085 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01005086
Jouni Malinen70692ad2009-02-16 19:39:13 +02005087 if (info->attrs[NL80211_ATTR_IE])
5088 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5089 else
5090 ie_len = 0;
5091
Johannes Berg4c476992010-10-04 21:36:35 +02005092 if (ie_len > wiphy->max_scan_ie_len)
5093 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02005094
Johannes Berg2a519312009-02-10 21:25:55 +01005095 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005096 + sizeof(*request->ssids) * n_ssids
5097 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005098 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005099 if (!request)
5100 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01005101
Johannes Berg2a519312009-02-10 21:25:55 +01005102 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005103 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005104 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005105 if (ie_len) {
5106 if (request->ssids)
5107 request->ie = (void *)(request->ssids + n_ssids);
5108 else
5109 request->ie = (void *)(request->channels + n_channels);
5110 }
Johannes Berg2a519312009-02-10 21:25:55 +01005111
Johannes Berg584991d2009-11-02 13:32:03 +01005112 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005113 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5114 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005115 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005116 struct ieee80211_channel *chan;
5117
5118 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5119
5120 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005121 err = -EINVAL;
5122 goto out_free;
5123 }
Johannes Berg584991d2009-11-02 13:32:03 +01005124
5125 /* ignore disabled channels */
5126 if (chan->flags & IEEE80211_CHAN_DISABLED)
5127 continue;
5128
5129 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005130 i++;
5131 }
5132 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005133 enum ieee80211_band band;
5134
Johannes Berg2a519312009-02-10 21:25:55 +01005135 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005136 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5137 int j;
5138 if (!wiphy->bands[band])
5139 continue;
5140 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005141 struct ieee80211_channel *chan;
5142
5143 chan = &wiphy->bands[band]->channels[j];
5144
5145 if (chan->flags & IEEE80211_CHAN_DISABLED)
5146 continue;
5147
5148 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005149 i++;
5150 }
5151 }
5152 }
5153
Johannes Berg584991d2009-11-02 13:32:03 +01005154 if (!i) {
5155 err = -EINVAL;
5156 goto out_free;
5157 }
5158
5159 request->n_channels = i;
5160
Johannes Berg2a519312009-02-10 21:25:55 +01005161 i = 0;
5162 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5163 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005164 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005165 err = -EINVAL;
5166 goto out_free;
5167 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005168 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005169 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005170 i++;
5171 }
5172 }
5173
Jouni Malinen70692ad2009-02-16 19:39:13 +02005174 if (info->attrs[NL80211_ATTR_IE]) {
5175 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005176 memcpy((void *)request->ie,
5177 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005178 request->ie_len);
5179 }
5180
Johannes Berg34850ab2011-07-18 18:08:35 +02005181 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005182 if (wiphy->bands[i])
5183 request->rates[i] =
5184 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005185
5186 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5187 nla_for_each_nested(attr,
5188 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5189 tmp) {
5190 enum ieee80211_band band = nla_type(attr);
5191
Dan Carpenter84404622011-07-29 11:52:18 +03005192 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005193 err = -EINVAL;
5194 goto out_free;
5195 }
5196 err = ieee80211_get_ratemask(wiphy->bands[band],
5197 nla_data(attr),
5198 nla_len(attr),
5199 &request->rates[band]);
5200 if (err)
5201 goto out_free;
5202 }
5203 }
5204
Sam Leffler46856bb2012-10-11 21:03:32 -07005205 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005206 request->flags = nla_get_u32(
5207 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005208 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5209 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5210 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5211 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005212 err = -EOPNOTSUPP;
5213 goto out_free;
5214 }
5215 }
Sam Lefflered4737712012-10-11 21:03:31 -07005216
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305217 request->no_cck =
5218 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5219
Johannes Bergfd014282012-06-18 19:17:03 +02005220 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005221 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005222 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005223
Johannes Berg79c97e92009-07-07 03:56:12 +02005224 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005225 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005226
Johannes Berg463d0182009-07-14 00:33:35 +02005227 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005228 nl80211_send_scan_start(rdev, wdev);
5229 if (wdev->netdev)
5230 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005231 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005232 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005233 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005234 kfree(request);
5235 }
Johannes Berg3b858752009-03-12 09:55:09 +01005236
Johannes Berg2a519312009-02-10 21:25:55 +01005237 return err;
5238}
5239
Luciano Coelho807f8a82011-05-11 17:09:35 +03005240static int nl80211_start_sched_scan(struct sk_buff *skb,
5241 struct genl_info *info)
5242{
5243 struct cfg80211_sched_scan_request *request;
5244 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5245 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005246 struct nlattr *attr;
5247 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005248 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005249 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005250 enum ieee80211_band band;
5251 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005252 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005253
5254 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5255 !rdev->ops->sched_scan_start)
5256 return -EOPNOTSUPP;
5257
5258 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5259 return -EINVAL;
5260
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005261 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5262 return -EINVAL;
5263
5264 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5265 if (interval == 0)
5266 return -EINVAL;
5267
Luciano Coelho807f8a82011-05-11 17:09:35 +03005268 wiphy = &rdev->wiphy;
5269
5270 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5271 n_channels = validate_scan_freqs(
5272 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5273 if (!n_channels)
5274 return -EINVAL;
5275 } else {
5276 n_channels = 0;
5277
5278 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5279 if (wiphy->bands[band])
5280 n_channels += wiphy->bands[band]->n_channels;
5281 }
5282
5283 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5284 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5285 tmp)
5286 n_ssids++;
5287
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005288 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005289 return -EINVAL;
5290
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005291 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5292 nla_for_each_nested(attr,
5293 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5294 tmp)
5295 n_match_sets++;
5296
5297 if (n_match_sets > wiphy->max_match_sets)
5298 return -EINVAL;
5299
Luciano Coelho807f8a82011-05-11 17:09:35 +03005300 if (info->attrs[NL80211_ATTR_IE])
5301 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5302 else
5303 ie_len = 0;
5304
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005305 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005306 return -EINVAL;
5307
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005308 mutex_lock(&rdev->sched_scan_mtx);
5309
5310 if (rdev->sched_scan_req) {
5311 err = -EINPROGRESS;
5312 goto out;
5313 }
5314
Luciano Coelho807f8a82011-05-11 17:09:35 +03005315 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005316 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005317 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005318 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005319 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005320 if (!request) {
5321 err = -ENOMEM;
5322 goto out;
5323 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005324
5325 if (n_ssids)
5326 request->ssids = (void *)&request->channels[n_channels];
5327 request->n_ssids = n_ssids;
5328 if (ie_len) {
5329 if (request->ssids)
5330 request->ie = (void *)(request->ssids + n_ssids);
5331 else
5332 request->ie = (void *)(request->channels + n_channels);
5333 }
5334
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005335 if (n_match_sets) {
5336 if (request->ie)
5337 request->match_sets = (void *)(request->ie + ie_len);
5338 else if (request->ssids)
5339 request->match_sets =
5340 (void *)(request->ssids + n_ssids);
5341 else
5342 request->match_sets =
5343 (void *)(request->channels + n_channels);
5344 }
5345 request->n_match_sets = n_match_sets;
5346
Luciano Coelho807f8a82011-05-11 17:09:35 +03005347 i = 0;
5348 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5349 /* user specified, bail out if channel not found */
5350 nla_for_each_nested(attr,
5351 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5352 tmp) {
5353 struct ieee80211_channel *chan;
5354
5355 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5356
5357 if (!chan) {
5358 err = -EINVAL;
5359 goto out_free;
5360 }
5361
5362 /* ignore disabled channels */
5363 if (chan->flags & IEEE80211_CHAN_DISABLED)
5364 continue;
5365
5366 request->channels[i] = chan;
5367 i++;
5368 }
5369 } else {
5370 /* all channels */
5371 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5372 int j;
5373 if (!wiphy->bands[band])
5374 continue;
5375 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5376 struct ieee80211_channel *chan;
5377
5378 chan = &wiphy->bands[band]->channels[j];
5379
5380 if (chan->flags & IEEE80211_CHAN_DISABLED)
5381 continue;
5382
5383 request->channels[i] = chan;
5384 i++;
5385 }
5386 }
5387 }
5388
5389 if (!i) {
5390 err = -EINVAL;
5391 goto out_free;
5392 }
5393
5394 request->n_channels = i;
5395
5396 i = 0;
5397 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5398 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5399 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005400 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005401 err = -EINVAL;
5402 goto out_free;
5403 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005404 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005405 memcpy(request->ssids[i].ssid, nla_data(attr),
5406 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005407 i++;
5408 }
5409 }
5410
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005411 i = 0;
5412 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5413 nla_for_each_nested(attr,
5414 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5415 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005416 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005417
5418 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5419 nla_data(attr), nla_len(attr),
5420 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005421 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005422 if (ssid) {
5423 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5424 err = -EINVAL;
5425 goto out_free;
5426 }
5427 memcpy(request->match_sets[i].ssid.ssid,
5428 nla_data(ssid), nla_len(ssid));
5429 request->match_sets[i].ssid.ssid_len =
5430 nla_len(ssid);
5431 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005432 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5433 if (rssi)
5434 request->rssi_thold = nla_get_u32(rssi);
5435 else
5436 request->rssi_thold =
5437 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005438 i++;
5439 }
5440 }
5441
Luciano Coelho807f8a82011-05-11 17:09:35 +03005442 if (info->attrs[NL80211_ATTR_IE]) {
5443 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5444 memcpy((void *)request->ie,
5445 nla_data(info->attrs[NL80211_ATTR_IE]),
5446 request->ie_len);
5447 }
5448
Sam Leffler46856bb2012-10-11 21:03:32 -07005449 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005450 request->flags = nla_get_u32(
5451 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005452 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5453 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5454 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5455 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005456 err = -EOPNOTSUPP;
5457 goto out_free;
5458 }
5459 }
Sam Lefflered4737712012-10-11 21:03:31 -07005460
Luciano Coelho807f8a82011-05-11 17:09:35 +03005461 request->dev = dev;
5462 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005463 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005464 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005465
Hila Gonene35e4d22012-06-27 17:19:42 +03005466 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005467 if (!err) {
5468 rdev->sched_scan_req = request;
5469 nl80211_send_sched_scan(rdev, dev,
5470 NL80211_CMD_START_SCHED_SCAN);
5471 goto out;
5472 }
5473
5474out_free:
5475 kfree(request);
5476out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005477 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005478 return err;
5479}
5480
5481static int nl80211_stop_sched_scan(struct sk_buff *skb,
5482 struct genl_info *info)
5483{
5484 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005485 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005486
5487 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5488 !rdev->ops->sched_scan_stop)
5489 return -EOPNOTSUPP;
5490
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005491 mutex_lock(&rdev->sched_scan_mtx);
5492 err = __cfg80211_stop_sched_scan(rdev, false);
5493 mutex_unlock(&rdev->sched_scan_mtx);
5494
5495 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005496}
5497
Simon Wunderlich04f39042013-02-08 18:16:19 +01005498static int nl80211_start_radar_detection(struct sk_buff *skb,
5499 struct genl_info *info)
5500{
5501 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5502 struct net_device *dev = info->user_ptr[1];
5503 struct wireless_dev *wdev = dev->ieee80211_ptr;
5504 struct cfg80211_chan_def chandef;
5505 int err;
5506
5507 err = nl80211_parse_chandef(rdev, info, &chandef);
5508 if (err)
5509 return err;
5510
5511 if (wdev->cac_started)
5512 return -EBUSY;
5513
5514 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5515 if (err < 0)
5516 return err;
5517
5518 if (err == 0)
5519 return -EINVAL;
5520
5521 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5522 return -EINVAL;
5523
5524 if (!rdev->ops->start_radar_detection)
5525 return -EOPNOTSUPP;
5526
5527 mutex_lock(&rdev->devlist_mtx);
5528 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5529 chandef.chan, CHAN_MODE_SHARED,
5530 BIT(chandef.width));
5531 if (err)
5532 goto err_locked;
5533
5534 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5535 if (!err) {
5536 wdev->channel = chandef.chan;
5537 wdev->cac_started = true;
5538 wdev->cac_start_time = jiffies;
5539 }
5540err_locked:
5541 mutex_unlock(&rdev->devlist_mtx);
5542
5543 return err;
5544}
5545
Johannes Berg9720bb32011-06-21 09:45:33 +02005546static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5547 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005548 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005549 struct wireless_dev *wdev,
5550 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005551{
Johannes Berg48ab9052009-07-10 18:42:31 +02005552 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005553 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005554 void *hdr;
5555 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005556 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005557
5558 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005559
Eric W. Biederman15e47302012-09-07 20:12:54 +00005560 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005561 NL80211_CMD_NEW_SCAN_RESULTS);
5562 if (!hdr)
5563 return -1;
5564
Johannes Berg9720bb32011-06-21 09:45:33 +02005565 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5566
Johannes Berg97990a02013-04-19 01:02:55 +02005567 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5568 goto nla_put_failure;
5569 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005570 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5571 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005572 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5573 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005574
5575 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5576 if (!bss)
5577 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005578 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005579 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005580 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005581
5582 rcu_read_lock();
5583 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005584 if (ies) {
5585 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5586 goto fail_unlock_rcu;
5587 tsf = true;
5588 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5589 ies->len, ies->data))
5590 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005591 }
5592 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005593 if (ies) {
5594 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5595 goto fail_unlock_rcu;
5596 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5597 ies->len, ies->data))
5598 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005599 }
5600 rcu_read_unlock();
5601
David S. Miller9360ffd2012-03-29 04:41:26 -04005602 if (res->beacon_interval &&
5603 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5604 goto nla_put_failure;
5605 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5606 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5607 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5608 jiffies_to_msecs(jiffies - intbss->ts)))
5609 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005610
Johannes Berg77965c92009-02-18 18:45:06 +01005611 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005612 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005613 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5614 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005615 break;
5616 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005617 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5618 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005619 break;
5620 default:
5621 break;
5622 }
5623
Johannes Berg48ab9052009-07-10 18:42:31 +02005624 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005625 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005626 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005627 if (intbss == wdev->current_bss &&
5628 nla_put_u32(msg, NL80211_BSS_STATUS,
5629 NL80211_BSS_STATUS_ASSOCIATED))
5630 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005631 break;
5632 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005633 if (intbss == wdev->current_bss &&
5634 nla_put_u32(msg, NL80211_BSS_STATUS,
5635 NL80211_BSS_STATUS_IBSS_JOINED))
5636 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005637 break;
5638 default:
5639 break;
5640 }
5641
Johannes Berg2a519312009-02-10 21:25:55 +01005642 nla_nest_end(msg, bss);
5643
5644 return genlmsg_end(msg, hdr);
5645
Johannes Berg8cef2c92013-02-05 16:54:31 +01005646 fail_unlock_rcu:
5647 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005648 nla_put_failure:
5649 genlmsg_cancel(msg, hdr);
5650 return -EMSGSIZE;
5651}
5652
Johannes Berg97990a02013-04-19 01:02:55 +02005653static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005654{
Johannes Berg48ab9052009-07-10 18:42:31 +02005655 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005656 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005657 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005658 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005659 int err;
5660
Johannes Berg97990a02013-04-19 01:02:55 +02005661 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005662 if (err)
5663 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005664
Johannes Berg48ab9052009-07-10 18:42:31 +02005665 wdev_lock(wdev);
5666 spin_lock_bh(&rdev->bss_lock);
5667 cfg80211_bss_expire(rdev);
5668
Johannes Berg9720bb32011-06-21 09:45:33 +02005669 cb->seq = rdev->bss_generation;
5670
Johannes Berg48ab9052009-07-10 18:42:31 +02005671 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005672 if (++idx <= start)
5673 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005674 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005675 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005676 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005677 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005678 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005679 }
5680 }
5681
Johannes Berg48ab9052009-07-10 18:42:31 +02005682 spin_unlock_bh(&rdev->bss_lock);
5683 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005684
Johannes Berg97990a02013-04-19 01:02:55 +02005685 cb->args[2] = idx;
5686 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005687
Johannes Berg67748892010-10-04 21:14:06 +02005688 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005689}
5690
Eric W. Biederman15e47302012-09-07 20:12:54 +00005691static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005692 int flags, struct net_device *dev,
5693 struct survey_info *survey)
5694{
5695 void *hdr;
5696 struct nlattr *infoattr;
5697
Eric W. Biederman15e47302012-09-07 20:12:54 +00005698 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005699 NL80211_CMD_NEW_SURVEY_RESULTS);
5700 if (!hdr)
5701 return -ENOMEM;
5702
David S. Miller9360ffd2012-03-29 04:41:26 -04005703 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5704 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005705
5706 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5707 if (!infoattr)
5708 goto nla_put_failure;
5709
David S. Miller9360ffd2012-03-29 04:41:26 -04005710 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5711 survey->channel->center_freq))
5712 goto nla_put_failure;
5713
5714 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5715 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5716 goto nla_put_failure;
5717 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5718 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5719 goto nla_put_failure;
5720 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5721 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5722 survey->channel_time))
5723 goto nla_put_failure;
5724 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5725 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5726 survey->channel_time_busy))
5727 goto nla_put_failure;
5728 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5729 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5730 survey->channel_time_ext_busy))
5731 goto nla_put_failure;
5732 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5733 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5734 survey->channel_time_rx))
5735 goto nla_put_failure;
5736 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5737 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5738 survey->channel_time_tx))
5739 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005740
5741 nla_nest_end(msg, infoattr);
5742
5743 return genlmsg_end(msg, hdr);
5744
5745 nla_put_failure:
5746 genlmsg_cancel(msg, hdr);
5747 return -EMSGSIZE;
5748}
5749
5750static int nl80211_dump_survey(struct sk_buff *skb,
5751 struct netlink_callback *cb)
5752{
5753 struct survey_info survey;
5754 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02005755 struct wireless_dev *wdev;
5756 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01005757 int res;
5758
Johannes Berg97990a02013-04-19 01:02:55 +02005759 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005760 if (res)
5761 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005762
Johannes Berg97990a02013-04-19 01:02:55 +02005763 if (!wdev->netdev) {
5764 res = -EINVAL;
5765 goto out_err;
5766 }
5767
Holger Schurig61fa7132009-11-11 12:25:40 +01005768 if (!dev->ops->dump_survey) {
5769 res = -EOPNOTSUPP;
5770 goto out_err;
5771 }
5772
5773 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005774 struct ieee80211_channel *chan;
5775
Johannes Berg97990a02013-04-19 01:02:55 +02005776 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005777 if (res == -ENOENT)
5778 break;
5779 if (res)
5780 goto out_err;
5781
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005782 /* Survey without a channel doesn't make sense */
5783 if (!survey.channel) {
5784 res = -EINVAL;
5785 goto out;
5786 }
5787
5788 chan = ieee80211_get_channel(&dev->wiphy,
5789 survey.channel->center_freq);
5790 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5791 survey_idx++;
5792 continue;
5793 }
5794
Holger Schurig61fa7132009-11-11 12:25:40 +01005795 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005796 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005797 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02005798 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01005799 goto out;
5800 survey_idx++;
5801 }
5802
5803 out:
Johannes Berg97990a02013-04-19 01:02:55 +02005804 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01005805 res = skb->len;
5806 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02005807 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005808 return res;
5809}
5810
Samuel Ortizb23aa672009-07-01 21:26:54 +02005811static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5812{
5813 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5814 NL80211_WPA_VERSION_2));
5815}
5816
Jouni Malinen636a5d32009-03-19 13:39:22 +02005817static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5818{
Johannes Berg4c476992010-10-04 21:36:35 +02005819 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5820 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005821 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005822 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5823 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005824 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005825 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005826 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005827
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005828 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5829 return -EINVAL;
5830
5831 if (!info->attrs[NL80211_ATTR_MAC])
5832 return -EINVAL;
5833
Jouni Malinen17780922009-03-27 20:52:47 +02005834 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5835 return -EINVAL;
5836
Johannes Berg19957bb2009-07-02 17:20:43 +02005837 if (!info->attrs[NL80211_ATTR_SSID])
5838 return -EINVAL;
5839
5840 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5841 return -EINVAL;
5842
Johannes Bergfffd0932009-07-08 14:22:54 +02005843 err = nl80211_parse_key(info, &key);
5844 if (err)
5845 return err;
5846
5847 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005848 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5849 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005850 if (!key.p.key || !key.p.key_len)
5851 return -EINVAL;
5852 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5853 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5854 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5855 key.p.key_len != WLAN_KEY_LEN_WEP104))
5856 return -EINVAL;
5857 if (key.idx > 4)
5858 return -EINVAL;
5859 } else {
5860 key.p.key_len = 0;
5861 key.p.key = NULL;
5862 }
5863
Johannes Bergafea0b72010-08-10 09:46:42 +02005864 if (key.idx >= 0) {
5865 int i;
5866 bool ok = false;
5867 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5868 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5869 ok = true;
5870 break;
5871 }
5872 }
Johannes Berg4c476992010-10-04 21:36:35 +02005873 if (!ok)
5874 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005875 }
5876
Johannes Berg4c476992010-10-04 21:36:35 +02005877 if (!rdev->ops->auth)
5878 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005879
Johannes Berg074ac8d2010-09-16 14:58:22 +02005880 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005881 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5882 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005883
Johannes Berg19957bb2009-07-02 17:20:43 +02005884 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005885 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005886 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005887 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5888 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005889
Johannes Berg19957bb2009-07-02 17:20:43 +02005890 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5891 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5892
5893 if (info->attrs[NL80211_ATTR_IE]) {
5894 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5895 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5896 }
5897
5898 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005899 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005900 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005901
Jouni Malinene39e5b52012-09-30 19:29:39 +03005902 if (auth_type == NL80211_AUTHTYPE_SAE &&
5903 !info->attrs[NL80211_ATTR_SAE_DATA])
5904 return -EINVAL;
5905
5906 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5907 if (auth_type != NL80211_AUTHTYPE_SAE)
5908 return -EINVAL;
5909 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5910 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5911 /* need to include at least Auth Transaction and Status Code */
5912 if (sae_data_len < 4)
5913 return -EINVAL;
5914 }
5915
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005916 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5917
Johannes Berg95de8172012-01-20 13:55:25 +01005918 /*
5919 * Since we no longer track auth state, ignore
5920 * requests to only change local state.
5921 */
5922 if (local_state_change)
5923 return 0;
5924
Johannes Berg4c476992010-10-04 21:36:35 +02005925 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5926 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005927 key.p.key, key.p.key_len, key.idx,
5928 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005929}
5930
Johannes Bergc0692b82010-08-27 14:26:53 +03005931static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5932 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005933 struct cfg80211_crypto_settings *settings,
5934 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005935{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005936 memset(settings, 0, sizeof(*settings));
5937
Samuel Ortizb23aa672009-07-01 21:26:54 +02005938 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5939
Johannes Bergc0692b82010-08-27 14:26:53 +03005940 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5941 u16 proto;
5942 proto = nla_get_u16(
5943 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5944 settings->control_port_ethertype = cpu_to_be16(proto);
5945 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5946 proto != ETH_P_PAE)
5947 return -EINVAL;
5948 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5949 settings->control_port_no_encrypt = true;
5950 } else
5951 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5952
Samuel Ortizb23aa672009-07-01 21:26:54 +02005953 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5954 void *data;
5955 int len, i;
5956
5957 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5958 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5959 settings->n_ciphers_pairwise = len / sizeof(u32);
5960
5961 if (len % sizeof(u32))
5962 return -EINVAL;
5963
Johannes Berg3dc27d22009-07-02 21:36:37 +02005964 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005965 return -EINVAL;
5966
5967 memcpy(settings->ciphers_pairwise, data, len);
5968
5969 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005970 if (!cfg80211_supported_cipher_suite(
5971 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005972 settings->ciphers_pairwise[i]))
5973 return -EINVAL;
5974 }
5975
5976 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5977 settings->cipher_group =
5978 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005979 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5980 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005981 return -EINVAL;
5982 }
5983
5984 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5985 settings->wpa_versions =
5986 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5987 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5988 return -EINVAL;
5989 }
5990
5991 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5992 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005993 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005994
5995 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5996 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5997 settings->n_akm_suites = len / sizeof(u32);
5998
5999 if (len % sizeof(u32))
6000 return -EINVAL;
6001
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006002 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6003 return -EINVAL;
6004
Samuel Ortizb23aa672009-07-01 21:26:54 +02006005 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006006 }
6007
6008 return 0;
6009}
6010
Jouni Malinen636a5d32009-03-19 13:39:22 +02006011static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6012{
Johannes Berg4c476992010-10-04 21:36:35 +02006013 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6014 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006015 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006016 struct cfg80211_assoc_request req = {};
6017 const u8 *bssid, *ssid;
6018 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006019
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006020 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6021 return -EINVAL;
6022
6023 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006024 !info->attrs[NL80211_ATTR_SSID] ||
6025 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006026 return -EINVAL;
6027
Johannes Berg4c476992010-10-04 21:36:35 +02006028 if (!rdev->ops->assoc)
6029 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006030
Johannes Berg074ac8d2010-09-16 14:58:22 +02006031 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006032 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6033 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006034
Johannes Berg19957bb2009-07-02 17:20:43 +02006035 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006036
Johannes Berg19957bb2009-07-02 17:20:43 +02006037 chan = ieee80211_get_channel(&rdev->wiphy,
6038 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006039 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6040 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006041
Johannes Berg19957bb2009-07-02 17:20:43 +02006042 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6043 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006044
6045 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006046 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6047 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006048 }
6049
Jouni Malinendc6382c2009-05-06 22:09:37 +03006050 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006051 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006052 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006053 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006054 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006055 else if (mfp != NL80211_MFP_NO)
6056 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006057 }
6058
Johannes Berg3e5d7642009-07-07 14:37:26 +02006059 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006060 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006061
Ben Greear7e7c8922011-11-18 11:31:59 -08006062 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006063 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006064
6065 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006066 memcpy(&req.ht_capa_mask,
6067 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6068 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006069
6070 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006071 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006072 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006073 memcpy(&req.ht_capa,
6074 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6075 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006076 }
6077
Johannes Bergee2aca32013-02-21 17:36:01 +01006078 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006079 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006080
6081 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006082 memcpy(&req.vht_capa_mask,
6083 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6084 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006085
6086 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006087 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006088 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006089 memcpy(&req.vht_capa,
6090 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6091 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006092 }
6093
Johannes Bergf62fab72013-02-21 20:09:09 +01006094 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006095 if (!err)
Johannes Bergf62fab72013-02-21 20:09:09 +01006096 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6097 ssid, ssid_len, &req);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006098
Jouni Malinen636a5d32009-03-19 13:39:22 +02006099 return err;
6100}
6101
6102static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6103{
Johannes Berg4c476992010-10-04 21:36:35 +02006104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6105 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006106 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006107 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006108 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006109 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006110
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006111 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6112 return -EINVAL;
6113
6114 if (!info->attrs[NL80211_ATTR_MAC])
6115 return -EINVAL;
6116
6117 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6118 return -EINVAL;
6119
Johannes Berg4c476992010-10-04 21:36:35 +02006120 if (!rdev->ops->deauth)
6121 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006122
Johannes Berg074ac8d2010-09-16 14:58:22 +02006123 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006124 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6125 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006126
Johannes Berg19957bb2009-07-02 17:20:43 +02006127 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006128
Johannes Berg19957bb2009-07-02 17:20:43 +02006129 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6130 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006131 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006132 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006133 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006134
6135 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006136 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6137 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006138 }
6139
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006140 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6141
Johannes Berg4c476992010-10-04 21:36:35 +02006142 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6143 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006144}
6145
6146static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6147{
Johannes Berg4c476992010-10-04 21:36:35 +02006148 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6149 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006150 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006151 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006152 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006153 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006154
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006155 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6156 return -EINVAL;
6157
6158 if (!info->attrs[NL80211_ATTR_MAC])
6159 return -EINVAL;
6160
6161 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6162 return -EINVAL;
6163
Johannes Berg4c476992010-10-04 21:36:35 +02006164 if (!rdev->ops->disassoc)
6165 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006166
Johannes Berg074ac8d2010-09-16 14:58:22 +02006167 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006168 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6169 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006170
Johannes Berg19957bb2009-07-02 17:20:43 +02006171 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006172
Johannes Berg19957bb2009-07-02 17:20:43 +02006173 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6174 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006175 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006176 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006177 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006178
6179 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006180 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6181 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006182 }
6183
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006184 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6185
Johannes Berg4c476992010-10-04 21:36:35 +02006186 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6187 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006188}
6189
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006190static bool
6191nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6192 int mcast_rate[IEEE80211_NUM_BANDS],
6193 int rateval)
6194{
6195 struct wiphy *wiphy = &rdev->wiphy;
6196 bool found = false;
6197 int band, i;
6198
6199 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6200 struct ieee80211_supported_band *sband;
6201
6202 sband = wiphy->bands[band];
6203 if (!sband)
6204 continue;
6205
6206 for (i = 0; i < sband->n_bitrates; i++) {
6207 if (sband->bitrates[i].bitrate == rateval) {
6208 mcast_rate[band] = i + 1;
6209 found = true;
6210 break;
6211 }
6212 }
6213 }
6214
6215 return found;
6216}
6217
Johannes Berg04a773a2009-04-19 21:24:32 +02006218static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6219{
Johannes Berg4c476992010-10-04 21:36:35 +02006220 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6221 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006222 struct cfg80211_ibss_params ibss;
6223 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006224 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006225 int err;
6226
Johannes Berg8e30bc52009-04-22 17:45:38 +02006227 memset(&ibss, 0, sizeof(ibss));
6228
Johannes Berg04a773a2009-04-19 21:24:32 +02006229 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6230 return -EINVAL;
6231
Johannes Berg683b6d32012-11-08 21:25:48 +01006232 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006233 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6234 return -EINVAL;
6235
Johannes Berg8e30bc52009-04-22 17:45:38 +02006236 ibss.beacon_interval = 100;
6237
6238 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6239 ibss.beacon_interval =
6240 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6241 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6242 return -EINVAL;
6243 }
6244
Johannes Berg4c476992010-10-04 21:36:35 +02006245 if (!rdev->ops->join_ibss)
6246 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006247
Johannes Berg4c476992010-10-04 21:36:35 +02006248 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6249 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006250
Johannes Berg79c97e92009-07-07 03:56:12 +02006251 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006252
Johannes Berg39193492011-09-16 13:45:25 +02006253 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006254 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006255
6256 if (!is_valid_ether_addr(ibss.bssid))
6257 return -EINVAL;
6258 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006259 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6260 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6261
6262 if (info->attrs[NL80211_ATTR_IE]) {
6263 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6264 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6265 }
6266
Johannes Berg683b6d32012-11-08 21:25:48 +01006267 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6268 if (err)
6269 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006270
Johannes Berg683b6d32012-11-08 21:25:48 +01006271 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006272 return -EINVAL;
6273
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006274 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6275 return -EINVAL;
6276 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6277 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01006278 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006279
Johannes Berg04a773a2009-04-19 21:24:32 +02006280 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006281 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006282
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006283 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6284 u8 *rates =
6285 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6286 int n_rates =
6287 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6288 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006289 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006290
Johannes Berg34850ab2011-07-18 18:08:35 +02006291 err = ieee80211_get_ratemask(sband, rates, n_rates,
6292 &ibss.basic_rates);
6293 if (err)
6294 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006295 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006296
6297 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6298 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6299 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6300 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006301
Johannes Berg4c476992010-10-04 21:36:35 +02006302 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306303 bool no_ht = false;
6304
Johannes Berg4c476992010-10-04 21:36:35 +02006305 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306306 info->attrs[NL80211_ATTR_KEYS],
6307 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006308 if (IS_ERR(connkeys))
6309 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306310
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006311 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6312 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306313 kfree(connkeys);
6314 return -EINVAL;
6315 }
Johannes Berg4c476992010-10-04 21:36:35 +02006316 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006317
Antonio Quartulli267335d2012-01-31 20:25:47 +01006318 ibss.control_port =
6319 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6320
Johannes Berg4c476992010-10-04 21:36:35 +02006321 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006322 if (err)
6323 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006324 return err;
6325}
6326
6327static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6328{
Johannes Berg4c476992010-10-04 21:36:35 +02006329 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6330 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006331
Johannes Berg4c476992010-10-04 21:36:35 +02006332 if (!rdev->ops->leave_ibss)
6333 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006334
Johannes Berg4c476992010-10-04 21:36:35 +02006335 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6336 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006337
Johannes Berg4c476992010-10-04 21:36:35 +02006338 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006339}
6340
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006341static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6342{
6343 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6344 struct net_device *dev = info->user_ptr[1];
6345 int mcast_rate[IEEE80211_NUM_BANDS];
6346 u32 nla_rate;
6347 int err;
6348
6349 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6350 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6351 return -EOPNOTSUPP;
6352
6353 if (!rdev->ops->set_mcast_rate)
6354 return -EOPNOTSUPP;
6355
6356 memset(mcast_rate, 0, sizeof(mcast_rate));
6357
6358 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6359 return -EINVAL;
6360
6361 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6362 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6363 return -EINVAL;
6364
6365 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6366
6367 return err;
6368}
6369
6370
Johannes Bergaff89a92009-07-01 21:26:51 +02006371#ifdef CONFIG_NL80211_TESTMODE
6372static struct genl_multicast_group nl80211_testmode_mcgrp = {
6373 .name = "testmode",
6374};
6375
6376static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6377{
Johannes Berg4c476992010-10-04 21:36:35 +02006378 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006379 int err;
6380
6381 if (!info->attrs[NL80211_ATTR_TESTDATA])
6382 return -EINVAL;
6383
Johannes Bergaff89a92009-07-01 21:26:51 +02006384 err = -EOPNOTSUPP;
6385 if (rdev->ops->testmode_cmd) {
6386 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006387 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006388 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6389 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6390 rdev->testmode_info = NULL;
6391 }
6392
Johannes Bergaff89a92009-07-01 21:26:51 +02006393 return err;
6394}
6395
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006396static int nl80211_testmode_dump(struct sk_buff *skb,
6397 struct netlink_callback *cb)
6398{
Johannes Berg00918d32011-12-13 17:22:05 +01006399 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006400 int err;
6401 long phy_idx;
6402 void *data = NULL;
6403 int data_len = 0;
6404
6405 if (cb->args[0]) {
6406 /*
6407 * 0 is a valid index, but not valid for args[0],
6408 * so we need to offset by 1.
6409 */
6410 phy_idx = cb->args[0] - 1;
6411 } else {
6412 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6413 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6414 nl80211_policy);
6415 if (err)
6416 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006417
Johannes Berg2bd7e352012-06-15 14:23:16 +02006418 mutex_lock(&cfg80211_mutex);
6419 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6420 nl80211_fam.attrbuf);
6421 if (IS_ERR(rdev)) {
6422 mutex_unlock(&cfg80211_mutex);
6423 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006424 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006425 phy_idx = rdev->wiphy_idx;
6426 rdev = NULL;
6427 mutex_unlock(&cfg80211_mutex);
6428
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006429 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6430 cb->args[1] =
6431 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6432 }
6433
6434 if (cb->args[1]) {
6435 data = nla_data((void *)cb->args[1]);
6436 data_len = nla_len((void *)cb->args[1]);
6437 }
6438
6439 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006440 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6441 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006442 mutex_unlock(&cfg80211_mutex);
6443 return -ENOENT;
6444 }
Johannes Berg00918d32011-12-13 17:22:05 +01006445 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006446 mutex_unlock(&cfg80211_mutex);
6447
Johannes Berg00918d32011-12-13 17:22:05 +01006448 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006449 err = -EOPNOTSUPP;
6450 goto out_err;
6451 }
6452
6453 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006454 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006455 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6456 NL80211_CMD_TESTMODE);
6457 struct nlattr *tmdata;
6458
David S. Miller9360ffd2012-03-29 04:41:26 -04006459 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006460 genlmsg_cancel(skb, hdr);
6461 break;
6462 }
6463
6464 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6465 if (!tmdata) {
6466 genlmsg_cancel(skb, hdr);
6467 break;
6468 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006469 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006470 nla_nest_end(skb, tmdata);
6471
6472 if (err == -ENOBUFS || err == -ENOENT) {
6473 genlmsg_cancel(skb, hdr);
6474 break;
6475 } else if (err) {
6476 genlmsg_cancel(skb, hdr);
6477 goto out_err;
6478 }
6479
6480 genlmsg_end(skb, hdr);
6481 }
6482
6483 err = skb->len;
6484 /* see above */
6485 cb->args[0] = phy_idx + 1;
6486 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006487 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006488 return err;
6489}
6490
Johannes Bergaff89a92009-07-01 21:26:51 +02006491static struct sk_buff *
6492__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006493 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006494{
6495 struct sk_buff *skb;
6496 void *hdr;
6497 struct nlattr *data;
6498
6499 skb = nlmsg_new(approxlen + 100, gfp);
6500 if (!skb)
6501 return NULL;
6502
Eric W. Biederman15e47302012-09-07 20:12:54 +00006503 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006504 if (!hdr) {
6505 kfree_skb(skb);
6506 return NULL;
6507 }
6508
David S. Miller9360ffd2012-03-29 04:41:26 -04006509 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6510 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006511 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6512
6513 ((void **)skb->cb)[0] = rdev;
6514 ((void **)skb->cb)[1] = hdr;
6515 ((void **)skb->cb)[2] = data;
6516
6517 return skb;
6518
6519 nla_put_failure:
6520 kfree_skb(skb);
6521 return NULL;
6522}
6523
6524struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6525 int approxlen)
6526{
6527 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6528
6529 if (WARN_ON(!rdev->testmode_info))
6530 return NULL;
6531
6532 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006533 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006534 rdev->testmode_info->snd_seq,
6535 GFP_KERNEL);
6536}
6537EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6538
6539int cfg80211_testmode_reply(struct sk_buff *skb)
6540{
6541 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6542 void *hdr = ((void **)skb->cb)[1];
6543 struct nlattr *data = ((void **)skb->cb)[2];
6544
6545 if (WARN_ON(!rdev->testmode_info)) {
6546 kfree_skb(skb);
6547 return -EINVAL;
6548 }
6549
6550 nla_nest_end(skb, data);
6551 genlmsg_end(skb, hdr);
6552 return genlmsg_reply(skb, rdev->testmode_info);
6553}
6554EXPORT_SYMBOL(cfg80211_testmode_reply);
6555
6556struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6557 int approxlen, gfp_t gfp)
6558{
6559 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6560
6561 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6562}
6563EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6564
6565void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6566{
6567 void *hdr = ((void **)skb->cb)[1];
6568 struct nlattr *data = ((void **)skb->cb)[2];
6569
6570 nla_nest_end(skb, data);
6571 genlmsg_end(skb, hdr);
6572 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6573}
6574EXPORT_SYMBOL(cfg80211_testmode_event);
6575#endif
6576
Samuel Ortizb23aa672009-07-01 21:26:54 +02006577static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6578{
Johannes Berg4c476992010-10-04 21:36:35 +02006579 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6580 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006581 struct cfg80211_connect_params connect;
6582 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006583 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006584 int err;
6585
6586 memset(&connect, 0, sizeof(connect));
6587
6588 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6589 return -EINVAL;
6590
6591 if (!info->attrs[NL80211_ATTR_SSID] ||
6592 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6593 return -EINVAL;
6594
6595 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6596 connect.auth_type =
6597 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006598 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6599 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006600 return -EINVAL;
6601 } else
6602 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6603
6604 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6605
Johannes Bergc0692b82010-08-27 14:26:53 +03006606 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006607 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006608 if (err)
6609 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006610
Johannes Berg074ac8d2010-09-16 14:58:22 +02006611 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006612 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6613 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006614
Johannes Berg79c97e92009-07-07 03:56:12 +02006615 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006616
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306617 connect.bg_scan_period = -1;
6618 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6619 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6620 connect.bg_scan_period =
6621 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6622 }
6623
Samuel Ortizb23aa672009-07-01 21:26:54 +02006624 if (info->attrs[NL80211_ATTR_MAC])
6625 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6626 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6627 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6628
6629 if (info->attrs[NL80211_ATTR_IE]) {
6630 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6631 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6632 }
6633
Jouni Malinencee00a92013-01-15 17:15:57 +02006634 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6635 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6636 if (connect.mfp != NL80211_MFP_REQUIRED &&
6637 connect.mfp != NL80211_MFP_NO)
6638 return -EINVAL;
6639 } else {
6640 connect.mfp = NL80211_MFP_NO;
6641 }
6642
Samuel Ortizb23aa672009-07-01 21:26:54 +02006643 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6644 connect.channel =
6645 ieee80211_get_channel(wiphy,
6646 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6647 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006648 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6649 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006650 }
6651
Johannes Bergfffd0932009-07-08 14:22:54 +02006652 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6653 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306654 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006655 if (IS_ERR(connkeys))
6656 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006657 }
6658
Ben Greear7e7c8922011-11-18 11:31:59 -08006659 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6660 connect.flags |= ASSOC_REQ_DISABLE_HT;
6661
6662 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6663 memcpy(&connect.ht_capa_mask,
6664 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6665 sizeof(connect.ht_capa_mask));
6666
6667 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006668 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6669 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006670 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006671 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006672 memcpy(&connect.ht_capa,
6673 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6674 sizeof(connect.ht_capa));
6675 }
6676
Johannes Bergee2aca32013-02-21 17:36:01 +01006677 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6678 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6679
6680 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6681 memcpy(&connect.vht_capa_mask,
6682 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6683 sizeof(connect.vht_capa_mask));
6684
6685 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6686 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
6687 kfree(connkeys);
6688 return -EINVAL;
6689 }
6690 memcpy(&connect.vht_capa,
6691 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6692 sizeof(connect.vht_capa));
6693 }
6694
Johannes Bergfffd0932009-07-08 14:22:54 +02006695 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006696 if (err)
6697 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006698 return err;
6699}
6700
6701static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6702{
Johannes Berg4c476992010-10-04 21:36:35 +02006703 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6704 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006705 u16 reason;
6706
6707 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6708 reason = WLAN_REASON_DEAUTH_LEAVING;
6709 else
6710 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6711
6712 if (reason == 0)
6713 return -EINVAL;
6714
Johannes Berg074ac8d2010-09-16 14:58:22 +02006715 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006716 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6717 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006718
Johannes Berg4c476992010-10-04 21:36:35 +02006719 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006720}
6721
Johannes Berg463d0182009-07-14 00:33:35 +02006722static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6723{
Johannes Berg4c476992010-10-04 21:36:35 +02006724 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006725 struct net *net;
6726 int err;
6727 u32 pid;
6728
6729 if (!info->attrs[NL80211_ATTR_PID])
6730 return -EINVAL;
6731
6732 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6733
Johannes Berg463d0182009-07-14 00:33:35 +02006734 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006735 if (IS_ERR(net))
6736 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006737
6738 err = 0;
6739
6740 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006741 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6742 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006743
Johannes Berg463d0182009-07-14 00:33:35 +02006744 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006745 return err;
6746}
6747
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006748static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6749{
Johannes Berg4c476992010-10-04 21:36:35 +02006750 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006751 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6752 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006753 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006754 struct cfg80211_pmksa pmksa;
6755
6756 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6757
6758 if (!info->attrs[NL80211_ATTR_MAC])
6759 return -EINVAL;
6760
6761 if (!info->attrs[NL80211_ATTR_PMKID])
6762 return -EINVAL;
6763
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006764 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6765 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6766
Johannes Berg074ac8d2010-09-16 14:58:22 +02006767 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006768 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6769 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006770
6771 switch (info->genlhdr->cmd) {
6772 case NL80211_CMD_SET_PMKSA:
6773 rdev_ops = rdev->ops->set_pmksa;
6774 break;
6775 case NL80211_CMD_DEL_PMKSA:
6776 rdev_ops = rdev->ops->del_pmksa;
6777 break;
6778 default:
6779 WARN_ON(1);
6780 break;
6781 }
6782
Johannes Berg4c476992010-10-04 21:36:35 +02006783 if (!rdev_ops)
6784 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006785
Johannes Berg4c476992010-10-04 21:36:35 +02006786 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006787}
6788
6789static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6790{
Johannes Berg4c476992010-10-04 21:36:35 +02006791 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6792 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006793
Johannes Berg074ac8d2010-09-16 14:58:22 +02006794 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006795 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6796 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006797
Johannes Berg4c476992010-10-04 21:36:35 +02006798 if (!rdev->ops->flush_pmksa)
6799 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006800
Hila Gonene35e4d22012-06-27 17:19:42 +03006801 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006802}
6803
Arik Nemtsov109086c2011-09-28 14:12:50 +03006804static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6805{
6806 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6807 struct net_device *dev = info->user_ptr[1];
6808 u8 action_code, dialog_token;
6809 u16 status_code;
6810 u8 *peer;
6811
6812 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6813 !rdev->ops->tdls_mgmt)
6814 return -EOPNOTSUPP;
6815
6816 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6817 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6818 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6819 !info->attrs[NL80211_ATTR_IE] ||
6820 !info->attrs[NL80211_ATTR_MAC])
6821 return -EINVAL;
6822
6823 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6824 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6825 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6826 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6827
Hila Gonene35e4d22012-06-27 17:19:42 +03006828 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6829 dialog_token, status_code,
6830 nla_data(info->attrs[NL80211_ATTR_IE]),
6831 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006832}
6833
6834static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6835{
6836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6837 struct net_device *dev = info->user_ptr[1];
6838 enum nl80211_tdls_operation operation;
6839 u8 *peer;
6840
6841 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6842 !rdev->ops->tdls_oper)
6843 return -EOPNOTSUPP;
6844
6845 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6846 !info->attrs[NL80211_ATTR_MAC])
6847 return -EINVAL;
6848
6849 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6850 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6851
Hila Gonene35e4d22012-06-27 17:19:42 +03006852 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006853}
6854
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006855static int nl80211_remain_on_channel(struct sk_buff *skb,
6856 struct genl_info *info)
6857{
Johannes Berg4c476992010-10-04 21:36:35 +02006858 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006859 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006860 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006861 struct sk_buff *msg;
6862 void *hdr;
6863 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006864 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006865 int err;
6866
6867 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6868 !info->attrs[NL80211_ATTR_DURATION])
6869 return -EINVAL;
6870
6871 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6872
Johannes Berg7c4ef712011-11-18 15:33:48 +01006873 if (!rdev->ops->remain_on_channel ||
6874 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006875 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006876
Johannes Bergebf348f2012-06-01 12:50:54 +02006877 /*
6878 * We should be on that channel for at least a minimum amount of
6879 * time (10ms) but no longer than the driver supports.
6880 */
6881 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6882 duration > rdev->wiphy.max_remain_on_channel_duration)
6883 return -EINVAL;
6884
Johannes Berg683b6d32012-11-08 21:25:48 +01006885 err = nl80211_parse_chandef(rdev, info, &chandef);
6886 if (err)
6887 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006888
6889 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006890 if (!msg)
6891 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006892
Eric W. Biederman15e47302012-09-07 20:12:54 +00006893 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006894 NL80211_CMD_REMAIN_ON_CHANNEL);
6895
6896 if (IS_ERR(hdr)) {
6897 err = PTR_ERR(hdr);
6898 goto free_msg;
6899 }
6900
Johannes Berg683b6d32012-11-08 21:25:48 +01006901 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6902 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006903
6904 if (err)
6905 goto free_msg;
6906
David S. Miller9360ffd2012-03-29 04:41:26 -04006907 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6908 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006909
6910 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006911
6912 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006913
6914 nla_put_failure:
6915 err = -ENOBUFS;
6916 free_msg:
6917 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006918 return err;
6919}
6920
6921static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6922 struct genl_info *info)
6923{
Johannes Berg4c476992010-10-04 21:36:35 +02006924 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006925 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006926 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006927
6928 if (!info->attrs[NL80211_ATTR_COOKIE])
6929 return -EINVAL;
6930
Johannes Berg4c476992010-10-04 21:36:35 +02006931 if (!rdev->ops->cancel_remain_on_channel)
6932 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006933
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006934 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6935
Hila Gonene35e4d22012-06-27 17:19:42 +03006936 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006937}
6938
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006939static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6940 u8 *rates, u8 rates_len)
6941{
6942 u8 i;
6943 u32 mask = 0;
6944
6945 for (i = 0; i < rates_len; i++) {
6946 int rate = (rates[i] & 0x7f) * 5;
6947 int ridx;
6948 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6949 struct ieee80211_rate *srate =
6950 &sband->bitrates[ridx];
6951 if (rate == srate->bitrate) {
6952 mask |= 1 << ridx;
6953 break;
6954 }
6955 }
6956 if (ridx == sband->n_bitrates)
6957 return 0; /* rate not found */
6958 }
6959
6960 return mask;
6961}
6962
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006963static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6964 u8 *rates, u8 rates_len,
6965 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6966{
6967 u8 i;
6968
6969 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6970
6971 for (i = 0; i < rates_len; i++) {
6972 int ridx, rbit;
6973
6974 ridx = rates[i] / 8;
6975 rbit = BIT(rates[i] % 8);
6976
6977 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006978 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006979 return false;
6980
6981 /* check availability */
6982 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6983 mcs[ridx] |= rbit;
6984 else
6985 return false;
6986 }
6987
6988 return true;
6989}
6990
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006991static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006992 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6993 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006994 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6995 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006996};
6997
6998static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6999 struct genl_info *info)
7000{
7001 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007002 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007003 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007004 int rem, i;
7005 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007006 struct nlattr *tx_rates;
7007 struct ieee80211_supported_band *sband;
7008
7009 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7010 return -EINVAL;
7011
Johannes Berg4c476992010-10-04 21:36:35 +02007012 if (!rdev->ops->set_bitrate_mask)
7013 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007014
7015 memset(&mask, 0, sizeof(mask));
7016 /* Default to all rates enabled */
7017 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7018 sband = rdev->wiphy.bands[i];
7019 mask.control[i].legacy =
7020 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007021 if (sband)
7022 memcpy(mask.control[i].mcs,
7023 sband->ht_cap.mcs.rx_mask,
7024 sizeof(mask.control[i].mcs));
7025 else
7026 memset(mask.control[i].mcs, 0,
7027 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007028 }
7029
7030 /*
7031 * The nested attribute uses enum nl80211_band as the index. This maps
7032 * directly to the enum ieee80211_band values used in cfg80211.
7033 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007034 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007035 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7036 {
7037 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007038 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7039 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007040 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007041 if (sband == NULL)
7042 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007043 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7044 nla_len(tx_rates), nl80211_txattr_policy);
7045 if (tb[NL80211_TXRATE_LEGACY]) {
7046 mask.control[band].legacy = rateset_to_mask(
7047 sband,
7048 nla_data(tb[NL80211_TXRATE_LEGACY]),
7049 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307050 if ((mask.control[band].legacy == 0) &&
7051 nla_len(tb[NL80211_TXRATE_LEGACY]))
7052 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007053 }
7054 if (tb[NL80211_TXRATE_MCS]) {
7055 if (!ht_rateset_to_mask(
7056 sband,
7057 nla_data(tb[NL80211_TXRATE_MCS]),
7058 nla_len(tb[NL80211_TXRATE_MCS]),
7059 mask.control[band].mcs))
7060 return -EINVAL;
7061 }
7062
7063 if (mask.control[band].legacy == 0) {
7064 /* don't allow empty legacy rates if HT
7065 * is not even supported. */
7066 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7067 return -EINVAL;
7068
7069 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7070 if (mask.control[band].mcs[i])
7071 break;
7072
7073 /* legacy and mcs rates may not be both empty */
7074 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02007075 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007076 }
7077 }
7078
Hila Gonene35e4d22012-06-27 17:19:42 +03007079 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007080}
7081
Johannes Berg2e161f72010-08-12 15:38:38 +02007082static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007083{
Johannes Berg4c476992010-10-04 21:36:35 +02007084 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007085 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007086 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007087
7088 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7089 return -EINVAL;
7090
Johannes Berg2e161f72010-08-12 15:38:38 +02007091 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7092 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007093
Johannes Berg71bbc992012-06-15 15:30:18 +02007094 switch (wdev->iftype) {
7095 case NL80211_IFTYPE_STATION:
7096 case NL80211_IFTYPE_ADHOC:
7097 case NL80211_IFTYPE_P2P_CLIENT:
7098 case NL80211_IFTYPE_AP:
7099 case NL80211_IFTYPE_AP_VLAN:
7100 case NL80211_IFTYPE_MESH_POINT:
7101 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007102 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007103 break;
7104 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007105 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007106 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007107
7108 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007109 if (!rdev->ops->mgmt_tx)
7110 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007111
Eric W. Biederman15e47302012-09-07 20:12:54 +00007112 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007113 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7114 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007115}
7116
Johannes Berg2e161f72010-08-12 15:38:38 +02007117static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007118{
Johannes Berg4c476992010-10-04 21:36:35 +02007119 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007120 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007121 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007122 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007123 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007124 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007125 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007126 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01007127 bool offchan, no_cck, dont_wait_for_ack;
7128
7129 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02007130
Johannes Berg683b6d32012-11-08 21:25:48 +01007131 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007132 return -EINVAL;
7133
Johannes Berg4c476992010-10-04 21:36:35 +02007134 if (!rdev->ops->mgmt_tx)
7135 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007136
Johannes Berg71bbc992012-06-15 15:30:18 +02007137 switch (wdev->iftype) {
7138 case NL80211_IFTYPE_STATION:
7139 case NL80211_IFTYPE_ADHOC:
7140 case NL80211_IFTYPE_P2P_CLIENT:
7141 case NL80211_IFTYPE_AP:
7142 case NL80211_IFTYPE_AP_VLAN:
7143 case NL80211_IFTYPE_MESH_POINT:
7144 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007145 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007146 break;
7147 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007148 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007149 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007150
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007151 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007152 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007153 return -EINVAL;
7154 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007155
7156 /*
7157 * We should wait on the channel for at least a minimum amount
7158 * of time (10ms) but no longer than the driver supports.
7159 */
7160 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7161 wait > rdev->wiphy.max_remain_on_channel_duration)
7162 return -EINVAL;
7163
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007164 }
7165
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007166 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7167
Johannes Berg7c4ef712011-11-18 15:33:48 +01007168 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7169 return -EINVAL;
7170
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307171 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7172
Johannes Berg683b6d32012-11-08 21:25:48 +01007173 err = nl80211_parse_chandef(rdev, info, &chandef);
7174 if (err)
7175 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02007176
Johannes Berge247bd902011-11-04 11:18:21 +01007177 if (!dont_wait_for_ack) {
7178 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7179 if (!msg)
7180 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007181
Eric W. Biederman15e47302012-09-07 20:12:54 +00007182 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007183 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007184
Johannes Berge247bd902011-11-04 11:18:21 +01007185 if (IS_ERR(hdr)) {
7186 err = PTR_ERR(hdr);
7187 goto free_msg;
7188 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007189 }
Johannes Berge247bd902011-11-04 11:18:21 +01007190
Johannes Berg683b6d32012-11-08 21:25:48 +01007191 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02007192 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7193 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01007194 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007195 if (err)
7196 goto free_msg;
7197
Johannes Berge247bd902011-11-04 11:18:21 +01007198 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007199 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7200 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007201
Johannes Berge247bd902011-11-04 11:18:21 +01007202 genlmsg_end(msg, hdr);
7203 return genlmsg_reply(msg, info);
7204 }
7205
7206 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007207
7208 nla_put_failure:
7209 err = -ENOBUFS;
7210 free_msg:
7211 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007212 return err;
7213}
7214
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007215static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7216{
7217 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007218 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007219 u64 cookie;
7220
7221 if (!info->attrs[NL80211_ATTR_COOKIE])
7222 return -EINVAL;
7223
7224 if (!rdev->ops->mgmt_tx_cancel_wait)
7225 return -EOPNOTSUPP;
7226
Johannes Berg71bbc992012-06-15 15:30:18 +02007227 switch (wdev->iftype) {
7228 case NL80211_IFTYPE_STATION:
7229 case NL80211_IFTYPE_ADHOC:
7230 case NL80211_IFTYPE_P2P_CLIENT:
7231 case NL80211_IFTYPE_AP:
7232 case NL80211_IFTYPE_AP_VLAN:
7233 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007234 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007235 break;
7236 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007237 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007238 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007239
7240 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7241
Hila Gonene35e4d22012-06-27 17:19:42 +03007242 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007243}
7244
Kalle Valoffb9eb32010-02-17 17:58:10 +02007245static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7246{
Johannes Berg4c476992010-10-04 21:36:35 +02007247 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007248 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007249 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007250 u8 ps_state;
7251 bool state;
7252 int err;
7253
Johannes Berg4c476992010-10-04 21:36:35 +02007254 if (!info->attrs[NL80211_ATTR_PS_STATE])
7255 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007256
7257 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7258
Johannes Berg4c476992010-10-04 21:36:35 +02007259 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7260 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007261
7262 wdev = dev->ieee80211_ptr;
7263
Johannes Berg4c476992010-10-04 21:36:35 +02007264 if (!rdev->ops->set_power_mgmt)
7265 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007266
7267 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7268
7269 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007270 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007271
Hila Gonene35e4d22012-06-27 17:19:42 +03007272 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007273 if (!err)
7274 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007275 return err;
7276}
7277
7278static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7279{
Johannes Berg4c476992010-10-04 21:36:35 +02007280 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007281 enum nl80211_ps_state ps_state;
7282 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007283 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007284 struct sk_buff *msg;
7285 void *hdr;
7286 int err;
7287
Kalle Valoffb9eb32010-02-17 17:58:10 +02007288 wdev = dev->ieee80211_ptr;
7289
Johannes Berg4c476992010-10-04 21:36:35 +02007290 if (!rdev->ops->set_power_mgmt)
7291 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007292
7293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007294 if (!msg)
7295 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007296
Eric W. Biederman15e47302012-09-07 20:12:54 +00007297 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007298 NL80211_CMD_GET_POWER_SAVE);
7299 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007300 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007301 goto free_msg;
7302 }
7303
7304 if (wdev->ps)
7305 ps_state = NL80211_PS_ENABLED;
7306 else
7307 ps_state = NL80211_PS_DISABLED;
7308
David S. Miller9360ffd2012-03-29 04:41:26 -04007309 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7310 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007311
7312 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007313 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007314
Johannes Berg4c476992010-10-04 21:36:35 +02007315 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007316 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007317 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007318 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007319 return err;
7320}
7321
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007322static struct nla_policy
7323nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7324 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7325 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7326 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007327 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7328 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7329 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007330};
7331
Thomas Pedersen84f10702012-07-12 16:17:33 -07007332static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007333 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007334{
7335 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7336 struct wireless_dev *wdev;
7337 struct net_device *dev = info->user_ptr[1];
7338
Johannes Bergd9d8b012012-11-26 12:51:52 +01007339 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007340 return -EINVAL;
7341
7342 wdev = dev->ieee80211_ptr;
7343
7344 if (!rdev->ops->set_cqm_txe_config)
7345 return -EOPNOTSUPP;
7346
7347 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7348 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7349 return -EOPNOTSUPP;
7350
Hila Gonene35e4d22012-06-27 17:19:42 +03007351 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007352}
7353
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007354static int nl80211_set_cqm_rssi(struct genl_info *info,
7355 s32 threshold, u32 hysteresis)
7356{
Johannes Berg4c476992010-10-04 21:36:35 +02007357 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007358 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007359 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007360
7361 if (threshold > 0)
7362 return -EINVAL;
7363
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007364 wdev = dev->ieee80211_ptr;
7365
Johannes Berg4c476992010-10-04 21:36:35 +02007366 if (!rdev->ops->set_cqm_rssi_config)
7367 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007368
Johannes Berg074ac8d2010-09-16 14:58:22 +02007369 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007370 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7371 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007372
Hila Gonene35e4d22012-06-27 17:19:42 +03007373 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007374}
7375
7376static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7377{
7378 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7379 struct nlattr *cqm;
7380 int err;
7381
7382 cqm = info->attrs[NL80211_ATTR_CQM];
7383 if (!cqm) {
7384 err = -EINVAL;
7385 goto out;
7386 }
7387
7388 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7389 nl80211_attr_cqm_policy);
7390 if (err)
7391 goto out;
7392
7393 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7394 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7395 s32 threshold;
7396 u32 hysteresis;
7397 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7398 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7399 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007400 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7401 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7402 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7403 u32 rate, pkts, intvl;
7404 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7405 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7406 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7407 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007408 } else
7409 err = -EINVAL;
7410
7411out:
7412 return err;
7413}
7414
Johannes Berg29cbe682010-12-03 09:20:44 +01007415static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7416{
7417 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7418 struct net_device *dev = info->user_ptr[1];
7419 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007420 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007421 int err;
7422
7423 /* start with default */
7424 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007425 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007426
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007427 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007428 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007429 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007430 if (err)
7431 return err;
7432 }
7433
7434 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7435 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7436 return -EINVAL;
7437
Javier Cardonac80d5452010-12-16 17:37:49 -08007438 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7439 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7440
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007441 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7442 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7443 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7444 return -EINVAL;
7445
Marco Porsch9bdbf042013-01-07 16:04:51 +01007446 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7447 setup.beacon_interval =
7448 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7449 if (setup.beacon_interval < 10 ||
7450 setup.beacon_interval > 10000)
7451 return -EINVAL;
7452 }
7453
7454 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7455 setup.dtim_period =
7456 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7457 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7458 return -EINVAL;
7459 }
7460
Javier Cardonac80d5452010-12-16 17:37:49 -08007461 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7462 /* parse additional setup parameters if given */
7463 err = nl80211_parse_mesh_setup(info, &setup);
7464 if (err)
7465 return err;
7466 }
7467
Thomas Pedersend37bb182013-03-04 13:06:13 -08007468 if (setup.user_mpm)
7469 cfg.auto_open_plinks = false;
7470
Johannes Bergcc1d2802012-05-16 23:50:20 +02007471 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007472 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7473 if (err)
7474 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007475 } else {
7476 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007477 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007478 }
7479
Javier Cardonac80d5452010-12-16 17:37:49 -08007480 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007481}
7482
7483static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7484{
7485 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7486 struct net_device *dev = info->user_ptr[1];
7487
7488 return cfg80211_leave_mesh(rdev, dev);
7489}
7490
Johannes Bergdfb89c52012-06-27 09:23:48 +02007491#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007492static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7493 struct cfg80211_registered_device *rdev)
7494{
7495 struct nlattr *nl_pats, *nl_pat;
7496 int i, pat_len;
7497
7498 if (!rdev->wowlan->n_patterns)
7499 return 0;
7500
7501 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7502 if (!nl_pats)
7503 return -ENOBUFS;
7504
7505 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7506 nl_pat = nla_nest_start(msg, i + 1);
7507 if (!nl_pat)
7508 return -ENOBUFS;
7509 pat_len = rdev->wowlan->patterns[i].pattern_len;
7510 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7511 DIV_ROUND_UP(pat_len, 8),
7512 rdev->wowlan->patterns[i].mask) ||
7513 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7514 pat_len, rdev->wowlan->patterns[i].pattern) ||
7515 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7516 rdev->wowlan->patterns[i].pkt_offset))
7517 return -ENOBUFS;
7518 nla_nest_end(msg, nl_pat);
7519 }
7520 nla_nest_end(msg, nl_pats);
7521
7522 return 0;
7523}
7524
Johannes Berg2a0e0472013-01-23 22:57:40 +01007525static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7526 struct cfg80211_wowlan_tcp *tcp)
7527{
7528 struct nlattr *nl_tcp;
7529
7530 if (!tcp)
7531 return 0;
7532
7533 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7534 if (!nl_tcp)
7535 return -ENOBUFS;
7536
7537 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7538 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7539 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7540 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7541 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7542 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7543 tcp->payload_len, tcp->payload) ||
7544 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7545 tcp->data_interval) ||
7546 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7547 tcp->wake_len, tcp->wake_data) ||
7548 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7549 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7550 return -ENOBUFS;
7551
7552 if (tcp->payload_seq.len &&
7553 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7554 sizeof(tcp->payload_seq), &tcp->payload_seq))
7555 return -ENOBUFS;
7556
7557 if (tcp->payload_tok.len &&
7558 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7559 sizeof(tcp->payload_tok) + tcp->tokens_size,
7560 &tcp->payload_tok))
7561 return -ENOBUFS;
7562
7563 return 0;
7564}
7565
Johannes Bergff1b6e62011-05-04 15:37:28 +02007566static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7567{
7568 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7569 struct sk_buff *msg;
7570 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007571 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007572
Johannes Berg2a0e0472013-01-23 22:57:40 +01007573 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7574 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007575 return -EOPNOTSUPP;
7576
Johannes Berg2a0e0472013-01-23 22:57:40 +01007577 if (rdev->wowlan && rdev->wowlan->tcp) {
7578 /* adjust size to have room for all the data */
7579 size += rdev->wowlan->tcp->tokens_size +
7580 rdev->wowlan->tcp->payload_len +
7581 rdev->wowlan->tcp->wake_len +
7582 rdev->wowlan->tcp->wake_len / 8;
7583 }
7584
7585 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007586 if (!msg)
7587 return -ENOMEM;
7588
Eric W. Biederman15e47302012-09-07 20:12:54 +00007589 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007590 NL80211_CMD_GET_WOWLAN);
7591 if (!hdr)
7592 goto nla_put_failure;
7593
7594 if (rdev->wowlan) {
7595 struct nlattr *nl_wowlan;
7596
7597 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7598 if (!nl_wowlan)
7599 goto nla_put_failure;
7600
David S. Miller9360ffd2012-03-29 04:41:26 -04007601 if ((rdev->wowlan->any &&
7602 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7603 (rdev->wowlan->disconnect &&
7604 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7605 (rdev->wowlan->magic_pkt &&
7606 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7607 (rdev->wowlan->gtk_rekey_failure &&
7608 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7609 (rdev->wowlan->eap_identity_req &&
7610 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7611 (rdev->wowlan->four_way_handshake &&
7612 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7613 (rdev->wowlan->rfkill_release &&
7614 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7615 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007616
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007617 if (nl80211_send_wowlan_patterns(msg, rdev))
7618 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007619
7620 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7621 goto nla_put_failure;
7622
Johannes Bergff1b6e62011-05-04 15:37:28 +02007623 nla_nest_end(msg, nl_wowlan);
7624 }
7625
7626 genlmsg_end(msg, hdr);
7627 return genlmsg_reply(msg, info);
7628
7629nla_put_failure:
7630 nlmsg_free(msg);
7631 return -ENOBUFS;
7632}
7633
Johannes Berg2a0e0472013-01-23 22:57:40 +01007634static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7635 struct nlattr *attr,
7636 struct cfg80211_wowlan *trig)
7637{
7638 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7639 struct cfg80211_wowlan_tcp *cfg;
7640 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7641 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7642 u32 size;
7643 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7644 int err, port;
7645
7646 if (!rdev->wiphy.wowlan.tcp)
7647 return -EINVAL;
7648
7649 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7650 nla_data(attr), nla_len(attr),
7651 nl80211_wowlan_tcp_policy);
7652 if (err)
7653 return err;
7654
7655 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7656 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7657 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7658 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7659 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7660 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7661 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7662 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7663 return -EINVAL;
7664
7665 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7666 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7667 return -EINVAL;
7668
7669 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg723d5682013-02-26 13:56:40 +01007670 rdev->wiphy.wowlan.tcp->data_interval_max ||
7671 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007672 return -EINVAL;
7673
7674 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7675 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7676 return -EINVAL;
7677
7678 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7679 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7680 return -EINVAL;
7681
7682 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7683 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7684
7685 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7686 tokens_size = tokln - sizeof(*tok);
7687
7688 if (!tok->len || tokens_size % tok->len)
7689 return -EINVAL;
7690 if (!rdev->wiphy.wowlan.tcp->tok)
7691 return -EINVAL;
7692 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7693 return -EINVAL;
7694 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7695 return -EINVAL;
7696 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7697 return -EINVAL;
7698 if (tok->offset + tok->len > data_size)
7699 return -EINVAL;
7700 }
7701
7702 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7703 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7704 if (!rdev->wiphy.wowlan.tcp->seq)
7705 return -EINVAL;
7706 if (seq->len == 0 || seq->len > 4)
7707 return -EINVAL;
7708 if (seq->len + seq->offset > data_size)
7709 return -EINVAL;
7710 }
7711
7712 size = sizeof(*cfg);
7713 size += data_size;
7714 size += wake_size + wake_mask_size;
7715 size += tokens_size;
7716
7717 cfg = kzalloc(size, GFP_KERNEL);
7718 if (!cfg)
7719 return -ENOMEM;
7720 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7721 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7722 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7723 ETH_ALEN);
7724 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7725 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7726 else
7727 port = 0;
7728#ifdef CONFIG_INET
7729 /* allocate a socket and port for it and use it */
7730 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7731 IPPROTO_TCP, &cfg->sock, 1);
7732 if (err) {
7733 kfree(cfg);
7734 return err;
7735 }
7736 if (inet_csk_get_port(cfg->sock->sk, port)) {
7737 sock_release(cfg->sock);
7738 kfree(cfg);
7739 return -EADDRINUSE;
7740 }
7741 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7742#else
7743 if (!port) {
7744 kfree(cfg);
7745 return -EINVAL;
7746 }
7747 cfg->src_port = port;
7748#endif
7749
7750 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7751 cfg->payload_len = data_size;
7752 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7753 memcpy((void *)cfg->payload,
7754 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7755 data_size);
7756 if (seq)
7757 cfg->payload_seq = *seq;
7758 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7759 cfg->wake_len = wake_size;
7760 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7761 memcpy((void *)cfg->wake_data,
7762 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7763 wake_size);
7764 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7765 data_size + wake_size;
7766 memcpy((void *)cfg->wake_mask,
7767 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7768 wake_mask_size);
7769 if (tok) {
7770 cfg->tokens_size = tokens_size;
7771 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7772 }
7773
7774 trig->tcp = cfg;
7775
7776 return 0;
7777}
7778
Johannes Bergff1b6e62011-05-04 15:37:28 +02007779static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7780{
7781 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7782 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007783 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007784 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007785 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7786 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007787 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007788
Johannes Berg2a0e0472013-01-23 22:57:40 +01007789 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7790 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007791 return -EOPNOTSUPP;
7792
Johannes Bergae33bd82012-07-12 16:25:02 +02007793 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7794 cfg80211_rdev_free_wowlan(rdev);
7795 rdev->wowlan = NULL;
7796 goto set_wakeup;
7797 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007798
7799 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7800 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7801 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7802 nl80211_wowlan_policy);
7803 if (err)
7804 return err;
7805
7806 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7807 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7808 return -EINVAL;
7809 new_triggers.any = true;
7810 }
7811
7812 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7813 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7814 return -EINVAL;
7815 new_triggers.disconnect = true;
7816 }
7817
7818 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7819 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7820 return -EINVAL;
7821 new_triggers.magic_pkt = true;
7822 }
7823
Johannes Berg77dbbb12011-07-13 10:48:55 +02007824 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7825 return -EINVAL;
7826
7827 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7828 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7829 return -EINVAL;
7830 new_triggers.gtk_rekey_failure = true;
7831 }
7832
7833 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7834 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7835 return -EINVAL;
7836 new_triggers.eap_identity_req = true;
7837 }
7838
7839 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7840 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7841 return -EINVAL;
7842 new_triggers.four_way_handshake = true;
7843 }
7844
7845 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7846 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7847 return -EINVAL;
7848 new_triggers.rfkill_release = true;
7849 }
7850
Johannes Bergff1b6e62011-05-04 15:37:28 +02007851 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7852 struct nlattr *pat;
7853 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007854 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007855 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7856
7857 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7858 rem)
7859 n_patterns++;
7860 if (n_patterns > wowlan->n_patterns)
7861 return -EINVAL;
7862
7863 new_triggers.patterns = kcalloc(n_patterns,
7864 sizeof(new_triggers.patterns[0]),
7865 GFP_KERNEL);
7866 if (!new_triggers.patterns)
7867 return -ENOMEM;
7868
7869 new_triggers.n_patterns = n_patterns;
7870 i = 0;
7871
7872 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7873 rem) {
7874 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7875 nla_data(pat), nla_len(pat), NULL);
7876 err = -EINVAL;
7877 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7878 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7879 goto error;
7880 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7881 mask_len = DIV_ROUND_UP(pat_len, 8);
7882 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7883 mask_len)
7884 goto error;
7885 if (pat_len > wowlan->pattern_max_len ||
7886 pat_len < wowlan->pattern_min_len)
7887 goto error;
7888
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007889 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7890 pkt_offset = 0;
7891 else
7892 pkt_offset = nla_get_u32(
7893 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7894 if (pkt_offset > wowlan->max_pkt_offset)
7895 goto error;
7896 new_triggers.patterns[i].pkt_offset = pkt_offset;
7897
Johannes Bergff1b6e62011-05-04 15:37:28 +02007898 new_triggers.patterns[i].mask =
7899 kmalloc(mask_len + pat_len, GFP_KERNEL);
7900 if (!new_triggers.patterns[i].mask) {
7901 err = -ENOMEM;
7902 goto error;
7903 }
7904 new_triggers.patterns[i].pattern =
7905 new_triggers.patterns[i].mask + mask_len;
7906 memcpy(new_triggers.patterns[i].mask,
7907 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7908 mask_len);
7909 new_triggers.patterns[i].pattern_len = pat_len;
7910 memcpy(new_triggers.patterns[i].pattern,
7911 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7912 pat_len);
7913 i++;
7914 }
7915 }
7916
Johannes Berg2a0e0472013-01-23 22:57:40 +01007917 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7918 err = nl80211_parse_wowlan_tcp(
7919 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7920 &new_triggers);
7921 if (err)
7922 goto error;
7923 }
7924
Johannes Bergae33bd82012-07-12 16:25:02 +02007925 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7926 if (!ntrig) {
7927 err = -ENOMEM;
7928 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007929 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007930 cfg80211_rdev_free_wowlan(rdev);
7931 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007932
Johannes Bergae33bd82012-07-12 16:25:02 +02007933 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007934 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007935 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007936
Johannes Bergff1b6e62011-05-04 15:37:28 +02007937 return 0;
7938 error:
7939 for (i = 0; i < new_triggers.n_patterns; i++)
7940 kfree(new_triggers.patterns[i].mask);
7941 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007942 if (new_triggers.tcp && new_triggers.tcp->sock)
7943 sock_release(new_triggers.tcp->sock);
7944 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007945 return err;
7946}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007947#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007948
Johannes Berge5497d72011-07-05 16:35:40 +02007949static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7950{
7951 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7952 struct net_device *dev = info->user_ptr[1];
7953 struct wireless_dev *wdev = dev->ieee80211_ptr;
7954 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7955 struct cfg80211_gtk_rekey_data rekey_data;
7956 int err;
7957
7958 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7959 return -EINVAL;
7960
7961 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7962 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7963 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7964 nl80211_rekey_policy);
7965 if (err)
7966 return err;
7967
7968 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7969 return -ERANGE;
7970 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7971 return -ERANGE;
7972 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7973 return -ERANGE;
7974
7975 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7976 NL80211_KEK_LEN);
7977 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7978 NL80211_KCK_LEN);
7979 memcpy(rekey_data.replay_ctr,
7980 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7981 NL80211_REPLAY_CTR_LEN);
7982
7983 wdev_lock(wdev);
7984 if (!wdev->current_bss) {
7985 err = -ENOTCONN;
7986 goto out;
7987 }
7988
7989 if (!rdev->ops->set_rekey_data) {
7990 err = -EOPNOTSUPP;
7991 goto out;
7992 }
7993
Hila Gonene35e4d22012-06-27 17:19:42 +03007994 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007995 out:
7996 wdev_unlock(wdev);
7997 return err;
7998}
7999
Johannes Berg28946da2011-11-04 11:18:12 +01008000static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8001 struct genl_info *info)
8002{
8003 struct net_device *dev = info->user_ptr[1];
8004 struct wireless_dev *wdev = dev->ieee80211_ptr;
8005
8006 if (wdev->iftype != NL80211_IFTYPE_AP &&
8007 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8008 return -EINVAL;
8009
Eric W. Biederman15e47302012-09-07 20:12:54 +00008010 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008011 return -EBUSY;
8012
Eric W. Biederman15e47302012-09-07 20:12:54 +00008013 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008014 return 0;
8015}
8016
Johannes Berg7f6cf312011-11-04 11:18:15 +01008017static int nl80211_probe_client(struct sk_buff *skb,
8018 struct genl_info *info)
8019{
8020 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8021 struct net_device *dev = info->user_ptr[1];
8022 struct wireless_dev *wdev = dev->ieee80211_ptr;
8023 struct sk_buff *msg;
8024 void *hdr;
8025 const u8 *addr;
8026 u64 cookie;
8027 int err;
8028
8029 if (wdev->iftype != NL80211_IFTYPE_AP &&
8030 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8031 return -EOPNOTSUPP;
8032
8033 if (!info->attrs[NL80211_ATTR_MAC])
8034 return -EINVAL;
8035
8036 if (!rdev->ops->probe_client)
8037 return -EOPNOTSUPP;
8038
8039 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8040 if (!msg)
8041 return -ENOMEM;
8042
Eric W. Biederman15e47302012-09-07 20:12:54 +00008043 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008044 NL80211_CMD_PROBE_CLIENT);
8045
8046 if (IS_ERR(hdr)) {
8047 err = PTR_ERR(hdr);
8048 goto free_msg;
8049 }
8050
8051 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8052
Hila Gonene35e4d22012-06-27 17:19:42 +03008053 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008054 if (err)
8055 goto free_msg;
8056
David S. Miller9360ffd2012-03-29 04:41:26 -04008057 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8058 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008059
8060 genlmsg_end(msg, hdr);
8061
8062 return genlmsg_reply(msg, info);
8063
8064 nla_put_failure:
8065 err = -ENOBUFS;
8066 free_msg:
8067 nlmsg_free(msg);
8068 return err;
8069}
8070
Johannes Berg5e7602302011-11-04 11:18:17 +01008071static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8072{
8073 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008074 struct cfg80211_beacon_registration *reg, *nreg;
8075 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008076
8077 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8078 return -EOPNOTSUPP;
8079
Ben Greear37c73b52012-10-26 14:49:25 -07008080 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8081 if (!nreg)
8082 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01008083
Ben Greear37c73b52012-10-26 14:49:25 -07008084 /* First, check if already registered. */
8085 spin_lock_bh(&rdev->beacon_registrations_lock);
8086 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8087 if (reg->nlportid == info->snd_portid) {
8088 rv = -EALREADY;
8089 goto out_err;
8090 }
8091 }
8092 /* Add it to the list */
8093 nreg->nlportid = info->snd_portid;
8094 list_add(&nreg->list, &rdev->beacon_registrations);
8095
8096 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01008097
8098 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008099out_err:
8100 spin_unlock_bh(&rdev->beacon_registrations_lock);
8101 kfree(nreg);
8102 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008103}
8104
Johannes Berg98104fde2012-06-16 00:19:54 +02008105static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8106{
8107 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8108 struct wireless_dev *wdev = info->user_ptr[1];
8109 int err;
8110
8111 if (!rdev->ops->start_p2p_device)
8112 return -EOPNOTSUPP;
8113
8114 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8115 return -EOPNOTSUPP;
8116
8117 if (wdev->p2p_started)
8118 return 0;
8119
8120 mutex_lock(&rdev->devlist_mtx);
8121 err = cfg80211_can_add_interface(rdev, wdev->iftype);
8122 mutex_unlock(&rdev->devlist_mtx);
8123 if (err)
8124 return err;
8125
Johannes Bergeeb126e2012-10-23 15:16:50 +02008126 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008127 if (err)
8128 return err;
8129
8130 wdev->p2p_started = true;
8131 mutex_lock(&rdev->devlist_mtx);
8132 rdev->opencount++;
8133 mutex_unlock(&rdev->devlist_mtx);
8134
8135 return 0;
8136}
8137
8138static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8139{
8140 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8141 struct wireless_dev *wdev = info->user_ptr[1];
8142
8143 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8144 return -EOPNOTSUPP;
8145
8146 if (!rdev->ops->stop_p2p_device)
8147 return -EOPNOTSUPP;
8148
8149 if (!wdev->p2p_started)
8150 return 0;
8151
Johannes Bergeeb126e2012-10-23 15:16:50 +02008152 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008153 wdev->p2p_started = false;
8154
8155 mutex_lock(&rdev->devlist_mtx);
8156 rdev->opencount--;
8157 mutex_unlock(&rdev->devlist_mtx);
8158
8159 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
8160 rdev->scan_req->aborted = true;
8161 ___cfg80211_scan_done(rdev, true);
8162 }
8163
8164 return 0;
8165}
8166
Johannes Berg3713b4e2013-02-14 16:19:38 +01008167static int nl80211_get_protocol_features(struct sk_buff *skb,
8168 struct genl_info *info)
8169{
8170 void *hdr;
8171 struct sk_buff *msg;
8172
8173 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8174 if (!msg)
8175 return -ENOMEM;
8176
8177 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8178 NL80211_CMD_GET_PROTOCOL_FEATURES);
8179 if (!hdr)
8180 goto nla_put_failure;
8181
8182 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8183 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8184 goto nla_put_failure;
8185
8186 genlmsg_end(msg, hdr);
8187 return genlmsg_reply(msg, info);
8188
8189 nla_put_failure:
8190 kfree_skb(msg);
8191 return -ENOBUFS;
8192}
8193
Jouni Malinen355199e2013-02-27 17:14:27 +02008194static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8195{
8196 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8197 struct cfg80211_update_ft_ies_params ft_params;
8198 struct net_device *dev = info->user_ptr[1];
8199
8200 if (!rdev->ops->update_ft_ies)
8201 return -EOPNOTSUPP;
8202
8203 if (!info->attrs[NL80211_ATTR_MDID] ||
8204 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8205 return -EINVAL;
8206
8207 memset(&ft_params, 0, sizeof(ft_params));
8208 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8209 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8210 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8211
8212 return rdev_update_ft_ies(rdev, dev, &ft_params);
8213}
8214
Johannes Berg4c476992010-10-04 21:36:35 +02008215#define NL80211_FLAG_NEED_WIPHY 0x01
8216#define NL80211_FLAG_NEED_NETDEV 0x02
8217#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008218#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8219#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8220 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008221#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008222/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008223#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8224 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008225
8226static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8227 struct genl_info *info)
8228{
8229 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008230 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008231 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008232 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8233
8234 if (rtnl)
8235 rtnl_lock();
8236
8237 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008238 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008239 if (IS_ERR(rdev)) {
8240 if (rtnl)
8241 rtnl_unlock();
8242 return PTR_ERR(rdev);
8243 }
8244 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008245 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8246 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008247 mutex_lock(&cfg80211_mutex);
8248 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8249 info->attrs);
8250 if (IS_ERR(wdev)) {
8251 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02008252 if (rtnl)
8253 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02008254 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02008255 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008256
Johannes Berg89a54e42012-06-15 14:33:17 +02008257 dev = wdev->netdev;
8258 rdev = wiphy_to_dev(wdev->wiphy);
8259
Johannes Berg1bf614e2012-06-15 15:23:36 +02008260 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8261 if (!dev) {
8262 mutex_unlock(&cfg80211_mutex);
8263 if (rtnl)
8264 rtnl_unlock();
8265 return -EINVAL;
8266 }
8267
8268 info->user_ptr[1] = dev;
8269 } else {
8270 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02008271 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008272
Johannes Berg1bf614e2012-06-15 15:23:36 +02008273 if (dev) {
8274 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8275 !netif_running(dev)) {
8276 mutex_unlock(&cfg80211_mutex);
8277 if (rtnl)
8278 rtnl_unlock();
8279 return -ENETDOWN;
8280 }
8281
8282 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008283 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8284 if (!wdev->p2p_started) {
8285 mutex_unlock(&cfg80211_mutex);
8286 if (rtnl)
8287 rtnl_unlock();
8288 return -ENETDOWN;
8289 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02008290 }
8291
Johannes Berg89a54e42012-06-15 14:33:17 +02008292 cfg80211_lock_rdev(rdev);
8293
8294 mutex_unlock(&cfg80211_mutex);
8295
Johannes Berg4c476992010-10-04 21:36:35 +02008296 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008297 }
8298
8299 return 0;
8300}
8301
8302static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8303 struct genl_info *info)
8304{
8305 if (info->user_ptr[0])
8306 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02008307 if (info->user_ptr[1]) {
8308 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8309 struct wireless_dev *wdev = info->user_ptr[1];
8310
8311 if (wdev->netdev)
8312 dev_put(wdev->netdev);
8313 } else {
8314 dev_put(info->user_ptr[1]);
8315 }
8316 }
Johannes Berg4c476992010-10-04 21:36:35 +02008317 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8318 rtnl_unlock();
8319}
8320
Johannes Berg55682962007-09-20 13:09:35 -04008321static struct genl_ops nl80211_ops[] = {
8322 {
8323 .cmd = NL80211_CMD_GET_WIPHY,
8324 .doit = nl80211_get_wiphy,
8325 .dumpit = nl80211_dump_wiphy,
8326 .policy = nl80211_policy,
8327 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008328 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04008329 },
8330 {
8331 .cmd = NL80211_CMD_SET_WIPHY,
8332 .doit = nl80211_set_wiphy,
8333 .policy = nl80211_policy,
8334 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008335 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008336 },
8337 {
8338 .cmd = NL80211_CMD_GET_INTERFACE,
8339 .doit = nl80211_get_interface,
8340 .dumpit = nl80211_dump_interface,
8341 .policy = nl80211_policy,
8342 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02008343 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04008344 },
8345 {
8346 .cmd = NL80211_CMD_SET_INTERFACE,
8347 .doit = nl80211_set_interface,
8348 .policy = nl80211_policy,
8349 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008350 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8351 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008352 },
8353 {
8354 .cmd = NL80211_CMD_NEW_INTERFACE,
8355 .doit = nl80211_new_interface,
8356 .policy = nl80211_policy,
8357 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008358 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8359 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008360 },
8361 {
8362 .cmd = NL80211_CMD_DEL_INTERFACE,
8363 .doit = nl80211_del_interface,
8364 .policy = nl80211_policy,
8365 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02008366 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008367 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008368 },
Johannes Berg41ade002007-12-19 02:03:29 +01008369 {
8370 .cmd = NL80211_CMD_GET_KEY,
8371 .doit = nl80211_get_key,
8372 .policy = nl80211_policy,
8373 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008374 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008375 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008376 },
8377 {
8378 .cmd = NL80211_CMD_SET_KEY,
8379 .doit = nl80211_set_key,
8380 .policy = nl80211_policy,
8381 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008382 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008383 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008384 },
8385 {
8386 .cmd = NL80211_CMD_NEW_KEY,
8387 .doit = nl80211_new_key,
8388 .policy = nl80211_policy,
8389 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008390 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008391 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008392 },
8393 {
8394 .cmd = NL80211_CMD_DEL_KEY,
8395 .doit = nl80211_del_key,
8396 .policy = nl80211_policy,
8397 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008398 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008399 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008400 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008401 {
8402 .cmd = NL80211_CMD_SET_BEACON,
8403 .policy = nl80211_policy,
8404 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008405 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008406 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008407 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008408 },
8409 {
Johannes Berg88600202012-02-13 15:17:18 +01008410 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008411 .policy = nl80211_policy,
8412 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008413 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008414 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008415 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008416 },
8417 {
Johannes Berg88600202012-02-13 15:17:18 +01008418 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008419 .policy = nl80211_policy,
8420 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008421 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008422 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008423 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008424 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008425 {
8426 .cmd = NL80211_CMD_GET_STATION,
8427 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008428 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008429 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008430 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8431 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008432 },
8433 {
8434 .cmd = NL80211_CMD_SET_STATION,
8435 .doit = nl80211_set_station,
8436 .policy = nl80211_policy,
8437 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008438 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008439 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008440 },
8441 {
8442 .cmd = NL80211_CMD_NEW_STATION,
8443 .doit = nl80211_new_station,
8444 .policy = nl80211_policy,
8445 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008446 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008447 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008448 },
8449 {
8450 .cmd = NL80211_CMD_DEL_STATION,
8451 .doit = nl80211_del_station,
8452 .policy = nl80211_policy,
8453 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008454 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008455 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008456 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008457 {
8458 .cmd = NL80211_CMD_GET_MPATH,
8459 .doit = nl80211_get_mpath,
8460 .dumpit = nl80211_dump_mpath,
8461 .policy = nl80211_policy,
8462 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008463 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008464 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008465 },
8466 {
8467 .cmd = NL80211_CMD_SET_MPATH,
8468 .doit = nl80211_set_mpath,
8469 .policy = nl80211_policy,
8470 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008471 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008472 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008473 },
8474 {
8475 .cmd = NL80211_CMD_NEW_MPATH,
8476 .doit = nl80211_new_mpath,
8477 .policy = nl80211_policy,
8478 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008479 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008480 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008481 },
8482 {
8483 .cmd = NL80211_CMD_DEL_MPATH,
8484 .doit = nl80211_del_mpath,
8485 .policy = nl80211_policy,
8486 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008487 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008488 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008489 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008490 {
8491 .cmd = NL80211_CMD_SET_BSS,
8492 .doit = nl80211_set_bss,
8493 .policy = nl80211_policy,
8494 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008495 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008496 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008497 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008498 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008499 .cmd = NL80211_CMD_GET_REG,
8500 .doit = nl80211_get_reg,
8501 .policy = nl80211_policy,
8502 /* can be retrieved by unprivileged users */
8503 },
8504 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008505 .cmd = NL80211_CMD_SET_REG,
8506 .doit = nl80211_set_reg,
8507 .policy = nl80211_policy,
8508 .flags = GENL_ADMIN_PERM,
8509 },
8510 {
8511 .cmd = NL80211_CMD_REQ_SET_REG,
8512 .doit = nl80211_req_set_reg,
8513 .policy = nl80211_policy,
8514 .flags = GENL_ADMIN_PERM,
8515 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008516 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008517 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8518 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008519 .policy = nl80211_policy,
8520 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008521 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008522 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008523 },
8524 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008525 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8526 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008527 .policy = nl80211_policy,
8528 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008529 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008530 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008531 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008532 {
Johannes Berg2a519312009-02-10 21:25:55 +01008533 .cmd = NL80211_CMD_TRIGGER_SCAN,
8534 .doit = nl80211_trigger_scan,
8535 .policy = nl80211_policy,
8536 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008537 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008538 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008539 },
8540 {
8541 .cmd = NL80211_CMD_GET_SCAN,
8542 .policy = nl80211_policy,
8543 .dumpit = nl80211_dump_scan,
8544 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008545 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008546 .cmd = NL80211_CMD_START_SCHED_SCAN,
8547 .doit = nl80211_start_sched_scan,
8548 .policy = nl80211_policy,
8549 .flags = GENL_ADMIN_PERM,
8550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8551 NL80211_FLAG_NEED_RTNL,
8552 },
8553 {
8554 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8555 .doit = nl80211_stop_sched_scan,
8556 .policy = nl80211_policy,
8557 .flags = GENL_ADMIN_PERM,
8558 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8559 NL80211_FLAG_NEED_RTNL,
8560 },
8561 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008562 .cmd = NL80211_CMD_AUTHENTICATE,
8563 .doit = nl80211_authenticate,
8564 .policy = nl80211_policy,
8565 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008566 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008567 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008568 },
8569 {
8570 .cmd = NL80211_CMD_ASSOCIATE,
8571 .doit = nl80211_associate,
8572 .policy = nl80211_policy,
8573 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008574 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008575 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008576 },
8577 {
8578 .cmd = NL80211_CMD_DEAUTHENTICATE,
8579 .doit = nl80211_deauthenticate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008584 },
8585 {
8586 .cmd = NL80211_CMD_DISASSOCIATE,
8587 .doit = nl80211_disassociate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008592 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008593 {
8594 .cmd = NL80211_CMD_JOIN_IBSS,
8595 .doit = nl80211_join_ibss,
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,
Johannes Berg04a773a2009-04-19 21:24:32 +02008600 },
8601 {
8602 .cmd = NL80211_CMD_LEAVE_IBSS,
8603 .doit = nl80211_leave_ibss,
8604 .policy = nl80211_policy,
8605 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008606 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008607 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008608 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008609#ifdef CONFIG_NL80211_TESTMODE
8610 {
8611 .cmd = NL80211_CMD_TESTMODE,
8612 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008613 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008614 .policy = nl80211_policy,
8615 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008616 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8617 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008618 },
8619#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008620 {
8621 .cmd = NL80211_CMD_CONNECT,
8622 .doit = nl80211_connect,
8623 .policy = nl80211_policy,
8624 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008625 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008626 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008627 },
8628 {
8629 .cmd = NL80211_CMD_DISCONNECT,
8630 .doit = nl80211_disconnect,
8631 .policy = nl80211_policy,
8632 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008633 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008634 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008635 },
Johannes Berg463d0182009-07-14 00:33:35 +02008636 {
8637 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8638 .doit = nl80211_wiphy_netns,
8639 .policy = nl80211_policy,
8640 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008641 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8642 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008643 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008644 {
8645 .cmd = NL80211_CMD_GET_SURVEY,
8646 .policy = nl80211_policy,
8647 .dumpit = nl80211_dump_survey,
8648 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008649 {
8650 .cmd = NL80211_CMD_SET_PMKSA,
8651 .doit = nl80211_setdel_pmksa,
8652 .policy = nl80211_policy,
8653 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008654 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008655 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008656 },
8657 {
8658 .cmd = NL80211_CMD_DEL_PMKSA,
8659 .doit = nl80211_setdel_pmksa,
8660 .policy = nl80211_policy,
8661 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008662 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008663 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008664 },
8665 {
8666 .cmd = NL80211_CMD_FLUSH_PMKSA,
8667 .doit = nl80211_flush_pmksa,
8668 .policy = nl80211_policy,
8669 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008670 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008671 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008672 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008673 {
8674 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8675 .doit = nl80211_remain_on_channel,
8676 .policy = nl80211_policy,
8677 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008678 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008679 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008680 },
8681 {
8682 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8683 .doit = nl80211_cancel_remain_on_channel,
8684 .policy = nl80211_policy,
8685 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008686 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008687 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008688 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008689 {
8690 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8691 .doit = nl80211_set_tx_bitrate_mask,
8692 .policy = nl80211_policy,
8693 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008694 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8695 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008696 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008697 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008698 .cmd = NL80211_CMD_REGISTER_FRAME,
8699 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008700 .policy = nl80211_policy,
8701 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008702 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008703 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008704 },
8705 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008706 .cmd = NL80211_CMD_FRAME,
8707 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008708 .policy = nl80211_policy,
8709 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008710 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008711 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008712 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008713 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008714 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8715 .doit = nl80211_tx_mgmt_cancel_wait,
8716 .policy = nl80211_policy,
8717 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008718 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008719 NL80211_FLAG_NEED_RTNL,
8720 },
8721 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008722 .cmd = NL80211_CMD_SET_POWER_SAVE,
8723 .doit = nl80211_set_power_save,
8724 .policy = nl80211_policy,
8725 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008726 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8727 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008728 },
8729 {
8730 .cmd = NL80211_CMD_GET_POWER_SAVE,
8731 .doit = nl80211_get_power_save,
8732 .policy = nl80211_policy,
8733 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008734 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8735 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008736 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008737 {
8738 .cmd = NL80211_CMD_SET_CQM,
8739 .doit = nl80211_set_cqm,
8740 .policy = nl80211_policy,
8741 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008742 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8743 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008744 },
Johannes Bergf444de02010-05-05 15:25:02 +02008745 {
8746 .cmd = NL80211_CMD_SET_CHANNEL,
8747 .doit = nl80211_set_channel,
8748 .policy = nl80211_policy,
8749 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008750 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8751 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008752 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008753 {
8754 .cmd = NL80211_CMD_SET_WDS_PEER,
8755 .doit = nl80211_set_wds_peer,
8756 .policy = nl80211_policy,
8757 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008758 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8759 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008760 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008761 {
8762 .cmd = NL80211_CMD_JOIN_MESH,
8763 .doit = nl80211_join_mesh,
8764 .policy = nl80211_policy,
8765 .flags = GENL_ADMIN_PERM,
8766 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8767 NL80211_FLAG_NEED_RTNL,
8768 },
8769 {
8770 .cmd = NL80211_CMD_LEAVE_MESH,
8771 .doit = nl80211_leave_mesh,
8772 .policy = nl80211_policy,
8773 .flags = GENL_ADMIN_PERM,
8774 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8775 NL80211_FLAG_NEED_RTNL,
8776 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008777#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008778 {
8779 .cmd = NL80211_CMD_GET_WOWLAN,
8780 .doit = nl80211_get_wowlan,
8781 .policy = nl80211_policy,
8782 /* can be retrieved by unprivileged users */
8783 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8784 NL80211_FLAG_NEED_RTNL,
8785 },
8786 {
8787 .cmd = NL80211_CMD_SET_WOWLAN,
8788 .doit = nl80211_set_wowlan,
8789 .policy = nl80211_policy,
8790 .flags = GENL_ADMIN_PERM,
8791 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8792 NL80211_FLAG_NEED_RTNL,
8793 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008794#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008795 {
8796 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8797 .doit = nl80211_set_rekey_data,
8798 .policy = nl80211_policy,
8799 .flags = GENL_ADMIN_PERM,
8800 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8801 NL80211_FLAG_NEED_RTNL,
8802 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008803 {
8804 .cmd = NL80211_CMD_TDLS_MGMT,
8805 .doit = nl80211_tdls_mgmt,
8806 .policy = nl80211_policy,
8807 .flags = GENL_ADMIN_PERM,
8808 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8809 NL80211_FLAG_NEED_RTNL,
8810 },
8811 {
8812 .cmd = NL80211_CMD_TDLS_OPER,
8813 .doit = nl80211_tdls_oper,
8814 .policy = nl80211_policy,
8815 .flags = GENL_ADMIN_PERM,
8816 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8817 NL80211_FLAG_NEED_RTNL,
8818 },
Johannes Berg28946da2011-11-04 11:18:12 +01008819 {
8820 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8821 .doit = nl80211_register_unexpected_frame,
8822 .policy = nl80211_policy,
8823 .flags = GENL_ADMIN_PERM,
8824 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8825 NL80211_FLAG_NEED_RTNL,
8826 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008827 {
8828 .cmd = NL80211_CMD_PROBE_CLIENT,
8829 .doit = nl80211_probe_client,
8830 .policy = nl80211_policy,
8831 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008832 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008833 NL80211_FLAG_NEED_RTNL,
8834 },
Johannes Berg5e7602302011-11-04 11:18:17 +01008835 {
8836 .cmd = NL80211_CMD_REGISTER_BEACONS,
8837 .doit = nl80211_register_beacons,
8838 .policy = nl80211_policy,
8839 .flags = GENL_ADMIN_PERM,
8840 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8841 NL80211_FLAG_NEED_RTNL,
8842 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008843 {
8844 .cmd = NL80211_CMD_SET_NOACK_MAP,
8845 .doit = nl80211_set_noack_map,
8846 .policy = nl80211_policy,
8847 .flags = GENL_ADMIN_PERM,
8848 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8849 NL80211_FLAG_NEED_RTNL,
8850 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008851 {
8852 .cmd = NL80211_CMD_START_P2P_DEVICE,
8853 .doit = nl80211_start_p2p_device,
8854 .policy = nl80211_policy,
8855 .flags = GENL_ADMIN_PERM,
8856 .internal_flags = NL80211_FLAG_NEED_WDEV |
8857 NL80211_FLAG_NEED_RTNL,
8858 },
8859 {
8860 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8861 .doit = nl80211_stop_p2p_device,
8862 .policy = nl80211_policy,
8863 .flags = GENL_ADMIN_PERM,
8864 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8865 NL80211_FLAG_NEED_RTNL,
8866 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008867 {
8868 .cmd = NL80211_CMD_SET_MCAST_RATE,
8869 .doit = nl80211_set_mcast_rate,
8870 .policy = nl80211_policy,
8871 .flags = GENL_ADMIN_PERM,
8872 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8873 NL80211_FLAG_NEED_RTNL,
8874 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308875 {
8876 .cmd = NL80211_CMD_SET_MAC_ACL,
8877 .doit = nl80211_set_mac_acl,
8878 .policy = nl80211_policy,
8879 .flags = GENL_ADMIN_PERM,
8880 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8881 NL80211_FLAG_NEED_RTNL,
8882 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008883 {
8884 .cmd = NL80211_CMD_RADAR_DETECT,
8885 .doit = nl80211_start_radar_detection,
8886 .policy = nl80211_policy,
8887 .flags = GENL_ADMIN_PERM,
8888 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8889 NL80211_FLAG_NEED_RTNL,
8890 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01008891 {
8892 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
8893 .doit = nl80211_get_protocol_features,
8894 .policy = nl80211_policy,
8895 },
Jouni Malinen355199e2013-02-27 17:14:27 +02008896 {
8897 .cmd = NL80211_CMD_UPDATE_FT_IES,
8898 .doit = nl80211_update_ft_ies,
8899 .policy = nl80211_policy,
8900 .flags = GENL_ADMIN_PERM,
8901 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8902 NL80211_FLAG_NEED_RTNL,
8903 },
Johannes Berg55682962007-09-20 13:09:35 -04008904};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008905
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008906static struct genl_multicast_group nl80211_mlme_mcgrp = {
8907 .name = "mlme",
8908};
Johannes Berg55682962007-09-20 13:09:35 -04008909
8910/* multicast groups */
8911static struct genl_multicast_group nl80211_config_mcgrp = {
8912 .name = "config",
8913};
Johannes Berg2a519312009-02-10 21:25:55 +01008914static struct genl_multicast_group nl80211_scan_mcgrp = {
8915 .name = "scan",
8916};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008917static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8918 .name = "regulatory",
8919};
Johannes Berg55682962007-09-20 13:09:35 -04008920
8921/* notification functions */
8922
8923void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8924{
8925 struct sk_buff *msg;
8926
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008927 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008928 if (!msg)
8929 return;
8930
Johannes Berg3713b4e2013-02-14 16:19:38 +01008931 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
8932 false, NULL, NULL, NULL) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04008933 nlmsg_free(msg);
8934 return;
8935 }
8936
Johannes Berg463d0182009-07-14 00:33:35 +02008937 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8938 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008939}
8940
Johannes Berg362a4152009-05-24 16:43:15 +02008941static int nl80211_add_scan_req(struct sk_buff *msg,
8942 struct cfg80211_registered_device *rdev)
8943{
8944 struct cfg80211_scan_request *req = rdev->scan_req;
8945 struct nlattr *nest;
8946 int i;
8947
Johannes Berg667503dd2009-07-07 03:56:11 +02008948 ASSERT_RDEV_LOCK(rdev);
8949
Johannes Berg362a4152009-05-24 16:43:15 +02008950 if (WARN_ON(!req))
8951 return 0;
8952
8953 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8954 if (!nest)
8955 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008956 for (i = 0; i < req->n_ssids; i++) {
8957 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8958 goto nla_put_failure;
8959 }
Johannes Berg362a4152009-05-24 16:43:15 +02008960 nla_nest_end(msg, nest);
8961
8962 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8963 if (!nest)
8964 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008965 for (i = 0; i < req->n_channels; i++) {
8966 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8967 goto nla_put_failure;
8968 }
Johannes Berg362a4152009-05-24 16:43:15 +02008969 nla_nest_end(msg, nest);
8970
David S. Miller9360ffd2012-03-29 04:41:26 -04008971 if (req->ie &&
8972 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8973 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008974
Sam Lefflered4737712012-10-11 21:03:31 -07008975 if (req->flags)
8976 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8977
Johannes Berg362a4152009-05-24 16:43:15 +02008978 return 0;
8979 nla_put_failure:
8980 return -ENOBUFS;
8981}
8982
Johannes Berga538e2d2009-06-16 19:56:42 +02008983static int nl80211_send_scan_msg(struct sk_buff *msg,
8984 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008985 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008986 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008987 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008988{
8989 void *hdr;
8990
Eric W. Biederman15e47302012-09-07 20:12:54 +00008991 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008992 if (!hdr)
8993 return -1;
8994
David S. Miller9360ffd2012-03-29 04:41:26 -04008995 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008996 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8997 wdev->netdev->ifindex)) ||
8998 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008999 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009000
Johannes Berg362a4152009-05-24 16:43:15 +02009001 /* ignore errors and send incomplete event anyway */
9002 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009003
9004 return genlmsg_end(msg, hdr);
9005
9006 nla_put_failure:
9007 genlmsg_cancel(msg, hdr);
9008 return -EMSGSIZE;
9009}
9010
Luciano Coelho807f8a82011-05-11 17:09:35 +03009011static int
9012nl80211_send_sched_scan_msg(struct sk_buff *msg,
9013 struct cfg80211_registered_device *rdev,
9014 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009015 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009016{
9017 void *hdr;
9018
Eric W. Biederman15e47302012-09-07 20:12:54 +00009019 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009020 if (!hdr)
9021 return -1;
9022
David S. Miller9360ffd2012-03-29 04:41:26 -04009023 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9024 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9025 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009026
9027 return genlmsg_end(msg, hdr);
9028
9029 nla_put_failure:
9030 genlmsg_cancel(msg, hdr);
9031 return -EMSGSIZE;
9032}
9033
Johannes Berga538e2d2009-06-16 19:56:42 +02009034void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009035 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009036{
9037 struct sk_buff *msg;
9038
Thomas Graf58050fc2012-06-28 03:57:45 +00009039 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009040 if (!msg)
9041 return;
9042
Johannes Bergfd014282012-06-18 19:17:03 +02009043 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009044 NL80211_CMD_TRIGGER_SCAN) < 0) {
9045 nlmsg_free(msg);
9046 return;
9047 }
9048
Johannes Berg463d0182009-07-14 00:33:35 +02009049 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9050 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009051}
9052
Johannes Berg2a519312009-02-10 21:25:55 +01009053void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009054 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009055{
9056 struct sk_buff *msg;
9057
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009058 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009059 if (!msg)
9060 return;
9061
Johannes Bergfd014282012-06-18 19:17:03 +02009062 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009063 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009064 nlmsg_free(msg);
9065 return;
9066 }
9067
Johannes Berg463d0182009-07-14 00:33:35 +02009068 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9069 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009070}
9071
9072void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009073 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009074{
9075 struct sk_buff *msg;
9076
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009077 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009078 if (!msg)
9079 return;
9080
Johannes Bergfd014282012-06-18 19:17:03 +02009081 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009082 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009083 nlmsg_free(msg);
9084 return;
9085 }
9086
Johannes Berg463d0182009-07-14 00:33:35 +02009087 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9088 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009089}
9090
Luciano Coelho807f8a82011-05-11 17:09:35 +03009091void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9092 struct net_device *netdev)
9093{
9094 struct sk_buff *msg;
9095
9096 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9097 if (!msg)
9098 return;
9099
9100 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9101 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9102 nlmsg_free(msg);
9103 return;
9104 }
9105
9106 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9107 nl80211_scan_mcgrp.id, GFP_KERNEL);
9108}
9109
9110void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9111 struct net_device *netdev, u32 cmd)
9112{
9113 struct sk_buff *msg;
9114
Thomas Graf58050fc2012-06-28 03:57:45 +00009115 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009116 if (!msg)
9117 return;
9118
9119 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9120 nlmsg_free(msg);
9121 return;
9122 }
9123
9124 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9125 nl80211_scan_mcgrp.id, GFP_KERNEL);
9126}
9127
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009128/*
9129 * This can happen on global regulatory changes or device specific settings
9130 * based on custom world regulatory domains.
9131 */
9132void nl80211_send_reg_change_event(struct regulatory_request *request)
9133{
9134 struct sk_buff *msg;
9135 void *hdr;
9136
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009137 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009138 if (!msg)
9139 return;
9140
9141 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9142 if (!hdr) {
9143 nlmsg_free(msg);
9144 return;
9145 }
9146
9147 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04009148 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9149 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009150
David S. Miller9360ffd2012-03-29 04:41:26 -04009151 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9152 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9153 NL80211_REGDOM_TYPE_WORLD))
9154 goto nla_put_failure;
9155 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9156 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9157 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9158 goto nla_put_failure;
9159 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9160 request->intersect) {
9161 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9162 NL80211_REGDOM_TYPE_INTERSECTION))
9163 goto nla_put_failure;
9164 } else {
9165 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9166 NL80211_REGDOM_TYPE_COUNTRY) ||
9167 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9168 request->alpha2))
9169 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009170 }
9171
Johannes Bergf4173762012-12-03 18:23:37 +01009172 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04009173 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9174 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009175
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009176 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009177
Johannes Bergbc43b282009-07-25 10:54:13 +02009178 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009179 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009180 GFP_ATOMIC);
9181 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009182
9183 return;
9184
9185nla_put_failure:
9186 genlmsg_cancel(msg, hdr);
9187 nlmsg_free(msg);
9188}
9189
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009190static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9191 struct net_device *netdev,
9192 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009193 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009194{
9195 struct sk_buff *msg;
9196 void *hdr;
9197
Johannes Berge6d6e342009-07-01 21:26:47 +02009198 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009199 if (!msg)
9200 return;
9201
9202 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9203 if (!hdr) {
9204 nlmsg_free(msg);
9205 return;
9206 }
9207
David S. Miller9360ffd2012-03-29 04:41:26 -04009208 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9209 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9210 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9211 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009212
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009213 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009214
Johannes Berg463d0182009-07-14 00:33:35 +02009215 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9216 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009217 return;
9218
9219 nla_put_failure:
9220 genlmsg_cancel(msg, hdr);
9221 nlmsg_free(msg);
9222}
9223
9224void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009225 struct net_device *netdev, const u8 *buf,
9226 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009227{
9228 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009229 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009230}
9231
9232void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9233 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009234 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009235{
Johannes Berge6d6e342009-07-01 21:26:47 +02009236 nl80211_send_mlme_event(rdev, netdev, buf, len,
9237 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009238}
9239
Jouni Malinen53b46b82009-03-27 20:53:56 +02009240void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009241 struct net_device *netdev, const u8 *buf,
9242 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009243{
9244 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009245 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009246}
9247
Jouni Malinen53b46b82009-03-27 20:53:56 +02009248void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9249 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009250 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009251{
9252 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009253 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009254}
9255
Johannes Berg947add32013-02-22 22:05:20 +01009256void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
9257 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +02009258{
Johannes Berg947add32013-02-22 22:05:20 +01009259 struct wireless_dev *wdev = dev->ieee80211_ptr;
9260 struct wiphy *wiphy = wdev->wiphy;
9261 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009262
Johannes Berg947add32013-02-22 22:05:20 +01009263 trace_cfg80211_send_unprot_deauth(dev);
9264 nl80211_send_mlme_event(rdev, dev, buf, len,
9265 NL80211_CMD_UNPROT_DEAUTHENTICATE, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009266}
Johannes Berg947add32013-02-22 22:05:20 +01009267EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
9268
9269void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
9270 size_t len)
9271{
9272 struct wireless_dev *wdev = dev->ieee80211_ptr;
9273 struct wiphy *wiphy = wdev->wiphy;
9274 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9275
9276 trace_cfg80211_send_unprot_disassoc(dev);
9277 nl80211_send_mlme_event(rdev, dev, buf, len,
9278 NL80211_CMD_UNPROT_DISASSOCIATE, GFP_ATOMIC);
9279}
9280EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009281
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04009282static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9283 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02009284 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009285{
9286 struct sk_buff *msg;
9287 void *hdr;
9288
Johannes Berge6d6e342009-07-01 21:26:47 +02009289 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009290 if (!msg)
9291 return;
9292
9293 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9294 if (!hdr) {
9295 nlmsg_free(msg);
9296 return;
9297 }
9298
David S. Miller9360ffd2012-03-29 04:41:26 -04009299 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9300 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9301 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9302 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9303 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03009304
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009305 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03009306
Johannes Berg463d0182009-07-14 00:33:35 +02009307 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9308 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009309 return;
9310
9311 nla_put_failure:
9312 genlmsg_cancel(msg, hdr);
9313 nlmsg_free(msg);
9314}
9315
9316void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009317 struct net_device *netdev, const u8 *addr,
9318 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009319{
9320 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02009321 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009322}
9323
9324void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009325 struct net_device *netdev, const u8 *addr,
9326 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009327{
Johannes Berge6d6e342009-07-01 21:26:47 +02009328 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9329 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009330}
9331
Samuel Ortizb23aa672009-07-01 21:26:54 +02009332void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9333 struct net_device *netdev, const u8 *bssid,
9334 const u8 *req_ie, size_t req_ie_len,
9335 const u8 *resp_ie, size_t resp_ie_len,
9336 u16 status, gfp_t gfp)
9337{
9338 struct sk_buff *msg;
9339 void *hdr;
9340
Thomas Graf58050fc2012-06-28 03:57:45 +00009341 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009342 if (!msg)
9343 return;
9344
9345 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9346 if (!hdr) {
9347 nlmsg_free(msg);
9348 return;
9349 }
9350
David S. Miller9360ffd2012-03-29 04:41:26 -04009351 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9352 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9353 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9354 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9355 (req_ie &&
9356 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9357 (resp_ie &&
9358 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9359 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009360
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009361 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009362
Johannes Berg463d0182009-07-14 00:33:35 +02009363 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9364 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009365 return;
9366
9367 nla_put_failure:
9368 genlmsg_cancel(msg, hdr);
9369 nlmsg_free(msg);
9370
9371}
9372
9373void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9374 struct net_device *netdev, const u8 *bssid,
9375 const u8 *req_ie, size_t req_ie_len,
9376 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9377{
9378 struct sk_buff *msg;
9379 void *hdr;
9380
Thomas Graf58050fc2012-06-28 03:57:45 +00009381 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009382 if (!msg)
9383 return;
9384
9385 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9386 if (!hdr) {
9387 nlmsg_free(msg);
9388 return;
9389 }
9390
David S. Miller9360ffd2012-03-29 04:41:26 -04009391 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9392 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9393 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9394 (req_ie &&
9395 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9396 (resp_ie &&
9397 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9398 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009399
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009400 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009401
Johannes Berg463d0182009-07-14 00:33:35 +02009402 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9403 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009404 return;
9405
9406 nla_put_failure:
9407 genlmsg_cancel(msg, hdr);
9408 nlmsg_free(msg);
9409
9410}
9411
9412void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9413 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02009414 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009415{
9416 struct sk_buff *msg;
9417 void *hdr;
9418
Thomas Graf58050fc2012-06-28 03:57:45 +00009419 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009420 if (!msg)
9421 return;
9422
9423 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9424 if (!hdr) {
9425 nlmsg_free(msg);
9426 return;
9427 }
9428
David S. Miller9360ffd2012-03-29 04:41:26 -04009429 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9430 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9431 (from_ap && reason &&
9432 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9433 (from_ap &&
9434 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9435 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9436 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009437
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009438 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009439
Johannes Berg463d0182009-07-14 00:33:35 +02009440 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9441 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009442 return;
9443
9444 nla_put_failure:
9445 genlmsg_cancel(msg, hdr);
9446 nlmsg_free(msg);
9447
9448}
9449
Johannes Berg04a773a2009-04-19 21:24:32 +02009450void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9451 struct net_device *netdev, const u8 *bssid,
9452 gfp_t gfp)
9453{
9454 struct sk_buff *msg;
9455 void *hdr;
9456
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009457 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009458 if (!msg)
9459 return;
9460
9461 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9462 if (!hdr) {
9463 nlmsg_free(msg);
9464 return;
9465 }
9466
David S. Miller9360ffd2012-03-29 04:41:26 -04009467 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9468 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9469 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9470 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009471
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009472 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009473
Johannes Berg463d0182009-07-14 00:33:35 +02009474 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9475 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009476 return;
9477
9478 nla_put_failure:
9479 genlmsg_cancel(msg, hdr);
9480 nlmsg_free(msg);
9481}
9482
Johannes Berg947add32013-02-22 22:05:20 +01009483void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
9484 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -07009485{
Johannes Berg947add32013-02-22 22:05:20 +01009486 struct wireless_dev *wdev = dev->ieee80211_ptr;
9487 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009488 struct sk_buff *msg;
9489 void *hdr;
9490
Johannes Berg947add32013-02-22 22:05:20 +01009491 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
9492 return;
9493
9494 trace_cfg80211_notify_new_peer_candidate(dev, addr);
9495
Javier Cardonac93b5e72011-04-07 15:08:34 -07009496 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9497 if (!msg)
9498 return;
9499
9500 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9501 if (!hdr) {
9502 nlmsg_free(msg);
9503 return;
9504 }
9505
David S. Miller9360ffd2012-03-29 04:41:26 -04009506 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +01009507 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9508 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009509 (ie_len && ie &&
9510 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9511 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009512
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009513 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009514
9515 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9516 nl80211_mlme_mcgrp.id, gfp);
9517 return;
9518
9519 nla_put_failure:
9520 genlmsg_cancel(msg, hdr);
9521 nlmsg_free(msg);
9522}
Johannes Berg947add32013-02-22 22:05:20 +01009523EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009524
Jouni Malinena3b8b052009-03-27 21:59:49 +02009525void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9526 struct net_device *netdev, const u8 *addr,
9527 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009528 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009529{
9530 struct sk_buff *msg;
9531 void *hdr;
9532
Johannes Berge6d6e342009-07-01 21:26:47 +02009533 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009534 if (!msg)
9535 return;
9536
9537 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9538 if (!hdr) {
9539 nlmsg_free(msg);
9540 return;
9541 }
9542
David S. Miller9360ffd2012-03-29 04:41:26 -04009543 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9544 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9545 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9546 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9547 (key_id != -1 &&
9548 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9549 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9550 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009551
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009552 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009553
Johannes Berg463d0182009-07-14 00:33:35 +02009554 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9555 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009556 return;
9557
9558 nla_put_failure:
9559 genlmsg_cancel(msg, hdr);
9560 nlmsg_free(msg);
9561}
9562
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009563void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9564 struct ieee80211_channel *channel_before,
9565 struct ieee80211_channel *channel_after)
9566{
9567 struct sk_buff *msg;
9568 void *hdr;
9569 struct nlattr *nl_freq;
9570
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009571 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009572 if (!msg)
9573 return;
9574
9575 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9576 if (!hdr) {
9577 nlmsg_free(msg);
9578 return;
9579 }
9580
9581 /*
9582 * Since we are applying the beacon hint to a wiphy we know its
9583 * wiphy_idx is valid
9584 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009585 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9586 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009587
9588 /* Before */
9589 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9590 if (!nl_freq)
9591 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009592 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009593 goto nla_put_failure;
9594 nla_nest_end(msg, nl_freq);
9595
9596 /* After */
9597 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9598 if (!nl_freq)
9599 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009600 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009601 goto nla_put_failure;
9602 nla_nest_end(msg, nl_freq);
9603
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009604 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009605
Johannes Berg463d0182009-07-14 00:33:35 +02009606 rcu_read_lock();
9607 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9608 GFP_ATOMIC);
9609 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009610
9611 return;
9612
9613nla_put_failure:
9614 genlmsg_cancel(msg, hdr);
9615 nlmsg_free(msg);
9616}
9617
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009618static void nl80211_send_remain_on_chan_event(
9619 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009620 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009621 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009622 unsigned int duration, gfp_t gfp)
9623{
9624 struct sk_buff *msg;
9625 void *hdr;
9626
9627 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9628 if (!msg)
9629 return;
9630
9631 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9632 if (!hdr) {
9633 nlmsg_free(msg);
9634 return;
9635 }
9636
David S. Miller9360ffd2012-03-29 04:41:26 -04009637 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009638 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9639 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009640 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009641 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009642 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9643 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009644 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9645 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009646
David S. Miller9360ffd2012-03-29 04:41:26 -04009647 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9648 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9649 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009650
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009651 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009652
9653 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9654 nl80211_mlme_mcgrp.id, gfp);
9655 return;
9656
9657 nla_put_failure:
9658 genlmsg_cancel(msg, hdr);
9659 nlmsg_free(msg);
9660}
9661
Johannes Berg947add32013-02-22 22:05:20 +01009662void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9663 struct ieee80211_channel *chan,
9664 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009665{
Johannes Berg947add32013-02-22 22:05:20 +01009666 struct wiphy *wiphy = wdev->wiphy;
9667 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9668
9669 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009670 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009671 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009672 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009673}
Johannes Berg947add32013-02-22 22:05:20 +01009674EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009675
Johannes Berg947add32013-02-22 22:05:20 +01009676void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9677 struct ieee80211_channel *chan,
9678 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009679{
Johannes Berg947add32013-02-22 22:05:20 +01009680 struct wiphy *wiphy = wdev->wiphy;
9681 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9682
9683 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009684 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009685 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009686}
Johannes Berg947add32013-02-22 22:05:20 +01009687EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009688
Johannes Berg947add32013-02-22 22:05:20 +01009689void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
9690 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +01009691{
Johannes Berg947add32013-02-22 22:05:20 +01009692 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9693 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +01009694 struct sk_buff *msg;
9695
Johannes Berg947add32013-02-22 22:05:20 +01009696 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
9697
Thomas Graf58050fc2012-06-28 03:57:45 +00009698 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009699 if (!msg)
9700 return;
9701
John W. Linville66266b32012-03-15 13:25:41 -04009702 if (nl80211_send_station(msg, 0, 0, 0,
9703 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009704 nlmsg_free(msg);
9705 return;
9706 }
9707
9708 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9709 nl80211_mlme_mcgrp.id, gfp);
9710}
Johannes Berg947add32013-02-22 22:05:20 +01009711EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +01009712
Johannes Berg947add32013-02-22 22:05:20 +01009713void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +02009714{
Johannes Berg947add32013-02-22 22:05:20 +01009715 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9716 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +02009717 struct sk_buff *msg;
9718 void *hdr;
9719
Johannes Berg947add32013-02-22 22:05:20 +01009720 trace_cfg80211_del_sta(dev, mac_addr);
9721
Thomas Graf58050fc2012-06-28 03:57:45 +00009722 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009723 if (!msg)
9724 return;
9725
9726 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9727 if (!hdr) {
9728 nlmsg_free(msg);
9729 return;
9730 }
9731
David S. Miller9360ffd2012-03-29 04:41:26 -04009732 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9733 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9734 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009735
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009736 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009737
9738 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9739 nl80211_mlme_mcgrp.id, gfp);
9740 return;
9741
9742 nla_put_failure:
9743 genlmsg_cancel(msg, hdr);
9744 nlmsg_free(msg);
9745}
Johannes Berg947add32013-02-22 22:05:20 +01009746EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +02009747
Johannes Berg947add32013-02-22 22:05:20 +01009748void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
9749 enum nl80211_connect_failed_reason reason,
9750 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309751{
Johannes Berg947add32013-02-22 22:05:20 +01009752 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9753 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309754 struct sk_buff *msg;
9755 void *hdr;
9756
9757 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9758 if (!msg)
9759 return;
9760
9761 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9762 if (!hdr) {
9763 nlmsg_free(msg);
9764 return;
9765 }
9766
9767 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9768 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9769 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9770 goto nla_put_failure;
9771
9772 genlmsg_end(msg, hdr);
9773
9774 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9775 nl80211_mlme_mcgrp.id, gfp);
9776 return;
9777
9778 nla_put_failure:
9779 genlmsg_cancel(msg, hdr);
9780 nlmsg_free(msg);
9781}
Johannes Berg947add32013-02-22 22:05:20 +01009782EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309783
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009784static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9785 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009786{
9787 struct wireless_dev *wdev = dev->ieee80211_ptr;
9788 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9789 struct sk_buff *msg;
9790 void *hdr;
9791 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009792 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009793
Eric W. Biederman15e47302012-09-07 20:12:54 +00009794 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009795 return false;
9796
9797 msg = nlmsg_new(100, gfp);
9798 if (!msg)
9799 return true;
9800
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009801 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009802 if (!hdr) {
9803 nlmsg_free(msg);
9804 return true;
9805 }
9806
David S. Miller9360ffd2012-03-29 04:41:26 -04009807 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9808 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9809 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9810 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009811
9812 err = genlmsg_end(msg, hdr);
9813 if (err < 0) {
9814 nlmsg_free(msg);
9815 return true;
9816 }
9817
Eric W. Biederman15e47302012-09-07 20:12:54 +00009818 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009819 return true;
9820
9821 nla_put_failure:
9822 genlmsg_cancel(msg, hdr);
9823 nlmsg_free(msg);
9824 return true;
9825}
9826
Johannes Berg947add32013-02-22 22:05:20 +01009827bool cfg80211_rx_spurious_frame(struct net_device *dev,
9828 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009829{
Johannes Berg947add32013-02-22 22:05:20 +01009830 struct wireless_dev *wdev = dev->ieee80211_ptr;
9831 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009832
Johannes Berg947add32013-02-22 22:05:20 +01009833 trace_cfg80211_rx_spurious_frame(dev, addr);
9834
9835 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9836 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
9837 trace_cfg80211_return_bool(false);
9838 return false;
9839 }
9840 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9841 addr, gfp);
9842 trace_cfg80211_return_bool(ret);
9843 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009844}
Johannes Berg947add32013-02-22 22:05:20 +01009845EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
9846
9847bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
9848 const u8 *addr, gfp_t gfp)
9849{
9850 struct wireless_dev *wdev = dev->ieee80211_ptr;
9851 bool ret;
9852
9853 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
9854
9855 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9856 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
9857 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
9858 trace_cfg80211_return_bool(false);
9859 return false;
9860 }
9861 ret = __nl80211_unexpected_frame(dev,
9862 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9863 addr, gfp);
9864 trace_cfg80211_return_bool(ret);
9865 return ret;
9866}
9867EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009868
Johannes Berg2e161f72010-08-12 15:38:38 +02009869int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009870 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009871 int freq, int sig_dbm,
9872 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009873{
Johannes Berg71bbc992012-06-15 15:30:18 +02009874 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009875 struct sk_buff *msg;
9876 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009877
9878 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9879 if (!msg)
9880 return -ENOMEM;
9881
Johannes Berg2e161f72010-08-12 15:38:38 +02009882 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009883 if (!hdr) {
9884 nlmsg_free(msg);
9885 return -ENOMEM;
9886 }
9887
David S. Miller9360ffd2012-03-29 04:41:26 -04009888 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009889 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9890 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009891 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9892 (sig_dbm &&
9893 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9894 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9895 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009896
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009897 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009898
Eric W. Biederman15e47302012-09-07 20:12:54 +00009899 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009900
9901 nla_put_failure:
9902 genlmsg_cancel(msg, hdr);
9903 nlmsg_free(msg);
9904 return -ENOBUFS;
9905}
9906
Johannes Berg947add32013-02-22 22:05:20 +01009907void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
9908 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009909{
Johannes Berg947add32013-02-22 22:05:20 +01009910 struct wiphy *wiphy = wdev->wiphy;
9911 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +02009912 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009913 struct sk_buff *msg;
9914 void *hdr;
9915
Johannes Berg947add32013-02-22 22:05:20 +01009916 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
9917
Jouni Malinen026331c2010-02-15 12:53:10 +02009918 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9919 if (!msg)
9920 return;
9921
Johannes Berg2e161f72010-08-12 15:38:38 +02009922 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009923 if (!hdr) {
9924 nlmsg_free(msg);
9925 return;
9926 }
9927
David S. Miller9360ffd2012-03-29 04:41:26 -04009928 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009929 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9930 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009931 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9932 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9933 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9934 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009935
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009936 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009937
9938 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9939 return;
9940
9941 nla_put_failure:
9942 genlmsg_cancel(msg, hdr);
9943 nlmsg_free(msg);
9944}
Johannes Berg947add32013-02-22 22:05:20 +01009945EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +02009946
Johannes Berg947add32013-02-22 22:05:20 +01009947void cfg80211_cqm_rssi_notify(struct net_device *dev,
9948 enum nl80211_cqm_rssi_threshold_event rssi_event,
9949 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009950{
Johannes Berg947add32013-02-22 22:05:20 +01009951 struct wireless_dev *wdev = dev->ieee80211_ptr;
9952 struct wiphy *wiphy = wdev->wiphy;
9953 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009954 struct sk_buff *msg;
9955 struct nlattr *pinfoattr;
9956 void *hdr;
9957
Johannes Berg947add32013-02-22 22:05:20 +01009958 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
9959
Thomas Graf58050fc2012-06-28 03:57:45 +00009960 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009961 if (!msg)
9962 return;
9963
9964 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9965 if (!hdr) {
9966 nlmsg_free(msg);
9967 return;
9968 }
9969
David S. Miller9360ffd2012-03-29 04:41:26 -04009970 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +01009971 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -04009972 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009973
9974 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9975 if (!pinfoattr)
9976 goto nla_put_failure;
9977
David S. Miller9360ffd2012-03-29 04:41:26 -04009978 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9979 rssi_event))
9980 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009981
9982 nla_nest_end(msg, pinfoattr);
9983
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009984 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009985
9986 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9987 nl80211_mlme_mcgrp.id, gfp);
9988 return;
9989
9990 nla_put_failure:
9991 genlmsg_cancel(msg, hdr);
9992 nlmsg_free(msg);
9993}
Johannes Berg947add32013-02-22 22:05:20 +01009994EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009995
Johannes Berg947add32013-02-22 22:05:20 +01009996static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9997 struct net_device *netdev, const u8 *bssid,
9998 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +02009999{
10000 struct sk_buff *msg;
10001 struct nlattr *rekey_attr;
10002 void *hdr;
10003
Thomas Graf58050fc2012-06-28 03:57:45 +000010004 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010005 if (!msg)
10006 return;
10007
10008 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10009 if (!hdr) {
10010 nlmsg_free(msg);
10011 return;
10012 }
10013
David S. Miller9360ffd2012-03-29 04:41:26 -040010014 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10015 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10016 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10017 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010018
10019 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10020 if (!rekey_attr)
10021 goto nla_put_failure;
10022
David S. Miller9360ffd2012-03-29 04:41:26 -040010023 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10024 NL80211_REPLAY_CTR_LEN, replay_ctr))
10025 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010026
10027 nla_nest_end(msg, rekey_attr);
10028
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010029 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010030
10031 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10032 nl80211_mlme_mcgrp.id, gfp);
10033 return;
10034
10035 nla_put_failure:
10036 genlmsg_cancel(msg, hdr);
10037 nlmsg_free(msg);
10038}
10039
Johannes Berg947add32013-02-22 22:05:20 +010010040void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10041 const u8 *replay_ctr, gfp_t gfp)
10042{
10043 struct wireless_dev *wdev = dev->ieee80211_ptr;
10044 struct wiphy *wiphy = wdev->wiphy;
10045 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10046
10047 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10048 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10049}
10050EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10051
10052static void
10053nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10054 struct net_device *netdev, int index,
10055 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010056{
10057 struct sk_buff *msg;
10058 struct nlattr *attr;
10059 void *hdr;
10060
Thomas Graf58050fc2012-06-28 03:57:45 +000010061 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010062 if (!msg)
10063 return;
10064
10065 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10066 if (!hdr) {
10067 nlmsg_free(msg);
10068 return;
10069 }
10070
David S. Miller9360ffd2012-03-29 04:41:26 -040010071 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10072 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10073 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010074
10075 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10076 if (!attr)
10077 goto nla_put_failure;
10078
David S. Miller9360ffd2012-03-29 04:41:26 -040010079 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10080 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10081 (preauth &&
10082 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10083 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010084
10085 nla_nest_end(msg, attr);
10086
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010087 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010088
10089 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10090 nl80211_mlme_mcgrp.id, gfp);
10091 return;
10092
10093 nla_put_failure:
10094 genlmsg_cancel(msg, hdr);
10095 nlmsg_free(msg);
10096}
10097
Johannes Berg947add32013-02-22 22:05:20 +010010098void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10099 const u8 *bssid, bool preauth, gfp_t gfp)
10100{
10101 struct wireless_dev *wdev = dev->ieee80211_ptr;
10102 struct wiphy *wiphy = wdev->wiphy;
10103 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10104
10105 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10106 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10107}
10108EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10109
10110static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10111 struct net_device *netdev,
10112 struct cfg80211_chan_def *chandef,
10113 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070010114{
10115 struct sk_buff *msg;
10116 void *hdr;
10117
Thomas Graf58050fc2012-06-28 03:57:45 +000010118 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070010119 if (!msg)
10120 return;
10121
10122 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10123 if (!hdr) {
10124 nlmsg_free(msg);
10125 return;
10126 }
10127
Johannes Berg683b6d32012-11-08 21:25:48 +010010128 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10129 goto nla_put_failure;
10130
10131 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040010132 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070010133
10134 genlmsg_end(msg, hdr);
10135
10136 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10137 nl80211_mlme_mcgrp.id, gfp);
10138 return;
10139
10140 nla_put_failure:
10141 genlmsg_cancel(msg, hdr);
10142 nlmsg_free(msg);
10143}
10144
Johannes Berg947add32013-02-22 22:05:20 +010010145void cfg80211_ch_switch_notify(struct net_device *dev,
10146 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070010147{
Johannes Berg947add32013-02-22 22:05:20 +010010148 struct wireless_dev *wdev = dev->ieee80211_ptr;
10149 struct wiphy *wiphy = wdev->wiphy;
10150 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10151
10152 trace_cfg80211_ch_switch_notify(dev, chandef);
10153
10154 wdev_lock(wdev);
10155
10156 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10157 wdev->iftype != NL80211_IFTYPE_P2P_GO))
10158 goto out;
10159
10160 wdev->channel = chandef->chan;
10161 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
10162out:
10163 wdev_unlock(wdev);
10164 return;
10165}
10166EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10167
10168void cfg80211_cqm_txe_notify(struct net_device *dev,
10169 const u8 *peer, u32 num_packets,
10170 u32 rate, u32 intvl, gfp_t gfp)
10171{
10172 struct wireless_dev *wdev = dev->ieee80211_ptr;
10173 struct wiphy *wiphy = wdev->wiphy;
10174 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010175 struct sk_buff *msg;
10176 struct nlattr *pinfoattr;
10177 void *hdr;
10178
10179 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10180 if (!msg)
10181 return;
10182
10183 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10184 if (!hdr) {
10185 nlmsg_free(msg);
10186 return;
10187 }
10188
10189 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010190 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070010191 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10192 goto nla_put_failure;
10193
10194 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10195 if (!pinfoattr)
10196 goto nla_put_failure;
10197
10198 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10199 goto nla_put_failure;
10200
10201 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10202 goto nla_put_failure;
10203
10204 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10205 goto nla_put_failure;
10206
10207 nla_nest_end(msg, pinfoattr);
10208
10209 genlmsg_end(msg, hdr);
10210
10211 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10212 nl80211_mlme_mcgrp.id, gfp);
10213 return;
10214
10215 nla_put_failure:
10216 genlmsg_cancel(msg, hdr);
10217 nlmsg_free(msg);
10218}
Johannes Berg947add32013-02-22 22:05:20 +010010219EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010220
10221void
Simon Wunderlich04f39042013-02-08 18:16:19 +010010222nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10223 struct cfg80211_chan_def *chandef,
10224 enum nl80211_radar_event event,
10225 struct net_device *netdev, gfp_t gfp)
10226{
10227 struct sk_buff *msg;
10228 void *hdr;
10229
10230 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10231 if (!msg)
10232 return;
10233
10234 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
10235 if (!hdr) {
10236 nlmsg_free(msg);
10237 return;
10238 }
10239
10240 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10241 goto nla_put_failure;
10242
10243 /* NOP and radar events don't need a netdev parameter */
10244 if (netdev) {
10245 struct wireless_dev *wdev = netdev->ieee80211_ptr;
10246
10247 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10248 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10249 goto nla_put_failure;
10250 }
10251
10252 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
10253 goto nla_put_failure;
10254
10255 if (nl80211_send_chandef(msg, chandef))
10256 goto nla_put_failure;
10257
10258 if (genlmsg_end(msg, hdr) < 0) {
10259 nlmsg_free(msg);
10260 return;
10261 }
10262
10263 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10264 nl80211_mlme_mcgrp.id, gfp);
10265 return;
10266
10267 nla_put_failure:
10268 genlmsg_cancel(msg, hdr);
10269 nlmsg_free(msg);
10270}
10271
Johannes Berg947add32013-02-22 22:05:20 +010010272void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10273 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010010274{
Johannes Berg947add32013-02-22 22:05:20 +010010275 struct wireless_dev *wdev = dev->ieee80211_ptr;
10276 struct wiphy *wiphy = wdev->wiphy;
10277 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010278 struct sk_buff *msg;
10279 struct nlattr *pinfoattr;
10280 void *hdr;
10281
Johannes Berg947add32013-02-22 22:05:20 +010010282 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
10283
Thomas Graf58050fc2012-06-28 03:57:45 +000010284 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010285 if (!msg)
10286 return;
10287
10288 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10289 if (!hdr) {
10290 nlmsg_free(msg);
10291 return;
10292 }
10293
David S. Miller9360ffd2012-03-29 04:41:26 -040010294 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010295 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010296 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10297 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010298
10299 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10300 if (!pinfoattr)
10301 goto nla_put_failure;
10302
David S. Miller9360ffd2012-03-29 04:41:26 -040010303 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10304 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010305
10306 nla_nest_end(msg, pinfoattr);
10307
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010308 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010309
10310 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10311 nl80211_mlme_mcgrp.id, gfp);
10312 return;
10313
10314 nla_put_failure:
10315 genlmsg_cancel(msg, hdr);
10316 nlmsg_free(msg);
10317}
Johannes Berg947add32013-02-22 22:05:20 +010010318EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010319
Johannes Berg7f6cf312011-11-04 11:18:15 +010010320void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10321 u64 cookie, bool acked, gfp_t gfp)
10322{
10323 struct wireless_dev *wdev = dev->ieee80211_ptr;
10324 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10325 struct sk_buff *msg;
10326 void *hdr;
10327 int err;
10328
Beni Lev4ee3e062012-08-27 12:49:39 +030010329 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10330
Thomas Graf58050fc2012-06-28 03:57:45 +000010331 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030010332
Johannes Berg7f6cf312011-11-04 11:18:15 +010010333 if (!msg)
10334 return;
10335
10336 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10337 if (!hdr) {
10338 nlmsg_free(msg);
10339 return;
10340 }
10341
David S. Miller9360ffd2012-03-29 04:41:26 -040010342 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10343 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10344 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10345 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10346 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10347 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010348
10349 err = genlmsg_end(msg, hdr);
10350 if (err < 0) {
10351 nlmsg_free(msg);
10352 return;
10353 }
10354
10355 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10356 nl80211_mlme_mcgrp.id, gfp);
10357 return;
10358
10359 nla_put_failure:
10360 genlmsg_cancel(msg, hdr);
10361 nlmsg_free(msg);
10362}
10363EXPORT_SYMBOL(cfg80211_probe_status);
10364
Johannes Berg5e7602302011-11-04 11:18:17 +010010365void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10366 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070010367 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010010368{
10369 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10370 struct sk_buff *msg;
10371 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070010372 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010010373
Beni Lev4ee3e062012-08-27 12:49:39 +030010374 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10375
Ben Greear37c73b52012-10-26 14:49:25 -070010376 spin_lock_bh(&rdev->beacon_registrations_lock);
10377 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10378 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10379 if (!msg) {
10380 spin_unlock_bh(&rdev->beacon_registrations_lock);
10381 return;
10382 }
Johannes Berg5e7602302011-11-04 11:18:17 +010010383
Ben Greear37c73b52012-10-26 14:49:25 -070010384 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10385 if (!hdr)
10386 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010010387
Ben Greear37c73b52012-10-26 14:49:25 -070010388 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10389 (freq &&
10390 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10391 (sig_dbm &&
10392 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10393 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10394 goto nla_put_failure;
10395
10396 genlmsg_end(msg, hdr);
10397
10398 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010010399 }
Ben Greear37c73b52012-10-26 14:49:25 -070010400 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010010401 return;
10402
10403 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070010404 spin_unlock_bh(&rdev->beacon_registrations_lock);
10405 if (hdr)
10406 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010010407 nlmsg_free(msg);
10408}
10409EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10410
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010411#ifdef CONFIG_PM
10412void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10413 struct cfg80211_wowlan_wakeup *wakeup,
10414 gfp_t gfp)
10415{
10416 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10417 struct sk_buff *msg;
10418 void *hdr;
10419 int err, size = 200;
10420
10421 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10422
10423 if (wakeup)
10424 size += wakeup->packet_present_len;
10425
10426 msg = nlmsg_new(size, gfp);
10427 if (!msg)
10428 return;
10429
10430 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10431 if (!hdr)
10432 goto free_msg;
10433
10434 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10435 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10436 goto free_msg;
10437
10438 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10439 wdev->netdev->ifindex))
10440 goto free_msg;
10441
10442 if (wakeup) {
10443 struct nlattr *reasons;
10444
10445 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10446
10447 if (wakeup->disconnect &&
10448 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10449 goto free_msg;
10450 if (wakeup->magic_pkt &&
10451 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10452 goto free_msg;
10453 if (wakeup->gtk_rekey_failure &&
10454 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10455 goto free_msg;
10456 if (wakeup->eap_identity_req &&
10457 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10458 goto free_msg;
10459 if (wakeup->four_way_handshake &&
10460 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10461 goto free_msg;
10462 if (wakeup->rfkill_release &&
10463 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10464 goto free_msg;
10465
10466 if (wakeup->pattern_idx >= 0 &&
10467 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10468 wakeup->pattern_idx))
10469 goto free_msg;
10470
Johannes Berg2a0e0472013-01-23 22:57:40 +010010471 if (wakeup->tcp_match)
10472 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10473
10474 if (wakeup->tcp_connlost)
10475 nla_put_flag(msg,
10476 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10477
10478 if (wakeup->tcp_nomoretokens)
10479 nla_put_flag(msg,
10480 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10481
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010482 if (wakeup->packet) {
10483 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10484 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10485
10486 if (!wakeup->packet_80211) {
10487 pkt_attr =
10488 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10489 len_attr =
10490 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10491 }
10492
10493 if (wakeup->packet_len &&
10494 nla_put_u32(msg, len_attr, wakeup->packet_len))
10495 goto free_msg;
10496
10497 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10498 wakeup->packet))
10499 goto free_msg;
10500 }
10501
10502 nla_nest_end(msg, reasons);
10503 }
10504
10505 err = genlmsg_end(msg, hdr);
10506 if (err < 0)
10507 goto free_msg;
10508
10509 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10510 nl80211_mlme_mcgrp.id, gfp);
10511 return;
10512
10513 free_msg:
10514 nlmsg_free(msg);
10515}
10516EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10517#endif
10518
Jouni Malinen3475b092012-11-16 22:49:57 +020010519void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10520 enum nl80211_tdls_operation oper,
10521 u16 reason_code, gfp_t gfp)
10522{
10523 struct wireless_dev *wdev = dev->ieee80211_ptr;
10524 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10525 struct sk_buff *msg;
10526 void *hdr;
10527 int err;
10528
10529 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10530 reason_code);
10531
10532 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10533 if (!msg)
10534 return;
10535
10536 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10537 if (!hdr) {
10538 nlmsg_free(msg);
10539 return;
10540 }
10541
10542 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10543 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10544 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10545 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10546 (reason_code > 0 &&
10547 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10548 goto nla_put_failure;
10549
10550 err = genlmsg_end(msg, hdr);
10551 if (err < 0) {
10552 nlmsg_free(msg);
10553 return;
10554 }
10555
10556 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10557 nl80211_mlme_mcgrp.id, gfp);
10558 return;
10559
10560 nla_put_failure:
10561 genlmsg_cancel(msg, hdr);
10562 nlmsg_free(msg);
10563}
10564EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10565
Jouni Malinen026331c2010-02-15 12:53:10 +020010566static int nl80211_netlink_notify(struct notifier_block * nb,
10567 unsigned long state,
10568 void *_notify)
10569{
10570 struct netlink_notify *notify = _notify;
10571 struct cfg80211_registered_device *rdev;
10572 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010573 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010574
10575 if (state != NETLINK_URELEASE)
10576 return NOTIFY_DONE;
10577
10578 rcu_read_lock();
10579
Johannes Berg5e7602302011-11-04 11:18:17 +010010580 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010581 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010582 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010583
10584 spin_lock_bh(&rdev->beacon_registrations_lock);
10585 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10586 list) {
10587 if (reg->nlportid == notify->portid) {
10588 list_del(&reg->list);
10589 kfree(reg);
10590 break;
10591 }
10592 }
10593 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010010594 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010595
10596 rcu_read_unlock();
10597
10598 return NOTIFY_DONE;
10599}
10600
10601static struct notifier_block nl80211_netlink_notifier = {
10602 .notifier_call = nl80211_netlink_notify,
10603};
10604
Jouni Malinen355199e2013-02-27 17:14:27 +020010605void cfg80211_ft_event(struct net_device *netdev,
10606 struct cfg80211_ft_event_params *ft_event)
10607{
10608 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
10609 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10610 struct sk_buff *msg;
10611 void *hdr;
10612 int err;
10613
10614 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
10615
10616 if (!ft_event->target_ap)
10617 return;
10618
10619 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10620 if (!msg)
10621 return;
10622
10623 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
10624 if (!hdr) {
10625 nlmsg_free(msg);
10626 return;
10627 }
10628
10629 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
10630 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
10631 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
10632 if (ft_event->ies)
10633 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
10634 if (ft_event->ric_ies)
10635 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
10636 ft_event->ric_ies);
10637
10638 err = genlmsg_end(msg, hdr);
10639 if (err < 0) {
10640 nlmsg_free(msg);
10641 return;
10642 }
10643
10644 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10645 nl80211_mlme_mcgrp.id, GFP_KERNEL);
10646}
10647EXPORT_SYMBOL(cfg80211_ft_event);
10648
Johannes Berg55682962007-09-20 13:09:35 -040010649/* initialisation/exit functions */
10650
10651int nl80211_init(void)
10652{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010653 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010654
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010655 err = genl_register_family_with_ops(&nl80211_fam,
10656 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010657 if (err)
10658 return err;
10659
Johannes Berg55682962007-09-20 13:09:35 -040010660 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10661 if (err)
10662 goto err_out;
10663
Johannes Berg2a519312009-02-10 21:25:55 +010010664 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10665 if (err)
10666 goto err_out;
10667
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010668 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10669 if (err)
10670 goto err_out;
10671
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010672 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10673 if (err)
10674 goto err_out;
10675
Johannes Bergaff89a92009-07-01 21:26:51 +020010676#ifdef CONFIG_NL80211_TESTMODE
10677 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10678 if (err)
10679 goto err_out;
10680#endif
10681
Jouni Malinen026331c2010-02-15 12:53:10 +020010682 err = netlink_register_notifier(&nl80211_netlink_notifier);
10683 if (err)
10684 goto err_out;
10685
Johannes Berg55682962007-09-20 13:09:35 -040010686 return 0;
10687 err_out:
10688 genl_unregister_family(&nl80211_fam);
10689 return err;
10690}
10691
10692void nl80211_exit(void)
10693{
Jouni Malinen026331c2010-02-15 12:53:10 +020010694 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010695 genl_unregister_family(&nl80211_fam);
10696}