blob: 3abcbbada6d42b729a62b682cea9b4df1af98fe4 [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);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001427
Kalle Valo4745fc02011-11-17 19:06:10 +02001428#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001429 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001430#endif
1431
Johannes Berg8fdc6212009-03-14 09:34:01 +01001432#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001433
Johannes Berg3713b4e2013-02-14 16:19:38 +01001434 if (dev->ops->connect || dev->ops->auth) {
1435 i++;
1436 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001437 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001438 }
1439
Johannes Berg3713b4e2013-02-14 16:19:38 +01001440 if (dev->ops->disconnect || dev->ops->deauth) {
1441 i++;
1442 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1443 goto nla_put_failure;
1444 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001445
Johannes Berg3713b4e2013-02-14 16:19:38 +01001446 nla_nest_end(msg, nl_cmds);
1447 (*split_start)++;
1448 if (split)
1449 break;
1450 case 5:
1451 if (dev->ops->remain_on_channel &&
1452 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1453 nla_put_u32(msg,
1454 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1455 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001456 goto nla_put_failure;
1457
Johannes Berg3713b4e2013-02-14 16:19:38 +01001458 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1459 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1460 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001461
Johannes Berg3713b4e2013-02-14 16:19:38 +01001462 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1463 goto nla_put_failure;
1464 (*split_start)++;
1465 if (split)
1466 break;
1467 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001468#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001469 if (nl80211_send_wowlan(msg, dev, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001470 goto nla_put_failure;
1471 (*split_start)++;
1472 if (split)
1473 break;
1474#else
1475 (*split_start)++;
1476#endif
1477 case 7:
1478 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1479 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001480 goto nla_put_failure;
1481
Johannes Bergcdc89b92013-02-18 23:54:36 +01001482 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001483 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001484
Johannes Berg3713b4e2013-02-14 16:19:38 +01001485 (*split_start)++;
1486 if (split)
1487 break;
1488 case 8:
1489 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1490 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1491 dev->wiphy.ap_sme_capa))
1492 goto nla_put_failure;
1493
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001494 features = dev->wiphy.features;
1495 /*
1496 * We can only add the per-channel limit information if the
1497 * dump is split, otherwise it makes it too big. Therefore
1498 * only advertise it in that case.
1499 */
1500 if (split)
1501 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1502 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001503 goto nla_put_failure;
1504
1505 if (dev->wiphy.ht_capa_mod_mask &&
1506 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1507 sizeof(*dev->wiphy.ht_capa_mod_mask),
1508 dev->wiphy.ht_capa_mod_mask))
1509 goto nla_put_failure;
1510
1511 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1512 dev->wiphy.max_acl_mac_addrs &&
1513 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1514 dev->wiphy.max_acl_mac_addrs))
1515 goto nla_put_failure;
1516
1517 /*
1518 * Any information below this point is only available to
1519 * applications that can deal with it being split. This
1520 * helps ensure that newly added capabilities don't break
1521 * older tools by overrunning their buffers.
1522 *
1523 * We still increment split_start so that in the split
1524 * case we'll continue with more data in the next round,
1525 * but break unconditionally so unsplit data stops here.
1526 */
1527 (*split_start)++;
1528 break;
1529 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001530 if (dev->wiphy.extended_capabilities &&
1531 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1532 dev->wiphy.extended_capabilities_len,
1533 dev->wiphy.extended_capabilities) ||
1534 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1535 dev->wiphy.extended_capabilities_len,
1536 dev->wiphy.extended_capabilities_mask)))
1537 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001538
Johannes Bergee2aca32013-02-21 17:36:01 +01001539 if (dev->wiphy.vht_capa_mod_mask &&
1540 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1541 sizeof(*dev->wiphy.vht_capa_mod_mask),
1542 dev->wiphy.vht_capa_mod_mask))
1543 goto nla_put_failure;
1544
Johannes Berg3713b4e2013-02-14 16:19:38 +01001545 /* done */
1546 *split_start = 0;
1547 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001548 }
Johannes Berg55682962007-09-20 13:09:35 -04001549 return genlmsg_end(msg, hdr);
1550
1551 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001552 genlmsg_cancel(msg, hdr);
1553 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001554}
1555
1556static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1557{
Johannes Berg645e77d2013-03-01 14:03:49 +01001558 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001559 int start = cb->args[0];
1560 struct cfg80211_registered_device *dev;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001561 s64 filter_wiphy = -1;
1562 bool split = false;
1563 struct nlattr **tb = nl80211_fam.attrbuf;
1564 int res;
Johannes Berg55682962007-09-20 13:09:35 -04001565
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001566 mutex_lock(&cfg80211_mutex);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001567 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1568 tb, nl80211_fam.maxattr, nl80211_policy);
1569 if (res == 0) {
1570 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1571 if (tb[NL80211_ATTR_WIPHY])
1572 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1573 if (tb[NL80211_ATTR_WDEV])
1574 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1575 if (tb[NL80211_ATTR_IFINDEX]) {
1576 struct net_device *netdev;
1577 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1578
1579 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1580 if (!netdev) {
1581 mutex_unlock(&cfg80211_mutex);
1582 return -ENODEV;
1583 }
1584 if (netdev->ieee80211_ptr) {
1585 dev = wiphy_to_dev(
1586 netdev->ieee80211_ptr->wiphy);
1587 filter_wiphy = dev->wiphy_idx;
1588 }
1589 dev_put(netdev);
1590 }
1591 }
1592
Johannes Berg79c97e92009-07-07 03:56:12 +02001593 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001594 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1595 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001596 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001597 continue;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001598 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1599 continue;
1600 /* attempt to fit multiple wiphy data chunks into the skb */
1601 do {
1602 ret = nl80211_send_wiphy(dev, skb,
1603 NETLINK_CB(cb->skb).portid,
1604 cb->nlh->nlmsg_seq,
1605 NLM_F_MULTI,
1606 split, &cb->args[1],
1607 &cb->args[2],
1608 &cb->args[3]);
1609 if (ret < 0) {
1610 /*
1611 * If sending the wiphy data didn't fit (ENOBUFS
1612 * or EMSGSIZE returned), this SKB is still
1613 * empty (so it's not too big because another
1614 * wiphy dataset is already in the skb) and
1615 * we've not tried to adjust the dump allocation
1616 * yet ... then adjust the alloc size to be
1617 * bigger, and return 1 but with the empty skb.
1618 * This results in an empty message being RX'ed
1619 * in userspace, but that is ignored.
1620 *
1621 * We can then retry with the larger buffer.
1622 */
1623 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1624 !skb->len &&
1625 cb->min_dump_alloc < 4096) {
1626 cb->min_dump_alloc = 4096;
1627 mutex_unlock(&cfg80211_mutex);
1628 return 1;
1629 }
1630 idx--;
1631 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001632 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001633 } while (cb->args[1] > 0);
1634 break;
Johannes Berg55682962007-09-20 13:09:35 -04001635 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001636 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001637
1638 cb->args[0] = idx;
1639
1640 return skb->len;
1641}
1642
1643static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1644{
1645 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001646 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001647
Johannes Berg645e77d2013-03-01 14:03:49 +01001648 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001649 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001650 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001651
Johannes Berg3713b4e2013-02-14 16:19:38 +01001652 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1653 false, NULL, NULL, NULL) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001654 nlmsg_free(msg);
1655 return -ENOBUFS;
1656 }
Johannes Berg55682962007-09-20 13:09:35 -04001657
Johannes Berg134e6372009-07-10 09:51:34 +00001658 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001659}
1660
Jouni Malinen31888482008-10-30 16:59:24 +02001661static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1662 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1663 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1664 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1665 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1666 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1667};
1668
1669static int parse_txq_params(struct nlattr *tb[],
1670 struct ieee80211_txq_params *txq_params)
1671{
Johannes Berga3304b02012-03-28 11:04:24 +02001672 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001673 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1674 !tb[NL80211_TXQ_ATTR_AIFS])
1675 return -EINVAL;
1676
Johannes Berga3304b02012-03-28 11:04:24 +02001677 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001678 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1679 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1680 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1681 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1682
Johannes Berga3304b02012-03-28 11:04:24 +02001683 if (txq_params->ac >= NL80211_NUM_ACS)
1684 return -EINVAL;
1685
Jouni Malinen31888482008-10-30 16:59:24 +02001686 return 0;
1687}
1688
Johannes Bergf444de02010-05-05 15:25:02 +02001689static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1690{
1691 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001692 * You can only set the channel explicitly for WDS interfaces,
1693 * all others have their channel managed via their respective
1694 * "establish a connection" command (connect, join, ...)
1695 *
1696 * For AP/GO and mesh mode, the channel can be set with the
1697 * channel userspace API, but is only stored and passed to the
1698 * low-level driver when the AP starts or the mesh is joined.
1699 * This is for backward compatibility, userspace can also give
1700 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001701 *
1702 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001703 * whatever else is going on, so they have their own special
1704 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001705 */
1706 return !wdev ||
1707 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001708 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001709 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1710 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001711}
1712
Johannes Berg683b6d32012-11-08 21:25:48 +01001713static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1714 struct genl_info *info,
1715 struct cfg80211_chan_def *chandef)
1716{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301717 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001718
1719 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1720 return -EINVAL;
1721
1722 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1723
1724 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001725 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1726 chandef->center_freq1 = control_freq;
1727 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001728
1729 /* Primary channel not allowed */
1730 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1731 return -EINVAL;
1732
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001733 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1734 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001735
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001736 chantype = nla_get_u32(
1737 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1738
1739 switch (chantype) {
1740 case NL80211_CHAN_NO_HT:
1741 case NL80211_CHAN_HT20:
1742 case NL80211_CHAN_HT40PLUS:
1743 case NL80211_CHAN_HT40MINUS:
1744 cfg80211_chandef_create(chandef, chandef->chan,
1745 chantype);
1746 break;
1747 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001748 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001749 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001750 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1751 chandef->width =
1752 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1753 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1754 chandef->center_freq1 =
1755 nla_get_u32(
1756 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1757 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1758 chandef->center_freq2 =
1759 nla_get_u32(
1760 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1761 }
1762
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001763 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001764 return -EINVAL;
1765
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001766 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1767 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001768 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001769
Johannes Berg683b6d32012-11-08 21:25:48 +01001770 return 0;
1771}
1772
Johannes Bergf444de02010-05-05 15:25:02 +02001773static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1774 struct wireless_dev *wdev,
1775 struct genl_info *info)
1776{
Johannes Berg683b6d32012-11-08 21:25:48 +01001777 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001778 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001779 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1780
1781 if (wdev)
1782 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001783
Johannes Bergf444de02010-05-05 15:25:02 +02001784 if (!nl80211_can_set_dev_channel(wdev))
1785 return -EOPNOTSUPP;
1786
Johannes Berg683b6d32012-11-08 21:25:48 +01001787 result = nl80211_parse_chandef(rdev, info, &chandef);
1788 if (result)
1789 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001790
1791 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001792 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001793 case NL80211_IFTYPE_AP:
1794 case NL80211_IFTYPE_P2P_GO:
1795 if (wdev->beacon_interval) {
1796 result = -EBUSY;
1797 break;
1798 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001799 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001800 result = -EINVAL;
1801 break;
1802 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001803 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001804 result = 0;
1805 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001806 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001807 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001808 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001809 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001810 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001811 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001812 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001813 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001814 }
1815 mutex_unlock(&rdev->devlist_mtx);
1816
1817 return result;
1818}
1819
1820static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1821{
Johannes Berg4c476992010-10-04 21:36:35 +02001822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1823 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001824
Johannes Berg4c476992010-10-04 21:36:35 +02001825 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001826}
1827
Bill Jordane8347eb2010-10-01 13:54:28 -04001828static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1829{
Johannes Berg43b19952010-10-07 13:10:30 +02001830 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1831 struct net_device *dev = info->user_ptr[1];
1832 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001833 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001834
1835 if (!info->attrs[NL80211_ATTR_MAC])
1836 return -EINVAL;
1837
Johannes Berg43b19952010-10-07 13:10:30 +02001838 if (netif_running(dev))
1839 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001840
Johannes Berg43b19952010-10-07 13:10:30 +02001841 if (!rdev->ops->set_wds_peer)
1842 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001843
Johannes Berg43b19952010-10-07 13:10:30 +02001844 if (wdev->iftype != NL80211_IFTYPE_WDS)
1845 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001846
1847 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001848 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001849}
1850
1851
Johannes Berg55682962007-09-20 13:09:35 -04001852static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1853{
1854 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001855 struct net_device *netdev = NULL;
1856 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001857 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001858 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001859 u32 changed;
1860 u8 retry_short = 0, retry_long = 0;
1861 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001862 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001863
Johannes Bergf444de02010-05-05 15:25:02 +02001864 /*
1865 * Try to find the wiphy and netdev. Normally this
1866 * function shouldn't need the netdev, but this is
1867 * done for backward compatibility -- previously
1868 * setting the channel was done per wiphy, but now
1869 * it is per netdev. Previous userland like hostapd
1870 * also passed a netdev to set_wiphy, so that it is
1871 * possible to let that go to the right netdev!
1872 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001873 mutex_lock(&cfg80211_mutex);
1874
Johannes Bergf444de02010-05-05 15:25:02 +02001875 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1876 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1877
1878 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1879 if (netdev && netdev->ieee80211_ptr) {
1880 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1881 mutex_lock(&rdev->mtx);
1882 } else
1883 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001884 }
1885
Johannes Bergf444de02010-05-05 15:25:02 +02001886 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001887 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1888 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001889 if (IS_ERR(rdev)) {
1890 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001891 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001892 }
1893 wdev = NULL;
1894 netdev = NULL;
1895 result = 0;
1896
1897 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001898 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001899 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001900
1901 /*
1902 * end workaround code, by now the rdev is available
1903 * and locked, and wdev may or may not be NULL.
1904 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001905
1906 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001907 result = cfg80211_dev_rename(
1908 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001909
1910 mutex_unlock(&cfg80211_mutex);
1911
1912 if (result)
1913 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001914
Jouni Malinen31888482008-10-30 16:59:24 +02001915 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1916 struct ieee80211_txq_params txq_params;
1917 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1918
1919 if (!rdev->ops->set_txq_params) {
1920 result = -EOPNOTSUPP;
1921 goto bad_res;
1922 }
1923
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001924 if (!netdev) {
1925 result = -EINVAL;
1926 goto bad_res;
1927 }
1928
Johannes Berg133a3ff2011-11-03 14:50:13 +01001929 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1930 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1931 result = -EINVAL;
1932 goto bad_res;
1933 }
1934
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001935 if (!netif_running(netdev)) {
1936 result = -ENETDOWN;
1937 goto bad_res;
1938 }
1939
Jouni Malinen31888482008-10-30 16:59:24 +02001940 nla_for_each_nested(nl_txq_params,
1941 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1942 rem_txq_params) {
1943 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1944 nla_data(nl_txq_params),
1945 nla_len(nl_txq_params),
1946 txq_params_policy);
1947 result = parse_txq_params(tb, &txq_params);
1948 if (result)
1949 goto bad_res;
1950
Hila Gonene35e4d22012-06-27 17:19:42 +03001951 result = rdev_set_txq_params(rdev, netdev,
1952 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001953 if (result)
1954 goto bad_res;
1955 }
1956 }
1957
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001958 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001959 result = __nl80211_set_channel(rdev,
1960 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1961 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001962 if (result)
1963 goto bad_res;
1964 }
1965
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001966 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001967 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001968 enum nl80211_tx_power_setting type;
1969 int idx, mbm = 0;
1970
Johannes Bergc8442112012-10-24 10:17:18 +02001971 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1972 txp_wdev = NULL;
1973
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001974 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001975 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001976 goto bad_res;
1977 }
1978
1979 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1980 type = nla_get_u32(info->attrs[idx]);
1981
1982 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1983 (type != NL80211_TX_POWER_AUTOMATIC)) {
1984 result = -EINVAL;
1985 goto bad_res;
1986 }
1987
1988 if (type != NL80211_TX_POWER_AUTOMATIC) {
1989 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1990 mbm = nla_get_u32(info->attrs[idx]);
1991 }
1992
Johannes Bergc8442112012-10-24 10:17:18 +02001993 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001994 if (result)
1995 goto bad_res;
1996 }
1997
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001998 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1999 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2000 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002001 if ((!rdev->wiphy.available_antennas_tx &&
2002 !rdev->wiphy.available_antennas_rx) ||
2003 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002004 result = -EOPNOTSUPP;
2005 goto bad_res;
2006 }
2007
2008 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2009 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2010
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002011 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002012 * available antenna masks, except for the "all" mask */
2013 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2014 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002015 result = -EINVAL;
2016 goto bad_res;
2017 }
2018
Bruno Randolf7f531e02010-12-16 11:30:22 +09002019 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2020 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002021
Hila Gonene35e4d22012-06-27 17:19:42 +03002022 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002023 if (result)
2024 goto bad_res;
2025 }
2026
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002027 changed = 0;
2028
2029 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2030 retry_short = nla_get_u8(
2031 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2032 if (retry_short == 0) {
2033 result = -EINVAL;
2034 goto bad_res;
2035 }
2036 changed |= WIPHY_PARAM_RETRY_SHORT;
2037 }
2038
2039 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2040 retry_long = nla_get_u8(
2041 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2042 if (retry_long == 0) {
2043 result = -EINVAL;
2044 goto bad_res;
2045 }
2046 changed |= WIPHY_PARAM_RETRY_LONG;
2047 }
2048
2049 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2050 frag_threshold = nla_get_u32(
2051 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2052 if (frag_threshold < 256) {
2053 result = -EINVAL;
2054 goto bad_res;
2055 }
2056 if (frag_threshold != (u32) -1) {
2057 /*
2058 * Fragments (apart from the last one) are required to
2059 * have even length. Make the fragmentation code
2060 * simpler by stripping LSB should someone try to use
2061 * odd threshold value.
2062 */
2063 frag_threshold &= ~0x1;
2064 }
2065 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2066 }
2067
2068 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2069 rts_threshold = nla_get_u32(
2070 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2071 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2072 }
2073
Lukáš Turek81077e82009-12-21 22:50:47 +01002074 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2075 coverage_class = nla_get_u8(
2076 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2077 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2078 }
2079
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002080 if (changed) {
2081 u8 old_retry_short, old_retry_long;
2082 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002083 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002084
2085 if (!rdev->ops->set_wiphy_params) {
2086 result = -EOPNOTSUPP;
2087 goto bad_res;
2088 }
2089
2090 old_retry_short = rdev->wiphy.retry_short;
2091 old_retry_long = rdev->wiphy.retry_long;
2092 old_frag_threshold = rdev->wiphy.frag_threshold;
2093 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002094 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002095
2096 if (changed & WIPHY_PARAM_RETRY_SHORT)
2097 rdev->wiphy.retry_short = retry_short;
2098 if (changed & WIPHY_PARAM_RETRY_LONG)
2099 rdev->wiphy.retry_long = retry_long;
2100 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2101 rdev->wiphy.frag_threshold = frag_threshold;
2102 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2103 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002104 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2105 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002106
Hila Gonene35e4d22012-06-27 17:19:42 +03002107 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002108 if (result) {
2109 rdev->wiphy.retry_short = old_retry_short;
2110 rdev->wiphy.retry_long = old_retry_long;
2111 rdev->wiphy.frag_threshold = old_frag_threshold;
2112 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002113 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002114 }
2115 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002116
Johannes Berg306d6112008-12-08 12:39:04 +01002117 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002118 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02002119 if (netdev)
2120 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002121 return result;
2122}
2123
Johannes Berg71bbc992012-06-15 15:30:18 +02002124static inline u64 wdev_id(struct wireless_dev *wdev)
2125{
2126 return (u64)wdev->identifier |
2127 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2128}
Johannes Berg55682962007-09-20 13:09:35 -04002129
Johannes Berg683b6d32012-11-08 21:25:48 +01002130static int nl80211_send_chandef(struct sk_buff *msg,
2131 struct cfg80211_chan_def *chandef)
2132{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002133 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002134
Johannes Berg683b6d32012-11-08 21:25:48 +01002135 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2136 chandef->chan->center_freq))
2137 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002138 switch (chandef->width) {
2139 case NL80211_CHAN_WIDTH_20_NOHT:
2140 case NL80211_CHAN_WIDTH_20:
2141 case NL80211_CHAN_WIDTH_40:
2142 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2143 cfg80211_get_chandef_type(chandef)))
2144 return -ENOBUFS;
2145 break;
2146 default:
2147 break;
2148 }
2149 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2150 return -ENOBUFS;
2151 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2152 return -ENOBUFS;
2153 if (chandef->center_freq2 &&
2154 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002155 return -ENOBUFS;
2156 return 0;
2157}
2158
Eric W. Biederman15e47302012-09-07 20:12:54 +00002159static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002160 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002161 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002162{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002163 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002164 void *hdr;
2165
Eric W. Biederman15e47302012-09-07 20:12:54 +00002166 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002167 if (!hdr)
2168 return -1;
2169
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002170 if (dev &&
2171 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002172 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002173 goto nla_put_failure;
2174
2175 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2176 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002177 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002178 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002179 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2180 rdev->devlist_generation ^
2181 (cfg80211_rdev_list_generation << 2)))
2182 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002183
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002184 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002185 int ret;
2186 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002187
Johannes Berg683b6d32012-11-08 21:25:48 +01002188 ret = rdev_get_channel(rdev, wdev, &chandef);
2189 if (ret == 0) {
2190 if (nl80211_send_chandef(msg, &chandef))
2191 goto nla_put_failure;
2192 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002193 }
2194
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002195 if (wdev->ssid_len) {
2196 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2197 goto nla_put_failure;
2198 }
2199
Johannes Berg55682962007-09-20 13:09:35 -04002200 return genlmsg_end(msg, hdr);
2201
2202 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002203 genlmsg_cancel(msg, hdr);
2204 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002205}
2206
2207static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2208{
2209 int wp_idx = 0;
2210 int if_idx = 0;
2211 int wp_start = cb->args[0];
2212 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002213 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002214 struct wireless_dev *wdev;
2215
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002216 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02002217 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2218 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002219 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002220 if (wp_idx < wp_start) {
2221 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002222 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002223 }
Johannes Berg55682962007-09-20 13:09:35 -04002224 if_idx = 0;
2225
Johannes Bergf5ea9122009-08-07 16:17:38 +02002226 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02002227 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002228 if (if_idx < if_start) {
2229 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002230 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002231 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002232 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002233 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002234 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002235 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002236 goto out;
2237 }
2238 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002239 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002240 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002241
2242 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002243 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002244 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002245 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002246
2247 cb->args[0] = wp_idx;
2248 cb->args[1] = if_idx;
2249
2250 return skb->len;
2251}
2252
2253static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2254{
2255 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002256 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002257 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002258
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002259 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002260 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002261 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002262
Eric W. Biederman15e47302012-09-07 20:12:54 +00002263 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002264 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002265 nlmsg_free(msg);
2266 return -ENOBUFS;
2267 }
Johannes Berg55682962007-09-20 13:09:35 -04002268
Johannes Berg134e6372009-07-10 09:51:34 +00002269 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002270}
2271
Michael Wu66f7ac52008-01-31 19:48:22 +01002272static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2273 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2274 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2275 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2276 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2277 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2278};
2279
2280static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2281{
2282 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2283 int flag;
2284
2285 *mntrflags = 0;
2286
2287 if (!nla)
2288 return -EINVAL;
2289
2290 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2291 nla, mntr_flags_policy))
2292 return -EINVAL;
2293
2294 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2295 if (flags[flag])
2296 *mntrflags |= (1<<flag);
2297
2298 return 0;
2299}
2300
Johannes Berg9bc383d2009-11-19 11:55:19 +01002301static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002302 struct net_device *netdev, u8 use_4addr,
2303 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002304{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002305 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002306 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002307 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002308 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002309 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002310
2311 switch (iftype) {
2312 case NL80211_IFTYPE_AP_VLAN:
2313 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2314 return 0;
2315 break;
2316 case NL80211_IFTYPE_STATION:
2317 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2318 return 0;
2319 break;
2320 default:
2321 break;
2322 }
2323
2324 return -EOPNOTSUPP;
2325}
2326
Johannes Berg55682962007-09-20 13:09:35 -04002327static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2328{
Johannes Berg4c476992010-10-04 21:36:35 +02002329 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002330 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002331 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002332 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002333 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002334 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002335 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002336
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002337 memset(&params, 0, sizeof(params));
2338
Johannes Berg04a773a2009-04-19 21:24:32 +02002339 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002340
Johannes Berg723b0382008-09-16 20:22:09 +02002341 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002342 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002343 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002344 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002345 if (ntype > NL80211_IFTYPE_MAX)
2346 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002347 }
2348
Johannes Berg92ffe052008-09-16 20:39:36 +02002349 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002350 struct wireless_dev *wdev = dev->ieee80211_ptr;
2351
Johannes Berg4c476992010-10-04 21:36:35 +02002352 if (ntype != NL80211_IFTYPE_MESH_POINT)
2353 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002354 if (netif_running(dev))
2355 return -EBUSY;
2356
2357 wdev_lock(wdev);
2358 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2359 IEEE80211_MAX_MESH_ID_LEN);
2360 wdev->mesh_id_up_len =
2361 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2362 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2363 wdev->mesh_id_up_len);
2364 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002365 }
2366
Felix Fietkau8b787642009-11-10 18:53:10 +01002367 if (info->attrs[NL80211_ATTR_4ADDR]) {
2368 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2369 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002370 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002371 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002372 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002373 } else {
2374 params.use_4addr = -1;
2375 }
2376
Johannes Berg92ffe052008-09-16 20:39:36 +02002377 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002378 if (ntype != NL80211_IFTYPE_MONITOR)
2379 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002380 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2381 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002382 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002383 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002384
2385 flags = &_flags;
2386 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002387 }
Johannes Berg3b858752009-03-12 09:55:09 +01002388
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002389 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002390 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002391 else
2392 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002393
Johannes Berg9bc383d2009-11-19 11:55:19 +01002394 if (!err && params.use_4addr != -1)
2395 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2396
Johannes Berg55682962007-09-20 13:09:35 -04002397 return err;
2398}
2399
2400static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2401{
Johannes Berg4c476992010-10-04 21:36:35 +02002402 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002403 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002404 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002405 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002406 int err;
2407 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002408 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002409
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002410 memset(&params, 0, sizeof(params));
2411
Johannes Berg55682962007-09-20 13:09:35 -04002412 if (!info->attrs[NL80211_ATTR_IFNAME])
2413 return -EINVAL;
2414
2415 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2416 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2417 if (type > NL80211_IFTYPE_MAX)
2418 return -EINVAL;
2419 }
2420
Johannes Berg79c97e92009-07-07 03:56:12 +02002421 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002422 !(rdev->wiphy.interface_modes & (1 << type)))
2423 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002424
Arend van Spriel1c18f142013-01-08 10:17:27 +01002425 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2426 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2427 ETH_ALEN);
2428 if (!is_valid_ether_addr(params.macaddr))
2429 return -EADDRNOTAVAIL;
2430 }
2431
Johannes Berg9bc383d2009-11-19 11:55:19 +01002432 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002433 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002434 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002435 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002436 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002437 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002438
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002439 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2440 if (!msg)
2441 return -ENOMEM;
2442
Michael Wu66f7ac52008-01-31 19:48:22 +01002443 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2444 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2445 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002446 wdev = rdev_add_virtual_intf(rdev,
2447 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2448 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002449 if (IS_ERR(wdev)) {
2450 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002451 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002452 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002453
Johannes Berg98104fde2012-06-16 00:19:54 +02002454 switch (type) {
2455 case NL80211_IFTYPE_MESH_POINT:
2456 if (!info->attrs[NL80211_ATTR_MESH_ID])
2457 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002458 wdev_lock(wdev);
2459 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2460 IEEE80211_MAX_MESH_ID_LEN);
2461 wdev->mesh_id_up_len =
2462 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2463 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2464 wdev->mesh_id_up_len);
2465 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002466 break;
2467 case NL80211_IFTYPE_P2P_DEVICE:
2468 /*
2469 * P2P Device doesn't have a netdev, so doesn't go
2470 * through the netdev notifier and must be added here
2471 */
2472 mutex_init(&wdev->mtx);
2473 INIT_LIST_HEAD(&wdev->event_list);
2474 spin_lock_init(&wdev->event_lock);
2475 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2476 spin_lock_init(&wdev->mgmt_registrations_lock);
2477
2478 mutex_lock(&rdev->devlist_mtx);
2479 wdev->identifier = ++rdev->wdev_id;
2480 list_add_rcu(&wdev->list, &rdev->wdev_list);
2481 rdev->devlist_generation++;
2482 mutex_unlock(&rdev->devlist_mtx);
2483 break;
2484 default:
2485 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002486 }
2487
Eric W. Biederman15e47302012-09-07 20:12:54 +00002488 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002489 rdev, wdev) < 0) {
2490 nlmsg_free(msg);
2491 return -ENOBUFS;
2492 }
2493
2494 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002495}
2496
2497static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2498{
Johannes Berg4c476992010-10-04 21:36:35 +02002499 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002500 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002501
Johannes Berg4c476992010-10-04 21:36:35 +02002502 if (!rdev->ops->del_virtual_intf)
2503 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002504
Johannes Berg84efbb82012-06-16 00:00:26 +02002505 /*
2506 * If we remove a wireless device without a netdev then clear
2507 * user_ptr[1] so that nl80211_post_doit won't dereference it
2508 * to check if it needs to do dev_put(). Otherwise it crashes
2509 * since the wdev has been freed, unlike with a netdev where
2510 * we need the dev_put() for the netdev to really be freed.
2511 */
2512 if (!wdev->netdev)
2513 info->user_ptr[1] = NULL;
2514
Hila Gonene35e4d22012-06-27 17:19:42 +03002515 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002516}
2517
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002518static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2519{
2520 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2521 struct net_device *dev = info->user_ptr[1];
2522 u16 noack_map;
2523
2524 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2525 return -EINVAL;
2526
2527 if (!rdev->ops->set_noack_map)
2528 return -EOPNOTSUPP;
2529
2530 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2531
Hila Gonene35e4d22012-06-27 17:19:42 +03002532 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002533}
2534
Johannes Berg41ade002007-12-19 02:03:29 +01002535struct get_key_cookie {
2536 struct sk_buff *msg;
2537 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002538 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002539};
2540
2541static void get_key_callback(void *c, struct key_params *params)
2542{
Johannes Bergb9454e82009-07-08 13:29:08 +02002543 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002544 struct get_key_cookie *cookie = c;
2545
David S. Miller9360ffd2012-03-29 04:41:26 -04002546 if ((params->key &&
2547 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2548 params->key_len, params->key)) ||
2549 (params->seq &&
2550 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2551 params->seq_len, params->seq)) ||
2552 (params->cipher &&
2553 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2554 params->cipher)))
2555 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002556
Johannes Bergb9454e82009-07-08 13:29:08 +02002557 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2558 if (!key)
2559 goto nla_put_failure;
2560
David S. Miller9360ffd2012-03-29 04:41:26 -04002561 if ((params->key &&
2562 nla_put(cookie->msg, NL80211_KEY_DATA,
2563 params->key_len, params->key)) ||
2564 (params->seq &&
2565 nla_put(cookie->msg, NL80211_KEY_SEQ,
2566 params->seq_len, params->seq)) ||
2567 (params->cipher &&
2568 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2569 params->cipher)))
2570 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002571
David S. Miller9360ffd2012-03-29 04:41:26 -04002572 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2573 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002574
2575 nla_nest_end(cookie->msg, key);
2576
Johannes Berg41ade002007-12-19 02:03:29 +01002577 return;
2578 nla_put_failure:
2579 cookie->error = 1;
2580}
2581
2582static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2583{
Johannes Berg4c476992010-10-04 21:36:35 +02002584 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002585 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002586 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002587 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002588 const u8 *mac_addr = NULL;
2589 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002590 struct get_key_cookie cookie = {
2591 .error = 0,
2592 };
2593 void *hdr;
2594 struct sk_buff *msg;
2595
2596 if (info->attrs[NL80211_ATTR_KEY_IDX])
2597 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2598
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002599 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002600 return -EINVAL;
2601
2602 if (info->attrs[NL80211_ATTR_MAC])
2603 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2604
Johannes Berge31b8212010-10-05 19:39:30 +02002605 pairwise = !!mac_addr;
2606 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2607 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2608 if (kt >= NUM_NL80211_KEYTYPES)
2609 return -EINVAL;
2610 if (kt != NL80211_KEYTYPE_GROUP &&
2611 kt != NL80211_KEYTYPE_PAIRWISE)
2612 return -EINVAL;
2613 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2614 }
2615
Johannes Berg4c476992010-10-04 21:36:35 +02002616 if (!rdev->ops->get_key)
2617 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002618
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002619 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002620 if (!msg)
2621 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002622
Eric W. Biederman15e47302012-09-07 20:12:54 +00002623 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002624 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002625 if (IS_ERR(hdr))
2626 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002627
2628 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002629 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002630
David S. Miller9360ffd2012-03-29 04:41:26 -04002631 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2632 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2633 goto nla_put_failure;
2634 if (mac_addr &&
2635 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2636 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002637
Johannes Berge31b8212010-10-05 19:39:30 +02002638 if (pairwise && mac_addr &&
2639 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2640 return -ENOENT;
2641
Hila Gonene35e4d22012-06-27 17:19:42 +03002642 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2643 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002644
2645 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002646 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002647
2648 if (cookie.error)
2649 goto nla_put_failure;
2650
2651 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002652 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002653
2654 nla_put_failure:
2655 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002656 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002657 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002658 return err;
2659}
2660
2661static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2662{
Johannes Berg4c476992010-10-04 21:36:35 +02002663 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002664 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002665 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002666 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002667
Johannes Bergb9454e82009-07-08 13:29:08 +02002668 err = nl80211_parse_key(info, &key);
2669 if (err)
2670 return err;
2671
2672 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002673 return -EINVAL;
2674
Johannes Bergb9454e82009-07-08 13:29:08 +02002675 /* only support setting default key */
2676 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002677 return -EINVAL;
2678
Johannes Bergfffd0932009-07-08 14:22:54 +02002679 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002680
2681 if (key.def) {
2682 if (!rdev->ops->set_default_key) {
2683 err = -EOPNOTSUPP;
2684 goto out;
2685 }
2686
2687 err = nl80211_key_allowed(dev->ieee80211_ptr);
2688 if (err)
2689 goto out;
2690
Hila Gonene35e4d22012-06-27 17:19:42 +03002691 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002692 key.def_uni, key.def_multi);
2693
2694 if (err)
2695 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002696
Johannes Berg3d23e342009-09-29 23:27:28 +02002697#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002698 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002699#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002700 } else {
2701 if (key.def_uni || !key.def_multi) {
2702 err = -EINVAL;
2703 goto out;
2704 }
2705
2706 if (!rdev->ops->set_default_mgmt_key) {
2707 err = -EOPNOTSUPP;
2708 goto out;
2709 }
2710
2711 err = nl80211_key_allowed(dev->ieee80211_ptr);
2712 if (err)
2713 goto out;
2714
Hila Gonene35e4d22012-06-27 17:19:42 +03002715 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002716 if (err)
2717 goto out;
2718
2719#ifdef CONFIG_CFG80211_WEXT
2720 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2721#endif
2722 }
2723
2724 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002725 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002726
Johannes Berg41ade002007-12-19 02:03:29 +01002727 return err;
2728}
2729
2730static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2731{
Johannes Berg4c476992010-10-04 21:36:35 +02002732 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002733 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002734 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002735 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002736 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002737
Johannes Bergb9454e82009-07-08 13:29:08 +02002738 err = nl80211_parse_key(info, &key);
2739 if (err)
2740 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002741
Johannes Bergb9454e82009-07-08 13:29:08 +02002742 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002743 return -EINVAL;
2744
Johannes Berg41ade002007-12-19 02:03:29 +01002745 if (info->attrs[NL80211_ATTR_MAC])
2746 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2747
Johannes Berge31b8212010-10-05 19:39:30 +02002748 if (key.type == -1) {
2749 if (mac_addr)
2750 key.type = NL80211_KEYTYPE_PAIRWISE;
2751 else
2752 key.type = NL80211_KEYTYPE_GROUP;
2753 }
2754
2755 /* for now */
2756 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2757 key.type != NL80211_KEYTYPE_GROUP)
2758 return -EINVAL;
2759
Johannes Berg4c476992010-10-04 21:36:35 +02002760 if (!rdev->ops->add_key)
2761 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002762
Johannes Berge31b8212010-10-05 19:39:30 +02002763 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2764 key.type == NL80211_KEYTYPE_PAIRWISE,
2765 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002766 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002767
2768 wdev_lock(dev->ieee80211_ptr);
2769 err = nl80211_key_allowed(dev->ieee80211_ptr);
2770 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002771 err = rdev_add_key(rdev, dev, key.idx,
2772 key.type == NL80211_KEYTYPE_PAIRWISE,
2773 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002774 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002775
Johannes Berg41ade002007-12-19 02:03:29 +01002776 return err;
2777}
2778
2779static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2780{
Johannes Berg4c476992010-10-04 21:36:35 +02002781 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002782 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002783 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002784 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002785 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002786
Johannes Bergb9454e82009-07-08 13:29:08 +02002787 err = nl80211_parse_key(info, &key);
2788 if (err)
2789 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002790
2791 if (info->attrs[NL80211_ATTR_MAC])
2792 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2793
Johannes Berge31b8212010-10-05 19:39:30 +02002794 if (key.type == -1) {
2795 if (mac_addr)
2796 key.type = NL80211_KEYTYPE_PAIRWISE;
2797 else
2798 key.type = NL80211_KEYTYPE_GROUP;
2799 }
2800
2801 /* for now */
2802 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2803 key.type != NL80211_KEYTYPE_GROUP)
2804 return -EINVAL;
2805
Johannes Berg4c476992010-10-04 21:36:35 +02002806 if (!rdev->ops->del_key)
2807 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002808
Johannes Bergfffd0932009-07-08 14:22:54 +02002809 wdev_lock(dev->ieee80211_ptr);
2810 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002811
2812 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2813 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2814 err = -ENOENT;
2815
Johannes Bergfffd0932009-07-08 14:22:54 +02002816 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002817 err = rdev_del_key(rdev, dev, key.idx,
2818 key.type == NL80211_KEYTYPE_PAIRWISE,
2819 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002820
Johannes Berg3d23e342009-09-29 23:27:28 +02002821#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002822 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002823 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002824 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002825 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002826 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2827 }
2828#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002829 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002830
Johannes Berg41ade002007-12-19 02:03:29 +01002831 return err;
2832}
2833
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302834/* This function returns an error or the number of nested attributes */
2835static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2836{
2837 struct nlattr *attr;
2838 int n_entries = 0, tmp;
2839
2840 nla_for_each_nested(attr, nl_attr, tmp) {
2841 if (nla_len(attr) != ETH_ALEN)
2842 return -EINVAL;
2843
2844 n_entries++;
2845 }
2846
2847 return n_entries;
2848}
2849
2850/*
2851 * This function parses ACL information and allocates memory for ACL data.
2852 * On successful return, the calling function is responsible to free the
2853 * ACL buffer returned by this function.
2854 */
2855static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2856 struct genl_info *info)
2857{
2858 enum nl80211_acl_policy acl_policy;
2859 struct nlattr *attr;
2860 struct cfg80211_acl_data *acl;
2861 int i = 0, n_entries, tmp;
2862
2863 if (!wiphy->max_acl_mac_addrs)
2864 return ERR_PTR(-EOPNOTSUPP);
2865
2866 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2867 return ERR_PTR(-EINVAL);
2868
2869 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2870 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2871 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2872 return ERR_PTR(-EINVAL);
2873
2874 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2875 return ERR_PTR(-EINVAL);
2876
2877 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2878 if (n_entries < 0)
2879 return ERR_PTR(n_entries);
2880
2881 if (n_entries > wiphy->max_acl_mac_addrs)
2882 return ERR_PTR(-ENOTSUPP);
2883
2884 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2885 GFP_KERNEL);
2886 if (!acl)
2887 return ERR_PTR(-ENOMEM);
2888
2889 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2890 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2891 i++;
2892 }
2893
2894 acl->n_acl_entries = n_entries;
2895 acl->acl_policy = acl_policy;
2896
2897 return acl;
2898}
2899
2900static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2901{
2902 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2903 struct net_device *dev = info->user_ptr[1];
2904 struct cfg80211_acl_data *acl;
2905 int err;
2906
2907 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2908 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2909 return -EOPNOTSUPP;
2910
2911 if (!dev->ieee80211_ptr->beacon_interval)
2912 return -EINVAL;
2913
2914 acl = parse_acl_data(&rdev->wiphy, info);
2915 if (IS_ERR(acl))
2916 return PTR_ERR(acl);
2917
2918 err = rdev_set_mac_acl(rdev, dev, acl);
2919
2920 kfree(acl);
2921
2922 return err;
2923}
2924
Johannes Berg88600202012-02-13 15:17:18 +01002925static int nl80211_parse_beacon(struct genl_info *info,
2926 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002927{
Johannes Berg88600202012-02-13 15:17:18 +01002928 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002929
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002930 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2931 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2932 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2933 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002934 return -EINVAL;
2935
Johannes Berg88600202012-02-13 15:17:18 +01002936 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002937
Johannes Berged1b6cc2007-12-19 02:03:32 +01002938 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002939 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2940 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2941 if (!bcn->head_len)
2942 return -EINVAL;
2943 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002944 }
2945
2946 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002947 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2948 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002949 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002950 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002951 }
2952
Johannes Berg4c476992010-10-04 21:36:35 +02002953 if (!haveinfo)
2954 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002955
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002956 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002957 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2958 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002959 }
2960
2961 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002962 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002963 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002964 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002965 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2966 }
2967
2968 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002969 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002970 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002971 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002972 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2973 }
2974
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002975 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002976 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002977 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002978 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002979 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2980 }
2981
Johannes Berg88600202012-02-13 15:17:18 +01002982 return 0;
2983}
2984
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002985static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2986 struct cfg80211_ap_settings *params)
2987{
2988 struct wireless_dev *wdev;
2989 bool ret = false;
2990
2991 mutex_lock(&rdev->devlist_mtx);
2992
Johannes Berg89a54e42012-06-15 14:33:17 +02002993 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002994 if (wdev->iftype != NL80211_IFTYPE_AP &&
2995 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2996 continue;
2997
Johannes Berg683b6d32012-11-08 21:25:48 +01002998 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002999 continue;
3000
Johannes Berg683b6d32012-11-08 21:25:48 +01003001 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003002 ret = true;
3003 break;
3004 }
3005
3006 mutex_unlock(&rdev->devlist_mtx);
3007
3008 return ret;
3009}
3010
Jouni Malinene39e5b52012-09-30 19:29:39 +03003011static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3012 enum nl80211_auth_type auth_type,
3013 enum nl80211_commands cmd)
3014{
3015 if (auth_type > NL80211_AUTHTYPE_MAX)
3016 return false;
3017
3018 switch (cmd) {
3019 case NL80211_CMD_AUTHENTICATE:
3020 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3021 auth_type == NL80211_AUTHTYPE_SAE)
3022 return false;
3023 return true;
3024 case NL80211_CMD_CONNECT:
3025 case NL80211_CMD_START_AP:
3026 /* SAE not supported yet */
3027 if (auth_type == NL80211_AUTHTYPE_SAE)
3028 return false;
3029 return true;
3030 default:
3031 return false;
3032 }
3033}
3034
Johannes Berg88600202012-02-13 15:17:18 +01003035static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3036{
3037 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3038 struct net_device *dev = info->user_ptr[1];
3039 struct wireless_dev *wdev = dev->ieee80211_ptr;
3040 struct cfg80211_ap_settings params;
3041 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003042 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003043
3044 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3045 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3046 return -EOPNOTSUPP;
3047
3048 if (!rdev->ops->start_ap)
3049 return -EOPNOTSUPP;
3050
3051 if (wdev->beacon_interval)
3052 return -EALREADY;
3053
3054 memset(&params, 0, sizeof(params));
3055
3056 /* these are required for START_AP */
3057 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3058 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3059 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3060 return -EINVAL;
3061
3062 err = nl80211_parse_beacon(info, &params.beacon);
3063 if (err)
3064 return err;
3065
3066 params.beacon_interval =
3067 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3068 params.dtim_period =
3069 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3070
3071 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3072 if (err)
3073 return err;
3074
3075 /*
3076 * In theory, some of these attributes should be required here
3077 * but since they were not used when the command was originally
3078 * added, keep them optional for old user space programs to let
3079 * them continue to work with drivers that do not need the
3080 * additional information -- drivers must check!
3081 */
3082 if (info->attrs[NL80211_ATTR_SSID]) {
3083 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3084 params.ssid_len =
3085 nla_len(info->attrs[NL80211_ATTR_SSID]);
3086 if (params.ssid_len == 0 ||
3087 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3088 return -EINVAL;
3089 }
3090
3091 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3092 params.hidden_ssid = nla_get_u32(
3093 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3094 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3095 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3096 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3097 return -EINVAL;
3098 }
3099
3100 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3101
3102 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3103 params.auth_type = nla_get_u32(
3104 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003105 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3106 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003107 return -EINVAL;
3108 } else
3109 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3110
3111 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3112 NL80211_MAX_NR_CIPHER_SUITES);
3113 if (err)
3114 return err;
3115
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303116 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3117 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3118 return -EOPNOTSUPP;
3119 params.inactivity_timeout = nla_get_u16(
3120 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3121 }
3122
Johannes Berg53cabad2012-11-14 15:17:28 +01003123 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3124 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3125 return -EINVAL;
3126 params.p2p_ctwindow =
3127 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3128 if (params.p2p_ctwindow > 127)
3129 return -EINVAL;
3130 if (params.p2p_ctwindow != 0 &&
3131 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3132 return -EINVAL;
3133 }
3134
3135 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3136 u8 tmp;
3137
3138 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3139 return -EINVAL;
3140 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3141 if (tmp > 1)
3142 return -EINVAL;
3143 params.p2p_opp_ps = tmp;
3144 if (params.p2p_opp_ps != 0 &&
3145 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3146 return -EINVAL;
3147 }
3148
Johannes Bergaa430da2012-05-16 23:50:18 +02003149 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003150 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3151 if (err)
3152 return err;
3153 } else if (wdev->preset_chandef.chan) {
3154 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003155 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003156 return -EINVAL;
3157
Johannes Berg683b6d32012-11-08 21:25:48 +01003158 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003159 return -EINVAL;
3160
Simon Wunderlich04f39042013-02-08 18:16:19 +01003161 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3162 if (err < 0)
3163 return err;
3164 if (err) {
3165 radar_detect_width = BIT(params.chandef.width);
3166 params.radar_required = true;
3167 }
3168
Michal Kaziore4e32452012-06-29 12:47:08 +02003169 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003170 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3171 params.chandef.chan,
3172 CHAN_MODE_SHARED,
3173 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003174 mutex_unlock(&rdev->devlist_mtx);
3175
3176 if (err)
3177 return err;
3178
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303179 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3180 params.acl = parse_acl_data(&rdev->wiphy, info);
3181 if (IS_ERR(params.acl))
3182 return PTR_ERR(params.acl);
3183 }
3184
Hila Gonene35e4d22012-06-27 17:19:42 +03003185 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003186 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003187 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003188 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003189 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003190 wdev->ssid_len = params.ssid_len;
3191 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003192 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303193
3194 kfree(params.acl);
3195
Johannes Berg56d18932011-05-09 18:41:15 +02003196 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003197}
3198
Johannes Berg88600202012-02-13 15:17:18 +01003199static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3200{
3201 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3202 struct net_device *dev = info->user_ptr[1];
3203 struct wireless_dev *wdev = dev->ieee80211_ptr;
3204 struct cfg80211_beacon_data params;
3205 int err;
3206
3207 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3208 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3209 return -EOPNOTSUPP;
3210
3211 if (!rdev->ops->change_beacon)
3212 return -EOPNOTSUPP;
3213
3214 if (!wdev->beacon_interval)
3215 return -EINVAL;
3216
3217 err = nl80211_parse_beacon(info, &params);
3218 if (err)
3219 return err;
3220
Hila Gonene35e4d22012-06-27 17:19:42 +03003221 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003222}
3223
3224static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003225{
Johannes Berg4c476992010-10-04 21:36:35 +02003226 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3227 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003228
Michal Kazior60771782012-06-29 12:46:56 +02003229 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003230}
3231
Johannes Berg5727ef12007-12-19 02:03:34 +01003232static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3233 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3234 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3235 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003236 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003237 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003238 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003239};
3240
Johannes Bergeccb8e82009-05-11 21:57:56 +03003241static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003242 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003243 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003244{
3245 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003246 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003247 int flag;
3248
Johannes Bergeccb8e82009-05-11 21:57:56 +03003249 /*
3250 * Try parsing the new attribute first so userspace
3251 * can specify both for older kernels.
3252 */
3253 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3254 if (nla) {
3255 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003256
Johannes Bergeccb8e82009-05-11 21:57:56 +03003257 sta_flags = nla_data(nla);
3258 params->sta_flags_mask = sta_flags->mask;
3259 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003260 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003261 if ((params->sta_flags_mask |
3262 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3263 return -EINVAL;
3264 return 0;
3265 }
3266
3267 /* if present, parse the old attribute */
3268
3269 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003270 if (!nla)
3271 return 0;
3272
3273 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3274 nla, sta_flags_policy))
3275 return -EINVAL;
3276
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003277 /*
3278 * Only allow certain flags for interface types so that
3279 * other attributes are silently ignored. Remember that
3280 * this is backward compatibility code with old userspace
3281 * and shouldn't be hit in other cases anyway.
3282 */
3283 switch (iftype) {
3284 case NL80211_IFTYPE_AP:
3285 case NL80211_IFTYPE_AP_VLAN:
3286 case NL80211_IFTYPE_P2P_GO:
3287 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3288 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3289 BIT(NL80211_STA_FLAG_WME) |
3290 BIT(NL80211_STA_FLAG_MFP);
3291 break;
3292 case NL80211_IFTYPE_P2P_CLIENT:
3293 case NL80211_IFTYPE_STATION:
3294 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3295 BIT(NL80211_STA_FLAG_TDLS_PEER);
3296 break;
3297 case NL80211_IFTYPE_MESH_POINT:
3298 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3299 BIT(NL80211_STA_FLAG_MFP) |
3300 BIT(NL80211_STA_FLAG_AUTHORIZED);
3301 default:
3302 return -EINVAL;
3303 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003304
Johannes Berg3383b5a2012-05-10 20:14:43 +02003305 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3306 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003307 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003308
Johannes Berg3383b5a2012-05-10 20:14:43 +02003309 /* no longer support new API additions in old API */
3310 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3311 return -EINVAL;
3312 }
3313 }
3314
Johannes Berg5727ef12007-12-19 02:03:34 +01003315 return 0;
3316}
3317
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003318static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3319 int attr)
3320{
3321 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003322 u32 bitrate;
3323 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003324
3325 rate = nla_nest_start(msg, attr);
3326 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003327 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003328
3329 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3330 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003331 /* report 16-bit bitrate only if we can */
3332 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003333 if (bitrate > 0 &&
3334 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3335 return false;
3336 if (bitrate_compat > 0 &&
3337 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3338 return false;
3339
3340 if (info->flags & RATE_INFO_FLAGS_MCS) {
3341 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3342 return false;
3343 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3344 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3345 return false;
3346 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3347 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3348 return false;
3349 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3350 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3351 return false;
3352 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3353 return false;
3354 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3355 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3356 return false;
3357 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3358 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3359 return false;
3360 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3361 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3362 return false;
3363 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3364 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3365 return false;
3366 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3367 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3368 return false;
3369 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003370
3371 nla_nest_end(msg, rate);
3372 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003373}
3374
Eric W. Biederman15e47302012-09-07 20:12:54 +00003375static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003376 int flags,
3377 struct cfg80211_registered_device *rdev,
3378 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003379 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003380{
3381 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003382 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003383
Eric W. Biederman15e47302012-09-07 20:12:54 +00003384 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003385 if (!hdr)
3386 return -1;
3387
David S. Miller9360ffd2012-03-29 04:41:26 -04003388 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3389 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3390 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3391 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003392
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003393 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3394 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003395 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003396 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3397 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3398 sinfo->connected_time))
3399 goto nla_put_failure;
3400 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3401 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3402 sinfo->inactive_time))
3403 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003404 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3405 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003406 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003407 (u32)sinfo->rx_bytes))
3408 goto nla_put_failure;
3409 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3410 NL80211_STA_INFO_TX_BYTES64)) &&
3411 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3412 (u32)sinfo->tx_bytes))
3413 goto nla_put_failure;
3414 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3415 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003416 sinfo->rx_bytes))
3417 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003418 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3419 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003420 sinfo->tx_bytes))
3421 goto nla_put_failure;
3422 if ((sinfo->filled & STATION_INFO_LLID) &&
3423 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3424 goto nla_put_failure;
3425 if ((sinfo->filled & STATION_INFO_PLID) &&
3426 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3427 goto nla_put_failure;
3428 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3429 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3430 sinfo->plink_state))
3431 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003432 switch (rdev->wiphy.signal_type) {
3433 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003434 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3435 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3436 sinfo->signal))
3437 goto nla_put_failure;
3438 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3439 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3440 sinfo->signal_avg))
3441 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003442 break;
3443 default:
3444 break;
3445 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003446 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003447 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3448 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003449 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003450 }
3451 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3452 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3453 NL80211_STA_INFO_RX_BITRATE))
3454 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003455 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003456 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3457 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3458 sinfo->rx_packets))
3459 goto nla_put_failure;
3460 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3461 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3462 sinfo->tx_packets))
3463 goto nla_put_failure;
3464 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3465 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3466 sinfo->tx_retries))
3467 goto nla_put_failure;
3468 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3469 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3470 sinfo->tx_failed))
3471 goto nla_put_failure;
3472 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3473 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3474 sinfo->beacon_loss_count))
3475 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003476 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3477 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3478 sinfo->local_pm))
3479 goto nla_put_failure;
3480 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3481 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3482 sinfo->peer_pm))
3483 goto nla_put_failure;
3484 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3485 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3486 sinfo->nonpeer_pm))
3487 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003488 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3489 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3490 if (!bss_param)
3491 goto nla_put_failure;
3492
David S. Miller9360ffd2012-03-29 04:41:26 -04003493 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3494 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3495 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3496 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3497 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3498 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3499 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3500 sinfo->bss_param.dtim_period) ||
3501 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3502 sinfo->bss_param.beacon_interval))
3503 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003504
3505 nla_nest_end(msg, bss_param);
3506 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003507 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3508 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3509 sizeof(struct nl80211_sta_flag_update),
3510 &sinfo->sta_flags))
3511 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003512 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3513 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3514 sinfo->t_offset))
3515 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003516 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003517
David S. Miller9360ffd2012-03-29 04:41:26 -04003518 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3519 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3520 sinfo->assoc_req_ies))
3521 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003522
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003523 return genlmsg_end(msg, hdr);
3524
3525 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003526 genlmsg_cancel(msg, hdr);
3527 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003528}
3529
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003530static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003531 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003532{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003533 struct station_info sinfo;
3534 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003535 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003536 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003537 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003538 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003539
Johannes Berg97990a02013-04-19 01:02:55 +02003540 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003541 if (err)
3542 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003543
Johannes Berg97990a02013-04-19 01:02:55 +02003544 if (!wdev->netdev) {
3545 err = -EINVAL;
3546 goto out_err;
3547 }
3548
Johannes Bergbba95fe2008-07-29 13:22:51 +02003549 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003550 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003551 goto out_err;
3552 }
3553
Johannes Bergbba95fe2008-07-29 13:22:51 +02003554 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003555 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003556 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003557 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003558 if (err == -ENOENT)
3559 break;
3560 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003561 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003562
3563 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003564 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003565 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003566 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003567 &sinfo) < 0)
3568 goto out;
3569
3570 sta_idx++;
3571 }
3572
3573
3574 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003575 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003576 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003577 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003578 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003579
3580 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003581}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003582
Johannes Berg5727ef12007-12-19 02:03:34 +01003583static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3584{
Johannes Berg4c476992010-10-04 21:36:35 +02003585 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3586 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003587 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003588 struct sk_buff *msg;
3589 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003590 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003591
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003592 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003593
3594 if (!info->attrs[NL80211_ATTR_MAC])
3595 return -EINVAL;
3596
3597 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3598
Johannes Berg4c476992010-10-04 21:36:35 +02003599 if (!rdev->ops->get_station)
3600 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003601
Hila Gonene35e4d22012-06-27 17:19:42 +03003602 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003603 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003604 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003605
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003606 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003607 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003608 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003609
Eric W. Biederman15e47302012-09-07 20:12:54 +00003610 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003611 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003612 nlmsg_free(msg);
3613 return -ENOBUFS;
3614 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003615
Johannes Berg4c476992010-10-04 21:36:35 +02003616 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003617}
3618
Johannes Berg77ee7c82013-02-15 00:48:33 +01003619int cfg80211_check_station_change(struct wiphy *wiphy,
3620 struct station_parameters *params,
3621 enum cfg80211_station_type statype)
3622{
3623 if (params->listen_interval != -1)
3624 return -EINVAL;
3625 if (params->aid)
3626 return -EINVAL;
3627
3628 /* When you run into this, adjust the code below for the new flag */
3629 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3630
3631 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003632 case CFG80211_STA_MESH_PEER_KERNEL:
3633 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003634 /*
3635 * No ignoring the TDLS flag here -- the userspace mesh
3636 * code doesn't have the bug of including TDLS in the
3637 * mask everywhere.
3638 */
3639 if (params->sta_flags_mask &
3640 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3641 BIT(NL80211_STA_FLAG_MFP) |
3642 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3643 return -EINVAL;
3644 break;
3645 case CFG80211_STA_TDLS_PEER_SETUP:
3646 case CFG80211_STA_TDLS_PEER_ACTIVE:
3647 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3648 return -EINVAL;
3649 /* ignore since it can't change */
3650 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3651 break;
3652 default:
3653 /* disallow mesh-specific things */
3654 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3655 return -EINVAL;
3656 if (params->local_pm)
3657 return -EINVAL;
3658 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3659 return -EINVAL;
3660 }
3661
3662 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3663 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3664 /* TDLS can't be set, ... */
3665 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3666 return -EINVAL;
3667 /*
3668 * ... but don't bother the driver with it. This works around
3669 * a hostapd/wpa_supplicant issue -- it always includes the
3670 * TLDS_PEER flag in the mask even for AP mode.
3671 */
3672 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3673 }
3674
3675 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3676 /* reject other things that can't change */
3677 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3678 return -EINVAL;
3679 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3680 return -EINVAL;
3681 if (params->supported_rates)
3682 return -EINVAL;
3683 if (params->ext_capab || params->ht_capa || params->vht_capa)
3684 return -EINVAL;
3685 }
3686
3687 if (statype != CFG80211_STA_AP_CLIENT) {
3688 if (params->vlan)
3689 return -EINVAL;
3690 }
3691
3692 switch (statype) {
3693 case CFG80211_STA_AP_MLME_CLIENT:
3694 /* Use this only for authorizing/unauthorizing a station */
3695 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3696 return -EOPNOTSUPP;
3697 break;
3698 case CFG80211_STA_AP_CLIENT:
3699 /* accept only the listed bits */
3700 if (params->sta_flags_mask &
3701 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3702 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3703 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3704 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3705 BIT(NL80211_STA_FLAG_WME) |
3706 BIT(NL80211_STA_FLAG_MFP)))
3707 return -EINVAL;
3708
3709 /* but authenticated/associated only if driver handles it */
3710 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3711 params->sta_flags_mask &
3712 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3713 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3714 return -EINVAL;
3715 break;
3716 case CFG80211_STA_IBSS:
3717 case CFG80211_STA_AP_STA:
3718 /* reject any changes other than AUTHORIZED */
3719 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3720 return -EINVAL;
3721 break;
3722 case CFG80211_STA_TDLS_PEER_SETUP:
3723 /* reject any changes other than AUTHORIZED or WME */
3724 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3725 BIT(NL80211_STA_FLAG_WME)))
3726 return -EINVAL;
3727 /* force (at least) rates when authorizing */
3728 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3729 !params->supported_rates)
3730 return -EINVAL;
3731 break;
3732 case CFG80211_STA_TDLS_PEER_ACTIVE:
3733 /* reject any changes */
3734 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003735 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003736 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3737 return -EINVAL;
3738 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003739 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003740 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3741 return -EINVAL;
3742 break;
3743 }
3744
3745 return 0;
3746}
3747EXPORT_SYMBOL(cfg80211_check_station_change);
3748
Johannes Berg5727ef12007-12-19 02:03:34 +01003749/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003750 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003751 */
Johannes Berg80b99892011-11-18 16:23:01 +01003752static struct net_device *get_vlan(struct genl_info *info,
3753 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003754{
Johannes Berg463d0182009-07-14 00:33:35 +02003755 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003756 struct net_device *v;
3757 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003758
Johannes Berg80b99892011-11-18 16:23:01 +01003759 if (!vlanattr)
3760 return NULL;
3761
3762 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3763 if (!v)
3764 return ERR_PTR(-ENODEV);
3765
3766 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3767 ret = -EINVAL;
3768 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003769 }
Johannes Berg80b99892011-11-18 16:23:01 +01003770
Johannes Berg77ee7c82013-02-15 00:48:33 +01003771 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3772 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3773 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3774 ret = -EINVAL;
3775 goto error;
3776 }
3777
Johannes Berg80b99892011-11-18 16:23:01 +01003778 if (!netif_running(v)) {
3779 ret = -ENETDOWN;
3780 goto error;
3781 }
3782
3783 return v;
3784 error:
3785 dev_put(v);
3786 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003787}
3788
Jouni Malinendf881292013-02-14 21:10:54 +02003789static struct nla_policy
3790nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3791 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3792 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3793};
3794
Johannes Bergff276692013-02-15 00:09:01 +01003795static int nl80211_parse_sta_wme(struct genl_info *info,
3796 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003797{
Jouni Malinendf881292013-02-14 21:10:54 +02003798 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3799 struct nlattr *nla;
3800 int err;
3801
Jouni Malinendf881292013-02-14 21:10:54 +02003802 /* parse WME attributes if present */
3803 if (!info->attrs[NL80211_ATTR_STA_WME])
3804 return 0;
3805
3806 nla = info->attrs[NL80211_ATTR_STA_WME];
3807 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3808 nl80211_sta_wme_policy);
3809 if (err)
3810 return err;
3811
3812 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3813 params->uapsd_queues = nla_get_u8(
3814 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3815 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3816 return -EINVAL;
3817
3818 if (tb[NL80211_STA_WME_MAX_SP])
3819 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3820
3821 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3822 return -EINVAL;
3823
3824 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3825
3826 return 0;
3827}
3828
Johannes Bergff276692013-02-15 00:09:01 +01003829static int nl80211_set_station_tdls(struct genl_info *info,
3830 struct station_parameters *params)
3831{
3832 /* Dummy STA entry gets updated once the peer capabilities are known */
3833 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3834 params->ht_capa =
3835 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3836 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3837 params->vht_capa =
3838 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3839
3840 return nl80211_parse_sta_wme(info, params);
3841}
3842
Johannes Berg5727ef12007-12-19 02:03:34 +01003843static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3844{
Johannes Berg4c476992010-10-04 21:36:35 +02003845 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003846 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003847 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003848 u8 *mac_addr;
3849 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003850
3851 memset(&params, 0, sizeof(params));
3852
3853 params.listen_interval = -1;
3854
Johannes Berg77ee7c82013-02-15 00:48:33 +01003855 if (!rdev->ops->change_station)
3856 return -EOPNOTSUPP;
3857
Johannes Berg5727ef12007-12-19 02:03:34 +01003858 if (info->attrs[NL80211_ATTR_STA_AID])
3859 return -EINVAL;
3860
3861 if (!info->attrs[NL80211_ATTR_MAC])
3862 return -EINVAL;
3863
3864 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3865
3866 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3867 params.supported_rates =
3868 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3869 params.supported_rates_len =
3870 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3871 }
3872
Jouni Malinen9d62a982013-02-14 21:10:13 +02003873 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3874 params.capability =
3875 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3876 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3877 }
3878
3879 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3880 params.ext_capab =
3881 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3882 params.ext_capab_len =
3883 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3884 }
3885
Jouni Malinendf881292013-02-14 21:10:54 +02003886 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003887 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003888
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003889 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003890 return -EINVAL;
3891
Johannes Bergf8bacc22013-02-14 23:27:01 +01003892 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003893 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003894 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3895 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3896 return -EINVAL;
3897 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003898
Johannes Bergf8bacc22013-02-14 23:27:01 +01003899 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003900 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003901 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3902 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3903 return -EINVAL;
3904 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3905 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003906
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003907 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3908 enum nl80211_mesh_power_mode pm = nla_get_u32(
3909 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3910
3911 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3912 pm > NL80211_MESH_POWER_MAX)
3913 return -EINVAL;
3914
3915 params.local_pm = pm;
3916 }
3917
Johannes Berg77ee7c82013-02-15 00:48:33 +01003918 /* Include parameters for TDLS peer (will check later) */
3919 err = nl80211_set_station_tdls(info, &params);
3920 if (err)
3921 return err;
3922
3923 params.vlan = get_vlan(info, rdev);
3924 if (IS_ERR(params.vlan))
3925 return PTR_ERR(params.vlan);
3926
Johannes Berga97f4422009-06-18 17:23:43 +02003927 switch (dev->ieee80211_ptr->iftype) {
3928 case NL80211_IFTYPE_AP:
3929 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003930 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003931 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003932 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01003933 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02003934 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02003935 break;
3936 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003937 err = -EOPNOTSUPP;
3938 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02003939 }
3940
Johannes Berg77ee7c82013-02-15 00:48:33 +01003941 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03003942 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003943
Johannes Berg77ee7c82013-02-15 00:48:33 +01003944 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01003945 if (params.vlan)
3946 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003947
Johannes Berg5727ef12007-12-19 02:03:34 +01003948 return err;
3949}
3950
3951static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3952{
Johannes Berg4c476992010-10-04 21:36:35 +02003953 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003954 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003955 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003956 struct station_parameters params;
3957 u8 *mac_addr = NULL;
3958
3959 memset(&params, 0, sizeof(params));
3960
Johannes Berg984c3112013-02-14 23:43:25 +01003961 if (!rdev->ops->add_station)
3962 return -EOPNOTSUPP;
3963
Johannes Berg5727ef12007-12-19 02:03:34 +01003964 if (!info->attrs[NL80211_ATTR_MAC])
3965 return -EINVAL;
3966
Johannes Berg5727ef12007-12-19 02:03:34 +01003967 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3968 return -EINVAL;
3969
3970 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3971 return -EINVAL;
3972
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003973 if (!info->attrs[NL80211_ATTR_STA_AID])
3974 return -EINVAL;
3975
Johannes Berg5727ef12007-12-19 02:03:34 +01003976 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3977 params.supported_rates =
3978 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3979 params.supported_rates_len =
3980 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3981 params.listen_interval =
3982 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003983
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003984 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3985 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3986 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003987
Jouni Malinen9d62a982013-02-14 21:10:13 +02003988 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3989 params.capability =
3990 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3991 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3992 }
3993
3994 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3995 params.ext_capab =
3996 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3997 params.ext_capab_len =
3998 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3999 }
4000
Jouni Malinen36aedc92008-08-25 11:58:58 +03004001 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4002 params.ht_capa =
4003 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004004
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004005 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4006 params.vht_capa =
4007 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4008
Johannes Bergf8bacc22013-02-14 23:27:01 +01004009 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004010 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004011 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4012 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4013 return -EINVAL;
4014 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004015
Johannes Bergff276692013-02-15 00:09:01 +01004016 err = nl80211_parse_sta_wme(info, &params);
4017 if (err)
4018 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004019
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004020 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004021 return -EINVAL;
4022
Johannes Berg77ee7c82013-02-15 00:48:33 +01004023 /* When you run into this, adjust the code below for the new flag */
4024 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4025
Johannes Bergbdd90d52011-12-14 12:20:27 +01004026 switch (dev->ieee80211_ptr->iftype) {
4027 case NL80211_IFTYPE_AP:
4028 case NL80211_IFTYPE_AP_VLAN:
4029 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004030 /* ignore WME attributes if iface/sta is not capable */
4031 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4032 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4033 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004034
Johannes Bergbdd90d52011-12-14 12:20:27 +01004035 /* TDLS peers cannot be added */
4036 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004037 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004038 /* but don't bother the driver with it */
4039 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004040
Johannes Bergd582cff2012-10-26 17:53:44 +02004041 /* allow authenticated/associated only if driver handles it */
4042 if (!(rdev->wiphy.features &
4043 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4044 params.sta_flags_mask &
4045 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4046 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4047 return -EINVAL;
4048
Johannes Bergbdd90d52011-12-14 12:20:27 +01004049 /* must be last in here for error handling */
4050 params.vlan = get_vlan(info, rdev);
4051 if (IS_ERR(params.vlan))
4052 return PTR_ERR(params.vlan);
4053 break;
4054 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004055 /* ignore uAPSD data */
4056 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4057
Johannes Bergd582cff2012-10-26 17:53:44 +02004058 /* associated is disallowed */
4059 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4060 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004061 /* TDLS peers cannot be added */
4062 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004063 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004064 break;
4065 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004066 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004067 /* ignore uAPSD data */
4068 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4069
Johannes Berg77ee7c82013-02-15 00:48:33 +01004070 /* these are disallowed */
4071 if (params.sta_flags_mask &
4072 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4073 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004074 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004075 /* Only TDLS peers can be added */
4076 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4077 return -EINVAL;
4078 /* Can only add if TDLS ... */
4079 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4080 return -EOPNOTSUPP;
4081 /* ... with external setup is supported */
4082 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4083 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004084 /*
4085 * Older wpa_supplicant versions always mark the TDLS peer
4086 * as authorized, but it shouldn't yet be.
4087 */
4088 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004089 break;
4090 default:
4091 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004092 }
4093
Johannes Bergbdd90d52011-12-14 12:20:27 +01004094 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004095
Hila Gonene35e4d22012-06-27 17:19:42 +03004096 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004097
Johannes Berg5727ef12007-12-19 02:03:34 +01004098 if (params.vlan)
4099 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004100 return err;
4101}
4102
4103static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4104{
Johannes Berg4c476992010-10-04 21:36:35 +02004105 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4106 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004107 u8 *mac_addr = NULL;
4108
4109 if (info->attrs[NL80211_ATTR_MAC])
4110 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4111
Johannes Berge80cf852009-05-11 14:43:13 +02004112 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004113 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004114 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004115 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4116 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004117
Johannes Berg4c476992010-10-04 21:36:35 +02004118 if (!rdev->ops->del_station)
4119 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004120
Hila Gonene35e4d22012-06-27 17:19:42 +03004121 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004122}
4123
Eric W. Biederman15e47302012-09-07 20:12:54 +00004124static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004125 int flags, struct net_device *dev,
4126 u8 *dst, u8 *next_hop,
4127 struct mpath_info *pinfo)
4128{
4129 void *hdr;
4130 struct nlattr *pinfoattr;
4131
Eric W. Biederman15e47302012-09-07 20:12:54 +00004132 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004133 if (!hdr)
4134 return -1;
4135
David S. Miller9360ffd2012-03-29 04:41:26 -04004136 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4137 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4138 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4139 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4140 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004141
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004142 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4143 if (!pinfoattr)
4144 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004145 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4146 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4147 pinfo->frame_qlen))
4148 goto nla_put_failure;
4149 if (((pinfo->filled & MPATH_INFO_SN) &&
4150 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4151 ((pinfo->filled & MPATH_INFO_METRIC) &&
4152 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4153 pinfo->metric)) ||
4154 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4155 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4156 pinfo->exptime)) ||
4157 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4158 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4159 pinfo->flags)) ||
4160 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4161 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4162 pinfo->discovery_timeout)) ||
4163 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4164 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4165 pinfo->discovery_retries)))
4166 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004167
4168 nla_nest_end(msg, pinfoattr);
4169
4170 return genlmsg_end(msg, hdr);
4171
4172 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004173 genlmsg_cancel(msg, hdr);
4174 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004175}
4176
4177static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004178 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004179{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004180 struct mpath_info pinfo;
4181 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004182 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004183 u8 dst[ETH_ALEN];
4184 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004185 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004186 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004187
Johannes Berg97990a02013-04-19 01:02:55 +02004188 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004189 if (err)
4190 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004191
4192 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004193 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004194 goto out_err;
4195 }
4196
Johannes Berg97990a02013-04-19 01:02:55 +02004197 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004198 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004199 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004200 }
4201
Johannes Bergbba95fe2008-07-29 13:22:51 +02004202 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004203 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4204 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004205 if (err == -ENOENT)
4206 break;
4207 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004208 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004209
Eric W. Biederman15e47302012-09-07 20:12:54 +00004210 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004211 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004212 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004213 &pinfo) < 0)
4214 goto out;
4215
4216 path_idx++;
4217 }
4218
4219
4220 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004221 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004222 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004223 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004224 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004225 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004226}
4227
4228static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4229{
Johannes Berg4c476992010-10-04 21:36:35 +02004230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004231 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004232 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004233 struct mpath_info pinfo;
4234 struct sk_buff *msg;
4235 u8 *dst = NULL;
4236 u8 next_hop[ETH_ALEN];
4237
4238 memset(&pinfo, 0, sizeof(pinfo));
4239
4240 if (!info->attrs[NL80211_ATTR_MAC])
4241 return -EINVAL;
4242
4243 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4244
Johannes Berg4c476992010-10-04 21:36:35 +02004245 if (!rdev->ops->get_mpath)
4246 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004247
Johannes Berg4c476992010-10-04 21:36:35 +02004248 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4249 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004250
Hila Gonene35e4d22012-06-27 17:19:42 +03004251 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004252 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004253 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004254
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004255 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004256 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004257 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004258
Eric W. Biederman15e47302012-09-07 20:12:54 +00004259 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004260 dev, dst, next_hop, &pinfo) < 0) {
4261 nlmsg_free(msg);
4262 return -ENOBUFS;
4263 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004264
Johannes Berg4c476992010-10-04 21:36:35 +02004265 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004266}
4267
4268static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4269{
Johannes Berg4c476992010-10-04 21:36:35 +02004270 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4271 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004272 u8 *dst = NULL;
4273 u8 *next_hop = NULL;
4274
4275 if (!info->attrs[NL80211_ATTR_MAC])
4276 return -EINVAL;
4277
4278 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4279 return -EINVAL;
4280
4281 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4282 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4283
Johannes Berg4c476992010-10-04 21:36:35 +02004284 if (!rdev->ops->change_mpath)
4285 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004286
Johannes Berg4c476992010-10-04 21:36:35 +02004287 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4288 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004289
Hila Gonene35e4d22012-06-27 17:19:42 +03004290 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004291}
Johannes Berg4c476992010-10-04 21:36:35 +02004292
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004293static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4294{
Johannes Berg4c476992010-10-04 21:36:35 +02004295 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4296 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004297 u8 *dst = NULL;
4298 u8 *next_hop = NULL;
4299
4300 if (!info->attrs[NL80211_ATTR_MAC])
4301 return -EINVAL;
4302
4303 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4304 return -EINVAL;
4305
4306 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4307 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4308
Johannes Berg4c476992010-10-04 21:36:35 +02004309 if (!rdev->ops->add_mpath)
4310 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004311
Johannes Berg4c476992010-10-04 21:36:35 +02004312 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4313 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004314
Hila Gonene35e4d22012-06-27 17:19:42 +03004315 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004316}
4317
4318static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4319{
Johannes Berg4c476992010-10-04 21:36:35 +02004320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4321 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004322 u8 *dst = NULL;
4323
4324 if (info->attrs[NL80211_ATTR_MAC])
4325 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4326
Johannes Berg4c476992010-10-04 21:36:35 +02004327 if (!rdev->ops->del_mpath)
4328 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004329
Hila Gonene35e4d22012-06-27 17:19:42 +03004330 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004331}
4332
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004333static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4334{
Johannes Berg4c476992010-10-04 21:36:35 +02004335 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4336 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004337 struct bss_parameters params;
4338
4339 memset(&params, 0, sizeof(params));
4340 /* default to not changing parameters */
4341 params.use_cts_prot = -1;
4342 params.use_short_preamble = -1;
4343 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004344 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004345 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004346 params.p2p_ctwindow = -1;
4347 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004348
4349 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4350 params.use_cts_prot =
4351 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4352 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4353 params.use_short_preamble =
4354 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4355 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4356 params.use_short_slot_time =
4357 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004358 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4359 params.basic_rates =
4360 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4361 params.basic_rates_len =
4362 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4363 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004364 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4365 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004366 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4367 params.ht_opmode =
4368 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004369
Johannes Berg53cabad2012-11-14 15:17:28 +01004370 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4371 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4372 return -EINVAL;
4373 params.p2p_ctwindow =
4374 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4375 if (params.p2p_ctwindow < 0)
4376 return -EINVAL;
4377 if (params.p2p_ctwindow != 0 &&
4378 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4379 return -EINVAL;
4380 }
4381
4382 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4383 u8 tmp;
4384
4385 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4386 return -EINVAL;
4387 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4388 if (tmp > 1)
4389 return -EINVAL;
4390 params.p2p_opp_ps = tmp;
4391 if (params.p2p_opp_ps &&
4392 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4393 return -EINVAL;
4394 }
4395
Johannes Berg4c476992010-10-04 21:36:35 +02004396 if (!rdev->ops->change_bss)
4397 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004398
Johannes Berg074ac8d2010-09-16 14:58:22 +02004399 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004400 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4401 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004402
Hila Gonene35e4d22012-06-27 17:19:42 +03004403 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004404}
4405
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004406static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004407 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4408 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4409 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4410 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4411 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4412 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4413};
4414
4415static int parse_reg_rule(struct nlattr *tb[],
4416 struct ieee80211_reg_rule *reg_rule)
4417{
4418 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4419 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4420
4421 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4422 return -EINVAL;
4423 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4424 return -EINVAL;
4425 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4426 return -EINVAL;
4427 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4428 return -EINVAL;
4429 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4430 return -EINVAL;
4431
4432 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4433
4434 freq_range->start_freq_khz =
4435 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4436 freq_range->end_freq_khz =
4437 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4438 freq_range->max_bandwidth_khz =
4439 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4440
4441 power_rule->max_eirp =
4442 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4443
4444 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4445 power_rule->max_antenna_gain =
4446 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4447
4448 return 0;
4449}
4450
4451static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4452{
4453 int r;
4454 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004455 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004456
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004457 /*
4458 * You should only get this when cfg80211 hasn't yet initialized
4459 * completely when built-in to the kernel right between the time
4460 * window between nl80211_init() and regulatory_init(), if that is
4461 * even possible.
4462 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004463 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004464 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004465
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004466 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4467 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004468
4469 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4470
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004471 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4472 user_reg_hint_type =
4473 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4474 else
4475 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4476
4477 switch (user_reg_hint_type) {
4478 case NL80211_USER_REG_HINT_USER:
4479 case NL80211_USER_REG_HINT_CELL_BASE:
4480 break;
4481 default:
4482 return -EINVAL;
4483 }
4484
4485 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004486
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004487 return r;
4488}
4489
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004490static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004491 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004492{
Johannes Berg4c476992010-10-04 21:36:35 +02004493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004494 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004495 struct wireless_dev *wdev = dev->ieee80211_ptr;
4496 struct mesh_config cur_params;
4497 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004498 void *hdr;
4499 struct nlattr *pinfoattr;
4500 struct sk_buff *msg;
4501
Johannes Berg29cbe682010-12-03 09:20:44 +01004502 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4503 return -EOPNOTSUPP;
4504
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004505 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004506 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004507
Johannes Berg29cbe682010-12-03 09:20:44 +01004508 wdev_lock(wdev);
4509 /* If not connected, get default parameters */
4510 if (!wdev->mesh_id_len)
4511 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4512 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004513 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004514 wdev_unlock(wdev);
4515
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004516 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004517 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004518
4519 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004520 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004521 if (!msg)
4522 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004523 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004524 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004525 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004526 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004527 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004528 if (!pinfoattr)
4529 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004530 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4531 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4532 cur_params.dot11MeshRetryTimeout) ||
4533 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4534 cur_params.dot11MeshConfirmTimeout) ||
4535 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4536 cur_params.dot11MeshHoldingTimeout) ||
4537 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4538 cur_params.dot11MeshMaxPeerLinks) ||
4539 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4540 cur_params.dot11MeshMaxRetries) ||
4541 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4542 cur_params.dot11MeshTTL) ||
4543 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4544 cur_params.element_ttl) ||
4545 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4546 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004547 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4548 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004549 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4550 cur_params.dot11MeshHWMPmaxPREQretries) ||
4551 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4552 cur_params.path_refresh_time) ||
4553 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4554 cur_params.min_discovery_timeout) ||
4555 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4556 cur_params.dot11MeshHWMPactivePathTimeout) ||
4557 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4558 cur_params.dot11MeshHWMPpreqMinInterval) ||
4559 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4560 cur_params.dot11MeshHWMPperrMinInterval) ||
4561 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4562 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4563 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4564 cur_params.dot11MeshHWMPRootMode) ||
4565 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4566 cur_params.dot11MeshHWMPRannInterval) ||
4567 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4568 cur_params.dot11MeshGateAnnouncementProtocol) ||
4569 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4570 cur_params.dot11MeshForwarding) ||
4571 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004572 cur_params.rssi_threshold) ||
4573 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004574 cur_params.ht_opmode) ||
4575 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4576 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4577 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004578 cur_params.dot11MeshHWMProotInterval) ||
4579 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004580 cur_params.dot11MeshHWMPconfirmationInterval) ||
4581 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4582 cur_params.power_mode) ||
4583 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4584 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004585 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004586 nla_nest_end(msg, pinfoattr);
4587 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004588 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004589
Johannes Berg3b858752009-03-12 09:55:09 +01004590 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004591 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004592 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004593 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004594 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004595}
4596
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004597static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004598 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4599 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4600 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4601 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4602 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4603 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004604 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004605 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004606 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004607 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4608 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4609 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4610 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4611 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004612 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004613 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004614 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004615 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004616 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004617 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004618 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4619 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004620 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4621 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004622 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004623 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4624 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004625};
4626
Javier Cardonac80d5452010-12-16 17:37:49 -08004627static const struct nla_policy
4628 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004629 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004630 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4631 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004632 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004633 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004634 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004635 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004636 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004637};
4638
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004639static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004640 struct mesh_config *cfg,
4641 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004642{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004643 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004644 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004645
Marco Porschea54fba2013-01-07 16:04:48 +01004646#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4647do { \
4648 if (tb[attr]) { \
4649 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4650 return -EINVAL; \
4651 cfg->param = fn(tb[attr]); \
4652 mask |= (1 << (attr - 1)); \
4653 } \
4654} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004655
4656
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004657 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004658 return -EINVAL;
4659 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004660 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004661 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004662 return -EINVAL;
4663
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004664 /* This makes sure that there aren't more than 32 mesh config
4665 * parameters (otherwise our bitfield scheme would not work.) */
4666 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4667
4668 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004669 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004670 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4671 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004672 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004673 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4674 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004675 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004676 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4677 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004678 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004679 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4680 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004681 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004682 mask, NL80211_MESHCONF_MAX_RETRIES,
4683 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004684 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004685 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004686 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004687 mask, NL80211_MESHCONF_ELEMENT_TTL,
4688 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004689 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004690 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4691 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004692 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4693 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004694 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4695 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004696 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004697 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4698 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004699 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004700 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4701 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004702 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004703 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4704 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004705 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4706 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004707 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4708 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004709 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004710 1, 65535, mask,
4711 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004712 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004713 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004714 1, 65535, mask,
4715 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004716 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004717 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004718 dot11MeshHWMPnetDiameterTraversalTime,
4719 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004720 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4721 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004722 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4723 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4724 nla_get_u8);
4725 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4726 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004727 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004728 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004729 dot11MeshGateAnnouncementProtocol, 0, 1,
4730 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004731 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004732 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004733 mask, NL80211_MESHCONF_FORWARDING,
4734 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004735 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004736 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4737 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004738 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004739 mask, NL80211_MESHCONF_HT_OPMODE,
4740 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004741 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004742 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004743 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4744 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004745 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004746 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4747 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004748 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004749 dot11MeshHWMPconfirmationInterval,
4750 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004751 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4752 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004753 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4754 NL80211_MESH_POWER_ACTIVE,
4755 NL80211_MESH_POWER_MAX,
4756 mask, NL80211_MESHCONF_POWER_MODE,
4757 nla_get_u32);
4758 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4759 0, 65535, mask,
4760 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004761 if (mask_out)
4762 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004763
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004764 return 0;
4765
4766#undef FILL_IN_MESH_PARAM_IF_SET
4767}
4768
Javier Cardonac80d5452010-12-16 17:37:49 -08004769static int nl80211_parse_mesh_setup(struct genl_info *info,
4770 struct mesh_setup *setup)
4771{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004772 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004773 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4774
4775 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4776 return -EINVAL;
4777 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4778 info->attrs[NL80211_ATTR_MESH_SETUP],
4779 nl80211_mesh_setup_params_policy))
4780 return -EINVAL;
4781
Javier Cardonad299a1f2012-03-31 11:31:33 -07004782 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4783 setup->sync_method =
4784 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4785 IEEE80211_SYNC_METHOD_VENDOR :
4786 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4787
Javier Cardonac80d5452010-12-16 17:37:49 -08004788 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4789 setup->path_sel_proto =
4790 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4791 IEEE80211_PATH_PROTOCOL_VENDOR :
4792 IEEE80211_PATH_PROTOCOL_HWMP;
4793
4794 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4795 setup->path_metric =
4796 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4797 IEEE80211_PATH_METRIC_VENDOR :
4798 IEEE80211_PATH_METRIC_AIRTIME;
4799
Javier Cardona581a8b02011-04-07 15:08:27 -07004800
4801 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004802 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004803 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004804 if (!is_valid_ie_attr(ieattr))
4805 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004806 setup->ie = nla_data(ieattr);
4807 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004808 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004809 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4810 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4811 return -EINVAL;
4812 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004813 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4814 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004815 if (setup->is_secure)
4816 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08004817
4818 return 0;
4819}
4820
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004821static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004822 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004823{
4824 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4825 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004826 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004827 struct mesh_config cfg;
4828 u32 mask;
4829 int err;
4830
Johannes Berg29cbe682010-12-03 09:20:44 +01004831 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4832 return -EOPNOTSUPP;
4833
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004834 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004835 return -EOPNOTSUPP;
4836
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004837 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004838 if (err)
4839 return err;
4840
Johannes Berg29cbe682010-12-03 09:20:44 +01004841 wdev_lock(wdev);
4842 if (!wdev->mesh_id_len)
4843 err = -ENOLINK;
4844
4845 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004846 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004847
4848 wdev_unlock(wdev);
4849
4850 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004851}
4852
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004853static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4854{
Johannes Berg458f4f92012-12-06 15:47:38 +01004855 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004856 struct sk_buff *msg;
4857 void *hdr = NULL;
4858 struct nlattr *nl_reg_rules;
4859 unsigned int i;
4860 int err = -EINVAL;
4861
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004862 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004863
4864 if (!cfg80211_regdomain)
4865 goto out;
4866
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004867 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004868 if (!msg) {
4869 err = -ENOBUFS;
4870 goto out;
4871 }
4872
Eric W. Biederman15e47302012-09-07 20:12:54 +00004873 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004874 NL80211_CMD_GET_REG);
4875 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004876 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004877
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004878 if (reg_last_request_cell_base() &&
4879 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4880 NL80211_USER_REG_HINT_CELL_BASE))
4881 goto nla_put_failure;
4882
Johannes Berg458f4f92012-12-06 15:47:38 +01004883 rcu_read_lock();
4884 regdom = rcu_dereference(cfg80211_regdomain);
4885
4886 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4887 (regdom->dfs_region &&
4888 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4889 goto nla_put_failure_rcu;
4890
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004891 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4892 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004893 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004894
Johannes Berg458f4f92012-12-06 15:47:38 +01004895 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004896 struct nlattr *nl_reg_rule;
4897 const struct ieee80211_reg_rule *reg_rule;
4898 const struct ieee80211_freq_range *freq_range;
4899 const struct ieee80211_power_rule *power_rule;
4900
Johannes Berg458f4f92012-12-06 15:47:38 +01004901 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004902 freq_range = &reg_rule->freq_range;
4903 power_rule = &reg_rule->power_rule;
4904
4905 nl_reg_rule = nla_nest_start(msg, i);
4906 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004907 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004908
David S. Miller9360ffd2012-03-29 04:41:26 -04004909 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4910 reg_rule->flags) ||
4911 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4912 freq_range->start_freq_khz) ||
4913 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4914 freq_range->end_freq_khz) ||
4915 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4916 freq_range->max_bandwidth_khz) ||
4917 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4918 power_rule->max_antenna_gain) ||
4919 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4920 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004921 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004922
4923 nla_nest_end(msg, nl_reg_rule);
4924 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004925 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004926
4927 nla_nest_end(msg, nl_reg_rules);
4928
4929 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004930 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004931 goto out;
4932
Johannes Berg458f4f92012-12-06 15:47:38 +01004933nla_put_failure_rcu:
4934 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004935nla_put_failure:
4936 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004937put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004938 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004939 err = -EMSGSIZE;
4940out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004941 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004942 return err;
4943}
4944
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004945static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4946{
4947 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4948 struct nlattr *nl_reg_rule;
4949 char *alpha2 = NULL;
4950 int rem_reg_rules = 0, r = 0;
4951 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004952 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004953 struct ieee80211_regdomain *rd = NULL;
4954
4955 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4956 return -EINVAL;
4957
4958 if (!info->attrs[NL80211_ATTR_REG_RULES])
4959 return -EINVAL;
4960
4961 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4962
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004963 if (info->attrs[NL80211_ATTR_DFS_REGION])
4964 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4965
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004966 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004967 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004968 num_rules++;
4969 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004970 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004971 }
4972
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004973 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004974 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004975
4976 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004977 if (!rd)
4978 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004979
4980 rd->n_reg_rules = num_rules;
4981 rd->alpha2[0] = alpha2[0];
4982 rd->alpha2[1] = alpha2[1];
4983
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004984 /*
4985 * Disable DFS master mode if the DFS region was
4986 * not supported or known on this kernel.
4987 */
4988 if (reg_supported_dfs_region(dfs_region))
4989 rd->dfs_region = dfs_region;
4990
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004991 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004992 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004993 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004994 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4995 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004996 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4997 if (r)
4998 goto bad_reg;
4999
5000 rule_idx++;
5001
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005002 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5003 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005004 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005005 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005006 }
5007
Johannes Berg6913b492012-12-04 00:48:59 +01005008 mutex_lock(&cfg80211_mutex);
5009
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005010 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005011 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005012 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01005013 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005014
Johannes Bergd2372b32008-10-24 20:32:20 +02005015 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005016 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005017 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005018}
5019
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005020static int validate_scan_freqs(struct nlattr *freqs)
5021{
5022 struct nlattr *attr1, *attr2;
5023 int n_channels = 0, tmp1, tmp2;
5024
5025 nla_for_each_nested(attr1, freqs, tmp1) {
5026 n_channels++;
5027 /*
5028 * Some hardware has a limited channel list for
5029 * scanning, and it is pretty much nonsensical
5030 * to scan for a channel twice, so disallow that
5031 * and don't require drivers to check that the
5032 * channel list they get isn't longer than what
5033 * they can scan, as long as they can scan all
5034 * the channels they registered at once.
5035 */
5036 nla_for_each_nested(attr2, freqs, tmp2)
5037 if (attr1 != attr2 &&
5038 nla_get_u32(attr1) == nla_get_u32(attr2))
5039 return 0;
5040 }
5041
5042 return n_channels;
5043}
5044
Johannes Berg2a519312009-02-10 21:25:55 +01005045static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5046{
Johannes Berg4c476992010-10-04 21:36:35 +02005047 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005048 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005049 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005050 struct nlattr *attr;
5051 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005052 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005053 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005054
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005055 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5056 return -EINVAL;
5057
Johannes Berg79c97e92009-07-07 03:56:12 +02005058 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005059
Johannes Berg4c476992010-10-04 21:36:35 +02005060 if (!rdev->ops->scan)
5061 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005062
Johannes Bergf9f47522013-03-19 15:04:07 +01005063 mutex_lock(&rdev->sched_scan_mtx);
5064 if (rdev->scan_req) {
5065 err = -EBUSY;
5066 goto unlock;
5067 }
Johannes Berg2a519312009-02-10 21:25:55 +01005068
5069 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005070 n_channels = validate_scan_freqs(
5071 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005072 if (!n_channels) {
5073 err = -EINVAL;
5074 goto unlock;
5075 }
Johannes Berg2a519312009-02-10 21:25:55 +01005076 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005077 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005078 n_channels = 0;
5079
Johannes Berg2a519312009-02-10 21:25:55 +01005080 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5081 if (wiphy->bands[band])
5082 n_channels += wiphy->bands[band]->n_channels;
5083 }
5084
5085 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5086 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5087 n_ssids++;
5088
Johannes Bergf9f47522013-03-19 15:04:07 +01005089 if (n_ssids > wiphy->max_scan_ssids) {
5090 err = -EINVAL;
5091 goto unlock;
5092 }
Johannes Berg2a519312009-02-10 21:25:55 +01005093
Jouni Malinen70692ad2009-02-16 19:39:13 +02005094 if (info->attrs[NL80211_ATTR_IE])
5095 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5096 else
5097 ie_len = 0;
5098
Johannes Bergf9f47522013-03-19 15:04:07 +01005099 if (ie_len > wiphy->max_scan_ie_len) {
5100 err = -EINVAL;
5101 goto unlock;
5102 }
Johannes Berg18a83652009-03-31 12:12:05 +02005103
Johannes Berg2a519312009-02-10 21:25:55 +01005104 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005105 + sizeof(*request->ssids) * n_ssids
5106 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005107 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005108 if (!request) {
5109 err = -ENOMEM;
5110 goto unlock;
5111 }
Johannes Berg2a519312009-02-10 21:25:55 +01005112
Johannes Berg2a519312009-02-10 21:25:55 +01005113 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005114 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005115 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005116 if (ie_len) {
5117 if (request->ssids)
5118 request->ie = (void *)(request->ssids + n_ssids);
5119 else
5120 request->ie = (void *)(request->channels + n_channels);
5121 }
Johannes Berg2a519312009-02-10 21:25:55 +01005122
Johannes Berg584991d2009-11-02 13:32:03 +01005123 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005124 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5125 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005126 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005127 struct ieee80211_channel *chan;
5128
5129 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5130
5131 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005132 err = -EINVAL;
5133 goto out_free;
5134 }
Johannes Berg584991d2009-11-02 13:32:03 +01005135
5136 /* ignore disabled channels */
5137 if (chan->flags & IEEE80211_CHAN_DISABLED)
5138 continue;
5139
5140 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005141 i++;
5142 }
5143 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005144 enum ieee80211_band band;
5145
Johannes Berg2a519312009-02-10 21:25:55 +01005146 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005147 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5148 int j;
5149 if (!wiphy->bands[band])
5150 continue;
5151 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005152 struct ieee80211_channel *chan;
5153
5154 chan = &wiphy->bands[band]->channels[j];
5155
5156 if (chan->flags & IEEE80211_CHAN_DISABLED)
5157 continue;
5158
5159 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005160 i++;
5161 }
5162 }
5163 }
5164
Johannes Berg584991d2009-11-02 13:32:03 +01005165 if (!i) {
5166 err = -EINVAL;
5167 goto out_free;
5168 }
5169
5170 request->n_channels = i;
5171
Johannes Berg2a519312009-02-10 21:25:55 +01005172 i = 0;
5173 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5174 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005175 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005176 err = -EINVAL;
5177 goto out_free;
5178 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005179 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005180 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005181 i++;
5182 }
5183 }
5184
Jouni Malinen70692ad2009-02-16 19:39:13 +02005185 if (info->attrs[NL80211_ATTR_IE]) {
5186 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005187 memcpy((void *)request->ie,
5188 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005189 request->ie_len);
5190 }
5191
Johannes Berg34850ab2011-07-18 18:08:35 +02005192 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005193 if (wiphy->bands[i])
5194 request->rates[i] =
5195 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005196
5197 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5198 nla_for_each_nested(attr,
5199 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5200 tmp) {
5201 enum ieee80211_band band = nla_type(attr);
5202
Dan Carpenter84404622011-07-29 11:52:18 +03005203 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005204 err = -EINVAL;
5205 goto out_free;
5206 }
5207 err = ieee80211_get_ratemask(wiphy->bands[band],
5208 nla_data(attr),
5209 nla_len(attr),
5210 &request->rates[band]);
5211 if (err)
5212 goto out_free;
5213 }
5214 }
5215
Sam Leffler46856bb2012-10-11 21:03:32 -07005216 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005217 request->flags = nla_get_u32(
5218 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005219 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5220 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5221 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5222 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005223 err = -EOPNOTSUPP;
5224 goto out_free;
5225 }
5226 }
Sam Lefflered4737712012-10-11 21:03:31 -07005227
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305228 request->no_cck =
5229 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5230
Johannes Bergfd014282012-06-18 19:17:03 +02005231 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005232 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005233 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005234
Johannes Berg79c97e92009-07-07 03:56:12 +02005235 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005236 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005237
Johannes Berg463d0182009-07-14 00:33:35 +02005238 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005239 nl80211_send_scan_start(rdev, wdev);
5240 if (wdev->netdev)
5241 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005242 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005243 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005244 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005245 kfree(request);
5246 }
Johannes Berg3b858752009-03-12 09:55:09 +01005247
Johannes Bergf9f47522013-03-19 15:04:07 +01005248 unlock:
5249 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01005250 return err;
5251}
5252
Luciano Coelho807f8a82011-05-11 17:09:35 +03005253static int nl80211_start_sched_scan(struct sk_buff *skb,
5254 struct genl_info *info)
5255{
5256 struct cfg80211_sched_scan_request *request;
5257 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5258 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005259 struct nlattr *attr;
5260 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005261 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005262 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005263 enum ieee80211_band band;
5264 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005265 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005266
5267 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5268 !rdev->ops->sched_scan_start)
5269 return -EOPNOTSUPP;
5270
5271 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5272 return -EINVAL;
5273
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005274 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5275 return -EINVAL;
5276
5277 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5278 if (interval == 0)
5279 return -EINVAL;
5280
Luciano Coelho807f8a82011-05-11 17:09:35 +03005281 wiphy = &rdev->wiphy;
5282
5283 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5284 n_channels = validate_scan_freqs(
5285 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5286 if (!n_channels)
5287 return -EINVAL;
5288 } else {
5289 n_channels = 0;
5290
5291 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5292 if (wiphy->bands[band])
5293 n_channels += wiphy->bands[band]->n_channels;
5294 }
5295
5296 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5297 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5298 tmp)
5299 n_ssids++;
5300
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005301 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005302 return -EINVAL;
5303
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005304 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5305 nla_for_each_nested(attr,
5306 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5307 tmp)
5308 n_match_sets++;
5309
5310 if (n_match_sets > wiphy->max_match_sets)
5311 return -EINVAL;
5312
Luciano Coelho807f8a82011-05-11 17:09:35 +03005313 if (info->attrs[NL80211_ATTR_IE])
5314 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5315 else
5316 ie_len = 0;
5317
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005318 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005319 return -EINVAL;
5320
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005321 mutex_lock(&rdev->sched_scan_mtx);
5322
5323 if (rdev->sched_scan_req) {
5324 err = -EINPROGRESS;
5325 goto out;
5326 }
5327
Luciano Coelho807f8a82011-05-11 17:09:35 +03005328 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005329 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005330 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005331 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005332 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005333 if (!request) {
5334 err = -ENOMEM;
5335 goto out;
5336 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005337
5338 if (n_ssids)
5339 request->ssids = (void *)&request->channels[n_channels];
5340 request->n_ssids = n_ssids;
5341 if (ie_len) {
5342 if (request->ssids)
5343 request->ie = (void *)(request->ssids + n_ssids);
5344 else
5345 request->ie = (void *)(request->channels + n_channels);
5346 }
5347
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005348 if (n_match_sets) {
5349 if (request->ie)
5350 request->match_sets = (void *)(request->ie + ie_len);
5351 else if (request->ssids)
5352 request->match_sets =
5353 (void *)(request->ssids + n_ssids);
5354 else
5355 request->match_sets =
5356 (void *)(request->channels + n_channels);
5357 }
5358 request->n_match_sets = n_match_sets;
5359
Luciano Coelho807f8a82011-05-11 17:09:35 +03005360 i = 0;
5361 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5362 /* user specified, bail out if channel not found */
5363 nla_for_each_nested(attr,
5364 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5365 tmp) {
5366 struct ieee80211_channel *chan;
5367
5368 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5369
5370 if (!chan) {
5371 err = -EINVAL;
5372 goto out_free;
5373 }
5374
5375 /* ignore disabled channels */
5376 if (chan->flags & IEEE80211_CHAN_DISABLED)
5377 continue;
5378
5379 request->channels[i] = chan;
5380 i++;
5381 }
5382 } else {
5383 /* all channels */
5384 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5385 int j;
5386 if (!wiphy->bands[band])
5387 continue;
5388 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5389 struct ieee80211_channel *chan;
5390
5391 chan = &wiphy->bands[band]->channels[j];
5392
5393 if (chan->flags & IEEE80211_CHAN_DISABLED)
5394 continue;
5395
5396 request->channels[i] = chan;
5397 i++;
5398 }
5399 }
5400 }
5401
5402 if (!i) {
5403 err = -EINVAL;
5404 goto out_free;
5405 }
5406
5407 request->n_channels = i;
5408
5409 i = 0;
5410 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5411 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5412 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005413 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005414 err = -EINVAL;
5415 goto out_free;
5416 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005417 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005418 memcpy(request->ssids[i].ssid, nla_data(attr),
5419 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005420 i++;
5421 }
5422 }
5423
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005424 i = 0;
5425 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5426 nla_for_each_nested(attr,
5427 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5428 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005429 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005430
5431 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5432 nla_data(attr), nla_len(attr),
5433 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005434 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005435 if (ssid) {
5436 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5437 err = -EINVAL;
5438 goto out_free;
5439 }
5440 memcpy(request->match_sets[i].ssid.ssid,
5441 nla_data(ssid), nla_len(ssid));
5442 request->match_sets[i].ssid.ssid_len =
5443 nla_len(ssid);
5444 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005445 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5446 if (rssi)
5447 request->rssi_thold = nla_get_u32(rssi);
5448 else
5449 request->rssi_thold =
5450 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005451 i++;
5452 }
5453 }
5454
Luciano Coelho807f8a82011-05-11 17:09:35 +03005455 if (info->attrs[NL80211_ATTR_IE]) {
5456 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5457 memcpy((void *)request->ie,
5458 nla_data(info->attrs[NL80211_ATTR_IE]),
5459 request->ie_len);
5460 }
5461
Sam Leffler46856bb2012-10-11 21:03:32 -07005462 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005463 request->flags = nla_get_u32(
5464 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005465 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5466 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5467 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5468 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005469 err = -EOPNOTSUPP;
5470 goto out_free;
5471 }
5472 }
Sam Lefflered4737712012-10-11 21:03:31 -07005473
Luciano Coelho807f8a82011-05-11 17:09:35 +03005474 request->dev = dev;
5475 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005476 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005477 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005478
Hila Gonene35e4d22012-06-27 17:19:42 +03005479 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005480 if (!err) {
5481 rdev->sched_scan_req = request;
5482 nl80211_send_sched_scan(rdev, dev,
5483 NL80211_CMD_START_SCHED_SCAN);
5484 goto out;
5485 }
5486
5487out_free:
5488 kfree(request);
5489out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005490 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005491 return err;
5492}
5493
5494static int nl80211_stop_sched_scan(struct sk_buff *skb,
5495 struct genl_info *info)
5496{
5497 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005498 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005499
5500 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5501 !rdev->ops->sched_scan_stop)
5502 return -EOPNOTSUPP;
5503
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005504 mutex_lock(&rdev->sched_scan_mtx);
5505 err = __cfg80211_stop_sched_scan(rdev, false);
5506 mutex_unlock(&rdev->sched_scan_mtx);
5507
5508 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005509}
5510
Simon Wunderlich04f39042013-02-08 18:16:19 +01005511static int nl80211_start_radar_detection(struct sk_buff *skb,
5512 struct genl_info *info)
5513{
5514 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5515 struct net_device *dev = info->user_ptr[1];
5516 struct wireless_dev *wdev = dev->ieee80211_ptr;
5517 struct cfg80211_chan_def chandef;
5518 int err;
5519
5520 err = nl80211_parse_chandef(rdev, info, &chandef);
5521 if (err)
5522 return err;
5523
5524 if (wdev->cac_started)
5525 return -EBUSY;
5526
5527 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5528 if (err < 0)
5529 return err;
5530
5531 if (err == 0)
5532 return -EINVAL;
5533
5534 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5535 return -EINVAL;
5536
5537 if (!rdev->ops->start_radar_detection)
5538 return -EOPNOTSUPP;
5539
5540 mutex_lock(&rdev->devlist_mtx);
5541 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5542 chandef.chan, CHAN_MODE_SHARED,
5543 BIT(chandef.width));
5544 if (err)
5545 goto err_locked;
5546
5547 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5548 if (!err) {
5549 wdev->channel = chandef.chan;
5550 wdev->cac_started = true;
5551 wdev->cac_start_time = jiffies;
5552 }
5553err_locked:
5554 mutex_unlock(&rdev->devlist_mtx);
5555
5556 return err;
5557}
5558
Johannes Berg9720bb32011-06-21 09:45:33 +02005559static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5560 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005561 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005562 struct wireless_dev *wdev,
5563 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005564{
Johannes Berg48ab9052009-07-10 18:42:31 +02005565 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005566 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005567 void *hdr;
5568 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005569 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005570
5571 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005572
Eric W. Biederman15e47302012-09-07 20:12:54 +00005573 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005574 NL80211_CMD_NEW_SCAN_RESULTS);
5575 if (!hdr)
5576 return -1;
5577
Johannes Berg9720bb32011-06-21 09:45:33 +02005578 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5579
Johannes Berg97990a02013-04-19 01:02:55 +02005580 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5581 goto nla_put_failure;
5582 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005583 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5584 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005585 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5586 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005587
5588 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5589 if (!bss)
5590 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005591 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005592 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005593 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005594
5595 rcu_read_lock();
5596 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005597 if (ies) {
5598 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5599 goto fail_unlock_rcu;
5600 tsf = true;
5601 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5602 ies->len, ies->data))
5603 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005604 }
5605 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005606 if (ies) {
5607 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5608 goto fail_unlock_rcu;
5609 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5610 ies->len, ies->data))
5611 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005612 }
5613 rcu_read_unlock();
5614
David S. Miller9360ffd2012-03-29 04:41:26 -04005615 if (res->beacon_interval &&
5616 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5617 goto nla_put_failure;
5618 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5619 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5620 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5621 jiffies_to_msecs(jiffies - intbss->ts)))
5622 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005623
Johannes Berg77965c92009-02-18 18:45:06 +01005624 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005625 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005626 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5627 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005628 break;
5629 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005630 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5631 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005632 break;
5633 default:
5634 break;
5635 }
5636
Johannes Berg48ab9052009-07-10 18:42:31 +02005637 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005638 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005639 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005640 if (intbss == wdev->current_bss &&
5641 nla_put_u32(msg, NL80211_BSS_STATUS,
5642 NL80211_BSS_STATUS_ASSOCIATED))
5643 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005644 break;
5645 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005646 if (intbss == wdev->current_bss &&
5647 nla_put_u32(msg, NL80211_BSS_STATUS,
5648 NL80211_BSS_STATUS_IBSS_JOINED))
5649 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005650 break;
5651 default:
5652 break;
5653 }
5654
Johannes Berg2a519312009-02-10 21:25:55 +01005655 nla_nest_end(msg, bss);
5656
5657 return genlmsg_end(msg, hdr);
5658
Johannes Berg8cef2c92013-02-05 16:54:31 +01005659 fail_unlock_rcu:
5660 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005661 nla_put_failure:
5662 genlmsg_cancel(msg, hdr);
5663 return -EMSGSIZE;
5664}
5665
Johannes Berg97990a02013-04-19 01:02:55 +02005666static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005667{
Johannes Berg48ab9052009-07-10 18:42:31 +02005668 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005669 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005670 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005671 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005672 int err;
5673
Johannes Berg97990a02013-04-19 01:02:55 +02005674 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005675 if (err)
5676 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005677
Johannes Berg48ab9052009-07-10 18:42:31 +02005678 wdev_lock(wdev);
5679 spin_lock_bh(&rdev->bss_lock);
5680 cfg80211_bss_expire(rdev);
5681
Johannes Berg9720bb32011-06-21 09:45:33 +02005682 cb->seq = rdev->bss_generation;
5683
Johannes Berg48ab9052009-07-10 18:42:31 +02005684 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005685 if (++idx <= start)
5686 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005687 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005688 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005689 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005690 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005691 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005692 }
5693 }
5694
Johannes Berg48ab9052009-07-10 18:42:31 +02005695 spin_unlock_bh(&rdev->bss_lock);
5696 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005697
Johannes Berg97990a02013-04-19 01:02:55 +02005698 cb->args[2] = idx;
5699 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005700
Johannes Berg67748892010-10-04 21:14:06 +02005701 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005702}
5703
Eric W. Biederman15e47302012-09-07 20:12:54 +00005704static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005705 int flags, struct net_device *dev,
5706 struct survey_info *survey)
5707{
5708 void *hdr;
5709 struct nlattr *infoattr;
5710
Eric W. Biederman15e47302012-09-07 20:12:54 +00005711 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005712 NL80211_CMD_NEW_SURVEY_RESULTS);
5713 if (!hdr)
5714 return -ENOMEM;
5715
David S. Miller9360ffd2012-03-29 04:41:26 -04005716 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5717 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005718
5719 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5720 if (!infoattr)
5721 goto nla_put_failure;
5722
David S. Miller9360ffd2012-03-29 04:41:26 -04005723 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5724 survey->channel->center_freq))
5725 goto nla_put_failure;
5726
5727 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5728 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5729 goto nla_put_failure;
5730 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5731 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5732 goto nla_put_failure;
5733 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5734 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5735 survey->channel_time))
5736 goto nla_put_failure;
5737 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5738 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5739 survey->channel_time_busy))
5740 goto nla_put_failure;
5741 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5742 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5743 survey->channel_time_ext_busy))
5744 goto nla_put_failure;
5745 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5746 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5747 survey->channel_time_rx))
5748 goto nla_put_failure;
5749 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5750 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5751 survey->channel_time_tx))
5752 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005753
5754 nla_nest_end(msg, infoattr);
5755
5756 return genlmsg_end(msg, hdr);
5757
5758 nla_put_failure:
5759 genlmsg_cancel(msg, hdr);
5760 return -EMSGSIZE;
5761}
5762
5763static int nl80211_dump_survey(struct sk_buff *skb,
5764 struct netlink_callback *cb)
5765{
5766 struct survey_info survey;
5767 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02005768 struct wireless_dev *wdev;
5769 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01005770 int res;
5771
Johannes Berg97990a02013-04-19 01:02:55 +02005772 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005773 if (res)
5774 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005775
Johannes Berg97990a02013-04-19 01:02:55 +02005776 if (!wdev->netdev) {
5777 res = -EINVAL;
5778 goto out_err;
5779 }
5780
Holger Schurig61fa7132009-11-11 12:25:40 +01005781 if (!dev->ops->dump_survey) {
5782 res = -EOPNOTSUPP;
5783 goto out_err;
5784 }
5785
5786 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005787 struct ieee80211_channel *chan;
5788
Johannes Berg97990a02013-04-19 01:02:55 +02005789 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005790 if (res == -ENOENT)
5791 break;
5792 if (res)
5793 goto out_err;
5794
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005795 /* Survey without a channel doesn't make sense */
5796 if (!survey.channel) {
5797 res = -EINVAL;
5798 goto out;
5799 }
5800
5801 chan = ieee80211_get_channel(&dev->wiphy,
5802 survey.channel->center_freq);
5803 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5804 survey_idx++;
5805 continue;
5806 }
5807
Holger Schurig61fa7132009-11-11 12:25:40 +01005808 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005809 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005810 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02005811 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01005812 goto out;
5813 survey_idx++;
5814 }
5815
5816 out:
Johannes Berg97990a02013-04-19 01:02:55 +02005817 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01005818 res = skb->len;
5819 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02005820 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005821 return res;
5822}
5823
Samuel Ortizb23aa672009-07-01 21:26:54 +02005824static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5825{
5826 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5827 NL80211_WPA_VERSION_2));
5828}
5829
Jouni Malinen636a5d32009-03-19 13:39:22 +02005830static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5831{
Johannes Berg4c476992010-10-04 21:36:35 +02005832 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5833 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005834 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005835 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5836 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005837 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005838 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005839 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005840
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005841 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5842 return -EINVAL;
5843
5844 if (!info->attrs[NL80211_ATTR_MAC])
5845 return -EINVAL;
5846
Jouni Malinen17780922009-03-27 20:52:47 +02005847 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5848 return -EINVAL;
5849
Johannes Berg19957bb2009-07-02 17:20:43 +02005850 if (!info->attrs[NL80211_ATTR_SSID])
5851 return -EINVAL;
5852
5853 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5854 return -EINVAL;
5855
Johannes Bergfffd0932009-07-08 14:22:54 +02005856 err = nl80211_parse_key(info, &key);
5857 if (err)
5858 return err;
5859
5860 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005861 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5862 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005863 if (!key.p.key || !key.p.key_len)
5864 return -EINVAL;
5865 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5866 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5867 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5868 key.p.key_len != WLAN_KEY_LEN_WEP104))
5869 return -EINVAL;
5870 if (key.idx > 4)
5871 return -EINVAL;
5872 } else {
5873 key.p.key_len = 0;
5874 key.p.key = NULL;
5875 }
5876
Johannes Bergafea0b72010-08-10 09:46:42 +02005877 if (key.idx >= 0) {
5878 int i;
5879 bool ok = false;
5880 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5881 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5882 ok = true;
5883 break;
5884 }
5885 }
Johannes Berg4c476992010-10-04 21:36:35 +02005886 if (!ok)
5887 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005888 }
5889
Johannes Berg4c476992010-10-04 21:36:35 +02005890 if (!rdev->ops->auth)
5891 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005892
Johannes Berg074ac8d2010-09-16 14:58:22 +02005893 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005894 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5895 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005896
Johannes Berg19957bb2009-07-02 17:20:43 +02005897 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005898 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005899 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005900 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5901 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005902
Johannes Berg19957bb2009-07-02 17:20:43 +02005903 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5904 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5905
5906 if (info->attrs[NL80211_ATTR_IE]) {
5907 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5908 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5909 }
5910
5911 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005912 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005913 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005914
Jouni Malinene39e5b52012-09-30 19:29:39 +03005915 if (auth_type == NL80211_AUTHTYPE_SAE &&
5916 !info->attrs[NL80211_ATTR_SAE_DATA])
5917 return -EINVAL;
5918
5919 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5920 if (auth_type != NL80211_AUTHTYPE_SAE)
5921 return -EINVAL;
5922 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5923 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5924 /* need to include at least Auth Transaction and Status Code */
5925 if (sae_data_len < 4)
5926 return -EINVAL;
5927 }
5928
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005929 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5930
Johannes Berg95de8172012-01-20 13:55:25 +01005931 /*
5932 * Since we no longer track auth state, ignore
5933 * requests to only change local state.
5934 */
5935 if (local_state_change)
5936 return 0;
5937
Johannes Berg4c476992010-10-04 21:36:35 +02005938 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5939 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005940 key.p.key, key.p.key_len, key.idx,
5941 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005942}
5943
Johannes Bergc0692b82010-08-27 14:26:53 +03005944static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5945 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005946 struct cfg80211_crypto_settings *settings,
5947 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005948{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005949 memset(settings, 0, sizeof(*settings));
5950
Samuel Ortizb23aa672009-07-01 21:26:54 +02005951 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5952
Johannes Bergc0692b82010-08-27 14:26:53 +03005953 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5954 u16 proto;
5955 proto = nla_get_u16(
5956 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5957 settings->control_port_ethertype = cpu_to_be16(proto);
5958 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5959 proto != ETH_P_PAE)
5960 return -EINVAL;
5961 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5962 settings->control_port_no_encrypt = true;
5963 } else
5964 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5965
Samuel Ortizb23aa672009-07-01 21:26:54 +02005966 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5967 void *data;
5968 int len, i;
5969
5970 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5971 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5972 settings->n_ciphers_pairwise = len / sizeof(u32);
5973
5974 if (len % sizeof(u32))
5975 return -EINVAL;
5976
Johannes Berg3dc27d22009-07-02 21:36:37 +02005977 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005978 return -EINVAL;
5979
5980 memcpy(settings->ciphers_pairwise, data, len);
5981
5982 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005983 if (!cfg80211_supported_cipher_suite(
5984 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005985 settings->ciphers_pairwise[i]))
5986 return -EINVAL;
5987 }
5988
5989 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5990 settings->cipher_group =
5991 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005992 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5993 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005994 return -EINVAL;
5995 }
5996
5997 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5998 settings->wpa_versions =
5999 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6000 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6001 return -EINVAL;
6002 }
6003
6004 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6005 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006006 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006007
6008 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6009 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6010 settings->n_akm_suites = len / sizeof(u32);
6011
6012 if (len % sizeof(u32))
6013 return -EINVAL;
6014
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006015 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6016 return -EINVAL;
6017
Samuel Ortizb23aa672009-07-01 21:26:54 +02006018 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006019 }
6020
6021 return 0;
6022}
6023
Jouni Malinen636a5d32009-03-19 13:39:22 +02006024static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6025{
Johannes Berg4c476992010-10-04 21:36:35 +02006026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6027 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006028 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006029 struct cfg80211_assoc_request req = {};
6030 const u8 *bssid, *ssid;
6031 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006032
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006033 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6034 return -EINVAL;
6035
6036 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006037 !info->attrs[NL80211_ATTR_SSID] ||
6038 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006039 return -EINVAL;
6040
Johannes Berg4c476992010-10-04 21:36:35 +02006041 if (!rdev->ops->assoc)
6042 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006043
Johannes Berg074ac8d2010-09-16 14:58:22 +02006044 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006045 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6046 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006047
Johannes Berg19957bb2009-07-02 17:20:43 +02006048 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006049
Johannes Berg19957bb2009-07-02 17:20:43 +02006050 chan = ieee80211_get_channel(&rdev->wiphy,
6051 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006052 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6053 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006054
Johannes Berg19957bb2009-07-02 17:20:43 +02006055 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6056 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006057
6058 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006059 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6060 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006061 }
6062
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006063 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006064 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006065 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006066 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006067 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006068 else if (mfp != NL80211_MFP_NO)
6069 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006070 }
6071
Johannes Berg3e5d7642009-07-07 14:37:26 +02006072 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006073 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006074
Ben Greear7e7c8922011-11-18 11:31:59 -08006075 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006076 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006077
6078 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006079 memcpy(&req.ht_capa_mask,
6080 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6081 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006082
6083 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006084 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006085 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006086 memcpy(&req.ht_capa,
6087 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6088 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006089 }
6090
Johannes Bergee2aca32013-02-21 17:36:01 +01006091 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006092 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006093
6094 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006095 memcpy(&req.vht_capa_mask,
6096 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6097 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006098
6099 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006100 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006101 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006102 memcpy(&req.vht_capa,
6103 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6104 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006105 }
6106
Johannes Bergf62fab72013-02-21 20:09:09 +01006107 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006108 if (!err)
Johannes Bergf62fab72013-02-21 20:09:09 +01006109 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6110 ssid, ssid_len, &req);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006111
Jouni Malinen636a5d32009-03-19 13:39:22 +02006112 return err;
6113}
6114
6115static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6116{
Johannes Berg4c476992010-10-04 21:36:35 +02006117 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6118 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006119 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006120 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006121 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006122 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006123
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006124 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6125 return -EINVAL;
6126
6127 if (!info->attrs[NL80211_ATTR_MAC])
6128 return -EINVAL;
6129
6130 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6131 return -EINVAL;
6132
Johannes Berg4c476992010-10-04 21:36:35 +02006133 if (!rdev->ops->deauth)
6134 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006135
Johannes Berg074ac8d2010-09-16 14:58:22 +02006136 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006137 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6138 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006139
Johannes Berg19957bb2009-07-02 17:20:43 +02006140 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006141
Johannes Berg19957bb2009-07-02 17:20:43 +02006142 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6143 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006144 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006145 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006146 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006147
6148 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006149 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6150 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006151 }
6152
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006153 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6154
Johannes Berg4c476992010-10-04 21:36:35 +02006155 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6156 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006157}
6158
6159static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6160{
Johannes Berg4c476992010-10-04 21:36:35 +02006161 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6162 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006163 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006164 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006165 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006166 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006167
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006168 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6169 return -EINVAL;
6170
6171 if (!info->attrs[NL80211_ATTR_MAC])
6172 return -EINVAL;
6173
6174 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6175 return -EINVAL;
6176
Johannes Berg4c476992010-10-04 21:36:35 +02006177 if (!rdev->ops->disassoc)
6178 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006179
Johannes Berg074ac8d2010-09-16 14:58:22 +02006180 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006181 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6182 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006183
Johannes Berg19957bb2009-07-02 17:20:43 +02006184 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006185
Johannes Berg19957bb2009-07-02 17:20:43 +02006186 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6187 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006188 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006189 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006190 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006191
6192 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006193 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6194 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006195 }
6196
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006197 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6198
Johannes Berg4c476992010-10-04 21:36:35 +02006199 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6200 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006201}
6202
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006203static bool
6204nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6205 int mcast_rate[IEEE80211_NUM_BANDS],
6206 int rateval)
6207{
6208 struct wiphy *wiphy = &rdev->wiphy;
6209 bool found = false;
6210 int band, i;
6211
6212 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6213 struct ieee80211_supported_band *sband;
6214
6215 sband = wiphy->bands[band];
6216 if (!sband)
6217 continue;
6218
6219 for (i = 0; i < sband->n_bitrates; i++) {
6220 if (sband->bitrates[i].bitrate == rateval) {
6221 mcast_rate[band] = i + 1;
6222 found = true;
6223 break;
6224 }
6225 }
6226 }
6227
6228 return found;
6229}
6230
Johannes Berg04a773a2009-04-19 21:24:32 +02006231static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6232{
Johannes Berg4c476992010-10-04 21:36:35 +02006233 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6234 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006235 struct cfg80211_ibss_params ibss;
6236 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006237 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006238 int err;
6239
Johannes Berg8e30bc52009-04-22 17:45:38 +02006240 memset(&ibss, 0, sizeof(ibss));
6241
Johannes Berg04a773a2009-04-19 21:24:32 +02006242 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6243 return -EINVAL;
6244
Johannes Berg683b6d32012-11-08 21:25:48 +01006245 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006246 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6247 return -EINVAL;
6248
Johannes Berg8e30bc52009-04-22 17:45:38 +02006249 ibss.beacon_interval = 100;
6250
6251 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6252 ibss.beacon_interval =
6253 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6254 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6255 return -EINVAL;
6256 }
6257
Johannes Berg4c476992010-10-04 21:36:35 +02006258 if (!rdev->ops->join_ibss)
6259 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006260
Johannes Berg4c476992010-10-04 21:36:35 +02006261 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6262 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006263
Johannes Berg79c97e92009-07-07 03:56:12 +02006264 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006265
Johannes Berg39193492011-09-16 13:45:25 +02006266 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006267 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006268
6269 if (!is_valid_ether_addr(ibss.bssid))
6270 return -EINVAL;
6271 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006272 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6273 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6274
6275 if (info->attrs[NL80211_ATTR_IE]) {
6276 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6277 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6278 }
6279
Johannes Berg683b6d32012-11-08 21:25:48 +01006280 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6281 if (err)
6282 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006283
Johannes Berg683b6d32012-11-08 21:25:48 +01006284 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006285 return -EINVAL;
6286
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006287 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6288 return -EINVAL;
6289 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6290 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01006291 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006292
Johannes Berg04a773a2009-04-19 21:24:32 +02006293 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006294 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006295
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006296 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6297 u8 *rates =
6298 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6299 int n_rates =
6300 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6301 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006302 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006303
Johannes Berg34850ab2011-07-18 18:08:35 +02006304 err = ieee80211_get_ratemask(sband, rates, n_rates,
6305 &ibss.basic_rates);
6306 if (err)
6307 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006308 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006309
6310 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6311 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6312 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6313 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006314
Johannes Berg4c476992010-10-04 21:36:35 +02006315 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306316 bool no_ht = false;
6317
Johannes Berg4c476992010-10-04 21:36:35 +02006318 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306319 info->attrs[NL80211_ATTR_KEYS],
6320 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006321 if (IS_ERR(connkeys))
6322 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306323
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006324 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6325 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306326 kfree(connkeys);
6327 return -EINVAL;
6328 }
Johannes Berg4c476992010-10-04 21:36:35 +02006329 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006330
Antonio Quartulli267335d2012-01-31 20:25:47 +01006331 ibss.control_port =
6332 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6333
Johannes Berg4c476992010-10-04 21:36:35 +02006334 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006335 if (err)
6336 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006337 return err;
6338}
6339
6340static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6341{
Johannes Berg4c476992010-10-04 21:36:35 +02006342 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6343 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006344
Johannes Berg4c476992010-10-04 21:36:35 +02006345 if (!rdev->ops->leave_ibss)
6346 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006347
Johannes Berg4c476992010-10-04 21:36:35 +02006348 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6349 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006350
Johannes Berg4c476992010-10-04 21:36:35 +02006351 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006352}
6353
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006354static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6355{
6356 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6357 struct net_device *dev = info->user_ptr[1];
6358 int mcast_rate[IEEE80211_NUM_BANDS];
6359 u32 nla_rate;
6360 int err;
6361
6362 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6363 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6364 return -EOPNOTSUPP;
6365
6366 if (!rdev->ops->set_mcast_rate)
6367 return -EOPNOTSUPP;
6368
6369 memset(mcast_rate, 0, sizeof(mcast_rate));
6370
6371 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6372 return -EINVAL;
6373
6374 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6375 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6376 return -EINVAL;
6377
6378 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6379
6380 return err;
6381}
6382
6383
Johannes Bergaff89a92009-07-01 21:26:51 +02006384#ifdef CONFIG_NL80211_TESTMODE
6385static struct genl_multicast_group nl80211_testmode_mcgrp = {
6386 .name = "testmode",
6387};
6388
6389static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6390{
Johannes Berg4c476992010-10-04 21:36:35 +02006391 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006392 int err;
6393
6394 if (!info->attrs[NL80211_ATTR_TESTDATA])
6395 return -EINVAL;
6396
Johannes Bergaff89a92009-07-01 21:26:51 +02006397 err = -EOPNOTSUPP;
6398 if (rdev->ops->testmode_cmd) {
6399 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006400 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006401 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6402 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6403 rdev->testmode_info = NULL;
6404 }
6405
Johannes Bergaff89a92009-07-01 21:26:51 +02006406 return err;
6407}
6408
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006409static int nl80211_testmode_dump(struct sk_buff *skb,
6410 struct netlink_callback *cb)
6411{
Johannes Berg00918d32011-12-13 17:22:05 +01006412 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006413 int err;
6414 long phy_idx;
6415 void *data = NULL;
6416 int data_len = 0;
6417
6418 if (cb->args[0]) {
6419 /*
6420 * 0 is a valid index, but not valid for args[0],
6421 * so we need to offset by 1.
6422 */
6423 phy_idx = cb->args[0] - 1;
6424 } else {
6425 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6426 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6427 nl80211_policy);
6428 if (err)
6429 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006430
Johannes Berg2bd7e352012-06-15 14:23:16 +02006431 mutex_lock(&cfg80211_mutex);
6432 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6433 nl80211_fam.attrbuf);
6434 if (IS_ERR(rdev)) {
6435 mutex_unlock(&cfg80211_mutex);
6436 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006437 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006438 phy_idx = rdev->wiphy_idx;
6439 rdev = NULL;
6440 mutex_unlock(&cfg80211_mutex);
6441
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006442 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6443 cb->args[1] =
6444 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6445 }
6446
6447 if (cb->args[1]) {
6448 data = nla_data((void *)cb->args[1]);
6449 data_len = nla_len((void *)cb->args[1]);
6450 }
6451
6452 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006453 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6454 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006455 mutex_unlock(&cfg80211_mutex);
6456 return -ENOENT;
6457 }
Johannes Berg00918d32011-12-13 17:22:05 +01006458 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006459 mutex_unlock(&cfg80211_mutex);
6460
Johannes Berg00918d32011-12-13 17:22:05 +01006461 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006462 err = -EOPNOTSUPP;
6463 goto out_err;
6464 }
6465
6466 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006467 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006468 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6469 NL80211_CMD_TESTMODE);
6470 struct nlattr *tmdata;
6471
David S. Miller9360ffd2012-03-29 04:41:26 -04006472 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006473 genlmsg_cancel(skb, hdr);
6474 break;
6475 }
6476
6477 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6478 if (!tmdata) {
6479 genlmsg_cancel(skb, hdr);
6480 break;
6481 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006482 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006483 nla_nest_end(skb, tmdata);
6484
6485 if (err == -ENOBUFS || err == -ENOENT) {
6486 genlmsg_cancel(skb, hdr);
6487 break;
6488 } else if (err) {
6489 genlmsg_cancel(skb, hdr);
6490 goto out_err;
6491 }
6492
6493 genlmsg_end(skb, hdr);
6494 }
6495
6496 err = skb->len;
6497 /* see above */
6498 cb->args[0] = phy_idx + 1;
6499 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006500 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006501 return err;
6502}
6503
Johannes Bergaff89a92009-07-01 21:26:51 +02006504static struct sk_buff *
6505__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006506 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006507{
6508 struct sk_buff *skb;
6509 void *hdr;
6510 struct nlattr *data;
6511
6512 skb = nlmsg_new(approxlen + 100, gfp);
6513 if (!skb)
6514 return NULL;
6515
Eric W. Biederman15e47302012-09-07 20:12:54 +00006516 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006517 if (!hdr) {
6518 kfree_skb(skb);
6519 return NULL;
6520 }
6521
David S. Miller9360ffd2012-03-29 04:41:26 -04006522 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6523 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006524 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6525
6526 ((void **)skb->cb)[0] = rdev;
6527 ((void **)skb->cb)[1] = hdr;
6528 ((void **)skb->cb)[2] = data;
6529
6530 return skb;
6531
6532 nla_put_failure:
6533 kfree_skb(skb);
6534 return NULL;
6535}
6536
6537struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6538 int approxlen)
6539{
6540 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6541
6542 if (WARN_ON(!rdev->testmode_info))
6543 return NULL;
6544
6545 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006546 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006547 rdev->testmode_info->snd_seq,
6548 GFP_KERNEL);
6549}
6550EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6551
6552int cfg80211_testmode_reply(struct sk_buff *skb)
6553{
6554 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6555 void *hdr = ((void **)skb->cb)[1];
6556 struct nlattr *data = ((void **)skb->cb)[2];
6557
6558 if (WARN_ON(!rdev->testmode_info)) {
6559 kfree_skb(skb);
6560 return -EINVAL;
6561 }
6562
6563 nla_nest_end(skb, data);
6564 genlmsg_end(skb, hdr);
6565 return genlmsg_reply(skb, rdev->testmode_info);
6566}
6567EXPORT_SYMBOL(cfg80211_testmode_reply);
6568
6569struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6570 int approxlen, gfp_t gfp)
6571{
6572 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6573
6574 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6575}
6576EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6577
6578void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6579{
6580 void *hdr = ((void **)skb->cb)[1];
6581 struct nlattr *data = ((void **)skb->cb)[2];
6582
6583 nla_nest_end(skb, data);
6584 genlmsg_end(skb, hdr);
6585 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6586}
6587EXPORT_SYMBOL(cfg80211_testmode_event);
6588#endif
6589
Samuel Ortizb23aa672009-07-01 21:26:54 +02006590static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6591{
Johannes Berg4c476992010-10-04 21:36:35 +02006592 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6593 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006594 struct cfg80211_connect_params connect;
6595 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006596 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006597 int err;
6598
6599 memset(&connect, 0, sizeof(connect));
6600
6601 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6602 return -EINVAL;
6603
6604 if (!info->attrs[NL80211_ATTR_SSID] ||
6605 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6606 return -EINVAL;
6607
6608 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6609 connect.auth_type =
6610 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006611 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6612 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006613 return -EINVAL;
6614 } else
6615 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6616
6617 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6618
Johannes Bergc0692b82010-08-27 14:26:53 +03006619 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006620 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006621 if (err)
6622 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006623
Johannes Berg074ac8d2010-09-16 14:58:22 +02006624 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006625 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6626 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006627
Johannes Berg79c97e92009-07-07 03:56:12 +02006628 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006629
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306630 connect.bg_scan_period = -1;
6631 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6632 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6633 connect.bg_scan_period =
6634 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6635 }
6636
Samuel Ortizb23aa672009-07-01 21:26:54 +02006637 if (info->attrs[NL80211_ATTR_MAC])
6638 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6639 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6640 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6641
6642 if (info->attrs[NL80211_ATTR_IE]) {
6643 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6644 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6645 }
6646
Jouni Malinencee00a92013-01-15 17:15:57 +02006647 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6648 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6649 if (connect.mfp != NL80211_MFP_REQUIRED &&
6650 connect.mfp != NL80211_MFP_NO)
6651 return -EINVAL;
6652 } else {
6653 connect.mfp = NL80211_MFP_NO;
6654 }
6655
Samuel Ortizb23aa672009-07-01 21:26:54 +02006656 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6657 connect.channel =
6658 ieee80211_get_channel(wiphy,
6659 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6660 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006661 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6662 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006663 }
6664
Johannes Bergfffd0932009-07-08 14:22:54 +02006665 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6666 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306667 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006668 if (IS_ERR(connkeys))
6669 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006670 }
6671
Ben Greear7e7c8922011-11-18 11:31:59 -08006672 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6673 connect.flags |= ASSOC_REQ_DISABLE_HT;
6674
6675 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6676 memcpy(&connect.ht_capa_mask,
6677 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6678 sizeof(connect.ht_capa_mask));
6679
6680 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006681 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6682 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006683 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006684 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006685 memcpy(&connect.ht_capa,
6686 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6687 sizeof(connect.ht_capa));
6688 }
6689
Johannes Bergee2aca32013-02-21 17:36:01 +01006690 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6691 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6692
6693 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6694 memcpy(&connect.vht_capa_mask,
6695 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6696 sizeof(connect.vht_capa_mask));
6697
6698 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6699 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
6700 kfree(connkeys);
6701 return -EINVAL;
6702 }
6703 memcpy(&connect.vht_capa,
6704 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6705 sizeof(connect.vht_capa));
6706 }
6707
Johannes Bergfffd0932009-07-08 14:22:54 +02006708 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006709 if (err)
6710 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006711 return err;
6712}
6713
6714static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6715{
Johannes Berg4c476992010-10-04 21:36:35 +02006716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6717 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006718 u16 reason;
6719
6720 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6721 reason = WLAN_REASON_DEAUTH_LEAVING;
6722 else
6723 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6724
6725 if (reason == 0)
6726 return -EINVAL;
6727
Johannes Berg074ac8d2010-09-16 14:58:22 +02006728 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006729 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6730 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006731
Johannes Berg4c476992010-10-04 21:36:35 +02006732 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006733}
6734
Johannes Berg463d0182009-07-14 00:33:35 +02006735static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6736{
Johannes Berg4c476992010-10-04 21:36:35 +02006737 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006738 struct net *net;
6739 int err;
6740 u32 pid;
6741
6742 if (!info->attrs[NL80211_ATTR_PID])
6743 return -EINVAL;
6744
6745 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6746
Johannes Berg463d0182009-07-14 00:33:35 +02006747 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006748 if (IS_ERR(net))
6749 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006750
6751 err = 0;
6752
6753 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006754 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6755 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006756
Johannes Berg463d0182009-07-14 00:33:35 +02006757 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006758 return err;
6759}
6760
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006761static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6762{
Johannes Berg4c476992010-10-04 21:36:35 +02006763 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006764 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6765 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006766 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006767 struct cfg80211_pmksa pmksa;
6768
6769 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6770
6771 if (!info->attrs[NL80211_ATTR_MAC])
6772 return -EINVAL;
6773
6774 if (!info->attrs[NL80211_ATTR_PMKID])
6775 return -EINVAL;
6776
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006777 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6778 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6779
Johannes Berg074ac8d2010-09-16 14:58:22 +02006780 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006781 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6782 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006783
6784 switch (info->genlhdr->cmd) {
6785 case NL80211_CMD_SET_PMKSA:
6786 rdev_ops = rdev->ops->set_pmksa;
6787 break;
6788 case NL80211_CMD_DEL_PMKSA:
6789 rdev_ops = rdev->ops->del_pmksa;
6790 break;
6791 default:
6792 WARN_ON(1);
6793 break;
6794 }
6795
Johannes Berg4c476992010-10-04 21:36:35 +02006796 if (!rdev_ops)
6797 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006798
Johannes Berg4c476992010-10-04 21:36:35 +02006799 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006800}
6801
6802static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6803{
Johannes Berg4c476992010-10-04 21:36:35 +02006804 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6805 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006806
Johannes Berg074ac8d2010-09-16 14:58:22 +02006807 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006808 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6809 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006810
Johannes Berg4c476992010-10-04 21:36:35 +02006811 if (!rdev->ops->flush_pmksa)
6812 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006813
Hila Gonene35e4d22012-06-27 17:19:42 +03006814 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006815}
6816
Arik Nemtsov109086c2011-09-28 14:12:50 +03006817static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6818{
6819 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6820 struct net_device *dev = info->user_ptr[1];
6821 u8 action_code, dialog_token;
6822 u16 status_code;
6823 u8 *peer;
6824
6825 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6826 !rdev->ops->tdls_mgmt)
6827 return -EOPNOTSUPP;
6828
6829 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6830 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6831 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6832 !info->attrs[NL80211_ATTR_IE] ||
6833 !info->attrs[NL80211_ATTR_MAC])
6834 return -EINVAL;
6835
6836 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6837 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6838 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6839 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6840
Hila Gonene35e4d22012-06-27 17:19:42 +03006841 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6842 dialog_token, status_code,
6843 nla_data(info->attrs[NL80211_ATTR_IE]),
6844 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006845}
6846
6847static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6848{
6849 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6850 struct net_device *dev = info->user_ptr[1];
6851 enum nl80211_tdls_operation operation;
6852 u8 *peer;
6853
6854 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6855 !rdev->ops->tdls_oper)
6856 return -EOPNOTSUPP;
6857
6858 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6859 !info->attrs[NL80211_ATTR_MAC])
6860 return -EINVAL;
6861
6862 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6863 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6864
Hila Gonene35e4d22012-06-27 17:19:42 +03006865 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006866}
6867
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006868static int nl80211_remain_on_channel(struct sk_buff *skb,
6869 struct genl_info *info)
6870{
Johannes Berg4c476992010-10-04 21:36:35 +02006871 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006872 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006873 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006874 struct sk_buff *msg;
6875 void *hdr;
6876 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006877 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006878 int err;
6879
6880 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6881 !info->attrs[NL80211_ATTR_DURATION])
6882 return -EINVAL;
6883
6884 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6885
Johannes Berg7c4ef712011-11-18 15:33:48 +01006886 if (!rdev->ops->remain_on_channel ||
6887 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006888 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006889
Johannes Bergebf348f2012-06-01 12:50:54 +02006890 /*
6891 * We should be on that channel for at least a minimum amount of
6892 * time (10ms) but no longer than the driver supports.
6893 */
6894 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6895 duration > rdev->wiphy.max_remain_on_channel_duration)
6896 return -EINVAL;
6897
Johannes Berg683b6d32012-11-08 21:25:48 +01006898 err = nl80211_parse_chandef(rdev, info, &chandef);
6899 if (err)
6900 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006901
6902 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006903 if (!msg)
6904 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006905
Eric W. Biederman15e47302012-09-07 20:12:54 +00006906 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006907 NL80211_CMD_REMAIN_ON_CHANNEL);
6908
6909 if (IS_ERR(hdr)) {
6910 err = PTR_ERR(hdr);
6911 goto free_msg;
6912 }
6913
Johannes Berg683b6d32012-11-08 21:25:48 +01006914 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6915 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006916
6917 if (err)
6918 goto free_msg;
6919
David S. Miller9360ffd2012-03-29 04:41:26 -04006920 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6921 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006922
6923 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006924
6925 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006926
6927 nla_put_failure:
6928 err = -ENOBUFS;
6929 free_msg:
6930 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006931 return err;
6932}
6933
6934static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6935 struct genl_info *info)
6936{
Johannes Berg4c476992010-10-04 21:36:35 +02006937 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006938 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006939 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006940
6941 if (!info->attrs[NL80211_ATTR_COOKIE])
6942 return -EINVAL;
6943
Johannes Berg4c476992010-10-04 21:36:35 +02006944 if (!rdev->ops->cancel_remain_on_channel)
6945 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006946
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006947 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6948
Hila Gonene35e4d22012-06-27 17:19:42 +03006949 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006950}
6951
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006952static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6953 u8 *rates, u8 rates_len)
6954{
6955 u8 i;
6956 u32 mask = 0;
6957
6958 for (i = 0; i < rates_len; i++) {
6959 int rate = (rates[i] & 0x7f) * 5;
6960 int ridx;
6961 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6962 struct ieee80211_rate *srate =
6963 &sband->bitrates[ridx];
6964 if (rate == srate->bitrate) {
6965 mask |= 1 << ridx;
6966 break;
6967 }
6968 }
6969 if (ridx == sband->n_bitrates)
6970 return 0; /* rate not found */
6971 }
6972
6973 return mask;
6974}
6975
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006976static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6977 u8 *rates, u8 rates_len,
6978 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6979{
6980 u8 i;
6981
6982 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6983
6984 for (i = 0; i < rates_len; i++) {
6985 int ridx, rbit;
6986
6987 ridx = rates[i] / 8;
6988 rbit = BIT(rates[i] % 8);
6989
6990 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006991 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006992 return false;
6993
6994 /* check availability */
6995 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6996 mcs[ridx] |= rbit;
6997 else
6998 return false;
6999 }
7000
7001 return true;
7002}
7003
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007004static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007005 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7006 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007007 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
7008 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007009};
7010
7011static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7012 struct genl_info *info)
7013{
7014 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007015 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007016 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007017 int rem, i;
7018 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007019 struct nlattr *tx_rates;
7020 struct ieee80211_supported_band *sband;
7021
7022 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7023 return -EINVAL;
7024
Johannes Berg4c476992010-10-04 21:36:35 +02007025 if (!rdev->ops->set_bitrate_mask)
7026 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007027
7028 memset(&mask, 0, sizeof(mask));
7029 /* Default to all rates enabled */
7030 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7031 sband = rdev->wiphy.bands[i];
7032 mask.control[i].legacy =
7033 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007034 if (sband)
7035 memcpy(mask.control[i].mcs,
7036 sband->ht_cap.mcs.rx_mask,
7037 sizeof(mask.control[i].mcs));
7038 else
7039 memset(mask.control[i].mcs, 0,
7040 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007041 }
7042
7043 /*
7044 * The nested attribute uses enum nl80211_band as the index. This maps
7045 * directly to the enum ieee80211_band values used in cfg80211.
7046 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007047 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007048 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7049 {
7050 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007051 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7052 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007053 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007054 if (sband == NULL)
7055 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007056 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7057 nla_len(tx_rates), nl80211_txattr_policy);
7058 if (tb[NL80211_TXRATE_LEGACY]) {
7059 mask.control[band].legacy = rateset_to_mask(
7060 sband,
7061 nla_data(tb[NL80211_TXRATE_LEGACY]),
7062 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307063 if ((mask.control[band].legacy == 0) &&
7064 nla_len(tb[NL80211_TXRATE_LEGACY]))
7065 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007066 }
7067 if (tb[NL80211_TXRATE_MCS]) {
7068 if (!ht_rateset_to_mask(
7069 sband,
7070 nla_data(tb[NL80211_TXRATE_MCS]),
7071 nla_len(tb[NL80211_TXRATE_MCS]),
7072 mask.control[band].mcs))
7073 return -EINVAL;
7074 }
7075
7076 if (mask.control[band].legacy == 0) {
7077 /* don't allow empty legacy rates if HT
7078 * is not even supported. */
7079 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7080 return -EINVAL;
7081
7082 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7083 if (mask.control[band].mcs[i])
7084 break;
7085
7086 /* legacy and mcs rates may not be both empty */
7087 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02007088 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007089 }
7090 }
7091
Hila Gonene35e4d22012-06-27 17:19:42 +03007092 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007093}
7094
Johannes Berg2e161f72010-08-12 15:38:38 +02007095static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007096{
Johannes Berg4c476992010-10-04 21:36:35 +02007097 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007098 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007099 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007100
7101 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7102 return -EINVAL;
7103
Johannes Berg2e161f72010-08-12 15:38:38 +02007104 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7105 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007106
Johannes Berg71bbc992012-06-15 15:30:18 +02007107 switch (wdev->iftype) {
7108 case NL80211_IFTYPE_STATION:
7109 case NL80211_IFTYPE_ADHOC:
7110 case NL80211_IFTYPE_P2P_CLIENT:
7111 case NL80211_IFTYPE_AP:
7112 case NL80211_IFTYPE_AP_VLAN:
7113 case NL80211_IFTYPE_MESH_POINT:
7114 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007115 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007116 break;
7117 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007118 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007119 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007120
7121 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007122 if (!rdev->ops->mgmt_tx)
7123 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007124
Eric W. Biederman15e47302012-09-07 20:12:54 +00007125 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007126 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7127 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007128}
7129
Johannes Berg2e161f72010-08-12 15:38:38 +02007130static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007131{
Johannes Berg4c476992010-10-04 21:36:35 +02007132 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007133 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007134 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007135 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007136 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007137 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007138 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007139 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01007140 bool offchan, no_cck, dont_wait_for_ack;
7141
7142 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02007143
Johannes Berg683b6d32012-11-08 21:25:48 +01007144 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007145 return -EINVAL;
7146
Johannes Berg4c476992010-10-04 21:36:35 +02007147 if (!rdev->ops->mgmt_tx)
7148 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007149
Johannes Berg71bbc992012-06-15 15:30:18 +02007150 switch (wdev->iftype) {
7151 case NL80211_IFTYPE_STATION:
7152 case NL80211_IFTYPE_ADHOC:
7153 case NL80211_IFTYPE_P2P_CLIENT:
7154 case NL80211_IFTYPE_AP:
7155 case NL80211_IFTYPE_AP_VLAN:
7156 case NL80211_IFTYPE_MESH_POINT:
7157 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007158 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007159 break;
7160 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007161 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007162 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007163
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007164 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007165 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007166 return -EINVAL;
7167 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007168
7169 /*
7170 * We should wait on the channel for at least a minimum amount
7171 * of time (10ms) but no longer than the driver supports.
7172 */
7173 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7174 wait > rdev->wiphy.max_remain_on_channel_duration)
7175 return -EINVAL;
7176
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007177 }
7178
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007179 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7180
Johannes Berg7c4ef712011-11-18 15:33:48 +01007181 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7182 return -EINVAL;
7183
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307184 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7185
Johannes Berg683b6d32012-11-08 21:25:48 +01007186 err = nl80211_parse_chandef(rdev, info, &chandef);
7187 if (err)
7188 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02007189
Johannes Berge247bd902011-11-04 11:18:21 +01007190 if (!dont_wait_for_ack) {
7191 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7192 if (!msg)
7193 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007194
Eric W. Biederman15e47302012-09-07 20:12:54 +00007195 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007196 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007197
Johannes Berge247bd902011-11-04 11:18:21 +01007198 if (IS_ERR(hdr)) {
7199 err = PTR_ERR(hdr);
7200 goto free_msg;
7201 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007202 }
Johannes Berge247bd902011-11-04 11:18:21 +01007203
Johannes Berg683b6d32012-11-08 21:25:48 +01007204 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02007205 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7206 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01007207 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007208 if (err)
7209 goto free_msg;
7210
Johannes Berge247bd902011-11-04 11:18:21 +01007211 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007212 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7213 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007214
Johannes Berge247bd902011-11-04 11:18:21 +01007215 genlmsg_end(msg, hdr);
7216 return genlmsg_reply(msg, info);
7217 }
7218
7219 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007220
7221 nla_put_failure:
7222 err = -ENOBUFS;
7223 free_msg:
7224 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007225 return err;
7226}
7227
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007228static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7229{
7230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007231 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007232 u64 cookie;
7233
7234 if (!info->attrs[NL80211_ATTR_COOKIE])
7235 return -EINVAL;
7236
7237 if (!rdev->ops->mgmt_tx_cancel_wait)
7238 return -EOPNOTSUPP;
7239
Johannes Berg71bbc992012-06-15 15:30:18 +02007240 switch (wdev->iftype) {
7241 case NL80211_IFTYPE_STATION:
7242 case NL80211_IFTYPE_ADHOC:
7243 case NL80211_IFTYPE_P2P_CLIENT:
7244 case NL80211_IFTYPE_AP:
7245 case NL80211_IFTYPE_AP_VLAN:
7246 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007247 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007248 break;
7249 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007250 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007251 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007252
7253 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7254
Hila Gonene35e4d22012-06-27 17:19:42 +03007255 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007256}
7257
Kalle Valoffb9eb32010-02-17 17:58:10 +02007258static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7259{
Johannes Berg4c476992010-10-04 21:36:35 +02007260 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007261 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007262 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007263 u8 ps_state;
7264 bool state;
7265 int err;
7266
Johannes Berg4c476992010-10-04 21:36:35 +02007267 if (!info->attrs[NL80211_ATTR_PS_STATE])
7268 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007269
7270 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7271
Johannes Berg4c476992010-10-04 21:36:35 +02007272 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7273 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007274
7275 wdev = dev->ieee80211_ptr;
7276
Johannes Berg4c476992010-10-04 21:36:35 +02007277 if (!rdev->ops->set_power_mgmt)
7278 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007279
7280 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7281
7282 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007283 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007284
Hila Gonene35e4d22012-06-27 17:19:42 +03007285 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007286 if (!err)
7287 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007288 return err;
7289}
7290
7291static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7292{
Johannes Berg4c476992010-10-04 21:36:35 +02007293 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007294 enum nl80211_ps_state ps_state;
7295 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007296 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007297 struct sk_buff *msg;
7298 void *hdr;
7299 int err;
7300
Kalle Valoffb9eb32010-02-17 17:58:10 +02007301 wdev = dev->ieee80211_ptr;
7302
Johannes Berg4c476992010-10-04 21:36:35 +02007303 if (!rdev->ops->set_power_mgmt)
7304 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007305
7306 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007307 if (!msg)
7308 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007309
Eric W. Biederman15e47302012-09-07 20:12:54 +00007310 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007311 NL80211_CMD_GET_POWER_SAVE);
7312 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007313 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007314 goto free_msg;
7315 }
7316
7317 if (wdev->ps)
7318 ps_state = NL80211_PS_ENABLED;
7319 else
7320 ps_state = NL80211_PS_DISABLED;
7321
David S. Miller9360ffd2012-03-29 04:41:26 -04007322 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7323 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007324
7325 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007326 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007327
Johannes Berg4c476992010-10-04 21:36:35 +02007328 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007329 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007330 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007331 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007332 return err;
7333}
7334
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007335static struct nla_policy
7336nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7337 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7338 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7339 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007340 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7341 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7342 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007343};
7344
Thomas Pedersen84f10702012-07-12 16:17:33 -07007345static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007346 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007347{
7348 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7349 struct wireless_dev *wdev;
7350 struct net_device *dev = info->user_ptr[1];
7351
Johannes Bergd9d8b012012-11-26 12:51:52 +01007352 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007353 return -EINVAL;
7354
7355 wdev = dev->ieee80211_ptr;
7356
7357 if (!rdev->ops->set_cqm_txe_config)
7358 return -EOPNOTSUPP;
7359
7360 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7361 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7362 return -EOPNOTSUPP;
7363
Hila Gonene35e4d22012-06-27 17:19:42 +03007364 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007365}
7366
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007367static int nl80211_set_cqm_rssi(struct genl_info *info,
7368 s32 threshold, u32 hysteresis)
7369{
Johannes Berg4c476992010-10-04 21:36:35 +02007370 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007371 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007372 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007373
7374 if (threshold > 0)
7375 return -EINVAL;
7376
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007377 wdev = dev->ieee80211_ptr;
7378
Johannes Berg4c476992010-10-04 21:36:35 +02007379 if (!rdev->ops->set_cqm_rssi_config)
7380 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007381
Johannes Berg074ac8d2010-09-16 14:58:22 +02007382 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007383 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7384 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007385
Hila Gonene35e4d22012-06-27 17:19:42 +03007386 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007387}
7388
7389static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7390{
7391 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7392 struct nlattr *cqm;
7393 int err;
7394
7395 cqm = info->attrs[NL80211_ATTR_CQM];
7396 if (!cqm) {
7397 err = -EINVAL;
7398 goto out;
7399 }
7400
7401 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7402 nl80211_attr_cqm_policy);
7403 if (err)
7404 goto out;
7405
7406 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7407 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7408 s32 threshold;
7409 u32 hysteresis;
7410 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7411 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7412 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007413 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7414 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7415 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7416 u32 rate, pkts, intvl;
7417 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7418 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7419 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7420 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007421 } else
7422 err = -EINVAL;
7423
7424out:
7425 return err;
7426}
7427
Johannes Berg29cbe682010-12-03 09:20:44 +01007428static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7429{
7430 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7431 struct net_device *dev = info->user_ptr[1];
7432 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007433 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007434 int err;
7435
7436 /* start with default */
7437 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007438 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007439
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007440 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007441 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007442 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007443 if (err)
7444 return err;
7445 }
7446
7447 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7448 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7449 return -EINVAL;
7450
Javier Cardonac80d5452010-12-16 17:37:49 -08007451 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7452 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7453
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007454 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7455 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7456 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7457 return -EINVAL;
7458
Marco Porsch9bdbf042013-01-07 16:04:51 +01007459 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7460 setup.beacon_interval =
7461 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7462 if (setup.beacon_interval < 10 ||
7463 setup.beacon_interval > 10000)
7464 return -EINVAL;
7465 }
7466
7467 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7468 setup.dtim_period =
7469 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7470 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7471 return -EINVAL;
7472 }
7473
Javier Cardonac80d5452010-12-16 17:37:49 -08007474 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7475 /* parse additional setup parameters if given */
7476 err = nl80211_parse_mesh_setup(info, &setup);
7477 if (err)
7478 return err;
7479 }
7480
Thomas Pedersend37bb182013-03-04 13:06:13 -08007481 if (setup.user_mpm)
7482 cfg.auto_open_plinks = false;
7483
Johannes Bergcc1d2802012-05-16 23:50:20 +02007484 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007485 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7486 if (err)
7487 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007488 } else {
7489 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007490 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007491 }
7492
Javier Cardonac80d5452010-12-16 17:37:49 -08007493 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007494}
7495
7496static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7497{
7498 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7499 struct net_device *dev = info->user_ptr[1];
7500
7501 return cfg80211_leave_mesh(rdev, dev);
7502}
7503
Johannes Bergdfb89c52012-06-27 09:23:48 +02007504#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007505static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7506 struct cfg80211_registered_device *rdev)
7507{
7508 struct nlattr *nl_pats, *nl_pat;
7509 int i, pat_len;
7510
7511 if (!rdev->wowlan->n_patterns)
7512 return 0;
7513
7514 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7515 if (!nl_pats)
7516 return -ENOBUFS;
7517
7518 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7519 nl_pat = nla_nest_start(msg, i + 1);
7520 if (!nl_pat)
7521 return -ENOBUFS;
7522 pat_len = rdev->wowlan->patterns[i].pattern_len;
7523 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7524 DIV_ROUND_UP(pat_len, 8),
7525 rdev->wowlan->patterns[i].mask) ||
7526 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7527 pat_len, rdev->wowlan->patterns[i].pattern) ||
7528 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7529 rdev->wowlan->patterns[i].pkt_offset))
7530 return -ENOBUFS;
7531 nla_nest_end(msg, nl_pat);
7532 }
7533 nla_nest_end(msg, nl_pats);
7534
7535 return 0;
7536}
7537
Johannes Berg2a0e0472013-01-23 22:57:40 +01007538static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7539 struct cfg80211_wowlan_tcp *tcp)
7540{
7541 struct nlattr *nl_tcp;
7542
7543 if (!tcp)
7544 return 0;
7545
7546 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7547 if (!nl_tcp)
7548 return -ENOBUFS;
7549
7550 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7551 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7552 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7553 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7554 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7555 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7556 tcp->payload_len, tcp->payload) ||
7557 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7558 tcp->data_interval) ||
7559 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7560 tcp->wake_len, tcp->wake_data) ||
7561 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7562 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7563 return -ENOBUFS;
7564
7565 if (tcp->payload_seq.len &&
7566 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7567 sizeof(tcp->payload_seq), &tcp->payload_seq))
7568 return -ENOBUFS;
7569
7570 if (tcp->payload_tok.len &&
7571 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7572 sizeof(tcp->payload_tok) + tcp->tokens_size,
7573 &tcp->payload_tok))
7574 return -ENOBUFS;
7575
7576 return 0;
7577}
7578
Johannes Bergff1b6e62011-05-04 15:37:28 +02007579static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7580{
7581 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7582 struct sk_buff *msg;
7583 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007584 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007585
Johannes Berg2a0e0472013-01-23 22:57:40 +01007586 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7587 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007588 return -EOPNOTSUPP;
7589
Johannes Berg2a0e0472013-01-23 22:57:40 +01007590 if (rdev->wowlan && rdev->wowlan->tcp) {
7591 /* adjust size to have room for all the data */
7592 size += rdev->wowlan->tcp->tokens_size +
7593 rdev->wowlan->tcp->payload_len +
7594 rdev->wowlan->tcp->wake_len +
7595 rdev->wowlan->tcp->wake_len / 8;
7596 }
7597
7598 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007599 if (!msg)
7600 return -ENOMEM;
7601
Eric W. Biederman15e47302012-09-07 20:12:54 +00007602 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007603 NL80211_CMD_GET_WOWLAN);
7604 if (!hdr)
7605 goto nla_put_failure;
7606
7607 if (rdev->wowlan) {
7608 struct nlattr *nl_wowlan;
7609
7610 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7611 if (!nl_wowlan)
7612 goto nla_put_failure;
7613
David S. Miller9360ffd2012-03-29 04:41:26 -04007614 if ((rdev->wowlan->any &&
7615 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7616 (rdev->wowlan->disconnect &&
7617 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7618 (rdev->wowlan->magic_pkt &&
7619 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7620 (rdev->wowlan->gtk_rekey_failure &&
7621 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7622 (rdev->wowlan->eap_identity_req &&
7623 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7624 (rdev->wowlan->four_way_handshake &&
7625 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7626 (rdev->wowlan->rfkill_release &&
7627 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7628 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007629
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007630 if (nl80211_send_wowlan_patterns(msg, rdev))
7631 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007632
7633 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7634 goto nla_put_failure;
7635
Johannes Bergff1b6e62011-05-04 15:37:28 +02007636 nla_nest_end(msg, nl_wowlan);
7637 }
7638
7639 genlmsg_end(msg, hdr);
7640 return genlmsg_reply(msg, info);
7641
7642nla_put_failure:
7643 nlmsg_free(msg);
7644 return -ENOBUFS;
7645}
7646
Johannes Berg2a0e0472013-01-23 22:57:40 +01007647static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7648 struct nlattr *attr,
7649 struct cfg80211_wowlan *trig)
7650{
7651 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7652 struct cfg80211_wowlan_tcp *cfg;
7653 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7654 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7655 u32 size;
7656 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7657 int err, port;
7658
7659 if (!rdev->wiphy.wowlan.tcp)
7660 return -EINVAL;
7661
7662 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7663 nla_data(attr), nla_len(attr),
7664 nl80211_wowlan_tcp_policy);
7665 if (err)
7666 return err;
7667
7668 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7669 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7670 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7671 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7672 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7673 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7674 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7675 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7676 return -EINVAL;
7677
7678 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7679 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7680 return -EINVAL;
7681
7682 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg723d5682013-02-26 13:56:40 +01007683 rdev->wiphy.wowlan.tcp->data_interval_max ||
7684 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007685 return -EINVAL;
7686
7687 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7688 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7689 return -EINVAL;
7690
7691 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7692 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7693 return -EINVAL;
7694
7695 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7696 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7697
7698 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7699 tokens_size = tokln - sizeof(*tok);
7700
7701 if (!tok->len || tokens_size % tok->len)
7702 return -EINVAL;
7703 if (!rdev->wiphy.wowlan.tcp->tok)
7704 return -EINVAL;
7705 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7706 return -EINVAL;
7707 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7708 return -EINVAL;
7709 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7710 return -EINVAL;
7711 if (tok->offset + tok->len > data_size)
7712 return -EINVAL;
7713 }
7714
7715 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7716 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7717 if (!rdev->wiphy.wowlan.tcp->seq)
7718 return -EINVAL;
7719 if (seq->len == 0 || seq->len > 4)
7720 return -EINVAL;
7721 if (seq->len + seq->offset > data_size)
7722 return -EINVAL;
7723 }
7724
7725 size = sizeof(*cfg);
7726 size += data_size;
7727 size += wake_size + wake_mask_size;
7728 size += tokens_size;
7729
7730 cfg = kzalloc(size, GFP_KERNEL);
7731 if (!cfg)
7732 return -ENOMEM;
7733 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7734 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7735 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7736 ETH_ALEN);
7737 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7738 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7739 else
7740 port = 0;
7741#ifdef CONFIG_INET
7742 /* allocate a socket and port for it and use it */
7743 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7744 IPPROTO_TCP, &cfg->sock, 1);
7745 if (err) {
7746 kfree(cfg);
7747 return err;
7748 }
7749 if (inet_csk_get_port(cfg->sock->sk, port)) {
7750 sock_release(cfg->sock);
7751 kfree(cfg);
7752 return -EADDRINUSE;
7753 }
7754 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7755#else
7756 if (!port) {
7757 kfree(cfg);
7758 return -EINVAL;
7759 }
7760 cfg->src_port = port;
7761#endif
7762
7763 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7764 cfg->payload_len = data_size;
7765 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7766 memcpy((void *)cfg->payload,
7767 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7768 data_size);
7769 if (seq)
7770 cfg->payload_seq = *seq;
7771 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7772 cfg->wake_len = wake_size;
7773 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7774 memcpy((void *)cfg->wake_data,
7775 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7776 wake_size);
7777 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7778 data_size + wake_size;
7779 memcpy((void *)cfg->wake_mask,
7780 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7781 wake_mask_size);
7782 if (tok) {
7783 cfg->tokens_size = tokens_size;
7784 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7785 }
7786
7787 trig->tcp = cfg;
7788
7789 return 0;
7790}
7791
Johannes Bergff1b6e62011-05-04 15:37:28 +02007792static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7793{
7794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7795 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007796 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007797 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007798 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7799 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007800 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007801
Johannes Berg2a0e0472013-01-23 22:57:40 +01007802 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7803 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007804 return -EOPNOTSUPP;
7805
Johannes Bergae33bd82012-07-12 16:25:02 +02007806 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7807 cfg80211_rdev_free_wowlan(rdev);
7808 rdev->wowlan = NULL;
7809 goto set_wakeup;
7810 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007811
7812 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7813 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7814 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7815 nl80211_wowlan_policy);
7816 if (err)
7817 return err;
7818
7819 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7820 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7821 return -EINVAL;
7822 new_triggers.any = true;
7823 }
7824
7825 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7826 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7827 return -EINVAL;
7828 new_triggers.disconnect = true;
7829 }
7830
7831 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7832 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7833 return -EINVAL;
7834 new_triggers.magic_pkt = true;
7835 }
7836
Johannes Berg77dbbb12011-07-13 10:48:55 +02007837 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7838 return -EINVAL;
7839
7840 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7841 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7842 return -EINVAL;
7843 new_triggers.gtk_rekey_failure = true;
7844 }
7845
7846 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7847 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7848 return -EINVAL;
7849 new_triggers.eap_identity_req = true;
7850 }
7851
7852 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7853 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7854 return -EINVAL;
7855 new_triggers.four_way_handshake = true;
7856 }
7857
7858 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7859 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7860 return -EINVAL;
7861 new_triggers.rfkill_release = true;
7862 }
7863
Johannes Bergff1b6e62011-05-04 15:37:28 +02007864 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7865 struct nlattr *pat;
7866 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007867 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007868 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7869
7870 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7871 rem)
7872 n_patterns++;
7873 if (n_patterns > wowlan->n_patterns)
7874 return -EINVAL;
7875
7876 new_triggers.patterns = kcalloc(n_patterns,
7877 sizeof(new_triggers.patterns[0]),
7878 GFP_KERNEL);
7879 if (!new_triggers.patterns)
7880 return -ENOMEM;
7881
7882 new_triggers.n_patterns = n_patterns;
7883 i = 0;
7884
7885 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7886 rem) {
7887 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7888 nla_data(pat), nla_len(pat), NULL);
7889 err = -EINVAL;
7890 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7891 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7892 goto error;
7893 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7894 mask_len = DIV_ROUND_UP(pat_len, 8);
7895 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7896 mask_len)
7897 goto error;
7898 if (pat_len > wowlan->pattern_max_len ||
7899 pat_len < wowlan->pattern_min_len)
7900 goto error;
7901
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007902 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7903 pkt_offset = 0;
7904 else
7905 pkt_offset = nla_get_u32(
7906 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7907 if (pkt_offset > wowlan->max_pkt_offset)
7908 goto error;
7909 new_triggers.patterns[i].pkt_offset = pkt_offset;
7910
Johannes Bergff1b6e62011-05-04 15:37:28 +02007911 new_triggers.patterns[i].mask =
7912 kmalloc(mask_len + pat_len, GFP_KERNEL);
7913 if (!new_triggers.patterns[i].mask) {
7914 err = -ENOMEM;
7915 goto error;
7916 }
7917 new_triggers.patterns[i].pattern =
7918 new_triggers.patterns[i].mask + mask_len;
7919 memcpy(new_triggers.patterns[i].mask,
7920 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7921 mask_len);
7922 new_triggers.patterns[i].pattern_len = pat_len;
7923 memcpy(new_triggers.patterns[i].pattern,
7924 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7925 pat_len);
7926 i++;
7927 }
7928 }
7929
Johannes Berg2a0e0472013-01-23 22:57:40 +01007930 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7931 err = nl80211_parse_wowlan_tcp(
7932 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7933 &new_triggers);
7934 if (err)
7935 goto error;
7936 }
7937
Johannes Bergae33bd82012-07-12 16:25:02 +02007938 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7939 if (!ntrig) {
7940 err = -ENOMEM;
7941 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007942 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007943 cfg80211_rdev_free_wowlan(rdev);
7944 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007945
Johannes Bergae33bd82012-07-12 16:25:02 +02007946 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007947 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007948 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007949
Johannes Bergff1b6e62011-05-04 15:37:28 +02007950 return 0;
7951 error:
7952 for (i = 0; i < new_triggers.n_patterns; i++)
7953 kfree(new_triggers.patterns[i].mask);
7954 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007955 if (new_triggers.tcp && new_triggers.tcp->sock)
7956 sock_release(new_triggers.tcp->sock);
7957 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007958 return err;
7959}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007960#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007961
Johannes Berge5497d72011-07-05 16:35:40 +02007962static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7963{
7964 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7965 struct net_device *dev = info->user_ptr[1];
7966 struct wireless_dev *wdev = dev->ieee80211_ptr;
7967 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7968 struct cfg80211_gtk_rekey_data rekey_data;
7969 int err;
7970
7971 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7972 return -EINVAL;
7973
7974 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7975 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7976 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7977 nl80211_rekey_policy);
7978 if (err)
7979 return err;
7980
7981 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7982 return -ERANGE;
7983 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7984 return -ERANGE;
7985 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7986 return -ERANGE;
7987
7988 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7989 NL80211_KEK_LEN);
7990 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7991 NL80211_KCK_LEN);
7992 memcpy(rekey_data.replay_ctr,
7993 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7994 NL80211_REPLAY_CTR_LEN);
7995
7996 wdev_lock(wdev);
7997 if (!wdev->current_bss) {
7998 err = -ENOTCONN;
7999 goto out;
8000 }
8001
8002 if (!rdev->ops->set_rekey_data) {
8003 err = -EOPNOTSUPP;
8004 goto out;
8005 }
8006
Hila Gonene35e4d22012-06-27 17:19:42 +03008007 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008008 out:
8009 wdev_unlock(wdev);
8010 return err;
8011}
8012
Johannes Berg28946da2011-11-04 11:18:12 +01008013static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8014 struct genl_info *info)
8015{
8016 struct net_device *dev = info->user_ptr[1];
8017 struct wireless_dev *wdev = dev->ieee80211_ptr;
8018
8019 if (wdev->iftype != NL80211_IFTYPE_AP &&
8020 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8021 return -EINVAL;
8022
Eric W. Biederman15e47302012-09-07 20:12:54 +00008023 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008024 return -EBUSY;
8025
Eric W. Biederman15e47302012-09-07 20:12:54 +00008026 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008027 return 0;
8028}
8029
Johannes Berg7f6cf312011-11-04 11:18:15 +01008030static int nl80211_probe_client(struct sk_buff *skb,
8031 struct genl_info *info)
8032{
8033 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8034 struct net_device *dev = info->user_ptr[1];
8035 struct wireless_dev *wdev = dev->ieee80211_ptr;
8036 struct sk_buff *msg;
8037 void *hdr;
8038 const u8 *addr;
8039 u64 cookie;
8040 int err;
8041
8042 if (wdev->iftype != NL80211_IFTYPE_AP &&
8043 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8044 return -EOPNOTSUPP;
8045
8046 if (!info->attrs[NL80211_ATTR_MAC])
8047 return -EINVAL;
8048
8049 if (!rdev->ops->probe_client)
8050 return -EOPNOTSUPP;
8051
8052 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8053 if (!msg)
8054 return -ENOMEM;
8055
Eric W. Biederman15e47302012-09-07 20:12:54 +00008056 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008057 NL80211_CMD_PROBE_CLIENT);
8058
8059 if (IS_ERR(hdr)) {
8060 err = PTR_ERR(hdr);
8061 goto free_msg;
8062 }
8063
8064 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8065
Hila Gonene35e4d22012-06-27 17:19:42 +03008066 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008067 if (err)
8068 goto free_msg;
8069
David S. Miller9360ffd2012-03-29 04:41:26 -04008070 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8071 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008072
8073 genlmsg_end(msg, hdr);
8074
8075 return genlmsg_reply(msg, info);
8076
8077 nla_put_failure:
8078 err = -ENOBUFS;
8079 free_msg:
8080 nlmsg_free(msg);
8081 return err;
8082}
8083
Johannes Berg5e760232011-11-04 11:18:17 +01008084static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8085{
8086 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008087 struct cfg80211_beacon_registration *reg, *nreg;
8088 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008089
8090 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8091 return -EOPNOTSUPP;
8092
Ben Greear37c73b52012-10-26 14:49:25 -07008093 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8094 if (!nreg)
8095 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008096
Ben Greear37c73b52012-10-26 14:49:25 -07008097 /* First, check if already registered. */
8098 spin_lock_bh(&rdev->beacon_registrations_lock);
8099 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8100 if (reg->nlportid == info->snd_portid) {
8101 rv = -EALREADY;
8102 goto out_err;
8103 }
8104 }
8105 /* Add it to the list */
8106 nreg->nlportid = info->snd_portid;
8107 list_add(&nreg->list, &rdev->beacon_registrations);
8108
8109 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008110
8111 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008112out_err:
8113 spin_unlock_bh(&rdev->beacon_registrations_lock);
8114 kfree(nreg);
8115 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008116}
8117
Johannes Berg98104fde2012-06-16 00:19:54 +02008118static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8119{
8120 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8121 struct wireless_dev *wdev = info->user_ptr[1];
8122 int err;
8123
8124 if (!rdev->ops->start_p2p_device)
8125 return -EOPNOTSUPP;
8126
8127 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8128 return -EOPNOTSUPP;
8129
8130 if (wdev->p2p_started)
8131 return 0;
8132
8133 mutex_lock(&rdev->devlist_mtx);
8134 err = cfg80211_can_add_interface(rdev, wdev->iftype);
8135 mutex_unlock(&rdev->devlist_mtx);
8136 if (err)
8137 return err;
8138
Johannes Bergeeb126e2012-10-23 15:16:50 +02008139 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008140 if (err)
8141 return err;
8142
8143 wdev->p2p_started = true;
8144 mutex_lock(&rdev->devlist_mtx);
8145 rdev->opencount++;
8146 mutex_unlock(&rdev->devlist_mtx);
8147
8148 return 0;
8149}
8150
8151static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8152{
8153 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8154 struct wireless_dev *wdev = info->user_ptr[1];
8155
8156 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8157 return -EOPNOTSUPP;
8158
8159 if (!rdev->ops->stop_p2p_device)
8160 return -EOPNOTSUPP;
8161
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008162 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +01008163 mutex_lock(&rdev->sched_scan_mtx);
8164 cfg80211_stop_p2p_device(rdev, wdev);
8165 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008166 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg98104fde2012-06-16 00:19:54 +02008167
8168 return 0;
8169}
8170
Johannes Berg3713b4e2013-02-14 16:19:38 +01008171static int nl80211_get_protocol_features(struct sk_buff *skb,
8172 struct genl_info *info)
8173{
8174 void *hdr;
8175 struct sk_buff *msg;
8176
8177 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8178 if (!msg)
8179 return -ENOMEM;
8180
8181 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8182 NL80211_CMD_GET_PROTOCOL_FEATURES);
8183 if (!hdr)
8184 goto nla_put_failure;
8185
8186 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8187 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8188 goto nla_put_failure;
8189
8190 genlmsg_end(msg, hdr);
8191 return genlmsg_reply(msg, info);
8192
8193 nla_put_failure:
8194 kfree_skb(msg);
8195 return -ENOBUFS;
8196}
8197
Jouni Malinen355199e2013-02-27 17:14:27 +02008198static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8199{
8200 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8201 struct cfg80211_update_ft_ies_params ft_params;
8202 struct net_device *dev = info->user_ptr[1];
8203
8204 if (!rdev->ops->update_ft_ies)
8205 return -EOPNOTSUPP;
8206
8207 if (!info->attrs[NL80211_ATTR_MDID] ||
8208 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8209 return -EINVAL;
8210
8211 memset(&ft_params, 0, sizeof(ft_params));
8212 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8213 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8214 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8215
8216 return rdev_update_ft_ies(rdev, dev, &ft_params);
8217}
8218
Johannes Berg4c476992010-10-04 21:36:35 +02008219#define NL80211_FLAG_NEED_WIPHY 0x01
8220#define NL80211_FLAG_NEED_NETDEV 0x02
8221#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008222#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8223#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8224 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008225#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008226/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008227#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8228 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008229
8230static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8231 struct genl_info *info)
8232{
8233 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008234 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008235 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008236 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8237
8238 if (rtnl)
8239 rtnl_lock();
8240
8241 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008242 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008243 if (IS_ERR(rdev)) {
8244 if (rtnl)
8245 rtnl_unlock();
8246 return PTR_ERR(rdev);
8247 }
8248 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008249 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8250 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008251 mutex_lock(&cfg80211_mutex);
8252 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8253 info->attrs);
8254 if (IS_ERR(wdev)) {
8255 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02008256 if (rtnl)
8257 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02008258 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02008259 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008260
Johannes Berg89a54e42012-06-15 14:33:17 +02008261 dev = wdev->netdev;
8262 rdev = wiphy_to_dev(wdev->wiphy);
8263
Johannes Berg1bf614e2012-06-15 15:23:36 +02008264 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8265 if (!dev) {
8266 mutex_unlock(&cfg80211_mutex);
8267 if (rtnl)
8268 rtnl_unlock();
8269 return -EINVAL;
8270 }
8271
8272 info->user_ptr[1] = dev;
8273 } else {
8274 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02008275 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008276
Johannes Berg1bf614e2012-06-15 15:23:36 +02008277 if (dev) {
8278 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8279 !netif_running(dev)) {
8280 mutex_unlock(&cfg80211_mutex);
8281 if (rtnl)
8282 rtnl_unlock();
8283 return -ENETDOWN;
8284 }
8285
8286 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008287 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8288 if (!wdev->p2p_started) {
8289 mutex_unlock(&cfg80211_mutex);
8290 if (rtnl)
8291 rtnl_unlock();
8292 return -ENETDOWN;
8293 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02008294 }
8295
Johannes Berg89a54e42012-06-15 14:33:17 +02008296 cfg80211_lock_rdev(rdev);
8297
8298 mutex_unlock(&cfg80211_mutex);
8299
Johannes Berg4c476992010-10-04 21:36:35 +02008300 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008301 }
8302
8303 return 0;
8304}
8305
8306static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8307 struct genl_info *info)
8308{
8309 if (info->user_ptr[0])
8310 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02008311 if (info->user_ptr[1]) {
8312 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8313 struct wireless_dev *wdev = info->user_ptr[1];
8314
8315 if (wdev->netdev)
8316 dev_put(wdev->netdev);
8317 } else {
8318 dev_put(info->user_ptr[1]);
8319 }
8320 }
Johannes Berg4c476992010-10-04 21:36:35 +02008321 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8322 rtnl_unlock();
8323}
8324
Johannes Berg55682962007-09-20 13:09:35 -04008325static struct genl_ops nl80211_ops[] = {
8326 {
8327 .cmd = NL80211_CMD_GET_WIPHY,
8328 .doit = nl80211_get_wiphy,
8329 .dumpit = nl80211_dump_wiphy,
8330 .policy = nl80211_policy,
8331 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008332 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04008333 },
8334 {
8335 .cmd = NL80211_CMD_SET_WIPHY,
8336 .doit = nl80211_set_wiphy,
8337 .policy = nl80211_policy,
8338 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008339 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008340 },
8341 {
8342 .cmd = NL80211_CMD_GET_INTERFACE,
8343 .doit = nl80211_get_interface,
8344 .dumpit = nl80211_dump_interface,
8345 .policy = nl80211_policy,
8346 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02008347 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04008348 },
8349 {
8350 .cmd = NL80211_CMD_SET_INTERFACE,
8351 .doit = nl80211_set_interface,
8352 .policy = nl80211_policy,
8353 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008354 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8355 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008356 },
8357 {
8358 .cmd = NL80211_CMD_NEW_INTERFACE,
8359 .doit = nl80211_new_interface,
8360 .policy = nl80211_policy,
8361 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008362 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8363 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008364 },
8365 {
8366 .cmd = NL80211_CMD_DEL_INTERFACE,
8367 .doit = nl80211_del_interface,
8368 .policy = nl80211_policy,
8369 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02008370 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008371 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008372 },
Johannes Berg41ade002007-12-19 02:03:29 +01008373 {
8374 .cmd = NL80211_CMD_GET_KEY,
8375 .doit = nl80211_get_key,
8376 .policy = nl80211_policy,
8377 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008378 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008379 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008380 },
8381 {
8382 .cmd = NL80211_CMD_SET_KEY,
8383 .doit = nl80211_set_key,
8384 .policy = nl80211_policy,
8385 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008386 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008387 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008388 },
8389 {
8390 .cmd = NL80211_CMD_NEW_KEY,
8391 .doit = nl80211_new_key,
8392 .policy = nl80211_policy,
8393 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008394 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008395 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008396 },
8397 {
8398 .cmd = NL80211_CMD_DEL_KEY,
8399 .doit = nl80211_del_key,
8400 .policy = nl80211_policy,
8401 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008402 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008403 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008404 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008405 {
8406 .cmd = NL80211_CMD_SET_BEACON,
8407 .policy = nl80211_policy,
8408 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008409 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008410 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008411 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008412 },
8413 {
Johannes Berg88600202012-02-13 15:17:18 +01008414 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008415 .policy = nl80211_policy,
8416 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008417 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008418 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008419 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008420 },
8421 {
Johannes Berg88600202012-02-13 15:17:18 +01008422 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008423 .policy = nl80211_policy,
8424 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008425 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008426 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008427 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008428 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008429 {
8430 .cmd = NL80211_CMD_GET_STATION,
8431 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008432 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008433 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008434 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8435 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008436 },
8437 {
8438 .cmd = NL80211_CMD_SET_STATION,
8439 .doit = nl80211_set_station,
8440 .policy = nl80211_policy,
8441 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008442 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008443 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008444 },
8445 {
8446 .cmd = NL80211_CMD_NEW_STATION,
8447 .doit = nl80211_new_station,
8448 .policy = nl80211_policy,
8449 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008450 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008451 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008452 },
8453 {
8454 .cmd = NL80211_CMD_DEL_STATION,
8455 .doit = nl80211_del_station,
8456 .policy = nl80211_policy,
8457 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008458 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008459 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008460 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008461 {
8462 .cmd = NL80211_CMD_GET_MPATH,
8463 .doit = nl80211_get_mpath,
8464 .dumpit = nl80211_dump_mpath,
8465 .policy = nl80211_policy,
8466 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008467 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008468 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008469 },
8470 {
8471 .cmd = NL80211_CMD_SET_MPATH,
8472 .doit = nl80211_set_mpath,
8473 .policy = nl80211_policy,
8474 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008475 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008476 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008477 },
8478 {
8479 .cmd = NL80211_CMD_NEW_MPATH,
8480 .doit = nl80211_new_mpath,
8481 .policy = nl80211_policy,
8482 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008483 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008484 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008485 },
8486 {
8487 .cmd = NL80211_CMD_DEL_MPATH,
8488 .doit = nl80211_del_mpath,
8489 .policy = nl80211_policy,
8490 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008491 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008492 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008493 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008494 {
8495 .cmd = NL80211_CMD_SET_BSS,
8496 .doit = nl80211_set_bss,
8497 .policy = nl80211_policy,
8498 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008499 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008500 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008501 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008502 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008503 .cmd = NL80211_CMD_GET_REG,
8504 .doit = nl80211_get_reg,
8505 .policy = nl80211_policy,
8506 /* can be retrieved by unprivileged users */
8507 },
8508 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008509 .cmd = NL80211_CMD_SET_REG,
8510 .doit = nl80211_set_reg,
8511 .policy = nl80211_policy,
8512 .flags = GENL_ADMIN_PERM,
8513 },
8514 {
8515 .cmd = NL80211_CMD_REQ_SET_REG,
8516 .doit = nl80211_req_set_reg,
8517 .policy = nl80211_policy,
8518 .flags = GENL_ADMIN_PERM,
8519 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008520 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008521 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8522 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008523 .policy = nl80211_policy,
8524 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008525 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008526 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008527 },
8528 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008529 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8530 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008531 .policy = nl80211_policy,
8532 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008533 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008534 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008535 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008536 {
Johannes Berg2a519312009-02-10 21:25:55 +01008537 .cmd = NL80211_CMD_TRIGGER_SCAN,
8538 .doit = nl80211_trigger_scan,
8539 .policy = nl80211_policy,
8540 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008541 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008542 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008543 },
8544 {
8545 .cmd = NL80211_CMD_GET_SCAN,
8546 .policy = nl80211_policy,
8547 .dumpit = nl80211_dump_scan,
8548 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008549 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008550 .cmd = NL80211_CMD_START_SCHED_SCAN,
8551 .doit = nl80211_start_sched_scan,
8552 .policy = nl80211_policy,
8553 .flags = GENL_ADMIN_PERM,
8554 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8555 NL80211_FLAG_NEED_RTNL,
8556 },
8557 {
8558 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8559 .doit = nl80211_stop_sched_scan,
8560 .policy = nl80211_policy,
8561 .flags = GENL_ADMIN_PERM,
8562 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8563 NL80211_FLAG_NEED_RTNL,
8564 },
8565 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008566 .cmd = NL80211_CMD_AUTHENTICATE,
8567 .doit = nl80211_authenticate,
8568 .policy = nl80211_policy,
8569 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008570 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008571 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008572 },
8573 {
8574 .cmd = NL80211_CMD_ASSOCIATE,
8575 .doit = nl80211_associate,
8576 .policy = nl80211_policy,
8577 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008578 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008579 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008580 },
8581 {
8582 .cmd = NL80211_CMD_DEAUTHENTICATE,
8583 .doit = nl80211_deauthenticate,
8584 .policy = nl80211_policy,
8585 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008586 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008587 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008588 },
8589 {
8590 .cmd = NL80211_CMD_DISASSOCIATE,
8591 .doit = nl80211_disassociate,
8592 .policy = nl80211_policy,
8593 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008594 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008595 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008596 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008597 {
8598 .cmd = NL80211_CMD_JOIN_IBSS,
8599 .doit = nl80211_join_ibss,
8600 .policy = nl80211_policy,
8601 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008602 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008603 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008604 },
8605 {
8606 .cmd = NL80211_CMD_LEAVE_IBSS,
8607 .doit = nl80211_leave_ibss,
8608 .policy = nl80211_policy,
8609 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008610 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008611 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008612 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008613#ifdef CONFIG_NL80211_TESTMODE
8614 {
8615 .cmd = NL80211_CMD_TESTMODE,
8616 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008617 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008618 .policy = nl80211_policy,
8619 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008620 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8621 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008622 },
8623#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008624 {
8625 .cmd = NL80211_CMD_CONNECT,
8626 .doit = nl80211_connect,
8627 .policy = nl80211_policy,
8628 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008629 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008630 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008631 },
8632 {
8633 .cmd = NL80211_CMD_DISCONNECT,
8634 .doit = nl80211_disconnect,
8635 .policy = nl80211_policy,
8636 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008637 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008638 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008639 },
Johannes Berg463d0182009-07-14 00:33:35 +02008640 {
8641 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8642 .doit = nl80211_wiphy_netns,
8643 .policy = nl80211_policy,
8644 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008645 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8646 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008647 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008648 {
8649 .cmd = NL80211_CMD_GET_SURVEY,
8650 .policy = nl80211_policy,
8651 .dumpit = nl80211_dump_survey,
8652 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008653 {
8654 .cmd = NL80211_CMD_SET_PMKSA,
8655 .doit = nl80211_setdel_pmksa,
8656 .policy = nl80211_policy,
8657 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008658 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008659 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008660 },
8661 {
8662 .cmd = NL80211_CMD_DEL_PMKSA,
8663 .doit = nl80211_setdel_pmksa,
8664 .policy = nl80211_policy,
8665 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008666 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008667 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008668 },
8669 {
8670 .cmd = NL80211_CMD_FLUSH_PMKSA,
8671 .doit = nl80211_flush_pmksa,
8672 .policy = nl80211_policy,
8673 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008674 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008675 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008676 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008677 {
8678 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8679 .doit = nl80211_remain_on_channel,
8680 .policy = nl80211_policy,
8681 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008682 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008683 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008684 },
8685 {
8686 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8687 .doit = nl80211_cancel_remain_on_channel,
8688 .policy = nl80211_policy,
8689 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008690 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008691 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008692 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008693 {
8694 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8695 .doit = nl80211_set_tx_bitrate_mask,
8696 .policy = nl80211_policy,
8697 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008698 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8699 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008700 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008701 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008702 .cmd = NL80211_CMD_REGISTER_FRAME,
8703 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008704 .policy = nl80211_policy,
8705 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008706 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008707 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008708 },
8709 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008710 .cmd = NL80211_CMD_FRAME,
8711 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008712 .policy = nl80211_policy,
8713 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008714 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008715 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008716 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008717 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008718 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8719 .doit = nl80211_tx_mgmt_cancel_wait,
8720 .policy = nl80211_policy,
8721 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008722 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008723 NL80211_FLAG_NEED_RTNL,
8724 },
8725 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008726 .cmd = NL80211_CMD_SET_POWER_SAVE,
8727 .doit = nl80211_set_power_save,
8728 .policy = nl80211_policy,
8729 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008730 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8731 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008732 },
8733 {
8734 .cmd = NL80211_CMD_GET_POWER_SAVE,
8735 .doit = nl80211_get_power_save,
8736 .policy = nl80211_policy,
8737 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008738 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8739 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008740 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008741 {
8742 .cmd = NL80211_CMD_SET_CQM,
8743 .doit = nl80211_set_cqm,
8744 .policy = nl80211_policy,
8745 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008746 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8747 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008748 },
Johannes Bergf444de02010-05-05 15:25:02 +02008749 {
8750 .cmd = NL80211_CMD_SET_CHANNEL,
8751 .doit = nl80211_set_channel,
8752 .policy = nl80211_policy,
8753 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008754 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8755 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008756 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008757 {
8758 .cmd = NL80211_CMD_SET_WDS_PEER,
8759 .doit = nl80211_set_wds_peer,
8760 .policy = nl80211_policy,
8761 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008762 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8763 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008764 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008765 {
8766 .cmd = NL80211_CMD_JOIN_MESH,
8767 .doit = nl80211_join_mesh,
8768 .policy = nl80211_policy,
8769 .flags = GENL_ADMIN_PERM,
8770 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8771 NL80211_FLAG_NEED_RTNL,
8772 },
8773 {
8774 .cmd = NL80211_CMD_LEAVE_MESH,
8775 .doit = nl80211_leave_mesh,
8776 .policy = nl80211_policy,
8777 .flags = GENL_ADMIN_PERM,
8778 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8779 NL80211_FLAG_NEED_RTNL,
8780 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008781#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008782 {
8783 .cmd = NL80211_CMD_GET_WOWLAN,
8784 .doit = nl80211_get_wowlan,
8785 .policy = nl80211_policy,
8786 /* can be retrieved by unprivileged users */
8787 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8788 NL80211_FLAG_NEED_RTNL,
8789 },
8790 {
8791 .cmd = NL80211_CMD_SET_WOWLAN,
8792 .doit = nl80211_set_wowlan,
8793 .policy = nl80211_policy,
8794 .flags = GENL_ADMIN_PERM,
8795 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8796 NL80211_FLAG_NEED_RTNL,
8797 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008798#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008799 {
8800 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8801 .doit = nl80211_set_rekey_data,
8802 .policy = nl80211_policy,
8803 .flags = GENL_ADMIN_PERM,
8804 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8805 NL80211_FLAG_NEED_RTNL,
8806 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008807 {
8808 .cmd = NL80211_CMD_TDLS_MGMT,
8809 .doit = nl80211_tdls_mgmt,
8810 .policy = nl80211_policy,
8811 .flags = GENL_ADMIN_PERM,
8812 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8813 NL80211_FLAG_NEED_RTNL,
8814 },
8815 {
8816 .cmd = NL80211_CMD_TDLS_OPER,
8817 .doit = nl80211_tdls_oper,
8818 .policy = nl80211_policy,
8819 .flags = GENL_ADMIN_PERM,
8820 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8821 NL80211_FLAG_NEED_RTNL,
8822 },
Johannes Berg28946da2011-11-04 11:18:12 +01008823 {
8824 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8825 .doit = nl80211_register_unexpected_frame,
8826 .policy = nl80211_policy,
8827 .flags = GENL_ADMIN_PERM,
8828 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8829 NL80211_FLAG_NEED_RTNL,
8830 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008831 {
8832 .cmd = NL80211_CMD_PROBE_CLIENT,
8833 .doit = nl80211_probe_client,
8834 .policy = nl80211_policy,
8835 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008836 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008837 NL80211_FLAG_NEED_RTNL,
8838 },
Johannes Berg5e760232011-11-04 11:18:17 +01008839 {
8840 .cmd = NL80211_CMD_REGISTER_BEACONS,
8841 .doit = nl80211_register_beacons,
8842 .policy = nl80211_policy,
8843 .flags = GENL_ADMIN_PERM,
8844 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8845 NL80211_FLAG_NEED_RTNL,
8846 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008847 {
8848 .cmd = NL80211_CMD_SET_NOACK_MAP,
8849 .doit = nl80211_set_noack_map,
8850 .policy = nl80211_policy,
8851 .flags = GENL_ADMIN_PERM,
8852 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8853 NL80211_FLAG_NEED_RTNL,
8854 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008855 {
8856 .cmd = NL80211_CMD_START_P2P_DEVICE,
8857 .doit = nl80211_start_p2p_device,
8858 .policy = nl80211_policy,
8859 .flags = GENL_ADMIN_PERM,
8860 .internal_flags = NL80211_FLAG_NEED_WDEV |
8861 NL80211_FLAG_NEED_RTNL,
8862 },
8863 {
8864 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8865 .doit = nl80211_stop_p2p_device,
8866 .policy = nl80211_policy,
8867 .flags = GENL_ADMIN_PERM,
8868 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8869 NL80211_FLAG_NEED_RTNL,
8870 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008871 {
8872 .cmd = NL80211_CMD_SET_MCAST_RATE,
8873 .doit = nl80211_set_mcast_rate,
8874 .policy = nl80211_policy,
8875 .flags = GENL_ADMIN_PERM,
8876 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8877 NL80211_FLAG_NEED_RTNL,
8878 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308879 {
8880 .cmd = NL80211_CMD_SET_MAC_ACL,
8881 .doit = nl80211_set_mac_acl,
8882 .policy = nl80211_policy,
8883 .flags = GENL_ADMIN_PERM,
8884 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8885 NL80211_FLAG_NEED_RTNL,
8886 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008887 {
8888 .cmd = NL80211_CMD_RADAR_DETECT,
8889 .doit = nl80211_start_radar_detection,
8890 .policy = nl80211_policy,
8891 .flags = GENL_ADMIN_PERM,
8892 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8893 NL80211_FLAG_NEED_RTNL,
8894 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01008895 {
8896 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
8897 .doit = nl80211_get_protocol_features,
8898 .policy = nl80211_policy,
8899 },
Jouni Malinen355199e2013-02-27 17:14:27 +02008900 {
8901 .cmd = NL80211_CMD_UPDATE_FT_IES,
8902 .doit = nl80211_update_ft_ies,
8903 .policy = nl80211_policy,
8904 .flags = GENL_ADMIN_PERM,
8905 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8906 NL80211_FLAG_NEED_RTNL,
8907 },
Johannes Berg55682962007-09-20 13:09:35 -04008908};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008909
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008910static struct genl_multicast_group nl80211_mlme_mcgrp = {
8911 .name = "mlme",
8912};
Johannes Berg55682962007-09-20 13:09:35 -04008913
8914/* multicast groups */
8915static struct genl_multicast_group nl80211_config_mcgrp = {
8916 .name = "config",
8917};
Johannes Berg2a519312009-02-10 21:25:55 +01008918static struct genl_multicast_group nl80211_scan_mcgrp = {
8919 .name = "scan",
8920};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008921static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8922 .name = "regulatory",
8923};
Johannes Berg55682962007-09-20 13:09:35 -04008924
8925/* notification functions */
8926
8927void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8928{
8929 struct sk_buff *msg;
8930
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008931 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008932 if (!msg)
8933 return;
8934
Johannes Berg3713b4e2013-02-14 16:19:38 +01008935 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
8936 false, NULL, NULL, NULL) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04008937 nlmsg_free(msg);
8938 return;
8939 }
8940
Johannes Berg463d0182009-07-14 00:33:35 +02008941 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8942 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008943}
8944
Johannes Berg362a4152009-05-24 16:43:15 +02008945static int nl80211_add_scan_req(struct sk_buff *msg,
8946 struct cfg80211_registered_device *rdev)
8947{
8948 struct cfg80211_scan_request *req = rdev->scan_req;
8949 struct nlattr *nest;
8950 int i;
8951
Johannes Bergf9f47522013-03-19 15:04:07 +01008952 lockdep_assert_held(&rdev->sched_scan_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +02008953
Johannes Berg362a4152009-05-24 16:43:15 +02008954 if (WARN_ON(!req))
8955 return 0;
8956
8957 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8958 if (!nest)
8959 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008960 for (i = 0; i < req->n_ssids; i++) {
8961 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8962 goto nla_put_failure;
8963 }
Johannes Berg362a4152009-05-24 16:43:15 +02008964 nla_nest_end(msg, nest);
8965
8966 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8967 if (!nest)
8968 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008969 for (i = 0; i < req->n_channels; i++) {
8970 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8971 goto nla_put_failure;
8972 }
Johannes Berg362a4152009-05-24 16:43:15 +02008973 nla_nest_end(msg, nest);
8974
David S. Miller9360ffd2012-03-29 04:41:26 -04008975 if (req->ie &&
8976 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8977 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008978
Sam Lefflered4737712012-10-11 21:03:31 -07008979 if (req->flags)
8980 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8981
Johannes Berg362a4152009-05-24 16:43:15 +02008982 return 0;
8983 nla_put_failure:
8984 return -ENOBUFS;
8985}
8986
Johannes Berga538e2d2009-06-16 19:56:42 +02008987static int nl80211_send_scan_msg(struct sk_buff *msg,
8988 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008989 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008990 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008991 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008992{
8993 void *hdr;
8994
Eric W. Biederman15e47302012-09-07 20:12:54 +00008995 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008996 if (!hdr)
8997 return -1;
8998
David S. Miller9360ffd2012-03-29 04:41:26 -04008999 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009000 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9001 wdev->netdev->ifindex)) ||
9002 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009003 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009004
Johannes Berg362a4152009-05-24 16:43:15 +02009005 /* ignore errors and send incomplete event anyway */
9006 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009007
9008 return genlmsg_end(msg, hdr);
9009
9010 nla_put_failure:
9011 genlmsg_cancel(msg, hdr);
9012 return -EMSGSIZE;
9013}
9014
Luciano Coelho807f8a82011-05-11 17:09:35 +03009015static int
9016nl80211_send_sched_scan_msg(struct sk_buff *msg,
9017 struct cfg80211_registered_device *rdev,
9018 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009019 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009020{
9021 void *hdr;
9022
Eric W. Biederman15e47302012-09-07 20:12:54 +00009023 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009024 if (!hdr)
9025 return -1;
9026
David S. Miller9360ffd2012-03-29 04:41:26 -04009027 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9028 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9029 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009030
9031 return genlmsg_end(msg, hdr);
9032
9033 nla_put_failure:
9034 genlmsg_cancel(msg, hdr);
9035 return -EMSGSIZE;
9036}
9037
Johannes Berga538e2d2009-06-16 19:56:42 +02009038void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009039 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009040{
9041 struct sk_buff *msg;
9042
Thomas Graf58050fc2012-06-28 03:57:45 +00009043 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009044 if (!msg)
9045 return;
9046
Johannes Bergfd014282012-06-18 19:17:03 +02009047 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009048 NL80211_CMD_TRIGGER_SCAN) < 0) {
9049 nlmsg_free(msg);
9050 return;
9051 }
9052
Johannes Berg463d0182009-07-14 00:33:35 +02009053 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9054 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009055}
9056
Johannes Berg2a519312009-02-10 21:25:55 +01009057void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009058 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009059{
9060 struct sk_buff *msg;
9061
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009062 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009063 if (!msg)
9064 return;
9065
Johannes Bergfd014282012-06-18 19:17:03 +02009066 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009067 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009068 nlmsg_free(msg);
9069 return;
9070 }
9071
Johannes Berg463d0182009-07-14 00:33:35 +02009072 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9073 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009074}
9075
9076void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009077 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009078{
9079 struct sk_buff *msg;
9080
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009081 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009082 if (!msg)
9083 return;
9084
Johannes Bergfd014282012-06-18 19:17:03 +02009085 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009086 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009087 nlmsg_free(msg);
9088 return;
9089 }
9090
Johannes Berg463d0182009-07-14 00:33:35 +02009091 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9092 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009093}
9094
Luciano Coelho807f8a82011-05-11 17:09:35 +03009095void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9096 struct net_device *netdev)
9097{
9098 struct sk_buff *msg;
9099
9100 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9101 if (!msg)
9102 return;
9103
9104 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9105 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9106 nlmsg_free(msg);
9107 return;
9108 }
9109
9110 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9111 nl80211_scan_mcgrp.id, GFP_KERNEL);
9112}
9113
9114void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9115 struct net_device *netdev, u32 cmd)
9116{
9117 struct sk_buff *msg;
9118
Thomas Graf58050fc2012-06-28 03:57:45 +00009119 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009120 if (!msg)
9121 return;
9122
9123 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9124 nlmsg_free(msg);
9125 return;
9126 }
9127
9128 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9129 nl80211_scan_mcgrp.id, GFP_KERNEL);
9130}
9131
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009132/*
9133 * This can happen on global regulatory changes or device specific settings
9134 * based on custom world regulatory domains.
9135 */
9136void nl80211_send_reg_change_event(struct regulatory_request *request)
9137{
9138 struct sk_buff *msg;
9139 void *hdr;
9140
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009141 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009142 if (!msg)
9143 return;
9144
9145 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9146 if (!hdr) {
9147 nlmsg_free(msg);
9148 return;
9149 }
9150
9151 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04009152 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9153 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009154
David S. Miller9360ffd2012-03-29 04:41:26 -04009155 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9156 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9157 NL80211_REGDOM_TYPE_WORLD))
9158 goto nla_put_failure;
9159 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9160 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9161 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9162 goto nla_put_failure;
9163 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9164 request->intersect) {
9165 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9166 NL80211_REGDOM_TYPE_INTERSECTION))
9167 goto nla_put_failure;
9168 } else {
9169 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9170 NL80211_REGDOM_TYPE_COUNTRY) ||
9171 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9172 request->alpha2))
9173 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009174 }
9175
Johannes Bergf4173762012-12-03 18:23:37 +01009176 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04009177 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9178 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009179
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009180 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009181
Johannes Bergbc43b282009-07-25 10:54:13 +02009182 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009183 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009184 GFP_ATOMIC);
9185 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009186
9187 return;
9188
9189nla_put_failure:
9190 genlmsg_cancel(msg, hdr);
9191 nlmsg_free(msg);
9192}
9193
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009194static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9195 struct net_device *netdev,
9196 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009197 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009198{
9199 struct sk_buff *msg;
9200 void *hdr;
9201
Johannes Berge6d6e342009-07-01 21:26:47 +02009202 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009203 if (!msg)
9204 return;
9205
9206 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9207 if (!hdr) {
9208 nlmsg_free(msg);
9209 return;
9210 }
9211
David S. Miller9360ffd2012-03-29 04:41:26 -04009212 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9213 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9214 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9215 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009216
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009217 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009218
Johannes Berg463d0182009-07-14 00:33:35 +02009219 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9220 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009221 return;
9222
9223 nla_put_failure:
9224 genlmsg_cancel(msg, hdr);
9225 nlmsg_free(msg);
9226}
9227
9228void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009229 struct net_device *netdev, const u8 *buf,
9230 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009231{
9232 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009233 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009234}
9235
9236void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9237 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009238 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009239{
Johannes Berge6d6e342009-07-01 21:26:47 +02009240 nl80211_send_mlme_event(rdev, netdev, buf, len,
9241 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009242}
9243
Jouni Malinen53b46b82009-03-27 20:53:56 +02009244void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009245 struct net_device *netdev, const u8 *buf,
9246 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009247{
9248 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009249 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009250}
9251
Jouni Malinen53b46b82009-03-27 20:53:56 +02009252void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9253 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009254 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009255{
9256 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009257 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009258}
9259
Johannes Berg947add32013-02-22 22:05:20 +01009260void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
9261 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +02009262{
Johannes Berg947add32013-02-22 22:05:20 +01009263 struct wireless_dev *wdev = dev->ieee80211_ptr;
9264 struct wiphy *wiphy = wdev->wiphy;
9265 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009266
Johannes Berg947add32013-02-22 22:05:20 +01009267 trace_cfg80211_send_unprot_deauth(dev);
9268 nl80211_send_mlme_event(rdev, dev, buf, len,
9269 NL80211_CMD_UNPROT_DEAUTHENTICATE, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009270}
Johannes Berg947add32013-02-22 22:05:20 +01009271EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
9272
9273void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
9274 size_t len)
9275{
9276 struct wireless_dev *wdev = dev->ieee80211_ptr;
9277 struct wiphy *wiphy = wdev->wiphy;
9278 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9279
9280 trace_cfg80211_send_unprot_disassoc(dev);
9281 nl80211_send_mlme_event(rdev, dev, buf, len,
9282 NL80211_CMD_UNPROT_DISASSOCIATE, GFP_ATOMIC);
9283}
9284EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009285
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04009286static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9287 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02009288 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009289{
9290 struct sk_buff *msg;
9291 void *hdr;
9292
Johannes Berge6d6e342009-07-01 21:26:47 +02009293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009294 if (!msg)
9295 return;
9296
9297 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9298 if (!hdr) {
9299 nlmsg_free(msg);
9300 return;
9301 }
9302
David S. Miller9360ffd2012-03-29 04:41:26 -04009303 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9304 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9305 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9306 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9307 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03009308
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009309 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03009310
Johannes Berg463d0182009-07-14 00:33:35 +02009311 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9312 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009313 return;
9314
9315 nla_put_failure:
9316 genlmsg_cancel(msg, hdr);
9317 nlmsg_free(msg);
9318}
9319
9320void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009321 struct net_device *netdev, const u8 *addr,
9322 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009323{
9324 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02009325 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009326}
9327
9328void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009329 struct net_device *netdev, const u8 *addr,
9330 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009331{
Johannes Berge6d6e342009-07-01 21:26:47 +02009332 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9333 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009334}
9335
Samuel Ortizb23aa672009-07-01 21:26:54 +02009336void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9337 struct net_device *netdev, const u8 *bssid,
9338 const u8 *req_ie, size_t req_ie_len,
9339 const u8 *resp_ie, size_t resp_ie_len,
9340 u16 status, gfp_t gfp)
9341{
9342 struct sk_buff *msg;
9343 void *hdr;
9344
Thomas Graf58050fc2012-06-28 03:57:45 +00009345 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009346 if (!msg)
9347 return;
9348
9349 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9350 if (!hdr) {
9351 nlmsg_free(msg);
9352 return;
9353 }
9354
David S. Miller9360ffd2012-03-29 04:41:26 -04009355 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9356 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9357 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9358 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9359 (req_ie &&
9360 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9361 (resp_ie &&
9362 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9363 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009364
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009365 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009366
Johannes Berg463d0182009-07-14 00:33:35 +02009367 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9368 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009369 return;
9370
9371 nla_put_failure:
9372 genlmsg_cancel(msg, hdr);
9373 nlmsg_free(msg);
9374
9375}
9376
9377void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9378 struct net_device *netdev, const u8 *bssid,
9379 const u8 *req_ie, size_t req_ie_len,
9380 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9381{
9382 struct sk_buff *msg;
9383 void *hdr;
9384
Thomas Graf58050fc2012-06-28 03:57:45 +00009385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009386 if (!msg)
9387 return;
9388
9389 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9390 if (!hdr) {
9391 nlmsg_free(msg);
9392 return;
9393 }
9394
David S. Miller9360ffd2012-03-29 04:41:26 -04009395 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9396 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9397 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9398 (req_ie &&
9399 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9400 (resp_ie &&
9401 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9402 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009403
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009404 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009405
Johannes Berg463d0182009-07-14 00:33:35 +02009406 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9407 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009408 return;
9409
9410 nla_put_failure:
9411 genlmsg_cancel(msg, hdr);
9412 nlmsg_free(msg);
9413
9414}
9415
9416void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9417 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02009418 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009419{
9420 struct sk_buff *msg;
9421 void *hdr;
9422
Thomas Graf58050fc2012-06-28 03:57:45 +00009423 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009424 if (!msg)
9425 return;
9426
9427 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
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 (from_ap && reason &&
9436 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9437 (from_ap &&
9438 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9439 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9440 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009441
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009442 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009443
Johannes Berg463d0182009-07-14 00:33:35 +02009444 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9445 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009446 return;
9447
9448 nla_put_failure:
9449 genlmsg_cancel(msg, hdr);
9450 nlmsg_free(msg);
9451
9452}
9453
Johannes Berg04a773a2009-04-19 21:24:32 +02009454void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9455 struct net_device *netdev, const u8 *bssid,
9456 gfp_t gfp)
9457{
9458 struct sk_buff *msg;
9459 void *hdr;
9460
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009461 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009462 if (!msg)
9463 return;
9464
9465 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9466 if (!hdr) {
9467 nlmsg_free(msg);
9468 return;
9469 }
9470
David S. Miller9360ffd2012-03-29 04:41:26 -04009471 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9472 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9473 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9474 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009475
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009476 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009477
Johannes Berg463d0182009-07-14 00:33:35 +02009478 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9479 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009480 return;
9481
9482 nla_put_failure:
9483 genlmsg_cancel(msg, hdr);
9484 nlmsg_free(msg);
9485}
9486
Johannes Berg947add32013-02-22 22:05:20 +01009487void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
9488 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -07009489{
Johannes Berg947add32013-02-22 22:05:20 +01009490 struct wireless_dev *wdev = dev->ieee80211_ptr;
9491 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009492 struct sk_buff *msg;
9493 void *hdr;
9494
Johannes Berg947add32013-02-22 22:05:20 +01009495 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
9496 return;
9497
9498 trace_cfg80211_notify_new_peer_candidate(dev, addr);
9499
Javier Cardonac93b5e72011-04-07 15:08:34 -07009500 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9501 if (!msg)
9502 return;
9503
9504 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9505 if (!hdr) {
9506 nlmsg_free(msg);
9507 return;
9508 }
9509
David S. Miller9360ffd2012-03-29 04:41:26 -04009510 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +01009511 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9512 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009513 (ie_len && ie &&
9514 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9515 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009516
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009517 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009518
9519 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9520 nl80211_mlme_mcgrp.id, gfp);
9521 return;
9522
9523 nla_put_failure:
9524 genlmsg_cancel(msg, hdr);
9525 nlmsg_free(msg);
9526}
Johannes Berg947add32013-02-22 22:05:20 +01009527EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009528
Jouni Malinena3b8b052009-03-27 21:59:49 +02009529void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9530 struct net_device *netdev, const u8 *addr,
9531 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009532 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009533{
9534 struct sk_buff *msg;
9535 void *hdr;
9536
Johannes Berge6d6e342009-07-01 21:26:47 +02009537 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009538 if (!msg)
9539 return;
9540
9541 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9542 if (!hdr) {
9543 nlmsg_free(msg);
9544 return;
9545 }
9546
David S. Miller9360ffd2012-03-29 04:41:26 -04009547 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9548 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9549 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9550 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9551 (key_id != -1 &&
9552 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9553 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9554 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009555
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009556 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009557
Johannes Berg463d0182009-07-14 00:33:35 +02009558 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9559 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009560 return;
9561
9562 nla_put_failure:
9563 genlmsg_cancel(msg, hdr);
9564 nlmsg_free(msg);
9565}
9566
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009567void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9568 struct ieee80211_channel *channel_before,
9569 struct ieee80211_channel *channel_after)
9570{
9571 struct sk_buff *msg;
9572 void *hdr;
9573 struct nlattr *nl_freq;
9574
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009575 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009576 if (!msg)
9577 return;
9578
9579 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9580 if (!hdr) {
9581 nlmsg_free(msg);
9582 return;
9583 }
9584
9585 /*
9586 * Since we are applying the beacon hint to a wiphy we know its
9587 * wiphy_idx is valid
9588 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009589 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9590 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009591
9592 /* Before */
9593 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9594 if (!nl_freq)
9595 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009596 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009597 goto nla_put_failure;
9598 nla_nest_end(msg, nl_freq);
9599
9600 /* After */
9601 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9602 if (!nl_freq)
9603 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009604 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009605 goto nla_put_failure;
9606 nla_nest_end(msg, nl_freq);
9607
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009608 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009609
Johannes Berg463d0182009-07-14 00:33:35 +02009610 rcu_read_lock();
9611 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9612 GFP_ATOMIC);
9613 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009614
9615 return;
9616
9617nla_put_failure:
9618 genlmsg_cancel(msg, hdr);
9619 nlmsg_free(msg);
9620}
9621
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009622static void nl80211_send_remain_on_chan_event(
9623 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009624 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009625 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009626 unsigned int duration, gfp_t gfp)
9627{
9628 struct sk_buff *msg;
9629 void *hdr;
9630
9631 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9632 if (!msg)
9633 return;
9634
9635 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9636 if (!hdr) {
9637 nlmsg_free(msg);
9638 return;
9639 }
9640
David S. Miller9360ffd2012-03-29 04:41:26 -04009641 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009642 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9643 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009644 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009645 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009646 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9647 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009648 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9649 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009650
David S. Miller9360ffd2012-03-29 04:41:26 -04009651 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9652 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9653 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009654
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009655 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009656
9657 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9658 nl80211_mlme_mcgrp.id, gfp);
9659 return;
9660
9661 nla_put_failure:
9662 genlmsg_cancel(msg, hdr);
9663 nlmsg_free(msg);
9664}
9665
Johannes Berg947add32013-02-22 22:05:20 +01009666void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9667 struct ieee80211_channel *chan,
9668 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009669{
Johannes Berg947add32013-02-22 22:05:20 +01009670 struct wiphy *wiphy = wdev->wiphy;
9671 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9672
9673 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009674 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009675 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009676 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009677}
Johannes Berg947add32013-02-22 22:05:20 +01009678EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009679
Johannes Berg947add32013-02-22 22:05:20 +01009680void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9681 struct ieee80211_channel *chan,
9682 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009683{
Johannes Berg947add32013-02-22 22:05:20 +01009684 struct wiphy *wiphy = wdev->wiphy;
9685 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9686
9687 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009688 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009689 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009690}
Johannes Berg947add32013-02-22 22:05:20 +01009691EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009692
Johannes Berg947add32013-02-22 22:05:20 +01009693void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
9694 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +01009695{
Johannes Berg947add32013-02-22 22:05:20 +01009696 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9697 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +01009698 struct sk_buff *msg;
9699
Johannes Berg947add32013-02-22 22:05:20 +01009700 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
9701
Thomas Graf58050fc2012-06-28 03:57:45 +00009702 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009703 if (!msg)
9704 return;
9705
John W. Linville66266b32012-03-15 13:25:41 -04009706 if (nl80211_send_station(msg, 0, 0, 0,
9707 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009708 nlmsg_free(msg);
9709 return;
9710 }
9711
9712 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9713 nl80211_mlme_mcgrp.id, gfp);
9714}
Johannes Berg947add32013-02-22 22:05:20 +01009715EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +01009716
Johannes Berg947add32013-02-22 22:05:20 +01009717void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +02009718{
Johannes Berg947add32013-02-22 22:05:20 +01009719 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9720 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +02009721 struct sk_buff *msg;
9722 void *hdr;
9723
Johannes Berg947add32013-02-22 22:05:20 +01009724 trace_cfg80211_del_sta(dev, mac_addr);
9725
Thomas Graf58050fc2012-06-28 03:57:45 +00009726 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009727 if (!msg)
9728 return;
9729
9730 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9731 if (!hdr) {
9732 nlmsg_free(msg);
9733 return;
9734 }
9735
David S. Miller9360ffd2012-03-29 04:41:26 -04009736 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9737 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9738 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009739
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009740 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009741
9742 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9743 nl80211_mlme_mcgrp.id, gfp);
9744 return;
9745
9746 nla_put_failure:
9747 genlmsg_cancel(msg, hdr);
9748 nlmsg_free(msg);
9749}
Johannes Berg947add32013-02-22 22:05:20 +01009750EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +02009751
Johannes Berg947add32013-02-22 22:05:20 +01009752void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
9753 enum nl80211_connect_failed_reason reason,
9754 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309755{
Johannes Berg947add32013-02-22 22:05:20 +01009756 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9757 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309758 struct sk_buff *msg;
9759 void *hdr;
9760
9761 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9762 if (!msg)
9763 return;
9764
9765 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9766 if (!hdr) {
9767 nlmsg_free(msg);
9768 return;
9769 }
9770
9771 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9772 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9773 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9774 goto nla_put_failure;
9775
9776 genlmsg_end(msg, hdr);
9777
9778 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9779 nl80211_mlme_mcgrp.id, gfp);
9780 return;
9781
9782 nla_put_failure:
9783 genlmsg_cancel(msg, hdr);
9784 nlmsg_free(msg);
9785}
Johannes Berg947add32013-02-22 22:05:20 +01009786EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309787
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009788static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9789 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009790{
9791 struct wireless_dev *wdev = dev->ieee80211_ptr;
9792 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9793 struct sk_buff *msg;
9794 void *hdr;
9795 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009796 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009797
Eric W. Biederman15e47302012-09-07 20:12:54 +00009798 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009799 return false;
9800
9801 msg = nlmsg_new(100, gfp);
9802 if (!msg)
9803 return true;
9804
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009805 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009806 if (!hdr) {
9807 nlmsg_free(msg);
9808 return true;
9809 }
9810
David S. Miller9360ffd2012-03-29 04:41:26 -04009811 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9812 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9813 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9814 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009815
9816 err = genlmsg_end(msg, hdr);
9817 if (err < 0) {
9818 nlmsg_free(msg);
9819 return true;
9820 }
9821
Eric W. Biederman15e47302012-09-07 20:12:54 +00009822 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009823 return true;
9824
9825 nla_put_failure:
9826 genlmsg_cancel(msg, hdr);
9827 nlmsg_free(msg);
9828 return true;
9829}
9830
Johannes Berg947add32013-02-22 22:05:20 +01009831bool cfg80211_rx_spurious_frame(struct net_device *dev,
9832 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009833{
Johannes Berg947add32013-02-22 22:05:20 +01009834 struct wireless_dev *wdev = dev->ieee80211_ptr;
9835 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009836
Johannes Berg947add32013-02-22 22:05:20 +01009837 trace_cfg80211_rx_spurious_frame(dev, addr);
9838
9839 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9840 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
9841 trace_cfg80211_return_bool(false);
9842 return false;
9843 }
9844 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9845 addr, gfp);
9846 trace_cfg80211_return_bool(ret);
9847 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009848}
Johannes Berg947add32013-02-22 22:05:20 +01009849EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
9850
9851bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
9852 const u8 *addr, gfp_t gfp)
9853{
9854 struct wireless_dev *wdev = dev->ieee80211_ptr;
9855 bool ret;
9856
9857 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
9858
9859 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9860 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
9861 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
9862 trace_cfg80211_return_bool(false);
9863 return false;
9864 }
9865 ret = __nl80211_unexpected_frame(dev,
9866 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9867 addr, gfp);
9868 trace_cfg80211_return_bool(ret);
9869 return ret;
9870}
9871EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009872
Johannes Berg2e161f72010-08-12 15:38:38 +02009873int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009874 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009875 int freq, int sig_dbm,
9876 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009877{
Johannes Berg71bbc992012-06-15 15:30:18 +02009878 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009879 struct sk_buff *msg;
9880 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009881
9882 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9883 if (!msg)
9884 return -ENOMEM;
9885
Johannes Berg2e161f72010-08-12 15:38:38 +02009886 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009887 if (!hdr) {
9888 nlmsg_free(msg);
9889 return -ENOMEM;
9890 }
9891
David S. Miller9360ffd2012-03-29 04:41:26 -04009892 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009893 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9894 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009895 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9896 (sig_dbm &&
9897 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9898 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9899 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009900
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009901 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009902
Eric W. Biederman15e47302012-09-07 20:12:54 +00009903 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009904
9905 nla_put_failure:
9906 genlmsg_cancel(msg, hdr);
9907 nlmsg_free(msg);
9908 return -ENOBUFS;
9909}
9910
Johannes Berg947add32013-02-22 22:05:20 +01009911void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
9912 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009913{
Johannes Berg947add32013-02-22 22:05:20 +01009914 struct wiphy *wiphy = wdev->wiphy;
9915 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +02009916 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009917 struct sk_buff *msg;
9918 void *hdr;
9919
Johannes Berg947add32013-02-22 22:05:20 +01009920 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
9921
Jouni Malinen026331c2010-02-15 12:53:10 +02009922 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9923 if (!msg)
9924 return;
9925
Johannes Berg2e161f72010-08-12 15:38:38 +02009926 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009927 if (!hdr) {
9928 nlmsg_free(msg);
9929 return;
9930 }
9931
David S. Miller9360ffd2012-03-29 04:41:26 -04009932 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009933 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9934 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009935 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9936 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9937 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9938 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009939
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009940 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009941
9942 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9943 return;
9944
9945 nla_put_failure:
9946 genlmsg_cancel(msg, hdr);
9947 nlmsg_free(msg);
9948}
Johannes Berg947add32013-02-22 22:05:20 +01009949EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +02009950
Johannes Berg947add32013-02-22 22:05:20 +01009951void cfg80211_cqm_rssi_notify(struct net_device *dev,
9952 enum nl80211_cqm_rssi_threshold_event rssi_event,
9953 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009954{
Johannes Berg947add32013-02-22 22:05:20 +01009955 struct wireless_dev *wdev = dev->ieee80211_ptr;
9956 struct wiphy *wiphy = wdev->wiphy;
9957 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009958 struct sk_buff *msg;
9959 struct nlattr *pinfoattr;
9960 void *hdr;
9961
Johannes Berg947add32013-02-22 22:05:20 +01009962 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
9963
Thomas Graf58050fc2012-06-28 03:57:45 +00009964 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009965 if (!msg)
9966 return;
9967
9968 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9969 if (!hdr) {
9970 nlmsg_free(msg);
9971 return;
9972 }
9973
David S. Miller9360ffd2012-03-29 04:41:26 -04009974 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +01009975 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -04009976 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009977
9978 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9979 if (!pinfoattr)
9980 goto nla_put_failure;
9981
David S. Miller9360ffd2012-03-29 04:41:26 -04009982 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9983 rssi_event))
9984 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009985
9986 nla_nest_end(msg, pinfoattr);
9987
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009988 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009989
9990 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9991 nl80211_mlme_mcgrp.id, gfp);
9992 return;
9993
9994 nla_put_failure:
9995 genlmsg_cancel(msg, hdr);
9996 nlmsg_free(msg);
9997}
Johannes Berg947add32013-02-22 22:05:20 +01009998EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009999
Johannes Berg947add32013-02-22 22:05:20 +010010000static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10001 struct net_device *netdev, const u8 *bssid,
10002 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010003{
10004 struct sk_buff *msg;
10005 struct nlattr *rekey_attr;
10006 void *hdr;
10007
Thomas Graf58050fc2012-06-28 03:57:45 +000010008 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010009 if (!msg)
10010 return;
10011
10012 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10013 if (!hdr) {
10014 nlmsg_free(msg);
10015 return;
10016 }
10017
David S. Miller9360ffd2012-03-29 04:41:26 -040010018 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10019 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10020 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10021 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010022
10023 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10024 if (!rekey_attr)
10025 goto nla_put_failure;
10026
David S. Miller9360ffd2012-03-29 04:41:26 -040010027 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10028 NL80211_REPLAY_CTR_LEN, replay_ctr))
10029 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010030
10031 nla_nest_end(msg, rekey_attr);
10032
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010033 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010034
10035 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10036 nl80211_mlme_mcgrp.id, gfp);
10037 return;
10038
10039 nla_put_failure:
10040 genlmsg_cancel(msg, hdr);
10041 nlmsg_free(msg);
10042}
10043
Johannes Berg947add32013-02-22 22:05:20 +010010044void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10045 const u8 *replay_ctr, gfp_t gfp)
10046{
10047 struct wireless_dev *wdev = dev->ieee80211_ptr;
10048 struct wiphy *wiphy = wdev->wiphy;
10049 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10050
10051 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10052 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10053}
10054EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10055
10056static void
10057nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10058 struct net_device *netdev, int index,
10059 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010060{
10061 struct sk_buff *msg;
10062 struct nlattr *attr;
10063 void *hdr;
10064
Thomas Graf58050fc2012-06-28 03:57:45 +000010065 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010066 if (!msg)
10067 return;
10068
10069 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10070 if (!hdr) {
10071 nlmsg_free(msg);
10072 return;
10073 }
10074
David S. Miller9360ffd2012-03-29 04:41:26 -040010075 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10076 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10077 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010078
10079 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10080 if (!attr)
10081 goto nla_put_failure;
10082
David S. Miller9360ffd2012-03-29 04:41:26 -040010083 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10084 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10085 (preauth &&
10086 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10087 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010088
10089 nla_nest_end(msg, attr);
10090
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010091 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010092
10093 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10094 nl80211_mlme_mcgrp.id, gfp);
10095 return;
10096
10097 nla_put_failure:
10098 genlmsg_cancel(msg, hdr);
10099 nlmsg_free(msg);
10100}
10101
Johannes Berg947add32013-02-22 22:05:20 +010010102void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10103 const u8 *bssid, bool preauth, gfp_t gfp)
10104{
10105 struct wireless_dev *wdev = dev->ieee80211_ptr;
10106 struct wiphy *wiphy = wdev->wiphy;
10107 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10108
10109 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10110 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10111}
10112EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10113
10114static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10115 struct net_device *netdev,
10116 struct cfg80211_chan_def *chandef,
10117 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070010118{
10119 struct sk_buff *msg;
10120 void *hdr;
10121
Thomas Graf58050fc2012-06-28 03:57:45 +000010122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070010123 if (!msg)
10124 return;
10125
10126 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10127 if (!hdr) {
10128 nlmsg_free(msg);
10129 return;
10130 }
10131
Johannes Berg683b6d32012-11-08 21:25:48 +010010132 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10133 goto nla_put_failure;
10134
10135 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040010136 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070010137
10138 genlmsg_end(msg, hdr);
10139
10140 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10141 nl80211_mlme_mcgrp.id, gfp);
10142 return;
10143
10144 nla_put_failure:
10145 genlmsg_cancel(msg, hdr);
10146 nlmsg_free(msg);
10147}
10148
Johannes Berg947add32013-02-22 22:05:20 +010010149void cfg80211_ch_switch_notify(struct net_device *dev,
10150 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070010151{
Johannes Berg947add32013-02-22 22:05:20 +010010152 struct wireless_dev *wdev = dev->ieee80211_ptr;
10153 struct wiphy *wiphy = wdev->wiphy;
10154 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10155
10156 trace_cfg80211_ch_switch_notify(dev, chandef);
10157
10158 wdev_lock(wdev);
10159
10160 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10161 wdev->iftype != NL80211_IFTYPE_P2P_GO))
10162 goto out;
10163
10164 wdev->channel = chandef->chan;
10165 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
10166out:
10167 wdev_unlock(wdev);
10168 return;
10169}
10170EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10171
10172void cfg80211_cqm_txe_notify(struct net_device *dev,
10173 const u8 *peer, u32 num_packets,
10174 u32 rate, u32 intvl, gfp_t gfp)
10175{
10176 struct wireless_dev *wdev = dev->ieee80211_ptr;
10177 struct wiphy *wiphy = wdev->wiphy;
10178 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010179 struct sk_buff *msg;
10180 struct nlattr *pinfoattr;
10181 void *hdr;
10182
10183 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10184 if (!msg)
10185 return;
10186
10187 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10188 if (!hdr) {
10189 nlmsg_free(msg);
10190 return;
10191 }
10192
10193 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010194 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070010195 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10196 goto nla_put_failure;
10197
10198 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10199 if (!pinfoattr)
10200 goto nla_put_failure;
10201
10202 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10203 goto nla_put_failure;
10204
10205 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10206 goto nla_put_failure;
10207
10208 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10209 goto nla_put_failure;
10210
10211 nla_nest_end(msg, pinfoattr);
10212
10213 genlmsg_end(msg, hdr);
10214
10215 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10216 nl80211_mlme_mcgrp.id, gfp);
10217 return;
10218
10219 nla_put_failure:
10220 genlmsg_cancel(msg, hdr);
10221 nlmsg_free(msg);
10222}
Johannes Berg947add32013-02-22 22:05:20 +010010223EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010224
10225void
Simon Wunderlich04f39042013-02-08 18:16:19 +010010226nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10227 struct cfg80211_chan_def *chandef,
10228 enum nl80211_radar_event event,
10229 struct net_device *netdev, gfp_t gfp)
10230{
10231 struct sk_buff *msg;
10232 void *hdr;
10233
10234 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10235 if (!msg)
10236 return;
10237
10238 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
10239 if (!hdr) {
10240 nlmsg_free(msg);
10241 return;
10242 }
10243
10244 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10245 goto nla_put_failure;
10246
10247 /* NOP and radar events don't need a netdev parameter */
10248 if (netdev) {
10249 struct wireless_dev *wdev = netdev->ieee80211_ptr;
10250
10251 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10252 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10253 goto nla_put_failure;
10254 }
10255
10256 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
10257 goto nla_put_failure;
10258
10259 if (nl80211_send_chandef(msg, chandef))
10260 goto nla_put_failure;
10261
10262 if (genlmsg_end(msg, hdr) < 0) {
10263 nlmsg_free(msg);
10264 return;
10265 }
10266
10267 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10268 nl80211_mlme_mcgrp.id, gfp);
10269 return;
10270
10271 nla_put_failure:
10272 genlmsg_cancel(msg, hdr);
10273 nlmsg_free(msg);
10274}
10275
Johannes Berg947add32013-02-22 22:05:20 +010010276void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10277 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010010278{
Johannes Berg947add32013-02-22 22:05:20 +010010279 struct wireless_dev *wdev = dev->ieee80211_ptr;
10280 struct wiphy *wiphy = wdev->wiphy;
10281 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010282 struct sk_buff *msg;
10283 struct nlattr *pinfoattr;
10284 void *hdr;
10285
Johannes Berg947add32013-02-22 22:05:20 +010010286 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
10287
Thomas Graf58050fc2012-06-28 03:57:45 +000010288 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010289 if (!msg)
10290 return;
10291
10292 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10293 if (!hdr) {
10294 nlmsg_free(msg);
10295 return;
10296 }
10297
David S. Miller9360ffd2012-03-29 04:41:26 -040010298 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010299 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010300 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10301 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010302
10303 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10304 if (!pinfoattr)
10305 goto nla_put_failure;
10306
David S. Miller9360ffd2012-03-29 04:41:26 -040010307 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10308 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010309
10310 nla_nest_end(msg, pinfoattr);
10311
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010312 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010313
10314 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10315 nl80211_mlme_mcgrp.id, gfp);
10316 return;
10317
10318 nla_put_failure:
10319 genlmsg_cancel(msg, hdr);
10320 nlmsg_free(msg);
10321}
Johannes Berg947add32013-02-22 22:05:20 +010010322EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010323
Johannes Berg7f6cf312011-11-04 11:18:15 +010010324void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10325 u64 cookie, bool acked, gfp_t gfp)
10326{
10327 struct wireless_dev *wdev = dev->ieee80211_ptr;
10328 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10329 struct sk_buff *msg;
10330 void *hdr;
10331 int err;
10332
Beni Lev4ee3e062012-08-27 12:49:39 +030010333 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10334
Thomas Graf58050fc2012-06-28 03:57:45 +000010335 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030010336
Johannes Berg7f6cf312011-11-04 11:18:15 +010010337 if (!msg)
10338 return;
10339
10340 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10341 if (!hdr) {
10342 nlmsg_free(msg);
10343 return;
10344 }
10345
David S. Miller9360ffd2012-03-29 04:41:26 -040010346 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10347 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10348 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10349 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10350 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10351 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010352
10353 err = genlmsg_end(msg, hdr);
10354 if (err < 0) {
10355 nlmsg_free(msg);
10356 return;
10357 }
10358
10359 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10360 nl80211_mlme_mcgrp.id, gfp);
10361 return;
10362
10363 nla_put_failure:
10364 genlmsg_cancel(msg, hdr);
10365 nlmsg_free(msg);
10366}
10367EXPORT_SYMBOL(cfg80211_probe_status);
10368
Johannes Berg5e760232011-11-04 11:18:17 +010010369void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10370 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070010371 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010010372{
10373 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10374 struct sk_buff *msg;
10375 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070010376 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010010377
Beni Lev4ee3e062012-08-27 12:49:39 +030010378 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10379
Ben Greear37c73b52012-10-26 14:49:25 -070010380 spin_lock_bh(&rdev->beacon_registrations_lock);
10381 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10382 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10383 if (!msg) {
10384 spin_unlock_bh(&rdev->beacon_registrations_lock);
10385 return;
10386 }
Johannes Berg5e760232011-11-04 11:18:17 +010010387
Ben Greear37c73b52012-10-26 14:49:25 -070010388 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10389 if (!hdr)
10390 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010010391
Ben Greear37c73b52012-10-26 14:49:25 -070010392 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10393 (freq &&
10394 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10395 (sig_dbm &&
10396 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10397 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10398 goto nla_put_failure;
10399
10400 genlmsg_end(msg, hdr);
10401
10402 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010010403 }
Ben Greear37c73b52012-10-26 14:49:25 -070010404 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010405 return;
10406
10407 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070010408 spin_unlock_bh(&rdev->beacon_registrations_lock);
10409 if (hdr)
10410 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010010411 nlmsg_free(msg);
10412}
10413EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10414
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010415#ifdef CONFIG_PM
10416void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10417 struct cfg80211_wowlan_wakeup *wakeup,
10418 gfp_t gfp)
10419{
10420 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10421 struct sk_buff *msg;
10422 void *hdr;
10423 int err, size = 200;
10424
10425 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10426
10427 if (wakeup)
10428 size += wakeup->packet_present_len;
10429
10430 msg = nlmsg_new(size, gfp);
10431 if (!msg)
10432 return;
10433
10434 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10435 if (!hdr)
10436 goto free_msg;
10437
10438 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10439 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10440 goto free_msg;
10441
10442 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10443 wdev->netdev->ifindex))
10444 goto free_msg;
10445
10446 if (wakeup) {
10447 struct nlattr *reasons;
10448
10449 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10450
10451 if (wakeup->disconnect &&
10452 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10453 goto free_msg;
10454 if (wakeup->magic_pkt &&
10455 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10456 goto free_msg;
10457 if (wakeup->gtk_rekey_failure &&
10458 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10459 goto free_msg;
10460 if (wakeup->eap_identity_req &&
10461 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10462 goto free_msg;
10463 if (wakeup->four_way_handshake &&
10464 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10465 goto free_msg;
10466 if (wakeup->rfkill_release &&
10467 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10468 goto free_msg;
10469
10470 if (wakeup->pattern_idx >= 0 &&
10471 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10472 wakeup->pattern_idx))
10473 goto free_msg;
10474
Johannes Berg2a0e0472013-01-23 22:57:40 +010010475 if (wakeup->tcp_match)
10476 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10477
10478 if (wakeup->tcp_connlost)
10479 nla_put_flag(msg,
10480 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10481
10482 if (wakeup->tcp_nomoretokens)
10483 nla_put_flag(msg,
10484 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10485
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010486 if (wakeup->packet) {
10487 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10488 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10489
10490 if (!wakeup->packet_80211) {
10491 pkt_attr =
10492 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10493 len_attr =
10494 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10495 }
10496
10497 if (wakeup->packet_len &&
10498 nla_put_u32(msg, len_attr, wakeup->packet_len))
10499 goto free_msg;
10500
10501 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10502 wakeup->packet))
10503 goto free_msg;
10504 }
10505
10506 nla_nest_end(msg, reasons);
10507 }
10508
10509 err = genlmsg_end(msg, hdr);
10510 if (err < 0)
10511 goto free_msg;
10512
10513 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10514 nl80211_mlme_mcgrp.id, gfp);
10515 return;
10516
10517 free_msg:
10518 nlmsg_free(msg);
10519}
10520EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10521#endif
10522
Jouni Malinen3475b092012-11-16 22:49:57 +020010523void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10524 enum nl80211_tdls_operation oper,
10525 u16 reason_code, gfp_t gfp)
10526{
10527 struct wireless_dev *wdev = dev->ieee80211_ptr;
10528 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10529 struct sk_buff *msg;
10530 void *hdr;
10531 int err;
10532
10533 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10534 reason_code);
10535
10536 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10537 if (!msg)
10538 return;
10539
10540 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10541 if (!hdr) {
10542 nlmsg_free(msg);
10543 return;
10544 }
10545
10546 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10547 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10548 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10549 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10550 (reason_code > 0 &&
10551 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10552 goto nla_put_failure;
10553
10554 err = genlmsg_end(msg, hdr);
10555 if (err < 0) {
10556 nlmsg_free(msg);
10557 return;
10558 }
10559
10560 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10561 nl80211_mlme_mcgrp.id, gfp);
10562 return;
10563
10564 nla_put_failure:
10565 genlmsg_cancel(msg, hdr);
10566 nlmsg_free(msg);
10567}
10568EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10569
Jouni Malinen026331c2010-02-15 12:53:10 +020010570static int nl80211_netlink_notify(struct notifier_block * nb,
10571 unsigned long state,
10572 void *_notify)
10573{
10574 struct netlink_notify *notify = _notify;
10575 struct cfg80211_registered_device *rdev;
10576 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010577 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010578
10579 if (state != NETLINK_URELEASE)
10580 return NOTIFY_DONE;
10581
10582 rcu_read_lock();
10583
Johannes Berg5e760232011-11-04 11:18:17 +010010584 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010585 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010586 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010587
10588 spin_lock_bh(&rdev->beacon_registrations_lock);
10589 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10590 list) {
10591 if (reg->nlportid == notify->portid) {
10592 list_del(&reg->list);
10593 kfree(reg);
10594 break;
10595 }
10596 }
10597 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010598 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010599
10600 rcu_read_unlock();
10601
10602 return NOTIFY_DONE;
10603}
10604
10605static struct notifier_block nl80211_netlink_notifier = {
10606 .notifier_call = nl80211_netlink_notify,
10607};
10608
Jouni Malinen355199e2013-02-27 17:14:27 +020010609void cfg80211_ft_event(struct net_device *netdev,
10610 struct cfg80211_ft_event_params *ft_event)
10611{
10612 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
10613 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10614 struct sk_buff *msg;
10615 void *hdr;
10616 int err;
10617
10618 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
10619
10620 if (!ft_event->target_ap)
10621 return;
10622
10623 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10624 if (!msg)
10625 return;
10626
10627 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
10628 if (!hdr) {
10629 nlmsg_free(msg);
10630 return;
10631 }
10632
10633 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
10634 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
10635 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
10636 if (ft_event->ies)
10637 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
10638 if (ft_event->ric_ies)
10639 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
10640 ft_event->ric_ies);
10641
10642 err = genlmsg_end(msg, hdr);
10643 if (err < 0) {
10644 nlmsg_free(msg);
10645 return;
10646 }
10647
10648 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10649 nl80211_mlme_mcgrp.id, GFP_KERNEL);
10650}
10651EXPORT_SYMBOL(cfg80211_ft_event);
10652
Johannes Berg55682962007-09-20 13:09:35 -040010653/* initialisation/exit functions */
10654
10655int nl80211_init(void)
10656{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010657 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010658
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010659 err = genl_register_family_with_ops(&nl80211_fam,
10660 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010661 if (err)
10662 return err;
10663
Johannes Berg55682962007-09-20 13:09:35 -040010664 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10665 if (err)
10666 goto err_out;
10667
Johannes Berg2a519312009-02-10 21:25:55 +010010668 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10669 if (err)
10670 goto err_out;
10671
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010672 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10673 if (err)
10674 goto err_out;
10675
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010676 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10677 if (err)
10678 goto err_out;
10679
Johannes Bergaff89a92009-07-01 21:26:51 +020010680#ifdef CONFIG_NL80211_TESTMODE
10681 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10682 if (err)
10683 goto err_out;
10684#endif
10685
Jouni Malinen026331c2010-02-15 12:53:10 +020010686 err = netlink_register_notifier(&nl80211_netlink_notifier);
10687 if (err)
10688 goto err_out;
10689
Johannes Berg55682962007-09-20 13:09:35 -040010690 return 0;
10691 err_out:
10692 genl_unregister_family(&nl80211_fam);
10693 return err;
10694}
10695
10696void nl80211_exit(void)
10697{
Jouni Malinen026331c2010-02-15 12:53:10 +020010698 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010699 genl_unregister_family(&nl80211_fam);
10700}