blob: 0dca987abae492d4eb0a539b7483e53ad4cc53a4 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300283
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700286
Johannes Berg6c739412011-11-03 09:27:01 +0100287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-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);
Arend van Spriel5de17982013-04-18 15:49:00 +02001427 if (split) {
1428 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1429 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1430 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001431
Kalle Valo4745fc02011-11-17 19:06:10 +02001432#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001433 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001434#endif
1435
Johannes Berg8fdc6212009-03-14 09:34:01 +01001436#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001437
Johannes Berg3713b4e2013-02-14 16:19:38 +01001438 if (dev->ops->connect || dev->ops->auth) {
1439 i++;
1440 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001441 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001442 }
1443
Johannes Berg3713b4e2013-02-14 16:19:38 +01001444 if (dev->ops->disconnect || dev->ops->deauth) {
1445 i++;
1446 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1447 goto nla_put_failure;
1448 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001449
Johannes Berg3713b4e2013-02-14 16:19:38 +01001450 nla_nest_end(msg, nl_cmds);
1451 (*split_start)++;
1452 if (split)
1453 break;
1454 case 5:
1455 if (dev->ops->remain_on_channel &&
1456 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1457 nla_put_u32(msg,
1458 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1459 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001460 goto nla_put_failure;
1461
Johannes Berg3713b4e2013-02-14 16:19:38 +01001462 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1463 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1464 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001465
Johannes Berg3713b4e2013-02-14 16:19:38 +01001466 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1467 goto nla_put_failure;
1468 (*split_start)++;
1469 if (split)
1470 break;
1471 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001472#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001473 if (nl80211_send_wowlan(msg, dev, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001474 goto nla_put_failure;
1475 (*split_start)++;
1476 if (split)
1477 break;
1478#else
1479 (*split_start)++;
1480#endif
1481 case 7:
1482 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1483 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001484 goto nla_put_failure;
1485
Johannes Bergcdc89b92013-02-18 23:54:36 +01001486 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001487 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001488
Johannes Berg3713b4e2013-02-14 16:19:38 +01001489 (*split_start)++;
1490 if (split)
1491 break;
1492 case 8:
1493 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1494 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1495 dev->wiphy.ap_sme_capa))
1496 goto nla_put_failure;
1497
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001498 features = dev->wiphy.features;
1499 /*
1500 * We can only add the per-channel limit information if the
1501 * dump is split, otherwise it makes it too big. Therefore
1502 * only advertise it in that case.
1503 */
1504 if (split)
1505 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1506 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001507 goto nla_put_failure;
1508
1509 if (dev->wiphy.ht_capa_mod_mask &&
1510 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1511 sizeof(*dev->wiphy.ht_capa_mod_mask),
1512 dev->wiphy.ht_capa_mod_mask))
1513 goto nla_put_failure;
1514
1515 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1516 dev->wiphy.max_acl_mac_addrs &&
1517 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1518 dev->wiphy.max_acl_mac_addrs))
1519 goto nla_put_failure;
1520
1521 /*
1522 * Any information below this point is only available to
1523 * applications that can deal with it being split. This
1524 * helps ensure that newly added capabilities don't break
1525 * older tools by overrunning their buffers.
1526 *
1527 * We still increment split_start so that in the split
1528 * case we'll continue with more data in the next round,
1529 * but break unconditionally so unsplit data stops here.
1530 */
1531 (*split_start)++;
1532 break;
1533 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001534 if (dev->wiphy.extended_capabilities &&
1535 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1536 dev->wiphy.extended_capabilities_len,
1537 dev->wiphy.extended_capabilities) ||
1538 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1539 dev->wiphy.extended_capabilities_len,
1540 dev->wiphy.extended_capabilities_mask)))
1541 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001542
Johannes Bergee2aca32013-02-21 17:36:01 +01001543 if (dev->wiphy.vht_capa_mod_mask &&
1544 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1545 sizeof(*dev->wiphy.vht_capa_mod_mask),
1546 dev->wiphy.vht_capa_mod_mask))
1547 goto nla_put_failure;
1548
Johannes Berg3713b4e2013-02-14 16:19:38 +01001549 /* done */
1550 *split_start = 0;
1551 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001552 }
Johannes Berg55682962007-09-20 13:09:35 -04001553 return genlmsg_end(msg, hdr);
1554
1555 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001556 genlmsg_cancel(msg, hdr);
1557 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001558}
1559
1560static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1561{
Johannes Berg645e77d2013-03-01 14:03:49 +01001562 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001563 int start = cb->args[0];
1564 struct cfg80211_registered_device *dev;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001565 s64 filter_wiphy = -1;
1566 bool split = false;
1567 struct nlattr **tb = nl80211_fam.attrbuf;
1568 int res;
Johannes Berg55682962007-09-20 13:09:35 -04001569
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001570 mutex_lock(&cfg80211_mutex);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001571 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1572 tb, nl80211_fam.maxattr, nl80211_policy);
1573 if (res == 0) {
1574 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1575 if (tb[NL80211_ATTR_WIPHY])
1576 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1577 if (tb[NL80211_ATTR_WDEV])
1578 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1579 if (tb[NL80211_ATTR_IFINDEX]) {
1580 struct net_device *netdev;
1581 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1582
1583 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1584 if (!netdev) {
1585 mutex_unlock(&cfg80211_mutex);
1586 return -ENODEV;
1587 }
1588 if (netdev->ieee80211_ptr) {
1589 dev = wiphy_to_dev(
1590 netdev->ieee80211_ptr->wiphy);
1591 filter_wiphy = dev->wiphy_idx;
1592 }
1593 dev_put(netdev);
1594 }
1595 }
1596
Johannes Berg79c97e92009-07-07 03:56:12 +02001597 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001598 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1599 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001600 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001601 continue;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001602 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1603 continue;
1604 /* attempt to fit multiple wiphy data chunks into the skb */
1605 do {
1606 ret = nl80211_send_wiphy(dev, skb,
1607 NETLINK_CB(cb->skb).portid,
1608 cb->nlh->nlmsg_seq,
1609 NLM_F_MULTI,
1610 split, &cb->args[1],
1611 &cb->args[2],
1612 &cb->args[3]);
1613 if (ret < 0) {
1614 /*
1615 * If sending the wiphy data didn't fit (ENOBUFS
1616 * or EMSGSIZE returned), this SKB is still
1617 * empty (so it's not too big because another
1618 * wiphy dataset is already in the skb) and
1619 * we've not tried to adjust the dump allocation
1620 * yet ... then adjust the alloc size to be
1621 * bigger, and return 1 but with the empty skb.
1622 * This results in an empty message being RX'ed
1623 * in userspace, but that is ignored.
1624 *
1625 * We can then retry with the larger buffer.
1626 */
1627 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1628 !skb->len &&
1629 cb->min_dump_alloc < 4096) {
1630 cb->min_dump_alloc = 4096;
1631 mutex_unlock(&cfg80211_mutex);
1632 return 1;
1633 }
1634 idx--;
1635 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001636 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001637 } while (cb->args[1] > 0);
1638 break;
Johannes Berg55682962007-09-20 13:09:35 -04001639 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001640 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001641
1642 cb->args[0] = idx;
1643
1644 return skb->len;
1645}
1646
1647static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1648{
1649 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001650 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001651
Johannes Berg645e77d2013-03-01 14:03:49 +01001652 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001653 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001654 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001655
Johannes Berg3713b4e2013-02-14 16:19:38 +01001656 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1657 false, NULL, NULL, NULL) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001658 nlmsg_free(msg);
1659 return -ENOBUFS;
1660 }
Johannes Berg55682962007-09-20 13:09:35 -04001661
Johannes Berg134e6372009-07-10 09:51:34 +00001662 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001663}
1664
Jouni Malinen31888482008-10-30 16:59:24 +02001665static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1666 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1667 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1668 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1669 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1670 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1671};
1672
1673static int parse_txq_params(struct nlattr *tb[],
1674 struct ieee80211_txq_params *txq_params)
1675{
Johannes Berga3304b02012-03-28 11:04:24 +02001676 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001677 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1678 !tb[NL80211_TXQ_ATTR_AIFS])
1679 return -EINVAL;
1680
Johannes Berga3304b02012-03-28 11:04:24 +02001681 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001682 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1683 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1684 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1685 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1686
Johannes Berga3304b02012-03-28 11:04:24 +02001687 if (txq_params->ac >= NL80211_NUM_ACS)
1688 return -EINVAL;
1689
Jouni Malinen31888482008-10-30 16:59:24 +02001690 return 0;
1691}
1692
Johannes Bergf444de02010-05-05 15:25:02 +02001693static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1694{
1695 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001696 * You can only set the channel explicitly for WDS interfaces,
1697 * all others have their channel managed via their respective
1698 * "establish a connection" command (connect, join, ...)
1699 *
1700 * For AP/GO and mesh mode, the channel can be set with the
1701 * channel userspace API, but is only stored and passed to the
1702 * low-level driver when the AP starts or the mesh is joined.
1703 * This is for backward compatibility, userspace can also give
1704 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001705 *
1706 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001707 * whatever else is going on, so they have their own special
1708 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001709 */
1710 return !wdev ||
1711 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001712 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001713 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1714 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001715}
1716
Johannes Berg683b6d32012-11-08 21:25:48 +01001717static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1718 struct genl_info *info,
1719 struct cfg80211_chan_def *chandef)
1720{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301721 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001722
1723 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1724 return -EINVAL;
1725
1726 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1727
1728 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001729 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1730 chandef->center_freq1 = control_freq;
1731 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001732
1733 /* Primary channel not allowed */
1734 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1735 return -EINVAL;
1736
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001737 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1738 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001739
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001740 chantype = nla_get_u32(
1741 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1742
1743 switch (chantype) {
1744 case NL80211_CHAN_NO_HT:
1745 case NL80211_CHAN_HT20:
1746 case NL80211_CHAN_HT40PLUS:
1747 case NL80211_CHAN_HT40MINUS:
1748 cfg80211_chandef_create(chandef, chandef->chan,
1749 chantype);
1750 break;
1751 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001752 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001753 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001754 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1755 chandef->width =
1756 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1757 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1758 chandef->center_freq1 =
1759 nla_get_u32(
1760 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1761 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1762 chandef->center_freq2 =
1763 nla_get_u32(
1764 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1765 }
1766
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001767 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001768 return -EINVAL;
1769
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001770 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1771 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001772 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001773
Johannes Berg683b6d32012-11-08 21:25:48 +01001774 return 0;
1775}
1776
Johannes Bergf444de02010-05-05 15:25:02 +02001777static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1778 struct wireless_dev *wdev,
1779 struct genl_info *info)
1780{
Johannes Berg683b6d32012-11-08 21:25:48 +01001781 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001782 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001783 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1784
1785 if (wdev)
1786 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001787
Johannes Bergf444de02010-05-05 15:25:02 +02001788 if (!nl80211_can_set_dev_channel(wdev))
1789 return -EOPNOTSUPP;
1790
Johannes Berg683b6d32012-11-08 21:25:48 +01001791 result = nl80211_parse_chandef(rdev, info, &chandef);
1792 if (result)
1793 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001794
1795 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001796 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001797 case NL80211_IFTYPE_AP:
1798 case NL80211_IFTYPE_P2P_GO:
1799 if (wdev->beacon_interval) {
1800 result = -EBUSY;
1801 break;
1802 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001803 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001804 result = -EINVAL;
1805 break;
1806 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001807 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001808 result = 0;
1809 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001810 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001811 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001812 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001813 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001814 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001815 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001816 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001817 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001818 }
1819 mutex_unlock(&rdev->devlist_mtx);
1820
1821 return result;
1822}
1823
1824static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1825{
Johannes Berg4c476992010-10-04 21:36:35 +02001826 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1827 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001828
Johannes Berg4c476992010-10-04 21:36:35 +02001829 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001830}
1831
Bill Jordane8347eb2010-10-01 13:54:28 -04001832static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1833{
Johannes Berg43b19952010-10-07 13:10:30 +02001834 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1835 struct net_device *dev = info->user_ptr[1];
1836 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001837 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001838
1839 if (!info->attrs[NL80211_ATTR_MAC])
1840 return -EINVAL;
1841
Johannes Berg43b19952010-10-07 13:10:30 +02001842 if (netif_running(dev))
1843 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001844
Johannes Berg43b19952010-10-07 13:10:30 +02001845 if (!rdev->ops->set_wds_peer)
1846 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001847
Johannes Berg43b19952010-10-07 13:10:30 +02001848 if (wdev->iftype != NL80211_IFTYPE_WDS)
1849 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001850
1851 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001852 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001853}
1854
1855
Johannes Berg55682962007-09-20 13:09:35 -04001856static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1857{
1858 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001859 struct net_device *netdev = NULL;
1860 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001861 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001862 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001863 u32 changed;
1864 u8 retry_short = 0, retry_long = 0;
1865 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001866 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001867
Johannes Bergf444de02010-05-05 15:25:02 +02001868 /*
1869 * Try to find the wiphy and netdev. Normally this
1870 * function shouldn't need the netdev, but this is
1871 * done for backward compatibility -- previously
1872 * setting the channel was done per wiphy, but now
1873 * it is per netdev. Previous userland like hostapd
1874 * also passed a netdev to set_wiphy, so that it is
1875 * possible to let that go to the right netdev!
1876 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001877 mutex_lock(&cfg80211_mutex);
1878
Johannes Bergf444de02010-05-05 15:25:02 +02001879 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1880 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1881
1882 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1883 if (netdev && netdev->ieee80211_ptr) {
1884 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1885 mutex_lock(&rdev->mtx);
1886 } else
1887 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001888 }
1889
Johannes Bergf444de02010-05-05 15:25:02 +02001890 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001891 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1892 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001893 if (IS_ERR(rdev)) {
1894 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001895 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001896 }
1897 wdev = NULL;
1898 netdev = NULL;
1899 result = 0;
1900
1901 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001902 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001903 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001904
1905 /*
1906 * end workaround code, by now the rdev is available
1907 * and locked, and wdev may or may not be NULL.
1908 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001909
1910 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001911 result = cfg80211_dev_rename(
1912 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001913
1914 mutex_unlock(&cfg80211_mutex);
1915
1916 if (result)
1917 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001918
Jouni Malinen31888482008-10-30 16:59:24 +02001919 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1920 struct ieee80211_txq_params txq_params;
1921 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1922
1923 if (!rdev->ops->set_txq_params) {
1924 result = -EOPNOTSUPP;
1925 goto bad_res;
1926 }
1927
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001928 if (!netdev) {
1929 result = -EINVAL;
1930 goto bad_res;
1931 }
1932
Johannes Berg133a3ff2011-11-03 14:50:13 +01001933 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1934 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1935 result = -EINVAL;
1936 goto bad_res;
1937 }
1938
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001939 if (!netif_running(netdev)) {
1940 result = -ENETDOWN;
1941 goto bad_res;
1942 }
1943
Jouni Malinen31888482008-10-30 16:59:24 +02001944 nla_for_each_nested(nl_txq_params,
1945 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1946 rem_txq_params) {
1947 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1948 nla_data(nl_txq_params),
1949 nla_len(nl_txq_params),
1950 txq_params_policy);
1951 result = parse_txq_params(tb, &txq_params);
1952 if (result)
1953 goto bad_res;
1954
Hila Gonene35e4d22012-06-27 17:19:42 +03001955 result = rdev_set_txq_params(rdev, netdev,
1956 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001957 if (result)
1958 goto bad_res;
1959 }
1960 }
1961
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001962 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001963 result = __nl80211_set_channel(rdev,
1964 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1965 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001966 if (result)
1967 goto bad_res;
1968 }
1969
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001970 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001971 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001972 enum nl80211_tx_power_setting type;
1973 int idx, mbm = 0;
1974
Johannes Bergc8442112012-10-24 10:17:18 +02001975 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1976 txp_wdev = NULL;
1977
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001978 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001979 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001980 goto bad_res;
1981 }
1982
1983 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1984 type = nla_get_u32(info->attrs[idx]);
1985
1986 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1987 (type != NL80211_TX_POWER_AUTOMATIC)) {
1988 result = -EINVAL;
1989 goto bad_res;
1990 }
1991
1992 if (type != NL80211_TX_POWER_AUTOMATIC) {
1993 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1994 mbm = nla_get_u32(info->attrs[idx]);
1995 }
1996
Johannes Bergc8442112012-10-24 10:17:18 +02001997 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001998 if (result)
1999 goto bad_res;
2000 }
2001
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002002 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2003 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2004 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002005 if ((!rdev->wiphy.available_antennas_tx &&
2006 !rdev->wiphy.available_antennas_rx) ||
2007 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002008 result = -EOPNOTSUPP;
2009 goto bad_res;
2010 }
2011
2012 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2013 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2014
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002015 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002016 * available antenna masks, except for the "all" mask */
2017 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2018 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002019 result = -EINVAL;
2020 goto bad_res;
2021 }
2022
Bruno Randolf7f531e02010-12-16 11:30:22 +09002023 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2024 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002025
Hila Gonene35e4d22012-06-27 17:19:42 +03002026 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002027 if (result)
2028 goto bad_res;
2029 }
2030
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002031 changed = 0;
2032
2033 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2034 retry_short = nla_get_u8(
2035 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2036 if (retry_short == 0) {
2037 result = -EINVAL;
2038 goto bad_res;
2039 }
2040 changed |= WIPHY_PARAM_RETRY_SHORT;
2041 }
2042
2043 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2044 retry_long = nla_get_u8(
2045 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2046 if (retry_long == 0) {
2047 result = -EINVAL;
2048 goto bad_res;
2049 }
2050 changed |= WIPHY_PARAM_RETRY_LONG;
2051 }
2052
2053 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2054 frag_threshold = nla_get_u32(
2055 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2056 if (frag_threshold < 256) {
2057 result = -EINVAL;
2058 goto bad_res;
2059 }
2060 if (frag_threshold != (u32) -1) {
2061 /*
2062 * Fragments (apart from the last one) are required to
2063 * have even length. Make the fragmentation code
2064 * simpler by stripping LSB should someone try to use
2065 * odd threshold value.
2066 */
2067 frag_threshold &= ~0x1;
2068 }
2069 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2070 }
2071
2072 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2073 rts_threshold = nla_get_u32(
2074 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2075 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2076 }
2077
Lukáš Turek81077e82009-12-21 22:50:47 +01002078 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2079 coverage_class = nla_get_u8(
2080 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2081 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2082 }
2083
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002084 if (changed) {
2085 u8 old_retry_short, old_retry_long;
2086 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002087 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002088
2089 if (!rdev->ops->set_wiphy_params) {
2090 result = -EOPNOTSUPP;
2091 goto bad_res;
2092 }
2093
2094 old_retry_short = rdev->wiphy.retry_short;
2095 old_retry_long = rdev->wiphy.retry_long;
2096 old_frag_threshold = rdev->wiphy.frag_threshold;
2097 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002098 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002099
2100 if (changed & WIPHY_PARAM_RETRY_SHORT)
2101 rdev->wiphy.retry_short = retry_short;
2102 if (changed & WIPHY_PARAM_RETRY_LONG)
2103 rdev->wiphy.retry_long = retry_long;
2104 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2105 rdev->wiphy.frag_threshold = frag_threshold;
2106 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2107 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002108 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2109 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002110
Hila Gonene35e4d22012-06-27 17:19:42 +03002111 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002112 if (result) {
2113 rdev->wiphy.retry_short = old_retry_short;
2114 rdev->wiphy.retry_long = old_retry_long;
2115 rdev->wiphy.frag_threshold = old_frag_threshold;
2116 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002117 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002118 }
2119 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002120
Johannes Berg306d6112008-12-08 12:39:04 +01002121 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002122 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02002123 if (netdev)
2124 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002125 return result;
2126}
2127
Johannes Berg71bbc992012-06-15 15:30:18 +02002128static inline u64 wdev_id(struct wireless_dev *wdev)
2129{
2130 return (u64)wdev->identifier |
2131 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2132}
Johannes Berg55682962007-09-20 13:09:35 -04002133
Johannes Berg683b6d32012-11-08 21:25:48 +01002134static int nl80211_send_chandef(struct sk_buff *msg,
2135 struct cfg80211_chan_def *chandef)
2136{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002137 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002138
Johannes Berg683b6d32012-11-08 21:25:48 +01002139 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2140 chandef->chan->center_freq))
2141 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002142 switch (chandef->width) {
2143 case NL80211_CHAN_WIDTH_20_NOHT:
2144 case NL80211_CHAN_WIDTH_20:
2145 case NL80211_CHAN_WIDTH_40:
2146 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2147 cfg80211_get_chandef_type(chandef)))
2148 return -ENOBUFS;
2149 break;
2150 default:
2151 break;
2152 }
2153 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2154 return -ENOBUFS;
2155 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2156 return -ENOBUFS;
2157 if (chandef->center_freq2 &&
2158 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002159 return -ENOBUFS;
2160 return 0;
2161}
2162
Eric W. Biederman15e47302012-09-07 20:12:54 +00002163static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002164 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002165 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002166{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002167 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002168 void *hdr;
2169
Eric W. Biederman15e47302012-09-07 20:12:54 +00002170 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002171 if (!hdr)
2172 return -1;
2173
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002174 if (dev &&
2175 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002176 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002177 goto nla_put_failure;
2178
2179 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2180 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002181 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002182 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002183 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2184 rdev->devlist_generation ^
2185 (cfg80211_rdev_list_generation << 2)))
2186 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002187
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002188 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002189 int ret;
2190 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002191
Johannes Berg683b6d32012-11-08 21:25:48 +01002192 ret = rdev_get_channel(rdev, wdev, &chandef);
2193 if (ret == 0) {
2194 if (nl80211_send_chandef(msg, &chandef))
2195 goto nla_put_failure;
2196 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002197 }
2198
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002199 if (wdev->ssid_len) {
2200 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2201 goto nla_put_failure;
2202 }
2203
Johannes Berg55682962007-09-20 13:09:35 -04002204 return genlmsg_end(msg, hdr);
2205
2206 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002207 genlmsg_cancel(msg, hdr);
2208 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002209}
2210
2211static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2212{
2213 int wp_idx = 0;
2214 int if_idx = 0;
2215 int wp_start = cb->args[0];
2216 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002217 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002218 struct wireless_dev *wdev;
2219
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002220 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02002221 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2222 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002223 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002224 if (wp_idx < wp_start) {
2225 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002226 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002227 }
Johannes Berg55682962007-09-20 13:09:35 -04002228 if_idx = 0;
2229
Johannes Bergf5ea9122009-08-07 16:17:38 +02002230 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02002231 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002232 if (if_idx < if_start) {
2233 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002234 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002235 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002236 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002237 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002238 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002239 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002240 goto out;
2241 }
2242 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002243 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002244 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002245
2246 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002247 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002248 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002249 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002250
2251 cb->args[0] = wp_idx;
2252 cb->args[1] = if_idx;
2253
2254 return skb->len;
2255}
2256
2257static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2258{
2259 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002260 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002261 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002262
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002263 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002264 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002265 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002266
Eric W. Biederman15e47302012-09-07 20:12:54 +00002267 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002268 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002269 nlmsg_free(msg);
2270 return -ENOBUFS;
2271 }
Johannes Berg55682962007-09-20 13:09:35 -04002272
Johannes Berg134e6372009-07-10 09:51:34 +00002273 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002274}
2275
Michael Wu66f7ac52008-01-31 19:48:22 +01002276static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2277 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2278 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2279 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2280 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2281 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2282};
2283
2284static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2285{
2286 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2287 int flag;
2288
2289 *mntrflags = 0;
2290
2291 if (!nla)
2292 return -EINVAL;
2293
2294 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2295 nla, mntr_flags_policy))
2296 return -EINVAL;
2297
2298 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2299 if (flags[flag])
2300 *mntrflags |= (1<<flag);
2301
2302 return 0;
2303}
2304
Johannes Berg9bc383d2009-11-19 11:55:19 +01002305static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002306 struct net_device *netdev, u8 use_4addr,
2307 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002308{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002309 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002310 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002311 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002312 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002313 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002314
2315 switch (iftype) {
2316 case NL80211_IFTYPE_AP_VLAN:
2317 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2318 return 0;
2319 break;
2320 case NL80211_IFTYPE_STATION:
2321 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2322 return 0;
2323 break;
2324 default:
2325 break;
2326 }
2327
2328 return -EOPNOTSUPP;
2329}
2330
Johannes Berg55682962007-09-20 13:09:35 -04002331static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2332{
Johannes Berg4c476992010-10-04 21:36:35 +02002333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002334 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002335 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002336 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002337 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002338 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002339 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002340
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002341 memset(&params, 0, sizeof(params));
2342
Johannes Berg04a773a2009-04-19 21:24:32 +02002343 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002344
Johannes Berg723b0382008-09-16 20:22:09 +02002345 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002346 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002347 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002348 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002349 if (ntype > NL80211_IFTYPE_MAX)
2350 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002351 }
2352
Johannes Berg92ffe052008-09-16 20:39:36 +02002353 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002354 struct wireless_dev *wdev = dev->ieee80211_ptr;
2355
Johannes Berg4c476992010-10-04 21:36:35 +02002356 if (ntype != NL80211_IFTYPE_MESH_POINT)
2357 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002358 if (netif_running(dev))
2359 return -EBUSY;
2360
2361 wdev_lock(wdev);
2362 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2363 IEEE80211_MAX_MESH_ID_LEN);
2364 wdev->mesh_id_up_len =
2365 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2366 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2367 wdev->mesh_id_up_len);
2368 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002369 }
2370
Felix Fietkau8b787642009-11-10 18:53:10 +01002371 if (info->attrs[NL80211_ATTR_4ADDR]) {
2372 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2373 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002374 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002375 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002376 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002377 } else {
2378 params.use_4addr = -1;
2379 }
2380
Johannes Berg92ffe052008-09-16 20:39:36 +02002381 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002382 if (ntype != NL80211_IFTYPE_MONITOR)
2383 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002384 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2385 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002386 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002387 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002388
2389 flags = &_flags;
2390 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002391 }
Johannes Berg3b858752009-03-12 09:55:09 +01002392
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002393 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002394 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002395 else
2396 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002397
Johannes Berg9bc383d2009-11-19 11:55:19 +01002398 if (!err && params.use_4addr != -1)
2399 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2400
Johannes Berg55682962007-09-20 13:09:35 -04002401 return err;
2402}
2403
2404static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2405{
Johannes Berg4c476992010-10-04 21:36:35 +02002406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002407 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002408 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002409 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002410 int err;
2411 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002412 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002413
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002414 memset(&params, 0, sizeof(params));
2415
Johannes Berg55682962007-09-20 13:09:35 -04002416 if (!info->attrs[NL80211_ATTR_IFNAME])
2417 return -EINVAL;
2418
2419 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2420 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2421 if (type > NL80211_IFTYPE_MAX)
2422 return -EINVAL;
2423 }
2424
Johannes Berg79c97e92009-07-07 03:56:12 +02002425 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002426 !(rdev->wiphy.interface_modes & (1 << type)))
2427 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002428
Arend van Spriel1c18f142013-01-08 10:17:27 +01002429 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2430 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2431 ETH_ALEN);
2432 if (!is_valid_ether_addr(params.macaddr))
2433 return -EADDRNOTAVAIL;
2434 }
2435
Johannes Berg9bc383d2009-11-19 11:55:19 +01002436 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002437 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002438 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002439 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002440 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002441 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002442
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002443 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2444 if (!msg)
2445 return -ENOMEM;
2446
Michael Wu66f7ac52008-01-31 19:48:22 +01002447 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2448 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2449 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002450 wdev = rdev_add_virtual_intf(rdev,
2451 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2452 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002453 if (IS_ERR(wdev)) {
2454 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002455 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002456 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002457
Johannes Berg98104fde2012-06-16 00:19:54 +02002458 switch (type) {
2459 case NL80211_IFTYPE_MESH_POINT:
2460 if (!info->attrs[NL80211_ATTR_MESH_ID])
2461 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002462 wdev_lock(wdev);
2463 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2464 IEEE80211_MAX_MESH_ID_LEN);
2465 wdev->mesh_id_up_len =
2466 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2467 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2468 wdev->mesh_id_up_len);
2469 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002470 break;
2471 case NL80211_IFTYPE_P2P_DEVICE:
2472 /*
2473 * P2P Device doesn't have a netdev, so doesn't go
2474 * through the netdev notifier and must be added here
2475 */
2476 mutex_init(&wdev->mtx);
2477 INIT_LIST_HEAD(&wdev->event_list);
2478 spin_lock_init(&wdev->event_lock);
2479 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2480 spin_lock_init(&wdev->mgmt_registrations_lock);
2481
2482 mutex_lock(&rdev->devlist_mtx);
2483 wdev->identifier = ++rdev->wdev_id;
2484 list_add_rcu(&wdev->list, &rdev->wdev_list);
2485 rdev->devlist_generation++;
2486 mutex_unlock(&rdev->devlist_mtx);
2487 break;
2488 default:
2489 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002490 }
2491
Eric W. Biederman15e47302012-09-07 20:12:54 +00002492 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002493 rdev, wdev) < 0) {
2494 nlmsg_free(msg);
2495 return -ENOBUFS;
2496 }
2497
2498 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002499}
2500
2501static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2502{
Johannes Berg4c476992010-10-04 21:36:35 +02002503 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002504 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002505
Johannes Berg4c476992010-10-04 21:36:35 +02002506 if (!rdev->ops->del_virtual_intf)
2507 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002508
Johannes Berg84efbb82012-06-16 00:00:26 +02002509 /*
2510 * If we remove a wireless device without a netdev then clear
2511 * user_ptr[1] so that nl80211_post_doit won't dereference it
2512 * to check if it needs to do dev_put(). Otherwise it crashes
2513 * since the wdev has been freed, unlike with a netdev where
2514 * we need the dev_put() for the netdev to really be freed.
2515 */
2516 if (!wdev->netdev)
2517 info->user_ptr[1] = NULL;
2518
Hila Gonene35e4d22012-06-27 17:19:42 +03002519 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002520}
2521
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002522static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2523{
2524 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2525 struct net_device *dev = info->user_ptr[1];
2526 u16 noack_map;
2527
2528 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2529 return -EINVAL;
2530
2531 if (!rdev->ops->set_noack_map)
2532 return -EOPNOTSUPP;
2533
2534 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2535
Hila Gonene35e4d22012-06-27 17:19:42 +03002536 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002537}
2538
Johannes Berg41ade002007-12-19 02:03:29 +01002539struct get_key_cookie {
2540 struct sk_buff *msg;
2541 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002542 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002543};
2544
2545static void get_key_callback(void *c, struct key_params *params)
2546{
Johannes Bergb9454e82009-07-08 13:29:08 +02002547 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002548 struct get_key_cookie *cookie = c;
2549
David S. Miller9360ffd2012-03-29 04:41:26 -04002550 if ((params->key &&
2551 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2552 params->key_len, params->key)) ||
2553 (params->seq &&
2554 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2555 params->seq_len, params->seq)) ||
2556 (params->cipher &&
2557 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2558 params->cipher)))
2559 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002560
Johannes Bergb9454e82009-07-08 13:29:08 +02002561 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2562 if (!key)
2563 goto nla_put_failure;
2564
David S. Miller9360ffd2012-03-29 04:41:26 -04002565 if ((params->key &&
2566 nla_put(cookie->msg, NL80211_KEY_DATA,
2567 params->key_len, params->key)) ||
2568 (params->seq &&
2569 nla_put(cookie->msg, NL80211_KEY_SEQ,
2570 params->seq_len, params->seq)) ||
2571 (params->cipher &&
2572 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2573 params->cipher)))
2574 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002575
David S. Miller9360ffd2012-03-29 04:41:26 -04002576 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2577 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002578
2579 nla_nest_end(cookie->msg, key);
2580
Johannes Berg41ade002007-12-19 02:03:29 +01002581 return;
2582 nla_put_failure:
2583 cookie->error = 1;
2584}
2585
2586static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2587{
Johannes Berg4c476992010-10-04 21:36:35 +02002588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002589 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002590 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002591 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002592 const u8 *mac_addr = NULL;
2593 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002594 struct get_key_cookie cookie = {
2595 .error = 0,
2596 };
2597 void *hdr;
2598 struct sk_buff *msg;
2599
2600 if (info->attrs[NL80211_ATTR_KEY_IDX])
2601 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2602
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002603 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002604 return -EINVAL;
2605
2606 if (info->attrs[NL80211_ATTR_MAC])
2607 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2608
Johannes Berge31b8212010-10-05 19:39:30 +02002609 pairwise = !!mac_addr;
2610 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2611 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2612 if (kt >= NUM_NL80211_KEYTYPES)
2613 return -EINVAL;
2614 if (kt != NL80211_KEYTYPE_GROUP &&
2615 kt != NL80211_KEYTYPE_PAIRWISE)
2616 return -EINVAL;
2617 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2618 }
2619
Johannes Berg4c476992010-10-04 21:36:35 +02002620 if (!rdev->ops->get_key)
2621 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002622
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002623 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002624 if (!msg)
2625 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002626
Eric W. Biederman15e47302012-09-07 20:12:54 +00002627 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002628 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002629 if (IS_ERR(hdr))
2630 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002631
2632 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002633 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002634
David S. Miller9360ffd2012-03-29 04:41:26 -04002635 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2636 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2637 goto nla_put_failure;
2638 if (mac_addr &&
2639 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2640 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002641
Johannes Berge31b8212010-10-05 19:39:30 +02002642 if (pairwise && mac_addr &&
2643 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2644 return -ENOENT;
2645
Hila Gonene35e4d22012-06-27 17:19:42 +03002646 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2647 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002648
2649 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002650 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002651
2652 if (cookie.error)
2653 goto nla_put_failure;
2654
2655 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002656 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002657
2658 nla_put_failure:
2659 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002660 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002661 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002662 return err;
2663}
2664
2665static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2666{
Johannes Berg4c476992010-10-04 21:36:35 +02002667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002668 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002669 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002670 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002671
Johannes Bergb9454e82009-07-08 13:29:08 +02002672 err = nl80211_parse_key(info, &key);
2673 if (err)
2674 return err;
2675
2676 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002677 return -EINVAL;
2678
Johannes Bergb9454e82009-07-08 13:29:08 +02002679 /* only support setting default key */
2680 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002681 return -EINVAL;
2682
Johannes Bergfffd0932009-07-08 14:22:54 +02002683 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002684
2685 if (key.def) {
2686 if (!rdev->ops->set_default_key) {
2687 err = -EOPNOTSUPP;
2688 goto out;
2689 }
2690
2691 err = nl80211_key_allowed(dev->ieee80211_ptr);
2692 if (err)
2693 goto out;
2694
Hila Gonene35e4d22012-06-27 17:19:42 +03002695 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002696 key.def_uni, key.def_multi);
2697
2698 if (err)
2699 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002700
Johannes Berg3d23e342009-09-29 23:27:28 +02002701#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002702 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002703#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002704 } else {
2705 if (key.def_uni || !key.def_multi) {
2706 err = -EINVAL;
2707 goto out;
2708 }
2709
2710 if (!rdev->ops->set_default_mgmt_key) {
2711 err = -EOPNOTSUPP;
2712 goto out;
2713 }
2714
2715 err = nl80211_key_allowed(dev->ieee80211_ptr);
2716 if (err)
2717 goto out;
2718
Hila Gonene35e4d22012-06-27 17:19:42 +03002719 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002720 if (err)
2721 goto out;
2722
2723#ifdef CONFIG_CFG80211_WEXT
2724 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2725#endif
2726 }
2727
2728 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002729 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002730
Johannes Berg41ade002007-12-19 02:03:29 +01002731 return err;
2732}
2733
2734static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2735{
Johannes Berg4c476992010-10-04 21:36:35 +02002736 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002737 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002738 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002739 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002740 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002741
Johannes Bergb9454e82009-07-08 13:29:08 +02002742 err = nl80211_parse_key(info, &key);
2743 if (err)
2744 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002745
Johannes Bergb9454e82009-07-08 13:29:08 +02002746 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002747 return -EINVAL;
2748
Johannes Berg41ade002007-12-19 02:03:29 +01002749 if (info->attrs[NL80211_ATTR_MAC])
2750 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2751
Johannes Berge31b8212010-10-05 19:39:30 +02002752 if (key.type == -1) {
2753 if (mac_addr)
2754 key.type = NL80211_KEYTYPE_PAIRWISE;
2755 else
2756 key.type = NL80211_KEYTYPE_GROUP;
2757 }
2758
2759 /* for now */
2760 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2761 key.type != NL80211_KEYTYPE_GROUP)
2762 return -EINVAL;
2763
Johannes Berg4c476992010-10-04 21:36:35 +02002764 if (!rdev->ops->add_key)
2765 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002766
Johannes Berge31b8212010-10-05 19:39:30 +02002767 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2768 key.type == NL80211_KEYTYPE_PAIRWISE,
2769 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002770 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002771
2772 wdev_lock(dev->ieee80211_ptr);
2773 err = nl80211_key_allowed(dev->ieee80211_ptr);
2774 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002775 err = rdev_add_key(rdev, dev, key.idx,
2776 key.type == NL80211_KEYTYPE_PAIRWISE,
2777 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002778 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002779
Johannes Berg41ade002007-12-19 02:03:29 +01002780 return err;
2781}
2782
2783static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2784{
Johannes Berg4c476992010-10-04 21:36:35 +02002785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002786 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002787 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002788 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002789 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002790
Johannes Bergb9454e82009-07-08 13:29:08 +02002791 err = nl80211_parse_key(info, &key);
2792 if (err)
2793 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002794
2795 if (info->attrs[NL80211_ATTR_MAC])
2796 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2797
Johannes Berge31b8212010-10-05 19:39:30 +02002798 if (key.type == -1) {
2799 if (mac_addr)
2800 key.type = NL80211_KEYTYPE_PAIRWISE;
2801 else
2802 key.type = NL80211_KEYTYPE_GROUP;
2803 }
2804
2805 /* for now */
2806 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2807 key.type != NL80211_KEYTYPE_GROUP)
2808 return -EINVAL;
2809
Johannes Berg4c476992010-10-04 21:36:35 +02002810 if (!rdev->ops->del_key)
2811 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002812
Johannes Bergfffd0932009-07-08 14:22:54 +02002813 wdev_lock(dev->ieee80211_ptr);
2814 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002815
2816 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2817 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2818 err = -ENOENT;
2819
Johannes Bergfffd0932009-07-08 14:22:54 +02002820 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002821 err = rdev_del_key(rdev, dev, key.idx,
2822 key.type == NL80211_KEYTYPE_PAIRWISE,
2823 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002824
Johannes Berg3d23e342009-09-29 23:27:28 +02002825#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002826 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002827 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002828 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002829 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002830 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2831 }
2832#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002833 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002834
Johannes Berg41ade002007-12-19 02:03:29 +01002835 return err;
2836}
2837
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302838/* This function returns an error or the number of nested attributes */
2839static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2840{
2841 struct nlattr *attr;
2842 int n_entries = 0, tmp;
2843
2844 nla_for_each_nested(attr, nl_attr, tmp) {
2845 if (nla_len(attr) != ETH_ALEN)
2846 return -EINVAL;
2847
2848 n_entries++;
2849 }
2850
2851 return n_entries;
2852}
2853
2854/*
2855 * This function parses ACL information and allocates memory for ACL data.
2856 * On successful return, the calling function is responsible to free the
2857 * ACL buffer returned by this function.
2858 */
2859static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2860 struct genl_info *info)
2861{
2862 enum nl80211_acl_policy acl_policy;
2863 struct nlattr *attr;
2864 struct cfg80211_acl_data *acl;
2865 int i = 0, n_entries, tmp;
2866
2867 if (!wiphy->max_acl_mac_addrs)
2868 return ERR_PTR(-EOPNOTSUPP);
2869
2870 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2871 return ERR_PTR(-EINVAL);
2872
2873 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2874 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2875 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2876 return ERR_PTR(-EINVAL);
2877
2878 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2879 return ERR_PTR(-EINVAL);
2880
2881 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2882 if (n_entries < 0)
2883 return ERR_PTR(n_entries);
2884
2885 if (n_entries > wiphy->max_acl_mac_addrs)
2886 return ERR_PTR(-ENOTSUPP);
2887
2888 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2889 GFP_KERNEL);
2890 if (!acl)
2891 return ERR_PTR(-ENOMEM);
2892
2893 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2894 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2895 i++;
2896 }
2897
2898 acl->n_acl_entries = n_entries;
2899 acl->acl_policy = acl_policy;
2900
2901 return acl;
2902}
2903
2904static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2905{
2906 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2907 struct net_device *dev = info->user_ptr[1];
2908 struct cfg80211_acl_data *acl;
2909 int err;
2910
2911 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2912 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2913 return -EOPNOTSUPP;
2914
2915 if (!dev->ieee80211_ptr->beacon_interval)
2916 return -EINVAL;
2917
2918 acl = parse_acl_data(&rdev->wiphy, info);
2919 if (IS_ERR(acl))
2920 return PTR_ERR(acl);
2921
2922 err = rdev_set_mac_acl(rdev, dev, acl);
2923
2924 kfree(acl);
2925
2926 return err;
2927}
2928
Johannes Berg88600202012-02-13 15:17:18 +01002929static int nl80211_parse_beacon(struct genl_info *info,
2930 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002931{
Johannes Berg88600202012-02-13 15:17:18 +01002932 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002933
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002934 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2935 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2936 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2937 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002938 return -EINVAL;
2939
Johannes Berg88600202012-02-13 15:17:18 +01002940 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002941
Johannes Berged1b6cc2007-12-19 02:03:32 +01002942 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002943 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2944 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2945 if (!bcn->head_len)
2946 return -EINVAL;
2947 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002948 }
2949
2950 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002951 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2952 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002953 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002954 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002955 }
2956
Johannes Berg4c476992010-10-04 21:36:35 +02002957 if (!haveinfo)
2958 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002959
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002960 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002961 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2962 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002963 }
2964
2965 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002966 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002967 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002968 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002969 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2970 }
2971
2972 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002973 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002974 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002975 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002976 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2977 }
2978
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002979 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002980 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002981 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002982 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002983 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2984 }
2985
Johannes Berg88600202012-02-13 15:17:18 +01002986 return 0;
2987}
2988
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002989static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2990 struct cfg80211_ap_settings *params)
2991{
2992 struct wireless_dev *wdev;
2993 bool ret = false;
2994
2995 mutex_lock(&rdev->devlist_mtx);
2996
Johannes Berg89a54e42012-06-15 14:33:17 +02002997 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002998 if (wdev->iftype != NL80211_IFTYPE_AP &&
2999 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3000 continue;
3001
Johannes Berg683b6d32012-11-08 21:25:48 +01003002 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003003 continue;
3004
Johannes Berg683b6d32012-11-08 21:25:48 +01003005 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003006 ret = true;
3007 break;
3008 }
3009
3010 mutex_unlock(&rdev->devlist_mtx);
3011
3012 return ret;
3013}
3014
Jouni Malinene39e5b52012-09-30 19:29:39 +03003015static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3016 enum nl80211_auth_type auth_type,
3017 enum nl80211_commands cmd)
3018{
3019 if (auth_type > NL80211_AUTHTYPE_MAX)
3020 return false;
3021
3022 switch (cmd) {
3023 case NL80211_CMD_AUTHENTICATE:
3024 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3025 auth_type == NL80211_AUTHTYPE_SAE)
3026 return false;
3027 return true;
3028 case NL80211_CMD_CONNECT:
3029 case NL80211_CMD_START_AP:
3030 /* SAE not supported yet */
3031 if (auth_type == NL80211_AUTHTYPE_SAE)
3032 return false;
3033 return true;
3034 default:
3035 return false;
3036 }
3037}
3038
Johannes Berg88600202012-02-13 15:17:18 +01003039static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3040{
3041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3042 struct net_device *dev = info->user_ptr[1];
3043 struct wireless_dev *wdev = dev->ieee80211_ptr;
3044 struct cfg80211_ap_settings params;
3045 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003046 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003047
3048 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3049 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3050 return -EOPNOTSUPP;
3051
3052 if (!rdev->ops->start_ap)
3053 return -EOPNOTSUPP;
3054
3055 if (wdev->beacon_interval)
3056 return -EALREADY;
3057
3058 memset(&params, 0, sizeof(params));
3059
3060 /* these are required for START_AP */
3061 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3062 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3063 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3064 return -EINVAL;
3065
3066 err = nl80211_parse_beacon(info, &params.beacon);
3067 if (err)
3068 return err;
3069
3070 params.beacon_interval =
3071 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3072 params.dtim_period =
3073 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3074
3075 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3076 if (err)
3077 return err;
3078
3079 /*
3080 * In theory, some of these attributes should be required here
3081 * but since they were not used when the command was originally
3082 * added, keep them optional for old user space programs to let
3083 * them continue to work with drivers that do not need the
3084 * additional information -- drivers must check!
3085 */
3086 if (info->attrs[NL80211_ATTR_SSID]) {
3087 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3088 params.ssid_len =
3089 nla_len(info->attrs[NL80211_ATTR_SSID]);
3090 if (params.ssid_len == 0 ||
3091 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3092 return -EINVAL;
3093 }
3094
3095 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3096 params.hidden_ssid = nla_get_u32(
3097 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3098 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3099 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3100 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3101 return -EINVAL;
3102 }
3103
3104 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3105
3106 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3107 params.auth_type = nla_get_u32(
3108 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003109 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3110 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003111 return -EINVAL;
3112 } else
3113 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3114
3115 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3116 NL80211_MAX_NR_CIPHER_SUITES);
3117 if (err)
3118 return err;
3119
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303120 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3121 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3122 return -EOPNOTSUPP;
3123 params.inactivity_timeout = nla_get_u16(
3124 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3125 }
3126
Johannes Berg53cabad2012-11-14 15:17:28 +01003127 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3128 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3129 return -EINVAL;
3130 params.p2p_ctwindow =
3131 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3132 if (params.p2p_ctwindow > 127)
3133 return -EINVAL;
3134 if (params.p2p_ctwindow != 0 &&
3135 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3136 return -EINVAL;
3137 }
3138
3139 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3140 u8 tmp;
3141
3142 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3143 return -EINVAL;
3144 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3145 if (tmp > 1)
3146 return -EINVAL;
3147 params.p2p_opp_ps = tmp;
3148 if (params.p2p_opp_ps != 0 &&
3149 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3150 return -EINVAL;
3151 }
3152
Johannes Bergaa430da2012-05-16 23:50:18 +02003153 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003154 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3155 if (err)
3156 return err;
3157 } else if (wdev->preset_chandef.chan) {
3158 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003159 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003160 return -EINVAL;
3161
Johannes Berg683b6d32012-11-08 21:25:48 +01003162 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003163 return -EINVAL;
3164
Simon Wunderlich04f39042013-02-08 18:16:19 +01003165 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3166 if (err < 0)
3167 return err;
3168 if (err) {
3169 radar_detect_width = BIT(params.chandef.width);
3170 params.radar_required = true;
3171 }
3172
Michal Kaziore4e32452012-06-29 12:47:08 +02003173 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003174 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3175 params.chandef.chan,
3176 CHAN_MODE_SHARED,
3177 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003178 mutex_unlock(&rdev->devlist_mtx);
3179
3180 if (err)
3181 return err;
3182
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303183 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3184 params.acl = parse_acl_data(&rdev->wiphy, info);
3185 if (IS_ERR(params.acl))
3186 return PTR_ERR(params.acl);
3187 }
3188
Hila Gonene35e4d22012-06-27 17:19:42 +03003189 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003190 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003191 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003192 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003193 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003194 wdev->ssid_len = params.ssid_len;
3195 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003196 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303197
3198 kfree(params.acl);
3199
Johannes Berg56d18932011-05-09 18:41:15 +02003200 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003201}
3202
Johannes Berg88600202012-02-13 15:17:18 +01003203static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3204{
3205 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3206 struct net_device *dev = info->user_ptr[1];
3207 struct wireless_dev *wdev = dev->ieee80211_ptr;
3208 struct cfg80211_beacon_data params;
3209 int err;
3210
3211 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3212 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3213 return -EOPNOTSUPP;
3214
3215 if (!rdev->ops->change_beacon)
3216 return -EOPNOTSUPP;
3217
3218 if (!wdev->beacon_interval)
3219 return -EINVAL;
3220
3221 err = nl80211_parse_beacon(info, &params);
3222 if (err)
3223 return err;
3224
Hila Gonene35e4d22012-06-27 17:19:42 +03003225 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003226}
3227
3228static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003229{
Johannes Berg4c476992010-10-04 21:36:35 +02003230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3231 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003232
Michal Kazior60771782012-06-29 12:46:56 +02003233 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003234}
3235
Johannes Berg5727ef12007-12-19 02:03:34 +01003236static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3237 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3238 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3239 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003240 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003241 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003242 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003243};
3244
Johannes Bergeccb8e82009-05-11 21:57:56 +03003245static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003246 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003247 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003248{
3249 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003250 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003251 int flag;
3252
Johannes Bergeccb8e82009-05-11 21:57:56 +03003253 /*
3254 * Try parsing the new attribute first so userspace
3255 * can specify both for older kernels.
3256 */
3257 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3258 if (nla) {
3259 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003260
Johannes Bergeccb8e82009-05-11 21:57:56 +03003261 sta_flags = nla_data(nla);
3262 params->sta_flags_mask = sta_flags->mask;
3263 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003264 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003265 if ((params->sta_flags_mask |
3266 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3267 return -EINVAL;
3268 return 0;
3269 }
3270
3271 /* if present, parse the old attribute */
3272
3273 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003274 if (!nla)
3275 return 0;
3276
3277 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3278 nla, sta_flags_policy))
3279 return -EINVAL;
3280
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003281 /*
3282 * Only allow certain flags for interface types so that
3283 * other attributes are silently ignored. Remember that
3284 * this is backward compatibility code with old userspace
3285 * and shouldn't be hit in other cases anyway.
3286 */
3287 switch (iftype) {
3288 case NL80211_IFTYPE_AP:
3289 case NL80211_IFTYPE_AP_VLAN:
3290 case NL80211_IFTYPE_P2P_GO:
3291 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3292 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3293 BIT(NL80211_STA_FLAG_WME) |
3294 BIT(NL80211_STA_FLAG_MFP);
3295 break;
3296 case NL80211_IFTYPE_P2P_CLIENT:
3297 case NL80211_IFTYPE_STATION:
3298 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3299 BIT(NL80211_STA_FLAG_TDLS_PEER);
3300 break;
3301 case NL80211_IFTYPE_MESH_POINT:
3302 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3303 BIT(NL80211_STA_FLAG_MFP) |
3304 BIT(NL80211_STA_FLAG_AUTHORIZED);
3305 default:
3306 return -EINVAL;
3307 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003308
Johannes Berg3383b5a2012-05-10 20:14:43 +02003309 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3310 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003311 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003312
Johannes Berg3383b5a2012-05-10 20:14:43 +02003313 /* no longer support new API additions in old API */
3314 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3315 return -EINVAL;
3316 }
3317 }
3318
Johannes Berg5727ef12007-12-19 02:03:34 +01003319 return 0;
3320}
3321
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003322static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3323 int attr)
3324{
3325 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003326 u32 bitrate;
3327 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003328
3329 rate = nla_nest_start(msg, attr);
3330 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003331 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003332
3333 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3334 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003335 /* report 16-bit bitrate only if we can */
3336 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003337 if (bitrate > 0 &&
3338 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3339 return false;
3340 if (bitrate_compat > 0 &&
3341 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3342 return false;
3343
3344 if (info->flags & RATE_INFO_FLAGS_MCS) {
3345 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3346 return false;
3347 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3348 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3349 return false;
3350 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3351 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3352 return false;
3353 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3354 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3355 return false;
3356 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3357 return false;
3358 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3359 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3360 return false;
3361 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3362 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3363 return false;
3364 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3365 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3366 return false;
3367 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3368 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3369 return false;
3370 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3371 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3372 return false;
3373 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003374
3375 nla_nest_end(msg, rate);
3376 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003377}
3378
Eric W. Biederman15e47302012-09-07 20:12:54 +00003379static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003380 int flags,
3381 struct cfg80211_registered_device *rdev,
3382 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003383 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003384{
3385 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003386 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003387
Eric W. Biederman15e47302012-09-07 20:12:54 +00003388 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003389 if (!hdr)
3390 return -1;
3391
David S. Miller9360ffd2012-03-29 04:41:26 -04003392 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3393 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3394 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3395 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003396
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003397 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3398 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003399 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003400 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3401 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3402 sinfo->connected_time))
3403 goto nla_put_failure;
3404 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3405 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3406 sinfo->inactive_time))
3407 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003408 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3409 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003410 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003411 (u32)sinfo->rx_bytes))
3412 goto nla_put_failure;
3413 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3414 NL80211_STA_INFO_TX_BYTES64)) &&
3415 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3416 (u32)sinfo->tx_bytes))
3417 goto nla_put_failure;
3418 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3419 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003420 sinfo->rx_bytes))
3421 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003422 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3423 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003424 sinfo->tx_bytes))
3425 goto nla_put_failure;
3426 if ((sinfo->filled & STATION_INFO_LLID) &&
3427 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3428 goto nla_put_failure;
3429 if ((sinfo->filled & STATION_INFO_PLID) &&
3430 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3431 goto nla_put_failure;
3432 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3433 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3434 sinfo->plink_state))
3435 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003436 switch (rdev->wiphy.signal_type) {
3437 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003438 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3439 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3440 sinfo->signal))
3441 goto nla_put_failure;
3442 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3443 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3444 sinfo->signal_avg))
3445 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003446 break;
3447 default:
3448 break;
3449 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003450 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003451 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3452 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003453 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003454 }
3455 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3456 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3457 NL80211_STA_INFO_RX_BITRATE))
3458 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003459 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003460 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3461 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3462 sinfo->rx_packets))
3463 goto nla_put_failure;
3464 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3465 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3466 sinfo->tx_packets))
3467 goto nla_put_failure;
3468 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3469 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3470 sinfo->tx_retries))
3471 goto nla_put_failure;
3472 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3473 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3474 sinfo->tx_failed))
3475 goto nla_put_failure;
3476 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3477 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3478 sinfo->beacon_loss_count))
3479 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003480 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3481 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3482 sinfo->local_pm))
3483 goto nla_put_failure;
3484 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3485 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3486 sinfo->peer_pm))
3487 goto nla_put_failure;
3488 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3489 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3490 sinfo->nonpeer_pm))
3491 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003492 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3493 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3494 if (!bss_param)
3495 goto nla_put_failure;
3496
David S. Miller9360ffd2012-03-29 04:41:26 -04003497 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3498 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3499 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3500 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3501 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3502 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3503 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3504 sinfo->bss_param.dtim_period) ||
3505 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3506 sinfo->bss_param.beacon_interval))
3507 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003508
3509 nla_nest_end(msg, bss_param);
3510 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003511 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3512 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3513 sizeof(struct nl80211_sta_flag_update),
3514 &sinfo->sta_flags))
3515 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003516 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3517 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3518 sinfo->t_offset))
3519 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003520 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003521
David S. Miller9360ffd2012-03-29 04:41:26 -04003522 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3523 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3524 sinfo->assoc_req_ies))
3525 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003526
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003527 return genlmsg_end(msg, hdr);
3528
3529 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003530 genlmsg_cancel(msg, hdr);
3531 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003532}
3533
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003534static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003535 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003536{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003537 struct station_info sinfo;
3538 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003539 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003540 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003541 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003542 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003543
Johannes Berg97990a02013-04-19 01:02:55 +02003544 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003545 if (err)
3546 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003547
Johannes Berg97990a02013-04-19 01:02:55 +02003548 if (!wdev->netdev) {
3549 err = -EINVAL;
3550 goto out_err;
3551 }
3552
Johannes Bergbba95fe2008-07-29 13:22:51 +02003553 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003554 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003555 goto out_err;
3556 }
3557
Johannes Bergbba95fe2008-07-29 13:22:51 +02003558 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003559 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003560 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003561 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003562 if (err == -ENOENT)
3563 break;
3564 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003565 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003566
3567 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003568 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003569 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003570 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003571 &sinfo) < 0)
3572 goto out;
3573
3574 sta_idx++;
3575 }
3576
3577
3578 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003579 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003580 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003581 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003582 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003583
3584 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003585}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003586
Johannes Berg5727ef12007-12-19 02:03:34 +01003587static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3588{
Johannes Berg4c476992010-10-04 21:36:35 +02003589 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3590 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003591 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003592 struct sk_buff *msg;
3593 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003594 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003595
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003596 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003597
3598 if (!info->attrs[NL80211_ATTR_MAC])
3599 return -EINVAL;
3600
3601 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3602
Johannes Berg4c476992010-10-04 21:36:35 +02003603 if (!rdev->ops->get_station)
3604 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003605
Hila Gonene35e4d22012-06-27 17:19:42 +03003606 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003607 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003608 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003609
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003610 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003611 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003612 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003613
Eric W. Biederman15e47302012-09-07 20:12:54 +00003614 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003615 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003616 nlmsg_free(msg);
3617 return -ENOBUFS;
3618 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003619
Johannes Berg4c476992010-10-04 21:36:35 +02003620 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003621}
3622
Johannes Berg77ee7c82013-02-15 00:48:33 +01003623int cfg80211_check_station_change(struct wiphy *wiphy,
3624 struct station_parameters *params,
3625 enum cfg80211_station_type statype)
3626{
3627 if (params->listen_interval != -1)
3628 return -EINVAL;
3629 if (params->aid)
3630 return -EINVAL;
3631
3632 /* When you run into this, adjust the code below for the new flag */
3633 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3634
3635 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003636 case CFG80211_STA_MESH_PEER_KERNEL:
3637 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003638 /*
3639 * No ignoring the TDLS flag here -- the userspace mesh
3640 * code doesn't have the bug of including TDLS in the
3641 * mask everywhere.
3642 */
3643 if (params->sta_flags_mask &
3644 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3645 BIT(NL80211_STA_FLAG_MFP) |
3646 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3647 return -EINVAL;
3648 break;
3649 case CFG80211_STA_TDLS_PEER_SETUP:
3650 case CFG80211_STA_TDLS_PEER_ACTIVE:
3651 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3652 return -EINVAL;
3653 /* ignore since it can't change */
3654 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3655 break;
3656 default:
3657 /* disallow mesh-specific things */
3658 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3659 return -EINVAL;
3660 if (params->local_pm)
3661 return -EINVAL;
3662 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3663 return -EINVAL;
3664 }
3665
3666 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3667 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3668 /* TDLS can't be set, ... */
3669 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3670 return -EINVAL;
3671 /*
3672 * ... but don't bother the driver with it. This works around
3673 * a hostapd/wpa_supplicant issue -- it always includes the
3674 * TLDS_PEER flag in the mask even for AP mode.
3675 */
3676 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3677 }
3678
3679 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3680 /* reject other things that can't change */
3681 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3682 return -EINVAL;
3683 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3684 return -EINVAL;
3685 if (params->supported_rates)
3686 return -EINVAL;
3687 if (params->ext_capab || params->ht_capa || params->vht_capa)
3688 return -EINVAL;
3689 }
3690
3691 if (statype != CFG80211_STA_AP_CLIENT) {
3692 if (params->vlan)
3693 return -EINVAL;
3694 }
3695
3696 switch (statype) {
3697 case CFG80211_STA_AP_MLME_CLIENT:
3698 /* Use this only for authorizing/unauthorizing a station */
3699 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3700 return -EOPNOTSUPP;
3701 break;
3702 case CFG80211_STA_AP_CLIENT:
3703 /* accept only the listed bits */
3704 if (params->sta_flags_mask &
3705 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3706 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3707 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3708 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3709 BIT(NL80211_STA_FLAG_WME) |
3710 BIT(NL80211_STA_FLAG_MFP)))
3711 return -EINVAL;
3712
3713 /* but authenticated/associated only if driver handles it */
3714 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3715 params->sta_flags_mask &
3716 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3717 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3718 return -EINVAL;
3719 break;
3720 case CFG80211_STA_IBSS:
3721 case CFG80211_STA_AP_STA:
3722 /* reject any changes other than AUTHORIZED */
3723 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3724 return -EINVAL;
3725 break;
3726 case CFG80211_STA_TDLS_PEER_SETUP:
3727 /* reject any changes other than AUTHORIZED or WME */
3728 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3729 BIT(NL80211_STA_FLAG_WME)))
3730 return -EINVAL;
3731 /* force (at least) rates when authorizing */
3732 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3733 !params->supported_rates)
3734 return -EINVAL;
3735 break;
3736 case CFG80211_STA_TDLS_PEER_ACTIVE:
3737 /* reject any changes */
3738 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003739 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003740 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3741 return -EINVAL;
3742 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003743 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003744 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3745 return -EINVAL;
3746 break;
3747 }
3748
3749 return 0;
3750}
3751EXPORT_SYMBOL(cfg80211_check_station_change);
3752
Johannes Berg5727ef12007-12-19 02:03:34 +01003753/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003754 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003755 */
Johannes Berg80b99892011-11-18 16:23:01 +01003756static struct net_device *get_vlan(struct genl_info *info,
3757 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003758{
Johannes Berg463d0182009-07-14 00:33:35 +02003759 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003760 struct net_device *v;
3761 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003762
Johannes Berg80b99892011-11-18 16:23:01 +01003763 if (!vlanattr)
3764 return NULL;
3765
3766 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3767 if (!v)
3768 return ERR_PTR(-ENODEV);
3769
3770 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3771 ret = -EINVAL;
3772 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003773 }
Johannes Berg80b99892011-11-18 16:23:01 +01003774
Johannes Berg77ee7c82013-02-15 00:48:33 +01003775 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3776 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3777 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3778 ret = -EINVAL;
3779 goto error;
3780 }
3781
Johannes Berg80b99892011-11-18 16:23:01 +01003782 if (!netif_running(v)) {
3783 ret = -ENETDOWN;
3784 goto error;
3785 }
3786
3787 return v;
3788 error:
3789 dev_put(v);
3790 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003791}
3792
Jouni Malinendf881292013-02-14 21:10:54 +02003793static struct nla_policy
3794nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3795 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3796 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3797};
3798
Johannes Bergff276692013-02-15 00:09:01 +01003799static int nl80211_parse_sta_wme(struct genl_info *info,
3800 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003801{
Jouni Malinendf881292013-02-14 21:10:54 +02003802 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3803 struct nlattr *nla;
3804 int err;
3805
Jouni Malinendf881292013-02-14 21:10:54 +02003806 /* parse WME attributes if present */
3807 if (!info->attrs[NL80211_ATTR_STA_WME])
3808 return 0;
3809
3810 nla = info->attrs[NL80211_ATTR_STA_WME];
3811 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3812 nl80211_sta_wme_policy);
3813 if (err)
3814 return err;
3815
3816 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3817 params->uapsd_queues = nla_get_u8(
3818 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3819 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3820 return -EINVAL;
3821
3822 if (tb[NL80211_STA_WME_MAX_SP])
3823 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3824
3825 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3826 return -EINVAL;
3827
3828 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3829
3830 return 0;
3831}
3832
Johannes Bergff276692013-02-15 00:09:01 +01003833static int nl80211_set_station_tdls(struct genl_info *info,
3834 struct station_parameters *params)
3835{
3836 /* Dummy STA entry gets updated once the peer capabilities are known */
3837 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3838 params->ht_capa =
3839 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3840 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3841 params->vht_capa =
3842 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3843
3844 return nl80211_parse_sta_wme(info, params);
3845}
3846
Johannes Berg5727ef12007-12-19 02:03:34 +01003847static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3848{
Johannes Berg4c476992010-10-04 21:36:35 +02003849 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003850 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003851 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003852 u8 *mac_addr;
3853 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003854
3855 memset(&params, 0, sizeof(params));
3856
3857 params.listen_interval = -1;
3858
Johannes Berg77ee7c82013-02-15 00:48:33 +01003859 if (!rdev->ops->change_station)
3860 return -EOPNOTSUPP;
3861
Johannes Berg5727ef12007-12-19 02:03:34 +01003862 if (info->attrs[NL80211_ATTR_STA_AID])
3863 return -EINVAL;
3864
3865 if (!info->attrs[NL80211_ATTR_MAC])
3866 return -EINVAL;
3867
3868 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3869
3870 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3871 params.supported_rates =
3872 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3873 params.supported_rates_len =
3874 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3875 }
3876
Jouni Malinen9d62a982013-02-14 21:10:13 +02003877 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3878 params.capability =
3879 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3880 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3881 }
3882
3883 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3884 params.ext_capab =
3885 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3886 params.ext_capab_len =
3887 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3888 }
3889
Jouni Malinendf881292013-02-14 21:10:54 +02003890 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003891 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003892
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003893 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003894 return -EINVAL;
3895
Johannes Bergf8bacc22013-02-14 23:27:01 +01003896 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003897 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003898 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3899 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3900 return -EINVAL;
3901 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003902
Johannes Bergf8bacc22013-02-14 23:27:01 +01003903 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003904 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003905 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3906 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3907 return -EINVAL;
3908 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3909 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003910
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003911 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3912 enum nl80211_mesh_power_mode pm = nla_get_u32(
3913 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3914
3915 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3916 pm > NL80211_MESH_POWER_MAX)
3917 return -EINVAL;
3918
3919 params.local_pm = pm;
3920 }
3921
Johannes Berg77ee7c82013-02-15 00:48:33 +01003922 /* Include parameters for TDLS peer (will check later) */
3923 err = nl80211_set_station_tdls(info, &params);
3924 if (err)
3925 return err;
3926
3927 params.vlan = get_vlan(info, rdev);
3928 if (IS_ERR(params.vlan))
3929 return PTR_ERR(params.vlan);
3930
Johannes Berga97f4422009-06-18 17:23:43 +02003931 switch (dev->ieee80211_ptr->iftype) {
3932 case NL80211_IFTYPE_AP:
3933 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003934 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003935 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003936 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01003937 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02003938 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02003939 break;
3940 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003941 err = -EOPNOTSUPP;
3942 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02003943 }
3944
Johannes Berg77ee7c82013-02-15 00:48:33 +01003945 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03003946 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003947
Johannes Berg77ee7c82013-02-15 00:48:33 +01003948 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01003949 if (params.vlan)
3950 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003951
Johannes Berg5727ef12007-12-19 02:03:34 +01003952 return err;
3953}
3954
3955static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3956{
Johannes Berg4c476992010-10-04 21:36:35 +02003957 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003958 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003959 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003960 struct station_parameters params;
3961 u8 *mac_addr = NULL;
3962
3963 memset(&params, 0, sizeof(params));
3964
Johannes Berg984c3112013-02-14 23:43:25 +01003965 if (!rdev->ops->add_station)
3966 return -EOPNOTSUPP;
3967
Johannes Berg5727ef12007-12-19 02:03:34 +01003968 if (!info->attrs[NL80211_ATTR_MAC])
3969 return -EINVAL;
3970
Johannes Berg5727ef12007-12-19 02:03:34 +01003971 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3972 return -EINVAL;
3973
3974 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3975 return -EINVAL;
3976
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003977 if (!info->attrs[NL80211_ATTR_STA_AID])
3978 return -EINVAL;
3979
Johannes Berg5727ef12007-12-19 02:03:34 +01003980 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3981 params.supported_rates =
3982 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3983 params.supported_rates_len =
3984 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3985 params.listen_interval =
3986 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003987
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003988 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3989 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3990 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003991
Jouni Malinen9d62a982013-02-14 21:10:13 +02003992 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3993 params.capability =
3994 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3995 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3996 }
3997
3998 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3999 params.ext_capab =
4000 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4001 params.ext_capab_len =
4002 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4003 }
4004
Jouni Malinen36aedc92008-08-25 11:58:58 +03004005 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4006 params.ht_capa =
4007 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004008
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004009 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4010 params.vht_capa =
4011 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4012
Johannes Bergf8bacc22013-02-14 23:27:01 +01004013 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004014 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004015 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4016 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4017 return -EINVAL;
4018 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004019
Johannes Bergff276692013-02-15 00:09:01 +01004020 err = nl80211_parse_sta_wme(info, &params);
4021 if (err)
4022 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004023
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004024 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004025 return -EINVAL;
4026
Johannes Berg77ee7c82013-02-15 00:48:33 +01004027 /* When you run into this, adjust the code below for the new flag */
4028 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4029
Johannes Bergbdd90d52011-12-14 12:20:27 +01004030 switch (dev->ieee80211_ptr->iftype) {
4031 case NL80211_IFTYPE_AP:
4032 case NL80211_IFTYPE_AP_VLAN:
4033 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004034 /* ignore WME attributes if iface/sta is not capable */
4035 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4036 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4037 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004038
Johannes Bergbdd90d52011-12-14 12:20:27 +01004039 /* TDLS peers cannot be added */
4040 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004041 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004042 /* but don't bother the driver with it */
4043 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004044
Johannes Bergd582cff2012-10-26 17:53:44 +02004045 /* allow authenticated/associated only if driver handles it */
4046 if (!(rdev->wiphy.features &
4047 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4048 params.sta_flags_mask &
4049 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4050 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4051 return -EINVAL;
4052
Johannes Bergbdd90d52011-12-14 12:20:27 +01004053 /* must be last in here for error handling */
4054 params.vlan = get_vlan(info, rdev);
4055 if (IS_ERR(params.vlan))
4056 return PTR_ERR(params.vlan);
4057 break;
4058 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004059 /* ignore uAPSD data */
4060 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4061
Johannes Bergd582cff2012-10-26 17:53:44 +02004062 /* associated is disallowed */
4063 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4064 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004065 /* TDLS peers cannot be added */
4066 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004067 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004068 break;
4069 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004070 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004071 /* ignore uAPSD data */
4072 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4073
Johannes Berg77ee7c82013-02-15 00:48:33 +01004074 /* these are disallowed */
4075 if (params.sta_flags_mask &
4076 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4077 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004078 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004079 /* Only TDLS peers can be added */
4080 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4081 return -EINVAL;
4082 /* Can only add if TDLS ... */
4083 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4084 return -EOPNOTSUPP;
4085 /* ... with external setup is supported */
4086 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4087 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004088 /*
4089 * Older wpa_supplicant versions always mark the TDLS peer
4090 * as authorized, but it shouldn't yet be.
4091 */
4092 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004093 break;
4094 default:
4095 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004096 }
4097
Johannes Bergbdd90d52011-12-14 12:20:27 +01004098 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004099
Hila Gonene35e4d22012-06-27 17:19:42 +03004100 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004101
Johannes Berg5727ef12007-12-19 02:03:34 +01004102 if (params.vlan)
4103 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004104 return err;
4105}
4106
4107static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4108{
Johannes Berg4c476992010-10-04 21:36:35 +02004109 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4110 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004111 u8 *mac_addr = NULL;
4112
4113 if (info->attrs[NL80211_ATTR_MAC])
4114 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4115
Johannes Berge80cf852009-05-11 14:43:13 +02004116 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004117 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004118 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004119 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4120 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004121
Johannes Berg4c476992010-10-04 21:36:35 +02004122 if (!rdev->ops->del_station)
4123 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004124
Hila Gonene35e4d22012-06-27 17:19:42 +03004125 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004126}
4127
Eric W. Biederman15e47302012-09-07 20:12:54 +00004128static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004129 int flags, struct net_device *dev,
4130 u8 *dst, u8 *next_hop,
4131 struct mpath_info *pinfo)
4132{
4133 void *hdr;
4134 struct nlattr *pinfoattr;
4135
Eric W. Biederman15e47302012-09-07 20:12:54 +00004136 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004137 if (!hdr)
4138 return -1;
4139
David S. Miller9360ffd2012-03-29 04:41:26 -04004140 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4141 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4142 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4143 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4144 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004145
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004146 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4147 if (!pinfoattr)
4148 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004149 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4150 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4151 pinfo->frame_qlen))
4152 goto nla_put_failure;
4153 if (((pinfo->filled & MPATH_INFO_SN) &&
4154 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4155 ((pinfo->filled & MPATH_INFO_METRIC) &&
4156 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4157 pinfo->metric)) ||
4158 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4159 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4160 pinfo->exptime)) ||
4161 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4162 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4163 pinfo->flags)) ||
4164 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4165 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4166 pinfo->discovery_timeout)) ||
4167 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4168 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4169 pinfo->discovery_retries)))
4170 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004171
4172 nla_nest_end(msg, pinfoattr);
4173
4174 return genlmsg_end(msg, hdr);
4175
4176 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004177 genlmsg_cancel(msg, hdr);
4178 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004179}
4180
4181static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004182 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004183{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004184 struct mpath_info pinfo;
4185 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004186 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004187 u8 dst[ETH_ALEN];
4188 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004189 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004190 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004191
Johannes Berg97990a02013-04-19 01:02:55 +02004192 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004193 if (err)
4194 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004195
4196 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004197 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004198 goto out_err;
4199 }
4200
Johannes Berg97990a02013-04-19 01:02:55 +02004201 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004202 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004203 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004204 }
4205
Johannes Bergbba95fe2008-07-29 13:22:51 +02004206 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004207 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4208 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004209 if (err == -ENOENT)
4210 break;
4211 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004212 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004213
Eric W. Biederman15e47302012-09-07 20:12:54 +00004214 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004215 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004216 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004217 &pinfo) < 0)
4218 goto out;
4219
4220 path_idx++;
4221 }
4222
4223
4224 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004225 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004226 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004227 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004228 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004229 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004230}
4231
4232static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4233{
Johannes Berg4c476992010-10-04 21:36:35 +02004234 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004235 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004236 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004237 struct mpath_info pinfo;
4238 struct sk_buff *msg;
4239 u8 *dst = NULL;
4240 u8 next_hop[ETH_ALEN];
4241
4242 memset(&pinfo, 0, sizeof(pinfo));
4243
4244 if (!info->attrs[NL80211_ATTR_MAC])
4245 return -EINVAL;
4246
4247 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4248
Johannes Berg4c476992010-10-04 21:36:35 +02004249 if (!rdev->ops->get_mpath)
4250 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004251
Johannes Berg4c476992010-10-04 21:36:35 +02004252 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4253 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004254
Hila Gonene35e4d22012-06-27 17:19:42 +03004255 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004256 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004257 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004258
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004259 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004260 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004261 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004262
Eric W. Biederman15e47302012-09-07 20:12:54 +00004263 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004264 dev, dst, next_hop, &pinfo) < 0) {
4265 nlmsg_free(msg);
4266 return -ENOBUFS;
4267 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004268
Johannes Berg4c476992010-10-04 21:36:35 +02004269 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004270}
4271
4272static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4273{
Johannes Berg4c476992010-10-04 21:36:35 +02004274 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4275 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004276 u8 *dst = NULL;
4277 u8 *next_hop = NULL;
4278
4279 if (!info->attrs[NL80211_ATTR_MAC])
4280 return -EINVAL;
4281
4282 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4283 return -EINVAL;
4284
4285 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4286 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4287
Johannes Berg4c476992010-10-04 21:36:35 +02004288 if (!rdev->ops->change_mpath)
4289 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004290
Johannes Berg4c476992010-10-04 21:36:35 +02004291 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4292 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004293
Hila Gonene35e4d22012-06-27 17:19:42 +03004294 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004295}
Johannes Berg4c476992010-10-04 21:36:35 +02004296
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004297static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4298{
Johannes Berg4c476992010-10-04 21:36:35 +02004299 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4300 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004301 u8 *dst = NULL;
4302 u8 *next_hop = NULL;
4303
4304 if (!info->attrs[NL80211_ATTR_MAC])
4305 return -EINVAL;
4306
4307 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4308 return -EINVAL;
4309
4310 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4311 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4312
Johannes Berg4c476992010-10-04 21:36:35 +02004313 if (!rdev->ops->add_mpath)
4314 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004315
Johannes Berg4c476992010-10-04 21:36:35 +02004316 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4317 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004318
Hila Gonene35e4d22012-06-27 17:19:42 +03004319 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004320}
4321
4322static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4323{
Johannes Berg4c476992010-10-04 21:36:35 +02004324 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4325 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004326 u8 *dst = NULL;
4327
4328 if (info->attrs[NL80211_ATTR_MAC])
4329 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4330
Johannes Berg4c476992010-10-04 21:36:35 +02004331 if (!rdev->ops->del_mpath)
4332 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004333
Hila Gonene35e4d22012-06-27 17:19:42 +03004334 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004335}
4336
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004337static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4338{
Johannes Berg4c476992010-10-04 21:36:35 +02004339 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4340 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004341 struct bss_parameters params;
4342
4343 memset(&params, 0, sizeof(params));
4344 /* default to not changing parameters */
4345 params.use_cts_prot = -1;
4346 params.use_short_preamble = -1;
4347 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004348 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004349 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004350 params.p2p_ctwindow = -1;
4351 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004352
4353 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4354 params.use_cts_prot =
4355 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4356 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4357 params.use_short_preamble =
4358 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4359 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4360 params.use_short_slot_time =
4361 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004362 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4363 params.basic_rates =
4364 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4365 params.basic_rates_len =
4366 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4367 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004368 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4369 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004370 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4371 params.ht_opmode =
4372 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004373
Johannes Berg53cabad2012-11-14 15:17:28 +01004374 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4375 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4376 return -EINVAL;
4377 params.p2p_ctwindow =
4378 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4379 if (params.p2p_ctwindow < 0)
4380 return -EINVAL;
4381 if (params.p2p_ctwindow != 0 &&
4382 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4383 return -EINVAL;
4384 }
4385
4386 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4387 u8 tmp;
4388
4389 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4390 return -EINVAL;
4391 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4392 if (tmp > 1)
4393 return -EINVAL;
4394 params.p2p_opp_ps = tmp;
4395 if (params.p2p_opp_ps &&
4396 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4397 return -EINVAL;
4398 }
4399
Johannes Berg4c476992010-10-04 21:36:35 +02004400 if (!rdev->ops->change_bss)
4401 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004402
Johannes Berg074ac8d2010-09-16 14:58:22 +02004403 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004404 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4405 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004406
Hila Gonene35e4d22012-06-27 17:19:42 +03004407 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004408}
4409
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004410static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004411 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4412 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4413 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4414 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4415 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4416 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4417};
4418
4419static int parse_reg_rule(struct nlattr *tb[],
4420 struct ieee80211_reg_rule *reg_rule)
4421{
4422 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4423 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4424
4425 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4426 return -EINVAL;
4427 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4428 return -EINVAL;
4429 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4430 return -EINVAL;
4431 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4432 return -EINVAL;
4433 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4434 return -EINVAL;
4435
4436 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4437
4438 freq_range->start_freq_khz =
4439 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4440 freq_range->end_freq_khz =
4441 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4442 freq_range->max_bandwidth_khz =
4443 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4444
4445 power_rule->max_eirp =
4446 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4447
4448 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4449 power_rule->max_antenna_gain =
4450 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4451
4452 return 0;
4453}
4454
4455static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4456{
4457 int r;
4458 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004459 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004460
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004461 /*
4462 * You should only get this when cfg80211 hasn't yet initialized
4463 * completely when built-in to the kernel right between the time
4464 * window between nl80211_init() and regulatory_init(), if that is
4465 * even possible.
4466 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004467 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004468 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004469
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004470 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4471 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004472
4473 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4474
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004475 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4476 user_reg_hint_type =
4477 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4478 else
4479 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4480
4481 switch (user_reg_hint_type) {
4482 case NL80211_USER_REG_HINT_USER:
4483 case NL80211_USER_REG_HINT_CELL_BASE:
4484 break;
4485 default:
4486 return -EINVAL;
4487 }
4488
4489 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004490
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004491 return r;
4492}
4493
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004494static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004495 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004496{
Johannes Berg4c476992010-10-04 21:36:35 +02004497 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004498 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004499 struct wireless_dev *wdev = dev->ieee80211_ptr;
4500 struct mesh_config cur_params;
4501 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004502 void *hdr;
4503 struct nlattr *pinfoattr;
4504 struct sk_buff *msg;
4505
Johannes Berg29cbe682010-12-03 09:20:44 +01004506 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4507 return -EOPNOTSUPP;
4508
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004509 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004510 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004511
Johannes Berg29cbe682010-12-03 09:20:44 +01004512 wdev_lock(wdev);
4513 /* If not connected, get default parameters */
4514 if (!wdev->mesh_id_len)
4515 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4516 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004517 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004518 wdev_unlock(wdev);
4519
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004520 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004521 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004522
4523 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004524 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004525 if (!msg)
4526 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004527 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004528 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004529 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004530 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004531 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004532 if (!pinfoattr)
4533 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004534 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4535 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4536 cur_params.dot11MeshRetryTimeout) ||
4537 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4538 cur_params.dot11MeshConfirmTimeout) ||
4539 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4540 cur_params.dot11MeshHoldingTimeout) ||
4541 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4542 cur_params.dot11MeshMaxPeerLinks) ||
4543 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4544 cur_params.dot11MeshMaxRetries) ||
4545 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4546 cur_params.dot11MeshTTL) ||
4547 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4548 cur_params.element_ttl) ||
4549 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4550 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004551 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4552 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004553 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4554 cur_params.dot11MeshHWMPmaxPREQretries) ||
4555 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4556 cur_params.path_refresh_time) ||
4557 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4558 cur_params.min_discovery_timeout) ||
4559 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4560 cur_params.dot11MeshHWMPactivePathTimeout) ||
4561 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4562 cur_params.dot11MeshHWMPpreqMinInterval) ||
4563 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4564 cur_params.dot11MeshHWMPperrMinInterval) ||
4565 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4566 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4567 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4568 cur_params.dot11MeshHWMPRootMode) ||
4569 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4570 cur_params.dot11MeshHWMPRannInterval) ||
4571 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4572 cur_params.dot11MeshGateAnnouncementProtocol) ||
4573 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4574 cur_params.dot11MeshForwarding) ||
4575 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004576 cur_params.rssi_threshold) ||
4577 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004578 cur_params.ht_opmode) ||
4579 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4580 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4581 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004582 cur_params.dot11MeshHWMProotInterval) ||
4583 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004584 cur_params.dot11MeshHWMPconfirmationInterval) ||
4585 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4586 cur_params.power_mode) ||
4587 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4588 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004589 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004590 nla_nest_end(msg, pinfoattr);
4591 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004592 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004593
Johannes Berg3b858752009-03-12 09:55:09 +01004594 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004595 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004596 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004597 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004598 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004599}
4600
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004601static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004602 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4603 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4604 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4605 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4606 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4607 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004608 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004609 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004610 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004611 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4612 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4613 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4614 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4615 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004616 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004617 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004618 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004619 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004620 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004621 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004622 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4623 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004624 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4625 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004626 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004627 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4628 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004629};
4630
Javier Cardonac80d5452010-12-16 17:37:49 -08004631static const struct nla_policy
4632 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004633 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004634 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4635 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004636 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004637 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004638 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004639 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004640 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004641};
4642
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004643static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004644 struct mesh_config *cfg,
4645 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004646{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004647 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004648 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004649
Marco Porschea54fba2013-01-07 16:04:48 +01004650#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4651do { \
4652 if (tb[attr]) { \
4653 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4654 return -EINVAL; \
4655 cfg->param = fn(tb[attr]); \
4656 mask |= (1 << (attr - 1)); \
4657 } \
4658} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004659
4660
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004661 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004662 return -EINVAL;
4663 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004664 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004665 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004666 return -EINVAL;
4667
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004668 /* This makes sure that there aren't more than 32 mesh config
4669 * parameters (otherwise our bitfield scheme would not work.) */
4670 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4671
4672 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004673 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004674 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4675 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004676 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004677 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4678 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004679 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004680 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4681 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004682 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004683 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4684 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004685 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004686 mask, NL80211_MESHCONF_MAX_RETRIES,
4687 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004688 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004689 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004690 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004691 mask, NL80211_MESHCONF_ELEMENT_TTL,
4692 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004693 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004694 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4695 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004696 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4697 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004698 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4699 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004700 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004701 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4702 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004703 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004704 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4705 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004706 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004707 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4708 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004709 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4710 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004711 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4712 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004713 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004714 1, 65535, mask,
4715 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004716 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004717 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004718 1, 65535, mask,
4719 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004720 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004721 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004722 dot11MeshHWMPnetDiameterTraversalTime,
4723 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004724 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4725 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004726 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4727 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4728 nla_get_u8);
4729 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4730 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004731 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004732 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004733 dot11MeshGateAnnouncementProtocol, 0, 1,
4734 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004735 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004736 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004737 mask, NL80211_MESHCONF_FORWARDING,
4738 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004739 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004740 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4741 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004742 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004743 mask, NL80211_MESHCONF_HT_OPMODE,
4744 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004745 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004746 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004747 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4748 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004749 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004750 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4751 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004752 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004753 dot11MeshHWMPconfirmationInterval,
4754 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004755 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4756 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004757 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4758 NL80211_MESH_POWER_ACTIVE,
4759 NL80211_MESH_POWER_MAX,
4760 mask, NL80211_MESHCONF_POWER_MODE,
4761 nla_get_u32);
4762 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4763 0, 65535, mask,
4764 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004765 if (mask_out)
4766 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004767
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004768 return 0;
4769
4770#undef FILL_IN_MESH_PARAM_IF_SET
4771}
4772
Javier Cardonac80d5452010-12-16 17:37:49 -08004773static int nl80211_parse_mesh_setup(struct genl_info *info,
4774 struct mesh_setup *setup)
4775{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004776 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004777 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4778
4779 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4780 return -EINVAL;
4781 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4782 info->attrs[NL80211_ATTR_MESH_SETUP],
4783 nl80211_mesh_setup_params_policy))
4784 return -EINVAL;
4785
Javier Cardonad299a1f2012-03-31 11:31:33 -07004786 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4787 setup->sync_method =
4788 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4789 IEEE80211_SYNC_METHOD_VENDOR :
4790 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4791
Javier Cardonac80d5452010-12-16 17:37:49 -08004792 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4793 setup->path_sel_proto =
4794 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4795 IEEE80211_PATH_PROTOCOL_VENDOR :
4796 IEEE80211_PATH_PROTOCOL_HWMP;
4797
4798 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4799 setup->path_metric =
4800 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4801 IEEE80211_PATH_METRIC_VENDOR :
4802 IEEE80211_PATH_METRIC_AIRTIME;
4803
Javier Cardona581a8b02011-04-07 15:08:27 -07004804
4805 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004806 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004807 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004808 if (!is_valid_ie_attr(ieattr))
4809 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004810 setup->ie = nla_data(ieattr);
4811 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004812 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004813 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4814 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4815 return -EINVAL;
4816 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004817 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4818 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004819 if (setup->is_secure)
4820 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08004821
4822 return 0;
4823}
4824
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004825static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004826 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004827{
4828 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4829 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004830 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004831 struct mesh_config cfg;
4832 u32 mask;
4833 int err;
4834
Johannes Berg29cbe682010-12-03 09:20:44 +01004835 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4836 return -EOPNOTSUPP;
4837
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004838 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004839 return -EOPNOTSUPP;
4840
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004841 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004842 if (err)
4843 return err;
4844
Johannes Berg29cbe682010-12-03 09:20:44 +01004845 wdev_lock(wdev);
4846 if (!wdev->mesh_id_len)
4847 err = -ENOLINK;
4848
4849 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004850 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004851
4852 wdev_unlock(wdev);
4853
4854 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004855}
4856
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004857static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4858{
Johannes Berg458f4f92012-12-06 15:47:38 +01004859 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004860 struct sk_buff *msg;
4861 void *hdr = NULL;
4862 struct nlattr *nl_reg_rules;
4863 unsigned int i;
4864 int err = -EINVAL;
4865
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004866 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004867
4868 if (!cfg80211_regdomain)
4869 goto out;
4870
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004871 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004872 if (!msg) {
4873 err = -ENOBUFS;
4874 goto out;
4875 }
4876
Eric W. Biederman15e47302012-09-07 20:12:54 +00004877 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004878 NL80211_CMD_GET_REG);
4879 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004880 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004881
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004882 if (reg_last_request_cell_base() &&
4883 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4884 NL80211_USER_REG_HINT_CELL_BASE))
4885 goto nla_put_failure;
4886
Johannes Berg458f4f92012-12-06 15:47:38 +01004887 rcu_read_lock();
4888 regdom = rcu_dereference(cfg80211_regdomain);
4889
4890 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4891 (regdom->dfs_region &&
4892 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4893 goto nla_put_failure_rcu;
4894
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004895 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4896 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004897 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004898
Johannes Berg458f4f92012-12-06 15:47:38 +01004899 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004900 struct nlattr *nl_reg_rule;
4901 const struct ieee80211_reg_rule *reg_rule;
4902 const struct ieee80211_freq_range *freq_range;
4903 const struct ieee80211_power_rule *power_rule;
4904
Johannes Berg458f4f92012-12-06 15:47:38 +01004905 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004906 freq_range = &reg_rule->freq_range;
4907 power_rule = &reg_rule->power_rule;
4908
4909 nl_reg_rule = nla_nest_start(msg, i);
4910 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004911 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004912
David S. Miller9360ffd2012-03-29 04:41:26 -04004913 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4914 reg_rule->flags) ||
4915 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4916 freq_range->start_freq_khz) ||
4917 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4918 freq_range->end_freq_khz) ||
4919 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4920 freq_range->max_bandwidth_khz) ||
4921 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4922 power_rule->max_antenna_gain) ||
4923 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4924 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004925 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004926
4927 nla_nest_end(msg, nl_reg_rule);
4928 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004929 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004930
4931 nla_nest_end(msg, nl_reg_rules);
4932
4933 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004934 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004935 goto out;
4936
Johannes Berg458f4f92012-12-06 15:47:38 +01004937nla_put_failure_rcu:
4938 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004939nla_put_failure:
4940 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004941put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004942 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004943 err = -EMSGSIZE;
4944out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004945 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004946 return err;
4947}
4948
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004949static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4950{
4951 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4952 struct nlattr *nl_reg_rule;
4953 char *alpha2 = NULL;
4954 int rem_reg_rules = 0, r = 0;
4955 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004956 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004957 struct ieee80211_regdomain *rd = NULL;
4958
4959 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4960 return -EINVAL;
4961
4962 if (!info->attrs[NL80211_ATTR_REG_RULES])
4963 return -EINVAL;
4964
4965 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4966
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004967 if (info->attrs[NL80211_ATTR_DFS_REGION])
4968 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4969
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004970 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004971 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004972 num_rules++;
4973 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004974 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004975 }
4976
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004977 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004978 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004979
4980 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004981 if (!rd)
4982 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004983
4984 rd->n_reg_rules = num_rules;
4985 rd->alpha2[0] = alpha2[0];
4986 rd->alpha2[1] = alpha2[1];
4987
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004988 /*
4989 * Disable DFS master mode if the DFS region was
4990 * not supported or known on this kernel.
4991 */
4992 if (reg_supported_dfs_region(dfs_region))
4993 rd->dfs_region = dfs_region;
4994
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004995 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004996 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004997 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004998 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4999 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005000 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5001 if (r)
5002 goto bad_reg;
5003
5004 rule_idx++;
5005
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005006 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5007 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005008 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005009 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005010 }
5011
Johannes Berg6913b492012-12-04 00:48:59 +01005012 mutex_lock(&cfg80211_mutex);
5013
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005014 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005015 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005016 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01005017 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005018
Johannes Bergd2372b32008-10-24 20:32:20 +02005019 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005020 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005021 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005022}
5023
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005024static int validate_scan_freqs(struct nlattr *freqs)
5025{
5026 struct nlattr *attr1, *attr2;
5027 int n_channels = 0, tmp1, tmp2;
5028
5029 nla_for_each_nested(attr1, freqs, tmp1) {
5030 n_channels++;
5031 /*
5032 * Some hardware has a limited channel list for
5033 * scanning, and it is pretty much nonsensical
5034 * to scan for a channel twice, so disallow that
5035 * and don't require drivers to check that the
5036 * channel list they get isn't longer than what
5037 * they can scan, as long as they can scan all
5038 * the channels they registered at once.
5039 */
5040 nla_for_each_nested(attr2, freqs, tmp2)
5041 if (attr1 != attr2 &&
5042 nla_get_u32(attr1) == nla_get_u32(attr2))
5043 return 0;
5044 }
5045
5046 return n_channels;
5047}
5048
Johannes Berg2a519312009-02-10 21:25:55 +01005049static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5050{
Johannes Berg4c476992010-10-04 21:36:35 +02005051 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005052 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005053 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005054 struct nlattr *attr;
5055 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005056 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005057 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005058
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005059 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5060 return -EINVAL;
5061
Johannes Berg79c97e92009-07-07 03:56:12 +02005062 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005063
Johannes Berg4c476992010-10-04 21:36:35 +02005064 if (!rdev->ops->scan)
5065 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005066
Johannes Bergf9f47522013-03-19 15:04:07 +01005067 mutex_lock(&rdev->sched_scan_mtx);
5068 if (rdev->scan_req) {
5069 err = -EBUSY;
5070 goto unlock;
5071 }
Johannes Berg2a519312009-02-10 21:25:55 +01005072
5073 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005074 n_channels = validate_scan_freqs(
5075 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005076 if (!n_channels) {
5077 err = -EINVAL;
5078 goto unlock;
5079 }
Johannes Berg2a519312009-02-10 21:25:55 +01005080 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005081 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005082 n_channels = 0;
5083
Johannes Berg2a519312009-02-10 21:25:55 +01005084 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5085 if (wiphy->bands[band])
5086 n_channels += wiphy->bands[band]->n_channels;
5087 }
5088
5089 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5090 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5091 n_ssids++;
5092
Johannes Bergf9f47522013-03-19 15:04:07 +01005093 if (n_ssids > wiphy->max_scan_ssids) {
5094 err = -EINVAL;
5095 goto unlock;
5096 }
Johannes Berg2a519312009-02-10 21:25:55 +01005097
Jouni Malinen70692ad2009-02-16 19:39:13 +02005098 if (info->attrs[NL80211_ATTR_IE])
5099 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5100 else
5101 ie_len = 0;
5102
Johannes Bergf9f47522013-03-19 15:04:07 +01005103 if (ie_len > wiphy->max_scan_ie_len) {
5104 err = -EINVAL;
5105 goto unlock;
5106 }
Johannes Berg18a83652009-03-31 12:12:05 +02005107
Johannes Berg2a519312009-02-10 21:25:55 +01005108 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005109 + sizeof(*request->ssids) * n_ssids
5110 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005111 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005112 if (!request) {
5113 err = -ENOMEM;
5114 goto unlock;
5115 }
Johannes Berg2a519312009-02-10 21:25:55 +01005116
Johannes Berg2a519312009-02-10 21:25:55 +01005117 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005118 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005119 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005120 if (ie_len) {
5121 if (request->ssids)
5122 request->ie = (void *)(request->ssids + n_ssids);
5123 else
5124 request->ie = (void *)(request->channels + n_channels);
5125 }
Johannes Berg2a519312009-02-10 21:25:55 +01005126
Johannes Berg584991d2009-11-02 13:32:03 +01005127 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005128 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5129 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005130 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005131 struct ieee80211_channel *chan;
5132
5133 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5134
5135 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005136 err = -EINVAL;
5137 goto out_free;
5138 }
Johannes Berg584991d2009-11-02 13:32:03 +01005139
5140 /* ignore disabled channels */
5141 if (chan->flags & IEEE80211_CHAN_DISABLED)
5142 continue;
5143
5144 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005145 i++;
5146 }
5147 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005148 enum ieee80211_band band;
5149
Johannes Berg2a519312009-02-10 21:25:55 +01005150 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005151 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5152 int j;
5153 if (!wiphy->bands[band])
5154 continue;
5155 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005156 struct ieee80211_channel *chan;
5157
5158 chan = &wiphy->bands[band]->channels[j];
5159
5160 if (chan->flags & IEEE80211_CHAN_DISABLED)
5161 continue;
5162
5163 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005164 i++;
5165 }
5166 }
5167 }
5168
Johannes Berg584991d2009-11-02 13:32:03 +01005169 if (!i) {
5170 err = -EINVAL;
5171 goto out_free;
5172 }
5173
5174 request->n_channels = i;
5175
Johannes Berg2a519312009-02-10 21:25:55 +01005176 i = 0;
5177 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5178 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005179 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005180 err = -EINVAL;
5181 goto out_free;
5182 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005183 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005184 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005185 i++;
5186 }
5187 }
5188
Jouni Malinen70692ad2009-02-16 19:39:13 +02005189 if (info->attrs[NL80211_ATTR_IE]) {
5190 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005191 memcpy((void *)request->ie,
5192 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005193 request->ie_len);
5194 }
5195
Johannes Berg34850ab2011-07-18 18:08:35 +02005196 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005197 if (wiphy->bands[i])
5198 request->rates[i] =
5199 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005200
5201 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5202 nla_for_each_nested(attr,
5203 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5204 tmp) {
5205 enum ieee80211_band band = nla_type(attr);
5206
Dan Carpenter84404622011-07-29 11:52:18 +03005207 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005208 err = -EINVAL;
5209 goto out_free;
5210 }
5211 err = ieee80211_get_ratemask(wiphy->bands[band],
5212 nla_data(attr),
5213 nla_len(attr),
5214 &request->rates[band]);
5215 if (err)
5216 goto out_free;
5217 }
5218 }
5219
Sam Leffler46856bb2012-10-11 21:03:32 -07005220 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005221 request->flags = nla_get_u32(
5222 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005223 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5224 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5225 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5226 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005227 err = -EOPNOTSUPP;
5228 goto out_free;
5229 }
5230 }
Sam Lefflered4737712012-10-11 21:03:31 -07005231
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305232 request->no_cck =
5233 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5234
Johannes Bergfd014282012-06-18 19:17:03 +02005235 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005236 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005237 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005238
Johannes Berg79c97e92009-07-07 03:56:12 +02005239 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005240 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005241
Johannes Berg463d0182009-07-14 00:33:35 +02005242 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005243 nl80211_send_scan_start(rdev, wdev);
5244 if (wdev->netdev)
5245 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005246 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005247 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005248 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005249 kfree(request);
5250 }
Johannes Berg3b858752009-03-12 09:55:09 +01005251
Johannes Bergf9f47522013-03-19 15:04:07 +01005252 unlock:
5253 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01005254 return err;
5255}
5256
Luciano Coelho807f8a82011-05-11 17:09:35 +03005257static int nl80211_start_sched_scan(struct sk_buff *skb,
5258 struct genl_info *info)
5259{
5260 struct cfg80211_sched_scan_request *request;
5261 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5262 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005263 struct nlattr *attr;
5264 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005265 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005266 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005267 enum ieee80211_band band;
5268 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005269 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005270
5271 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5272 !rdev->ops->sched_scan_start)
5273 return -EOPNOTSUPP;
5274
5275 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5276 return -EINVAL;
5277
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005278 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5279 return -EINVAL;
5280
5281 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5282 if (interval == 0)
5283 return -EINVAL;
5284
Luciano Coelho807f8a82011-05-11 17:09:35 +03005285 wiphy = &rdev->wiphy;
5286
5287 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5288 n_channels = validate_scan_freqs(
5289 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5290 if (!n_channels)
5291 return -EINVAL;
5292 } else {
5293 n_channels = 0;
5294
5295 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5296 if (wiphy->bands[band])
5297 n_channels += wiphy->bands[band]->n_channels;
5298 }
5299
5300 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5301 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5302 tmp)
5303 n_ssids++;
5304
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005305 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005306 return -EINVAL;
5307
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005308 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5309 nla_for_each_nested(attr,
5310 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5311 tmp)
5312 n_match_sets++;
5313
5314 if (n_match_sets > wiphy->max_match_sets)
5315 return -EINVAL;
5316
Luciano Coelho807f8a82011-05-11 17:09:35 +03005317 if (info->attrs[NL80211_ATTR_IE])
5318 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5319 else
5320 ie_len = 0;
5321
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005322 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005323 return -EINVAL;
5324
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005325 mutex_lock(&rdev->sched_scan_mtx);
5326
5327 if (rdev->sched_scan_req) {
5328 err = -EINPROGRESS;
5329 goto out;
5330 }
5331
Luciano Coelho807f8a82011-05-11 17:09:35 +03005332 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005333 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005334 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005335 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005336 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005337 if (!request) {
5338 err = -ENOMEM;
5339 goto out;
5340 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005341
5342 if (n_ssids)
5343 request->ssids = (void *)&request->channels[n_channels];
5344 request->n_ssids = n_ssids;
5345 if (ie_len) {
5346 if (request->ssids)
5347 request->ie = (void *)(request->ssids + n_ssids);
5348 else
5349 request->ie = (void *)(request->channels + n_channels);
5350 }
5351
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005352 if (n_match_sets) {
5353 if (request->ie)
5354 request->match_sets = (void *)(request->ie + ie_len);
5355 else if (request->ssids)
5356 request->match_sets =
5357 (void *)(request->ssids + n_ssids);
5358 else
5359 request->match_sets =
5360 (void *)(request->channels + n_channels);
5361 }
5362 request->n_match_sets = n_match_sets;
5363
Luciano Coelho807f8a82011-05-11 17:09:35 +03005364 i = 0;
5365 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5366 /* user specified, bail out if channel not found */
5367 nla_for_each_nested(attr,
5368 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5369 tmp) {
5370 struct ieee80211_channel *chan;
5371
5372 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5373
5374 if (!chan) {
5375 err = -EINVAL;
5376 goto out_free;
5377 }
5378
5379 /* ignore disabled channels */
5380 if (chan->flags & IEEE80211_CHAN_DISABLED)
5381 continue;
5382
5383 request->channels[i] = chan;
5384 i++;
5385 }
5386 } else {
5387 /* all channels */
5388 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5389 int j;
5390 if (!wiphy->bands[band])
5391 continue;
5392 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5393 struct ieee80211_channel *chan;
5394
5395 chan = &wiphy->bands[band]->channels[j];
5396
5397 if (chan->flags & IEEE80211_CHAN_DISABLED)
5398 continue;
5399
5400 request->channels[i] = chan;
5401 i++;
5402 }
5403 }
5404 }
5405
5406 if (!i) {
5407 err = -EINVAL;
5408 goto out_free;
5409 }
5410
5411 request->n_channels = i;
5412
5413 i = 0;
5414 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5415 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5416 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005417 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005418 err = -EINVAL;
5419 goto out_free;
5420 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005421 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005422 memcpy(request->ssids[i].ssid, nla_data(attr),
5423 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005424 i++;
5425 }
5426 }
5427
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005428 i = 0;
5429 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5430 nla_for_each_nested(attr,
5431 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5432 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005433 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005434
5435 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5436 nla_data(attr), nla_len(attr),
5437 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005438 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005439 if (ssid) {
5440 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5441 err = -EINVAL;
5442 goto out_free;
5443 }
5444 memcpy(request->match_sets[i].ssid.ssid,
5445 nla_data(ssid), nla_len(ssid));
5446 request->match_sets[i].ssid.ssid_len =
5447 nla_len(ssid);
5448 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005449 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5450 if (rssi)
5451 request->rssi_thold = nla_get_u32(rssi);
5452 else
5453 request->rssi_thold =
5454 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005455 i++;
5456 }
5457 }
5458
Luciano Coelho807f8a82011-05-11 17:09:35 +03005459 if (info->attrs[NL80211_ATTR_IE]) {
5460 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5461 memcpy((void *)request->ie,
5462 nla_data(info->attrs[NL80211_ATTR_IE]),
5463 request->ie_len);
5464 }
5465
Sam Leffler46856bb2012-10-11 21:03:32 -07005466 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005467 request->flags = nla_get_u32(
5468 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005469 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5470 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5471 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5472 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005473 err = -EOPNOTSUPP;
5474 goto out_free;
5475 }
5476 }
Sam Lefflered4737712012-10-11 21:03:31 -07005477
Luciano Coelho807f8a82011-05-11 17:09:35 +03005478 request->dev = dev;
5479 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005480 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005481 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005482
Hila Gonene35e4d22012-06-27 17:19:42 +03005483 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005484 if (!err) {
5485 rdev->sched_scan_req = request;
5486 nl80211_send_sched_scan(rdev, dev,
5487 NL80211_CMD_START_SCHED_SCAN);
5488 goto out;
5489 }
5490
5491out_free:
5492 kfree(request);
5493out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005494 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005495 return err;
5496}
5497
5498static int nl80211_stop_sched_scan(struct sk_buff *skb,
5499 struct genl_info *info)
5500{
5501 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005502 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005503
5504 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5505 !rdev->ops->sched_scan_stop)
5506 return -EOPNOTSUPP;
5507
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005508 mutex_lock(&rdev->sched_scan_mtx);
5509 err = __cfg80211_stop_sched_scan(rdev, false);
5510 mutex_unlock(&rdev->sched_scan_mtx);
5511
5512 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005513}
5514
Simon Wunderlich04f39042013-02-08 18:16:19 +01005515static int nl80211_start_radar_detection(struct sk_buff *skb,
5516 struct genl_info *info)
5517{
5518 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5519 struct net_device *dev = info->user_ptr[1];
5520 struct wireless_dev *wdev = dev->ieee80211_ptr;
5521 struct cfg80211_chan_def chandef;
5522 int err;
5523
5524 err = nl80211_parse_chandef(rdev, info, &chandef);
5525 if (err)
5526 return err;
5527
5528 if (wdev->cac_started)
5529 return -EBUSY;
5530
5531 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5532 if (err < 0)
5533 return err;
5534
5535 if (err == 0)
5536 return -EINVAL;
5537
5538 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5539 return -EINVAL;
5540
5541 if (!rdev->ops->start_radar_detection)
5542 return -EOPNOTSUPP;
5543
5544 mutex_lock(&rdev->devlist_mtx);
5545 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5546 chandef.chan, CHAN_MODE_SHARED,
5547 BIT(chandef.width));
5548 if (err)
5549 goto err_locked;
5550
5551 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5552 if (!err) {
5553 wdev->channel = chandef.chan;
5554 wdev->cac_started = true;
5555 wdev->cac_start_time = jiffies;
5556 }
5557err_locked:
5558 mutex_unlock(&rdev->devlist_mtx);
5559
5560 return err;
5561}
5562
Johannes Berg9720bb32011-06-21 09:45:33 +02005563static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5564 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005565 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005566 struct wireless_dev *wdev,
5567 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005568{
Johannes Berg48ab9052009-07-10 18:42:31 +02005569 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005570 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005571 void *hdr;
5572 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005573 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005574
5575 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005576
Eric W. Biederman15e47302012-09-07 20:12:54 +00005577 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005578 NL80211_CMD_NEW_SCAN_RESULTS);
5579 if (!hdr)
5580 return -1;
5581
Johannes Berg9720bb32011-06-21 09:45:33 +02005582 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5583
Johannes Berg97990a02013-04-19 01:02:55 +02005584 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5585 goto nla_put_failure;
5586 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005587 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5588 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005589 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5590 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005591
5592 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5593 if (!bss)
5594 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005595 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005596 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005597 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005598
5599 rcu_read_lock();
5600 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005601 if (ies) {
5602 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5603 goto fail_unlock_rcu;
5604 tsf = true;
5605 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5606 ies->len, ies->data))
5607 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005608 }
5609 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005610 if (ies) {
5611 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5612 goto fail_unlock_rcu;
5613 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5614 ies->len, ies->data))
5615 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005616 }
5617 rcu_read_unlock();
5618
David S. Miller9360ffd2012-03-29 04:41:26 -04005619 if (res->beacon_interval &&
5620 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5621 goto nla_put_failure;
5622 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5623 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5624 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5625 jiffies_to_msecs(jiffies - intbss->ts)))
5626 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005627
Johannes Berg77965c92009-02-18 18:45:06 +01005628 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005629 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005630 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5631 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005632 break;
5633 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005634 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5635 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005636 break;
5637 default:
5638 break;
5639 }
5640
Johannes Berg48ab9052009-07-10 18:42:31 +02005641 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005642 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005643 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005644 if (intbss == wdev->current_bss &&
5645 nla_put_u32(msg, NL80211_BSS_STATUS,
5646 NL80211_BSS_STATUS_ASSOCIATED))
5647 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005648 break;
5649 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005650 if (intbss == wdev->current_bss &&
5651 nla_put_u32(msg, NL80211_BSS_STATUS,
5652 NL80211_BSS_STATUS_IBSS_JOINED))
5653 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005654 break;
5655 default:
5656 break;
5657 }
5658
Johannes Berg2a519312009-02-10 21:25:55 +01005659 nla_nest_end(msg, bss);
5660
5661 return genlmsg_end(msg, hdr);
5662
Johannes Berg8cef2c92013-02-05 16:54:31 +01005663 fail_unlock_rcu:
5664 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005665 nla_put_failure:
5666 genlmsg_cancel(msg, hdr);
5667 return -EMSGSIZE;
5668}
5669
Johannes Berg97990a02013-04-19 01:02:55 +02005670static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005671{
Johannes Berg48ab9052009-07-10 18:42:31 +02005672 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005673 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005674 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005675 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005676 int err;
5677
Johannes Berg97990a02013-04-19 01:02:55 +02005678 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005679 if (err)
5680 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005681
Johannes Berg48ab9052009-07-10 18:42:31 +02005682 wdev_lock(wdev);
5683 spin_lock_bh(&rdev->bss_lock);
5684 cfg80211_bss_expire(rdev);
5685
Johannes Berg9720bb32011-06-21 09:45:33 +02005686 cb->seq = rdev->bss_generation;
5687
Johannes Berg48ab9052009-07-10 18:42:31 +02005688 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005689 if (++idx <= start)
5690 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005691 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005692 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005693 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005694 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005695 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005696 }
5697 }
5698
Johannes Berg48ab9052009-07-10 18:42:31 +02005699 spin_unlock_bh(&rdev->bss_lock);
5700 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005701
Johannes Berg97990a02013-04-19 01:02:55 +02005702 cb->args[2] = idx;
5703 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005704
Johannes Berg67748892010-10-04 21:14:06 +02005705 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005706}
5707
Eric W. Biederman15e47302012-09-07 20:12:54 +00005708static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005709 int flags, struct net_device *dev,
5710 struct survey_info *survey)
5711{
5712 void *hdr;
5713 struct nlattr *infoattr;
5714
Eric W. Biederman15e47302012-09-07 20:12:54 +00005715 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005716 NL80211_CMD_NEW_SURVEY_RESULTS);
5717 if (!hdr)
5718 return -ENOMEM;
5719
David S. Miller9360ffd2012-03-29 04:41:26 -04005720 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5721 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005722
5723 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5724 if (!infoattr)
5725 goto nla_put_failure;
5726
David S. Miller9360ffd2012-03-29 04:41:26 -04005727 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5728 survey->channel->center_freq))
5729 goto nla_put_failure;
5730
5731 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5732 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5733 goto nla_put_failure;
5734 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5735 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5736 goto nla_put_failure;
5737 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5738 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5739 survey->channel_time))
5740 goto nla_put_failure;
5741 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5742 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5743 survey->channel_time_busy))
5744 goto nla_put_failure;
5745 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5746 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5747 survey->channel_time_ext_busy))
5748 goto nla_put_failure;
5749 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5750 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5751 survey->channel_time_rx))
5752 goto nla_put_failure;
5753 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5754 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5755 survey->channel_time_tx))
5756 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005757
5758 nla_nest_end(msg, infoattr);
5759
5760 return genlmsg_end(msg, hdr);
5761
5762 nla_put_failure:
5763 genlmsg_cancel(msg, hdr);
5764 return -EMSGSIZE;
5765}
5766
5767static int nl80211_dump_survey(struct sk_buff *skb,
5768 struct netlink_callback *cb)
5769{
5770 struct survey_info survey;
5771 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02005772 struct wireless_dev *wdev;
5773 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01005774 int res;
5775
Johannes Berg97990a02013-04-19 01:02:55 +02005776 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005777 if (res)
5778 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005779
Johannes Berg97990a02013-04-19 01:02:55 +02005780 if (!wdev->netdev) {
5781 res = -EINVAL;
5782 goto out_err;
5783 }
5784
Holger Schurig61fa7132009-11-11 12:25:40 +01005785 if (!dev->ops->dump_survey) {
5786 res = -EOPNOTSUPP;
5787 goto out_err;
5788 }
5789
5790 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005791 struct ieee80211_channel *chan;
5792
Johannes Berg97990a02013-04-19 01:02:55 +02005793 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005794 if (res == -ENOENT)
5795 break;
5796 if (res)
5797 goto out_err;
5798
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005799 /* Survey without a channel doesn't make sense */
5800 if (!survey.channel) {
5801 res = -EINVAL;
5802 goto out;
5803 }
5804
5805 chan = ieee80211_get_channel(&dev->wiphy,
5806 survey.channel->center_freq);
5807 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5808 survey_idx++;
5809 continue;
5810 }
5811
Holger Schurig61fa7132009-11-11 12:25:40 +01005812 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005813 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005814 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02005815 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01005816 goto out;
5817 survey_idx++;
5818 }
5819
5820 out:
Johannes Berg97990a02013-04-19 01:02:55 +02005821 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01005822 res = skb->len;
5823 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02005824 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005825 return res;
5826}
5827
Samuel Ortizb23aa672009-07-01 21:26:54 +02005828static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5829{
5830 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5831 NL80211_WPA_VERSION_2));
5832}
5833
Jouni Malinen636a5d32009-03-19 13:39:22 +02005834static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5835{
Johannes Berg4c476992010-10-04 21:36:35 +02005836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5837 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005838 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005839 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5840 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005841 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005842 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005843 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005844
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005845 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5846 return -EINVAL;
5847
5848 if (!info->attrs[NL80211_ATTR_MAC])
5849 return -EINVAL;
5850
Jouni Malinen17780922009-03-27 20:52:47 +02005851 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5852 return -EINVAL;
5853
Johannes Berg19957bb2009-07-02 17:20:43 +02005854 if (!info->attrs[NL80211_ATTR_SSID])
5855 return -EINVAL;
5856
5857 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5858 return -EINVAL;
5859
Johannes Bergfffd0932009-07-08 14:22:54 +02005860 err = nl80211_parse_key(info, &key);
5861 if (err)
5862 return err;
5863
5864 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005865 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5866 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005867 if (!key.p.key || !key.p.key_len)
5868 return -EINVAL;
5869 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5870 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5871 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5872 key.p.key_len != WLAN_KEY_LEN_WEP104))
5873 return -EINVAL;
5874 if (key.idx > 4)
5875 return -EINVAL;
5876 } else {
5877 key.p.key_len = 0;
5878 key.p.key = NULL;
5879 }
5880
Johannes Bergafea0b72010-08-10 09:46:42 +02005881 if (key.idx >= 0) {
5882 int i;
5883 bool ok = false;
5884 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5885 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5886 ok = true;
5887 break;
5888 }
5889 }
Johannes Berg4c476992010-10-04 21:36:35 +02005890 if (!ok)
5891 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005892 }
5893
Johannes Berg4c476992010-10-04 21:36:35 +02005894 if (!rdev->ops->auth)
5895 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005896
Johannes Berg074ac8d2010-09-16 14:58:22 +02005897 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005898 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5899 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005900
Johannes Berg19957bb2009-07-02 17:20:43 +02005901 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005902 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005903 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005904 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5905 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005906
Johannes Berg19957bb2009-07-02 17:20:43 +02005907 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5908 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5909
5910 if (info->attrs[NL80211_ATTR_IE]) {
5911 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5912 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5913 }
5914
5915 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005916 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005917 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005918
Jouni Malinene39e5b52012-09-30 19:29:39 +03005919 if (auth_type == NL80211_AUTHTYPE_SAE &&
5920 !info->attrs[NL80211_ATTR_SAE_DATA])
5921 return -EINVAL;
5922
5923 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5924 if (auth_type != NL80211_AUTHTYPE_SAE)
5925 return -EINVAL;
5926 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5927 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5928 /* need to include at least Auth Transaction and Status Code */
5929 if (sae_data_len < 4)
5930 return -EINVAL;
5931 }
5932
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005933 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5934
Johannes Berg95de8172012-01-20 13:55:25 +01005935 /*
5936 * Since we no longer track auth state, ignore
5937 * requests to only change local state.
5938 */
5939 if (local_state_change)
5940 return 0;
5941
Johannes Berg4c476992010-10-04 21:36:35 +02005942 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5943 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005944 key.p.key, key.p.key_len, key.idx,
5945 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005946}
5947
Johannes Bergc0692b82010-08-27 14:26:53 +03005948static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5949 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005950 struct cfg80211_crypto_settings *settings,
5951 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005952{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005953 memset(settings, 0, sizeof(*settings));
5954
Samuel Ortizb23aa672009-07-01 21:26:54 +02005955 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5956
Johannes Bergc0692b82010-08-27 14:26:53 +03005957 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5958 u16 proto;
5959 proto = nla_get_u16(
5960 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5961 settings->control_port_ethertype = cpu_to_be16(proto);
5962 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5963 proto != ETH_P_PAE)
5964 return -EINVAL;
5965 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5966 settings->control_port_no_encrypt = true;
5967 } else
5968 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5969
Samuel Ortizb23aa672009-07-01 21:26:54 +02005970 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5971 void *data;
5972 int len, i;
5973
5974 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5975 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5976 settings->n_ciphers_pairwise = len / sizeof(u32);
5977
5978 if (len % sizeof(u32))
5979 return -EINVAL;
5980
Johannes Berg3dc27d22009-07-02 21:36:37 +02005981 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005982 return -EINVAL;
5983
5984 memcpy(settings->ciphers_pairwise, data, len);
5985
5986 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005987 if (!cfg80211_supported_cipher_suite(
5988 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005989 settings->ciphers_pairwise[i]))
5990 return -EINVAL;
5991 }
5992
5993 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5994 settings->cipher_group =
5995 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005996 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5997 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005998 return -EINVAL;
5999 }
6000
6001 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6002 settings->wpa_versions =
6003 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6004 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6005 return -EINVAL;
6006 }
6007
6008 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6009 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006010 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006011
6012 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6013 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6014 settings->n_akm_suites = len / sizeof(u32);
6015
6016 if (len % sizeof(u32))
6017 return -EINVAL;
6018
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006019 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6020 return -EINVAL;
6021
Samuel Ortizb23aa672009-07-01 21:26:54 +02006022 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006023 }
6024
6025 return 0;
6026}
6027
Jouni Malinen636a5d32009-03-19 13:39:22 +02006028static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6029{
Johannes Berg4c476992010-10-04 21:36:35 +02006030 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6031 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006032 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006033 struct cfg80211_assoc_request req = {};
6034 const u8 *bssid, *ssid;
6035 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006036
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006037 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6038 return -EINVAL;
6039
6040 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006041 !info->attrs[NL80211_ATTR_SSID] ||
6042 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006043 return -EINVAL;
6044
Johannes Berg4c476992010-10-04 21:36:35 +02006045 if (!rdev->ops->assoc)
6046 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006047
Johannes Berg074ac8d2010-09-16 14:58:22 +02006048 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006049 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6050 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006051
Johannes Berg19957bb2009-07-02 17:20:43 +02006052 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006053
Johannes Berg19957bb2009-07-02 17:20:43 +02006054 chan = ieee80211_get_channel(&rdev->wiphy,
6055 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006056 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6057 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006058
Johannes Berg19957bb2009-07-02 17:20:43 +02006059 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6060 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006061
6062 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006063 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6064 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006065 }
6066
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006067 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006068 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006069 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006070 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006071 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006072 else if (mfp != NL80211_MFP_NO)
6073 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006074 }
6075
Johannes Berg3e5d7642009-07-07 14:37:26 +02006076 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006077 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006078
Ben Greear7e7c8922011-11-18 11:31:59 -08006079 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006080 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006081
6082 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006083 memcpy(&req.ht_capa_mask,
6084 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6085 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006086
6087 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006088 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006089 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006090 memcpy(&req.ht_capa,
6091 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6092 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006093 }
6094
Johannes Bergee2aca32013-02-21 17:36:01 +01006095 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006096 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006097
6098 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006099 memcpy(&req.vht_capa_mask,
6100 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6101 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006102
6103 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006104 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006105 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006106 memcpy(&req.vht_capa,
6107 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6108 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006109 }
6110
Johannes Bergf62fab72013-02-21 20:09:09 +01006111 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006112 if (!err)
Johannes Bergf62fab72013-02-21 20:09:09 +01006113 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6114 ssid, ssid_len, &req);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006115
Jouni Malinen636a5d32009-03-19 13:39:22 +02006116 return err;
6117}
6118
6119static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6120{
Johannes Berg4c476992010-10-04 21:36:35 +02006121 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6122 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006123 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006124 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006125 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006126 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006127
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006128 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6129 return -EINVAL;
6130
6131 if (!info->attrs[NL80211_ATTR_MAC])
6132 return -EINVAL;
6133
6134 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6135 return -EINVAL;
6136
Johannes Berg4c476992010-10-04 21:36:35 +02006137 if (!rdev->ops->deauth)
6138 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006139
Johannes Berg074ac8d2010-09-16 14:58:22 +02006140 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006141 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6142 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006143
Johannes Berg19957bb2009-07-02 17:20:43 +02006144 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006145
Johannes Berg19957bb2009-07-02 17:20:43 +02006146 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6147 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006148 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006149 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006150 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006151
6152 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006153 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6154 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006155 }
6156
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006157 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6158
Johannes Berg4c476992010-10-04 21:36:35 +02006159 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6160 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006161}
6162
6163static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6164{
Johannes Berg4c476992010-10-04 21:36:35 +02006165 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6166 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006167 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006168 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006169 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006170 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006171
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006172 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6173 return -EINVAL;
6174
6175 if (!info->attrs[NL80211_ATTR_MAC])
6176 return -EINVAL;
6177
6178 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6179 return -EINVAL;
6180
Johannes Berg4c476992010-10-04 21:36:35 +02006181 if (!rdev->ops->disassoc)
6182 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006183
Johannes Berg074ac8d2010-09-16 14:58:22 +02006184 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006185 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6186 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006187
Johannes Berg19957bb2009-07-02 17:20:43 +02006188 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006189
Johannes Berg19957bb2009-07-02 17:20:43 +02006190 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6191 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006192 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006193 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006194 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006195
6196 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006197 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6198 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006199 }
6200
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006201 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6202
Johannes Berg4c476992010-10-04 21:36:35 +02006203 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6204 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006205}
6206
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006207static bool
6208nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6209 int mcast_rate[IEEE80211_NUM_BANDS],
6210 int rateval)
6211{
6212 struct wiphy *wiphy = &rdev->wiphy;
6213 bool found = false;
6214 int band, i;
6215
6216 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6217 struct ieee80211_supported_band *sband;
6218
6219 sband = wiphy->bands[band];
6220 if (!sband)
6221 continue;
6222
6223 for (i = 0; i < sband->n_bitrates; i++) {
6224 if (sband->bitrates[i].bitrate == rateval) {
6225 mcast_rate[band] = i + 1;
6226 found = true;
6227 break;
6228 }
6229 }
6230 }
6231
6232 return found;
6233}
6234
Johannes Berg04a773a2009-04-19 21:24:32 +02006235static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6236{
Johannes Berg4c476992010-10-04 21:36:35 +02006237 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6238 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006239 struct cfg80211_ibss_params ibss;
6240 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006241 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006242 int err;
6243
Johannes Berg8e30bc52009-04-22 17:45:38 +02006244 memset(&ibss, 0, sizeof(ibss));
6245
Johannes Berg04a773a2009-04-19 21:24:32 +02006246 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6247 return -EINVAL;
6248
Johannes Berg683b6d32012-11-08 21:25:48 +01006249 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006250 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6251 return -EINVAL;
6252
Johannes Berg8e30bc52009-04-22 17:45:38 +02006253 ibss.beacon_interval = 100;
6254
6255 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6256 ibss.beacon_interval =
6257 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6258 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6259 return -EINVAL;
6260 }
6261
Johannes Berg4c476992010-10-04 21:36:35 +02006262 if (!rdev->ops->join_ibss)
6263 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006264
Johannes Berg4c476992010-10-04 21:36:35 +02006265 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6266 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006267
Johannes Berg79c97e92009-07-07 03:56:12 +02006268 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006269
Johannes Berg39193492011-09-16 13:45:25 +02006270 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006271 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006272
6273 if (!is_valid_ether_addr(ibss.bssid))
6274 return -EINVAL;
6275 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006276 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6277 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6278
6279 if (info->attrs[NL80211_ATTR_IE]) {
6280 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6281 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6282 }
6283
Johannes Berg683b6d32012-11-08 21:25:48 +01006284 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6285 if (err)
6286 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006287
Johannes Berg683b6d32012-11-08 21:25:48 +01006288 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006289 return -EINVAL;
6290
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006291 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6292 return -EINVAL;
6293 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6294 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01006295 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006296
Johannes Berg04a773a2009-04-19 21:24:32 +02006297 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006298 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006299
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006300 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6301 u8 *rates =
6302 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6303 int n_rates =
6304 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6305 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006306 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006307
Johannes Berg34850ab2011-07-18 18:08:35 +02006308 err = ieee80211_get_ratemask(sband, rates, n_rates,
6309 &ibss.basic_rates);
6310 if (err)
6311 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006312 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006313
6314 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6315 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6316 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6317 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006318
Johannes Berg4c476992010-10-04 21:36:35 +02006319 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306320 bool no_ht = false;
6321
Johannes Berg4c476992010-10-04 21:36:35 +02006322 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306323 info->attrs[NL80211_ATTR_KEYS],
6324 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006325 if (IS_ERR(connkeys))
6326 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306327
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006328 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6329 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306330 kfree(connkeys);
6331 return -EINVAL;
6332 }
Johannes Berg4c476992010-10-04 21:36:35 +02006333 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006334
Antonio Quartulli267335d2012-01-31 20:25:47 +01006335 ibss.control_port =
6336 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6337
Johannes Berg4c476992010-10-04 21:36:35 +02006338 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006339 if (err)
6340 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006341 return err;
6342}
6343
6344static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6345{
Johannes Berg4c476992010-10-04 21:36:35 +02006346 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6347 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006348
Johannes Berg4c476992010-10-04 21:36:35 +02006349 if (!rdev->ops->leave_ibss)
6350 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006351
Johannes Berg4c476992010-10-04 21:36:35 +02006352 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6353 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006354
Johannes Berg4c476992010-10-04 21:36:35 +02006355 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006356}
6357
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006358static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6359{
6360 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6361 struct net_device *dev = info->user_ptr[1];
6362 int mcast_rate[IEEE80211_NUM_BANDS];
6363 u32 nla_rate;
6364 int err;
6365
6366 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6367 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6368 return -EOPNOTSUPP;
6369
6370 if (!rdev->ops->set_mcast_rate)
6371 return -EOPNOTSUPP;
6372
6373 memset(mcast_rate, 0, sizeof(mcast_rate));
6374
6375 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6376 return -EINVAL;
6377
6378 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6379 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6380 return -EINVAL;
6381
6382 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6383
6384 return err;
6385}
6386
6387
Johannes Bergaff89a92009-07-01 21:26:51 +02006388#ifdef CONFIG_NL80211_TESTMODE
6389static struct genl_multicast_group nl80211_testmode_mcgrp = {
6390 .name = "testmode",
6391};
6392
6393static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6394{
Johannes Berg4c476992010-10-04 21:36:35 +02006395 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006396 int err;
6397
6398 if (!info->attrs[NL80211_ATTR_TESTDATA])
6399 return -EINVAL;
6400
Johannes Bergaff89a92009-07-01 21:26:51 +02006401 err = -EOPNOTSUPP;
6402 if (rdev->ops->testmode_cmd) {
6403 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006404 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006405 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6406 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6407 rdev->testmode_info = NULL;
6408 }
6409
Johannes Bergaff89a92009-07-01 21:26:51 +02006410 return err;
6411}
6412
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006413static int nl80211_testmode_dump(struct sk_buff *skb,
6414 struct netlink_callback *cb)
6415{
Johannes Berg00918d32011-12-13 17:22:05 +01006416 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006417 int err;
6418 long phy_idx;
6419 void *data = NULL;
6420 int data_len = 0;
6421
6422 if (cb->args[0]) {
6423 /*
6424 * 0 is a valid index, but not valid for args[0],
6425 * so we need to offset by 1.
6426 */
6427 phy_idx = cb->args[0] - 1;
6428 } else {
6429 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6430 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6431 nl80211_policy);
6432 if (err)
6433 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006434
Johannes Berg2bd7e352012-06-15 14:23:16 +02006435 mutex_lock(&cfg80211_mutex);
6436 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6437 nl80211_fam.attrbuf);
6438 if (IS_ERR(rdev)) {
6439 mutex_unlock(&cfg80211_mutex);
6440 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006441 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006442 phy_idx = rdev->wiphy_idx;
6443 rdev = NULL;
6444 mutex_unlock(&cfg80211_mutex);
6445
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006446 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6447 cb->args[1] =
6448 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6449 }
6450
6451 if (cb->args[1]) {
6452 data = nla_data((void *)cb->args[1]);
6453 data_len = nla_len((void *)cb->args[1]);
6454 }
6455
6456 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006457 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6458 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006459 mutex_unlock(&cfg80211_mutex);
6460 return -ENOENT;
6461 }
Johannes Berg00918d32011-12-13 17:22:05 +01006462 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006463 mutex_unlock(&cfg80211_mutex);
6464
Johannes Berg00918d32011-12-13 17:22:05 +01006465 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006466 err = -EOPNOTSUPP;
6467 goto out_err;
6468 }
6469
6470 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006471 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006472 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6473 NL80211_CMD_TESTMODE);
6474 struct nlattr *tmdata;
6475
David S. Miller9360ffd2012-03-29 04:41:26 -04006476 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006477 genlmsg_cancel(skb, hdr);
6478 break;
6479 }
6480
6481 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6482 if (!tmdata) {
6483 genlmsg_cancel(skb, hdr);
6484 break;
6485 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006486 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006487 nla_nest_end(skb, tmdata);
6488
6489 if (err == -ENOBUFS || err == -ENOENT) {
6490 genlmsg_cancel(skb, hdr);
6491 break;
6492 } else if (err) {
6493 genlmsg_cancel(skb, hdr);
6494 goto out_err;
6495 }
6496
6497 genlmsg_end(skb, hdr);
6498 }
6499
6500 err = skb->len;
6501 /* see above */
6502 cb->args[0] = phy_idx + 1;
6503 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006504 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006505 return err;
6506}
6507
Johannes Bergaff89a92009-07-01 21:26:51 +02006508static struct sk_buff *
6509__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006510 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006511{
6512 struct sk_buff *skb;
6513 void *hdr;
6514 struct nlattr *data;
6515
6516 skb = nlmsg_new(approxlen + 100, gfp);
6517 if (!skb)
6518 return NULL;
6519
Eric W. Biederman15e47302012-09-07 20:12:54 +00006520 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006521 if (!hdr) {
6522 kfree_skb(skb);
6523 return NULL;
6524 }
6525
David S. Miller9360ffd2012-03-29 04:41:26 -04006526 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6527 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006528 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6529
6530 ((void **)skb->cb)[0] = rdev;
6531 ((void **)skb->cb)[1] = hdr;
6532 ((void **)skb->cb)[2] = data;
6533
6534 return skb;
6535
6536 nla_put_failure:
6537 kfree_skb(skb);
6538 return NULL;
6539}
6540
6541struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6542 int approxlen)
6543{
6544 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6545
6546 if (WARN_ON(!rdev->testmode_info))
6547 return NULL;
6548
6549 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006550 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006551 rdev->testmode_info->snd_seq,
6552 GFP_KERNEL);
6553}
6554EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6555
6556int cfg80211_testmode_reply(struct sk_buff *skb)
6557{
6558 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6559 void *hdr = ((void **)skb->cb)[1];
6560 struct nlattr *data = ((void **)skb->cb)[2];
6561
6562 if (WARN_ON(!rdev->testmode_info)) {
6563 kfree_skb(skb);
6564 return -EINVAL;
6565 }
6566
6567 nla_nest_end(skb, data);
6568 genlmsg_end(skb, hdr);
6569 return genlmsg_reply(skb, rdev->testmode_info);
6570}
6571EXPORT_SYMBOL(cfg80211_testmode_reply);
6572
6573struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6574 int approxlen, gfp_t gfp)
6575{
6576 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6577
6578 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6579}
6580EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6581
6582void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6583{
6584 void *hdr = ((void **)skb->cb)[1];
6585 struct nlattr *data = ((void **)skb->cb)[2];
6586
6587 nla_nest_end(skb, data);
6588 genlmsg_end(skb, hdr);
6589 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6590}
6591EXPORT_SYMBOL(cfg80211_testmode_event);
6592#endif
6593
Samuel Ortizb23aa672009-07-01 21:26:54 +02006594static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6595{
Johannes Berg4c476992010-10-04 21:36:35 +02006596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6597 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006598 struct cfg80211_connect_params connect;
6599 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006600 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006601 int err;
6602
6603 memset(&connect, 0, sizeof(connect));
6604
6605 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6606 return -EINVAL;
6607
6608 if (!info->attrs[NL80211_ATTR_SSID] ||
6609 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6610 return -EINVAL;
6611
6612 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6613 connect.auth_type =
6614 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006615 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6616 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006617 return -EINVAL;
6618 } else
6619 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6620
6621 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6622
Johannes Bergc0692b82010-08-27 14:26:53 +03006623 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006624 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006625 if (err)
6626 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006627
Johannes Berg074ac8d2010-09-16 14:58:22 +02006628 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006629 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6630 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006631
Johannes Berg79c97e92009-07-07 03:56:12 +02006632 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006633
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306634 connect.bg_scan_period = -1;
6635 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6636 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6637 connect.bg_scan_period =
6638 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6639 }
6640
Samuel Ortizb23aa672009-07-01 21:26:54 +02006641 if (info->attrs[NL80211_ATTR_MAC])
6642 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6643 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6644 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6645
6646 if (info->attrs[NL80211_ATTR_IE]) {
6647 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6648 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6649 }
6650
Jouni Malinencee00a92013-01-15 17:15:57 +02006651 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6652 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6653 if (connect.mfp != NL80211_MFP_REQUIRED &&
6654 connect.mfp != NL80211_MFP_NO)
6655 return -EINVAL;
6656 } else {
6657 connect.mfp = NL80211_MFP_NO;
6658 }
6659
Samuel Ortizb23aa672009-07-01 21:26:54 +02006660 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6661 connect.channel =
6662 ieee80211_get_channel(wiphy,
6663 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6664 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006665 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6666 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006667 }
6668
Johannes Bergfffd0932009-07-08 14:22:54 +02006669 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6670 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306671 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006672 if (IS_ERR(connkeys))
6673 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006674 }
6675
Ben Greear7e7c8922011-11-18 11:31:59 -08006676 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6677 connect.flags |= ASSOC_REQ_DISABLE_HT;
6678
6679 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6680 memcpy(&connect.ht_capa_mask,
6681 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6682 sizeof(connect.ht_capa_mask));
6683
6684 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006685 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6686 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006687 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006688 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006689 memcpy(&connect.ht_capa,
6690 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6691 sizeof(connect.ht_capa));
6692 }
6693
Johannes Bergee2aca32013-02-21 17:36:01 +01006694 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6695 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6696
6697 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6698 memcpy(&connect.vht_capa_mask,
6699 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6700 sizeof(connect.vht_capa_mask));
6701
6702 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6703 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
6704 kfree(connkeys);
6705 return -EINVAL;
6706 }
6707 memcpy(&connect.vht_capa,
6708 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6709 sizeof(connect.vht_capa));
6710 }
6711
Johannes Bergfffd0932009-07-08 14:22:54 +02006712 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006713 if (err)
6714 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006715 return err;
6716}
6717
6718static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6719{
Johannes Berg4c476992010-10-04 21:36:35 +02006720 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6721 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006722 u16 reason;
6723
6724 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6725 reason = WLAN_REASON_DEAUTH_LEAVING;
6726 else
6727 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6728
6729 if (reason == 0)
6730 return -EINVAL;
6731
Johannes Berg074ac8d2010-09-16 14:58:22 +02006732 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006733 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6734 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006735
Johannes Berg4c476992010-10-04 21:36:35 +02006736 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006737}
6738
Johannes Berg463d0182009-07-14 00:33:35 +02006739static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6740{
Johannes Berg4c476992010-10-04 21:36:35 +02006741 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006742 struct net *net;
6743 int err;
6744 u32 pid;
6745
6746 if (!info->attrs[NL80211_ATTR_PID])
6747 return -EINVAL;
6748
6749 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6750
Johannes Berg463d0182009-07-14 00:33:35 +02006751 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006752 if (IS_ERR(net))
6753 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006754
6755 err = 0;
6756
6757 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006758 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6759 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006760
Johannes Berg463d0182009-07-14 00:33:35 +02006761 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006762 return err;
6763}
6764
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006765static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6766{
Johannes Berg4c476992010-10-04 21:36:35 +02006767 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006768 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6769 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006770 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006771 struct cfg80211_pmksa pmksa;
6772
6773 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6774
6775 if (!info->attrs[NL80211_ATTR_MAC])
6776 return -EINVAL;
6777
6778 if (!info->attrs[NL80211_ATTR_PMKID])
6779 return -EINVAL;
6780
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006781 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6782 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6783
Johannes Berg074ac8d2010-09-16 14:58:22 +02006784 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006785 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6786 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006787
6788 switch (info->genlhdr->cmd) {
6789 case NL80211_CMD_SET_PMKSA:
6790 rdev_ops = rdev->ops->set_pmksa;
6791 break;
6792 case NL80211_CMD_DEL_PMKSA:
6793 rdev_ops = rdev->ops->del_pmksa;
6794 break;
6795 default:
6796 WARN_ON(1);
6797 break;
6798 }
6799
Johannes Berg4c476992010-10-04 21:36:35 +02006800 if (!rdev_ops)
6801 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006802
Johannes Berg4c476992010-10-04 21:36:35 +02006803 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006804}
6805
6806static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6807{
Johannes Berg4c476992010-10-04 21:36:35 +02006808 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6809 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006810
Johannes Berg074ac8d2010-09-16 14:58:22 +02006811 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006812 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6813 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006814
Johannes Berg4c476992010-10-04 21:36:35 +02006815 if (!rdev->ops->flush_pmksa)
6816 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006817
Hila Gonene35e4d22012-06-27 17:19:42 +03006818 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006819}
6820
Arik Nemtsov109086c2011-09-28 14:12:50 +03006821static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6822{
6823 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6824 struct net_device *dev = info->user_ptr[1];
6825 u8 action_code, dialog_token;
6826 u16 status_code;
6827 u8 *peer;
6828
6829 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6830 !rdev->ops->tdls_mgmt)
6831 return -EOPNOTSUPP;
6832
6833 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6834 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6835 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6836 !info->attrs[NL80211_ATTR_IE] ||
6837 !info->attrs[NL80211_ATTR_MAC])
6838 return -EINVAL;
6839
6840 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6841 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6842 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6843 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6844
Hila Gonene35e4d22012-06-27 17:19:42 +03006845 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6846 dialog_token, status_code,
6847 nla_data(info->attrs[NL80211_ATTR_IE]),
6848 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006849}
6850
6851static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6852{
6853 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6854 struct net_device *dev = info->user_ptr[1];
6855 enum nl80211_tdls_operation operation;
6856 u8 *peer;
6857
6858 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6859 !rdev->ops->tdls_oper)
6860 return -EOPNOTSUPP;
6861
6862 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6863 !info->attrs[NL80211_ATTR_MAC])
6864 return -EINVAL;
6865
6866 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6867 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6868
Hila Gonene35e4d22012-06-27 17:19:42 +03006869 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006870}
6871
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006872static int nl80211_remain_on_channel(struct sk_buff *skb,
6873 struct genl_info *info)
6874{
Johannes Berg4c476992010-10-04 21:36:35 +02006875 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006876 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006877 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006878 struct sk_buff *msg;
6879 void *hdr;
6880 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006881 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006882 int err;
6883
6884 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6885 !info->attrs[NL80211_ATTR_DURATION])
6886 return -EINVAL;
6887
6888 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6889
Johannes Berg7c4ef712011-11-18 15:33:48 +01006890 if (!rdev->ops->remain_on_channel ||
6891 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006892 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006893
Johannes Bergebf348f2012-06-01 12:50:54 +02006894 /*
6895 * We should be on that channel for at least a minimum amount of
6896 * time (10ms) but no longer than the driver supports.
6897 */
6898 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6899 duration > rdev->wiphy.max_remain_on_channel_duration)
6900 return -EINVAL;
6901
Johannes Berg683b6d32012-11-08 21:25:48 +01006902 err = nl80211_parse_chandef(rdev, info, &chandef);
6903 if (err)
6904 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006905
6906 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006907 if (!msg)
6908 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006909
Eric W. Biederman15e47302012-09-07 20:12:54 +00006910 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006911 NL80211_CMD_REMAIN_ON_CHANNEL);
6912
6913 if (IS_ERR(hdr)) {
6914 err = PTR_ERR(hdr);
6915 goto free_msg;
6916 }
6917
Johannes Berg683b6d32012-11-08 21:25:48 +01006918 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6919 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006920
6921 if (err)
6922 goto free_msg;
6923
David S. Miller9360ffd2012-03-29 04:41:26 -04006924 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6925 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006926
6927 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006928
6929 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006930
6931 nla_put_failure:
6932 err = -ENOBUFS;
6933 free_msg:
6934 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006935 return err;
6936}
6937
6938static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6939 struct genl_info *info)
6940{
Johannes Berg4c476992010-10-04 21:36:35 +02006941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006942 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006943 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006944
6945 if (!info->attrs[NL80211_ATTR_COOKIE])
6946 return -EINVAL;
6947
Johannes Berg4c476992010-10-04 21:36:35 +02006948 if (!rdev->ops->cancel_remain_on_channel)
6949 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006950
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006951 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6952
Hila Gonene35e4d22012-06-27 17:19:42 +03006953 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006954}
6955
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006956static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6957 u8 *rates, u8 rates_len)
6958{
6959 u8 i;
6960 u32 mask = 0;
6961
6962 for (i = 0; i < rates_len; i++) {
6963 int rate = (rates[i] & 0x7f) * 5;
6964 int ridx;
6965 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6966 struct ieee80211_rate *srate =
6967 &sband->bitrates[ridx];
6968 if (rate == srate->bitrate) {
6969 mask |= 1 << ridx;
6970 break;
6971 }
6972 }
6973 if (ridx == sband->n_bitrates)
6974 return 0; /* rate not found */
6975 }
6976
6977 return mask;
6978}
6979
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006980static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6981 u8 *rates, u8 rates_len,
6982 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6983{
6984 u8 i;
6985
6986 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6987
6988 for (i = 0; i < rates_len; i++) {
6989 int ridx, rbit;
6990
6991 ridx = rates[i] / 8;
6992 rbit = BIT(rates[i] % 8);
6993
6994 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006995 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006996 return false;
6997
6998 /* check availability */
6999 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7000 mcs[ridx] |= rbit;
7001 else
7002 return false;
7003 }
7004
7005 return true;
7006}
7007
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007008static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007009 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7010 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007011 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
7012 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007013};
7014
7015static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7016 struct genl_info *info)
7017{
7018 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007019 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007020 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007021 int rem, i;
7022 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007023 struct nlattr *tx_rates;
7024 struct ieee80211_supported_band *sband;
7025
7026 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7027 return -EINVAL;
7028
Johannes Berg4c476992010-10-04 21:36:35 +02007029 if (!rdev->ops->set_bitrate_mask)
7030 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007031
7032 memset(&mask, 0, sizeof(mask));
7033 /* Default to all rates enabled */
7034 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7035 sband = rdev->wiphy.bands[i];
7036 mask.control[i].legacy =
7037 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007038 if (sband)
7039 memcpy(mask.control[i].mcs,
7040 sband->ht_cap.mcs.rx_mask,
7041 sizeof(mask.control[i].mcs));
7042 else
7043 memset(mask.control[i].mcs, 0,
7044 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007045 }
7046
7047 /*
7048 * The nested attribute uses enum nl80211_band as the index. This maps
7049 * directly to the enum ieee80211_band values used in cfg80211.
7050 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007051 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007052 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7053 {
7054 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007055 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7056 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007057 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007058 if (sband == NULL)
7059 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007060 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7061 nla_len(tx_rates), nl80211_txattr_policy);
7062 if (tb[NL80211_TXRATE_LEGACY]) {
7063 mask.control[band].legacy = rateset_to_mask(
7064 sband,
7065 nla_data(tb[NL80211_TXRATE_LEGACY]),
7066 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307067 if ((mask.control[band].legacy == 0) &&
7068 nla_len(tb[NL80211_TXRATE_LEGACY]))
7069 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007070 }
7071 if (tb[NL80211_TXRATE_MCS]) {
7072 if (!ht_rateset_to_mask(
7073 sband,
7074 nla_data(tb[NL80211_TXRATE_MCS]),
7075 nla_len(tb[NL80211_TXRATE_MCS]),
7076 mask.control[band].mcs))
7077 return -EINVAL;
7078 }
7079
7080 if (mask.control[band].legacy == 0) {
7081 /* don't allow empty legacy rates if HT
7082 * is not even supported. */
7083 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7084 return -EINVAL;
7085
7086 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7087 if (mask.control[band].mcs[i])
7088 break;
7089
7090 /* legacy and mcs rates may not be both empty */
7091 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02007092 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007093 }
7094 }
7095
Hila Gonene35e4d22012-06-27 17:19:42 +03007096 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007097}
7098
Johannes Berg2e161f72010-08-12 15:38:38 +02007099static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007100{
Johannes Berg4c476992010-10-04 21:36:35 +02007101 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007102 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007103 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007104
7105 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7106 return -EINVAL;
7107
Johannes Berg2e161f72010-08-12 15:38:38 +02007108 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7109 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007110
Johannes Berg71bbc992012-06-15 15:30:18 +02007111 switch (wdev->iftype) {
7112 case NL80211_IFTYPE_STATION:
7113 case NL80211_IFTYPE_ADHOC:
7114 case NL80211_IFTYPE_P2P_CLIENT:
7115 case NL80211_IFTYPE_AP:
7116 case NL80211_IFTYPE_AP_VLAN:
7117 case NL80211_IFTYPE_MESH_POINT:
7118 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007119 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007120 break;
7121 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007122 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007123 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007124
7125 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007126 if (!rdev->ops->mgmt_tx)
7127 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007128
Eric W. Biederman15e47302012-09-07 20:12:54 +00007129 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007130 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7131 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007132}
7133
Johannes Berg2e161f72010-08-12 15:38:38 +02007134static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007135{
Johannes Berg4c476992010-10-04 21:36:35 +02007136 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007137 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007138 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007139 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007140 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007141 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007142 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007143 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01007144 bool offchan, no_cck, dont_wait_for_ack;
7145
7146 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02007147
Johannes Berg683b6d32012-11-08 21:25:48 +01007148 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007149 return -EINVAL;
7150
Johannes Berg4c476992010-10-04 21:36:35 +02007151 if (!rdev->ops->mgmt_tx)
7152 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007153
Johannes Berg71bbc992012-06-15 15:30:18 +02007154 switch (wdev->iftype) {
7155 case NL80211_IFTYPE_STATION:
7156 case NL80211_IFTYPE_ADHOC:
7157 case NL80211_IFTYPE_P2P_CLIENT:
7158 case NL80211_IFTYPE_AP:
7159 case NL80211_IFTYPE_AP_VLAN:
7160 case NL80211_IFTYPE_MESH_POINT:
7161 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007162 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007163 break;
7164 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007165 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007166 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007167
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007168 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007169 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007170 return -EINVAL;
7171 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007172
7173 /*
7174 * We should wait on the channel for at least a minimum amount
7175 * of time (10ms) but no longer than the driver supports.
7176 */
7177 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7178 wait > rdev->wiphy.max_remain_on_channel_duration)
7179 return -EINVAL;
7180
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007181 }
7182
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007183 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7184
Johannes Berg7c4ef712011-11-18 15:33:48 +01007185 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7186 return -EINVAL;
7187
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307188 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7189
Johannes Berg683b6d32012-11-08 21:25:48 +01007190 err = nl80211_parse_chandef(rdev, info, &chandef);
7191 if (err)
7192 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02007193
Johannes Berge247bd902011-11-04 11:18:21 +01007194 if (!dont_wait_for_ack) {
7195 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7196 if (!msg)
7197 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007198
Eric W. Biederman15e47302012-09-07 20:12:54 +00007199 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007200 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007201
Johannes Berge247bd902011-11-04 11:18:21 +01007202 if (IS_ERR(hdr)) {
7203 err = PTR_ERR(hdr);
7204 goto free_msg;
7205 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007206 }
Johannes Berge247bd902011-11-04 11:18:21 +01007207
Johannes Berg683b6d32012-11-08 21:25:48 +01007208 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02007209 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7210 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01007211 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007212 if (err)
7213 goto free_msg;
7214
Johannes Berge247bd902011-11-04 11:18:21 +01007215 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007216 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7217 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007218
Johannes Berge247bd902011-11-04 11:18:21 +01007219 genlmsg_end(msg, hdr);
7220 return genlmsg_reply(msg, info);
7221 }
7222
7223 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007224
7225 nla_put_failure:
7226 err = -ENOBUFS;
7227 free_msg:
7228 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007229 return err;
7230}
7231
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007232static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7233{
7234 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007235 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007236 u64 cookie;
7237
7238 if (!info->attrs[NL80211_ATTR_COOKIE])
7239 return -EINVAL;
7240
7241 if (!rdev->ops->mgmt_tx_cancel_wait)
7242 return -EOPNOTSUPP;
7243
Johannes Berg71bbc992012-06-15 15:30:18 +02007244 switch (wdev->iftype) {
7245 case NL80211_IFTYPE_STATION:
7246 case NL80211_IFTYPE_ADHOC:
7247 case NL80211_IFTYPE_P2P_CLIENT:
7248 case NL80211_IFTYPE_AP:
7249 case NL80211_IFTYPE_AP_VLAN:
7250 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007251 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007252 break;
7253 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007254 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007255 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007256
7257 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7258
Hila Gonene35e4d22012-06-27 17:19:42 +03007259 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007260}
7261
Kalle Valoffb9eb32010-02-17 17:58:10 +02007262static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7263{
Johannes Berg4c476992010-10-04 21:36:35 +02007264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007265 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007266 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007267 u8 ps_state;
7268 bool state;
7269 int err;
7270
Johannes Berg4c476992010-10-04 21:36:35 +02007271 if (!info->attrs[NL80211_ATTR_PS_STATE])
7272 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007273
7274 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7275
Johannes Berg4c476992010-10-04 21:36:35 +02007276 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7277 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007278
7279 wdev = dev->ieee80211_ptr;
7280
Johannes Berg4c476992010-10-04 21:36:35 +02007281 if (!rdev->ops->set_power_mgmt)
7282 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007283
7284 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7285
7286 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007287 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007288
Hila Gonene35e4d22012-06-27 17:19:42 +03007289 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007290 if (!err)
7291 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007292 return err;
7293}
7294
7295static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7296{
Johannes Berg4c476992010-10-04 21:36:35 +02007297 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007298 enum nl80211_ps_state ps_state;
7299 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007300 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007301 struct sk_buff *msg;
7302 void *hdr;
7303 int err;
7304
Kalle Valoffb9eb32010-02-17 17:58:10 +02007305 wdev = dev->ieee80211_ptr;
7306
Johannes Berg4c476992010-10-04 21:36:35 +02007307 if (!rdev->ops->set_power_mgmt)
7308 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007309
7310 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007311 if (!msg)
7312 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007313
Eric W. Biederman15e47302012-09-07 20:12:54 +00007314 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007315 NL80211_CMD_GET_POWER_SAVE);
7316 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007317 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007318 goto free_msg;
7319 }
7320
7321 if (wdev->ps)
7322 ps_state = NL80211_PS_ENABLED;
7323 else
7324 ps_state = NL80211_PS_DISABLED;
7325
David S. Miller9360ffd2012-03-29 04:41:26 -04007326 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7327 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007328
7329 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007330 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007331
Johannes Berg4c476992010-10-04 21:36:35 +02007332 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007333 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007334 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007335 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007336 return err;
7337}
7338
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007339static struct nla_policy
7340nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7341 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7342 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7343 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007344 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7345 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7346 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007347};
7348
Thomas Pedersen84f10702012-07-12 16:17:33 -07007349static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007350 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007351{
7352 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7353 struct wireless_dev *wdev;
7354 struct net_device *dev = info->user_ptr[1];
7355
Johannes Bergd9d8b012012-11-26 12:51:52 +01007356 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007357 return -EINVAL;
7358
7359 wdev = dev->ieee80211_ptr;
7360
7361 if (!rdev->ops->set_cqm_txe_config)
7362 return -EOPNOTSUPP;
7363
7364 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7365 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7366 return -EOPNOTSUPP;
7367
Hila Gonene35e4d22012-06-27 17:19:42 +03007368 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007369}
7370
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007371static int nl80211_set_cqm_rssi(struct genl_info *info,
7372 s32 threshold, u32 hysteresis)
7373{
Johannes Berg4c476992010-10-04 21:36:35 +02007374 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007375 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007376 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007377
7378 if (threshold > 0)
7379 return -EINVAL;
7380
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007381 wdev = dev->ieee80211_ptr;
7382
Johannes Berg4c476992010-10-04 21:36:35 +02007383 if (!rdev->ops->set_cqm_rssi_config)
7384 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007385
Johannes Berg074ac8d2010-09-16 14:58:22 +02007386 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007387 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7388 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007389
Hila Gonene35e4d22012-06-27 17:19:42 +03007390 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007391}
7392
7393static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7394{
7395 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7396 struct nlattr *cqm;
7397 int err;
7398
7399 cqm = info->attrs[NL80211_ATTR_CQM];
7400 if (!cqm) {
7401 err = -EINVAL;
7402 goto out;
7403 }
7404
7405 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7406 nl80211_attr_cqm_policy);
7407 if (err)
7408 goto out;
7409
7410 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7411 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7412 s32 threshold;
7413 u32 hysteresis;
7414 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7415 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7416 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007417 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7418 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7419 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7420 u32 rate, pkts, intvl;
7421 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7422 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7423 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7424 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007425 } else
7426 err = -EINVAL;
7427
7428out:
7429 return err;
7430}
7431
Johannes Berg29cbe682010-12-03 09:20:44 +01007432static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7433{
7434 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7435 struct net_device *dev = info->user_ptr[1];
7436 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007437 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007438 int err;
7439
7440 /* start with default */
7441 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007442 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007443
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007444 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007445 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007446 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007447 if (err)
7448 return err;
7449 }
7450
7451 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7452 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7453 return -EINVAL;
7454
Javier Cardonac80d5452010-12-16 17:37:49 -08007455 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7456 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7457
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007458 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7459 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7460 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7461 return -EINVAL;
7462
Marco Porsch9bdbf042013-01-07 16:04:51 +01007463 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7464 setup.beacon_interval =
7465 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7466 if (setup.beacon_interval < 10 ||
7467 setup.beacon_interval > 10000)
7468 return -EINVAL;
7469 }
7470
7471 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7472 setup.dtim_period =
7473 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7474 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7475 return -EINVAL;
7476 }
7477
Javier Cardonac80d5452010-12-16 17:37:49 -08007478 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7479 /* parse additional setup parameters if given */
7480 err = nl80211_parse_mesh_setup(info, &setup);
7481 if (err)
7482 return err;
7483 }
7484
Thomas Pedersend37bb182013-03-04 13:06:13 -08007485 if (setup.user_mpm)
7486 cfg.auto_open_plinks = false;
7487
Johannes Bergcc1d2802012-05-16 23:50:20 +02007488 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007489 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7490 if (err)
7491 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007492 } else {
7493 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007494 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007495 }
7496
Javier Cardonac80d5452010-12-16 17:37:49 -08007497 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007498}
7499
7500static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7501{
7502 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7503 struct net_device *dev = info->user_ptr[1];
7504
7505 return cfg80211_leave_mesh(rdev, dev);
7506}
7507
Johannes Bergdfb89c52012-06-27 09:23:48 +02007508#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007509static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7510 struct cfg80211_registered_device *rdev)
7511{
7512 struct nlattr *nl_pats, *nl_pat;
7513 int i, pat_len;
7514
7515 if (!rdev->wowlan->n_patterns)
7516 return 0;
7517
7518 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7519 if (!nl_pats)
7520 return -ENOBUFS;
7521
7522 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7523 nl_pat = nla_nest_start(msg, i + 1);
7524 if (!nl_pat)
7525 return -ENOBUFS;
7526 pat_len = rdev->wowlan->patterns[i].pattern_len;
7527 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7528 DIV_ROUND_UP(pat_len, 8),
7529 rdev->wowlan->patterns[i].mask) ||
7530 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7531 pat_len, rdev->wowlan->patterns[i].pattern) ||
7532 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7533 rdev->wowlan->patterns[i].pkt_offset))
7534 return -ENOBUFS;
7535 nla_nest_end(msg, nl_pat);
7536 }
7537 nla_nest_end(msg, nl_pats);
7538
7539 return 0;
7540}
7541
Johannes Berg2a0e0472013-01-23 22:57:40 +01007542static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7543 struct cfg80211_wowlan_tcp *tcp)
7544{
7545 struct nlattr *nl_tcp;
7546
7547 if (!tcp)
7548 return 0;
7549
7550 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7551 if (!nl_tcp)
7552 return -ENOBUFS;
7553
7554 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7555 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7556 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7557 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7558 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7559 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7560 tcp->payload_len, tcp->payload) ||
7561 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7562 tcp->data_interval) ||
7563 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7564 tcp->wake_len, tcp->wake_data) ||
7565 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7566 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7567 return -ENOBUFS;
7568
7569 if (tcp->payload_seq.len &&
7570 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7571 sizeof(tcp->payload_seq), &tcp->payload_seq))
7572 return -ENOBUFS;
7573
7574 if (tcp->payload_tok.len &&
7575 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7576 sizeof(tcp->payload_tok) + tcp->tokens_size,
7577 &tcp->payload_tok))
7578 return -ENOBUFS;
7579
7580 return 0;
7581}
7582
Johannes Bergff1b6e62011-05-04 15:37:28 +02007583static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7584{
7585 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7586 struct sk_buff *msg;
7587 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007588 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007589
Johannes Berg2a0e0472013-01-23 22:57:40 +01007590 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7591 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007592 return -EOPNOTSUPP;
7593
Johannes Berg2a0e0472013-01-23 22:57:40 +01007594 if (rdev->wowlan && rdev->wowlan->tcp) {
7595 /* adjust size to have room for all the data */
7596 size += rdev->wowlan->tcp->tokens_size +
7597 rdev->wowlan->tcp->payload_len +
7598 rdev->wowlan->tcp->wake_len +
7599 rdev->wowlan->tcp->wake_len / 8;
7600 }
7601
7602 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007603 if (!msg)
7604 return -ENOMEM;
7605
Eric W. Biederman15e47302012-09-07 20:12:54 +00007606 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007607 NL80211_CMD_GET_WOWLAN);
7608 if (!hdr)
7609 goto nla_put_failure;
7610
7611 if (rdev->wowlan) {
7612 struct nlattr *nl_wowlan;
7613
7614 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7615 if (!nl_wowlan)
7616 goto nla_put_failure;
7617
David S. Miller9360ffd2012-03-29 04:41:26 -04007618 if ((rdev->wowlan->any &&
7619 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7620 (rdev->wowlan->disconnect &&
7621 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7622 (rdev->wowlan->magic_pkt &&
7623 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7624 (rdev->wowlan->gtk_rekey_failure &&
7625 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7626 (rdev->wowlan->eap_identity_req &&
7627 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7628 (rdev->wowlan->four_way_handshake &&
7629 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7630 (rdev->wowlan->rfkill_release &&
7631 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7632 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007633
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007634 if (nl80211_send_wowlan_patterns(msg, rdev))
7635 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007636
7637 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7638 goto nla_put_failure;
7639
Johannes Bergff1b6e62011-05-04 15:37:28 +02007640 nla_nest_end(msg, nl_wowlan);
7641 }
7642
7643 genlmsg_end(msg, hdr);
7644 return genlmsg_reply(msg, info);
7645
7646nla_put_failure:
7647 nlmsg_free(msg);
7648 return -ENOBUFS;
7649}
7650
Johannes Berg2a0e0472013-01-23 22:57:40 +01007651static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7652 struct nlattr *attr,
7653 struct cfg80211_wowlan *trig)
7654{
7655 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7656 struct cfg80211_wowlan_tcp *cfg;
7657 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7658 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7659 u32 size;
7660 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7661 int err, port;
7662
7663 if (!rdev->wiphy.wowlan.tcp)
7664 return -EINVAL;
7665
7666 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7667 nla_data(attr), nla_len(attr),
7668 nl80211_wowlan_tcp_policy);
7669 if (err)
7670 return err;
7671
7672 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7673 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7674 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7675 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7676 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7677 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7678 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7679 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7680 return -EINVAL;
7681
7682 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7683 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7684 return -EINVAL;
7685
7686 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg723d5682013-02-26 13:56:40 +01007687 rdev->wiphy.wowlan.tcp->data_interval_max ||
7688 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007689 return -EINVAL;
7690
7691 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7692 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7693 return -EINVAL;
7694
7695 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7696 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7697 return -EINVAL;
7698
7699 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7700 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7701
7702 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7703 tokens_size = tokln - sizeof(*tok);
7704
7705 if (!tok->len || tokens_size % tok->len)
7706 return -EINVAL;
7707 if (!rdev->wiphy.wowlan.tcp->tok)
7708 return -EINVAL;
7709 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7710 return -EINVAL;
7711 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7712 return -EINVAL;
7713 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7714 return -EINVAL;
7715 if (tok->offset + tok->len > data_size)
7716 return -EINVAL;
7717 }
7718
7719 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7720 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7721 if (!rdev->wiphy.wowlan.tcp->seq)
7722 return -EINVAL;
7723 if (seq->len == 0 || seq->len > 4)
7724 return -EINVAL;
7725 if (seq->len + seq->offset > data_size)
7726 return -EINVAL;
7727 }
7728
7729 size = sizeof(*cfg);
7730 size += data_size;
7731 size += wake_size + wake_mask_size;
7732 size += tokens_size;
7733
7734 cfg = kzalloc(size, GFP_KERNEL);
7735 if (!cfg)
7736 return -ENOMEM;
7737 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7738 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7739 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7740 ETH_ALEN);
7741 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7742 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7743 else
7744 port = 0;
7745#ifdef CONFIG_INET
7746 /* allocate a socket and port for it and use it */
7747 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7748 IPPROTO_TCP, &cfg->sock, 1);
7749 if (err) {
7750 kfree(cfg);
7751 return err;
7752 }
7753 if (inet_csk_get_port(cfg->sock->sk, port)) {
7754 sock_release(cfg->sock);
7755 kfree(cfg);
7756 return -EADDRINUSE;
7757 }
7758 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7759#else
7760 if (!port) {
7761 kfree(cfg);
7762 return -EINVAL;
7763 }
7764 cfg->src_port = port;
7765#endif
7766
7767 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7768 cfg->payload_len = data_size;
7769 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7770 memcpy((void *)cfg->payload,
7771 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7772 data_size);
7773 if (seq)
7774 cfg->payload_seq = *seq;
7775 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7776 cfg->wake_len = wake_size;
7777 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7778 memcpy((void *)cfg->wake_data,
7779 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7780 wake_size);
7781 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7782 data_size + wake_size;
7783 memcpy((void *)cfg->wake_mask,
7784 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7785 wake_mask_size);
7786 if (tok) {
7787 cfg->tokens_size = tokens_size;
7788 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7789 }
7790
7791 trig->tcp = cfg;
7792
7793 return 0;
7794}
7795
Johannes Bergff1b6e62011-05-04 15:37:28 +02007796static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7797{
7798 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7799 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007800 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007801 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007802 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7803 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007804 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007805
Johannes Berg2a0e0472013-01-23 22:57:40 +01007806 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7807 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007808 return -EOPNOTSUPP;
7809
Johannes Bergae33bd82012-07-12 16:25:02 +02007810 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7811 cfg80211_rdev_free_wowlan(rdev);
7812 rdev->wowlan = NULL;
7813 goto set_wakeup;
7814 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007815
7816 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7817 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7818 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7819 nl80211_wowlan_policy);
7820 if (err)
7821 return err;
7822
7823 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7824 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7825 return -EINVAL;
7826 new_triggers.any = true;
7827 }
7828
7829 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7830 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7831 return -EINVAL;
7832 new_triggers.disconnect = true;
7833 }
7834
7835 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7836 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7837 return -EINVAL;
7838 new_triggers.magic_pkt = true;
7839 }
7840
Johannes Berg77dbbb12011-07-13 10:48:55 +02007841 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7842 return -EINVAL;
7843
7844 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7845 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7846 return -EINVAL;
7847 new_triggers.gtk_rekey_failure = true;
7848 }
7849
7850 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7851 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7852 return -EINVAL;
7853 new_triggers.eap_identity_req = true;
7854 }
7855
7856 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7857 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7858 return -EINVAL;
7859 new_triggers.four_way_handshake = true;
7860 }
7861
7862 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7863 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7864 return -EINVAL;
7865 new_triggers.rfkill_release = true;
7866 }
7867
Johannes Bergff1b6e62011-05-04 15:37:28 +02007868 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7869 struct nlattr *pat;
7870 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007871 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007872 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7873
7874 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7875 rem)
7876 n_patterns++;
7877 if (n_patterns > wowlan->n_patterns)
7878 return -EINVAL;
7879
7880 new_triggers.patterns = kcalloc(n_patterns,
7881 sizeof(new_triggers.patterns[0]),
7882 GFP_KERNEL);
7883 if (!new_triggers.patterns)
7884 return -ENOMEM;
7885
7886 new_triggers.n_patterns = n_patterns;
7887 i = 0;
7888
7889 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7890 rem) {
7891 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7892 nla_data(pat), nla_len(pat), NULL);
7893 err = -EINVAL;
7894 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7895 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7896 goto error;
7897 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7898 mask_len = DIV_ROUND_UP(pat_len, 8);
7899 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7900 mask_len)
7901 goto error;
7902 if (pat_len > wowlan->pattern_max_len ||
7903 pat_len < wowlan->pattern_min_len)
7904 goto error;
7905
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007906 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7907 pkt_offset = 0;
7908 else
7909 pkt_offset = nla_get_u32(
7910 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7911 if (pkt_offset > wowlan->max_pkt_offset)
7912 goto error;
7913 new_triggers.patterns[i].pkt_offset = pkt_offset;
7914
Johannes Bergff1b6e62011-05-04 15:37:28 +02007915 new_triggers.patterns[i].mask =
7916 kmalloc(mask_len + pat_len, GFP_KERNEL);
7917 if (!new_triggers.patterns[i].mask) {
7918 err = -ENOMEM;
7919 goto error;
7920 }
7921 new_triggers.patterns[i].pattern =
7922 new_triggers.patterns[i].mask + mask_len;
7923 memcpy(new_triggers.patterns[i].mask,
7924 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7925 mask_len);
7926 new_triggers.patterns[i].pattern_len = pat_len;
7927 memcpy(new_triggers.patterns[i].pattern,
7928 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7929 pat_len);
7930 i++;
7931 }
7932 }
7933
Johannes Berg2a0e0472013-01-23 22:57:40 +01007934 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7935 err = nl80211_parse_wowlan_tcp(
7936 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7937 &new_triggers);
7938 if (err)
7939 goto error;
7940 }
7941
Johannes Bergae33bd82012-07-12 16:25:02 +02007942 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7943 if (!ntrig) {
7944 err = -ENOMEM;
7945 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007946 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007947 cfg80211_rdev_free_wowlan(rdev);
7948 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007949
Johannes Bergae33bd82012-07-12 16:25:02 +02007950 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007951 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007952 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007953
Johannes Bergff1b6e62011-05-04 15:37:28 +02007954 return 0;
7955 error:
7956 for (i = 0; i < new_triggers.n_patterns; i++)
7957 kfree(new_triggers.patterns[i].mask);
7958 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007959 if (new_triggers.tcp && new_triggers.tcp->sock)
7960 sock_release(new_triggers.tcp->sock);
7961 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007962 return err;
7963}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007964#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007965
Johannes Berge5497d72011-07-05 16:35:40 +02007966static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7967{
7968 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7969 struct net_device *dev = info->user_ptr[1];
7970 struct wireless_dev *wdev = dev->ieee80211_ptr;
7971 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7972 struct cfg80211_gtk_rekey_data rekey_data;
7973 int err;
7974
7975 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7976 return -EINVAL;
7977
7978 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7979 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7980 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7981 nl80211_rekey_policy);
7982 if (err)
7983 return err;
7984
7985 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7986 return -ERANGE;
7987 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7988 return -ERANGE;
7989 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7990 return -ERANGE;
7991
7992 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7993 NL80211_KEK_LEN);
7994 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7995 NL80211_KCK_LEN);
7996 memcpy(rekey_data.replay_ctr,
7997 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7998 NL80211_REPLAY_CTR_LEN);
7999
8000 wdev_lock(wdev);
8001 if (!wdev->current_bss) {
8002 err = -ENOTCONN;
8003 goto out;
8004 }
8005
8006 if (!rdev->ops->set_rekey_data) {
8007 err = -EOPNOTSUPP;
8008 goto out;
8009 }
8010
Hila Gonene35e4d22012-06-27 17:19:42 +03008011 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008012 out:
8013 wdev_unlock(wdev);
8014 return err;
8015}
8016
Johannes Berg28946da2011-11-04 11:18:12 +01008017static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8018 struct genl_info *info)
8019{
8020 struct net_device *dev = info->user_ptr[1];
8021 struct wireless_dev *wdev = dev->ieee80211_ptr;
8022
8023 if (wdev->iftype != NL80211_IFTYPE_AP &&
8024 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8025 return -EINVAL;
8026
Eric W. Biederman15e47302012-09-07 20:12:54 +00008027 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008028 return -EBUSY;
8029
Eric W. Biederman15e47302012-09-07 20:12:54 +00008030 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008031 return 0;
8032}
8033
Johannes Berg7f6cf312011-11-04 11:18:15 +01008034static int nl80211_probe_client(struct sk_buff *skb,
8035 struct genl_info *info)
8036{
8037 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8038 struct net_device *dev = info->user_ptr[1];
8039 struct wireless_dev *wdev = dev->ieee80211_ptr;
8040 struct sk_buff *msg;
8041 void *hdr;
8042 const u8 *addr;
8043 u64 cookie;
8044 int err;
8045
8046 if (wdev->iftype != NL80211_IFTYPE_AP &&
8047 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8048 return -EOPNOTSUPP;
8049
8050 if (!info->attrs[NL80211_ATTR_MAC])
8051 return -EINVAL;
8052
8053 if (!rdev->ops->probe_client)
8054 return -EOPNOTSUPP;
8055
8056 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8057 if (!msg)
8058 return -ENOMEM;
8059
Eric W. Biederman15e47302012-09-07 20:12:54 +00008060 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008061 NL80211_CMD_PROBE_CLIENT);
8062
8063 if (IS_ERR(hdr)) {
8064 err = PTR_ERR(hdr);
8065 goto free_msg;
8066 }
8067
8068 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8069
Hila Gonene35e4d22012-06-27 17:19:42 +03008070 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008071 if (err)
8072 goto free_msg;
8073
David S. Miller9360ffd2012-03-29 04:41:26 -04008074 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8075 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008076
8077 genlmsg_end(msg, hdr);
8078
8079 return genlmsg_reply(msg, info);
8080
8081 nla_put_failure:
8082 err = -ENOBUFS;
8083 free_msg:
8084 nlmsg_free(msg);
8085 return err;
8086}
8087
Johannes Berg5e760232011-11-04 11:18:17 +01008088static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8089{
8090 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008091 struct cfg80211_beacon_registration *reg, *nreg;
8092 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008093
8094 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8095 return -EOPNOTSUPP;
8096
Ben Greear37c73b52012-10-26 14:49:25 -07008097 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8098 if (!nreg)
8099 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008100
Ben Greear37c73b52012-10-26 14:49:25 -07008101 /* First, check if already registered. */
8102 spin_lock_bh(&rdev->beacon_registrations_lock);
8103 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8104 if (reg->nlportid == info->snd_portid) {
8105 rv = -EALREADY;
8106 goto out_err;
8107 }
8108 }
8109 /* Add it to the list */
8110 nreg->nlportid = info->snd_portid;
8111 list_add(&nreg->list, &rdev->beacon_registrations);
8112
8113 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008114
8115 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008116out_err:
8117 spin_unlock_bh(&rdev->beacon_registrations_lock);
8118 kfree(nreg);
8119 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008120}
8121
Johannes Berg98104fde2012-06-16 00:19:54 +02008122static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8123{
8124 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8125 struct wireless_dev *wdev = info->user_ptr[1];
8126 int err;
8127
8128 if (!rdev->ops->start_p2p_device)
8129 return -EOPNOTSUPP;
8130
8131 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8132 return -EOPNOTSUPP;
8133
8134 if (wdev->p2p_started)
8135 return 0;
8136
8137 mutex_lock(&rdev->devlist_mtx);
8138 err = cfg80211_can_add_interface(rdev, wdev->iftype);
8139 mutex_unlock(&rdev->devlist_mtx);
8140 if (err)
8141 return err;
8142
Johannes Bergeeb126e2012-10-23 15:16:50 +02008143 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008144 if (err)
8145 return err;
8146
8147 wdev->p2p_started = true;
8148 mutex_lock(&rdev->devlist_mtx);
8149 rdev->opencount++;
8150 mutex_unlock(&rdev->devlist_mtx);
8151
8152 return 0;
8153}
8154
8155static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8156{
8157 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8158 struct wireless_dev *wdev = info->user_ptr[1];
8159
8160 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8161 return -EOPNOTSUPP;
8162
8163 if (!rdev->ops->stop_p2p_device)
8164 return -EOPNOTSUPP;
8165
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008166 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +01008167 mutex_lock(&rdev->sched_scan_mtx);
8168 cfg80211_stop_p2p_device(rdev, wdev);
8169 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008170 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg98104fde2012-06-16 00:19:54 +02008171
8172 return 0;
8173}
8174
Johannes Berg3713b4e2013-02-14 16:19:38 +01008175static int nl80211_get_protocol_features(struct sk_buff *skb,
8176 struct genl_info *info)
8177{
8178 void *hdr;
8179 struct sk_buff *msg;
8180
8181 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8182 if (!msg)
8183 return -ENOMEM;
8184
8185 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8186 NL80211_CMD_GET_PROTOCOL_FEATURES);
8187 if (!hdr)
8188 goto nla_put_failure;
8189
8190 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8191 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8192 goto nla_put_failure;
8193
8194 genlmsg_end(msg, hdr);
8195 return genlmsg_reply(msg, info);
8196
8197 nla_put_failure:
8198 kfree_skb(msg);
8199 return -ENOBUFS;
8200}
8201
Jouni Malinen355199e2013-02-27 17:14:27 +02008202static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8203{
8204 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8205 struct cfg80211_update_ft_ies_params ft_params;
8206 struct net_device *dev = info->user_ptr[1];
8207
8208 if (!rdev->ops->update_ft_ies)
8209 return -EOPNOTSUPP;
8210
8211 if (!info->attrs[NL80211_ATTR_MDID] ||
8212 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8213 return -EINVAL;
8214
8215 memset(&ft_params, 0, sizeof(ft_params));
8216 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8217 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8218 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8219
8220 return rdev_update_ft_ies(rdev, dev, &ft_params);
8221}
8222
Arend van Spriel5de17982013-04-18 15:49:00 +02008223static int nl80211_crit_protocol_start(struct sk_buff *skb,
8224 struct genl_info *info)
8225{
8226 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8227 struct wireless_dev *wdev = info->user_ptr[1];
8228 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8229 u16 duration;
8230 int ret;
8231
8232 if (!rdev->ops->crit_proto_start)
8233 return -EOPNOTSUPP;
8234
8235 if (WARN_ON(!rdev->ops->crit_proto_stop))
8236 return -EINVAL;
8237
8238 if (rdev->crit_proto_nlportid)
8239 return -EBUSY;
8240
8241 /* determine protocol if provided */
8242 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8243 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8244
8245 if (proto >= NUM_NL80211_CRIT_PROTO)
8246 return -EINVAL;
8247
8248 /* timeout must be provided */
8249 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8250 return -EINVAL;
8251
8252 duration =
8253 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8254
8255 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8256 return -ERANGE;
8257
8258 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8259 if (!ret)
8260 rdev->crit_proto_nlportid = info->snd_portid;
8261
8262 return ret;
8263}
8264
8265static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8266 struct genl_info *info)
8267{
8268 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8269 struct wireless_dev *wdev = info->user_ptr[1];
8270
8271 if (!rdev->ops->crit_proto_stop)
8272 return -EOPNOTSUPP;
8273
8274 if (rdev->crit_proto_nlportid) {
8275 rdev->crit_proto_nlportid = 0;
8276 rdev_crit_proto_stop(rdev, wdev);
8277 }
8278 return 0;
8279}
8280
Johannes Berg4c476992010-10-04 21:36:35 +02008281#define NL80211_FLAG_NEED_WIPHY 0x01
8282#define NL80211_FLAG_NEED_NETDEV 0x02
8283#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008284#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8285#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8286 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008287#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008288/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008289#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8290 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008291
8292static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8293 struct genl_info *info)
8294{
8295 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008296 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008297 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008298 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8299
8300 if (rtnl)
8301 rtnl_lock();
8302
8303 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008304 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008305 if (IS_ERR(rdev)) {
8306 if (rtnl)
8307 rtnl_unlock();
8308 return PTR_ERR(rdev);
8309 }
8310 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008311 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8312 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008313 mutex_lock(&cfg80211_mutex);
8314 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8315 info->attrs);
8316 if (IS_ERR(wdev)) {
8317 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02008318 if (rtnl)
8319 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02008320 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02008321 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008322
Johannes Berg89a54e42012-06-15 14:33:17 +02008323 dev = wdev->netdev;
8324 rdev = wiphy_to_dev(wdev->wiphy);
8325
Johannes Berg1bf614e2012-06-15 15:23:36 +02008326 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8327 if (!dev) {
8328 mutex_unlock(&cfg80211_mutex);
8329 if (rtnl)
8330 rtnl_unlock();
8331 return -EINVAL;
8332 }
8333
8334 info->user_ptr[1] = dev;
8335 } else {
8336 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02008337 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008338
Johannes Berg1bf614e2012-06-15 15:23:36 +02008339 if (dev) {
8340 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8341 !netif_running(dev)) {
8342 mutex_unlock(&cfg80211_mutex);
8343 if (rtnl)
8344 rtnl_unlock();
8345 return -ENETDOWN;
8346 }
8347
8348 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008349 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8350 if (!wdev->p2p_started) {
8351 mutex_unlock(&cfg80211_mutex);
8352 if (rtnl)
8353 rtnl_unlock();
8354 return -ENETDOWN;
8355 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02008356 }
8357
Johannes Berg89a54e42012-06-15 14:33:17 +02008358 cfg80211_lock_rdev(rdev);
8359
8360 mutex_unlock(&cfg80211_mutex);
8361
Johannes Berg4c476992010-10-04 21:36:35 +02008362 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008363 }
8364
8365 return 0;
8366}
8367
8368static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8369 struct genl_info *info)
8370{
8371 if (info->user_ptr[0])
8372 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02008373 if (info->user_ptr[1]) {
8374 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8375 struct wireless_dev *wdev = info->user_ptr[1];
8376
8377 if (wdev->netdev)
8378 dev_put(wdev->netdev);
8379 } else {
8380 dev_put(info->user_ptr[1]);
8381 }
8382 }
Johannes Berg4c476992010-10-04 21:36:35 +02008383 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8384 rtnl_unlock();
8385}
8386
Johannes Berg55682962007-09-20 13:09:35 -04008387static struct genl_ops nl80211_ops[] = {
8388 {
8389 .cmd = NL80211_CMD_GET_WIPHY,
8390 .doit = nl80211_get_wiphy,
8391 .dumpit = nl80211_dump_wiphy,
8392 .policy = nl80211_policy,
8393 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008394 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04008395 },
8396 {
8397 .cmd = NL80211_CMD_SET_WIPHY,
8398 .doit = nl80211_set_wiphy,
8399 .policy = nl80211_policy,
8400 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008401 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008402 },
8403 {
8404 .cmd = NL80211_CMD_GET_INTERFACE,
8405 .doit = nl80211_get_interface,
8406 .dumpit = nl80211_dump_interface,
8407 .policy = nl80211_policy,
8408 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02008409 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04008410 },
8411 {
8412 .cmd = NL80211_CMD_SET_INTERFACE,
8413 .doit = nl80211_set_interface,
8414 .policy = nl80211_policy,
8415 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008416 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8417 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008418 },
8419 {
8420 .cmd = NL80211_CMD_NEW_INTERFACE,
8421 .doit = nl80211_new_interface,
8422 .policy = nl80211_policy,
8423 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008424 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8425 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008426 },
8427 {
8428 .cmd = NL80211_CMD_DEL_INTERFACE,
8429 .doit = nl80211_del_interface,
8430 .policy = nl80211_policy,
8431 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02008432 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008433 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008434 },
Johannes Berg41ade002007-12-19 02:03:29 +01008435 {
8436 .cmd = NL80211_CMD_GET_KEY,
8437 .doit = nl80211_get_key,
8438 .policy = nl80211_policy,
8439 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008440 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008441 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008442 },
8443 {
8444 .cmd = NL80211_CMD_SET_KEY,
8445 .doit = nl80211_set_key,
8446 .policy = nl80211_policy,
8447 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008448 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008449 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008450 },
8451 {
8452 .cmd = NL80211_CMD_NEW_KEY,
8453 .doit = nl80211_new_key,
8454 .policy = nl80211_policy,
8455 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008456 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008457 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008458 },
8459 {
8460 .cmd = NL80211_CMD_DEL_KEY,
8461 .doit = nl80211_del_key,
8462 .policy = nl80211_policy,
8463 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008464 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008465 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008466 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008467 {
8468 .cmd = NL80211_CMD_SET_BEACON,
8469 .policy = nl80211_policy,
8470 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008471 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008472 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008473 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008474 },
8475 {
Johannes Berg88600202012-02-13 15:17:18 +01008476 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008477 .policy = nl80211_policy,
8478 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008479 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008480 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008481 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008482 },
8483 {
Johannes Berg88600202012-02-13 15:17:18 +01008484 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008485 .policy = nl80211_policy,
8486 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008487 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008488 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008489 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008490 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008491 {
8492 .cmd = NL80211_CMD_GET_STATION,
8493 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008494 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008495 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008496 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8497 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008498 },
8499 {
8500 .cmd = NL80211_CMD_SET_STATION,
8501 .doit = nl80211_set_station,
8502 .policy = nl80211_policy,
8503 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008504 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008505 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008506 },
8507 {
8508 .cmd = NL80211_CMD_NEW_STATION,
8509 .doit = nl80211_new_station,
8510 .policy = nl80211_policy,
8511 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008512 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008513 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008514 },
8515 {
8516 .cmd = NL80211_CMD_DEL_STATION,
8517 .doit = nl80211_del_station,
8518 .policy = nl80211_policy,
8519 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008520 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008521 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008522 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008523 {
8524 .cmd = NL80211_CMD_GET_MPATH,
8525 .doit = nl80211_get_mpath,
8526 .dumpit = nl80211_dump_mpath,
8527 .policy = nl80211_policy,
8528 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008529 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008530 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008531 },
8532 {
8533 .cmd = NL80211_CMD_SET_MPATH,
8534 .doit = nl80211_set_mpath,
8535 .policy = nl80211_policy,
8536 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008537 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008538 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008539 },
8540 {
8541 .cmd = NL80211_CMD_NEW_MPATH,
8542 .doit = nl80211_new_mpath,
8543 .policy = nl80211_policy,
8544 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008545 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008546 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008547 },
8548 {
8549 .cmd = NL80211_CMD_DEL_MPATH,
8550 .doit = nl80211_del_mpath,
8551 .policy = nl80211_policy,
8552 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008553 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008554 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008555 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008556 {
8557 .cmd = NL80211_CMD_SET_BSS,
8558 .doit = nl80211_set_bss,
8559 .policy = nl80211_policy,
8560 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008561 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008562 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008563 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008564 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008565 .cmd = NL80211_CMD_GET_REG,
8566 .doit = nl80211_get_reg,
8567 .policy = nl80211_policy,
8568 /* can be retrieved by unprivileged users */
8569 },
8570 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008571 .cmd = NL80211_CMD_SET_REG,
8572 .doit = nl80211_set_reg,
8573 .policy = nl80211_policy,
8574 .flags = GENL_ADMIN_PERM,
8575 },
8576 {
8577 .cmd = NL80211_CMD_REQ_SET_REG,
8578 .doit = nl80211_req_set_reg,
8579 .policy = nl80211_policy,
8580 .flags = GENL_ADMIN_PERM,
8581 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008582 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008583 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8584 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008585 .policy = nl80211_policy,
8586 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008587 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008588 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008589 },
8590 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008591 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8592 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008593 .policy = nl80211_policy,
8594 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008595 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008596 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008597 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008598 {
Johannes Berg2a519312009-02-10 21:25:55 +01008599 .cmd = NL80211_CMD_TRIGGER_SCAN,
8600 .doit = nl80211_trigger_scan,
8601 .policy = nl80211_policy,
8602 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008603 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008604 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008605 },
8606 {
8607 .cmd = NL80211_CMD_GET_SCAN,
8608 .policy = nl80211_policy,
8609 .dumpit = nl80211_dump_scan,
8610 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008611 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008612 .cmd = NL80211_CMD_START_SCHED_SCAN,
8613 .doit = nl80211_start_sched_scan,
8614 .policy = nl80211_policy,
8615 .flags = GENL_ADMIN_PERM,
8616 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8617 NL80211_FLAG_NEED_RTNL,
8618 },
8619 {
8620 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8621 .doit = nl80211_stop_sched_scan,
8622 .policy = nl80211_policy,
8623 .flags = GENL_ADMIN_PERM,
8624 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8625 NL80211_FLAG_NEED_RTNL,
8626 },
8627 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008628 .cmd = NL80211_CMD_AUTHENTICATE,
8629 .doit = nl80211_authenticate,
8630 .policy = nl80211_policy,
8631 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008632 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008633 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008634 },
8635 {
8636 .cmd = NL80211_CMD_ASSOCIATE,
8637 .doit = nl80211_associate,
8638 .policy = nl80211_policy,
8639 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008640 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008641 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008642 },
8643 {
8644 .cmd = NL80211_CMD_DEAUTHENTICATE,
8645 .doit = nl80211_deauthenticate,
8646 .policy = nl80211_policy,
8647 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008648 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008649 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008650 },
8651 {
8652 .cmd = NL80211_CMD_DISASSOCIATE,
8653 .doit = nl80211_disassociate,
8654 .policy = nl80211_policy,
8655 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008656 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008657 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008658 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008659 {
8660 .cmd = NL80211_CMD_JOIN_IBSS,
8661 .doit = nl80211_join_ibss,
8662 .policy = nl80211_policy,
8663 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008664 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008665 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008666 },
8667 {
8668 .cmd = NL80211_CMD_LEAVE_IBSS,
8669 .doit = nl80211_leave_ibss,
8670 .policy = nl80211_policy,
8671 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008672 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008673 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008674 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008675#ifdef CONFIG_NL80211_TESTMODE
8676 {
8677 .cmd = NL80211_CMD_TESTMODE,
8678 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008679 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008680 .policy = nl80211_policy,
8681 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008682 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8683 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008684 },
8685#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008686 {
8687 .cmd = NL80211_CMD_CONNECT,
8688 .doit = nl80211_connect,
8689 .policy = nl80211_policy,
8690 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008691 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008692 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008693 },
8694 {
8695 .cmd = NL80211_CMD_DISCONNECT,
8696 .doit = nl80211_disconnect,
8697 .policy = nl80211_policy,
8698 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008699 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008700 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008701 },
Johannes Berg463d0182009-07-14 00:33:35 +02008702 {
8703 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8704 .doit = nl80211_wiphy_netns,
8705 .policy = nl80211_policy,
8706 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008707 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8708 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008709 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008710 {
8711 .cmd = NL80211_CMD_GET_SURVEY,
8712 .policy = nl80211_policy,
8713 .dumpit = nl80211_dump_survey,
8714 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008715 {
8716 .cmd = NL80211_CMD_SET_PMKSA,
8717 .doit = nl80211_setdel_pmksa,
8718 .policy = nl80211_policy,
8719 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008720 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008721 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008722 },
8723 {
8724 .cmd = NL80211_CMD_DEL_PMKSA,
8725 .doit = nl80211_setdel_pmksa,
8726 .policy = nl80211_policy,
8727 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008728 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008729 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008730 },
8731 {
8732 .cmd = NL80211_CMD_FLUSH_PMKSA,
8733 .doit = nl80211_flush_pmksa,
8734 .policy = nl80211_policy,
8735 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008736 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008737 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008738 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008739 {
8740 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8741 .doit = nl80211_remain_on_channel,
8742 .policy = nl80211_policy,
8743 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008744 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008745 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008746 },
8747 {
8748 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8749 .doit = nl80211_cancel_remain_on_channel,
8750 .policy = nl80211_policy,
8751 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008752 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008753 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008754 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008755 {
8756 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8757 .doit = nl80211_set_tx_bitrate_mask,
8758 .policy = nl80211_policy,
8759 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008760 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8761 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008762 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008763 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008764 .cmd = NL80211_CMD_REGISTER_FRAME,
8765 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008766 .policy = nl80211_policy,
8767 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008768 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008769 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008770 },
8771 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008772 .cmd = NL80211_CMD_FRAME,
8773 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008774 .policy = nl80211_policy,
8775 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008776 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008777 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008778 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008779 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008780 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8781 .doit = nl80211_tx_mgmt_cancel_wait,
8782 .policy = nl80211_policy,
8783 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008784 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008785 NL80211_FLAG_NEED_RTNL,
8786 },
8787 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008788 .cmd = NL80211_CMD_SET_POWER_SAVE,
8789 .doit = nl80211_set_power_save,
8790 .policy = nl80211_policy,
8791 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008792 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8793 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008794 },
8795 {
8796 .cmd = NL80211_CMD_GET_POWER_SAVE,
8797 .doit = nl80211_get_power_save,
8798 .policy = nl80211_policy,
8799 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008800 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8801 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008802 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008803 {
8804 .cmd = NL80211_CMD_SET_CQM,
8805 .doit = nl80211_set_cqm,
8806 .policy = nl80211_policy,
8807 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008808 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8809 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008810 },
Johannes Bergf444de02010-05-05 15:25:02 +02008811 {
8812 .cmd = NL80211_CMD_SET_CHANNEL,
8813 .doit = nl80211_set_channel,
8814 .policy = nl80211_policy,
8815 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008816 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8817 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008818 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008819 {
8820 .cmd = NL80211_CMD_SET_WDS_PEER,
8821 .doit = nl80211_set_wds_peer,
8822 .policy = nl80211_policy,
8823 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008824 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8825 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008826 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008827 {
8828 .cmd = NL80211_CMD_JOIN_MESH,
8829 .doit = nl80211_join_mesh,
8830 .policy = nl80211_policy,
8831 .flags = GENL_ADMIN_PERM,
8832 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8833 NL80211_FLAG_NEED_RTNL,
8834 },
8835 {
8836 .cmd = NL80211_CMD_LEAVE_MESH,
8837 .doit = nl80211_leave_mesh,
8838 .policy = nl80211_policy,
8839 .flags = GENL_ADMIN_PERM,
8840 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8841 NL80211_FLAG_NEED_RTNL,
8842 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008843#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008844 {
8845 .cmd = NL80211_CMD_GET_WOWLAN,
8846 .doit = nl80211_get_wowlan,
8847 .policy = nl80211_policy,
8848 /* can be retrieved by unprivileged users */
8849 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8850 NL80211_FLAG_NEED_RTNL,
8851 },
8852 {
8853 .cmd = NL80211_CMD_SET_WOWLAN,
8854 .doit = nl80211_set_wowlan,
8855 .policy = nl80211_policy,
8856 .flags = GENL_ADMIN_PERM,
8857 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8858 NL80211_FLAG_NEED_RTNL,
8859 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008860#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008861 {
8862 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8863 .doit = nl80211_set_rekey_data,
8864 .policy = nl80211_policy,
8865 .flags = GENL_ADMIN_PERM,
8866 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8867 NL80211_FLAG_NEED_RTNL,
8868 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008869 {
8870 .cmd = NL80211_CMD_TDLS_MGMT,
8871 .doit = nl80211_tdls_mgmt,
8872 .policy = nl80211_policy,
8873 .flags = GENL_ADMIN_PERM,
8874 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8875 NL80211_FLAG_NEED_RTNL,
8876 },
8877 {
8878 .cmd = NL80211_CMD_TDLS_OPER,
8879 .doit = nl80211_tdls_oper,
8880 .policy = nl80211_policy,
8881 .flags = GENL_ADMIN_PERM,
8882 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8883 NL80211_FLAG_NEED_RTNL,
8884 },
Johannes Berg28946da2011-11-04 11:18:12 +01008885 {
8886 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8887 .doit = nl80211_register_unexpected_frame,
8888 .policy = nl80211_policy,
8889 .flags = GENL_ADMIN_PERM,
8890 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8891 NL80211_FLAG_NEED_RTNL,
8892 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008893 {
8894 .cmd = NL80211_CMD_PROBE_CLIENT,
8895 .doit = nl80211_probe_client,
8896 .policy = nl80211_policy,
8897 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008898 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008899 NL80211_FLAG_NEED_RTNL,
8900 },
Johannes Berg5e760232011-11-04 11:18:17 +01008901 {
8902 .cmd = NL80211_CMD_REGISTER_BEACONS,
8903 .doit = nl80211_register_beacons,
8904 .policy = nl80211_policy,
8905 .flags = GENL_ADMIN_PERM,
8906 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8907 NL80211_FLAG_NEED_RTNL,
8908 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008909 {
8910 .cmd = NL80211_CMD_SET_NOACK_MAP,
8911 .doit = nl80211_set_noack_map,
8912 .policy = nl80211_policy,
8913 .flags = GENL_ADMIN_PERM,
8914 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8915 NL80211_FLAG_NEED_RTNL,
8916 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008917 {
8918 .cmd = NL80211_CMD_START_P2P_DEVICE,
8919 .doit = nl80211_start_p2p_device,
8920 .policy = nl80211_policy,
8921 .flags = GENL_ADMIN_PERM,
8922 .internal_flags = NL80211_FLAG_NEED_WDEV |
8923 NL80211_FLAG_NEED_RTNL,
8924 },
8925 {
8926 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8927 .doit = nl80211_stop_p2p_device,
8928 .policy = nl80211_policy,
8929 .flags = GENL_ADMIN_PERM,
8930 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8931 NL80211_FLAG_NEED_RTNL,
8932 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008933 {
8934 .cmd = NL80211_CMD_SET_MCAST_RATE,
8935 .doit = nl80211_set_mcast_rate,
8936 .policy = nl80211_policy,
8937 .flags = GENL_ADMIN_PERM,
8938 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8939 NL80211_FLAG_NEED_RTNL,
8940 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308941 {
8942 .cmd = NL80211_CMD_SET_MAC_ACL,
8943 .doit = nl80211_set_mac_acl,
8944 .policy = nl80211_policy,
8945 .flags = GENL_ADMIN_PERM,
8946 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8947 NL80211_FLAG_NEED_RTNL,
8948 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008949 {
8950 .cmd = NL80211_CMD_RADAR_DETECT,
8951 .doit = nl80211_start_radar_detection,
8952 .policy = nl80211_policy,
8953 .flags = GENL_ADMIN_PERM,
8954 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8955 NL80211_FLAG_NEED_RTNL,
8956 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01008957 {
8958 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
8959 .doit = nl80211_get_protocol_features,
8960 .policy = nl80211_policy,
8961 },
Jouni Malinen355199e2013-02-27 17:14:27 +02008962 {
8963 .cmd = NL80211_CMD_UPDATE_FT_IES,
8964 .doit = nl80211_update_ft_ies,
8965 .policy = nl80211_policy,
8966 .flags = GENL_ADMIN_PERM,
8967 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8968 NL80211_FLAG_NEED_RTNL,
8969 },
Arend van Spriel5de17982013-04-18 15:49:00 +02008970 {
8971 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
8972 .doit = nl80211_crit_protocol_start,
8973 .policy = nl80211_policy,
8974 .flags = GENL_ADMIN_PERM,
8975 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8976 NL80211_FLAG_NEED_RTNL,
8977 },
8978 {
8979 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
8980 .doit = nl80211_crit_protocol_stop,
8981 .policy = nl80211_policy,
8982 .flags = GENL_ADMIN_PERM,
8983 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8984 NL80211_FLAG_NEED_RTNL,
8985 }
Johannes Berg55682962007-09-20 13:09:35 -04008986};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008987
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008988static struct genl_multicast_group nl80211_mlme_mcgrp = {
8989 .name = "mlme",
8990};
Johannes Berg55682962007-09-20 13:09:35 -04008991
8992/* multicast groups */
8993static struct genl_multicast_group nl80211_config_mcgrp = {
8994 .name = "config",
8995};
Johannes Berg2a519312009-02-10 21:25:55 +01008996static struct genl_multicast_group nl80211_scan_mcgrp = {
8997 .name = "scan",
8998};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008999static struct genl_multicast_group nl80211_regulatory_mcgrp = {
9000 .name = "regulatory",
9001};
Johannes Berg55682962007-09-20 13:09:35 -04009002
9003/* notification functions */
9004
9005void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9006{
9007 struct sk_buff *msg;
9008
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009009 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009010 if (!msg)
9011 return;
9012
Johannes Berg3713b4e2013-02-14 16:19:38 +01009013 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
9014 false, NULL, NULL, NULL) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04009015 nlmsg_free(msg);
9016 return;
9017 }
9018
Johannes Berg463d0182009-07-14 00:33:35 +02009019 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9020 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009021}
9022
Johannes Berg362a4152009-05-24 16:43:15 +02009023static int nl80211_add_scan_req(struct sk_buff *msg,
9024 struct cfg80211_registered_device *rdev)
9025{
9026 struct cfg80211_scan_request *req = rdev->scan_req;
9027 struct nlattr *nest;
9028 int i;
9029
Johannes Bergf9f47522013-03-19 15:04:07 +01009030 lockdep_assert_held(&rdev->sched_scan_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +02009031
Johannes Berg362a4152009-05-24 16:43:15 +02009032 if (WARN_ON(!req))
9033 return 0;
9034
9035 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9036 if (!nest)
9037 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009038 for (i = 0; i < req->n_ssids; i++) {
9039 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9040 goto nla_put_failure;
9041 }
Johannes Berg362a4152009-05-24 16:43:15 +02009042 nla_nest_end(msg, nest);
9043
9044 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9045 if (!nest)
9046 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009047 for (i = 0; i < req->n_channels; i++) {
9048 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9049 goto nla_put_failure;
9050 }
Johannes Berg362a4152009-05-24 16:43:15 +02009051 nla_nest_end(msg, nest);
9052
David S. Miller9360ffd2012-03-29 04:41:26 -04009053 if (req->ie &&
9054 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9055 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02009056
Sam Lefflered4737712012-10-11 21:03:31 -07009057 if (req->flags)
9058 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
9059
Johannes Berg362a4152009-05-24 16:43:15 +02009060 return 0;
9061 nla_put_failure:
9062 return -ENOBUFS;
9063}
9064
Johannes Berga538e2d2009-06-16 19:56:42 +02009065static int nl80211_send_scan_msg(struct sk_buff *msg,
9066 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009067 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009068 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02009069 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01009070{
9071 void *hdr;
9072
Eric W. Biederman15e47302012-09-07 20:12:54 +00009073 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01009074 if (!hdr)
9075 return -1;
9076
David S. Miller9360ffd2012-03-29 04:41:26 -04009077 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009078 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9079 wdev->netdev->ifindex)) ||
9080 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009081 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009082
Johannes Berg362a4152009-05-24 16:43:15 +02009083 /* ignore errors and send incomplete event anyway */
9084 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009085
9086 return genlmsg_end(msg, hdr);
9087
9088 nla_put_failure:
9089 genlmsg_cancel(msg, hdr);
9090 return -EMSGSIZE;
9091}
9092
Luciano Coelho807f8a82011-05-11 17:09:35 +03009093static int
9094nl80211_send_sched_scan_msg(struct sk_buff *msg,
9095 struct cfg80211_registered_device *rdev,
9096 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009097 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009098{
9099 void *hdr;
9100
Eric W. Biederman15e47302012-09-07 20:12:54 +00009101 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009102 if (!hdr)
9103 return -1;
9104
David S. Miller9360ffd2012-03-29 04:41:26 -04009105 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9106 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9107 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009108
9109 return genlmsg_end(msg, hdr);
9110
9111 nla_put_failure:
9112 genlmsg_cancel(msg, hdr);
9113 return -EMSGSIZE;
9114}
9115
Johannes Berga538e2d2009-06-16 19:56:42 +02009116void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009117 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009118{
9119 struct sk_buff *msg;
9120
Thomas Graf58050fc2012-06-28 03:57:45 +00009121 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009122 if (!msg)
9123 return;
9124
Johannes Bergfd014282012-06-18 19:17:03 +02009125 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009126 NL80211_CMD_TRIGGER_SCAN) < 0) {
9127 nlmsg_free(msg);
9128 return;
9129 }
9130
Johannes Berg463d0182009-07-14 00:33:35 +02009131 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9132 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009133}
9134
Johannes Berg2a519312009-02-10 21:25:55 +01009135void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009136 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009137{
9138 struct sk_buff *msg;
9139
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009140 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009141 if (!msg)
9142 return;
9143
Johannes Bergfd014282012-06-18 19:17:03 +02009144 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009145 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009146 nlmsg_free(msg);
9147 return;
9148 }
9149
Johannes Berg463d0182009-07-14 00:33:35 +02009150 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9151 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009152}
9153
9154void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009155 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009156{
9157 struct sk_buff *msg;
9158
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009159 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009160 if (!msg)
9161 return;
9162
Johannes Bergfd014282012-06-18 19:17:03 +02009163 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009164 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009165 nlmsg_free(msg);
9166 return;
9167 }
9168
Johannes Berg463d0182009-07-14 00:33:35 +02009169 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9170 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009171}
9172
Luciano Coelho807f8a82011-05-11 17:09:35 +03009173void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9174 struct net_device *netdev)
9175{
9176 struct sk_buff *msg;
9177
9178 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9179 if (!msg)
9180 return;
9181
9182 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9183 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9184 nlmsg_free(msg);
9185 return;
9186 }
9187
9188 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9189 nl80211_scan_mcgrp.id, GFP_KERNEL);
9190}
9191
9192void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9193 struct net_device *netdev, u32 cmd)
9194{
9195 struct sk_buff *msg;
9196
Thomas Graf58050fc2012-06-28 03:57:45 +00009197 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009198 if (!msg)
9199 return;
9200
9201 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9202 nlmsg_free(msg);
9203 return;
9204 }
9205
9206 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9207 nl80211_scan_mcgrp.id, GFP_KERNEL);
9208}
9209
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009210/*
9211 * This can happen on global regulatory changes or device specific settings
9212 * based on custom world regulatory domains.
9213 */
9214void nl80211_send_reg_change_event(struct regulatory_request *request)
9215{
9216 struct sk_buff *msg;
9217 void *hdr;
9218
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009219 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009220 if (!msg)
9221 return;
9222
9223 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9224 if (!hdr) {
9225 nlmsg_free(msg);
9226 return;
9227 }
9228
9229 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04009230 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9231 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009232
David S. Miller9360ffd2012-03-29 04:41:26 -04009233 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9234 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9235 NL80211_REGDOM_TYPE_WORLD))
9236 goto nla_put_failure;
9237 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9238 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9239 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9240 goto nla_put_failure;
9241 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9242 request->intersect) {
9243 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9244 NL80211_REGDOM_TYPE_INTERSECTION))
9245 goto nla_put_failure;
9246 } else {
9247 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9248 NL80211_REGDOM_TYPE_COUNTRY) ||
9249 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9250 request->alpha2))
9251 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009252 }
9253
Johannes Bergf4173762012-12-03 18:23:37 +01009254 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04009255 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9256 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009257
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009258 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009259
Johannes Bergbc43b282009-07-25 10:54:13 +02009260 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009261 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009262 GFP_ATOMIC);
9263 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009264
9265 return;
9266
9267nla_put_failure:
9268 genlmsg_cancel(msg, hdr);
9269 nlmsg_free(msg);
9270}
9271
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009272static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9273 struct net_device *netdev,
9274 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009275 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009276{
9277 struct sk_buff *msg;
9278 void *hdr;
9279
Johannes Berge6d6e342009-07-01 21:26:47 +02009280 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009281 if (!msg)
9282 return;
9283
9284 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9285 if (!hdr) {
9286 nlmsg_free(msg);
9287 return;
9288 }
9289
David S. Miller9360ffd2012-03-29 04:41:26 -04009290 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9291 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9292 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9293 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009294
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009295 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009296
Johannes Berg463d0182009-07-14 00:33:35 +02009297 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9298 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009299 return;
9300
9301 nla_put_failure:
9302 genlmsg_cancel(msg, hdr);
9303 nlmsg_free(msg);
9304}
9305
9306void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009307 struct net_device *netdev, const u8 *buf,
9308 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009309{
9310 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009311 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009312}
9313
9314void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9315 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009316 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009317{
Johannes Berge6d6e342009-07-01 21:26:47 +02009318 nl80211_send_mlme_event(rdev, netdev, buf, len,
9319 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009320}
9321
Jouni Malinen53b46b82009-03-27 20:53:56 +02009322void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009323 struct net_device *netdev, const u8 *buf,
9324 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009325{
9326 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009327 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009328}
9329
Jouni Malinen53b46b82009-03-27 20:53:56 +02009330void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9331 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009332 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009333{
9334 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009335 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009336}
9337
Johannes Berg947add32013-02-22 22:05:20 +01009338void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
9339 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +02009340{
Johannes Berg947add32013-02-22 22:05:20 +01009341 struct wireless_dev *wdev = dev->ieee80211_ptr;
9342 struct wiphy *wiphy = wdev->wiphy;
9343 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009344
Johannes Berg947add32013-02-22 22:05:20 +01009345 trace_cfg80211_send_unprot_deauth(dev);
9346 nl80211_send_mlme_event(rdev, dev, buf, len,
9347 NL80211_CMD_UNPROT_DEAUTHENTICATE, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009348}
Johannes Berg947add32013-02-22 22:05:20 +01009349EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
9350
9351void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
9352 size_t len)
9353{
9354 struct wireless_dev *wdev = dev->ieee80211_ptr;
9355 struct wiphy *wiphy = wdev->wiphy;
9356 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9357
9358 trace_cfg80211_send_unprot_disassoc(dev);
9359 nl80211_send_mlme_event(rdev, dev, buf, len,
9360 NL80211_CMD_UNPROT_DISASSOCIATE, GFP_ATOMIC);
9361}
9362EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009363
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04009364static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9365 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02009366 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009367{
9368 struct sk_buff *msg;
9369 void *hdr;
9370
Johannes Berge6d6e342009-07-01 21:26:47 +02009371 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009372 if (!msg)
9373 return;
9374
9375 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9376 if (!hdr) {
9377 nlmsg_free(msg);
9378 return;
9379 }
9380
David S. Miller9360ffd2012-03-29 04:41:26 -04009381 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9382 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9383 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9384 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9385 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03009386
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009387 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03009388
Johannes Berg463d0182009-07-14 00:33:35 +02009389 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9390 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009391 return;
9392
9393 nla_put_failure:
9394 genlmsg_cancel(msg, hdr);
9395 nlmsg_free(msg);
9396}
9397
9398void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009399 struct net_device *netdev, const u8 *addr,
9400 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009401{
9402 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02009403 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009404}
9405
9406void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009407 struct net_device *netdev, const u8 *addr,
9408 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009409{
Johannes Berge6d6e342009-07-01 21:26:47 +02009410 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9411 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009412}
9413
Samuel Ortizb23aa672009-07-01 21:26:54 +02009414void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9415 struct net_device *netdev, const u8 *bssid,
9416 const u8 *req_ie, size_t req_ie_len,
9417 const u8 *resp_ie, size_t resp_ie_len,
9418 u16 status, gfp_t gfp)
9419{
9420 struct sk_buff *msg;
9421 void *hdr;
9422
Thomas Graf58050fc2012-06-28 03:57:45 +00009423 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009424 if (!msg)
9425 return;
9426
9427 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9428 if (!hdr) {
9429 nlmsg_free(msg);
9430 return;
9431 }
9432
David S. Miller9360ffd2012-03-29 04:41:26 -04009433 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9434 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9435 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9436 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9437 (req_ie &&
9438 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9439 (resp_ie &&
9440 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9441 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009442
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009443 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009444
Johannes Berg463d0182009-07-14 00:33:35 +02009445 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9446 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009447 return;
9448
9449 nla_put_failure:
9450 genlmsg_cancel(msg, hdr);
9451 nlmsg_free(msg);
9452
9453}
9454
9455void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9456 struct net_device *netdev, const u8 *bssid,
9457 const u8 *req_ie, size_t req_ie_len,
9458 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9459{
9460 struct sk_buff *msg;
9461 void *hdr;
9462
Thomas Graf58050fc2012-06-28 03:57:45 +00009463 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009464 if (!msg)
9465 return;
9466
9467 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9468 if (!hdr) {
9469 nlmsg_free(msg);
9470 return;
9471 }
9472
David S. Miller9360ffd2012-03-29 04:41:26 -04009473 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9474 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9475 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9476 (req_ie &&
9477 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9478 (resp_ie &&
9479 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9480 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009481
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009482 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009483
Johannes Berg463d0182009-07-14 00:33:35 +02009484 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9485 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009486 return;
9487
9488 nla_put_failure:
9489 genlmsg_cancel(msg, hdr);
9490 nlmsg_free(msg);
9491
9492}
9493
9494void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9495 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02009496 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009497{
9498 struct sk_buff *msg;
9499 void *hdr;
9500
Thomas Graf58050fc2012-06-28 03:57:45 +00009501 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009502 if (!msg)
9503 return;
9504
9505 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9506 if (!hdr) {
9507 nlmsg_free(msg);
9508 return;
9509 }
9510
David S. Miller9360ffd2012-03-29 04:41:26 -04009511 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9512 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9513 (from_ap && reason &&
9514 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9515 (from_ap &&
9516 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9517 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9518 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009519
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009520 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009521
Johannes Berg463d0182009-07-14 00:33:35 +02009522 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9523 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009524 return;
9525
9526 nla_put_failure:
9527 genlmsg_cancel(msg, hdr);
9528 nlmsg_free(msg);
9529
9530}
9531
Johannes Berg04a773a2009-04-19 21:24:32 +02009532void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9533 struct net_device *netdev, const u8 *bssid,
9534 gfp_t gfp)
9535{
9536 struct sk_buff *msg;
9537 void *hdr;
9538
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009539 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009540 if (!msg)
9541 return;
9542
9543 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9544 if (!hdr) {
9545 nlmsg_free(msg);
9546 return;
9547 }
9548
David S. Miller9360ffd2012-03-29 04:41:26 -04009549 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9550 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9551 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9552 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009553
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009554 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009555
Johannes Berg463d0182009-07-14 00:33:35 +02009556 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9557 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009558 return;
9559
9560 nla_put_failure:
9561 genlmsg_cancel(msg, hdr);
9562 nlmsg_free(msg);
9563}
9564
Johannes Berg947add32013-02-22 22:05:20 +01009565void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
9566 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -07009567{
Johannes Berg947add32013-02-22 22:05:20 +01009568 struct wireless_dev *wdev = dev->ieee80211_ptr;
9569 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009570 struct sk_buff *msg;
9571 void *hdr;
9572
Johannes Berg947add32013-02-22 22:05:20 +01009573 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
9574 return;
9575
9576 trace_cfg80211_notify_new_peer_candidate(dev, addr);
9577
Javier Cardonac93b5e72011-04-07 15:08:34 -07009578 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9579 if (!msg)
9580 return;
9581
9582 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9583 if (!hdr) {
9584 nlmsg_free(msg);
9585 return;
9586 }
9587
David S. Miller9360ffd2012-03-29 04:41:26 -04009588 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +01009589 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9590 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009591 (ie_len && ie &&
9592 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9593 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009594
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009595 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009596
9597 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9598 nl80211_mlme_mcgrp.id, gfp);
9599 return;
9600
9601 nla_put_failure:
9602 genlmsg_cancel(msg, hdr);
9603 nlmsg_free(msg);
9604}
Johannes Berg947add32013-02-22 22:05:20 +01009605EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009606
Jouni Malinena3b8b052009-03-27 21:59:49 +02009607void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9608 struct net_device *netdev, const u8 *addr,
9609 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009610 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009611{
9612 struct sk_buff *msg;
9613 void *hdr;
9614
Johannes Berge6d6e342009-07-01 21:26:47 +02009615 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009616 if (!msg)
9617 return;
9618
9619 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9620 if (!hdr) {
9621 nlmsg_free(msg);
9622 return;
9623 }
9624
David S. Miller9360ffd2012-03-29 04:41:26 -04009625 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9626 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9627 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9628 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9629 (key_id != -1 &&
9630 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9631 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9632 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009633
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009634 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009635
Johannes Berg463d0182009-07-14 00:33:35 +02009636 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9637 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009638 return;
9639
9640 nla_put_failure:
9641 genlmsg_cancel(msg, hdr);
9642 nlmsg_free(msg);
9643}
9644
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009645void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9646 struct ieee80211_channel *channel_before,
9647 struct ieee80211_channel *channel_after)
9648{
9649 struct sk_buff *msg;
9650 void *hdr;
9651 struct nlattr *nl_freq;
9652
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009653 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009654 if (!msg)
9655 return;
9656
9657 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9658 if (!hdr) {
9659 nlmsg_free(msg);
9660 return;
9661 }
9662
9663 /*
9664 * Since we are applying the beacon hint to a wiphy we know its
9665 * wiphy_idx is valid
9666 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009667 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9668 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009669
9670 /* Before */
9671 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9672 if (!nl_freq)
9673 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009674 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009675 goto nla_put_failure;
9676 nla_nest_end(msg, nl_freq);
9677
9678 /* After */
9679 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9680 if (!nl_freq)
9681 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009682 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009683 goto nla_put_failure;
9684 nla_nest_end(msg, nl_freq);
9685
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009686 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009687
Johannes Berg463d0182009-07-14 00:33:35 +02009688 rcu_read_lock();
9689 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9690 GFP_ATOMIC);
9691 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009692
9693 return;
9694
9695nla_put_failure:
9696 genlmsg_cancel(msg, hdr);
9697 nlmsg_free(msg);
9698}
9699
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009700static void nl80211_send_remain_on_chan_event(
9701 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009702 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009703 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009704 unsigned int duration, gfp_t gfp)
9705{
9706 struct sk_buff *msg;
9707 void *hdr;
9708
9709 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9710 if (!msg)
9711 return;
9712
9713 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9714 if (!hdr) {
9715 nlmsg_free(msg);
9716 return;
9717 }
9718
David S. Miller9360ffd2012-03-29 04:41:26 -04009719 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009720 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9721 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009722 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009723 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009724 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9725 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009726 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9727 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009728
David S. Miller9360ffd2012-03-29 04:41:26 -04009729 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9730 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9731 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009732
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009733 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009734
9735 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9736 nl80211_mlme_mcgrp.id, gfp);
9737 return;
9738
9739 nla_put_failure:
9740 genlmsg_cancel(msg, hdr);
9741 nlmsg_free(msg);
9742}
9743
Johannes Berg947add32013-02-22 22:05:20 +01009744void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9745 struct ieee80211_channel *chan,
9746 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009747{
Johannes Berg947add32013-02-22 22:05:20 +01009748 struct wiphy *wiphy = wdev->wiphy;
9749 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9750
9751 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009752 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009753 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009754 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009755}
Johannes Berg947add32013-02-22 22:05:20 +01009756EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009757
Johannes Berg947add32013-02-22 22:05:20 +01009758void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9759 struct ieee80211_channel *chan,
9760 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009761{
Johannes Berg947add32013-02-22 22:05:20 +01009762 struct wiphy *wiphy = wdev->wiphy;
9763 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9764
9765 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009766 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009767 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009768}
Johannes Berg947add32013-02-22 22:05:20 +01009769EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009770
Johannes Berg947add32013-02-22 22:05:20 +01009771void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
9772 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +01009773{
Johannes Berg947add32013-02-22 22:05:20 +01009774 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9775 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +01009776 struct sk_buff *msg;
9777
Johannes Berg947add32013-02-22 22:05:20 +01009778 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
9779
Thomas Graf58050fc2012-06-28 03:57:45 +00009780 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009781 if (!msg)
9782 return;
9783
John W. Linville66266b32012-03-15 13:25:41 -04009784 if (nl80211_send_station(msg, 0, 0, 0,
9785 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009786 nlmsg_free(msg);
9787 return;
9788 }
9789
9790 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9791 nl80211_mlme_mcgrp.id, gfp);
9792}
Johannes Berg947add32013-02-22 22:05:20 +01009793EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +01009794
Johannes Berg947add32013-02-22 22:05:20 +01009795void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +02009796{
Johannes Berg947add32013-02-22 22:05:20 +01009797 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9798 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +02009799 struct sk_buff *msg;
9800 void *hdr;
9801
Johannes Berg947add32013-02-22 22:05:20 +01009802 trace_cfg80211_del_sta(dev, mac_addr);
9803
Thomas Graf58050fc2012-06-28 03:57:45 +00009804 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009805 if (!msg)
9806 return;
9807
9808 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9809 if (!hdr) {
9810 nlmsg_free(msg);
9811 return;
9812 }
9813
David S. Miller9360ffd2012-03-29 04:41:26 -04009814 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9815 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9816 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009817
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009818 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009819
9820 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9821 nl80211_mlme_mcgrp.id, gfp);
9822 return;
9823
9824 nla_put_failure:
9825 genlmsg_cancel(msg, hdr);
9826 nlmsg_free(msg);
9827}
Johannes Berg947add32013-02-22 22:05:20 +01009828EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +02009829
Johannes Berg947add32013-02-22 22:05:20 +01009830void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
9831 enum nl80211_connect_failed_reason reason,
9832 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309833{
Johannes Berg947add32013-02-22 22:05:20 +01009834 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9835 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309836 struct sk_buff *msg;
9837 void *hdr;
9838
9839 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9840 if (!msg)
9841 return;
9842
9843 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9844 if (!hdr) {
9845 nlmsg_free(msg);
9846 return;
9847 }
9848
9849 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9850 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9851 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9852 goto nla_put_failure;
9853
9854 genlmsg_end(msg, hdr);
9855
9856 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9857 nl80211_mlme_mcgrp.id, gfp);
9858 return;
9859
9860 nla_put_failure:
9861 genlmsg_cancel(msg, hdr);
9862 nlmsg_free(msg);
9863}
Johannes Berg947add32013-02-22 22:05:20 +01009864EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309865
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009866static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9867 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009868{
9869 struct wireless_dev *wdev = dev->ieee80211_ptr;
9870 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9871 struct sk_buff *msg;
9872 void *hdr;
9873 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009874 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009875
Eric W. Biederman15e47302012-09-07 20:12:54 +00009876 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009877 return false;
9878
9879 msg = nlmsg_new(100, gfp);
9880 if (!msg)
9881 return true;
9882
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009883 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009884 if (!hdr) {
9885 nlmsg_free(msg);
9886 return true;
9887 }
9888
David S. Miller9360ffd2012-03-29 04:41:26 -04009889 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9890 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9891 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9892 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009893
9894 err = genlmsg_end(msg, hdr);
9895 if (err < 0) {
9896 nlmsg_free(msg);
9897 return true;
9898 }
9899
Eric W. Biederman15e47302012-09-07 20:12:54 +00009900 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009901 return true;
9902
9903 nla_put_failure:
9904 genlmsg_cancel(msg, hdr);
9905 nlmsg_free(msg);
9906 return true;
9907}
9908
Johannes Berg947add32013-02-22 22:05:20 +01009909bool cfg80211_rx_spurious_frame(struct net_device *dev,
9910 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009911{
Johannes Berg947add32013-02-22 22:05:20 +01009912 struct wireless_dev *wdev = dev->ieee80211_ptr;
9913 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009914
Johannes Berg947add32013-02-22 22:05:20 +01009915 trace_cfg80211_rx_spurious_frame(dev, addr);
9916
9917 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9918 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
9919 trace_cfg80211_return_bool(false);
9920 return false;
9921 }
9922 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9923 addr, gfp);
9924 trace_cfg80211_return_bool(ret);
9925 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009926}
Johannes Berg947add32013-02-22 22:05:20 +01009927EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
9928
9929bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
9930 const u8 *addr, gfp_t gfp)
9931{
9932 struct wireless_dev *wdev = dev->ieee80211_ptr;
9933 bool ret;
9934
9935 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
9936
9937 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9938 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
9939 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
9940 trace_cfg80211_return_bool(false);
9941 return false;
9942 }
9943 ret = __nl80211_unexpected_frame(dev,
9944 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9945 addr, gfp);
9946 trace_cfg80211_return_bool(ret);
9947 return ret;
9948}
9949EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009950
Johannes Berg2e161f72010-08-12 15:38:38 +02009951int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009952 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009953 int freq, int sig_dbm,
9954 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009955{
Johannes Berg71bbc992012-06-15 15:30:18 +02009956 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009957 struct sk_buff *msg;
9958 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009959
9960 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9961 if (!msg)
9962 return -ENOMEM;
9963
Johannes Berg2e161f72010-08-12 15:38:38 +02009964 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009965 if (!hdr) {
9966 nlmsg_free(msg);
9967 return -ENOMEM;
9968 }
9969
David S. Miller9360ffd2012-03-29 04:41:26 -04009970 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009971 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9972 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +03009973 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009974 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9975 (sig_dbm &&
9976 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9977 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9978 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009979
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009980 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009981
Eric W. Biederman15e47302012-09-07 20:12:54 +00009982 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009983
9984 nla_put_failure:
9985 genlmsg_cancel(msg, hdr);
9986 nlmsg_free(msg);
9987 return -ENOBUFS;
9988}
9989
Johannes Berg947add32013-02-22 22:05:20 +01009990void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
9991 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009992{
Johannes Berg947add32013-02-22 22:05:20 +01009993 struct wiphy *wiphy = wdev->wiphy;
9994 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +02009995 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009996 struct sk_buff *msg;
9997 void *hdr;
9998
Johannes Berg947add32013-02-22 22:05:20 +01009999 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10000
Jouni Malinen026331c2010-02-15 12:53:10 +020010001 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10002 if (!msg)
10003 return;
10004
Johannes Berg2e161f72010-08-12 15:38:38 +020010005 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010006 if (!hdr) {
10007 nlmsg_free(msg);
10008 return;
10009 }
10010
David S. Miller9360ffd2012-03-29 04:41:26 -040010011 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010012 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10013 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010014 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010015 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10016 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10017 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10018 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010019
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010020 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010021
10022 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
10023 return;
10024
10025 nla_put_failure:
10026 genlmsg_cancel(msg, hdr);
10027 nlmsg_free(msg);
10028}
Johannes Berg947add32013-02-22 22:05:20 +010010029EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020010030
Johannes Berg947add32013-02-22 22:05:20 +010010031void cfg80211_cqm_rssi_notify(struct net_device *dev,
10032 enum nl80211_cqm_rssi_threshold_event rssi_event,
10033 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010034{
Johannes Berg947add32013-02-22 22:05:20 +010010035 struct wireless_dev *wdev = dev->ieee80211_ptr;
10036 struct wiphy *wiphy = wdev->wiphy;
10037 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010038 struct sk_buff *msg;
10039 struct nlattr *pinfoattr;
10040 void *hdr;
10041
Johannes Berg947add32013-02-22 22:05:20 +010010042 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10043
Thomas Graf58050fc2012-06-28 03:57:45 +000010044 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010045 if (!msg)
10046 return;
10047
10048 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10049 if (!hdr) {
10050 nlmsg_free(msg);
10051 return;
10052 }
10053
David S. Miller9360ffd2012-03-29 04:41:26 -040010054 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010055 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040010056 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010057
10058 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10059 if (!pinfoattr)
10060 goto nla_put_failure;
10061
David S. Miller9360ffd2012-03-29 04:41:26 -040010062 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10063 rssi_event))
10064 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010065
10066 nla_nest_end(msg, pinfoattr);
10067
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010068 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010069
10070 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10071 nl80211_mlme_mcgrp.id, gfp);
10072 return;
10073
10074 nla_put_failure:
10075 genlmsg_cancel(msg, hdr);
10076 nlmsg_free(msg);
10077}
Johannes Berg947add32013-02-22 22:05:20 +010010078EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010079
Johannes Berg947add32013-02-22 22:05:20 +010010080static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10081 struct net_device *netdev, const u8 *bssid,
10082 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010083{
10084 struct sk_buff *msg;
10085 struct nlattr *rekey_attr;
10086 void *hdr;
10087
Thomas Graf58050fc2012-06-28 03:57:45 +000010088 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010089 if (!msg)
10090 return;
10091
10092 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10093 if (!hdr) {
10094 nlmsg_free(msg);
10095 return;
10096 }
10097
David S. Miller9360ffd2012-03-29 04:41:26 -040010098 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10099 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10100 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10101 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010102
10103 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10104 if (!rekey_attr)
10105 goto nla_put_failure;
10106
David S. Miller9360ffd2012-03-29 04:41:26 -040010107 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10108 NL80211_REPLAY_CTR_LEN, replay_ctr))
10109 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010110
10111 nla_nest_end(msg, rekey_attr);
10112
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010113 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010114
10115 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10116 nl80211_mlme_mcgrp.id, gfp);
10117 return;
10118
10119 nla_put_failure:
10120 genlmsg_cancel(msg, hdr);
10121 nlmsg_free(msg);
10122}
10123
Johannes Berg947add32013-02-22 22:05:20 +010010124void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10125 const u8 *replay_ctr, gfp_t gfp)
10126{
10127 struct wireless_dev *wdev = dev->ieee80211_ptr;
10128 struct wiphy *wiphy = wdev->wiphy;
10129 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10130
10131 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10132 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10133}
10134EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10135
10136static void
10137nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10138 struct net_device *netdev, int index,
10139 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010140{
10141 struct sk_buff *msg;
10142 struct nlattr *attr;
10143 void *hdr;
10144
Thomas Graf58050fc2012-06-28 03:57:45 +000010145 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010146 if (!msg)
10147 return;
10148
10149 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10150 if (!hdr) {
10151 nlmsg_free(msg);
10152 return;
10153 }
10154
David S. Miller9360ffd2012-03-29 04:41:26 -040010155 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10156 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10157 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010158
10159 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10160 if (!attr)
10161 goto nla_put_failure;
10162
David S. Miller9360ffd2012-03-29 04:41:26 -040010163 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10164 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10165 (preauth &&
10166 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10167 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010168
10169 nla_nest_end(msg, attr);
10170
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010171 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010172
10173 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10174 nl80211_mlme_mcgrp.id, gfp);
10175 return;
10176
10177 nla_put_failure:
10178 genlmsg_cancel(msg, hdr);
10179 nlmsg_free(msg);
10180}
10181
Johannes Berg947add32013-02-22 22:05:20 +010010182void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10183 const u8 *bssid, bool preauth, gfp_t gfp)
10184{
10185 struct wireless_dev *wdev = dev->ieee80211_ptr;
10186 struct wiphy *wiphy = wdev->wiphy;
10187 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10188
10189 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10190 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10191}
10192EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10193
10194static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10195 struct net_device *netdev,
10196 struct cfg80211_chan_def *chandef,
10197 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070010198{
10199 struct sk_buff *msg;
10200 void *hdr;
10201
Thomas Graf58050fc2012-06-28 03:57:45 +000010202 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070010203 if (!msg)
10204 return;
10205
10206 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10207 if (!hdr) {
10208 nlmsg_free(msg);
10209 return;
10210 }
10211
Johannes Berg683b6d32012-11-08 21:25:48 +010010212 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10213 goto nla_put_failure;
10214
10215 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040010216 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070010217
10218 genlmsg_end(msg, hdr);
10219
10220 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10221 nl80211_mlme_mcgrp.id, gfp);
10222 return;
10223
10224 nla_put_failure:
10225 genlmsg_cancel(msg, hdr);
10226 nlmsg_free(msg);
10227}
10228
Johannes Berg947add32013-02-22 22:05:20 +010010229void cfg80211_ch_switch_notify(struct net_device *dev,
10230 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070010231{
Johannes Berg947add32013-02-22 22:05:20 +010010232 struct wireless_dev *wdev = dev->ieee80211_ptr;
10233 struct wiphy *wiphy = wdev->wiphy;
10234 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10235
10236 trace_cfg80211_ch_switch_notify(dev, chandef);
10237
10238 wdev_lock(wdev);
10239
10240 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10241 wdev->iftype != NL80211_IFTYPE_P2P_GO))
10242 goto out;
10243
10244 wdev->channel = chandef->chan;
10245 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
10246out:
10247 wdev_unlock(wdev);
10248 return;
10249}
10250EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10251
10252void cfg80211_cqm_txe_notify(struct net_device *dev,
10253 const u8 *peer, u32 num_packets,
10254 u32 rate, u32 intvl, gfp_t gfp)
10255{
10256 struct wireless_dev *wdev = dev->ieee80211_ptr;
10257 struct wiphy *wiphy = wdev->wiphy;
10258 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010259 struct sk_buff *msg;
10260 struct nlattr *pinfoattr;
10261 void *hdr;
10262
10263 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10264 if (!msg)
10265 return;
10266
10267 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10268 if (!hdr) {
10269 nlmsg_free(msg);
10270 return;
10271 }
10272
10273 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010274 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070010275 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10276 goto nla_put_failure;
10277
10278 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10279 if (!pinfoattr)
10280 goto nla_put_failure;
10281
10282 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10283 goto nla_put_failure;
10284
10285 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10286 goto nla_put_failure;
10287
10288 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10289 goto nla_put_failure;
10290
10291 nla_nest_end(msg, pinfoattr);
10292
10293 genlmsg_end(msg, hdr);
10294
10295 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10296 nl80211_mlme_mcgrp.id, gfp);
10297 return;
10298
10299 nla_put_failure:
10300 genlmsg_cancel(msg, hdr);
10301 nlmsg_free(msg);
10302}
Johannes Berg947add32013-02-22 22:05:20 +010010303EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010304
10305void
Simon Wunderlich04f39042013-02-08 18:16:19 +010010306nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10307 struct cfg80211_chan_def *chandef,
10308 enum nl80211_radar_event event,
10309 struct net_device *netdev, gfp_t gfp)
10310{
10311 struct sk_buff *msg;
10312 void *hdr;
10313
10314 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10315 if (!msg)
10316 return;
10317
10318 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
10319 if (!hdr) {
10320 nlmsg_free(msg);
10321 return;
10322 }
10323
10324 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10325 goto nla_put_failure;
10326
10327 /* NOP and radar events don't need a netdev parameter */
10328 if (netdev) {
10329 struct wireless_dev *wdev = netdev->ieee80211_ptr;
10330
10331 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10332 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10333 goto nla_put_failure;
10334 }
10335
10336 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
10337 goto nla_put_failure;
10338
10339 if (nl80211_send_chandef(msg, chandef))
10340 goto nla_put_failure;
10341
10342 if (genlmsg_end(msg, hdr) < 0) {
10343 nlmsg_free(msg);
10344 return;
10345 }
10346
10347 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10348 nl80211_mlme_mcgrp.id, gfp);
10349 return;
10350
10351 nla_put_failure:
10352 genlmsg_cancel(msg, hdr);
10353 nlmsg_free(msg);
10354}
10355
Johannes Berg947add32013-02-22 22:05:20 +010010356void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10357 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010010358{
Johannes Berg947add32013-02-22 22:05:20 +010010359 struct wireless_dev *wdev = dev->ieee80211_ptr;
10360 struct wiphy *wiphy = wdev->wiphy;
10361 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010362 struct sk_buff *msg;
10363 struct nlattr *pinfoattr;
10364 void *hdr;
10365
Johannes Berg947add32013-02-22 22:05:20 +010010366 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
10367
Thomas Graf58050fc2012-06-28 03:57:45 +000010368 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010369 if (!msg)
10370 return;
10371
10372 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10373 if (!hdr) {
10374 nlmsg_free(msg);
10375 return;
10376 }
10377
David S. Miller9360ffd2012-03-29 04:41:26 -040010378 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010379 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010380 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10381 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010382
10383 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10384 if (!pinfoattr)
10385 goto nla_put_failure;
10386
David S. Miller9360ffd2012-03-29 04:41:26 -040010387 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10388 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010389
10390 nla_nest_end(msg, pinfoattr);
10391
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010392 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010393
10394 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10395 nl80211_mlme_mcgrp.id, gfp);
10396 return;
10397
10398 nla_put_failure:
10399 genlmsg_cancel(msg, hdr);
10400 nlmsg_free(msg);
10401}
Johannes Berg947add32013-02-22 22:05:20 +010010402EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010403
Johannes Berg7f6cf312011-11-04 11:18:15 +010010404void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10405 u64 cookie, bool acked, gfp_t gfp)
10406{
10407 struct wireless_dev *wdev = dev->ieee80211_ptr;
10408 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10409 struct sk_buff *msg;
10410 void *hdr;
10411 int err;
10412
Beni Lev4ee3e062012-08-27 12:49:39 +030010413 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10414
Thomas Graf58050fc2012-06-28 03:57:45 +000010415 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030010416
Johannes Berg7f6cf312011-11-04 11:18:15 +010010417 if (!msg)
10418 return;
10419
10420 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10421 if (!hdr) {
10422 nlmsg_free(msg);
10423 return;
10424 }
10425
David S. Miller9360ffd2012-03-29 04:41:26 -040010426 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10427 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10428 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10429 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10430 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10431 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010432
10433 err = genlmsg_end(msg, hdr);
10434 if (err < 0) {
10435 nlmsg_free(msg);
10436 return;
10437 }
10438
10439 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10440 nl80211_mlme_mcgrp.id, gfp);
10441 return;
10442
10443 nla_put_failure:
10444 genlmsg_cancel(msg, hdr);
10445 nlmsg_free(msg);
10446}
10447EXPORT_SYMBOL(cfg80211_probe_status);
10448
Johannes Berg5e760232011-11-04 11:18:17 +010010449void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10450 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070010451 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010010452{
10453 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10454 struct sk_buff *msg;
10455 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070010456 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010010457
Beni Lev4ee3e062012-08-27 12:49:39 +030010458 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10459
Ben Greear37c73b52012-10-26 14:49:25 -070010460 spin_lock_bh(&rdev->beacon_registrations_lock);
10461 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10462 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10463 if (!msg) {
10464 spin_unlock_bh(&rdev->beacon_registrations_lock);
10465 return;
10466 }
Johannes Berg5e760232011-11-04 11:18:17 +010010467
Ben Greear37c73b52012-10-26 14:49:25 -070010468 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10469 if (!hdr)
10470 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010010471
Ben Greear37c73b52012-10-26 14:49:25 -070010472 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10473 (freq &&
10474 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10475 (sig_dbm &&
10476 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10477 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10478 goto nla_put_failure;
10479
10480 genlmsg_end(msg, hdr);
10481
10482 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010010483 }
Ben Greear37c73b52012-10-26 14:49:25 -070010484 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010485 return;
10486
10487 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070010488 spin_unlock_bh(&rdev->beacon_registrations_lock);
10489 if (hdr)
10490 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010010491 nlmsg_free(msg);
10492}
10493EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10494
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010495#ifdef CONFIG_PM
10496void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10497 struct cfg80211_wowlan_wakeup *wakeup,
10498 gfp_t gfp)
10499{
10500 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10501 struct sk_buff *msg;
10502 void *hdr;
10503 int err, size = 200;
10504
10505 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10506
10507 if (wakeup)
10508 size += wakeup->packet_present_len;
10509
10510 msg = nlmsg_new(size, gfp);
10511 if (!msg)
10512 return;
10513
10514 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10515 if (!hdr)
10516 goto free_msg;
10517
10518 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10519 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10520 goto free_msg;
10521
10522 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10523 wdev->netdev->ifindex))
10524 goto free_msg;
10525
10526 if (wakeup) {
10527 struct nlattr *reasons;
10528
10529 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10530
10531 if (wakeup->disconnect &&
10532 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10533 goto free_msg;
10534 if (wakeup->magic_pkt &&
10535 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10536 goto free_msg;
10537 if (wakeup->gtk_rekey_failure &&
10538 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10539 goto free_msg;
10540 if (wakeup->eap_identity_req &&
10541 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10542 goto free_msg;
10543 if (wakeup->four_way_handshake &&
10544 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10545 goto free_msg;
10546 if (wakeup->rfkill_release &&
10547 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10548 goto free_msg;
10549
10550 if (wakeup->pattern_idx >= 0 &&
10551 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10552 wakeup->pattern_idx))
10553 goto free_msg;
10554
Johannes Berg2a0e0472013-01-23 22:57:40 +010010555 if (wakeup->tcp_match)
10556 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10557
10558 if (wakeup->tcp_connlost)
10559 nla_put_flag(msg,
10560 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10561
10562 if (wakeup->tcp_nomoretokens)
10563 nla_put_flag(msg,
10564 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10565
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010566 if (wakeup->packet) {
10567 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10568 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10569
10570 if (!wakeup->packet_80211) {
10571 pkt_attr =
10572 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10573 len_attr =
10574 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10575 }
10576
10577 if (wakeup->packet_len &&
10578 nla_put_u32(msg, len_attr, wakeup->packet_len))
10579 goto free_msg;
10580
10581 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10582 wakeup->packet))
10583 goto free_msg;
10584 }
10585
10586 nla_nest_end(msg, reasons);
10587 }
10588
10589 err = genlmsg_end(msg, hdr);
10590 if (err < 0)
10591 goto free_msg;
10592
10593 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10594 nl80211_mlme_mcgrp.id, gfp);
10595 return;
10596
10597 free_msg:
10598 nlmsg_free(msg);
10599}
10600EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10601#endif
10602
Jouni Malinen3475b092012-11-16 22:49:57 +020010603void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10604 enum nl80211_tdls_operation oper,
10605 u16 reason_code, gfp_t gfp)
10606{
10607 struct wireless_dev *wdev = dev->ieee80211_ptr;
10608 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10609 struct sk_buff *msg;
10610 void *hdr;
10611 int err;
10612
10613 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10614 reason_code);
10615
10616 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10617 if (!msg)
10618 return;
10619
10620 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10621 if (!hdr) {
10622 nlmsg_free(msg);
10623 return;
10624 }
10625
10626 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10627 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10628 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10629 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10630 (reason_code > 0 &&
10631 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10632 goto nla_put_failure;
10633
10634 err = genlmsg_end(msg, hdr);
10635 if (err < 0) {
10636 nlmsg_free(msg);
10637 return;
10638 }
10639
10640 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10641 nl80211_mlme_mcgrp.id, gfp);
10642 return;
10643
10644 nla_put_failure:
10645 genlmsg_cancel(msg, hdr);
10646 nlmsg_free(msg);
10647}
10648EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10649
Jouni Malinen026331c2010-02-15 12:53:10 +020010650static int nl80211_netlink_notify(struct notifier_block * nb,
10651 unsigned long state,
10652 void *_notify)
10653{
10654 struct netlink_notify *notify = _notify;
10655 struct cfg80211_registered_device *rdev;
10656 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010657 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010658
10659 if (state != NETLINK_URELEASE)
10660 return NOTIFY_DONE;
10661
10662 rcu_read_lock();
10663
Johannes Berg5e760232011-11-04 11:18:17 +010010664 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010665 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010666 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010667
10668 spin_lock_bh(&rdev->beacon_registrations_lock);
10669 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10670 list) {
10671 if (reg->nlportid == notify->portid) {
10672 list_del(&reg->list);
10673 kfree(reg);
10674 break;
10675 }
10676 }
10677 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010678 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010679
10680 rcu_read_unlock();
10681
10682 return NOTIFY_DONE;
10683}
10684
10685static struct notifier_block nl80211_netlink_notifier = {
10686 .notifier_call = nl80211_netlink_notify,
10687};
10688
Jouni Malinen355199e2013-02-27 17:14:27 +020010689void cfg80211_ft_event(struct net_device *netdev,
10690 struct cfg80211_ft_event_params *ft_event)
10691{
10692 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
10693 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10694 struct sk_buff *msg;
10695 void *hdr;
10696 int err;
10697
10698 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
10699
10700 if (!ft_event->target_ap)
10701 return;
10702
10703 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10704 if (!msg)
10705 return;
10706
10707 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
10708 if (!hdr) {
10709 nlmsg_free(msg);
10710 return;
10711 }
10712
10713 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
10714 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
10715 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
10716 if (ft_event->ies)
10717 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
10718 if (ft_event->ric_ies)
10719 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
10720 ft_event->ric_ies);
10721
10722 err = genlmsg_end(msg, hdr);
10723 if (err < 0) {
10724 nlmsg_free(msg);
10725 return;
10726 }
10727
10728 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10729 nl80211_mlme_mcgrp.id, GFP_KERNEL);
10730}
10731EXPORT_SYMBOL(cfg80211_ft_event);
10732
Arend van Spriel5de17982013-04-18 15:49:00 +020010733void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
10734{
10735 struct cfg80211_registered_device *rdev;
10736 struct sk_buff *msg;
10737 void *hdr;
10738 u32 nlportid;
10739
10740 rdev = wiphy_to_dev(wdev->wiphy);
10741 if (!rdev->crit_proto_nlportid)
10742 return;
10743
10744 nlportid = rdev->crit_proto_nlportid;
10745 rdev->crit_proto_nlportid = 0;
10746
10747 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10748 if (!msg)
10749 return;
10750
10751 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
10752 if (!hdr)
10753 goto nla_put_failure;
10754
10755 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10756 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10757 goto nla_put_failure;
10758
10759 genlmsg_end(msg, hdr);
10760
10761 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
10762 return;
10763
10764 nla_put_failure:
10765 if (hdr)
10766 genlmsg_cancel(msg, hdr);
10767 nlmsg_free(msg);
10768
10769}
10770EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
10771
Johannes Berg55682962007-09-20 13:09:35 -040010772/* initialisation/exit functions */
10773
10774int nl80211_init(void)
10775{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010776 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010777
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010778 err = genl_register_family_with_ops(&nl80211_fam,
10779 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010780 if (err)
10781 return err;
10782
Johannes Berg55682962007-09-20 13:09:35 -040010783 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10784 if (err)
10785 goto err_out;
10786
Johannes Berg2a519312009-02-10 21:25:55 +010010787 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10788 if (err)
10789 goto err_out;
10790
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010791 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10792 if (err)
10793 goto err_out;
10794
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010795 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10796 if (err)
10797 goto err_out;
10798
Johannes Bergaff89a92009-07-01 21:26:51 +020010799#ifdef CONFIG_NL80211_TESTMODE
10800 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10801 if (err)
10802 goto err_out;
10803#endif
10804
Jouni Malinen026331c2010-02-15 12:53:10 +020010805 err = netlink_register_notifier(&nl80211_netlink_notifier);
10806 if (err)
10807 goto err_out;
10808
Johannes Berg55682962007-09-20 13:09:35 -040010809 return 0;
10810 err_out:
10811 genl_unregister_family(&nl80211_fam);
10812 return err;
10813}
10814
10815void nl80211_exit(void)
10816{
Jouni Malinen026331c2010-02-15 12:53:10 +020010817 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010818 genl_unregister_family(&nl80211_fam);
10819}