blob: 580ffeaef3d5a2b5665c3491c0282ce571a4ab35 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc902008-08-25 11:58:58 +0300283
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700286
Johannes Berg6c739412011-11-03 09:27:01 +0100287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300301 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300302 [NL80211_ATTR_STA_FLAGS2] = {
303 .len = sizeof(struct nl80211_sta_flag_update),
304 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300305 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300306 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
307 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200308 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
309 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
310 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200311 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100312 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100313 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
314 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100315 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
316 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200317 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200318 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
319 .len = IEEE80211_MAX_DATA_LEN },
320 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200321 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200322 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300323 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200324 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300325 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
326 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200327 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900328 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
329 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100330 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100331 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100332 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200333 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700334 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300335 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200336 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200337 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300338 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300339 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
340 .len = IEEE80211_MAX_DATA_LEN },
341 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530343 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300344 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530345 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300346 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
349 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100351 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200352 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
353 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700354 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800355 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
356 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
357 .len = NL80211_HT_CAPABILITY_LEN
358 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100359 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530360 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530361 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200362 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700363 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300364 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000365 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700366 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100367 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
368 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530369 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
370 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200371 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
372 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg55682962007-09-20 13:09:35 -0400373};
374
Johannes Berge31b8212010-10-05 19:39:30 +0200375/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000376static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200377 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200378 [NL80211_KEY_IDX] = { .type = NLA_U8 },
379 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200380 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200381 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
382 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200383 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100384 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
385};
386
387/* policy for the key default flags */
388static const struct nla_policy
389nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
390 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
391 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200392};
393
Johannes Bergff1b6e62011-05-04 15:37:28 +0200394/* policy for WoWLAN attributes */
395static const struct nla_policy
396nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
397 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
398 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
400 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200401 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
402 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
403 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
404 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100405 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
406};
407
408static const struct nla_policy
409nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
410 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
411 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
412 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
413 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
414 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
415 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
416 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
417 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
418 },
419 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
420 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
421 },
422 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
423 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
424 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200425};
426
Johannes Berge5497d72011-07-05 16:35:40 +0200427/* policy for GTK rekey offload attributes */
428static const struct nla_policy
429nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
430 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
431 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
432 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
433};
434
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300435static const struct nla_policy
436nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200437 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300438 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700439 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300440};
441
Holger Schuriga0438972009-11-11 11:30:02 +0100442/* ifidx get helper */
443static int nl80211_get_ifidx(struct netlink_callback *cb)
444{
445 int res;
446
447 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
448 nl80211_fam.attrbuf, nl80211_fam.maxattr,
449 nl80211_policy);
450 if (res)
451 return res;
452
453 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
454 return -EINVAL;
455
456 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
457 if (!res)
458 return -EINVAL;
459 return res;
460}
461
Johannes Berg67748892010-10-04 21:14:06 +0200462static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
463 struct netlink_callback *cb,
464 struct cfg80211_registered_device **rdev,
465 struct net_device **dev)
466{
467 int ifidx = cb->args[0];
468 int err;
469
470 if (!ifidx)
471 ifidx = nl80211_get_ifidx(cb);
472 if (ifidx < 0)
473 return ifidx;
474
475 cb->args[0] = ifidx;
476
477 rtnl_lock();
478
479 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
480 if (!*dev) {
481 err = -ENODEV;
482 goto out_rtnl;
483 }
484
485 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100486 if (IS_ERR(*rdev)) {
487 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200488 goto out_rtnl;
489 }
490
491 return 0;
492 out_rtnl:
493 rtnl_unlock();
494 return err;
495}
496
497static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
498{
499 cfg80211_unlock_rdev(rdev);
500 rtnl_unlock();
501}
502
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100503/* IE validation */
504static bool is_valid_ie_attr(const struct nlattr *attr)
505{
506 const u8 *pos;
507 int len;
508
509 if (!attr)
510 return true;
511
512 pos = nla_data(attr);
513 len = nla_len(attr);
514
515 while (len) {
516 u8 elemlen;
517
518 if (len < 2)
519 return false;
520 len -= 2;
521
522 elemlen = pos[1];
523 if (elemlen > len)
524 return false;
525
526 len -= elemlen;
527 pos += 2 + elemlen;
528 }
529
530 return true;
531}
532
Johannes Berg55682962007-09-20 13:09:35 -0400533/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000534static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400535 int flags, u8 cmd)
536{
537 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000538 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400539}
540
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400541static int nl80211_msg_put_channel(struct sk_buff *msg,
542 struct ieee80211_channel *chan)
543{
David S. Miller9360ffd2012-03-29 04:41:26 -0400544 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
545 chan->center_freq))
546 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400547
David S. Miller9360ffd2012-03-29 04:41:26 -0400548 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
549 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
550 goto nla_put_failure;
551 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
552 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
553 goto nla_put_failure;
554 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
555 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
556 goto nla_put_failure;
Simon Wunderlich04f39042013-02-08 18:16:19 +0100557 if (chan->flags & IEEE80211_CHAN_RADAR) {
558 u32 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
559 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
560 goto nla_put_failure;
561 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
562 chan->dfs_state))
563 goto nla_put_failure;
564 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME, time))
565 goto nla_put_failure;
566 }
Johannes Berg50640f12012-12-12 17:59:39 +0100567 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
568 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
569 goto nla_put_failure;
570 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
571 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
572 goto nla_put_failure;
573 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
574 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
575 goto nla_put_failure;
576 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
577 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
578 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400579
David S. Miller9360ffd2012-03-29 04:41:26 -0400580 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
581 DBM_TO_MBM(chan->max_power)))
582 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400583
584 return 0;
585
586 nla_put_failure:
587 return -ENOBUFS;
588}
589
Johannes Berg55682962007-09-20 13:09:35 -0400590/* netlink command implementations */
591
Johannes Bergb9454e82009-07-08 13:29:08 +0200592struct key_parse {
593 struct key_params p;
594 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200595 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200596 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100597 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200598};
599
600static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
601{
602 struct nlattr *tb[NL80211_KEY_MAX + 1];
603 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
604 nl80211_key_policy);
605 if (err)
606 return err;
607
608 k->def = !!tb[NL80211_KEY_DEFAULT];
609 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
610
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100611 if (k->def) {
612 k->def_uni = true;
613 k->def_multi = true;
614 }
615 if (k->defmgmt)
616 k->def_multi = true;
617
Johannes Bergb9454e82009-07-08 13:29:08 +0200618 if (tb[NL80211_KEY_IDX])
619 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
620
621 if (tb[NL80211_KEY_DATA]) {
622 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
623 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
624 }
625
626 if (tb[NL80211_KEY_SEQ]) {
627 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
628 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
629 }
630
631 if (tb[NL80211_KEY_CIPHER])
632 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
633
Johannes Berge31b8212010-10-05 19:39:30 +0200634 if (tb[NL80211_KEY_TYPE]) {
635 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
636 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
637 return -EINVAL;
638 }
639
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100640 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
641 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100642 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
643 tb[NL80211_KEY_DEFAULT_TYPES],
644 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100645 if (err)
646 return err;
647
648 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
649 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
650 }
651
Johannes Bergb9454e82009-07-08 13:29:08 +0200652 return 0;
653}
654
655static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
656{
657 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
658 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
659 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
660 }
661
662 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
663 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
664 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
665 }
666
667 if (info->attrs[NL80211_ATTR_KEY_IDX])
668 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
669
670 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
671 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
672
673 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
674 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
675
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100676 if (k->def) {
677 k->def_uni = true;
678 k->def_multi = true;
679 }
680 if (k->defmgmt)
681 k->def_multi = true;
682
Johannes Berge31b8212010-10-05 19:39:30 +0200683 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
684 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
685 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
686 return -EINVAL;
687 }
688
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100689 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
690 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
691 int err = nla_parse_nested(
692 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
693 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
694 nl80211_key_default_policy);
695 if (err)
696 return err;
697
698 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
699 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
700 }
701
Johannes Bergb9454e82009-07-08 13:29:08 +0200702 return 0;
703}
704
705static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
706{
707 int err;
708
709 memset(k, 0, sizeof(*k));
710 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200711 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200712
713 if (info->attrs[NL80211_ATTR_KEY])
714 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
715 else
716 err = nl80211_parse_key_old(info, k);
717
718 if (err)
719 return err;
720
721 if (k->def && k->defmgmt)
722 return -EINVAL;
723
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100724 if (k->defmgmt) {
725 if (k->def_uni || !k->def_multi)
726 return -EINVAL;
727 }
728
Johannes Bergb9454e82009-07-08 13:29:08 +0200729 if (k->idx != -1) {
730 if (k->defmgmt) {
731 if (k->idx < 4 || k->idx > 5)
732 return -EINVAL;
733 } else if (k->def) {
734 if (k->idx < 0 || k->idx > 3)
735 return -EINVAL;
736 } else {
737 if (k->idx < 0 || k->idx > 5)
738 return -EINVAL;
739 }
740 }
741
742 return 0;
743}
744
Johannes Bergfffd0932009-07-08 14:22:54 +0200745static struct cfg80211_cached_keys *
746nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530747 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200748{
749 struct key_parse parse;
750 struct nlattr *key;
751 struct cfg80211_cached_keys *result;
752 int rem, err, def = 0;
753
754 result = kzalloc(sizeof(*result), GFP_KERNEL);
755 if (!result)
756 return ERR_PTR(-ENOMEM);
757
758 result->def = -1;
759 result->defmgmt = -1;
760
761 nla_for_each_nested(key, keys, rem) {
762 memset(&parse, 0, sizeof(parse));
763 parse.idx = -1;
764
765 err = nl80211_parse_key_new(key, &parse);
766 if (err)
767 goto error;
768 err = -EINVAL;
769 if (!parse.p.key)
770 goto error;
771 if (parse.idx < 0 || parse.idx > 4)
772 goto error;
773 if (parse.def) {
774 if (def)
775 goto error;
776 def = 1;
777 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100778 if (!parse.def_uni || !parse.def_multi)
779 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200780 } else if (parse.defmgmt)
781 goto error;
782 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200783 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200784 if (err)
785 goto error;
786 result->params[parse.idx].cipher = parse.p.cipher;
787 result->params[parse.idx].key_len = parse.p.key_len;
788 result->params[parse.idx].key = result->data[parse.idx];
789 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530790
791 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
792 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
793 if (no_ht)
794 *no_ht = true;
795 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200796 }
797
798 return result;
799 error:
800 kfree(result);
801 return ERR_PTR(err);
802}
803
804static int nl80211_key_allowed(struct wireless_dev *wdev)
805{
806 ASSERT_WDEV_LOCK(wdev);
807
Johannes Bergfffd0932009-07-08 14:22:54 +0200808 switch (wdev->iftype) {
809 case NL80211_IFTYPE_AP:
810 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200811 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700812 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200813 break;
814 case NL80211_IFTYPE_ADHOC:
815 if (!wdev->current_bss)
816 return -ENOLINK;
817 break;
818 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200819 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200820 if (wdev->sme_state != CFG80211_SME_CONNECTED)
821 return -ENOLINK;
822 break;
823 default:
824 return -EINVAL;
825 }
826
827 return 0;
828}
829
Johannes Berg7527a782011-05-13 10:58:57 +0200830static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
831{
832 struct nlattr *nl_modes = nla_nest_start(msg, attr);
833 int i;
834
835 if (!nl_modes)
836 goto nla_put_failure;
837
838 i = 0;
839 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400840 if ((ifmodes & 1) && nla_put_flag(msg, i))
841 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200842 ifmodes >>= 1;
843 i++;
844 }
845
846 nla_nest_end(msg, nl_modes);
847 return 0;
848
849nla_put_failure:
850 return -ENOBUFS;
851}
852
853static int nl80211_put_iface_combinations(struct wiphy *wiphy,
854 struct sk_buff *msg)
855{
856 struct nlattr *nl_combis;
857 int i, j;
858
859 nl_combis = nla_nest_start(msg,
860 NL80211_ATTR_INTERFACE_COMBINATIONS);
861 if (!nl_combis)
862 goto nla_put_failure;
863
864 for (i = 0; i < wiphy->n_iface_combinations; i++) {
865 const struct ieee80211_iface_combination *c;
866 struct nlattr *nl_combi, *nl_limits;
867
868 c = &wiphy->iface_combinations[i];
869
870 nl_combi = nla_nest_start(msg, i + 1);
871 if (!nl_combi)
872 goto nla_put_failure;
873
874 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
875 if (!nl_limits)
876 goto nla_put_failure;
877
878 for (j = 0; j < c->n_limits; j++) {
879 struct nlattr *nl_limit;
880
881 nl_limit = nla_nest_start(msg, j + 1);
882 if (!nl_limit)
883 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400884 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
885 c->limits[j].max))
886 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200887 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
888 c->limits[j].types))
889 goto nla_put_failure;
890 nla_nest_end(msg, nl_limit);
891 }
892
893 nla_nest_end(msg, nl_limits);
894
David S. Miller9360ffd2012-03-29 04:41:26 -0400895 if (c->beacon_int_infra_match &&
896 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
897 goto nla_put_failure;
898 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
899 c->num_different_channels) ||
900 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
901 c->max_interfaces))
902 goto nla_put_failure;
Simon Wunderlich11c4a072013-01-08 14:04:07 +0100903 if (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
904 c->radar_detect_widths))
905 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200906
907 nla_nest_end(msg, nl_combi);
908 }
909
910 nla_nest_end(msg, nl_combis);
911
912 return 0;
913nla_put_failure:
914 return -ENOBUFS;
915}
916
Johannes Berg2a0e0472013-01-23 22:57:40 +0100917#ifdef CONFIG_PM
918static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
919 struct sk_buff *msg)
920{
921 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
922 struct nlattr *nl_tcp;
923
924 if (!tcp)
925 return 0;
926
927 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
928 if (!nl_tcp)
929 return -ENOBUFS;
930
931 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
932 tcp->data_payload_max))
933 return -ENOBUFS;
934
935 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
936 tcp->data_payload_max))
937 return -ENOBUFS;
938
939 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
940 return -ENOBUFS;
941
942 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
943 sizeof(*tcp->tok), tcp->tok))
944 return -ENOBUFS;
945
946 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
947 tcp->data_interval_max))
948 return -ENOBUFS;
949
950 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
951 tcp->wake_payload_max))
952 return -ENOBUFS;
953
954 nla_nest_end(msg, nl_tcp);
955 return 0;
956}
957#endif
958
Eric W. Biederman15e47302012-09-07 20:12:54 +0000959static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400960 struct cfg80211_registered_device *dev)
961{
962 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100963 struct nlattr *nl_bands, *nl_band;
964 struct nlattr *nl_freqs, *nl_freq;
965 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100966 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100967 enum ieee80211_band band;
968 struct ieee80211_channel *chan;
969 struct ieee80211_rate *rate;
970 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200971 const struct ieee80211_txrx_stypes *mgmt_stypes =
972 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400973
Eric W. Biederman15e47302012-09-07 20:12:54 +0000974 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400975 if (!hdr)
976 return -1;
977
David S. Miller9360ffd2012-03-29 04:41:26 -0400978 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
979 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
980 nla_put_u32(msg, NL80211_ATTR_GENERATION,
981 cfg80211_rdev_list_generation) ||
982 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
983 dev->wiphy.retry_short) ||
984 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
985 dev->wiphy.retry_long) ||
986 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
987 dev->wiphy.frag_threshold) ||
988 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
989 dev->wiphy.rts_threshold) ||
990 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
991 dev->wiphy.coverage_class) ||
992 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
993 dev->wiphy.max_scan_ssids) ||
994 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
995 dev->wiphy.max_sched_scan_ssids) ||
996 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
997 dev->wiphy.max_scan_ie_len) ||
998 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
999 dev->wiphy.max_sched_scan_ie_len) ||
1000 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1001 dev->wiphy.max_match_sets))
1002 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001003
David S. Miller9360ffd2012-03-29 04:41:26 -04001004 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1005 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1006 goto nla_put_failure;
1007 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1008 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1009 goto nla_put_failure;
1010 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1011 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1012 goto nla_put_failure;
1013 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1014 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1015 goto nla_put_failure;
1016 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1017 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1018 goto nla_put_failure;
1019 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1020 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1021 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001022
David S. Miller9360ffd2012-03-29 04:41:26 -04001023 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1024 sizeof(u32) * dev->wiphy.n_cipher_suites,
1025 dev->wiphy.cipher_suites))
1026 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001027
David S. Miller9360ffd2012-03-29 04:41:26 -04001028 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1029 dev->wiphy.max_num_pmkids))
1030 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +05301031
David S. Miller9360ffd2012-03-29 04:41:26 -04001032 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1033 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1034 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +02001035
David S. Miller9360ffd2012-03-29 04:41:26 -04001036 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1037 dev->wiphy.available_antennas_tx) ||
1038 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1039 dev->wiphy.available_antennas_rx))
1040 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001041
David S. Miller9360ffd2012-03-29 04:41:26 -04001042 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1043 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1044 dev->wiphy.probe_resp_offload))
1045 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +02001046
Bruno Randolf7f531e02010-12-16 11:30:22 +09001047 if ((dev->wiphy.available_antennas_tx ||
1048 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001049 u32 tx_ant = 0, rx_ant = 0;
1050 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +03001051 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001052 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001053 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
1054 tx_ant) ||
1055 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
1056 rx_ant))
1057 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001058 }
1059 }
1060
Johannes Berg7527a782011-05-13 10:58:57 +02001061 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1062 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001063 goto nla_put_failure;
1064
Johannes Bergee688b002008-01-24 19:38:39 +01001065 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1066 if (!nl_bands)
1067 goto nla_put_failure;
1068
1069 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1070 if (!dev->wiphy.bands[band])
1071 continue;
1072
1073 nl_band = nla_nest_start(msg, band);
1074 if (!nl_band)
1075 goto nla_put_failure;
1076
Johannes Bergd51626d2008-10-09 12:20:13 +02001077 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -04001078 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
1079 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1080 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
1081 &dev->wiphy.bands[band]->ht_cap.mcs) ||
1082 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1083 dev->wiphy.bands[band]->ht_cap.cap) ||
1084 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1085 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
1086 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1087 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
1088 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001089
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001090 /* add VHT info */
1091 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1092 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1093 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1094 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1095 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1096 dev->wiphy.bands[band]->vht_cap.cap)))
1097 goto nla_put_failure;
1098
Johannes Bergee688b002008-01-24 19:38:39 +01001099 /* add frequencies */
1100 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1101 if (!nl_freqs)
1102 goto nla_put_failure;
1103
1104 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1105 nl_freq = nla_nest_start(msg, i);
1106 if (!nl_freq)
1107 goto nla_put_failure;
1108
1109 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001110
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001111 if (nl80211_msg_put_channel(msg, chan))
1112 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001113
Johannes Bergee688b002008-01-24 19:38:39 +01001114 nla_nest_end(msg, nl_freq);
1115 }
1116
1117 nla_nest_end(msg, nl_freqs);
1118
1119 /* add bitrates */
1120 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1121 if (!nl_rates)
1122 goto nla_put_failure;
1123
1124 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1125 nl_rate = nla_nest_start(msg, i);
1126 if (!nl_rate)
1127 goto nla_put_failure;
1128
1129 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001130 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1131 rate->bitrate))
1132 goto nla_put_failure;
1133 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1134 nla_put_flag(msg,
1135 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1136 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001137
1138 nla_nest_end(msg, nl_rate);
1139 }
1140
1141 nla_nest_end(msg, nl_rates);
1142
1143 nla_nest_end(msg, nl_band);
1144 }
1145 nla_nest_end(msg, nl_bands);
1146
Johannes Berg8fdc6212009-03-14 09:34:01 +01001147 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1148 if (!nl_cmds)
1149 goto nla_put_failure;
1150
1151 i = 0;
1152#define CMD(op, n) \
1153 do { \
1154 if (dev->ops->op) { \
1155 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001156 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1157 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001158 } \
1159 } while (0)
1160
1161 CMD(add_virtual_intf, NEW_INTERFACE);
1162 CMD(change_virtual_intf, SET_INTERFACE);
1163 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001164 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001165 CMD(add_station, NEW_STATION);
1166 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001167 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001168 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001169 CMD(auth, AUTHENTICATE);
1170 CMD(assoc, ASSOCIATE);
1171 CMD(deauth, DEAUTHENTICATE);
1172 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001173 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001174 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001175 CMD(set_pmksa, SET_PMKSA);
1176 CMD(del_pmksa, DEL_PMKSA);
1177 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001178 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1179 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001180 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001181 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001182 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001183 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001184 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001185 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1186 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001187 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001188 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001189 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001190 i++;
1191 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1192 goto nla_put_failure;
1193 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001194 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001195 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1196 CMD(tdls_mgmt, TDLS_MGMT);
1197 CMD(tdls_oper, TDLS_OPER);
1198 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001199 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1200 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001201 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001202 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e7602302011-11-04 11:18:17 +01001203 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1204 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001205 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1206 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01001207 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001208 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001209 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001210
Kalle Valo4745fc02011-11-17 19:06:10 +02001211#ifdef CONFIG_NL80211_TESTMODE
1212 CMD(testmode_cmd, TESTMODE);
1213#endif
1214
Johannes Berg8fdc6212009-03-14 09:34:01 +01001215#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001216
Johannes Berg6829c872009-07-02 09:13:27 +02001217 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001218 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001219 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1220 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001221 }
1222
Johannes Berg6829c872009-07-02 09:13:27 +02001223 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001224 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001225 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1226 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001227 }
1228
Johannes Berg8fdc6212009-03-14 09:34:01 +01001229 nla_nest_end(msg, nl_cmds);
1230
Johannes Berg7c4ef712011-11-18 15:33:48 +01001231 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001232 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1233 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1234 dev->wiphy.max_remain_on_channel_duration))
1235 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001236
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1238 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1239 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001240
Johannes Berg2e161f72010-08-12 15:38:38 +02001241 if (mgmt_stypes) {
1242 u16 stypes;
1243 struct nlattr *nl_ftypes, *nl_ifs;
1244 enum nl80211_iftype ift;
1245
1246 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1247 if (!nl_ifs)
1248 goto nla_put_failure;
1249
1250 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1251 nl_ftypes = nla_nest_start(msg, ift);
1252 if (!nl_ftypes)
1253 goto nla_put_failure;
1254 i = 0;
1255 stypes = mgmt_stypes[ift].tx;
1256 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001257 if ((stypes & 1) &&
1258 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1259 (i << 4) | IEEE80211_FTYPE_MGMT))
1260 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001261 stypes >>= 1;
1262 i++;
1263 }
1264 nla_nest_end(msg, nl_ftypes);
1265 }
1266
Johannes Berg74b70a42010-08-24 12:15:53 +02001267 nla_nest_end(msg, nl_ifs);
1268
Johannes Berg2e161f72010-08-12 15:38:38 +02001269 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1270 if (!nl_ifs)
1271 goto nla_put_failure;
1272
1273 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1274 nl_ftypes = nla_nest_start(msg, ift);
1275 if (!nl_ftypes)
1276 goto nla_put_failure;
1277 i = 0;
1278 stypes = mgmt_stypes[ift].rx;
1279 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001280 if ((stypes & 1) &&
1281 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1282 (i << 4) | IEEE80211_FTYPE_MGMT))
1283 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001284 stypes >>= 1;
1285 i++;
1286 }
1287 nla_nest_end(msg, nl_ftypes);
1288 }
1289 nla_nest_end(msg, nl_ifs);
1290 }
1291
Johannes Bergdfb89c52012-06-27 09:23:48 +02001292#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001293 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1294 struct nlattr *nl_wowlan;
1295
1296 nl_wowlan = nla_nest_start(msg,
1297 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1298 if (!nl_wowlan)
1299 goto nla_put_failure;
1300
David S. Miller9360ffd2012-03-29 04:41:26 -04001301 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1302 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1303 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1304 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1305 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1306 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1307 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1308 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1309 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1310 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1311 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1312 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1313 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1314 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1315 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1316 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1317 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001318 if (dev->wiphy.wowlan.n_patterns) {
1319 struct nl80211_wowlan_pattern_support pat = {
1320 .max_patterns = dev->wiphy.wowlan.n_patterns,
1321 .min_pattern_len =
1322 dev->wiphy.wowlan.pattern_min_len,
1323 .max_pattern_len =
1324 dev->wiphy.wowlan.pattern_max_len,
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08001325 .max_pkt_offset =
1326 dev->wiphy.wowlan.max_pkt_offset,
Johannes Bergff1b6e62011-05-04 15:37:28 +02001327 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001328 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1329 sizeof(pat), &pat))
1330 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001331 }
1332
Johannes Berg2a0e0472013-01-23 22:57:40 +01001333 if (nl80211_send_wowlan_tcp_caps(dev, msg))
1334 goto nla_put_failure;
1335
Johannes Bergff1b6e62011-05-04 15:37:28 +02001336 nla_nest_end(msg, nl_wowlan);
1337 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001338#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001339
Johannes Berg7527a782011-05-13 10:58:57 +02001340 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1341 dev->wiphy.software_iftypes))
1342 goto nla_put_failure;
1343
1344 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1345 goto nla_put_failure;
1346
David S. Miller9360ffd2012-03-29 04:41:26 -04001347 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1348 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1349 dev->wiphy.ap_sme_capa))
1350 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001351
David S. Miller9360ffd2012-03-29 04:41:26 -04001352 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1353 dev->wiphy.features))
1354 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001355
David S. Miller9360ffd2012-03-29 04:41:26 -04001356 if (dev->wiphy.ht_capa_mod_mask &&
1357 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1358 sizeof(*dev->wiphy.ht_capa_mod_mask),
1359 dev->wiphy.ht_capa_mod_mask))
1360 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001361
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05301362 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1363 dev->wiphy.max_acl_mac_addrs &&
1364 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1365 dev->wiphy.max_acl_mac_addrs))
1366 goto nla_put_failure;
1367
Johannes Berga50df0c2013-02-11 14:20:05 +01001368 if (dev->wiphy.extended_capabilities &&
1369 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1370 dev->wiphy.extended_capabilities_len,
1371 dev->wiphy.extended_capabilities) ||
1372 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1373 dev->wiphy.extended_capabilities_len,
1374 dev->wiphy.extended_capabilities_mask)))
1375 goto nla_put_failure;
1376
Johannes Berg55682962007-09-20 13:09:35 -04001377 return genlmsg_end(msg, hdr);
1378
1379 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001380 genlmsg_cancel(msg, hdr);
1381 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001382}
1383
1384static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1385{
1386 int idx = 0;
1387 int start = cb->args[0];
1388 struct cfg80211_registered_device *dev;
1389
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001390 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001391 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001392 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1393 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001394 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001395 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001396 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001397 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001398 dev) < 0) {
1399 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001400 break;
Julius Volzb4637272008-07-08 14:02:19 +02001401 }
Johannes Berg55682962007-09-20 13:09:35 -04001402 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001403 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001404
1405 cb->args[0] = idx;
1406
1407 return skb->len;
1408}
1409
1410static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1411{
1412 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001413 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001414
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001415 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001416 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001417 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001418
Eric W. Biederman15e47302012-09-07 20:12:54 +00001419 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001420 nlmsg_free(msg);
1421 return -ENOBUFS;
1422 }
Johannes Berg55682962007-09-20 13:09:35 -04001423
Johannes Berg134e6372009-07-10 09:51:34 +00001424 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001425}
1426
Jouni Malinen31888482008-10-30 16:59:24 +02001427static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1428 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1429 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1430 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1431 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1432 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1433};
1434
1435static int parse_txq_params(struct nlattr *tb[],
1436 struct ieee80211_txq_params *txq_params)
1437{
Johannes Berga3304b02012-03-28 11:04:24 +02001438 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001439 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1440 !tb[NL80211_TXQ_ATTR_AIFS])
1441 return -EINVAL;
1442
Johannes Berga3304b02012-03-28 11:04:24 +02001443 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001444 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1445 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1446 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1447 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1448
Johannes Berga3304b02012-03-28 11:04:24 +02001449 if (txq_params->ac >= NL80211_NUM_ACS)
1450 return -EINVAL;
1451
Jouni Malinen31888482008-10-30 16:59:24 +02001452 return 0;
1453}
1454
Johannes Bergf444de02010-05-05 15:25:02 +02001455static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1456{
1457 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001458 * You can only set the channel explicitly for WDS interfaces,
1459 * all others have their channel managed via their respective
1460 * "establish a connection" command (connect, join, ...)
1461 *
1462 * For AP/GO and mesh mode, the channel can be set with the
1463 * channel userspace API, but is only stored and passed to the
1464 * low-level driver when the AP starts or the mesh is joined.
1465 * This is for backward compatibility, userspace can also give
1466 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001467 *
1468 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001469 * whatever else is going on, so they have their own special
1470 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001471 */
1472 return !wdev ||
1473 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001474 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001475 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1476 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001477}
1478
Johannes Berg683b6d32012-11-08 21:25:48 +01001479static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1480 struct genl_info *info,
1481 struct cfg80211_chan_def *chandef)
1482{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301483 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001484
1485 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1486 return -EINVAL;
1487
1488 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1489
1490 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001491 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1492 chandef->center_freq1 = control_freq;
1493 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001494
1495 /* Primary channel not allowed */
1496 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1497 return -EINVAL;
1498
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001499 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1500 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001501
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001502 chantype = nla_get_u32(
1503 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1504
1505 switch (chantype) {
1506 case NL80211_CHAN_NO_HT:
1507 case NL80211_CHAN_HT20:
1508 case NL80211_CHAN_HT40PLUS:
1509 case NL80211_CHAN_HT40MINUS:
1510 cfg80211_chandef_create(chandef, chandef->chan,
1511 chantype);
1512 break;
1513 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001514 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001515 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001516 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1517 chandef->width =
1518 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1519 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1520 chandef->center_freq1 =
1521 nla_get_u32(
1522 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1523 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1524 chandef->center_freq2 =
1525 nla_get_u32(
1526 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1527 }
1528
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001529 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001530 return -EINVAL;
1531
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001532 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1533 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001534 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001535
Johannes Berg683b6d32012-11-08 21:25:48 +01001536 return 0;
1537}
1538
Johannes Bergf444de02010-05-05 15:25:02 +02001539static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1540 struct wireless_dev *wdev,
1541 struct genl_info *info)
1542{
Johannes Berg683b6d32012-11-08 21:25:48 +01001543 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001544 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001545 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1546
1547 if (wdev)
1548 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001549
Johannes Bergf444de02010-05-05 15:25:02 +02001550 if (!nl80211_can_set_dev_channel(wdev))
1551 return -EOPNOTSUPP;
1552
Johannes Berg683b6d32012-11-08 21:25:48 +01001553 result = nl80211_parse_chandef(rdev, info, &chandef);
1554 if (result)
1555 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001556
1557 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001558 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001559 case NL80211_IFTYPE_AP:
1560 case NL80211_IFTYPE_P2P_GO:
1561 if (wdev->beacon_interval) {
1562 result = -EBUSY;
1563 break;
1564 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001565 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001566 result = -EINVAL;
1567 break;
1568 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001569 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001570 result = 0;
1571 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001572 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001573 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001574 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001575 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001576 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001577 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001578 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001579 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001580 }
1581 mutex_unlock(&rdev->devlist_mtx);
1582
1583 return result;
1584}
1585
1586static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1587{
Johannes Berg4c476992010-10-04 21:36:35 +02001588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1589 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001590
Johannes Berg4c476992010-10-04 21:36:35 +02001591 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001592}
1593
Bill Jordane8347eb2010-10-01 13:54:28 -04001594static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1595{
Johannes Berg43b19952010-10-07 13:10:30 +02001596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1597 struct net_device *dev = info->user_ptr[1];
1598 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001599 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001600
1601 if (!info->attrs[NL80211_ATTR_MAC])
1602 return -EINVAL;
1603
Johannes Berg43b19952010-10-07 13:10:30 +02001604 if (netif_running(dev))
1605 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001606
Johannes Berg43b19952010-10-07 13:10:30 +02001607 if (!rdev->ops->set_wds_peer)
1608 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001609
Johannes Berg43b19952010-10-07 13:10:30 +02001610 if (wdev->iftype != NL80211_IFTYPE_WDS)
1611 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001612
1613 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001614 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001615}
1616
1617
Johannes Berg55682962007-09-20 13:09:35 -04001618static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1619{
1620 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001621 struct net_device *netdev = NULL;
1622 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001623 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001624 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001625 u32 changed;
1626 u8 retry_short = 0, retry_long = 0;
1627 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001628 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001629
Johannes Bergf444de02010-05-05 15:25:02 +02001630 /*
1631 * Try to find the wiphy and netdev. Normally this
1632 * function shouldn't need the netdev, but this is
1633 * done for backward compatibility -- previously
1634 * setting the channel was done per wiphy, but now
1635 * it is per netdev. Previous userland like hostapd
1636 * also passed a netdev to set_wiphy, so that it is
1637 * possible to let that go to the right netdev!
1638 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001639 mutex_lock(&cfg80211_mutex);
1640
Johannes Bergf444de02010-05-05 15:25:02 +02001641 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1642 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1643
1644 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1645 if (netdev && netdev->ieee80211_ptr) {
1646 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1647 mutex_lock(&rdev->mtx);
1648 } else
1649 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001650 }
1651
Johannes Bergf444de02010-05-05 15:25:02 +02001652 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001653 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1654 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001655 if (IS_ERR(rdev)) {
1656 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001657 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001658 }
1659 wdev = NULL;
1660 netdev = NULL;
1661 result = 0;
1662
1663 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001664 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001665 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001666
1667 /*
1668 * end workaround code, by now the rdev is available
1669 * and locked, and wdev may or may not be NULL.
1670 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001671
1672 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001673 result = cfg80211_dev_rename(
1674 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001675
1676 mutex_unlock(&cfg80211_mutex);
1677
1678 if (result)
1679 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001680
Jouni Malinen31888482008-10-30 16:59:24 +02001681 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1682 struct ieee80211_txq_params txq_params;
1683 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1684
1685 if (!rdev->ops->set_txq_params) {
1686 result = -EOPNOTSUPP;
1687 goto bad_res;
1688 }
1689
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001690 if (!netdev) {
1691 result = -EINVAL;
1692 goto bad_res;
1693 }
1694
Johannes Berg133a3ff2011-11-03 14:50:13 +01001695 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1696 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1697 result = -EINVAL;
1698 goto bad_res;
1699 }
1700
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001701 if (!netif_running(netdev)) {
1702 result = -ENETDOWN;
1703 goto bad_res;
1704 }
1705
Jouni Malinen31888482008-10-30 16:59:24 +02001706 nla_for_each_nested(nl_txq_params,
1707 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1708 rem_txq_params) {
1709 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1710 nla_data(nl_txq_params),
1711 nla_len(nl_txq_params),
1712 txq_params_policy);
1713 result = parse_txq_params(tb, &txq_params);
1714 if (result)
1715 goto bad_res;
1716
Hila Gonene35e4d22012-06-27 17:19:42 +03001717 result = rdev_set_txq_params(rdev, netdev,
1718 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001719 if (result)
1720 goto bad_res;
1721 }
1722 }
1723
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001724 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001725 result = __nl80211_set_channel(rdev,
1726 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1727 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001728 if (result)
1729 goto bad_res;
1730 }
1731
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001732 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001733 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001734 enum nl80211_tx_power_setting type;
1735 int idx, mbm = 0;
1736
Johannes Bergc8442112012-10-24 10:17:18 +02001737 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1738 txp_wdev = NULL;
1739
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001740 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001741 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001742 goto bad_res;
1743 }
1744
1745 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1746 type = nla_get_u32(info->attrs[idx]);
1747
1748 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1749 (type != NL80211_TX_POWER_AUTOMATIC)) {
1750 result = -EINVAL;
1751 goto bad_res;
1752 }
1753
1754 if (type != NL80211_TX_POWER_AUTOMATIC) {
1755 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1756 mbm = nla_get_u32(info->attrs[idx]);
1757 }
1758
Johannes Bergc8442112012-10-24 10:17:18 +02001759 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001760 if (result)
1761 goto bad_res;
1762 }
1763
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001764 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1765 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1766 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001767 if ((!rdev->wiphy.available_antennas_tx &&
1768 !rdev->wiphy.available_antennas_rx) ||
1769 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001770 result = -EOPNOTSUPP;
1771 goto bad_res;
1772 }
1773
1774 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1775 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1776
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001777 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001778 * available antenna masks, except for the "all" mask */
1779 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1780 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001781 result = -EINVAL;
1782 goto bad_res;
1783 }
1784
Bruno Randolf7f531e02010-12-16 11:30:22 +09001785 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1786 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001787
Hila Gonene35e4d22012-06-27 17:19:42 +03001788 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001789 if (result)
1790 goto bad_res;
1791 }
1792
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001793 changed = 0;
1794
1795 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1796 retry_short = nla_get_u8(
1797 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1798 if (retry_short == 0) {
1799 result = -EINVAL;
1800 goto bad_res;
1801 }
1802 changed |= WIPHY_PARAM_RETRY_SHORT;
1803 }
1804
1805 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1806 retry_long = nla_get_u8(
1807 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1808 if (retry_long == 0) {
1809 result = -EINVAL;
1810 goto bad_res;
1811 }
1812 changed |= WIPHY_PARAM_RETRY_LONG;
1813 }
1814
1815 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1816 frag_threshold = nla_get_u32(
1817 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1818 if (frag_threshold < 256) {
1819 result = -EINVAL;
1820 goto bad_res;
1821 }
1822 if (frag_threshold != (u32) -1) {
1823 /*
1824 * Fragments (apart from the last one) are required to
1825 * have even length. Make the fragmentation code
1826 * simpler by stripping LSB should someone try to use
1827 * odd threshold value.
1828 */
1829 frag_threshold &= ~0x1;
1830 }
1831 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1832 }
1833
1834 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1835 rts_threshold = nla_get_u32(
1836 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1837 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1838 }
1839
Lukáš Turek81077e82009-12-21 22:50:47 +01001840 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1841 coverage_class = nla_get_u8(
1842 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1843 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1844 }
1845
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001846 if (changed) {
1847 u8 old_retry_short, old_retry_long;
1848 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001849 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001850
1851 if (!rdev->ops->set_wiphy_params) {
1852 result = -EOPNOTSUPP;
1853 goto bad_res;
1854 }
1855
1856 old_retry_short = rdev->wiphy.retry_short;
1857 old_retry_long = rdev->wiphy.retry_long;
1858 old_frag_threshold = rdev->wiphy.frag_threshold;
1859 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001860 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001861
1862 if (changed & WIPHY_PARAM_RETRY_SHORT)
1863 rdev->wiphy.retry_short = retry_short;
1864 if (changed & WIPHY_PARAM_RETRY_LONG)
1865 rdev->wiphy.retry_long = retry_long;
1866 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1867 rdev->wiphy.frag_threshold = frag_threshold;
1868 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1869 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001870 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1871 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001872
Hila Gonene35e4d22012-06-27 17:19:42 +03001873 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001874 if (result) {
1875 rdev->wiphy.retry_short = old_retry_short;
1876 rdev->wiphy.retry_long = old_retry_long;
1877 rdev->wiphy.frag_threshold = old_frag_threshold;
1878 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001879 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001880 }
1881 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001882
Johannes Berg306d6112008-12-08 12:39:04 +01001883 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001884 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001885 if (netdev)
1886 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001887 return result;
1888}
1889
Johannes Berg71bbc992012-06-15 15:30:18 +02001890static inline u64 wdev_id(struct wireless_dev *wdev)
1891{
1892 return (u64)wdev->identifier |
1893 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1894}
Johannes Berg55682962007-09-20 13:09:35 -04001895
Johannes Berg683b6d32012-11-08 21:25:48 +01001896static int nl80211_send_chandef(struct sk_buff *msg,
1897 struct cfg80211_chan_def *chandef)
1898{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001899 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001900
Johannes Berg683b6d32012-11-08 21:25:48 +01001901 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1902 chandef->chan->center_freq))
1903 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001904 switch (chandef->width) {
1905 case NL80211_CHAN_WIDTH_20_NOHT:
1906 case NL80211_CHAN_WIDTH_20:
1907 case NL80211_CHAN_WIDTH_40:
1908 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1909 cfg80211_get_chandef_type(chandef)))
1910 return -ENOBUFS;
1911 break;
1912 default:
1913 break;
1914 }
1915 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1916 return -ENOBUFS;
1917 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1918 return -ENOBUFS;
1919 if (chandef->center_freq2 &&
1920 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001921 return -ENOBUFS;
1922 return 0;
1923}
1924
Eric W. Biederman15e47302012-09-07 20:12:54 +00001925static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001926 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001927 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001928{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001929 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001930 void *hdr;
1931
Eric W. Biederman15e47302012-09-07 20:12:54 +00001932 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001933 if (!hdr)
1934 return -1;
1935
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001936 if (dev &&
1937 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001938 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001939 goto nla_put_failure;
1940
1941 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1942 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001943 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001944 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001945 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1946 rdev->devlist_generation ^
1947 (cfg80211_rdev_list_generation << 2)))
1948 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001949
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001950 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001951 int ret;
1952 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001953
Johannes Berg683b6d32012-11-08 21:25:48 +01001954 ret = rdev_get_channel(rdev, wdev, &chandef);
1955 if (ret == 0) {
1956 if (nl80211_send_chandef(msg, &chandef))
1957 goto nla_put_failure;
1958 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001959 }
1960
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001961 if (wdev->ssid_len) {
1962 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1963 goto nla_put_failure;
1964 }
1965
Johannes Berg55682962007-09-20 13:09:35 -04001966 return genlmsg_end(msg, hdr);
1967
1968 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001969 genlmsg_cancel(msg, hdr);
1970 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001971}
1972
1973static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1974{
1975 int wp_idx = 0;
1976 int if_idx = 0;
1977 int wp_start = cb->args[0];
1978 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001979 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001980 struct wireless_dev *wdev;
1981
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001982 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001983 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1984 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001985 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001986 if (wp_idx < wp_start) {
1987 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001988 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001989 }
Johannes Berg55682962007-09-20 13:09:35 -04001990 if_idx = 0;
1991
Johannes Bergf5ea9122009-08-07 16:17:38 +02001992 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001993 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001994 if (if_idx < if_start) {
1995 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001996 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001997 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001998 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001999 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002000 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002001 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002002 goto out;
2003 }
2004 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002005 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002006 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002007
2008 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002009 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002010 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002011 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002012
2013 cb->args[0] = wp_idx;
2014 cb->args[1] = if_idx;
2015
2016 return skb->len;
2017}
2018
2019static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2020{
2021 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002022 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002023 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002024
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002025 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002026 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002027 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002028
Eric W. Biederman15e47302012-09-07 20:12:54 +00002029 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002030 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002031 nlmsg_free(msg);
2032 return -ENOBUFS;
2033 }
Johannes Berg55682962007-09-20 13:09:35 -04002034
Johannes Berg134e6372009-07-10 09:51:34 +00002035 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002036}
2037
Michael Wu66f7ac52008-01-31 19:48:22 +01002038static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2039 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2040 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2041 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2042 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2043 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2044};
2045
2046static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2047{
2048 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2049 int flag;
2050
2051 *mntrflags = 0;
2052
2053 if (!nla)
2054 return -EINVAL;
2055
2056 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2057 nla, mntr_flags_policy))
2058 return -EINVAL;
2059
2060 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2061 if (flags[flag])
2062 *mntrflags |= (1<<flag);
2063
2064 return 0;
2065}
2066
Johannes Berg9bc383d2009-11-19 11:55:19 +01002067static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002068 struct net_device *netdev, u8 use_4addr,
2069 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002070{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002071 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002072 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002073 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002074 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002075 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002076
2077 switch (iftype) {
2078 case NL80211_IFTYPE_AP_VLAN:
2079 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2080 return 0;
2081 break;
2082 case NL80211_IFTYPE_STATION:
2083 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2084 return 0;
2085 break;
2086 default:
2087 break;
2088 }
2089
2090 return -EOPNOTSUPP;
2091}
2092
Johannes Berg55682962007-09-20 13:09:35 -04002093static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2094{
Johannes Berg4c476992010-10-04 21:36:35 +02002095 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002096 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002097 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002098 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002099 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002100 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002101 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002102
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002103 memset(&params, 0, sizeof(params));
2104
Johannes Berg04a773a2009-04-19 21:24:32 +02002105 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002106
Johannes Berg723b0382008-09-16 20:22:09 +02002107 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002108 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002109 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002110 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002111 if (ntype > NL80211_IFTYPE_MAX)
2112 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002113 }
2114
Johannes Berg92ffe052008-09-16 20:39:36 +02002115 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002116 struct wireless_dev *wdev = dev->ieee80211_ptr;
2117
Johannes Berg4c476992010-10-04 21:36:35 +02002118 if (ntype != NL80211_IFTYPE_MESH_POINT)
2119 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002120 if (netif_running(dev))
2121 return -EBUSY;
2122
2123 wdev_lock(wdev);
2124 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2125 IEEE80211_MAX_MESH_ID_LEN);
2126 wdev->mesh_id_up_len =
2127 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2128 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2129 wdev->mesh_id_up_len);
2130 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002131 }
2132
Felix Fietkau8b787642009-11-10 18:53:10 +01002133 if (info->attrs[NL80211_ATTR_4ADDR]) {
2134 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2135 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002136 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002137 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002138 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002139 } else {
2140 params.use_4addr = -1;
2141 }
2142
Johannes Berg92ffe052008-09-16 20:39:36 +02002143 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002144 if (ntype != NL80211_IFTYPE_MONITOR)
2145 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002146 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2147 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002148 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002149 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002150
2151 flags = &_flags;
2152 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002153 }
Johannes Berg3b858752009-03-12 09:55:09 +01002154
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002155 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002156 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002157 else
2158 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002159
Johannes Berg9bc383d2009-11-19 11:55:19 +01002160 if (!err && params.use_4addr != -1)
2161 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2162
Johannes Berg55682962007-09-20 13:09:35 -04002163 return err;
2164}
2165
2166static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2167{
Johannes Berg4c476992010-10-04 21:36:35 +02002168 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002169 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002170 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002171 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002172 int err;
2173 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002174 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002175
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002176 memset(&params, 0, sizeof(params));
2177
Johannes Berg55682962007-09-20 13:09:35 -04002178 if (!info->attrs[NL80211_ATTR_IFNAME])
2179 return -EINVAL;
2180
2181 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2182 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2183 if (type > NL80211_IFTYPE_MAX)
2184 return -EINVAL;
2185 }
2186
Johannes Berg79c97e92009-07-07 03:56:12 +02002187 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002188 !(rdev->wiphy.interface_modes & (1 << type)))
2189 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002190
Arend van Spriel1c18f142013-01-08 10:17:27 +01002191 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2192 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2193 ETH_ALEN);
2194 if (!is_valid_ether_addr(params.macaddr))
2195 return -EADDRNOTAVAIL;
2196 }
2197
Johannes Berg9bc383d2009-11-19 11:55:19 +01002198 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002199 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002200 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002201 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002202 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002203 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002204
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002205 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2206 if (!msg)
2207 return -ENOMEM;
2208
Michael Wu66f7ac52008-01-31 19:48:22 +01002209 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2210 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2211 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002212 wdev = rdev_add_virtual_intf(rdev,
2213 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2214 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002215 if (IS_ERR(wdev)) {
2216 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002217 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002218 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002219
Johannes Berg98104fde2012-06-16 00:19:54 +02002220 switch (type) {
2221 case NL80211_IFTYPE_MESH_POINT:
2222 if (!info->attrs[NL80211_ATTR_MESH_ID])
2223 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002224 wdev_lock(wdev);
2225 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2226 IEEE80211_MAX_MESH_ID_LEN);
2227 wdev->mesh_id_up_len =
2228 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2229 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2230 wdev->mesh_id_up_len);
2231 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002232 break;
2233 case NL80211_IFTYPE_P2P_DEVICE:
2234 /*
2235 * P2P Device doesn't have a netdev, so doesn't go
2236 * through the netdev notifier and must be added here
2237 */
2238 mutex_init(&wdev->mtx);
2239 INIT_LIST_HEAD(&wdev->event_list);
2240 spin_lock_init(&wdev->event_lock);
2241 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2242 spin_lock_init(&wdev->mgmt_registrations_lock);
2243
2244 mutex_lock(&rdev->devlist_mtx);
2245 wdev->identifier = ++rdev->wdev_id;
2246 list_add_rcu(&wdev->list, &rdev->wdev_list);
2247 rdev->devlist_generation++;
2248 mutex_unlock(&rdev->devlist_mtx);
2249 break;
2250 default:
2251 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002252 }
2253
Eric W. Biederman15e47302012-09-07 20:12:54 +00002254 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002255 rdev, wdev) < 0) {
2256 nlmsg_free(msg);
2257 return -ENOBUFS;
2258 }
2259
2260 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002261}
2262
2263static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2264{
Johannes Berg4c476992010-10-04 21:36:35 +02002265 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002266 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002267
Johannes Berg4c476992010-10-04 21:36:35 +02002268 if (!rdev->ops->del_virtual_intf)
2269 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002270
Johannes Berg84efbb82012-06-16 00:00:26 +02002271 /*
2272 * If we remove a wireless device without a netdev then clear
2273 * user_ptr[1] so that nl80211_post_doit won't dereference it
2274 * to check if it needs to do dev_put(). Otherwise it crashes
2275 * since the wdev has been freed, unlike with a netdev where
2276 * we need the dev_put() for the netdev to really be freed.
2277 */
2278 if (!wdev->netdev)
2279 info->user_ptr[1] = NULL;
2280
Hila Gonene35e4d22012-06-27 17:19:42 +03002281 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002282}
2283
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002284static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2285{
2286 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2287 struct net_device *dev = info->user_ptr[1];
2288 u16 noack_map;
2289
2290 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2291 return -EINVAL;
2292
2293 if (!rdev->ops->set_noack_map)
2294 return -EOPNOTSUPP;
2295
2296 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2297
Hila Gonene35e4d22012-06-27 17:19:42 +03002298 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002299}
2300
Johannes Berg41ade002007-12-19 02:03:29 +01002301struct get_key_cookie {
2302 struct sk_buff *msg;
2303 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002304 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002305};
2306
2307static void get_key_callback(void *c, struct key_params *params)
2308{
Johannes Bergb9454e82009-07-08 13:29:08 +02002309 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002310 struct get_key_cookie *cookie = c;
2311
David S. Miller9360ffd2012-03-29 04:41:26 -04002312 if ((params->key &&
2313 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2314 params->key_len, params->key)) ||
2315 (params->seq &&
2316 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2317 params->seq_len, params->seq)) ||
2318 (params->cipher &&
2319 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2320 params->cipher)))
2321 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002322
Johannes Bergb9454e82009-07-08 13:29:08 +02002323 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2324 if (!key)
2325 goto nla_put_failure;
2326
David S. Miller9360ffd2012-03-29 04:41:26 -04002327 if ((params->key &&
2328 nla_put(cookie->msg, NL80211_KEY_DATA,
2329 params->key_len, params->key)) ||
2330 (params->seq &&
2331 nla_put(cookie->msg, NL80211_KEY_SEQ,
2332 params->seq_len, params->seq)) ||
2333 (params->cipher &&
2334 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2335 params->cipher)))
2336 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002337
David S. Miller9360ffd2012-03-29 04:41:26 -04002338 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2339 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002340
2341 nla_nest_end(cookie->msg, key);
2342
Johannes Berg41ade002007-12-19 02:03:29 +01002343 return;
2344 nla_put_failure:
2345 cookie->error = 1;
2346}
2347
2348static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2349{
Johannes Berg4c476992010-10-04 21:36:35 +02002350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002351 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002352 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002353 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002354 const u8 *mac_addr = NULL;
2355 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002356 struct get_key_cookie cookie = {
2357 .error = 0,
2358 };
2359 void *hdr;
2360 struct sk_buff *msg;
2361
2362 if (info->attrs[NL80211_ATTR_KEY_IDX])
2363 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2364
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002365 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002366 return -EINVAL;
2367
2368 if (info->attrs[NL80211_ATTR_MAC])
2369 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2370
Johannes Berge31b8212010-10-05 19:39:30 +02002371 pairwise = !!mac_addr;
2372 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2373 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2374 if (kt >= NUM_NL80211_KEYTYPES)
2375 return -EINVAL;
2376 if (kt != NL80211_KEYTYPE_GROUP &&
2377 kt != NL80211_KEYTYPE_PAIRWISE)
2378 return -EINVAL;
2379 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2380 }
2381
Johannes Berg4c476992010-10-04 21:36:35 +02002382 if (!rdev->ops->get_key)
2383 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002384
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002386 if (!msg)
2387 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002388
Eric W. Biederman15e47302012-09-07 20:12:54 +00002389 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002390 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002391 if (IS_ERR(hdr))
2392 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002393
2394 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002395 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002396
David S. Miller9360ffd2012-03-29 04:41:26 -04002397 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2398 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2399 goto nla_put_failure;
2400 if (mac_addr &&
2401 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2402 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002403
Johannes Berge31b8212010-10-05 19:39:30 +02002404 if (pairwise && mac_addr &&
2405 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2406 return -ENOENT;
2407
Hila Gonene35e4d22012-06-27 17:19:42 +03002408 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2409 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002410
2411 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002412 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002413
2414 if (cookie.error)
2415 goto nla_put_failure;
2416
2417 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002418 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002419
2420 nla_put_failure:
2421 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002422 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002423 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002424 return err;
2425}
2426
2427static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2428{
Johannes Berg4c476992010-10-04 21:36:35 +02002429 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002430 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002431 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002432 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002433
Johannes Bergb9454e82009-07-08 13:29:08 +02002434 err = nl80211_parse_key(info, &key);
2435 if (err)
2436 return err;
2437
2438 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002439 return -EINVAL;
2440
Johannes Bergb9454e82009-07-08 13:29:08 +02002441 /* only support setting default key */
2442 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002443 return -EINVAL;
2444
Johannes Bergfffd0932009-07-08 14:22:54 +02002445 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002446
2447 if (key.def) {
2448 if (!rdev->ops->set_default_key) {
2449 err = -EOPNOTSUPP;
2450 goto out;
2451 }
2452
2453 err = nl80211_key_allowed(dev->ieee80211_ptr);
2454 if (err)
2455 goto out;
2456
Hila Gonene35e4d22012-06-27 17:19:42 +03002457 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002458 key.def_uni, key.def_multi);
2459
2460 if (err)
2461 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002462
Johannes Berg3d23e342009-09-29 23:27:28 +02002463#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002464 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002465#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002466 } else {
2467 if (key.def_uni || !key.def_multi) {
2468 err = -EINVAL;
2469 goto out;
2470 }
2471
2472 if (!rdev->ops->set_default_mgmt_key) {
2473 err = -EOPNOTSUPP;
2474 goto out;
2475 }
2476
2477 err = nl80211_key_allowed(dev->ieee80211_ptr);
2478 if (err)
2479 goto out;
2480
Hila Gonene35e4d22012-06-27 17:19:42 +03002481 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002482 if (err)
2483 goto out;
2484
2485#ifdef CONFIG_CFG80211_WEXT
2486 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2487#endif
2488 }
2489
2490 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002491 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002492
Johannes Berg41ade002007-12-19 02:03:29 +01002493 return err;
2494}
2495
2496static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2497{
Johannes Berg4c476992010-10-04 21:36:35 +02002498 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002499 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002500 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002501 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002502 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002503
Johannes Bergb9454e82009-07-08 13:29:08 +02002504 err = nl80211_parse_key(info, &key);
2505 if (err)
2506 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002507
Johannes Bergb9454e82009-07-08 13:29:08 +02002508 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002509 return -EINVAL;
2510
Johannes Berg41ade002007-12-19 02:03:29 +01002511 if (info->attrs[NL80211_ATTR_MAC])
2512 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2513
Johannes Berge31b8212010-10-05 19:39:30 +02002514 if (key.type == -1) {
2515 if (mac_addr)
2516 key.type = NL80211_KEYTYPE_PAIRWISE;
2517 else
2518 key.type = NL80211_KEYTYPE_GROUP;
2519 }
2520
2521 /* for now */
2522 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2523 key.type != NL80211_KEYTYPE_GROUP)
2524 return -EINVAL;
2525
Johannes Berg4c476992010-10-04 21:36:35 +02002526 if (!rdev->ops->add_key)
2527 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002528
Johannes Berge31b8212010-10-05 19:39:30 +02002529 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2530 key.type == NL80211_KEYTYPE_PAIRWISE,
2531 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002532 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002533
2534 wdev_lock(dev->ieee80211_ptr);
2535 err = nl80211_key_allowed(dev->ieee80211_ptr);
2536 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002537 err = rdev_add_key(rdev, dev, key.idx,
2538 key.type == NL80211_KEYTYPE_PAIRWISE,
2539 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002540 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002541
Johannes Berg41ade002007-12-19 02:03:29 +01002542 return err;
2543}
2544
2545static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2546{
Johannes Berg4c476992010-10-04 21:36:35 +02002547 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002548 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002549 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002550 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002551 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002552
Johannes Bergb9454e82009-07-08 13:29:08 +02002553 err = nl80211_parse_key(info, &key);
2554 if (err)
2555 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002556
2557 if (info->attrs[NL80211_ATTR_MAC])
2558 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2559
Johannes Berge31b8212010-10-05 19:39:30 +02002560 if (key.type == -1) {
2561 if (mac_addr)
2562 key.type = NL80211_KEYTYPE_PAIRWISE;
2563 else
2564 key.type = NL80211_KEYTYPE_GROUP;
2565 }
2566
2567 /* for now */
2568 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2569 key.type != NL80211_KEYTYPE_GROUP)
2570 return -EINVAL;
2571
Johannes Berg4c476992010-10-04 21:36:35 +02002572 if (!rdev->ops->del_key)
2573 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002574
Johannes Bergfffd0932009-07-08 14:22:54 +02002575 wdev_lock(dev->ieee80211_ptr);
2576 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002577
2578 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2579 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2580 err = -ENOENT;
2581
Johannes Bergfffd0932009-07-08 14:22:54 +02002582 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002583 err = rdev_del_key(rdev, dev, key.idx,
2584 key.type == NL80211_KEYTYPE_PAIRWISE,
2585 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002586
Johannes Berg3d23e342009-09-29 23:27:28 +02002587#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002588 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002589 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002590 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002591 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002592 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2593 }
2594#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002595 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002596
Johannes Berg41ade002007-12-19 02:03:29 +01002597 return err;
2598}
2599
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302600/* This function returns an error or the number of nested attributes */
2601static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2602{
2603 struct nlattr *attr;
2604 int n_entries = 0, tmp;
2605
2606 nla_for_each_nested(attr, nl_attr, tmp) {
2607 if (nla_len(attr) != ETH_ALEN)
2608 return -EINVAL;
2609
2610 n_entries++;
2611 }
2612
2613 return n_entries;
2614}
2615
2616/*
2617 * This function parses ACL information and allocates memory for ACL data.
2618 * On successful return, the calling function is responsible to free the
2619 * ACL buffer returned by this function.
2620 */
2621static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2622 struct genl_info *info)
2623{
2624 enum nl80211_acl_policy acl_policy;
2625 struct nlattr *attr;
2626 struct cfg80211_acl_data *acl;
2627 int i = 0, n_entries, tmp;
2628
2629 if (!wiphy->max_acl_mac_addrs)
2630 return ERR_PTR(-EOPNOTSUPP);
2631
2632 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2633 return ERR_PTR(-EINVAL);
2634
2635 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2636 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2637 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2638 return ERR_PTR(-EINVAL);
2639
2640 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2641 return ERR_PTR(-EINVAL);
2642
2643 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2644 if (n_entries < 0)
2645 return ERR_PTR(n_entries);
2646
2647 if (n_entries > wiphy->max_acl_mac_addrs)
2648 return ERR_PTR(-ENOTSUPP);
2649
2650 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2651 GFP_KERNEL);
2652 if (!acl)
2653 return ERR_PTR(-ENOMEM);
2654
2655 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2656 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2657 i++;
2658 }
2659
2660 acl->n_acl_entries = n_entries;
2661 acl->acl_policy = acl_policy;
2662
2663 return acl;
2664}
2665
2666static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2667{
2668 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2669 struct net_device *dev = info->user_ptr[1];
2670 struct cfg80211_acl_data *acl;
2671 int err;
2672
2673 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2674 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2675 return -EOPNOTSUPP;
2676
2677 if (!dev->ieee80211_ptr->beacon_interval)
2678 return -EINVAL;
2679
2680 acl = parse_acl_data(&rdev->wiphy, info);
2681 if (IS_ERR(acl))
2682 return PTR_ERR(acl);
2683
2684 err = rdev_set_mac_acl(rdev, dev, acl);
2685
2686 kfree(acl);
2687
2688 return err;
2689}
2690
Johannes Berg88600202012-02-13 15:17:18 +01002691static int nl80211_parse_beacon(struct genl_info *info,
2692 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002693{
Johannes Berg88600202012-02-13 15:17:18 +01002694 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002695
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002696 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2697 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2698 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2699 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002700 return -EINVAL;
2701
Johannes Berg88600202012-02-13 15:17:18 +01002702 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002703
Johannes Berged1b6cc2007-12-19 02:03:32 +01002704 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002705 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2706 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2707 if (!bcn->head_len)
2708 return -EINVAL;
2709 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002710 }
2711
2712 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002713 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2714 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002715 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002716 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002717 }
2718
Johannes Berg4c476992010-10-04 21:36:35 +02002719 if (!haveinfo)
2720 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002721
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002722 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002723 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2724 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002725 }
2726
2727 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002728 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002729 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002730 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002731 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2732 }
2733
2734 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002735 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002736 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002737 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002738 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2739 }
2740
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002741 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002742 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002743 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002744 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002745 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2746 }
2747
Johannes Berg88600202012-02-13 15:17:18 +01002748 return 0;
2749}
2750
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002751static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2752 struct cfg80211_ap_settings *params)
2753{
2754 struct wireless_dev *wdev;
2755 bool ret = false;
2756
2757 mutex_lock(&rdev->devlist_mtx);
2758
Johannes Berg89a54e42012-06-15 14:33:17 +02002759 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002760 if (wdev->iftype != NL80211_IFTYPE_AP &&
2761 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2762 continue;
2763
Johannes Berg683b6d32012-11-08 21:25:48 +01002764 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002765 continue;
2766
Johannes Berg683b6d32012-11-08 21:25:48 +01002767 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002768 ret = true;
2769 break;
2770 }
2771
2772 mutex_unlock(&rdev->devlist_mtx);
2773
2774 return ret;
2775}
2776
Jouni Malinene39e5b52012-09-30 19:29:39 +03002777static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2778 enum nl80211_auth_type auth_type,
2779 enum nl80211_commands cmd)
2780{
2781 if (auth_type > NL80211_AUTHTYPE_MAX)
2782 return false;
2783
2784 switch (cmd) {
2785 case NL80211_CMD_AUTHENTICATE:
2786 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2787 auth_type == NL80211_AUTHTYPE_SAE)
2788 return false;
2789 return true;
2790 case NL80211_CMD_CONNECT:
2791 case NL80211_CMD_START_AP:
2792 /* SAE not supported yet */
2793 if (auth_type == NL80211_AUTHTYPE_SAE)
2794 return false;
2795 return true;
2796 default:
2797 return false;
2798 }
2799}
2800
Johannes Berg88600202012-02-13 15:17:18 +01002801static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2802{
2803 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2804 struct net_device *dev = info->user_ptr[1];
2805 struct wireless_dev *wdev = dev->ieee80211_ptr;
2806 struct cfg80211_ap_settings params;
2807 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01002808 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01002809
2810 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2811 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2812 return -EOPNOTSUPP;
2813
2814 if (!rdev->ops->start_ap)
2815 return -EOPNOTSUPP;
2816
2817 if (wdev->beacon_interval)
2818 return -EALREADY;
2819
2820 memset(&params, 0, sizeof(params));
2821
2822 /* these are required for START_AP */
2823 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2824 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2825 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2826 return -EINVAL;
2827
2828 err = nl80211_parse_beacon(info, &params.beacon);
2829 if (err)
2830 return err;
2831
2832 params.beacon_interval =
2833 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2834 params.dtim_period =
2835 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2836
2837 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2838 if (err)
2839 return err;
2840
2841 /*
2842 * In theory, some of these attributes should be required here
2843 * but since they were not used when the command was originally
2844 * added, keep them optional for old user space programs to let
2845 * them continue to work with drivers that do not need the
2846 * additional information -- drivers must check!
2847 */
2848 if (info->attrs[NL80211_ATTR_SSID]) {
2849 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2850 params.ssid_len =
2851 nla_len(info->attrs[NL80211_ATTR_SSID]);
2852 if (params.ssid_len == 0 ||
2853 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2854 return -EINVAL;
2855 }
2856
2857 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2858 params.hidden_ssid = nla_get_u32(
2859 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2860 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2861 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2862 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2863 return -EINVAL;
2864 }
2865
2866 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2867
2868 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2869 params.auth_type = nla_get_u32(
2870 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002871 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2872 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002873 return -EINVAL;
2874 } else
2875 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2876
2877 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2878 NL80211_MAX_NR_CIPHER_SUITES);
2879 if (err)
2880 return err;
2881
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302882 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2883 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2884 return -EOPNOTSUPP;
2885 params.inactivity_timeout = nla_get_u16(
2886 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2887 }
2888
Johannes Berg53cabad2012-11-14 15:17:28 +01002889 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2890 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2891 return -EINVAL;
2892 params.p2p_ctwindow =
2893 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2894 if (params.p2p_ctwindow > 127)
2895 return -EINVAL;
2896 if (params.p2p_ctwindow != 0 &&
2897 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2898 return -EINVAL;
2899 }
2900
2901 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2902 u8 tmp;
2903
2904 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2905 return -EINVAL;
2906 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2907 if (tmp > 1)
2908 return -EINVAL;
2909 params.p2p_opp_ps = tmp;
2910 if (params.p2p_opp_ps != 0 &&
2911 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2912 return -EINVAL;
2913 }
2914
Johannes Bergaa430da2012-05-16 23:50:18 +02002915 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002916 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2917 if (err)
2918 return err;
2919 } else if (wdev->preset_chandef.chan) {
2920 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002921 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002922 return -EINVAL;
2923
Johannes Berg683b6d32012-11-08 21:25:48 +01002924 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002925 return -EINVAL;
2926
Simon Wunderlich04f39042013-02-08 18:16:19 +01002927 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
2928 if (err < 0)
2929 return err;
2930 if (err) {
2931 radar_detect_width = BIT(params.chandef.width);
2932 params.radar_required = true;
2933 }
2934
Michal Kaziore4e32452012-06-29 12:47:08 +02002935 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01002936 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
2937 params.chandef.chan,
2938 CHAN_MODE_SHARED,
2939 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02002940 mutex_unlock(&rdev->devlist_mtx);
2941
2942 if (err)
2943 return err;
2944
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302945 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
2946 params.acl = parse_acl_data(&rdev->wiphy, info);
2947 if (IS_ERR(params.acl))
2948 return PTR_ERR(params.acl);
2949 }
2950
Hila Gonene35e4d22012-06-27 17:19:42 +03002951 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002952 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002953 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002954 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002955 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002956 wdev->ssid_len = params.ssid_len;
2957 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002958 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302959
2960 kfree(params.acl);
2961
Johannes Berg56d18932011-05-09 18:41:15 +02002962 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002963}
2964
Johannes Berg88600202012-02-13 15:17:18 +01002965static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2966{
2967 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2968 struct net_device *dev = info->user_ptr[1];
2969 struct wireless_dev *wdev = dev->ieee80211_ptr;
2970 struct cfg80211_beacon_data params;
2971 int err;
2972
2973 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2974 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2975 return -EOPNOTSUPP;
2976
2977 if (!rdev->ops->change_beacon)
2978 return -EOPNOTSUPP;
2979
2980 if (!wdev->beacon_interval)
2981 return -EINVAL;
2982
2983 err = nl80211_parse_beacon(info, &params);
2984 if (err)
2985 return err;
2986
Hila Gonene35e4d22012-06-27 17:19:42 +03002987 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002988}
2989
2990static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002991{
Johannes Berg4c476992010-10-04 21:36:35 +02002992 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2993 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002994
Michal Kazior60771782012-06-29 12:46:56 +02002995 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002996}
2997
Johannes Berg5727ef12007-12-19 02:03:34 +01002998static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2999 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3000 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3001 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003002 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003003 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003004 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003005};
3006
Johannes Bergeccb8e82009-05-11 21:57:56 +03003007static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003008 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003009 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003010{
3011 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003012 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003013 int flag;
3014
Johannes Bergeccb8e82009-05-11 21:57:56 +03003015 /*
3016 * Try parsing the new attribute first so userspace
3017 * can specify both for older kernels.
3018 */
3019 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3020 if (nla) {
3021 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003022
Johannes Bergeccb8e82009-05-11 21:57:56 +03003023 sta_flags = nla_data(nla);
3024 params->sta_flags_mask = sta_flags->mask;
3025 params->sta_flags_set = sta_flags->set;
3026 if ((params->sta_flags_mask |
3027 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3028 return -EINVAL;
3029 return 0;
3030 }
3031
3032 /* if present, parse the old attribute */
3033
3034 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003035 if (!nla)
3036 return 0;
3037
3038 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3039 nla, sta_flags_policy))
3040 return -EINVAL;
3041
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003042 /*
3043 * Only allow certain flags for interface types so that
3044 * other attributes are silently ignored. Remember that
3045 * this is backward compatibility code with old userspace
3046 * and shouldn't be hit in other cases anyway.
3047 */
3048 switch (iftype) {
3049 case NL80211_IFTYPE_AP:
3050 case NL80211_IFTYPE_AP_VLAN:
3051 case NL80211_IFTYPE_P2P_GO:
3052 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3053 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3054 BIT(NL80211_STA_FLAG_WME) |
3055 BIT(NL80211_STA_FLAG_MFP);
3056 break;
3057 case NL80211_IFTYPE_P2P_CLIENT:
3058 case NL80211_IFTYPE_STATION:
3059 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3060 BIT(NL80211_STA_FLAG_TDLS_PEER);
3061 break;
3062 case NL80211_IFTYPE_MESH_POINT:
3063 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3064 BIT(NL80211_STA_FLAG_MFP) |
3065 BIT(NL80211_STA_FLAG_AUTHORIZED);
3066 default:
3067 return -EINVAL;
3068 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003069
Johannes Berg3383b5a2012-05-10 20:14:43 +02003070 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3071 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003072 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003073
Johannes Berg3383b5a2012-05-10 20:14:43 +02003074 /* no longer support new API additions in old API */
3075 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3076 return -EINVAL;
3077 }
3078 }
3079
Johannes Berg5727ef12007-12-19 02:03:34 +01003080 return 0;
3081}
3082
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003083static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3084 int attr)
3085{
3086 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003087 u32 bitrate;
3088 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003089
3090 rate = nla_nest_start(msg, attr);
3091 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003092 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003093
3094 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3095 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003096 /* report 16-bit bitrate only if we can */
3097 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003098 if (bitrate > 0 &&
3099 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3100 return false;
3101 if (bitrate_compat > 0 &&
3102 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3103 return false;
3104
3105 if (info->flags & RATE_INFO_FLAGS_MCS) {
3106 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3107 return false;
3108 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3109 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3110 return false;
3111 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3112 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3113 return false;
3114 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3115 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3116 return false;
3117 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3118 return false;
3119 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3120 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3121 return false;
3122 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3123 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3124 return false;
3125 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3126 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3127 return false;
3128 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3129 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3130 return false;
3131 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3132 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3133 return false;
3134 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003135
3136 nla_nest_end(msg, rate);
3137 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003138}
3139
Eric W. Biederman15e47302012-09-07 20:12:54 +00003140static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003141 int flags,
3142 struct cfg80211_registered_device *rdev,
3143 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003144 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003145{
3146 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003147 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003148
Eric W. Biederman15e47302012-09-07 20:12:54 +00003149 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003150 if (!hdr)
3151 return -1;
3152
David S. Miller9360ffd2012-03-29 04:41:26 -04003153 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3154 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3155 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3156 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003157
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003158 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3159 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003160 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003161 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3162 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3163 sinfo->connected_time))
3164 goto nla_put_failure;
3165 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3166 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3167 sinfo->inactive_time))
3168 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003169 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3170 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003171 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003172 (u32)sinfo->rx_bytes))
3173 goto nla_put_failure;
3174 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3175 NL80211_STA_INFO_TX_BYTES64)) &&
3176 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3177 (u32)sinfo->tx_bytes))
3178 goto nla_put_failure;
3179 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3180 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003181 sinfo->rx_bytes))
3182 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003183 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3184 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003185 sinfo->tx_bytes))
3186 goto nla_put_failure;
3187 if ((sinfo->filled & STATION_INFO_LLID) &&
3188 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3189 goto nla_put_failure;
3190 if ((sinfo->filled & STATION_INFO_PLID) &&
3191 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3192 goto nla_put_failure;
3193 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3194 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3195 sinfo->plink_state))
3196 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003197 switch (rdev->wiphy.signal_type) {
3198 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003199 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3200 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3201 sinfo->signal))
3202 goto nla_put_failure;
3203 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3204 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3205 sinfo->signal_avg))
3206 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003207 break;
3208 default:
3209 break;
3210 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003211 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003212 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3213 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003214 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003215 }
3216 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3217 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3218 NL80211_STA_INFO_RX_BITRATE))
3219 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003220 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003221 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3222 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3223 sinfo->rx_packets))
3224 goto nla_put_failure;
3225 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3226 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3227 sinfo->tx_packets))
3228 goto nla_put_failure;
3229 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3230 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3231 sinfo->tx_retries))
3232 goto nla_put_failure;
3233 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3234 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3235 sinfo->tx_failed))
3236 goto nla_put_failure;
3237 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3238 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3239 sinfo->beacon_loss_count))
3240 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003241 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3242 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3243 sinfo->local_pm))
3244 goto nla_put_failure;
3245 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3246 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3247 sinfo->peer_pm))
3248 goto nla_put_failure;
3249 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3250 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3251 sinfo->nonpeer_pm))
3252 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003253 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3254 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3255 if (!bss_param)
3256 goto nla_put_failure;
3257
David S. Miller9360ffd2012-03-29 04:41:26 -04003258 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3259 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3260 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3261 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3262 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3263 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3264 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3265 sinfo->bss_param.dtim_period) ||
3266 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3267 sinfo->bss_param.beacon_interval))
3268 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003269
3270 nla_nest_end(msg, bss_param);
3271 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003272 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3273 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3274 sizeof(struct nl80211_sta_flag_update),
3275 &sinfo->sta_flags))
3276 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003277 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3278 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3279 sinfo->t_offset))
3280 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003281 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003282
David S. Miller9360ffd2012-03-29 04:41:26 -04003283 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3284 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3285 sinfo->assoc_req_ies))
3286 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003287
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003288 return genlmsg_end(msg, hdr);
3289
3290 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003291 genlmsg_cancel(msg, hdr);
3292 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003293}
3294
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003295static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003296 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003297{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003298 struct station_info sinfo;
3299 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003300 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003301 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003302 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003303 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003304
Johannes Berg67748892010-10-04 21:14:06 +02003305 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3306 if (err)
3307 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003308
3309 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003310 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003311 goto out_err;
3312 }
3313
Johannes Bergbba95fe2008-07-29 13:22:51 +02003314 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003315 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003316 err = rdev_dump_station(dev, netdev, sta_idx,
3317 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003318 if (err == -ENOENT)
3319 break;
3320 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003321 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003322
3323 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003324 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003325 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003326 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003327 &sinfo) < 0)
3328 goto out;
3329
3330 sta_idx++;
3331 }
3332
3333
3334 out:
3335 cb->args[1] = sta_idx;
3336 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003337 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003338 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003339
3340 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003341}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003342
Johannes Berg5727ef12007-12-19 02:03:34 +01003343static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3344{
Johannes Berg4c476992010-10-04 21:36:35 +02003345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3346 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003347 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003348 struct sk_buff *msg;
3349 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003350 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003351
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003352 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003353
3354 if (!info->attrs[NL80211_ATTR_MAC])
3355 return -EINVAL;
3356
3357 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3358
Johannes Berg4c476992010-10-04 21:36:35 +02003359 if (!rdev->ops->get_station)
3360 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003361
Hila Gonene35e4d22012-06-27 17:19:42 +03003362 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003363 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003364 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003365
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003366 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003367 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003368 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003369
Eric W. Biederman15e47302012-09-07 20:12:54 +00003370 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003371 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003372 nlmsg_free(msg);
3373 return -ENOBUFS;
3374 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003375
Johannes Berg4c476992010-10-04 21:36:35 +02003376 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003377}
3378
3379/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003380 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003381 */
Johannes Berg80b99892011-11-18 16:23:01 +01003382static struct net_device *get_vlan(struct genl_info *info,
3383 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003384{
Johannes Berg463d0182009-07-14 00:33:35 +02003385 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003386 struct net_device *v;
3387 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003388
Johannes Berg80b99892011-11-18 16:23:01 +01003389 if (!vlanattr)
3390 return NULL;
3391
3392 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3393 if (!v)
3394 return ERR_PTR(-ENODEV);
3395
3396 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3397 ret = -EINVAL;
3398 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003399 }
Johannes Berg80b99892011-11-18 16:23:01 +01003400
3401 if (!netif_running(v)) {
3402 ret = -ENETDOWN;
3403 goto error;
3404 }
3405
3406 return v;
3407 error:
3408 dev_put(v);
3409 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003410}
3411
Jouni Malinendf881292013-02-14 21:10:54 +02003412static struct nla_policy
3413nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3414 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3415 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3416};
3417
3418static int nl80211_set_station_tdls(struct genl_info *info,
3419 struct station_parameters *params)
3420{
3421 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3422 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3423 struct nlattr *nla;
3424 int err;
3425
3426 /* Can only set if TDLS ... */
3427 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3428 return -EOPNOTSUPP;
3429
3430 /* ... with external setup is supported */
3431 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3432 return -EOPNOTSUPP;
3433
3434 /* Dummy STA entry gets updated once the peer capabilities are known */
3435 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3436 params->ht_capa =
3437 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3438 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3439 params->vht_capa =
3440 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3441
3442 /* parse WME attributes if present */
3443 if (!info->attrs[NL80211_ATTR_STA_WME])
3444 return 0;
3445
3446 nla = info->attrs[NL80211_ATTR_STA_WME];
3447 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3448 nl80211_sta_wme_policy);
3449 if (err)
3450 return err;
3451
3452 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3453 params->uapsd_queues = nla_get_u8(
3454 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3455 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3456 return -EINVAL;
3457
3458 if (tb[NL80211_STA_WME_MAX_SP])
3459 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3460
3461 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3462 return -EINVAL;
3463
3464 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3465
3466 return 0;
3467}
3468
Johannes Berg5727ef12007-12-19 02:03:34 +01003469static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3470{
Johannes Berg4c476992010-10-04 21:36:35 +02003471 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003472 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003473 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003474 struct station_parameters params;
3475 u8 *mac_addr = NULL;
3476
3477 memset(&params, 0, sizeof(params));
3478
3479 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003480 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003481
3482 if (info->attrs[NL80211_ATTR_STA_AID])
3483 return -EINVAL;
3484
3485 if (!info->attrs[NL80211_ATTR_MAC])
3486 return -EINVAL;
3487
3488 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3489
3490 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3491 params.supported_rates =
3492 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3493 params.supported_rates_len =
3494 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3495 }
3496
Jouni Malinen9d62a982013-02-14 21:10:13 +02003497 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3498 params.capability =
3499 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3500 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3501 }
3502
3503 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3504 params.ext_capab =
3505 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3506 params.ext_capab_len =
3507 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3508 }
3509
Jouni Malinendf881292013-02-14 21:10:54 +02003510 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003511 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03003512
Johannes Bergbdd90d52011-12-14 12:20:27 +01003513 if (!rdev->ops->change_station)
3514 return -EOPNOTSUPP;
3515
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003516 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003517 return -EINVAL;
3518
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003519 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3520 params.plink_action =
3521 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3522
Javier Cardona9c3990a2011-05-03 16:57:11 -07003523 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3524 params.plink_state =
3525 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3526
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003527 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3528 enum nl80211_mesh_power_mode pm = nla_get_u32(
3529 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3530
3531 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3532 pm > NL80211_MESH_POWER_MAX)
3533 return -EINVAL;
3534
3535 params.local_pm = pm;
3536 }
3537
Johannes Berga97f4422009-06-18 17:23:43 +02003538 switch (dev->ieee80211_ptr->iftype) {
3539 case NL80211_IFTYPE_AP:
3540 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003541 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003542 /* disallow mesh-specific things */
3543 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003544 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003545 if (params.local_pm)
3546 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003547
3548 /* TDLS can't be set, ... */
3549 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3550 return -EINVAL;
3551 /*
3552 * ... but don't bother the driver with it. This works around
3553 * a hostapd/wpa_supplicant issue -- it always includes the
3554 * TLDS_PEER flag in the mask even for AP mode.
3555 */
3556 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3557
3558 /* accept only the listed bits */
3559 if (params.sta_flags_mask &
3560 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003561 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3562 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003563 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3564 BIT(NL80211_STA_FLAG_WME) |
3565 BIT(NL80211_STA_FLAG_MFP)))
3566 return -EINVAL;
3567
Johannes Bergd582cff2012-10-26 17:53:44 +02003568 /* but authenticated/associated only if driver handles it */
3569 if (!(rdev->wiphy.features &
3570 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3571 params.sta_flags_mask &
3572 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3573 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3574 return -EINVAL;
3575
Johannes Bergba23d202012-12-27 17:32:09 +01003576 /* reject other things that can't change */
3577 if (params.supported_rates)
3578 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003579 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3580 return -EINVAL;
3581 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3582 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003583 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3584 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3585 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003586
Johannes Bergbdd90d52011-12-14 12:20:27 +01003587 /* must be last in here for error handling */
3588 params.vlan = get_vlan(info, rdev);
3589 if (IS_ERR(params.vlan))
3590 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003591 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003592 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003593 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003594 /*
3595 * Don't allow userspace to change the TDLS_PEER flag,
3596 * but silently ignore attempts to change it since we
3597 * don't have state here to verify that it doesn't try
3598 * to change the flag.
3599 */
3600 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Jouni Malinendf881292013-02-14 21:10:54 +02003601 /* Include parameters for TDLS peer (driver will check) */
3602 err = nl80211_set_station_tdls(info, &params);
3603 if (err)
3604 return err;
3605 /* disallow things sta doesn't support */
3606 if (params.plink_action)
3607 return -EINVAL;
3608 if (params.local_pm)
3609 return -EINVAL;
3610 /* reject any changes other than AUTHORIZED or WME (for TDLS) */
3611 if (params.sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3612 BIT(NL80211_STA_FLAG_WME)))
3613 return -EINVAL;
3614 break;
Antonio Quartulli267335d2012-01-31 20:25:47 +01003615 case NL80211_IFTYPE_ADHOC:
3616 /* disallow things sta doesn't support */
3617 if (params.plink_action)
3618 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003619 if (params.local_pm)
3620 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003621 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3622 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3623 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003624 /* reject any changes other than AUTHORIZED */
3625 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3626 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003627 break;
3628 case NL80211_IFTYPE_MESH_POINT:
3629 /* disallow things mesh doesn't support */
3630 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003631 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003632 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003633 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003634 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3635 return -EINVAL;
3636 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3637 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003638 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3639 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3640 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003641 /*
3642 * No special handling for TDLS here -- the userspace
3643 * mesh code doesn't have this bug.
3644 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003645 if (params.sta_flags_mask &
3646 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003647 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003648 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003649 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003650 break;
3651 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003652 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003653 }
3654
Johannes Bergbdd90d52011-12-14 12:20:27 +01003655 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003656
Hila Gonene35e4d22012-06-27 17:19:42 +03003657 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003658
Johannes Berg5727ef12007-12-19 02:03:34 +01003659 if (params.vlan)
3660 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003661
Johannes Berg5727ef12007-12-19 02:03:34 +01003662 return err;
3663}
3664
3665static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3666{
Johannes Berg4c476992010-10-04 21:36:35 +02003667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003668 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003669 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003670 struct station_parameters params;
3671 u8 *mac_addr = NULL;
3672
3673 memset(&params, 0, sizeof(params));
3674
3675 if (!info->attrs[NL80211_ATTR_MAC])
3676 return -EINVAL;
3677
Johannes Berg5727ef12007-12-19 02:03:34 +01003678 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3679 return -EINVAL;
3680
3681 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3682 return -EINVAL;
3683
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003684 if (!info->attrs[NL80211_ATTR_STA_AID])
3685 return -EINVAL;
3686
Johannes Berg5727ef12007-12-19 02:03:34 +01003687 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3688 params.supported_rates =
3689 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3690 params.supported_rates_len =
3691 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3692 params.listen_interval =
3693 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003694
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003695 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3696 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3697 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003698
Jouni Malinen9d62a982013-02-14 21:10:13 +02003699 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3700 params.capability =
3701 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3702 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3703 }
3704
3705 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3706 params.ext_capab =
3707 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3708 params.ext_capab_len =
3709 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3710 }
3711
Jouni Malinen36aedc902008-08-25 11:58:58 +03003712 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3713 params.ht_capa =
3714 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003715
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003716 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3717 params.vht_capa =
3718 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3719
Javier Cardona96b78df2011-04-07 15:08:33 -07003720 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3721 params.plink_action =
3722 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3723
Johannes Bergbdd90d52011-12-14 12:20:27 +01003724 if (!rdev->ops->add_station)
3725 return -EOPNOTSUPP;
3726
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003727 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003728 return -EINVAL;
3729
Johannes Bergbdd90d52011-12-14 12:20:27 +01003730 switch (dev->ieee80211_ptr->iftype) {
3731 case NL80211_IFTYPE_AP:
3732 case NL80211_IFTYPE_AP_VLAN:
3733 case NL80211_IFTYPE_P2P_GO:
3734 /* parse WME attributes if sta is WME capable */
3735 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3736 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3737 info->attrs[NL80211_ATTR_STA_WME]) {
3738 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3739 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003740
Johannes Bergbdd90d52011-12-14 12:20:27 +01003741 nla = info->attrs[NL80211_ATTR_STA_WME];
3742 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3743 nl80211_sta_wme_policy);
3744 if (err)
3745 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003746
Johannes Bergbdd90d52011-12-14 12:20:27 +01003747 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3748 params.uapsd_queues =
3749 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3750 if (params.uapsd_queues &
3751 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3752 return -EINVAL;
3753
3754 if (tb[NL80211_STA_WME_MAX_SP])
3755 params.max_sp =
3756 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3757
3758 if (params.max_sp &
3759 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3760 return -EINVAL;
3761
3762 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3763 }
3764 /* TDLS peers cannot be added */
3765 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003766 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003767 /* but don't bother the driver with it */
3768 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003769
Johannes Bergd582cff2012-10-26 17:53:44 +02003770 /* allow authenticated/associated only if driver handles it */
3771 if (!(rdev->wiphy.features &
3772 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3773 params.sta_flags_mask &
3774 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3775 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3776 return -EINVAL;
3777
Johannes Bergbdd90d52011-12-14 12:20:27 +01003778 /* must be last in here for error handling */
3779 params.vlan = get_vlan(info, rdev);
3780 if (IS_ERR(params.vlan))
3781 return PTR_ERR(params.vlan);
3782 break;
3783 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergd582cff2012-10-26 17:53:44 +02003784 /* associated is disallowed */
3785 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3786 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003787 /* TDLS peers cannot be added */
3788 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003789 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003790 break;
3791 case NL80211_IFTYPE_STATION:
Johannes Bergd582cff2012-10-26 17:53:44 +02003792 /* associated is disallowed */
3793 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3794 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003795 /* Only TDLS peers can be added */
3796 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3797 return -EINVAL;
3798 /* Can only add if TDLS ... */
3799 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3800 return -EOPNOTSUPP;
3801 /* ... with external setup is supported */
3802 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3803 return -EOPNOTSUPP;
3804 break;
3805 default:
3806 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003807 }
3808
Johannes Bergbdd90d52011-12-14 12:20:27 +01003809 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003810
Hila Gonene35e4d22012-06-27 17:19:42 +03003811 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003812
Johannes Berg5727ef12007-12-19 02:03:34 +01003813 if (params.vlan)
3814 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003815 return err;
3816}
3817
3818static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3819{
Johannes Berg4c476992010-10-04 21:36:35 +02003820 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3821 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003822 u8 *mac_addr = NULL;
3823
3824 if (info->attrs[NL80211_ATTR_MAC])
3825 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3826
Johannes Berge80cf852009-05-11 14:43:13 +02003827 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003828 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003829 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003830 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3831 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003832
Johannes Berg4c476992010-10-04 21:36:35 +02003833 if (!rdev->ops->del_station)
3834 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003835
Hila Gonene35e4d22012-06-27 17:19:42 +03003836 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003837}
3838
Eric W. Biederman15e47302012-09-07 20:12:54 +00003839static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003840 int flags, struct net_device *dev,
3841 u8 *dst, u8 *next_hop,
3842 struct mpath_info *pinfo)
3843{
3844 void *hdr;
3845 struct nlattr *pinfoattr;
3846
Eric W. Biederman15e47302012-09-07 20:12:54 +00003847 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003848 if (!hdr)
3849 return -1;
3850
David S. Miller9360ffd2012-03-29 04:41:26 -04003851 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3852 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3853 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3854 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3855 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003856
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003857 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3858 if (!pinfoattr)
3859 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003860 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3861 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3862 pinfo->frame_qlen))
3863 goto nla_put_failure;
3864 if (((pinfo->filled & MPATH_INFO_SN) &&
3865 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3866 ((pinfo->filled & MPATH_INFO_METRIC) &&
3867 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3868 pinfo->metric)) ||
3869 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3870 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3871 pinfo->exptime)) ||
3872 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3873 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3874 pinfo->flags)) ||
3875 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3876 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3877 pinfo->discovery_timeout)) ||
3878 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3879 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3880 pinfo->discovery_retries)))
3881 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003882
3883 nla_nest_end(msg, pinfoattr);
3884
3885 return genlmsg_end(msg, hdr);
3886
3887 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003888 genlmsg_cancel(msg, hdr);
3889 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003890}
3891
3892static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003893 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003894{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003895 struct mpath_info pinfo;
3896 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003897 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003898 u8 dst[ETH_ALEN];
3899 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003900 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003901 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003902
Johannes Berg67748892010-10-04 21:14:06 +02003903 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3904 if (err)
3905 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003906
3907 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003908 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003909 goto out_err;
3910 }
3911
Jouni Malineneec60b02009-03-20 21:21:19 +02003912 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3913 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003914 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003915 }
3916
Johannes Bergbba95fe2008-07-29 13:22:51 +02003917 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003918 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3919 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003920 if (err == -ENOENT)
3921 break;
3922 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003923 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003924
Eric W. Biederman15e47302012-09-07 20:12:54 +00003925 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003926 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3927 netdev, dst, next_hop,
3928 &pinfo) < 0)
3929 goto out;
3930
3931 path_idx++;
3932 }
3933
3934
3935 out:
3936 cb->args[1] = path_idx;
3937 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003938 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003939 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003940 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003941}
3942
3943static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3944{
Johannes Berg4c476992010-10-04 21:36:35 +02003945 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003946 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003947 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003948 struct mpath_info pinfo;
3949 struct sk_buff *msg;
3950 u8 *dst = NULL;
3951 u8 next_hop[ETH_ALEN];
3952
3953 memset(&pinfo, 0, sizeof(pinfo));
3954
3955 if (!info->attrs[NL80211_ATTR_MAC])
3956 return -EINVAL;
3957
3958 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3959
Johannes Berg4c476992010-10-04 21:36:35 +02003960 if (!rdev->ops->get_mpath)
3961 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003962
Johannes Berg4c476992010-10-04 21:36:35 +02003963 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3964 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003965
Hila Gonene35e4d22012-06-27 17:19:42 +03003966 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003967 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003968 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003969
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003970 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003971 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003972 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003973
Eric W. Biederman15e47302012-09-07 20:12:54 +00003974 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003975 dev, dst, next_hop, &pinfo) < 0) {
3976 nlmsg_free(msg);
3977 return -ENOBUFS;
3978 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003979
Johannes Berg4c476992010-10-04 21:36:35 +02003980 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003981}
3982
3983static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3984{
Johannes Berg4c476992010-10-04 21:36:35 +02003985 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3986 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003987 u8 *dst = NULL;
3988 u8 *next_hop = NULL;
3989
3990 if (!info->attrs[NL80211_ATTR_MAC])
3991 return -EINVAL;
3992
3993 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3994 return -EINVAL;
3995
3996 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3997 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3998
Johannes Berg4c476992010-10-04 21:36:35 +02003999 if (!rdev->ops->change_mpath)
4000 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004001
Johannes Berg4c476992010-10-04 21:36:35 +02004002 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4003 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004004
Hila Gonene35e4d22012-06-27 17:19:42 +03004005 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004006}
Johannes Berg4c476992010-10-04 21:36:35 +02004007
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004008static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4009{
Johannes Berg4c476992010-10-04 21:36:35 +02004010 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4011 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004012 u8 *dst = NULL;
4013 u8 *next_hop = NULL;
4014
4015 if (!info->attrs[NL80211_ATTR_MAC])
4016 return -EINVAL;
4017
4018 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4019 return -EINVAL;
4020
4021 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4022 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4023
Johannes Berg4c476992010-10-04 21:36:35 +02004024 if (!rdev->ops->add_mpath)
4025 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004026
Johannes Berg4c476992010-10-04 21:36:35 +02004027 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4028 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004029
Hila Gonene35e4d22012-06-27 17:19:42 +03004030 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004031}
4032
4033static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4034{
Johannes Berg4c476992010-10-04 21:36:35 +02004035 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4036 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004037 u8 *dst = NULL;
4038
4039 if (info->attrs[NL80211_ATTR_MAC])
4040 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4041
Johannes Berg4c476992010-10-04 21:36:35 +02004042 if (!rdev->ops->del_mpath)
4043 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004044
Hila Gonene35e4d22012-06-27 17:19:42 +03004045 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004046}
4047
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004048static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4049{
Johannes Berg4c476992010-10-04 21:36:35 +02004050 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4051 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004052 struct bss_parameters params;
4053
4054 memset(&params, 0, sizeof(params));
4055 /* default to not changing parameters */
4056 params.use_cts_prot = -1;
4057 params.use_short_preamble = -1;
4058 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004059 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004060 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004061 params.p2p_ctwindow = -1;
4062 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004063
4064 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4065 params.use_cts_prot =
4066 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4067 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4068 params.use_short_preamble =
4069 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4070 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4071 params.use_short_slot_time =
4072 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004073 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4074 params.basic_rates =
4075 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4076 params.basic_rates_len =
4077 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4078 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004079 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4080 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004081 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4082 params.ht_opmode =
4083 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004084
Johannes Berg53cabad2012-11-14 15:17:28 +01004085 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4086 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4087 return -EINVAL;
4088 params.p2p_ctwindow =
4089 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4090 if (params.p2p_ctwindow < 0)
4091 return -EINVAL;
4092 if (params.p2p_ctwindow != 0 &&
4093 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4094 return -EINVAL;
4095 }
4096
4097 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4098 u8 tmp;
4099
4100 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4101 return -EINVAL;
4102 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4103 if (tmp > 1)
4104 return -EINVAL;
4105 params.p2p_opp_ps = tmp;
4106 if (params.p2p_opp_ps &&
4107 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4108 return -EINVAL;
4109 }
4110
Johannes Berg4c476992010-10-04 21:36:35 +02004111 if (!rdev->ops->change_bss)
4112 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004113
Johannes Berg074ac8d2010-09-16 14:58:22 +02004114 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004115 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4116 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004117
Hila Gonene35e4d22012-06-27 17:19:42 +03004118 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004119}
4120
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004121static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004122 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4123 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4124 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4125 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4126 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4127 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4128};
4129
4130static int parse_reg_rule(struct nlattr *tb[],
4131 struct ieee80211_reg_rule *reg_rule)
4132{
4133 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4134 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4135
4136 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4137 return -EINVAL;
4138 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4139 return -EINVAL;
4140 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4141 return -EINVAL;
4142 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4143 return -EINVAL;
4144 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4145 return -EINVAL;
4146
4147 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4148
4149 freq_range->start_freq_khz =
4150 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4151 freq_range->end_freq_khz =
4152 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4153 freq_range->max_bandwidth_khz =
4154 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4155
4156 power_rule->max_eirp =
4157 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4158
4159 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4160 power_rule->max_antenna_gain =
4161 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4162
4163 return 0;
4164}
4165
4166static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4167{
4168 int r;
4169 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004170 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004171
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004172 /*
4173 * You should only get this when cfg80211 hasn't yet initialized
4174 * completely when built-in to the kernel right between the time
4175 * window between nl80211_init() and regulatory_init(), if that is
4176 * even possible.
4177 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004178 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004179 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004180
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004181 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4182 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004183
4184 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4185
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004186 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4187 user_reg_hint_type =
4188 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4189 else
4190 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4191
4192 switch (user_reg_hint_type) {
4193 case NL80211_USER_REG_HINT_USER:
4194 case NL80211_USER_REG_HINT_CELL_BASE:
4195 break;
4196 default:
4197 return -EINVAL;
4198 }
4199
4200 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004201
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004202 return r;
4203}
4204
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004205static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004206 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004207{
Johannes Berg4c476992010-10-04 21:36:35 +02004208 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004209 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004210 struct wireless_dev *wdev = dev->ieee80211_ptr;
4211 struct mesh_config cur_params;
4212 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004213 void *hdr;
4214 struct nlattr *pinfoattr;
4215 struct sk_buff *msg;
4216
Johannes Berg29cbe682010-12-03 09:20:44 +01004217 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4218 return -EOPNOTSUPP;
4219
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004220 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004221 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004222
Johannes Berg29cbe682010-12-03 09:20:44 +01004223 wdev_lock(wdev);
4224 /* If not connected, get default parameters */
4225 if (!wdev->mesh_id_len)
4226 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4227 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004228 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004229 wdev_unlock(wdev);
4230
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004231 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004232 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004233
4234 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004235 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004236 if (!msg)
4237 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004238 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004239 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004240 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004241 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004242 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004243 if (!pinfoattr)
4244 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004245 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4246 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4247 cur_params.dot11MeshRetryTimeout) ||
4248 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4249 cur_params.dot11MeshConfirmTimeout) ||
4250 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4251 cur_params.dot11MeshHoldingTimeout) ||
4252 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4253 cur_params.dot11MeshMaxPeerLinks) ||
4254 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4255 cur_params.dot11MeshMaxRetries) ||
4256 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4257 cur_params.dot11MeshTTL) ||
4258 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4259 cur_params.element_ttl) ||
4260 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4261 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004262 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4263 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004264 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4265 cur_params.dot11MeshHWMPmaxPREQretries) ||
4266 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4267 cur_params.path_refresh_time) ||
4268 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4269 cur_params.min_discovery_timeout) ||
4270 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4271 cur_params.dot11MeshHWMPactivePathTimeout) ||
4272 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4273 cur_params.dot11MeshHWMPpreqMinInterval) ||
4274 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4275 cur_params.dot11MeshHWMPperrMinInterval) ||
4276 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4277 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4278 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4279 cur_params.dot11MeshHWMPRootMode) ||
4280 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4281 cur_params.dot11MeshHWMPRannInterval) ||
4282 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4283 cur_params.dot11MeshGateAnnouncementProtocol) ||
4284 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4285 cur_params.dot11MeshForwarding) ||
4286 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004287 cur_params.rssi_threshold) ||
4288 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004289 cur_params.ht_opmode) ||
4290 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4291 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4292 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004293 cur_params.dot11MeshHWMProotInterval) ||
4294 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004295 cur_params.dot11MeshHWMPconfirmationInterval) ||
4296 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4297 cur_params.power_mode) ||
4298 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4299 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004300 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004301 nla_nest_end(msg, pinfoattr);
4302 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004303 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004304
Johannes Berg3b858752009-03-12 09:55:09 +01004305 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004306 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004307 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004308 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004309 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004310}
4311
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004312static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004313 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4314 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4315 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4316 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4317 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4318 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004319 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004320 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004321 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004322 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4323 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4324 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4325 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4326 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004327 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004328 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004329 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004330 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004331 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004332 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004333 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4334 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004335 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4336 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004337 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004338 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4339 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004340};
4341
Javier Cardonac80d5452010-12-16 17:37:49 -08004342static const struct nla_policy
4343 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004344 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004345 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4346 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004347 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004348 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004349 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004350 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004351};
4352
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004353static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004354 struct mesh_config *cfg,
4355 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004356{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004357 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004358 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004359
Marco Porschea54fba2013-01-07 16:04:48 +01004360#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4361do { \
4362 if (tb[attr]) { \
4363 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4364 return -EINVAL; \
4365 cfg->param = fn(tb[attr]); \
4366 mask |= (1 << (attr - 1)); \
4367 } \
4368} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004369
4370
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004371 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004372 return -EINVAL;
4373 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004374 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004375 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004376 return -EINVAL;
4377
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004378 /* This makes sure that there aren't more than 32 mesh config
4379 * parameters (otherwise our bitfield scheme would not work.) */
4380 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4381
4382 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004383 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004384 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4385 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004386 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004387 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4388 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004389 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004390 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4391 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004392 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004393 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4394 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004395 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004396 mask, NL80211_MESHCONF_MAX_RETRIES,
4397 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004398 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004399 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004400 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004401 mask, NL80211_MESHCONF_ELEMENT_TTL,
4402 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004403 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004404 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4405 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004406 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4407 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004408 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4409 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004410 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004411 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4412 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004413 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004414 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4415 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004416 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004417 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4418 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004419 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4420 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004421 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4422 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004423 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004424 1, 65535, mask,
4425 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004426 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004427 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004428 1, 65535, mask,
4429 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004430 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004431 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004432 dot11MeshHWMPnetDiameterTraversalTime,
4433 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004434 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4435 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004436 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4437 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4438 nla_get_u8);
4439 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4440 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004441 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004442 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004443 dot11MeshGateAnnouncementProtocol, 0, 1,
4444 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004445 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004446 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004447 mask, NL80211_MESHCONF_FORWARDING,
4448 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004449 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004450 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4451 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004452 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004453 mask, NL80211_MESHCONF_HT_OPMODE,
4454 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004455 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004456 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004457 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4458 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004459 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004460 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4461 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004462 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004463 dot11MeshHWMPconfirmationInterval,
4464 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004465 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4466 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004467 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4468 NL80211_MESH_POWER_ACTIVE,
4469 NL80211_MESH_POWER_MAX,
4470 mask, NL80211_MESHCONF_POWER_MODE,
4471 nla_get_u32);
4472 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4473 0, 65535, mask,
4474 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004475 if (mask_out)
4476 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004477
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004478 return 0;
4479
4480#undef FILL_IN_MESH_PARAM_IF_SET
4481}
4482
Javier Cardonac80d5452010-12-16 17:37:49 -08004483static int nl80211_parse_mesh_setup(struct genl_info *info,
4484 struct mesh_setup *setup)
4485{
4486 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4487
4488 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4489 return -EINVAL;
4490 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4491 info->attrs[NL80211_ATTR_MESH_SETUP],
4492 nl80211_mesh_setup_params_policy))
4493 return -EINVAL;
4494
Javier Cardonad299a1f2012-03-31 11:31:33 -07004495 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4496 setup->sync_method =
4497 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4498 IEEE80211_SYNC_METHOD_VENDOR :
4499 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4500
Javier Cardonac80d5452010-12-16 17:37:49 -08004501 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4502 setup->path_sel_proto =
4503 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4504 IEEE80211_PATH_PROTOCOL_VENDOR :
4505 IEEE80211_PATH_PROTOCOL_HWMP;
4506
4507 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4508 setup->path_metric =
4509 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4510 IEEE80211_PATH_METRIC_VENDOR :
4511 IEEE80211_PATH_METRIC_AIRTIME;
4512
Javier Cardona581a8b02011-04-07 15:08:27 -07004513
4514 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004515 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004516 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004517 if (!is_valid_ie_attr(ieattr))
4518 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004519 setup->ie = nla_data(ieattr);
4520 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004521 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004522 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4523 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004524
4525 return 0;
4526}
4527
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004528static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004529 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004530{
4531 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4532 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004533 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004534 struct mesh_config cfg;
4535 u32 mask;
4536 int err;
4537
Johannes Berg29cbe682010-12-03 09:20:44 +01004538 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4539 return -EOPNOTSUPP;
4540
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004541 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004542 return -EOPNOTSUPP;
4543
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004544 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004545 if (err)
4546 return err;
4547
Johannes Berg29cbe682010-12-03 09:20:44 +01004548 wdev_lock(wdev);
4549 if (!wdev->mesh_id_len)
4550 err = -ENOLINK;
4551
4552 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004553 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004554
4555 wdev_unlock(wdev);
4556
4557 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004558}
4559
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004560static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4561{
Johannes Berg458f4f92012-12-06 15:47:38 +01004562 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004563 struct sk_buff *msg;
4564 void *hdr = NULL;
4565 struct nlattr *nl_reg_rules;
4566 unsigned int i;
4567 int err = -EINVAL;
4568
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004569 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004570
4571 if (!cfg80211_regdomain)
4572 goto out;
4573
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004574 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004575 if (!msg) {
4576 err = -ENOBUFS;
4577 goto out;
4578 }
4579
Eric W. Biederman15e47302012-09-07 20:12:54 +00004580 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004581 NL80211_CMD_GET_REG);
4582 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004583 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004584
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004585 if (reg_last_request_cell_base() &&
4586 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4587 NL80211_USER_REG_HINT_CELL_BASE))
4588 goto nla_put_failure;
4589
Johannes Berg458f4f92012-12-06 15:47:38 +01004590 rcu_read_lock();
4591 regdom = rcu_dereference(cfg80211_regdomain);
4592
4593 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4594 (regdom->dfs_region &&
4595 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4596 goto nla_put_failure_rcu;
4597
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004598 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4599 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004600 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004601
Johannes Berg458f4f92012-12-06 15:47:38 +01004602 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004603 struct nlattr *nl_reg_rule;
4604 const struct ieee80211_reg_rule *reg_rule;
4605 const struct ieee80211_freq_range *freq_range;
4606 const struct ieee80211_power_rule *power_rule;
4607
Johannes Berg458f4f92012-12-06 15:47:38 +01004608 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004609 freq_range = &reg_rule->freq_range;
4610 power_rule = &reg_rule->power_rule;
4611
4612 nl_reg_rule = nla_nest_start(msg, i);
4613 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004614 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004615
David S. Miller9360ffd2012-03-29 04:41:26 -04004616 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4617 reg_rule->flags) ||
4618 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4619 freq_range->start_freq_khz) ||
4620 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4621 freq_range->end_freq_khz) ||
4622 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4623 freq_range->max_bandwidth_khz) ||
4624 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4625 power_rule->max_antenna_gain) ||
4626 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4627 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004628 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004629
4630 nla_nest_end(msg, nl_reg_rule);
4631 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004632 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004633
4634 nla_nest_end(msg, nl_reg_rules);
4635
4636 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004637 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004638 goto out;
4639
Johannes Berg458f4f92012-12-06 15:47:38 +01004640nla_put_failure_rcu:
4641 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004642nla_put_failure:
4643 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004644put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004645 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004646 err = -EMSGSIZE;
4647out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004648 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004649 return err;
4650}
4651
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004652static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4653{
4654 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4655 struct nlattr *nl_reg_rule;
4656 char *alpha2 = NULL;
4657 int rem_reg_rules = 0, r = 0;
4658 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004659 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004660 struct ieee80211_regdomain *rd = NULL;
4661
4662 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4663 return -EINVAL;
4664
4665 if (!info->attrs[NL80211_ATTR_REG_RULES])
4666 return -EINVAL;
4667
4668 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4669
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004670 if (info->attrs[NL80211_ATTR_DFS_REGION])
4671 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4672
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004673 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004674 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004675 num_rules++;
4676 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004677 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004678 }
4679
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004680 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004681 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004682
4683 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004684 if (!rd)
4685 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004686
4687 rd->n_reg_rules = num_rules;
4688 rd->alpha2[0] = alpha2[0];
4689 rd->alpha2[1] = alpha2[1];
4690
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004691 /*
4692 * Disable DFS master mode if the DFS region was
4693 * not supported or known on this kernel.
4694 */
4695 if (reg_supported_dfs_region(dfs_region))
4696 rd->dfs_region = dfs_region;
4697
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004698 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004699 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004700 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004701 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4702 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004703 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4704 if (r)
4705 goto bad_reg;
4706
4707 rule_idx++;
4708
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004709 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4710 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004711 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004712 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004713 }
4714
Johannes Berg6913b492012-12-04 00:48:59 +01004715 mutex_lock(&cfg80211_mutex);
4716
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004717 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004718 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004719 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004720 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004721
Johannes Bergd2372b32008-10-24 20:32:20 +02004722 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004723 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004724 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004725}
4726
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004727static int validate_scan_freqs(struct nlattr *freqs)
4728{
4729 struct nlattr *attr1, *attr2;
4730 int n_channels = 0, tmp1, tmp2;
4731
4732 nla_for_each_nested(attr1, freqs, tmp1) {
4733 n_channels++;
4734 /*
4735 * Some hardware has a limited channel list for
4736 * scanning, and it is pretty much nonsensical
4737 * to scan for a channel twice, so disallow that
4738 * and don't require drivers to check that the
4739 * channel list they get isn't longer than what
4740 * they can scan, as long as they can scan all
4741 * the channels they registered at once.
4742 */
4743 nla_for_each_nested(attr2, freqs, tmp2)
4744 if (attr1 != attr2 &&
4745 nla_get_u32(attr1) == nla_get_u32(attr2))
4746 return 0;
4747 }
4748
4749 return n_channels;
4750}
4751
Johannes Berg2a519312009-02-10 21:25:55 +01004752static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4753{
Johannes Berg4c476992010-10-04 21:36:35 +02004754 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004755 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004756 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004757 struct nlattr *attr;
4758 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004759 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004760 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004761
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004762 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4763 return -EINVAL;
4764
Johannes Berg79c97e92009-07-07 03:56:12 +02004765 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004766
Johannes Berg4c476992010-10-04 21:36:35 +02004767 if (!rdev->ops->scan)
4768 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004769
Johannes Berg4c476992010-10-04 21:36:35 +02004770 if (rdev->scan_req)
4771 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004772
4773 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004774 n_channels = validate_scan_freqs(
4775 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004776 if (!n_channels)
4777 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004778 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004779 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004780 n_channels = 0;
4781
Johannes Berg2a519312009-02-10 21:25:55 +01004782 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4783 if (wiphy->bands[band])
4784 n_channels += wiphy->bands[band]->n_channels;
4785 }
4786
4787 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4788 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4789 n_ssids++;
4790
Johannes Berg4c476992010-10-04 21:36:35 +02004791 if (n_ssids > wiphy->max_scan_ssids)
4792 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004793
Jouni Malinen70692ad2009-02-16 19:39:13 +02004794 if (info->attrs[NL80211_ATTR_IE])
4795 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4796 else
4797 ie_len = 0;
4798
Johannes Berg4c476992010-10-04 21:36:35 +02004799 if (ie_len > wiphy->max_scan_ie_len)
4800 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004801
Johannes Berg2a519312009-02-10 21:25:55 +01004802 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004803 + sizeof(*request->ssids) * n_ssids
4804 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004805 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004806 if (!request)
4807 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004808
Johannes Berg2a519312009-02-10 21:25:55 +01004809 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004810 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004811 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004812 if (ie_len) {
4813 if (request->ssids)
4814 request->ie = (void *)(request->ssids + n_ssids);
4815 else
4816 request->ie = (void *)(request->channels + n_channels);
4817 }
Johannes Berg2a519312009-02-10 21:25:55 +01004818
Johannes Berg584991d2009-11-02 13:32:03 +01004819 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004820 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4821 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004822 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004823 struct ieee80211_channel *chan;
4824
4825 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4826
4827 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004828 err = -EINVAL;
4829 goto out_free;
4830 }
Johannes Berg584991d2009-11-02 13:32:03 +01004831
4832 /* ignore disabled channels */
4833 if (chan->flags & IEEE80211_CHAN_DISABLED)
4834 continue;
4835
4836 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004837 i++;
4838 }
4839 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004840 enum ieee80211_band band;
4841
Johannes Berg2a519312009-02-10 21:25:55 +01004842 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004843 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4844 int j;
4845 if (!wiphy->bands[band])
4846 continue;
4847 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004848 struct ieee80211_channel *chan;
4849
4850 chan = &wiphy->bands[band]->channels[j];
4851
4852 if (chan->flags & IEEE80211_CHAN_DISABLED)
4853 continue;
4854
4855 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004856 i++;
4857 }
4858 }
4859 }
4860
Johannes Berg584991d2009-11-02 13:32:03 +01004861 if (!i) {
4862 err = -EINVAL;
4863 goto out_free;
4864 }
4865
4866 request->n_channels = i;
4867
Johannes Berg2a519312009-02-10 21:25:55 +01004868 i = 0;
4869 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4870 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004871 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004872 err = -EINVAL;
4873 goto out_free;
4874 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004875 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004876 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004877 i++;
4878 }
4879 }
4880
Jouni Malinen70692ad2009-02-16 19:39:13 +02004881 if (info->attrs[NL80211_ATTR_IE]) {
4882 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004883 memcpy((void *)request->ie,
4884 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004885 request->ie_len);
4886 }
4887
Johannes Berg34850ab2011-07-18 18:08:35 +02004888 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004889 if (wiphy->bands[i])
4890 request->rates[i] =
4891 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004892
4893 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4894 nla_for_each_nested(attr,
4895 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4896 tmp) {
4897 enum ieee80211_band band = nla_type(attr);
4898
Dan Carpenter84404622011-07-29 11:52:18 +03004899 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004900 err = -EINVAL;
4901 goto out_free;
4902 }
4903 err = ieee80211_get_ratemask(wiphy->bands[band],
4904 nla_data(attr),
4905 nla_len(attr),
4906 &request->rates[band]);
4907 if (err)
4908 goto out_free;
4909 }
4910 }
4911
Sam Leffler46856bb2012-10-11 21:03:32 -07004912 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004913 request->flags = nla_get_u32(
4914 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004915 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4916 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4917 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4918 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004919 err = -EOPNOTSUPP;
4920 goto out_free;
4921 }
4922 }
Sam Lefflered4737712012-10-11 21:03:31 -07004923
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304924 request->no_cck =
4925 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4926
Johannes Bergfd014282012-06-18 19:17:03 +02004927 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004928 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004929 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004930
Johannes Berg79c97e92009-07-07 03:56:12 +02004931 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004932 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004933
Johannes Berg463d0182009-07-14 00:33:35 +02004934 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004935 nl80211_send_scan_start(rdev, wdev);
4936 if (wdev->netdev)
4937 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004938 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004939 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004940 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004941 kfree(request);
4942 }
Johannes Berg3b858752009-03-12 09:55:09 +01004943
Johannes Berg2a519312009-02-10 21:25:55 +01004944 return err;
4945}
4946
Luciano Coelho807f8a82011-05-11 17:09:35 +03004947static int nl80211_start_sched_scan(struct sk_buff *skb,
4948 struct genl_info *info)
4949{
4950 struct cfg80211_sched_scan_request *request;
4951 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4952 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004953 struct nlattr *attr;
4954 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004955 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004956 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004957 enum ieee80211_band band;
4958 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004959 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004960
4961 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4962 !rdev->ops->sched_scan_start)
4963 return -EOPNOTSUPP;
4964
4965 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4966 return -EINVAL;
4967
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004968 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4969 return -EINVAL;
4970
4971 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4972 if (interval == 0)
4973 return -EINVAL;
4974
Luciano Coelho807f8a82011-05-11 17:09:35 +03004975 wiphy = &rdev->wiphy;
4976
4977 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4978 n_channels = validate_scan_freqs(
4979 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4980 if (!n_channels)
4981 return -EINVAL;
4982 } else {
4983 n_channels = 0;
4984
4985 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4986 if (wiphy->bands[band])
4987 n_channels += wiphy->bands[band]->n_channels;
4988 }
4989
4990 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4991 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4992 tmp)
4993 n_ssids++;
4994
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004995 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004996 return -EINVAL;
4997
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004998 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4999 nla_for_each_nested(attr,
5000 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5001 tmp)
5002 n_match_sets++;
5003
5004 if (n_match_sets > wiphy->max_match_sets)
5005 return -EINVAL;
5006
Luciano Coelho807f8a82011-05-11 17:09:35 +03005007 if (info->attrs[NL80211_ATTR_IE])
5008 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5009 else
5010 ie_len = 0;
5011
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005012 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005013 return -EINVAL;
5014
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005015 mutex_lock(&rdev->sched_scan_mtx);
5016
5017 if (rdev->sched_scan_req) {
5018 err = -EINPROGRESS;
5019 goto out;
5020 }
5021
Luciano Coelho807f8a82011-05-11 17:09:35 +03005022 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005023 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005024 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005025 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005026 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005027 if (!request) {
5028 err = -ENOMEM;
5029 goto out;
5030 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005031
5032 if (n_ssids)
5033 request->ssids = (void *)&request->channels[n_channels];
5034 request->n_ssids = n_ssids;
5035 if (ie_len) {
5036 if (request->ssids)
5037 request->ie = (void *)(request->ssids + n_ssids);
5038 else
5039 request->ie = (void *)(request->channels + n_channels);
5040 }
5041
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005042 if (n_match_sets) {
5043 if (request->ie)
5044 request->match_sets = (void *)(request->ie + ie_len);
5045 else if (request->ssids)
5046 request->match_sets =
5047 (void *)(request->ssids + n_ssids);
5048 else
5049 request->match_sets =
5050 (void *)(request->channels + n_channels);
5051 }
5052 request->n_match_sets = n_match_sets;
5053
Luciano Coelho807f8a82011-05-11 17:09:35 +03005054 i = 0;
5055 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5056 /* user specified, bail out if channel not found */
5057 nla_for_each_nested(attr,
5058 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5059 tmp) {
5060 struct ieee80211_channel *chan;
5061
5062 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5063
5064 if (!chan) {
5065 err = -EINVAL;
5066 goto out_free;
5067 }
5068
5069 /* ignore disabled channels */
5070 if (chan->flags & IEEE80211_CHAN_DISABLED)
5071 continue;
5072
5073 request->channels[i] = chan;
5074 i++;
5075 }
5076 } else {
5077 /* all channels */
5078 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5079 int j;
5080 if (!wiphy->bands[band])
5081 continue;
5082 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5083 struct ieee80211_channel *chan;
5084
5085 chan = &wiphy->bands[band]->channels[j];
5086
5087 if (chan->flags & IEEE80211_CHAN_DISABLED)
5088 continue;
5089
5090 request->channels[i] = chan;
5091 i++;
5092 }
5093 }
5094 }
5095
5096 if (!i) {
5097 err = -EINVAL;
5098 goto out_free;
5099 }
5100
5101 request->n_channels = i;
5102
5103 i = 0;
5104 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5105 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5106 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005107 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005108 err = -EINVAL;
5109 goto out_free;
5110 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005111 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005112 memcpy(request->ssids[i].ssid, nla_data(attr),
5113 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005114 i++;
5115 }
5116 }
5117
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005118 i = 0;
5119 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5120 nla_for_each_nested(attr,
5121 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5122 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005123 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005124
5125 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5126 nla_data(attr), nla_len(attr),
5127 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005128 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005129 if (ssid) {
5130 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5131 err = -EINVAL;
5132 goto out_free;
5133 }
5134 memcpy(request->match_sets[i].ssid.ssid,
5135 nla_data(ssid), nla_len(ssid));
5136 request->match_sets[i].ssid.ssid_len =
5137 nla_len(ssid);
5138 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005139 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5140 if (rssi)
5141 request->rssi_thold = nla_get_u32(rssi);
5142 else
5143 request->rssi_thold =
5144 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005145 i++;
5146 }
5147 }
5148
Luciano Coelho807f8a82011-05-11 17:09:35 +03005149 if (info->attrs[NL80211_ATTR_IE]) {
5150 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5151 memcpy((void *)request->ie,
5152 nla_data(info->attrs[NL80211_ATTR_IE]),
5153 request->ie_len);
5154 }
5155
Sam Leffler46856bb2012-10-11 21:03:32 -07005156 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005157 request->flags = nla_get_u32(
5158 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005159 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5160 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5161 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5162 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005163 err = -EOPNOTSUPP;
5164 goto out_free;
5165 }
5166 }
Sam Lefflered4737712012-10-11 21:03:31 -07005167
Luciano Coelho807f8a82011-05-11 17:09:35 +03005168 request->dev = dev;
5169 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005170 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005171 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005172
Hila Gonene35e4d22012-06-27 17:19:42 +03005173 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005174 if (!err) {
5175 rdev->sched_scan_req = request;
5176 nl80211_send_sched_scan(rdev, dev,
5177 NL80211_CMD_START_SCHED_SCAN);
5178 goto out;
5179 }
5180
5181out_free:
5182 kfree(request);
5183out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005184 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005185 return err;
5186}
5187
5188static int nl80211_stop_sched_scan(struct sk_buff *skb,
5189 struct genl_info *info)
5190{
5191 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005192 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005193
5194 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5195 !rdev->ops->sched_scan_stop)
5196 return -EOPNOTSUPP;
5197
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005198 mutex_lock(&rdev->sched_scan_mtx);
5199 err = __cfg80211_stop_sched_scan(rdev, false);
5200 mutex_unlock(&rdev->sched_scan_mtx);
5201
5202 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005203}
5204
Simon Wunderlich04f39042013-02-08 18:16:19 +01005205static int nl80211_start_radar_detection(struct sk_buff *skb,
5206 struct genl_info *info)
5207{
5208 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5209 struct net_device *dev = info->user_ptr[1];
5210 struct wireless_dev *wdev = dev->ieee80211_ptr;
5211 struct cfg80211_chan_def chandef;
5212 int err;
5213
5214 err = nl80211_parse_chandef(rdev, info, &chandef);
5215 if (err)
5216 return err;
5217
5218 if (wdev->cac_started)
5219 return -EBUSY;
5220
5221 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5222 if (err < 0)
5223 return err;
5224
5225 if (err == 0)
5226 return -EINVAL;
5227
5228 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5229 return -EINVAL;
5230
5231 if (!rdev->ops->start_radar_detection)
5232 return -EOPNOTSUPP;
5233
5234 mutex_lock(&rdev->devlist_mtx);
5235 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5236 chandef.chan, CHAN_MODE_SHARED,
5237 BIT(chandef.width));
5238 if (err)
5239 goto err_locked;
5240
5241 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5242 if (!err) {
5243 wdev->channel = chandef.chan;
5244 wdev->cac_started = true;
5245 wdev->cac_start_time = jiffies;
5246 }
5247err_locked:
5248 mutex_unlock(&rdev->devlist_mtx);
5249
5250 return err;
5251}
5252
Johannes Berg9720bb32011-06-21 09:45:33 +02005253static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5254 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005255 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005256 struct wireless_dev *wdev,
5257 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005258{
Johannes Berg48ab9052009-07-10 18:42:31 +02005259 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005260 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005261 void *hdr;
5262 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005263 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005264
5265 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005266
Eric W. Biederman15e47302012-09-07 20:12:54 +00005267 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005268 NL80211_CMD_NEW_SCAN_RESULTS);
5269 if (!hdr)
5270 return -1;
5271
Johannes Berg9720bb32011-06-21 09:45:33 +02005272 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5273
David S. Miller9360ffd2012-03-29 04:41:26 -04005274 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5275 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5276 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005277
5278 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5279 if (!bss)
5280 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005281 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005282 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005283 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005284
5285 rcu_read_lock();
5286 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005287 if (ies) {
5288 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5289 goto fail_unlock_rcu;
5290 tsf = true;
5291 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5292 ies->len, ies->data))
5293 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005294 }
5295 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005296 if (ies) {
5297 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5298 goto fail_unlock_rcu;
5299 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5300 ies->len, ies->data))
5301 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005302 }
5303 rcu_read_unlock();
5304
David S. Miller9360ffd2012-03-29 04:41:26 -04005305 if (res->beacon_interval &&
5306 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5307 goto nla_put_failure;
5308 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5309 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5310 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5311 jiffies_to_msecs(jiffies - intbss->ts)))
5312 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005313
Johannes Berg77965c92009-02-18 18:45:06 +01005314 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005315 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005316 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5317 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005318 break;
5319 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005320 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5321 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005322 break;
5323 default:
5324 break;
5325 }
5326
Johannes Berg48ab9052009-07-10 18:42:31 +02005327 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005328 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005329 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005330 if (intbss == wdev->current_bss &&
5331 nla_put_u32(msg, NL80211_BSS_STATUS,
5332 NL80211_BSS_STATUS_ASSOCIATED))
5333 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005334 break;
5335 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005336 if (intbss == wdev->current_bss &&
5337 nla_put_u32(msg, NL80211_BSS_STATUS,
5338 NL80211_BSS_STATUS_IBSS_JOINED))
5339 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005340 break;
5341 default:
5342 break;
5343 }
5344
Johannes Berg2a519312009-02-10 21:25:55 +01005345 nla_nest_end(msg, bss);
5346
5347 return genlmsg_end(msg, hdr);
5348
Johannes Berg8cef2c92013-02-05 16:54:31 +01005349 fail_unlock_rcu:
5350 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005351 nla_put_failure:
5352 genlmsg_cancel(msg, hdr);
5353 return -EMSGSIZE;
5354}
5355
5356static int nl80211_dump_scan(struct sk_buff *skb,
5357 struct netlink_callback *cb)
5358{
Johannes Berg48ab9052009-07-10 18:42:31 +02005359 struct cfg80211_registered_device *rdev;
5360 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005361 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005362 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005363 int start = cb->args[1], idx = 0;
5364 int err;
5365
Johannes Berg67748892010-10-04 21:14:06 +02005366 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5367 if (err)
5368 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005369
Johannes Berg48ab9052009-07-10 18:42:31 +02005370 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005371
Johannes Berg48ab9052009-07-10 18:42:31 +02005372 wdev_lock(wdev);
5373 spin_lock_bh(&rdev->bss_lock);
5374 cfg80211_bss_expire(rdev);
5375
Johannes Berg9720bb32011-06-21 09:45:33 +02005376 cb->seq = rdev->bss_generation;
5377
Johannes Berg48ab9052009-07-10 18:42:31 +02005378 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005379 if (++idx <= start)
5380 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005381 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005382 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005383 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005384 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005385 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005386 }
5387 }
5388
Johannes Berg48ab9052009-07-10 18:42:31 +02005389 spin_unlock_bh(&rdev->bss_lock);
5390 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005391
5392 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005393 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005394
Johannes Berg67748892010-10-04 21:14:06 +02005395 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005396}
5397
Eric W. Biederman15e47302012-09-07 20:12:54 +00005398static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005399 int flags, struct net_device *dev,
5400 struct survey_info *survey)
5401{
5402 void *hdr;
5403 struct nlattr *infoattr;
5404
Eric W. Biederman15e47302012-09-07 20:12:54 +00005405 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005406 NL80211_CMD_NEW_SURVEY_RESULTS);
5407 if (!hdr)
5408 return -ENOMEM;
5409
David S. Miller9360ffd2012-03-29 04:41:26 -04005410 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5411 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005412
5413 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5414 if (!infoattr)
5415 goto nla_put_failure;
5416
David S. Miller9360ffd2012-03-29 04:41:26 -04005417 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5418 survey->channel->center_freq))
5419 goto nla_put_failure;
5420
5421 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5422 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5423 goto nla_put_failure;
5424 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5425 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5426 goto nla_put_failure;
5427 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5428 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5429 survey->channel_time))
5430 goto nla_put_failure;
5431 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5432 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5433 survey->channel_time_busy))
5434 goto nla_put_failure;
5435 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5436 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5437 survey->channel_time_ext_busy))
5438 goto nla_put_failure;
5439 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5440 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5441 survey->channel_time_rx))
5442 goto nla_put_failure;
5443 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5444 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5445 survey->channel_time_tx))
5446 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005447
5448 nla_nest_end(msg, infoattr);
5449
5450 return genlmsg_end(msg, hdr);
5451
5452 nla_put_failure:
5453 genlmsg_cancel(msg, hdr);
5454 return -EMSGSIZE;
5455}
5456
5457static int nl80211_dump_survey(struct sk_buff *skb,
5458 struct netlink_callback *cb)
5459{
5460 struct survey_info survey;
5461 struct cfg80211_registered_device *dev;
5462 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005463 int survey_idx = cb->args[1];
5464 int res;
5465
Johannes Berg67748892010-10-04 21:14:06 +02005466 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5467 if (res)
5468 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005469
5470 if (!dev->ops->dump_survey) {
5471 res = -EOPNOTSUPP;
5472 goto out_err;
5473 }
5474
5475 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005476 struct ieee80211_channel *chan;
5477
Hila Gonene35e4d22012-06-27 17:19:42 +03005478 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005479 if (res == -ENOENT)
5480 break;
5481 if (res)
5482 goto out_err;
5483
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005484 /* Survey without a channel doesn't make sense */
5485 if (!survey.channel) {
5486 res = -EINVAL;
5487 goto out;
5488 }
5489
5490 chan = ieee80211_get_channel(&dev->wiphy,
5491 survey.channel->center_freq);
5492 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5493 survey_idx++;
5494 continue;
5495 }
5496
Holger Schurig61fa7132009-11-11 12:25:40 +01005497 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005498 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005499 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5500 netdev,
5501 &survey) < 0)
5502 goto out;
5503 survey_idx++;
5504 }
5505
5506 out:
5507 cb->args[1] = survey_idx;
5508 res = skb->len;
5509 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005510 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005511 return res;
5512}
5513
Samuel Ortizb23aa672009-07-01 21:26:54 +02005514static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5515{
5516 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5517 NL80211_WPA_VERSION_2));
5518}
5519
Jouni Malinen636a5d32009-03-19 13:39:22 +02005520static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5521{
Johannes Berg4c476992010-10-04 21:36:35 +02005522 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5523 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005524 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005525 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5526 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005527 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005528 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005529 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005530
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005531 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5532 return -EINVAL;
5533
5534 if (!info->attrs[NL80211_ATTR_MAC])
5535 return -EINVAL;
5536
Jouni Malinen17780922009-03-27 20:52:47 +02005537 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5538 return -EINVAL;
5539
Johannes Berg19957bb2009-07-02 17:20:43 +02005540 if (!info->attrs[NL80211_ATTR_SSID])
5541 return -EINVAL;
5542
5543 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5544 return -EINVAL;
5545
Johannes Bergfffd0932009-07-08 14:22:54 +02005546 err = nl80211_parse_key(info, &key);
5547 if (err)
5548 return err;
5549
5550 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005551 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5552 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005553 if (!key.p.key || !key.p.key_len)
5554 return -EINVAL;
5555 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5556 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5557 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5558 key.p.key_len != WLAN_KEY_LEN_WEP104))
5559 return -EINVAL;
5560 if (key.idx > 4)
5561 return -EINVAL;
5562 } else {
5563 key.p.key_len = 0;
5564 key.p.key = NULL;
5565 }
5566
Johannes Bergafea0b72010-08-10 09:46:42 +02005567 if (key.idx >= 0) {
5568 int i;
5569 bool ok = false;
5570 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5571 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5572 ok = true;
5573 break;
5574 }
5575 }
Johannes Berg4c476992010-10-04 21:36:35 +02005576 if (!ok)
5577 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005578 }
5579
Johannes Berg4c476992010-10-04 21:36:35 +02005580 if (!rdev->ops->auth)
5581 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005582
Johannes Berg074ac8d2010-09-16 14:58:22 +02005583 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005584 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5585 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005586
Johannes Berg19957bb2009-07-02 17:20:43 +02005587 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005588 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005589 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005590 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5591 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005592
Johannes Berg19957bb2009-07-02 17:20:43 +02005593 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5594 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5595
5596 if (info->attrs[NL80211_ATTR_IE]) {
5597 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5598 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5599 }
5600
5601 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005602 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005603 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005604
Jouni Malinene39e5b52012-09-30 19:29:39 +03005605 if (auth_type == NL80211_AUTHTYPE_SAE &&
5606 !info->attrs[NL80211_ATTR_SAE_DATA])
5607 return -EINVAL;
5608
5609 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5610 if (auth_type != NL80211_AUTHTYPE_SAE)
5611 return -EINVAL;
5612 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5613 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5614 /* need to include at least Auth Transaction and Status Code */
5615 if (sae_data_len < 4)
5616 return -EINVAL;
5617 }
5618
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005619 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5620
Johannes Berg95de8172012-01-20 13:55:25 +01005621 /*
5622 * Since we no longer track auth state, ignore
5623 * requests to only change local state.
5624 */
5625 if (local_state_change)
5626 return 0;
5627
Johannes Berg4c476992010-10-04 21:36:35 +02005628 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5629 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005630 key.p.key, key.p.key_len, key.idx,
5631 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005632}
5633
Johannes Bergc0692b82010-08-27 14:26:53 +03005634static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5635 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005636 struct cfg80211_crypto_settings *settings,
5637 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005638{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005639 memset(settings, 0, sizeof(*settings));
5640
Samuel Ortizb23aa672009-07-01 21:26:54 +02005641 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5642
Johannes Bergc0692b82010-08-27 14:26:53 +03005643 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5644 u16 proto;
5645 proto = nla_get_u16(
5646 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5647 settings->control_port_ethertype = cpu_to_be16(proto);
5648 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5649 proto != ETH_P_PAE)
5650 return -EINVAL;
5651 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5652 settings->control_port_no_encrypt = true;
5653 } else
5654 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5655
Samuel Ortizb23aa672009-07-01 21:26:54 +02005656 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5657 void *data;
5658 int len, i;
5659
5660 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5661 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5662 settings->n_ciphers_pairwise = len / sizeof(u32);
5663
5664 if (len % sizeof(u32))
5665 return -EINVAL;
5666
Johannes Berg3dc27d22009-07-02 21:36:37 +02005667 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005668 return -EINVAL;
5669
5670 memcpy(settings->ciphers_pairwise, data, len);
5671
5672 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005673 if (!cfg80211_supported_cipher_suite(
5674 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005675 settings->ciphers_pairwise[i]))
5676 return -EINVAL;
5677 }
5678
5679 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5680 settings->cipher_group =
5681 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005682 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5683 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005684 return -EINVAL;
5685 }
5686
5687 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5688 settings->wpa_versions =
5689 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5690 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5691 return -EINVAL;
5692 }
5693
5694 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5695 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005696 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005697
5698 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5699 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5700 settings->n_akm_suites = len / sizeof(u32);
5701
5702 if (len % sizeof(u32))
5703 return -EINVAL;
5704
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005705 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5706 return -EINVAL;
5707
Samuel Ortizb23aa672009-07-01 21:26:54 +02005708 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005709 }
5710
5711 return 0;
5712}
5713
Jouni Malinen636a5d32009-03-19 13:39:22 +02005714static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5715{
Johannes Berg4c476992010-10-04 21:36:35 +02005716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5717 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005718 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005719 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005720 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005721 int err, ssid_len, ie_len = 0;
5722 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005723 u32 flags = 0;
5724 struct ieee80211_ht_cap *ht_capa = NULL;
5725 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005726
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005727 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5728 return -EINVAL;
5729
5730 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005731 !info->attrs[NL80211_ATTR_SSID] ||
5732 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005733 return -EINVAL;
5734
Johannes Berg4c476992010-10-04 21:36:35 +02005735 if (!rdev->ops->assoc)
5736 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005737
Johannes Berg074ac8d2010-09-16 14:58:22 +02005738 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005739 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5740 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005741
Johannes Berg19957bb2009-07-02 17:20:43 +02005742 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005743
Johannes Berg19957bb2009-07-02 17:20:43 +02005744 chan = ieee80211_get_channel(&rdev->wiphy,
5745 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005746 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5747 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005748
Johannes Berg19957bb2009-07-02 17:20:43 +02005749 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5750 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005751
5752 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005753 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5754 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005755 }
5756
Jouni Malinendc6382c2009-05-06 22:09:37 +03005757 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005758 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005759 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005760 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005761 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005762 else if (mfp != NL80211_MFP_NO)
5763 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005764 }
5765
Johannes Berg3e5d7642009-07-07 14:37:26 +02005766 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5767 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5768
Ben Greear7e7c8922011-11-18 11:31:59 -08005769 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5770 flags |= ASSOC_REQ_DISABLE_HT;
5771
5772 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5773 ht_capa_mask =
5774 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5775
5776 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5777 if (!ht_capa_mask)
5778 return -EINVAL;
5779 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5780 }
5781
Johannes Bergc0692b82010-08-27 14:26:53 +03005782 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005783 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005784 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5785 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005786 &crypto, flags, ht_capa,
5787 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005788
Jouni Malinen636a5d32009-03-19 13:39:22 +02005789 return err;
5790}
5791
5792static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5793{
Johannes Berg4c476992010-10-04 21:36:35 +02005794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5795 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005796 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005797 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005798 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005799 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005800
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005801 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5802 return -EINVAL;
5803
5804 if (!info->attrs[NL80211_ATTR_MAC])
5805 return -EINVAL;
5806
5807 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5808 return -EINVAL;
5809
Johannes Berg4c476992010-10-04 21:36:35 +02005810 if (!rdev->ops->deauth)
5811 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005812
Johannes Berg074ac8d2010-09-16 14:58:22 +02005813 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005814 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5815 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005816
Johannes Berg19957bb2009-07-02 17:20:43 +02005817 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005818
Johannes Berg19957bb2009-07-02 17:20:43 +02005819 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5820 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005821 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005822 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005823 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005824
5825 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005826 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5827 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005828 }
5829
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005830 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5831
Johannes Berg4c476992010-10-04 21:36:35 +02005832 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5833 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005834}
5835
5836static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5837{
Johannes Berg4c476992010-10-04 21:36:35 +02005838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5839 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005840 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005841 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005842 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005843 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005844
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005845 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5846 return -EINVAL;
5847
5848 if (!info->attrs[NL80211_ATTR_MAC])
5849 return -EINVAL;
5850
5851 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5852 return -EINVAL;
5853
Johannes Berg4c476992010-10-04 21:36:35 +02005854 if (!rdev->ops->disassoc)
5855 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005856
Johannes Berg074ac8d2010-09-16 14:58:22 +02005857 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005858 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5859 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005860
Johannes Berg19957bb2009-07-02 17:20:43 +02005861 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005862
Johannes Berg19957bb2009-07-02 17:20:43 +02005863 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5864 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005865 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005866 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005867 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005868
5869 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005870 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5871 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005872 }
5873
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005874 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5875
Johannes Berg4c476992010-10-04 21:36:35 +02005876 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5877 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005878}
5879
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005880static bool
5881nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5882 int mcast_rate[IEEE80211_NUM_BANDS],
5883 int rateval)
5884{
5885 struct wiphy *wiphy = &rdev->wiphy;
5886 bool found = false;
5887 int band, i;
5888
5889 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5890 struct ieee80211_supported_band *sband;
5891
5892 sband = wiphy->bands[band];
5893 if (!sband)
5894 continue;
5895
5896 for (i = 0; i < sband->n_bitrates; i++) {
5897 if (sband->bitrates[i].bitrate == rateval) {
5898 mcast_rate[band] = i + 1;
5899 found = true;
5900 break;
5901 }
5902 }
5903 }
5904
5905 return found;
5906}
5907
Johannes Berg04a773a2009-04-19 21:24:32 +02005908static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5909{
Johannes Berg4c476992010-10-04 21:36:35 +02005910 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5911 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005912 struct cfg80211_ibss_params ibss;
5913 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005914 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005915 int err;
5916
Johannes Berg8e30bc52009-04-22 17:45:38 +02005917 memset(&ibss, 0, sizeof(ibss));
5918
Johannes Berg04a773a2009-04-19 21:24:32 +02005919 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5920 return -EINVAL;
5921
Johannes Berg683b6d32012-11-08 21:25:48 +01005922 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005923 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5924 return -EINVAL;
5925
Johannes Berg8e30bc52009-04-22 17:45:38 +02005926 ibss.beacon_interval = 100;
5927
5928 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5929 ibss.beacon_interval =
5930 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5931 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5932 return -EINVAL;
5933 }
5934
Johannes Berg4c476992010-10-04 21:36:35 +02005935 if (!rdev->ops->join_ibss)
5936 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005937
Johannes Berg4c476992010-10-04 21:36:35 +02005938 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5939 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005940
Johannes Berg79c97e92009-07-07 03:56:12 +02005941 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005942
Johannes Berg39193492011-09-16 13:45:25 +02005943 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005944 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005945
5946 if (!is_valid_ether_addr(ibss.bssid))
5947 return -EINVAL;
5948 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005949 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5950 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5951
5952 if (info->attrs[NL80211_ATTR_IE]) {
5953 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5954 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5955 }
5956
Johannes Berg683b6d32012-11-08 21:25:48 +01005957 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5958 if (err)
5959 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005960
Johannes Berg683b6d32012-11-08 21:25:48 +01005961 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005962 return -EINVAL;
5963
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005964 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5965 return -EINVAL;
5966 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5967 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005968 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005969
Johannes Berg04a773a2009-04-19 21:24:32 +02005970 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005971 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005972
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005973 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5974 u8 *rates =
5975 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5976 int n_rates =
5977 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5978 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005979 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005980
Johannes Berg34850ab2011-07-18 18:08:35 +02005981 err = ieee80211_get_ratemask(sband, rates, n_rates,
5982 &ibss.basic_rates);
5983 if (err)
5984 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005985 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005986
5987 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5988 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5989 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5990 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005991
Johannes Berg4c476992010-10-04 21:36:35 +02005992 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305993 bool no_ht = false;
5994
Johannes Berg4c476992010-10-04 21:36:35 +02005995 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305996 info->attrs[NL80211_ATTR_KEYS],
5997 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005998 if (IS_ERR(connkeys))
5999 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306000
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006001 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6002 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306003 kfree(connkeys);
6004 return -EINVAL;
6005 }
Johannes Berg4c476992010-10-04 21:36:35 +02006006 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006007
Antonio Quartulli267335d2012-01-31 20:25:47 +01006008 ibss.control_port =
6009 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6010
Johannes Berg4c476992010-10-04 21:36:35 +02006011 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006012 if (err)
6013 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006014 return err;
6015}
6016
6017static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6018{
Johannes Berg4c476992010-10-04 21:36:35 +02006019 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6020 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006021
Johannes Berg4c476992010-10-04 21:36:35 +02006022 if (!rdev->ops->leave_ibss)
6023 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006024
Johannes Berg4c476992010-10-04 21:36:35 +02006025 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6026 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006027
Johannes Berg4c476992010-10-04 21:36:35 +02006028 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006029}
6030
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006031static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6032{
6033 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6034 struct net_device *dev = info->user_ptr[1];
6035 int mcast_rate[IEEE80211_NUM_BANDS];
6036 u32 nla_rate;
6037 int err;
6038
6039 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6040 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6041 return -EOPNOTSUPP;
6042
6043 if (!rdev->ops->set_mcast_rate)
6044 return -EOPNOTSUPP;
6045
6046 memset(mcast_rate, 0, sizeof(mcast_rate));
6047
6048 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6049 return -EINVAL;
6050
6051 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6052 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6053 return -EINVAL;
6054
6055 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6056
6057 return err;
6058}
6059
6060
Johannes Bergaff89a92009-07-01 21:26:51 +02006061#ifdef CONFIG_NL80211_TESTMODE
6062static struct genl_multicast_group nl80211_testmode_mcgrp = {
6063 .name = "testmode",
6064};
6065
6066static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6067{
Johannes Berg4c476992010-10-04 21:36:35 +02006068 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006069 int err;
6070
6071 if (!info->attrs[NL80211_ATTR_TESTDATA])
6072 return -EINVAL;
6073
Johannes Bergaff89a92009-07-01 21:26:51 +02006074 err = -EOPNOTSUPP;
6075 if (rdev->ops->testmode_cmd) {
6076 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006077 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006078 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6079 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6080 rdev->testmode_info = NULL;
6081 }
6082
Johannes Bergaff89a92009-07-01 21:26:51 +02006083 return err;
6084}
6085
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006086static int nl80211_testmode_dump(struct sk_buff *skb,
6087 struct netlink_callback *cb)
6088{
Johannes Berg00918d32011-12-13 17:22:05 +01006089 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006090 int err;
6091 long phy_idx;
6092 void *data = NULL;
6093 int data_len = 0;
6094
6095 if (cb->args[0]) {
6096 /*
6097 * 0 is a valid index, but not valid for args[0],
6098 * so we need to offset by 1.
6099 */
6100 phy_idx = cb->args[0] - 1;
6101 } else {
6102 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6103 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6104 nl80211_policy);
6105 if (err)
6106 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006107
Johannes Berg2bd7e352012-06-15 14:23:16 +02006108 mutex_lock(&cfg80211_mutex);
6109 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6110 nl80211_fam.attrbuf);
6111 if (IS_ERR(rdev)) {
6112 mutex_unlock(&cfg80211_mutex);
6113 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006114 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006115 phy_idx = rdev->wiphy_idx;
6116 rdev = NULL;
6117 mutex_unlock(&cfg80211_mutex);
6118
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006119 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6120 cb->args[1] =
6121 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6122 }
6123
6124 if (cb->args[1]) {
6125 data = nla_data((void *)cb->args[1]);
6126 data_len = nla_len((void *)cb->args[1]);
6127 }
6128
6129 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006130 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6131 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006132 mutex_unlock(&cfg80211_mutex);
6133 return -ENOENT;
6134 }
Johannes Berg00918d32011-12-13 17:22:05 +01006135 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006136 mutex_unlock(&cfg80211_mutex);
6137
Johannes Berg00918d32011-12-13 17:22:05 +01006138 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006139 err = -EOPNOTSUPP;
6140 goto out_err;
6141 }
6142
6143 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006144 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006145 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6146 NL80211_CMD_TESTMODE);
6147 struct nlattr *tmdata;
6148
David S. Miller9360ffd2012-03-29 04:41:26 -04006149 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006150 genlmsg_cancel(skb, hdr);
6151 break;
6152 }
6153
6154 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6155 if (!tmdata) {
6156 genlmsg_cancel(skb, hdr);
6157 break;
6158 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006159 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006160 nla_nest_end(skb, tmdata);
6161
6162 if (err == -ENOBUFS || err == -ENOENT) {
6163 genlmsg_cancel(skb, hdr);
6164 break;
6165 } else if (err) {
6166 genlmsg_cancel(skb, hdr);
6167 goto out_err;
6168 }
6169
6170 genlmsg_end(skb, hdr);
6171 }
6172
6173 err = skb->len;
6174 /* see above */
6175 cb->args[0] = phy_idx + 1;
6176 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006177 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006178 return err;
6179}
6180
Johannes Bergaff89a92009-07-01 21:26:51 +02006181static struct sk_buff *
6182__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006183 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006184{
6185 struct sk_buff *skb;
6186 void *hdr;
6187 struct nlattr *data;
6188
6189 skb = nlmsg_new(approxlen + 100, gfp);
6190 if (!skb)
6191 return NULL;
6192
Eric W. Biederman15e47302012-09-07 20:12:54 +00006193 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006194 if (!hdr) {
6195 kfree_skb(skb);
6196 return NULL;
6197 }
6198
David S. Miller9360ffd2012-03-29 04:41:26 -04006199 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6200 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006201 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6202
6203 ((void **)skb->cb)[0] = rdev;
6204 ((void **)skb->cb)[1] = hdr;
6205 ((void **)skb->cb)[2] = data;
6206
6207 return skb;
6208
6209 nla_put_failure:
6210 kfree_skb(skb);
6211 return NULL;
6212}
6213
6214struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6215 int approxlen)
6216{
6217 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6218
6219 if (WARN_ON(!rdev->testmode_info))
6220 return NULL;
6221
6222 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006223 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006224 rdev->testmode_info->snd_seq,
6225 GFP_KERNEL);
6226}
6227EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6228
6229int cfg80211_testmode_reply(struct sk_buff *skb)
6230{
6231 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6232 void *hdr = ((void **)skb->cb)[1];
6233 struct nlattr *data = ((void **)skb->cb)[2];
6234
6235 if (WARN_ON(!rdev->testmode_info)) {
6236 kfree_skb(skb);
6237 return -EINVAL;
6238 }
6239
6240 nla_nest_end(skb, data);
6241 genlmsg_end(skb, hdr);
6242 return genlmsg_reply(skb, rdev->testmode_info);
6243}
6244EXPORT_SYMBOL(cfg80211_testmode_reply);
6245
6246struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6247 int approxlen, gfp_t gfp)
6248{
6249 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6250
6251 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6252}
6253EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6254
6255void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6256{
6257 void *hdr = ((void **)skb->cb)[1];
6258 struct nlattr *data = ((void **)skb->cb)[2];
6259
6260 nla_nest_end(skb, data);
6261 genlmsg_end(skb, hdr);
6262 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6263}
6264EXPORT_SYMBOL(cfg80211_testmode_event);
6265#endif
6266
Samuel Ortizb23aa672009-07-01 21:26:54 +02006267static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6268{
Johannes Berg4c476992010-10-04 21:36:35 +02006269 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6270 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006271 struct cfg80211_connect_params connect;
6272 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006273 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006274 int err;
6275
6276 memset(&connect, 0, sizeof(connect));
6277
6278 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6279 return -EINVAL;
6280
6281 if (!info->attrs[NL80211_ATTR_SSID] ||
6282 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6283 return -EINVAL;
6284
6285 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6286 connect.auth_type =
6287 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006288 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6289 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006290 return -EINVAL;
6291 } else
6292 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6293
6294 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6295
Johannes Bergc0692b82010-08-27 14:26:53 +03006296 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006297 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006298 if (err)
6299 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006300
Johannes Berg074ac8d2010-09-16 14:58:22 +02006301 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006302 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6303 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006304
Johannes Berg79c97e92009-07-07 03:56:12 +02006305 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006306
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306307 connect.bg_scan_period = -1;
6308 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6309 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6310 connect.bg_scan_period =
6311 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6312 }
6313
Samuel Ortizb23aa672009-07-01 21:26:54 +02006314 if (info->attrs[NL80211_ATTR_MAC])
6315 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6316 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6317 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6318
6319 if (info->attrs[NL80211_ATTR_IE]) {
6320 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6321 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6322 }
6323
Jouni Malinencee00a92013-01-15 17:15:57 +02006324 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6325 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6326 if (connect.mfp != NL80211_MFP_REQUIRED &&
6327 connect.mfp != NL80211_MFP_NO)
6328 return -EINVAL;
6329 } else {
6330 connect.mfp = NL80211_MFP_NO;
6331 }
6332
Samuel Ortizb23aa672009-07-01 21:26:54 +02006333 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6334 connect.channel =
6335 ieee80211_get_channel(wiphy,
6336 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6337 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006338 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6339 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006340 }
6341
Johannes Bergfffd0932009-07-08 14:22:54 +02006342 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6343 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306344 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006345 if (IS_ERR(connkeys))
6346 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006347 }
6348
Ben Greear7e7c8922011-11-18 11:31:59 -08006349 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6350 connect.flags |= ASSOC_REQ_DISABLE_HT;
6351
6352 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6353 memcpy(&connect.ht_capa_mask,
6354 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6355 sizeof(connect.ht_capa_mask));
6356
6357 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006358 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6359 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006360 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006361 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006362 memcpy(&connect.ht_capa,
6363 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6364 sizeof(connect.ht_capa));
6365 }
6366
Johannes Bergfffd0932009-07-08 14:22:54 +02006367 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006368 if (err)
6369 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006370 return err;
6371}
6372
6373static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6374{
Johannes Berg4c476992010-10-04 21:36:35 +02006375 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6376 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006377 u16 reason;
6378
6379 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6380 reason = WLAN_REASON_DEAUTH_LEAVING;
6381 else
6382 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6383
6384 if (reason == 0)
6385 return -EINVAL;
6386
Johannes Berg074ac8d2010-09-16 14:58:22 +02006387 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006388 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6389 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006390
Johannes Berg4c476992010-10-04 21:36:35 +02006391 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006392}
6393
Johannes Berg463d0182009-07-14 00:33:35 +02006394static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6395{
Johannes Berg4c476992010-10-04 21:36:35 +02006396 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006397 struct net *net;
6398 int err;
6399 u32 pid;
6400
6401 if (!info->attrs[NL80211_ATTR_PID])
6402 return -EINVAL;
6403
6404 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6405
Johannes Berg463d0182009-07-14 00:33:35 +02006406 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006407 if (IS_ERR(net))
6408 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006409
6410 err = 0;
6411
6412 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006413 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6414 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006415
Johannes Berg463d0182009-07-14 00:33:35 +02006416 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006417 return err;
6418}
6419
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006420static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6421{
Johannes Berg4c476992010-10-04 21:36:35 +02006422 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006423 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6424 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006425 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006426 struct cfg80211_pmksa pmksa;
6427
6428 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6429
6430 if (!info->attrs[NL80211_ATTR_MAC])
6431 return -EINVAL;
6432
6433 if (!info->attrs[NL80211_ATTR_PMKID])
6434 return -EINVAL;
6435
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006436 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6437 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6438
Johannes Berg074ac8d2010-09-16 14:58:22 +02006439 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006440 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6441 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006442
6443 switch (info->genlhdr->cmd) {
6444 case NL80211_CMD_SET_PMKSA:
6445 rdev_ops = rdev->ops->set_pmksa;
6446 break;
6447 case NL80211_CMD_DEL_PMKSA:
6448 rdev_ops = rdev->ops->del_pmksa;
6449 break;
6450 default:
6451 WARN_ON(1);
6452 break;
6453 }
6454
Johannes Berg4c476992010-10-04 21:36:35 +02006455 if (!rdev_ops)
6456 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006457
Johannes Berg4c476992010-10-04 21:36:35 +02006458 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006459}
6460
6461static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6462{
Johannes Berg4c476992010-10-04 21:36:35 +02006463 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6464 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006465
Johannes Berg074ac8d2010-09-16 14:58:22 +02006466 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006467 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6468 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006469
Johannes Berg4c476992010-10-04 21:36:35 +02006470 if (!rdev->ops->flush_pmksa)
6471 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006472
Hila Gonene35e4d22012-06-27 17:19:42 +03006473 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006474}
6475
Arik Nemtsov109086c2011-09-28 14:12:50 +03006476static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6477{
6478 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6479 struct net_device *dev = info->user_ptr[1];
6480 u8 action_code, dialog_token;
6481 u16 status_code;
6482 u8 *peer;
6483
6484 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6485 !rdev->ops->tdls_mgmt)
6486 return -EOPNOTSUPP;
6487
6488 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6489 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6490 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6491 !info->attrs[NL80211_ATTR_IE] ||
6492 !info->attrs[NL80211_ATTR_MAC])
6493 return -EINVAL;
6494
6495 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6496 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6497 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6498 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6499
Hila Gonene35e4d22012-06-27 17:19:42 +03006500 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6501 dialog_token, status_code,
6502 nla_data(info->attrs[NL80211_ATTR_IE]),
6503 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006504}
6505
6506static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6507{
6508 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6509 struct net_device *dev = info->user_ptr[1];
6510 enum nl80211_tdls_operation operation;
6511 u8 *peer;
6512
6513 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6514 !rdev->ops->tdls_oper)
6515 return -EOPNOTSUPP;
6516
6517 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6518 !info->attrs[NL80211_ATTR_MAC])
6519 return -EINVAL;
6520
6521 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6522 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6523
Hila Gonene35e4d22012-06-27 17:19:42 +03006524 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006525}
6526
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006527static int nl80211_remain_on_channel(struct sk_buff *skb,
6528 struct genl_info *info)
6529{
Johannes Berg4c476992010-10-04 21:36:35 +02006530 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006531 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006532 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006533 struct sk_buff *msg;
6534 void *hdr;
6535 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006536 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006537 int err;
6538
6539 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6540 !info->attrs[NL80211_ATTR_DURATION])
6541 return -EINVAL;
6542
6543 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6544
Johannes Berg7c4ef712011-11-18 15:33:48 +01006545 if (!rdev->ops->remain_on_channel ||
6546 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006547 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006548
Johannes Bergebf348f2012-06-01 12:50:54 +02006549 /*
6550 * We should be on that channel for at least a minimum amount of
6551 * time (10ms) but no longer than the driver supports.
6552 */
6553 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6554 duration > rdev->wiphy.max_remain_on_channel_duration)
6555 return -EINVAL;
6556
Johannes Berg683b6d32012-11-08 21:25:48 +01006557 err = nl80211_parse_chandef(rdev, info, &chandef);
6558 if (err)
6559 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006560
6561 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006562 if (!msg)
6563 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006564
Eric W. Biederman15e47302012-09-07 20:12:54 +00006565 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006566 NL80211_CMD_REMAIN_ON_CHANNEL);
6567
6568 if (IS_ERR(hdr)) {
6569 err = PTR_ERR(hdr);
6570 goto free_msg;
6571 }
6572
Johannes Berg683b6d32012-11-08 21:25:48 +01006573 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6574 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006575
6576 if (err)
6577 goto free_msg;
6578
David S. Miller9360ffd2012-03-29 04:41:26 -04006579 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6580 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006581
6582 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006583
6584 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006585
6586 nla_put_failure:
6587 err = -ENOBUFS;
6588 free_msg:
6589 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006590 return err;
6591}
6592
6593static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6594 struct genl_info *info)
6595{
Johannes Berg4c476992010-10-04 21:36:35 +02006596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006597 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006598 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006599
6600 if (!info->attrs[NL80211_ATTR_COOKIE])
6601 return -EINVAL;
6602
Johannes Berg4c476992010-10-04 21:36:35 +02006603 if (!rdev->ops->cancel_remain_on_channel)
6604 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006605
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006606 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6607
Hila Gonene35e4d22012-06-27 17:19:42 +03006608 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006609}
6610
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006611static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6612 u8 *rates, u8 rates_len)
6613{
6614 u8 i;
6615 u32 mask = 0;
6616
6617 for (i = 0; i < rates_len; i++) {
6618 int rate = (rates[i] & 0x7f) * 5;
6619 int ridx;
6620 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6621 struct ieee80211_rate *srate =
6622 &sband->bitrates[ridx];
6623 if (rate == srate->bitrate) {
6624 mask |= 1 << ridx;
6625 break;
6626 }
6627 }
6628 if (ridx == sband->n_bitrates)
6629 return 0; /* rate not found */
6630 }
6631
6632 return mask;
6633}
6634
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006635static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6636 u8 *rates, u8 rates_len,
6637 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6638{
6639 u8 i;
6640
6641 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6642
6643 for (i = 0; i < rates_len; i++) {
6644 int ridx, rbit;
6645
6646 ridx = rates[i] / 8;
6647 rbit = BIT(rates[i] % 8);
6648
6649 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006650 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006651 return false;
6652
6653 /* check availability */
6654 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6655 mcs[ridx] |= rbit;
6656 else
6657 return false;
6658 }
6659
6660 return true;
6661}
6662
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006663static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006664 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6665 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006666 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6667 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006668};
6669
6670static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6671 struct genl_info *info)
6672{
6673 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006674 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006675 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006676 int rem, i;
6677 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006678 struct nlattr *tx_rates;
6679 struct ieee80211_supported_band *sband;
6680
6681 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6682 return -EINVAL;
6683
Johannes Berg4c476992010-10-04 21:36:35 +02006684 if (!rdev->ops->set_bitrate_mask)
6685 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006686
6687 memset(&mask, 0, sizeof(mask));
6688 /* Default to all rates enabled */
6689 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6690 sband = rdev->wiphy.bands[i];
6691 mask.control[i].legacy =
6692 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006693 if (sband)
6694 memcpy(mask.control[i].mcs,
6695 sband->ht_cap.mcs.rx_mask,
6696 sizeof(mask.control[i].mcs));
6697 else
6698 memset(mask.control[i].mcs, 0,
6699 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006700 }
6701
6702 /*
6703 * The nested attribute uses enum nl80211_band as the index. This maps
6704 * directly to the enum ieee80211_band values used in cfg80211.
6705 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006706 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006707 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6708 {
6709 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006710 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6711 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006712 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006713 if (sband == NULL)
6714 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006715 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6716 nla_len(tx_rates), nl80211_txattr_policy);
6717 if (tb[NL80211_TXRATE_LEGACY]) {
6718 mask.control[band].legacy = rateset_to_mask(
6719 sband,
6720 nla_data(tb[NL80211_TXRATE_LEGACY]),
6721 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306722 if ((mask.control[band].legacy == 0) &&
6723 nla_len(tb[NL80211_TXRATE_LEGACY]))
6724 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006725 }
6726 if (tb[NL80211_TXRATE_MCS]) {
6727 if (!ht_rateset_to_mask(
6728 sband,
6729 nla_data(tb[NL80211_TXRATE_MCS]),
6730 nla_len(tb[NL80211_TXRATE_MCS]),
6731 mask.control[band].mcs))
6732 return -EINVAL;
6733 }
6734
6735 if (mask.control[band].legacy == 0) {
6736 /* don't allow empty legacy rates if HT
6737 * is not even supported. */
6738 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6739 return -EINVAL;
6740
6741 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6742 if (mask.control[band].mcs[i])
6743 break;
6744
6745 /* legacy and mcs rates may not be both empty */
6746 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006747 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006748 }
6749 }
6750
Hila Gonene35e4d22012-06-27 17:19:42 +03006751 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006752}
6753
Johannes Berg2e161f72010-08-12 15:38:38 +02006754static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006755{
Johannes Berg4c476992010-10-04 21:36:35 +02006756 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006757 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006758 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006759
6760 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6761 return -EINVAL;
6762
Johannes Berg2e161f72010-08-12 15:38:38 +02006763 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6764 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006765
Johannes Berg71bbc992012-06-15 15:30:18 +02006766 switch (wdev->iftype) {
6767 case NL80211_IFTYPE_STATION:
6768 case NL80211_IFTYPE_ADHOC:
6769 case NL80211_IFTYPE_P2P_CLIENT:
6770 case NL80211_IFTYPE_AP:
6771 case NL80211_IFTYPE_AP_VLAN:
6772 case NL80211_IFTYPE_MESH_POINT:
6773 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006774 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006775 break;
6776 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006777 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006778 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006779
6780 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006781 if (!rdev->ops->mgmt_tx)
6782 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006783
Eric W. Biederman15e47302012-09-07 20:12:54 +00006784 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006785 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6786 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006787}
6788
Johannes Berg2e161f72010-08-12 15:38:38 +02006789static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006790{
Johannes Berg4c476992010-10-04 21:36:35 +02006791 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006792 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006793 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006794 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006795 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006796 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006797 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006798 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006799 bool offchan, no_cck, dont_wait_for_ack;
6800
6801 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006802
Johannes Berg683b6d32012-11-08 21:25:48 +01006803 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006804 return -EINVAL;
6805
Johannes Berg4c476992010-10-04 21:36:35 +02006806 if (!rdev->ops->mgmt_tx)
6807 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006808
Johannes Berg71bbc992012-06-15 15:30:18 +02006809 switch (wdev->iftype) {
6810 case NL80211_IFTYPE_STATION:
6811 case NL80211_IFTYPE_ADHOC:
6812 case NL80211_IFTYPE_P2P_CLIENT:
6813 case NL80211_IFTYPE_AP:
6814 case NL80211_IFTYPE_AP_VLAN:
6815 case NL80211_IFTYPE_MESH_POINT:
6816 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006817 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006818 break;
6819 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006820 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006821 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006822
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006823 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006824 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006825 return -EINVAL;
6826 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006827
6828 /*
6829 * We should wait on the channel for at least a minimum amount
6830 * of time (10ms) but no longer than the driver supports.
6831 */
6832 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6833 wait > rdev->wiphy.max_remain_on_channel_duration)
6834 return -EINVAL;
6835
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006836 }
6837
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006838 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6839
Johannes Berg7c4ef712011-11-18 15:33:48 +01006840 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6841 return -EINVAL;
6842
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306843 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6844
Johannes Berg683b6d32012-11-08 21:25:48 +01006845 err = nl80211_parse_chandef(rdev, info, &chandef);
6846 if (err)
6847 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006848
Johannes Berge247bd902011-11-04 11:18:21 +01006849 if (!dont_wait_for_ack) {
6850 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6851 if (!msg)
6852 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006853
Eric W. Biederman15e47302012-09-07 20:12:54 +00006854 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006855 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006856
Johannes Berge247bd902011-11-04 11:18:21 +01006857 if (IS_ERR(hdr)) {
6858 err = PTR_ERR(hdr);
6859 goto free_msg;
6860 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006861 }
Johannes Berge247bd902011-11-04 11:18:21 +01006862
Johannes Berg683b6d32012-11-08 21:25:48 +01006863 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006864 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6865 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006866 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006867 if (err)
6868 goto free_msg;
6869
Johannes Berge247bd902011-11-04 11:18:21 +01006870 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006871 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6872 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006873
Johannes Berge247bd902011-11-04 11:18:21 +01006874 genlmsg_end(msg, hdr);
6875 return genlmsg_reply(msg, info);
6876 }
6877
6878 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006879
6880 nla_put_failure:
6881 err = -ENOBUFS;
6882 free_msg:
6883 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006884 return err;
6885}
6886
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006887static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6888{
6889 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006890 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006891 u64 cookie;
6892
6893 if (!info->attrs[NL80211_ATTR_COOKIE])
6894 return -EINVAL;
6895
6896 if (!rdev->ops->mgmt_tx_cancel_wait)
6897 return -EOPNOTSUPP;
6898
Johannes Berg71bbc992012-06-15 15:30:18 +02006899 switch (wdev->iftype) {
6900 case NL80211_IFTYPE_STATION:
6901 case NL80211_IFTYPE_ADHOC:
6902 case NL80211_IFTYPE_P2P_CLIENT:
6903 case NL80211_IFTYPE_AP:
6904 case NL80211_IFTYPE_AP_VLAN:
6905 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006906 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006907 break;
6908 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006909 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006910 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006911
6912 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6913
Hila Gonene35e4d22012-06-27 17:19:42 +03006914 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006915}
6916
Kalle Valoffb9eb32010-02-17 17:58:10 +02006917static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6918{
Johannes Berg4c476992010-10-04 21:36:35 +02006919 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006920 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006921 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006922 u8 ps_state;
6923 bool state;
6924 int err;
6925
Johannes Berg4c476992010-10-04 21:36:35 +02006926 if (!info->attrs[NL80211_ATTR_PS_STATE])
6927 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006928
6929 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6930
Johannes Berg4c476992010-10-04 21:36:35 +02006931 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6932 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006933
6934 wdev = dev->ieee80211_ptr;
6935
Johannes Berg4c476992010-10-04 21:36:35 +02006936 if (!rdev->ops->set_power_mgmt)
6937 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006938
6939 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6940
6941 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006942 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006943
Hila Gonene35e4d22012-06-27 17:19:42 +03006944 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006945 if (!err)
6946 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006947 return err;
6948}
6949
6950static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6951{
Johannes Berg4c476992010-10-04 21:36:35 +02006952 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006953 enum nl80211_ps_state ps_state;
6954 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006955 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006956 struct sk_buff *msg;
6957 void *hdr;
6958 int err;
6959
Kalle Valoffb9eb32010-02-17 17:58:10 +02006960 wdev = dev->ieee80211_ptr;
6961
Johannes Berg4c476992010-10-04 21:36:35 +02006962 if (!rdev->ops->set_power_mgmt)
6963 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006964
6965 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006966 if (!msg)
6967 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006968
Eric W. Biederman15e47302012-09-07 20:12:54 +00006969 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006970 NL80211_CMD_GET_POWER_SAVE);
6971 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006972 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006973 goto free_msg;
6974 }
6975
6976 if (wdev->ps)
6977 ps_state = NL80211_PS_ENABLED;
6978 else
6979 ps_state = NL80211_PS_DISABLED;
6980
David S. Miller9360ffd2012-03-29 04:41:26 -04006981 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6982 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006983
6984 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006985 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006986
Johannes Berg4c476992010-10-04 21:36:35 +02006987 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006988 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006989 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006990 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006991 return err;
6992}
6993
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006994static struct nla_policy
6995nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6996 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6997 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6998 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006999 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7000 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7001 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007002};
7003
Thomas Pedersen84f10702012-07-12 16:17:33 -07007004static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007005 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007006{
7007 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7008 struct wireless_dev *wdev;
7009 struct net_device *dev = info->user_ptr[1];
7010
Johannes Bergd9d8b012012-11-26 12:51:52 +01007011 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007012 return -EINVAL;
7013
7014 wdev = dev->ieee80211_ptr;
7015
7016 if (!rdev->ops->set_cqm_txe_config)
7017 return -EOPNOTSUPP;
7018
7019 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7020 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7021 return -EOPNOTSUPP;
7022
Hila Gonene35e4d22012-06-27 17:19:42 +03007023 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007024}
7025
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007026static int nl80211_set_cqm_rssi(struct genl_info *info,
7027 s32 threshold, u32 hysteresis)
7028{
Johannes Berg4c476992010-10-04 21:36:35 +02007029 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007030 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007031 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007032
7033 if (threshold > 0)
7034 return -EINVAL;
7035
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007036 wdev = dev->ieee80211_ptr;
7037
Johannes Berg4c476992010-10-04 21:36:35 +02007038 if (!rdev->ops->set_cqm_rssi_config)
7039 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007040
Johannes Berg074ac8d2010-09-16 14:58:22 +02007041 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007042 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7043 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007044
Hila Gonene35e4d22012-06-27 17:19:42 +03007045 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007046}
7047
7048static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7049{
7050 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7051 struct nlattr *cqm;
7052 int err;
7053
7054 cqm = info->attrs[NL80211_ATTR_CQM];
7055 if (!cqm) {
7056 err = -EINVAL;
7057 goto out;
7058 }
7059
7060 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7061 nl80211_attr_cqm_policy);
7062 if (err)
7063 goto out;
7064
7065 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7066 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7067 s32 threshold;
7068 u32 hysteresis;
7069 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7070 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7071 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007072 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7073 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7074 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7075 u32 rate, pkts, intvl;
7076 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7077 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7078 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7079 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007080 } else
7081 err = -EINVAL;
7082
7083out:
7084 return err;
7085}
7086
Johannes Berg29cbe682010-12-03 09:20:44 +01007087static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7088{
7089 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7090 struct net_device *dev = info->user_ptr[1];
7091 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007092 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007093 int err;
7094
7095 /* start with default */
7096 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007097 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007098
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007099 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007100 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007101 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007102 if (err)
7103 return err;
7104 }
7105
7106 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7107 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7108 return -EINVAL;
7109
Javier Cardonac80d5452010-12-16 17:37:49 -08007110 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7111 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7112
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007113 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7114 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7115 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7116 return -EINVAL;
7117
Marco Porsch9bdbf042013-01-07 16:04:51 +01007118 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7119 setup.beacon_interval =
7120 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7121 if (setup.beacon_interval < 10 ||
7122 setup.beacon_interval > 10000)
7123 return -EINVAL;
7124 }
7125
7126 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7127 setup.dtim_period =
7128 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7129 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7130 return -EINVAL;
7131 }
7132
Javier Cardonac80d5452010-12-16 17:37:49 -08007133 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7134 /* parse additional setup parameters if given */
7135 err = nl80211_parse_mesh_setup(info, &setup);
7136 if (err)
7137 return err;
7138 }
7139
Johannes Bergcc1d2802012-05-16 23:50:20 +02007140 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007141 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7142 if (err)
7143 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007144 } else {
7145 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007146 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007147 }
7148
Javier Cardonac80d5452010-12-16 17:37:49 -08007149 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007150}
7151
7152static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7153{
7154 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7155 struct net_device *dev = info->user_ptr[1];
7156
7157 return cfg80211_leave_mesh(rdev, dev);
7158}
7159
Johannes Bergdfb89c52012-06-27 09:23:48 +02007160#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007161static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7162 struct cfg80211_registered_device *rdev)
7163{
7164 struct nlattr *nl_pats, *nl_pat;
7165 int i, pat_len;
7166
7167 if (!rdev->wowlan->n_patterns)
7168 return 0;
7169
7170 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7171 if (!nl_pats)
7172 return -ENOBUFS;
7173
7174 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7175 nl_pat = nla_nest_start(msg, i + 1);
7176 if (!nl_pat)
7177 return -ENOBUFS;
7178 pat_len = rdev->wowlan->patterns[i].pattern_len;
7179 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7180 DIV_ROUND_UP(pat_len, 8),
7181 rdev->wowlan->patterns[i].mask) ||
7182 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7183 pat_len, rdev->wowlan->patterns[i].pattern) ||
7184 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7185 rdev->wowlan->patterns[i].pkt_offset))
7186 return -ENOBUFS;
7187 nla_nest_end(msg, nl_pat);
7188 }
7189 nla_nest_end(msg, nl_pats);
7190
7191 return 0;
7192}
7193
Johannes Berg2a0e0472013-01-23 22:57:40 +01007194static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7195 struct cfg80211_wowlan_tcp *tcp)
7196{
7197 struct nlattr *nl_tcp;
7198
7199 if (!tcp)
7200 return 0;
7201
7202 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7203 if (!nl_tcp)
7204 return -ENOBUFS;
7205
7206 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7207 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7208 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7209 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7210 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7211 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7212 tcp->payload_len, tcp->payload) ||
7213 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7214 tcp->data_interval) ||
7215 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7216 tcp->wake_len, tcp->wake_data) ||
7217 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7218 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7219 return -ENOBUFS;
7220
7221 if (tcp->payload_seq.len &&
7222 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7223 sizeof(tcp->payload_seq), &tcp->payload_seq))
7224 return -ENOBUFS;
7225
7226 if (tcp->payload_tok.len &&
7227 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7228 sizeof(tcp->payload_tok) + tcp->tokens_size,
7229 &tcp->payload_tok))
7230 return -ENOBUFS;
7231
7232 return 0;
7233}
7234
Johannes Bergff1b6e62011-05-04 15:37:28 +02007235static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7236{
7237 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7238 struct sk_buff *msg;
7239 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007240 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007241
Johannes Berg2a0e0472013-01-23 22:57:40 +01007242 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7243 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007244 return -EOPNOTSUPP;
7245
Johannes Berg2a0e0472013-01-23 22:57:40 +01007246 if (rdev->wowlan && rdev->wowlan->tcp) {
7247 /* adjust size to have room for all the data */
7248 size += rdev->wowlan->tcp->tokens_size +
7249 rdev->wowlan->tcp->payload_len +
7250 rdev->wowlan->tcp->wake_len +
7251 rdev->wowlan->tcp->wake_len / 8;
7252 }
7253
7254 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007255 if (!msg)
7256 return -ENOMEM;
7257
Eric W. Biederman15e47302012-09-07 20:12:54 +00007258 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007259 NL80211_CMD_GET_WOWLAN);
7260 if (!hdr)
7261 goto nla_put_failure;
7262
7263 if (rdev->wowlan) {
7264 struct nlattr *nl_wowlan;
7265
7266 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7267 if (!nl_wowlan)
7268 goto nla_put_failure;
7269
David S. Miller9360ffd2012-03-29 04:41:26 -04007270 if ((rdev->wowlan->any &&
7271 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7272 (rdev->wowlan->disconnect &&
7273 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7274 (rdev->wowlan->magic_pkt &&
7275 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7276 (rdev->wowlan->gtk_rekey_failure &&
7277 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7278 (rdev->wowlan->eap_identity_req &&
7279 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7280 (rdev->wowlan->four_way_handshake &&
7281 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7282 (rdev->wowlan->rfkill_release &&
7283 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7284 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007285
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007286 if (nl80211_send_wowlan_patterns(msg, rdev))
7287 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007288
7289 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7290 goto nla_put_failure;
7291
Johannes Bergff1b6e62011-05-04 15:37:28 +02007292 nla_nest_end(msg, nl_wowlan);
7293 }
7294
7295 genlmsg_end(msg, hdr);
7296 return genlmsg_reply(msg, info);
7297
7298nla_put_failure:
7299 nlmsg_free(msg);
7300 return -ENOBUFS;
7301}
7302
Johannes Berg2a0e0472013-01-23 22:57:40 +01007303static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7304 struct nlattr *attr,
7305 struct cfg80211_wowlan *trig)
7306{
7307 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7308 struct cfg80211_wowlan_tcp *cfg;
7309 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7310 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7311 u32 size;
7312 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7313 int err, port;
7314
7315 if (!rdev->wiphy.wowlan.tcp)
7316 return -EINVAL;
7317
7318 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7319 nla_data(attr), nla_len(attr),
7320 nl80211_wowlan_tcp_policy);
7321 if (err)
7322 return err;
7323
7324 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7325 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7326 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7327 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7328 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7329 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7330 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7331 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7332 return -EINVAL;
7333
7334 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7335 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7336 return -EINVAL;
7337
7338 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7339 rdev->wiphy.wowlan.tcp->data_interval_max)
7340 return -EINVAL;
7341
7342 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7343 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7344 return -EINVAL;
7345
7346 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7347 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7348 return -EINVAL;
7349
7350 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7351 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7352
7353 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7354 tokens_size = tokln - sizeof(*tok);
7355
7356 if (!tok->len || tokens_size % tok->len)
7357 return -EINVAL;
7358 if (!rdev->wiphy.wowlan.tcp->tok)
7359 return -EINVAL;
7360 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7361 return -EINVAL;
7362 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7363 return -EINVAL;
7364 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7365 return -EINVAL;
7366 if (tok->offset + tok->len > data_size)
7367 return -EINVAL;
7368 }
7369
7370 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7371 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7372 if (!rdev->wiphy.wowlan.tcp->seq)
7373 return -EINVAL;
7374 if (seq->len == 0 || seq->len > 4)
7375 return -EINVAL;
7376 if (seq->len + seq->offset > data_size)
7377 return -EINVAL;
7378 }
7379
7380 size = sizeof(*cfg);
7381 size += data_size;
7382 size += wake_size + wake_mask_size;
7383 size += tokens_size;
7384
7385 cfg = kzalloc(size, GFP_KERNEL);
7386 if (!cfg)
7387 return -ENOMEM;
7388 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7389 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7390 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7391 ETH_ALEN);
7392 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7393 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7394 else
7395 port = 0;
7396#ifdef CONFIG_INET
7397 /* allocate a socket and port for it and use it */
7398 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7399 IPPROTO_TCP, &cfg->sock, 1);
7400 if (err) {
7401 kfree(cfg);
7402 return err;
7403 }
7404 if (inet_csk_get_port(cfg->sock->sk, port)) {
7405 sock_release(cfg->sock);
7406 kfree(cfg);
7407 return -EADDRINUSE;
7408 }
7409 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7410#else
7411 if (!port) {
7412 kfree(cfg);
7413 return -EINVAL;
7414 }
7415 cfg->src_port = port;
7416#endif
7417
7418 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7419 cfg->payload_len = data_size;
7420 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7421 memcpy((void *)cfg->payload,
7422 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7423 data_size);
7424 if (seq)
7425 cfg->payload_seq = *seq;
7426 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7427 cfg->wake_len = wake_size;
7428 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7429 memcpy((void *)cfg->wake_data,
7430 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7431 wake_size);
7432 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7433 data_size + wake_size;
7434 memcpy((void *)cfg->wake_mask,
7435 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7436 wake_mask_size);
7437 if (tok) {
7438 cfg->tokens_size = tokens_size;
7439 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7440 }
7441
7442 trig->tcp = cfg;
7443
7444 return 0;
7445}
7446
Johannes Bergff1b6e62011-05-04 15:37:28 +02007447static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7448{
7449 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7450 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007451 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007452 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007453 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7454 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007455 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007456
Johannes Berg2a0e0472013-01-23 22:57:40 +01007457 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7458 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007459 return -EOPNOTSUPP;
7460
Johannes Bergae33bd82012-07-12 16:25:02 +02007461 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7462 cfg80211_rdev_free_wowlan(rdev);
7463 rdev->wowlan = NULL;
7464 goto set_wakeup;
7465 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007466
7467 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7468 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7469 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7470 nl80211_wowlan_policy);
7471 if (err)
7472 return err;
7473
7474 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7475 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7476 return -EINVAL;
7477 new_triggers.any = true;
7478 }
7479
7480 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7481 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7482 return -EINVAL;
7483 new_triggers.disconnect = true;
7484 }
7485
7486 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7487 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7488 return -EINVAL;
7489 new_triggers.magic_pkt = true;
7490 }
7491
Johannes Berg77dbbb12011-07-13 10:48:55 +02007492 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7493 return -EINVAL;
7494
7495 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7496 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7497 return -EINVAL;
7498 new_triggers.gtk_rekey_failure = true;
7499 }
7500
7501 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7502 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7503 return -EINVAL;
7504 new_triggers.eap_identity_req = true;
7505 }
7506
7507 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7508 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7509 return -EINVAL;
7510 new_triggers.four_way_handshake = true;
7511 }
7512
7513 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7514 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7515 return -EINVAL;
7516 new_triggers.rfkill_release = true;
7517 }
7518
Johannes Bergff1b6e62011-05-04 15:37:28 +02007519 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7520 struct nlattr *pat;
7521 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007522 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007523 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7524
7525 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7526 rem)
7527 n_patterns++;
7528 if (n_patterns > wowlan->n_patterns)
7529 return -EINVAL;
7530
7531 new_triggers.patterns = kcalloc(n_patterns,
7532 sizeof(new_triggers.patterns[0]),
7533 GFP_KERNEL);
7534 if (!new_triggers.patterns)
7535 return -ENOMEM;
7536
7537 new_triggers.n_patterns = n_patterns;
7538 i = 0;
7539
7540 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7541 rem) {
7542 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7543 nla_data(pat), nla_len(pat), NULL);
7544 err = -EINVAL;
7545 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7546 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7547 goto error;
7548 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7549 mask_len = DIV_ROUND_UP(pat_len, 8);
7550 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7551 mask_len)
7552 goto error;
7553 if (pat_len > wowlan->pattern_max_len ||
7554 pat_len < wowlan->pattern_min_len)
7555 goto error;
7556
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007557 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7558 pkt_offset = 0;
7559 else
7560 pkt_offset = nla_get_u32(
7561 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7562 if (pkt_offset > wowlan->max_pkt_offset)
7563 goto error;
7564 new_triggers.patterns[i].pkt_offset = pkt_offset;
7565
Johannes Bergff1b6e62011-05-04 15:37:28 +02007566 new_triggers.patterns[i].mask =
7567 kmalloc(mask_len + pat_len, GFP_KERNEL);
7568 if (!new_triggers.patterns[i].mask) {
7569 err = -ENOMEM;
7570 goto error;
7571 }
7572 new_triggers.patterns[i].pattern =
7573 new_triggers.patterns[i].mask + mask_len;
7574 memcpy(new_triggers.patterns[i].mask,
7575 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7576 mask_len);
7577 new_triggers.patterns[i].pattern_len = pat_len;
7578 memcpy(new_triggers.patterns[i].pattern,
7579 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7580 pat_len);
7581 i++;
7582 }
7583 }
7584
Johannes Berg2a0e0472013-01-23 22:57:40 +01007585 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7586 err = nl80211_parse_wowlan_tcp(
7587 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7588 &new_triggers);
7589 if (err)
7590 goto error;
7591 }
7592
Johannes Bergae33bd82012-07-12 16:25:02 +02007593 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7594 if (!ntrig) {
7595 err = -ENOMEM;
7596 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007597 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007598 cfg80211_rdev_free_wowlan(rdev);
7599 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007600
Johannes Bergae33bd82012-07-12 16:25:02 +02007601 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007602 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007603 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007604
Johannes Bergff1b6e62011-05-04 15:37:28 +02007605 return 0;
7606 error:
7607 for (i = 0; i < new_triggers.n_patterns; i++)
7608 kfree(new_triggers.patterns[i].mask);
7609 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007610 if (new_triggers.tcp && new_triggers.tcp->sock)
7611 sock_release(new_triggers.tcp->sock);
7612 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007613 return err;
7614}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007615#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007616
Johannes Berge5497d72011-07-05 16:35:40 +02007617static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7618{
7619 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7620 struct net_device *dev = info->user_ptr[1];
7621 struct wireless_dev *wdev = dev->ieee80211_ptr;
7622 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7623 struct cfg80211_gtk_rekey_data rekey_data;
7624 int err;
7625
7626 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7627 return -EINVAL;
7628
7629 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7630 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7631 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7632 nl80211_rekey_policy);
7633 if (err)
7634 return err;
7635
7636 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7637 return -ERANGE;
7638 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7639 return -ERANGE;
7640 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7641 return -ERANGE;
7642
7643 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7644 NL80211_KEK_LEN);
7645 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7646 NL80211_KCK_LEN);
7647 memcpy(rekey_data.replay_ctr,
7648 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7649 NL80211_REPLAY_CTR_LEN);
7650
7651 wdev_lock(wdev);
7652 if (!wdev->current_bss) {
7653 err = -ENOTCONN;
7654 goto out;
7655 }
7656
7657 if (!rdev->ops->set_rekey_data) {
7658 err = -EOPNOTSUPP;
7659 goto out;
7660 }
7661
Hila Gonene35e4d22012-06-27 17:19:42 +03007662 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007663 out:
7664 wdev_unlock(wdev);
7665 return err;
7666}
7667
Johannes Berg28946da2011-11-04 11:18:12 +01007668static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7669 struct genl_info *info)
7670{
7671 struct net_device *dev = info->user_ptr[1];
7672 struct wireless_dev *wdev = dev->ieee80211_ptr;
7673
7674 if (wdev->iftype != NL80211_IFTYPE_AP &&
7675 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7676 return -EINVAL;
7677
Eric W. Biederman15e47302012-09-07 20:12:54 +00007678 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007679 return -EBUSY;
7680
Eric W. Biederman15e47302012-09-07 20:12:54 +00007681 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007682 return 0;
7683}
7684
Johannes Berg7f6cf312011-11-04 11:18:15 +01007685static int nl80211_probe_client(struct sk_buff *skb,
7686 struct genl_info *info)
7687{
7688 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7689 struct net_device *dev = info->user_ptr[1];
7690 struct wireless_dev *wdev = dev->ieee80211_ptr;
7691 struct sk_buff *msg;
7692 void *hdr;
7693 const u8 *addr;
7694 u64 cookie;
7695 int err;
7696
7697 if (wdev->iftype != NL80211_IFTYPE_AP &&
7698 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7699 return -EOPNOTSUPP;
7700
7701 if (!info->attrs[NL80211_ATTR_MAC])
7702 return -EINVAL;
7703
7704 if (!rdev->ops->probe_client)
7705 return -EOPNOTSUPP;
7706
7707 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7708 if (!msg)
7709 return -ENOMEM;
7710
Eric W. Biederman15e47302012-09-07 20:12:54 +00007711 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007712 NL80211_CMD_PROBE_CLIENT);
7713
7714 if (IS_ERR(hdr)) {
7715 err = PTR_ERR(hdr);
7716 goto free_msg;
7717 }
7718
7719 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7720
Hila Gonene35e4d22012-06-27 17:19:42 +03007721 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007722 if (err)
7723 goto free_msg;
7724
David S. Miller9360ffd2012-03-29 04:41:26 -04007725 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7726 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007727
7728 genlmsg_end(msg, hdr);
7729
7730 return genlmsg_reply(msg, info);
7731
7732 nla_put_failure:
7733 err = -ENOBUFS;
7734 free_msg:
7735 nlmsg_free(msg);
7736 return err;
7737}
7738
Johannes Berg5e7602302011-11-04 11:18:17 +01007739static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7740{
7741 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007742 struct cfg80211_beacon_registration *reg, *nreg;
7743 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01007744
7745 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7746 return -EOPNOTSUPP;
7747
Ben Greear37c73b52012-10-26 14:49:25 -07007748 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7749 if (!nreg)
7750 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01007751
Ben Greear37c73b52012-10-26 14:49:25 -07007752 /* First, check if already registered. */
7753 spin_lock_bh(&rdev->beacon_registrations_lock);
7754 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7755 if (reg->nlportid == info->snd_portid) {
7756 rv = -EALREADY;
7757 goto out_err;
7758 }
7759 }
7760 /* Add it to the list */
7761 nreg->nlportid = info->snd_portid;
7762 list_add(&nreg->list, &rdev->beacon_registrations);
7763
7764 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01007765
7766 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007767out_err:
7768 spin_unlock_bh(&rdev->beacon_registrations_lock);
7769 kfree(nreg);
7770 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01007771}
7772
Johannes Berg98104fde2012-06-16 00:19:54 +02007773static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7774{
7775 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7776 struct wireless_dev *wdev = info->user_ptr[1];
7777 int err;
7778
7779 if (!rdev->ops->start_p2p_device)
7780 return -EOPNOTSUPP;
7781
7782 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7783 return -EOPNOTSUPP;
7784
7785 if (wdev->p2p_started)
7786 return 0;
7787
7788 mutex_lock(&rdev->devlist_mtx);
7789 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7790 mutex_unlock(&rdev->devlist_mtx);
7791 if (err)
7792 return err;
7793
Johannes Bergeeb126e2012-10-23 15:16:50 +02007794 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007795 if (err)
7796 return err;
7797
7798 wdev->p2p_started = true;
7799 mutex_lock(&rdev->devlist_mtx);
7800 rdev->opencount++;
7801 mutex_unlock(&rdev->devlist_mtx);
7802
7803 return 0;
7804}
7805
7806static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7807{
7808 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7809 struct wireless_dev *wdev = info->user_ptr[1];
7810
7811 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7812 return -EOPNOTSUPP;
7813
7814 if (!rdev->ops->stop_p2p_device)
7815 return -EOPNOTSUPP;
7816
7817 if (!wdev->p2p_started)
7818 return 0;
7819
Johannes Bergeeb126e2012-10-23 15:16:50 +02007820 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007821 wdev->p2p_started = false;
7822
7823 mutex_lock(&rdev->devlist_mtx);
7824 rdev->opencount--;
7825 mutex_unlock(&rdev->devlist_mtx);
7826
7827 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7828 rdev->scan_req->aborted = true;
7829 ___cfg80211_scan_done(rdev, true);
7830 }
7831
7832 return 0;
7833}
7834
Johannes Berg4c476992010-10-04 21:36:35 +02007835#define NL80211_FLAG_NEED_WIPHY 0x01
7836#define NL80211_FLAG_NEED_NETDEV 0x02
7837#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007838#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7839#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7840 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007841#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007842/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007843#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7844 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007845
7846static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7847 struct genl_info *info)
7848{
7849 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007850 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007851 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007852 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7853
7854 if (rtnl)
7855 rtnl_lock();
7856
7857 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007858 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007859 if (IS_ERR(rdev)) {
7860 if (rtnl)
7861 rtnl_unlock();
7862 return PTR_ERR(rdev);
7863 }
7864 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007865 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7866 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007867 mutex_lock(&cfg80211_mutex);
7868 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7869 info->attrs);
7870 if (IS_ERR(wdev)) {
7871 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007872 if (rtnl)
7873 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007874 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007875 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007876
Johannes Berg89a54e42012-06-15 14:33:17 +02007877 dev = wdev->netdev;
7878 rdev = wiphy_to_dev(wdev->wiphy);
7879
Johannes Berg1bf614e2012-06-15 15:23:36 +02007880 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7881 if (!dev) {
7882 mutex_unlock(&cfg80211_mutex);
7883 if (rtnl)
7884 rtnl_unlock();
7885 return -EINVAL;
7886 }
7887
7888 info->user_ptr[1] = dev;
7889 } else {
7890 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007891 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007892
Johannes Berg1bf614e2012-06-15 15:23:36 +02007893 if (dev) {
7894 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7895 !netif_running(dev)) {
7896 mutex_unlock(&cfg80211_mutex);
7897 if (rtnl)
7898 rtnl_unlock();
7899 return -ENETDOWN;
7900 }
7901
7902 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007903 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7904 if (!wdev->p2p_started) {
7905 mutex_unlock(&cfg80211_mutex);
7906 if (rtnl)
7907 rtnl_unlock();
7908 return -ENETDOWN;
7909 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007910 }
7911
Johannes Berg89a54e42012-06-15 14:33:17 +02007912 cfg80211_lock_rdev(rdev);
7913
7914 mutex_unlock(&cfg80211_mutex);
7915
Johannes Berg4c476992010-10-04 21:36:35 +02007916 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007917 }
7918
7919 return 0;
7920}
7921
7922static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7923 struct genl_info *info)
7924{
7925 if (info->user_ptr[0])
7926 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007927 if (info->user_ptr[1]) {
7928 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7929 struct wireless_dev *wdev = info->user_ptr[1];
7930
7931 if (wdev->netdev)
7932 dev_put(wdev->netdev);
7933 } else {
7934 dev_put(info->user_ptr[1]);
7935 }
7936 }
Johannes Berg4c476992010-10-04 21:36:35 +02007937 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7938 rtnl_unlock();
7939}
7940
Johannes Berg55682962007-09-20 13:09:35 -04007941static struct genl_ops nl80211_ops[] = {
7942 {
7943 .cmd = NL80211_CMD_GET_WIPHY,
7944 .doit = nl80211_get_wiphy,
7945 .dumpit = nl80211_dump_wiphy,
7946 .policy = nl80211_policy,
7947 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007948 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007949 },
7950 {
7951 .cmd = NL80211_CMD_SET_WIPHY,
7952 .doit = nl80211_set_wiphy,
7953 .policy = nl80211_policy,
7954 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007955 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007956 },
7957 {
7958 .cmd = NL80211_CMD_GET_INTERFACE,
7959 .doit = nl80211_get_interface,
7960 .dumpit = nl80211_dump_interface,
7961 .policy = nl80211_policy,
7962 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007963 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007964 },
7965 {
7966 .cmd = NL80211_CMD_SET_INTERFACE,
7967 .doit = nl80211_set_interface,
7968 .policy = nl80211_policy,
7969 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007970 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7971 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007972 },
7973 {
7974 .cmd = NL80211_CMD_NEW_INTERFACE,
7975 .doit = nl80211_new_interface,
7976 .policy = nl80211_policy,
7977 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007978 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7979 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007980 },
7981 {
7982 .cmd = NL80211_CMD_DEL_INTERFACE,
7983 .doit = nl80211_del_interface,
7984 .policy = nl80211_policy,
7985 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007986 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007987 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007988 },
Johannes Berg41ade002007-12-19 02:03:29 +01007989 {
7990 .cmd = NL80211_CMD_GET_KEY,
7991 .doit = nl80211_get_key,
7992 .policy = nl80211_policy,
7993 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007994 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007995 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007996 },
7997 {
7998 .cmd = NL80211_CMD_SET_KEY,
7999 .doit = nl80211_set_key,
8000 .policy = nl80211_policy,
8001 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008002 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008003 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008004 },
8005 {
8006 .cmd = NL80211_CMD_NEW_KEY,
8007 .doit = nl80211_new_key,
8008 .policy = nl80211_policy,
8009 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008010 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008011 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008012 },
8013 {
8014 .cmd = NL80211_CMD_DEL_KEY,
8015 .doit = nl80211_del_key,
8016 .policy = nl80211_policy,
8017 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008018 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008019 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008020 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008021 {
8022 .cmd = NL80211_CMD_SET_BEACON,
8023 .policy = nl80211_policy,
8024 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008025 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008026 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008027 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008028 },
8029 {
Johannes Berg88600202012-02-13 15:17:18 +01008030 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008031 .policy = nl80211_policy,
8032 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008033 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008034 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008035 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008036 },
8037 {
Johannes Berg88600202012-02-13 15:17:18 +01008038 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008039 .policy = nl80211_policy,
8040 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008041 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008042 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008043 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008044 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008045 {
8046 .cmd = NL80211_CMD_GET_STATION,
8047 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008048 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008049 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008050 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8051 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008052 },
8053 {
8054 .cmd = NL80211_CMD_SET_STATION,
8055 .doit = nl80211_set_station,
8056 .policy = nl80211_policy,
8057 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008058 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008059 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008060 },
8061 {
8062 .cmd = NL80211_CMD_NEW_STATION,
8063 .doit = nl80211_new_station,
8064 .policy = nl80211_policy,
8065 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008066 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008067 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008068 },
8069 {
8070 .cmd = NL80211_CMD_DEL_STATION,
8071 .doit = nl80211_del_station,
8072 .policy = nl80211_policy,
8073 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008074 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008075 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008076 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008077 {
8078 .cmd = NL80211_CMD_GET_MPATH,
8079 .doit = nl80211_get_mpath,
8080 .dumpit = nl80211_dump_mpath,
8081 .policy = nl80211_policy,
8082 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008083 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008084 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008085 },
8086 {
8087 .cmd = NL80211_CMD_SET_MPATH,
8088 .doit = nl80211_set_mpath,
8089 .policy = nl80211_policy,
8090 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008091 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008092 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008093 },
8094 {
8095 .cmd = NL80211_CMD_NEW_MPATH,
8096 .doit = nl80211_new_mpath,
8097 .policy = nl80211_policy,
8098 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008099 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008100 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008101 },
8102 {
8103 .cmd = NL80211_CMD_DEL_MPATH,
8104 .doit = nl80211_del_mpath,
8105 .policy = nl80211_policy,
8106 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008107 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008108 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008109 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008110 {
8111 .cmd = NL80211_CMD_SET_BSS,
8112 .doit = nl80211_set_bss,
8113 .policy = nl80211_policy,
8114 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008115 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008116 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008117 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008118 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008119 .cmd = NL80211_CMD_GET_REG,
8120 .doit = nl80211_get_reg,
8121 .policy = nl80211_policy,
8122 /* can be retrieved by unprivileged users */
8123 },
8124 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008125 .cmd = NL80211_CMD_SET_REG,
8126 .doit = nl80211_set_reg,
8127 .policy = nl80211_policy,
8128 .flags = GENL_ADMIN_PERM,
8129 },
8130 {
8131 .cmd = NL80211_CMD_REQ_SET_REG,
8132 .doit = nl80211_req_set_reg,
8133 .policy = nl80211_policy,
8134 .flags = GENL_ADMIN_PERM,
8135 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008136 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008137 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8138 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008139 .policy = nl80211_policy,
8140 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008141 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008142 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008143 },
8144 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008145 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8146 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008147 .policy = nl80211_policy,
8148 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008149 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008150 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008151 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008152 {
Johannes Berg2a519312009-02-10 21:25:55 +01008153 .cmd = NL80211_CMD_TRIGGER_SCAN,
8154 .doit = nl80211_trigger_scan,
8155 .policy = nl80211_policy,
8156 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008157 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008158 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008159 },
8160 {
8161 .cmd = NL80211_CMD_GET_SCAN,
8162 .policy = nl80211_policy,
8163 .dumpit = nl80211_dump_scan,
8164 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008165 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008166 .cmd = NL80211_CMD_START_SCHED_SCAN,
8167 .doit = nl80211_start_sched_scan,
8168 .policy = nl80211_policy,
8169 .flags = GENL_ADMIN_PERM,
8170 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8171 NL80211_FLAG_NEED_RTNL,
8172 },
8173 {
8174 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8175 .doit = nl80211_stop_sched_scan,
8176 .policy = nl80211_policy,
8177 .flags = GENL_ADMIN_PERM,
8178 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8179 NL80211_FLAG_NEED_RTNL,
8180 },
8181 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008182 .cmd = NL80211_CMD_AUTHENTICATE,
8183 .doit = nl80211_authenticate,
8184 .policy = nl80211_policy,
8185 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008186 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008187 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008188 },
8189 {
8190 .cmd = NL80211_CMD_ASSOCIATE,
8191 .doit = nl80211_associate,
8192 .policy = nl80211_policy,
8193 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008194 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008195 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008196 },
8197 {
8198 .cmd = NL80211_CMD_DEAUTHENTICATE,
8199 .doit = nl80211_deauthenticate,
8200 .policy = nl80211_policy,
8201 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008202 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008203 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008204 },
8205 {
8206 .cmd = NL80211_CMD_DISASSOCIATE,
8207 .doit = nl80211_disassociate,
8208 .policy = nl80211_policy,
8209 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008210 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008211 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008212 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008213 {
8214 .cmd = NL80211_CMD_JOIN_IBSS,
8215 .doit = nl80211_join_ibss,
8216 .policy = nl80211_policy,
8217 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008218 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008219 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008220 },
8221 {
8222 .cmd = NL80211_CMD_LEAVE_IBSS,
8223 .doit = nl80211_leave_ibss,
8224 .policy = nl80211_policy,
8225 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008226 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008227 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008228 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008229#ifdef CONFIG_NL80211_TESTMODE
8230 {
8231 .cmd = NL80211_CMD_TESTMODE,
8232 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008233 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008234 .policy = nl80211_policy,
8235 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008236 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8237 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008238 },
8239#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008240 {
8241 .cmd = NL80211_CMD_CONNECT,
8242 .doit = nl80211_connect,
8243 .policy = nl80211_policy,
8244 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008245 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008246 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008247 },
8248 {
8249 .cmd = NL80211_CMD_DISCONNECT,
8250 .doit = nl80211_disconnect,
8251 .policy = nl80211_policy,
8252 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008253 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008254 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008255 },
Johannes Berg463d0182009-07-14 00:33:35 +02008256 {
8257 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8258 .doit = nl80211_wiphy_netns,
8259 .policy = nl80211_policy,
8260 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008261 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8262 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008263 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008264 {
8265 .cmd = NL80211_CMD_GET_SURVEY,
8266 .policy = nl80211_policy,
8267 .dumpit = nl80211_dump_survey,
8268 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008269 {
8270 .cmd = NL80211_CMD_SET_PMKSA,
8271 .doit = nl80211_setdel_pmksa,
8272 .policy = nl80211_policy,
8273 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008274 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008275 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008276 },
8277 {
8278 .cmd = NL80211_CMD_DEL_PMKSA,
8279 .doit = nl80211_setdel_pmksa,
8280 .policy = nl80211_policy,
8281 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008282 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008283 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008284 },
8285 {
8286 .cmd = NL80211_CMD_FLUSH_PMKSA,
8287 .doit = nl80211_flush_pmksa,
8288 .policy = nl80211_policy,
8289 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008290 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008291 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008292 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008293 {
8294 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8295 .doit = nl80211_remain_on_channel,
8296 .policy = nl80211_policy,
8297 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008298 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008299 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008300 },
8301 {
8302 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8303 .doit = nl80211_cancel_remain_on_channel,
8304 .policy = nl80211_policy,
8305 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008306 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008307 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008308 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008309 {
8310 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8311 .doit = nl80211_set_tx_bitrate_mask,
8312 .policy = nl80211_policy,
8313 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008314 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8315 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008316 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008317 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008318 .cmd = NL80211_CMD_REGISTER_FRAME,
8319 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008320 .policy = nl80211_policy,
8321 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008322 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008323 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008324 },
8325 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008326 .cmd = NL80211_CMD_FRAME,
8327 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008328 .policy = nl80211_policy,
8329 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008330 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008331 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008332 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008333 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008334 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8335 .doit = nl80211_tx_mgmt_cancel_wait,
8336 .policy = nl80211_policy,
8337 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008338 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008339 NL80211_FLAG_NEED_RTNL,
8340 },
8341 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008342 .cmd = NL80211_CMD_SET_POWER_SAVE,
8343 .doit = nl80211_set_power_save,
8344 .policy = nl80211_policy,
8345 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008346 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8347 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008348 },
8349 {
8350 .cmd = NL80211_CMD_GET_POWER_SAVE,
8351 .doit = nl80211_get_power_save,
8352 .policy = nl80211_policy,
8353 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008354 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8355 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008356 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008357 {
8358 .cmd = NL80211_CMD_SET_CQM,
8359 .doit = nl80211_set_cqm,
8360 .policy = nl80211_policy,
8361 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008362 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8363 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008364 },
Johannes Bergf444de02010-05-05 15:25:02 +02008365 {
8366 .cmd = NL80211_CMD_SET_CHANNEL,
8367 .doit = nl80211_set_channel,
8368 .policy = nl80211_policy,
8369 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008370 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8371 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008372 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008373 {
8374 .cmd = NL80211_CMD_SET_WDS_PEER,
8375 .doit = nl80211_set_wds_peer,
8376 .policy = nl80211_policy,
8377 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008378 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8379 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008380 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008381 {
8382 .cmd = NL80211_CMD_JOIN_MESH,
8383 .doit = nl80211_join_mesh,
8384 .policy = nl80211_policy,
8385 .flags = GENL_ADMIN_PERM,
8386 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8387 NL80211_FLAG_NEED_RTNL,
8388 },
8389 {
8390 .cmd = NL80211_CMD_LEAVE_MESH,
8391 .doit = nl80211_leave_mesh,
8392 .policy = nl80211_policy,
8393 .flags = GENL_ADMIN_PERM,
8394 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8395 NL80211_FLAG_NEED_RTNL,
8396 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008397#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008398 {
8399 .cmd = NL80211_CMD_GET_WOWLAN,
8400 .doit = nl80211_get_wowlan,
8401 .policy = nl80211_policy,
8402 /* can be retrieved by unprivileged users */
8403 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8404 NL80211_FLAG_NEED_RTNL,
8405 },
8406 {
8407 .cmd = NL80211_CMD_SET_WOWLAN,
8408 .doit = nl80211_set_wowlan,
8409 .policy = nl80211_policy,
8410 .flags = GENL_ADMIN_PERM,
8411 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8412 NL80211_FLAG_NEED_RTNL,
8413 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008414#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008415 {
8416 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8417 .doit = nl80211_set_rekey_data,
8418 .policy = nl80211_policy,
8419 .flags = GENL_ADMIN_PERM,
8420 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8421 NL80211_FLAG_NEED_RTNL,
8422 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008423 {
8424 .cmd = NL80211_CMD_TDLS_MGMT,
8425 .doit = nl80211_tdls_mgmt,
8426 .policy = nl80211_policy,
8427 .flags = GENL_ADMIN_PERM,
8428 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8429 NL80211_FLAG_NEED_RTNL,
8430 },
8431 {
8432 .cmd = NL80211_CMD_TDLS_OPER,
8433 .doit = nl80211_tdls_oper,
8434 .policy = nl80211_policy,
8435 .flags = GENL_ADMIN_PERM,
8436 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8437 NL80211_FLAG_NEED_RTNL,
8438 },
Johannes Berg28946da2011-11-04 11:18:12 +01008439 {
8440 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8441 .doit = nl80211_register_unexpected_frame,
8442 .policy = nl80211_policy,
8443 .flags = GENL_ADMIN_PERM,
8444 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8445 NL80211_FLAG_NEED_RTNL,
8446 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008447 {
8448 .cmd = NL80211_CMD_PROBE_CLIENT,
8449 .doit = nl80211_probe_client,
8450 .policy = nl80211_policy,
8451 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008452 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008453 NL80211_FLAG_NEED_RTNL,
8454 },
Johannes Berg5e7602302011-11-04 11:18:17 +01008455 {
8456 .cmd = NL80211_CMD_REGISTER_BEACONS,
8457 .doit = nl80211_register_beacons,
8458 .policy = nl80211_policy,
8459 .flags = GENL_ADMIN_PERM,
8460 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8461 NL80211_FLAG_NEED_RTNL,
8462 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008463 {
8464 .cmd = NL80211_CMD_SET_NOACK_MAP,
8465 .doit = nl80211_set_noack_map,
8466 .policy = nl80211_policy,
8467 .flags = GENL_ADMIN_PERM,
8468 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8469 NL80211_FLAG_NEED_RTNL,
8470 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008471 {
8472 .cmd = NL80211_CMD_START_P2P_DEVICE,
8473 .doit = nl80211_start_p2p_device,
8474 .policy = nl80211_policy,
8475 .flags = GENL_ADMIN_PERM,
8476 .internal_flags = NL80211_FLAG_NEED_WDEV |
8477 NL80211_FLAG_NEED_RTNL,
8478 },
8479 {
8480 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8481 .doit = nl80211_stop_p2p_device,
8482 .policy = nl80211_policy,
8483 .flags = GENL_ADMIN_PERM,
8484 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8485 NL80211_FLAG_NEED_RTNL,
8486 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008487 {
8488 .cmd = NL80211_CMD_SET_MCAST_RATE,
8489 .doit = nl80211_set_mcast_rate,
8490 .policy = nl80211_policy,
8491 .flags = GENL_ADMIN_PERM,
8492 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8493 NL80211_FLAG_NEED_RTNL,
8494 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308495 {
8496 .cmd = NL80211_CMD_SET_MAC_ACL,
8497 .doit = nl80211_set_mac_acl,
8498 .policy = nl80211_policy,
8499 .flags = GENL_ADMIN_PERM,
8500 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8501 NL80211_FLAG_NEED_RTNL,
8502 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008503 {
8504 .cmd = NL80211_CMD_RADAR_DETECT,
8505 .doit = nl80211_start_radar_detection,
8506 .policy = nl80211_policy,
8507 .flags = GENL_ADMIN_PERM,
8508 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8509 NL80211_FLAG_NEED_RTNL,
8510 },
Johannes Berg55682962007-09-20 13:09:35 -04008511};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008512
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008513static struct genl_multicast_group nl80211_mlme_mcgrp = {
8514 .name = "mlme",
8515};
Johannes Berg55682962007-09-20 13:09:35 -04008516
8517/* multicast groups */
8518static struct genl_multicast_group nl80211_config_mcgrp = {
8519 .name = "config",
8520};
Johannes Berg2a519312009-02-10 21:25:55 +01008521static struct genl_multicast_group nl80211_scan_mcgrp = {
8522 .name = "scan",
8523};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008524static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8525 .name = "regulatory",
8526};
Johannes Berg55682962007-09-20 13:09:35 -04008527
8528/* notification functions */
8529
8530void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8531{
8532 struct sk_buff *msg;
8533
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008534 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008535 if (!msg)
8536 return;
8537
8538 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
8539 nlmsg_free(msg);
8540 return;
8541 }
8542
Johannes Berg463d0182009-07-14 00:33:35 +02008543 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8544 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008545}
8546
Johannes Berg362a4152009-05-24 16:43:15 +02008547static int nl80211_add_scan_req(struct sk_buff *msg,
8548 struct cfg80211_registered_device *rdev)
8549{
8550 struct cfg80211_scan_request *req = rdev->scan_req;
8551 struct nlattr *nest;
8552 int i;
8553
Johannes Berg667503dd2009-07-07 03:56:11 +02008554 ASSERT_RDEV_LOCK(rdev);
8555
Johannes Berg362a4152009-05-24 16:43:15 +02008556 if (WARN_ON(!req))
8557 return 0;
8558
8559 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8560 if (!nest)
8561 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008562 for (i = 0; i < req->n_ssids; i++) {
8563 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8564 goto nla_put_failure;
8565 }
Johannes Berg362a4152009-05-24 16:43:15 +02008566 nla_nest_end(msg, nest);
8567
8568 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8569 if (!nest)
8570 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008571 for (i = 0; i < req->n_channels; i++) {
8572 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8573 goto nla_put_failure;
8574 }
Johannes Berg362a4152009-05-24 16:43:15 +02008575 nla_nest_end(msg, nest);
8576
David S. Miller9360ffd2012-03-29 04:41:26 -04008577 if (req->ie &&
8578 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8579 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008580
Sam Lefflered4737712012-10-11 21:03:31 -07008581 if (req->flags)
8582 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8583
Johannes Berg362a4152009-05-24 16:43:15 +02008584 return 0;
8585 nla_put_failure:
8586 return -ENOBUFS;
8587}
8588
Johannes Berga538e2d2009-06-16 19:56:42 +02008589static int nl80211_send_scan_msg(struct sk_buff *msg,
8590 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008591 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008592 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008593 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008594{
8595 void *hdr;
8596
Eric W. Biederman15e47302012-09-07 20:12:54 +00008597 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008598 if (!hdr)
8599 return -1;
8600
David S. Miller9360ffd2012-03-29 04:41:26 -04008601 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008602 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8603 wdev->netdev->ifindex)) ||
8604 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008605 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008606
Johannes Berg362a4152009-05-24 16:43:15 +02008607 /* ignore errors and send incomplete event anyway */
8608 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008609
8610 return genlmsg_end(msg, hdr);
8611
8612 nla_put_failure:
8613 genlmsg_cancel(msg, hdr);
8614 return -EMSGSIZE;
8615}
8616
Luciano Coelho807f8a82011-05-11 17:09:35 +03008617static int
8618nl80211_send_sched_scan_msg(struct sk_buff *msg,
8619 struct cfg80211_registered_device *rdev,
8620 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008621 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008622{
8623 void *hdr;
8624
Eric W. Biederman15e47302012-09-07 20:12:54 +00008625 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008626 if (!hdr)
8627 return -1;
8628
David S. Miller9360ffd2012-03-29 04:41:26 -04008629 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8630 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8631 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008632
8633 return genlmsg_end(msg, hdr);
8634
8635 nla_put_failure:
8636 genlmsg_cancel(msg, hdr);
8637 return -EMSGSIZE;
8638}
8639
Johannes Berga538e2d2009-06-16 19:56:42 +02008640void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008641 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008642{
8643 struct sk_buff *msg;
8644
Thomas Graf58050fc2012-06-28 03:57:45 +00008645 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008646 if (!msg)
8647 return;
8648
Johannes Bergfd014282012-06-18 19:17:03 +02008649 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008650 NL80211_CMD_TRIGGER_SCAN) < 0) {
8651 nlmsg_free(msg);
8652 return;
8653 }
8654
Johannes Berg463d0182009-07-14 00:33:35 +02008655 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8656 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008657}
8658
Johannes Berg2a519312009-02-10 21:25:55 +01008659void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008660 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008661{
8662 struct sk_buff *msg;
8663
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008664 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008665 if (!msg)
8666 return;
8667
Johannes Bergfd014282012-06-18 19:17:03 +02008668 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008669 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008670 nlmsg_free(msg);
8671 return;
8672 }
8673
Johannes Berg463d0182009-07-14 00:33:35 +02008674 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8675 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008676}
8677
8678void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008679 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008680{
8681 struct sk_buff *msg;
8682
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008684 if (!msg)
8685 return;
8686
Johannes Bergfd014282012-06-18 19:17:03 +02008687 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008688 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008689 nlmsg_free(msg);
8690 return;
8691 }
8692
Johannes Berg463d0182009-07-14 00:33:35 +02008693 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8694 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008695}
8696
Luciano Coelho807f8a82011-05-11 17:09:35 +03008697void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8698 struct net_device *netdev)
8699{
8700 struct sk_buff *msg;
8701
8702 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8703 if (!msg)
8704 return;
8705
8706 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8707 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8708 nlmsg_free(msg);
8709 return;
8710 }
8711
8712 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8713 nl80211_scan_mcgrp.id, GFP_KERNEL);
8714}
8715
8716void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8717 struct net_device *netdev, u32 cmd)
8718{
8719 struct sk_buff *msg;
8720
Thomas Graf58050fc2012-06-28 03:57:45 +00008721 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008722 if (!msg)
8723 return;
8724
8725 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8726 nlmsg_free(msg);
8727 return;
8728 }
8729
8730 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8731 nl80211_scan_mcgrp.id, GFP_KERNEL);
8732}
8733
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008734/*
8735 * This can happen on global regulatory changes or device specific settings
8736 * based on custom world regulatory domains.
8737 */
8738void nl80211_send_reg_change_event(struct regulatory_request *request)
8739{
8740 struct sk_buff *msg;
8741 void *hdr;
8742
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008743 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008744 if (!msg)
8745 return;
8746
8747 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8748 if (!hdr) {
8749 nlmsg_free(msg);
8750 return;
8751 }
8752
8753 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008754 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8755 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008756
David S. Miller9360ffd2012-03-29 04:41:26 -04008757 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8758 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8759 NL80211_REGDOM_TYPE_WORLD))
8760 goto nla_put_failure;
8761 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8762 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8763 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8764 goto nla_put_failure;
8765 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8766 request->intersect) {
8767 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8768 NL80211_REGDOM_TYPE_INTERSECTION))
8769 goto nla_put_failure;
8770 } else {
8771 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8772 NL80211_REGDOM_TYPE_COUNTRY) ||
8773 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8774 request->alpha2))
8775 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008776 }
8777
Johannes Bergf4173762012-12-03 18:23:37 +01008778 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008779 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8780 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008781
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008782 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008783
Johannes Bergbc43b282009-07-25 10:54:13 +02008784 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008785 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008786 GFP_ATOMIC);
8787 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008788
8789 return;
8790
8791nla_put_failure:
8792 genlmsg_cancel(msg, hdr);
8793 nlmsg_free(msg);
8794}
8795
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008796static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8797 struct net_device *netdev,
8798 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008799 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008800{
8801 struct sk_buff *msg;
8802 void *hdr;
8803
Johannes Berge6d6e342009-07-01 21:26:47 +02008804 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008805 if (!msg)
8806 return;
8807
8808 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8809 if (!hdr) {
8810 nlmsg_free(msg);
8811 return;
8812 }
8813
David S. Miller9360ffd2012-03-29 04:41:26 -04008814 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8815 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8816 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8817 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008818
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008819 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008820
Johannes Berg463d0182009-07-14 00:33:35 +02008821 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8822 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008823 return;
8824
8825 nla_put_failure:
8826 genlmsg_cancel(msg, hdr);
8827 nlmsg_free(msg);
8828}
8829
8830void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008831 struct net_device *netdev, const u8 *buf,
8832 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008833{
8834 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008835 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008836}
8837
8838void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8839 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008840 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008841{
Johannes Berge6d6e342009-07-01 21:26:47 +02008842 nl80211_send_mlme_event(rdev, netdev, buf, len,
8843 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008844}
8845
Jouni Malinen53b46b82009-03-27 20:53:56 +02008846void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008847 struct net_device *netdev, const u8 *buf,
8848 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008849{
8850 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008851 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008852}
8853
Jouni Malinen53b46b82009-03-27 20:53:56 +02008854void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8855 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008856 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008857{
8858 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008859 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008860}
8861
Jouni Malinencf4e5942010-12-16 00:52:40 +02008862void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8863 struct net_device *netdev, const u8 *buf,
8864 size_t len, gfp_t gfp)
8865{
8866 nl80211_send_mlme_event(rdev, netdev, buf, len,
8867 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8868}
8869
8870void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8871 struct net_device *netdev, const u8 *buf,
8872 size_t len, gfp_t gfp)
8873{
8874 nl80211_send_mlme_event(rdev, netdev, buf, len,
8875 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8876}
8877
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008878static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8879 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008880 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008881{
8882 struct sk_buff *msg;
8883 void *hdr;
8884
Johannes Berge6d6e342009-07-01 21:26:47 +02008885 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008886 if (!msg)
8887 return;
8888
8889 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8890 if (!hdr) {
8891 nlmsg_free(msg);
8892 return;
8893 }
8894
David S. Miller9360ffd2012-03-29 04:41:26 -04008895 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8896 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8897 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8898 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8899 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008900
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008901 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008902
Johannes Berg463d0182009-07-14 00:33:35 +02008903 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8904 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008905 return;
8906
8907 nla_put_failure:
8908 genlmsg_cancel(msg, hdr);
8909 nlmsg_free(msg);
8910}
8911
8912void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008913 struct net_device *netdev, const u8 *addr,
8914 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008915{
8916 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008917 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008918}
8919
8920void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008921 struct net_device *netdev, const u8 *addr,
8922 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008923{
Johannes Berge6d6e342009-07-01 21:26:47 +02008924 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8925 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008926}
8927
Samuel Ortizb23aa672009-07-01 21:26:54 +02008928void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8929 struct net_device *netdev, const u8 *bssid,
8930 const u8 *req_ie, size_t req_ie_len,
8931 const u8 *resp_ie, size_t resp_ie_len,
8932 u16 status, gfp_t gfp)
8933{
8934 struct sk_buff *msg;
8935 void *hdr;
8936
Thomas Graf58050fc2012-06-28 03:57:45 +00008937 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008938 if (!msg)
8939 return;
8940
8941 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8942 if (!hdr) {
8943 nlmsg_free(msg);
8944 return;
8945 }
8946
David S. Miller9360ffd2012-03-29 04:41:26 -04008947 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8948 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8949 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8950 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8951 (req_ie &&
8952 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8953 (resp_ie &&
8954 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8955 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008956
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008957 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008958
Johannes Berg463d0182009-07-14 00:33:35 +02008959 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8960 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008961 return;
8962
8963 nla_put_failure:
8964 genlmsg_cancel(msg, hdr);
8965 nlmsg_free(msg);
8966
8967}
8968
8969void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8970 struct net_device *netdev, const u8 *bssid,
8971 const u8 *req_ie, size_t req_ie_len,
8972 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8973{
8974 struct sk_buff *msg;
8975 void *hdr;
8976
Thomas Graf58050fc2012-06-28 03:57:45 +00008977 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008978 if (!msg)
8979 return;
8980
8981 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8982 if (!hdr) {
8983 nlmsg_free(msg);
8984 return;
8985 }
8986
David S. Miller9360ffd2012-03-29 04:41:26 -04008987 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8988 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8989 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8990 (req_ie &&
8991 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8992 (resp_ie &&
8993 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8994 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008995
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008996 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008997
Johannes Berg463d0182009-07-14 00:33:35 +02008998 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8999 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009000 return;
9001
9002 nla_put_failure:
9003 genlmsg_cancel(msg, hdr);
9004 nlmsg_free(msg);
9005
9006}
9007
9008void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9009 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02009010 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009011{
9012 struct sk_buff *msg;
9013 void *hdr;
9014
Thomas Graf58050fc2012-06-28 03:57:45 +00009015 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009016 if (!msg)
9017 return;
9018
9019 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9020 if (!hdr) {
9021 nlmsg_free(msg);
9022 return;
9023 }
9024
David S. Miller9360ffd2012-03-29 04:41:26 -04009025 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9026 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9027 (from_ap && reason &&
9028 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9029 (from_ap &&
9030 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9031 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9032 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009033
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009034 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009035
Johannes Berg463d0182009-07-14 00:33:35 +02009036 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9037 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009038 return;
9039
9040 nla_put_failure:
9041 genlmsg_cancel(msg, hdr);
9042 nlmsg_free(msg);
9043
9044}
9045
Johannes Berg04a773a2009-04-19 21:24:32 +02009046void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9047 struct net_device *netdev, const u8 *bssid,
9048 gfp_t gfp)
9049{
9050 struct sk_buff *msg;
9051 void *hdr;
9052
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009053 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009054 if (!msg)
9055 return;
9056
9057 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9058 if (!hdr) {
9059 nlmsg_free(msg);
9060 return;
9061 }
9062
David S. Miller9360ffd2012-03-29 04:41:26 -04009063 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9064 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9065 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9066 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009067
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009068 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009069
Johannes Berg463d0182009-07-14 00:33:35 +02009070 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9071 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009072 return;
9073
9074 nla_put_failure:
9075 genlmsg_cancel(msg, hdr);
9076 nlmsg_free(msg);
9077}
9078
Javier Cardonac93b5e72011-04-07 15:08:34 -07009079void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
9080 struct net_device *netdev,
9081 const u8 *macaddr, const u8* ie, u8 ie_len,
9082 gfp_t gfp)
9083{
9084 struct sk_buff *msg;
9085 void *hdr;
9086
9087 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9088 if (!msg)
9089 return;
9090
9091 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9092 if (!hdr) {
9093 nlmsg_free(msg);
9094 return;
9095 }
9096
David S. Miller9360ffd2012-03-29 04:41:26 -04009097 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9098 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9099 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
9100 (ie_len && ie &&
9101 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9102 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009103
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009104 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009105
9106 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9107 nl80211_mlme_mcgrp.id, gfp);
9108 return;
9109
9110 nla_put_failure:
9111 genlmsg_cancel(msg, hdr);
9112 nlmsg_free(msg);
9113}
9114
Jouni Malinena3b8b052009-03-27 21:59:49 +02009115void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9116 struct net_device *netdev, const u8 *addr,
9117 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009118 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009119{
9120 struct sk_buff *msg;
9121 void *hdr;
9122
Johannes Berge6d6e342009-07-01 21:26:47 +02009123 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009124 if (!msg)
9125 return;
9126
9127 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9128 if (!hdr) {
9129 nlmsg_free(msg);
9130 return;
9131 }
9132
David S. Miller9360ffd2012-03-29 04:41:26 -04009133 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9134 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9135 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9136 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9137 (key_id != -1 &&
9138 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9139 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9140 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009141
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009142 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009143
Johannes Berg463d0182009-07-14 00:33:35 +02009144 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9145 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009146 return;
9147
9148 nla_put_failure:
9149 genlmsg_cancel(msg, hdr);
9150 nlmsg_free(msg);
9151}
9152
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009153void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9154 struct ieee80211_channel *channel_before,
9155 struct ieee80211_channel *channel_after)
9156{
9157 struct sk_buff *msg;
9158 void *hdr;
9159 struct nlattr *nl_freq;
9160
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009161 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009162 if (!msg)
9163 return;
9164
9165 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9166 if (!hdr) {
9167 nlmsg_free(msg);
9168 return;
9169 }
9170
9171 /*
9172 * Since we are applying the beacon hint to a wiphy we know its
9173 * wiphy_idx is valid
9174 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009175 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9176 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009177
9178 /* Before */
9179 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9180 if (!nl_freq)
9181 goto nla_put_failure;
9182 if (nl80211_msg_put_channel(msg, channel_before))
9183 goto nla_put_failure;
9184 nla_nest_end(msg, nl_freq);
9185
9186 /* After */
9187 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9188 if (!nl_freq)
9189 goto nla_put_failure;
9190 if (nl80211_msg_put_channel(msg, channel_after))
9191 goto nla_put_failure;
9192 nla_nest_end(msg, nl_freq);
9193
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009194 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009195
Johannes Berg463d0182009-07-14 00:33:35 +02009196 rcu_read_lock();
9197 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9198 GFP_ATOMIC);
9199 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009200
9201 return;
9202
9203nla_put_failure:
9204 genlmsg_cancel(msg, hdr);
9205 nlmsg_free(msg);
9206}
9207
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009208static void nl80211_send_remain_on_chan_event(
9209 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009210 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009211 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009212 unsigned int duration, gfp_t gfp)
9213{
9214 struct sk_buff *msg;
9215 void *hdr;
9216
9217 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9218 if (!msg)
9219 return;
9220
9221 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9222 if (!hdr) {
9223 nlmsg_free(msg);
9224 return;
9225 }
9226
David S. Miller9360ffd2012-03-29 04:41:26 -04009227 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009228 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9229 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009230 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009231 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009232 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9233 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009234 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9235 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009236
David S. Miller9360ffd2012-03-29 04:41:26 -04009237 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9238 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9239 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009240
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009241 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009242
9243 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9244 nl80211_mlme_mcgrp.id, gfp);
9245 return;
9246
9247 nla_put_failure:
9248 genlmsg_cancel(msg, hdr);
9249 nlmsg_free(msg);
9250}
9251
9252void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009253 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009254 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009255 unsigned int duration, gfp_t gfp)
9256{
9257 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009258 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009259 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009260}
9261
9262void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02009263 struct cfg80211_registered_device *rdev,
9264 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01009265 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009266{
9267 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009268 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009269}
9270
Johannes Berg98b62182009-12-23 13:15:44 +01009271void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
9272 struct net_device *dev, const u8 *mac_addr,
9273 struct station_info *sinfo, gfp_t gfp)
9274{
9275 struct sk_buff *msg;
9276
Thomas Graf58050fc2012-06-28 03:57:45 +00009277 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009278 if (!msg)
9279 return;
9280
John W. Linville66266b32012-03-15 13:25:41 -04009281 if (nl80211_send_station(msg, 0, 0, 0,
9282 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009283 nlmsg_free(msg);
9284 return;
9285 }
9286
9287 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9288 nl80211_mlme_mcgrp.id, gfp);
9289}
9290
Jouni Malinenec15e682011-03-23 15:29:52 +02009291void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
9292 struct net_device *dev, const u8 *mac_addr,
9293 gfp_t gfp)
9294{
9295 struct sk_buff *msg;
9296 void *hdr;
9297
Thomas Graf58050fc2012-06-28 03:57:45 +00009298 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009299 if (!msg)
9300 return;
9301
9302 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9303 if (!hdr) {
9304 nlmsg_free(msg);
9305 return;
9306 }
9307
David S. Miller9360ffd2012-03-29 04:41:26 -04009308 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9309 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9310 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009311
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009312 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009313
9314 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9315 nl80211_mlme_mcgrp.id, gfp);
9316 return;
9317
9318 nla_put_failure:
9319 genlmsg_cancel(msg, hdr);
9320 nlmsg_free(msg);
9321}
9322
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309323void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
9324 struct net_device *dev, const u8 *mac_addr,
9325 enum nl80211_connect_failed_reason reason,
9326 gfp_t gfp)
9327{
9328 struct sk_buff *msg;
9329 void *hdr;
9330
9331 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9332 if (!msg)
9333 return;
9334
9335 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9336 if (!hdr) {
9337 nlmsg_free(msg);
9338 return;
9339 }
9340
9341 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9342 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9343 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9344 goto nla_put_failure;
9345
9346 genlmsg_end(msg, hdr);
9347
9348 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9349 nl80211_mlme_mcgrp.id, gfp);
9350 return;
9351
9352 nla_put_failure:
9353 genlmsg_cancel(msg, hdr);
9354 nlmsg_free(msg);
9355}
9356
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009357static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9358 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009359{
9360 struct wireless_dev *wdev = dev->ieee80211_ptr;
9361 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9362 struct sk_buff *msg;
9363 void *hdr;
9364 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009365 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009366
Eric W. Biederman15e47302012-09-07 20:12:54 +00009367 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009368 return false;
9369
9370 msg = nlmsg_new(100, gfp);
9371 if (!msg)
9372 return true;
9373
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009374 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009375 if (!hdr) {
9376 nlmsg_free(msg);
9377 return true;
9378 }
9379
David S. Miller9360ffd2012-03-29 04:41:26 -04009380 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9381 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9382 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9383 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009384
9385 err = genlmsg_end(msg, hdr);
9386 if (err < 0) {
9387 nlmsg_free(msg);
9388 return true;
9389 }
9390
Eric W. Biederman15e47302012-09-07 20:12:54 +00009391 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009392 return true;
9393
9394 nla_put_failure:
9395 genlmsg_cancel(msg, hdr);
9396 nlmsg_free(msg);
9397 return true;
9398}
9399
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009400bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
9401{
9402 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9403 addr, gfp);
9404}
9405
9406bool nl80211_unexpected_4addr_frame(struct net_device *dev,
9407 const u8 *addr, gfp_t gfp)
9408{
9409 return __nl80211_unexpected_frame(dev,
9410 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9411 addr, gfp);
9412}
9413
Johannes Berg2e161f72010-08-12 15:38:38 +02009414int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009415 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009416 int freq, int sig_dbm,
9417 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009418{
Johannes Berg71bbc992012-06-15 15:30:18 +02009419 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009420 struct sk_buff *msg;
9421 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009422
9423 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9424 if (!msg)
9425 return -ENOMEM;
9426
Johannes Berg2e161f72010-08-12 15:38:38 +02009427 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009428 if (!hdr) {
9429 nlmsg_free(msg);
9430 return -ENOMEM;
9431 }
9432
David S. Miller9360ffd2012-03-29 04:41:26 -04009433 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009434 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9435 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009436 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9437 (sig_dbm &&
9438 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9439 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9440 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009441
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009442 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009443
Eric W. Biederman15e47302012-09-07 20:12:54 +00009444 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009445
9446 nla_put_failure:
9447 genlmsg_cancel(msg, hdr);
9448 nlmsg_free(msg);
9449 return -ENOBUFS;
9450}
9451
Johannes Berg2e161f72010-08-12 15:38:38 +02009452void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009453 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02009454 const u8 *buf, size_t len, bool ack,
9455 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009456{
Johannes Berg71bbc992012-06-15 15:30:18 +02009457 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009458 struct sk_buff *msg;
9459 void *hdr;
9460
9461 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9462 if (!msg)
9463 return;
9464
Johannes Berg2e161f72010-08-12 15:38:38 +02009465 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009466 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) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009472 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9473 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009474 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9475 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9476 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9477 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009478
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009479 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009480
9481 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9482 return;
9483
9484 nla_put_failure:
9485 genlmsg_cancel(msg, hdr);
9486 nlmsg_free(msg);
9487}
9488
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009489void
9490nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9491 struct net_device *netdev,
9492 enum nl80211_cqm_rssi_threshold_event rssi_event,
9493 gfp_t gfp)
9494{
9495 struct sk_buff *msg;
9496 struct nlattr *pinfoattr;
9497 void *hdr;
9498
Thomas Graf58050fc2012-06-28 03:57:45 +00009499 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009500 if (!msg)
9501 return;
9502
9503 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9504 if (!hdr) {
9505 nlmsg_free(msg);
9506 return;
9507 }
9508
David S. Miller9360ffd2012-03-29 04:41:26 -04009509 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9510 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9511 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009512
9513 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9514 if (!pinfoattr)
9515 goto nla_put_failure;
9516
David S. Miller9360ffd2012-03-29 04:41:26 -04009517 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9518 rssi_event))
9519 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009520
9521 nla_nest_end(msg, pinfoattr);
9522
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009523 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009524
9525 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9526 nl80211_mlme_mcgrp.id, gfp);
9527 return;
9528
9529 nla_put_failure:
9530 genlmsg_cancel(msg, hdr);
9531 nlmsg_free(msg);
9532}
9533
Johannes Berge5497d72011-07-05 16:35:40 +02009534void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9535 struct net_device *netdev, const u8 *bssid,
9536 const u8 *replay_ctr, gfp_t gfp)
9537{
9538 struct sk_buff *msg;
9539 struct nlattr *rekey_attr;
9540 void *hdr;
9541
Thomas Graf58050fc2012-06-28 03:57:45 +00009542 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009543 if (!msg)
9544 return;
9545
9546 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9547 if (!hdr) {
9548 nlmsg_free(msg);
9549 return;
9550 }
9551
David S. Miller9360ffd2012-03-29 04:41:26 -04009552 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9553 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9554 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9555 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009556
9557 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9558 if (!rekey_attr)
9559 goto nla_put_failure;
9560
David S. Miller9360ffd2012-03-29 04:41:26 -04009561 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9562 NL80211_REPLAY_CTR_LEN, replay_ctr))
9563 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009564
9565 nla_nest_end(msg, rekey_attr);
9566
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009567 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009568
9569 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9570 nl80211_mlme_mcgrp.id, gfp);
9571 return;
9572
9573 nla_put_failure:
9574 genlmsg_cancel(msg, hdr);
9575 nlmsg_free(msg);
9576}
9577
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009578void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9579 struct net_device *netdev, int index,
9580 const u8 *bssid, bool preauth, gfp_t gfp)
9581{
9582 struct sk_buff *msg;
9583 struct nlattr *attr;
9584 void *hdr;
9585
Thomas Graf58050fc2012-06-28 03:57:45 +00009586 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009587 if (!msg)
9588 return;
9589
9590 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
9591 if (!hdr) {
9592 nlmsg_free(msg);
9593 return;
9594 }
9595
David S. Miller9360ffd2012-03-29 04:41:26 -04009596 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9597 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9598 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009599
9600 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9601 if (!attr)
9602 goto nla_put_failure;
9603
David S. Miller9360ffd2012-03-29 04:41:26 -04009604 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9605 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9606 (preauth &&
9607 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9608 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009609
9610 nla_nest_end(msg, attr);
9611
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009612 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009613
9614 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9615 nl80211_mlme_mcgrp.id, gfp);
9616 return;
9617
9618 nla_put_failure:
9619 genlmsg_cancel(msg, hdr);
9620 nlmsg_free(msg);
9621}
9622
Thomas Pedersen53145262012-04-06 13:35:47 -07009623void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009624 struct net_device *netdev,
9625 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009626{
9627 struct sk_buff *msg;
9628 void *hdr;
9629
Thomas Graf58050fc2012-06-28 03:57:45 +00009630 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009631 if (!msg)
9632 return;
9633
9634 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9635 if (!hdr) {
9636 nlmsg_free(msg);
9637 return;
9638 }
9639
Johannes Berg683b6d32012-11-08 21:25:48 +01009640 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9641 goto nla_put_failure;
9642
9643 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009644 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009645
9646 genlmsg_end(msg, hdr);
9647
9648 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9649 nl80211_mlme_mcgrp.id, gfp);
9650 return;
9651
9652 nla_put_failure:
9653 genlmsg_cancel(msg, hdr);
9654 nlmsg_free(msg);
9655}
9656
Johannes Bergc063dbf2010-11-24 08:10:05 +01009657void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009658nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9659 struct net_device *netdev, const u8 *peer,
9660 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9661{
9662 struct sk_buff *msg;
9663 struct nlattr *pinfoattr;
9664 void *hdr;
9665
9666 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9667 if (!msg)
9668 return;
9669
9670 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9671 if (!hdr) {
9672 nlmsg_free(msg);
9673 return;
9674 }
9675
9676 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9677 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9678 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9679 goto nla_put_failure;
9680
9681 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9682 if (!pinfoattr)
9683 goto nla_put_failure;
9684
9685 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9686 goto nla_put_failure;
9687
9688 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9689 goto nla_put_failure;
9690
9691 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9692 goto nla_put_failure;
9693
9694 nla_nest_end(msg, pinfoattr);
9695
9696 genlmsg_end(msg, hdr);
9697
9698 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9699 nl80211_mlme_mcgrp.id, gfp);
9700 return;
9701
9702 nla_put_failure:
9703 genlmsg_cancel(msg, hdr);
9704 nlmsg_free(msg);
9705}
9706
9707void
Simon Wunderlich04f39042013-02-08 18:16:19 +01009708nl80211_radar_notify(struct cfg80211_registered_device *rdev,
9709 struct cfg80211_chan_def *chandef,
9710 enum nl80211_radar_event event,
9711 struct net_device *netdev, gfp_t gfp)
9712{
9713 struct sk_buff *msg;
9714 void *hdr;
9715
9716 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9717 if (!msg)
9718 return;
9719
9720 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
9721 if (!hdr) {
9722 nlmsg_free(msg);
9723 return;
9724 }
9725
9726 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9727 goto nla_put_failure;
9728
9729 /* NOP and radar events don't need a netdev parameter */
9730 if (netdev) {
9731 struct wireless_dev *wdev = netdev->ieee80211_ptr;
9732
9733 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9734 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9735 goto nla_put_failure;
9736 }
9737
9738 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
9739 goto nla_put_failure;
9740
9741 if (nl80211_send_chandef(msg, chandef))
9742 goto nla_put_failure;
9743
9744 if (genlmsg_end(msg, hdr) < 0) {
9745 nlmsg_free(msg);
9746 return;
9747 }
9748
9749 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9750 nl80211_mlme_mcgrp.id, gfp);
9751 return;
9752
9753 nla_put_failure:
9754 genlmsg_cancel(msg, hdr);
9755 nlmsg_free(msg);
9756}
9757
9758void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009759nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9760 struct net_device *netdev, const u8 *peer,
9761 u32 num_packets, gfp_t gfp)
9762{
9763 struct sk_buff *msg;
9764 struct nlattr *pinfoattr;
9765 void *hdr;
9766
Thomas Graf58050fc2012-06-28 03:57:45 +00009767 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009768 if (!msg)
9769 return;
9770
9771 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9772 if (!hdr) {
9773 nlmsg_free(msg);
9774 return;
9775 }
9776
David S. Miller9360ffd2012-03-29 04:41:26 -04009777 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9778 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9779 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9780 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009781
9782 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9783 if (!pinfoattr)
9784 goto nla_put_failure;
9785
David S. Miller9360ffd2012-03-29 04:41:26 -04009786 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9787 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009788
9789 nla_nest_end(msg, pinfoattr);
9790
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009791 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009792
9793 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9794 nl80211_mlme_mcgrp.id, gfp);
9795 return;
9796
9797 nla_put_failure:
9798 genlmsg_cancel(msg, hdr);
9799 nlmsg_free(msg);
9800}
9801
Johannes Berg7f6cf312011-11-04 11:18:15 +01009802void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9803 u64 cookie, bool acked, gfp_t gfp)
9804{
9805 struct wireless_dev *wdev = dev->ieee80211_ptr;
9806 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9807 struct sk_buff *msg;
9808 void *hdr;
9809 int err;
9810
Beni Lev4ee3e062012-08-27 12:49:39 +03009811 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9812
Thomas Graf58050fc2012-06-28 03:57:45 +00009813 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009814
Johannes Berg7f6cf312011-11-04 11:18:15 +01009815 if (!msg)
9816 return;
9817
9818 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9819 if (!hdr) {
9820 nlmsg_free(msg);
9821 return;
9822 }
9823
David S. Miller9360ffd2012-03-29 04:41:26 -04009824 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9825 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9826 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9827 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9828 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9829 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009830
9831 err = genlmsg_end(msg, hdr);
9832 if (err < 0) {
9833 nlmsg_free(msg);
9834 return;
9835 }
9836
9837 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9838 nl80211_mlme_mcgrp.id, gfp);
9839 return;
9840
9841 nla_put_failure:
9842 genlmsg_cancel(msg, hdr);
9843 nlmsg_free(msg);
9844}
9845EXPORT_SYMBOL(cfg80211_probe_status);
9846
Johannes Berg5e7602302011-11-04 11:18:17 +01009847void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9848 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009849 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +01009850{
9851 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9852 struct sk_buff *msg;
9853 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009854 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +01009855
Beni Lev4ee3e062012-08-27 12:49:39 +03009856 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9857
Ben Greear37c73b52012-10-26 14:49:25 -07009858 spin_lock_bh(&rdev->beacon_registrations_lock);
9859 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9860 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9861 if (!msg) {
9862 spin_unlock_bh(&rdev->beacon_registrations_lock);
9863 return;
9864 }
Johannes Berg5e7602302011-11-04 11:18:17 +01009865
Ben Greear37c73b52012-10-26 14:49:25 -07009866 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9867 if (!hdr)
9868 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01009869
Ben Greear37c73b52012-10-26 14:49:25 -07009870 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9871 (freq &&
9872 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9873 (sig_dbm &&
9874 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9875 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9876 goto nla_put_failure;
9877
9878 genlmsg_end(msg, hdr);
9879
9880 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +01009881 }
Ben Greear37c73b52012-10-26 14:49:25 -07009882 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01009883 return;
9884
9885 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009886 spin_unlock_bh(&rdev->beacon_registrations_lock);
9887 if (hdr)
9888 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +01009889 nlmsg_free(msg);
9890}
9891EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9892
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009893#ifdef CONFIG_PM
9894void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
9895 struct cfg80211_wowlan_wakeup *wakeup,
9896 gfp_t gfp)
9897{
9898 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9899 struct sk_buff *msg;
9900 void *hdr;
9901 int err, size = 200;
9902
9903 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
9904
9905 if (wakeup)
9906 size += wakeup->packet_present_len;
9907
9908 msg = nlmsg_new(size, gfp);
9909 if (!msg)
9910 return;
9911
9912 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
9913 if (!hdr)
9914 goto free_msg;
9915
9916 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9917 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9918 goto free_msg;
9919
9920 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9921 wdev->netdev->ifindex))
9922 goto free_msg;
9923
9924 if (wakeup) {
9925 struct nlattr *reasons;
9926
9927 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9928
9929 if (wakeup->disconnect &&
9930 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
9931 goto free_msg;
9932 if (wakeup->magic_pkt &&
9933 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
9934 goto free_msg;
9935 if (wakeup->gtk_rekey_failure &&
9936 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
9937 goto free_msg;
9938 if (wakeup->eap_identity_req &&
9939 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
9940 goto free_msg;
9941 if (wakeup->four_way_handshake &&
9942 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
9943 goto free_msg;
9944 if (wakeup->rfkill_release &&
9945 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
9946 goto free_msg;
9947
9948 if (wakeup->pattern_idx >= 0 &&
9949 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
9950 wakeup->pattern_idx))
9951 goto free_msg;
9952
Johannes Berg2a0e0472013-01-23 22:57:40 +01009953 if (wakeup->tcp_match)
9954 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
9955
9956 if (wakeup->tcp_connlost)
9957 nla_put_flag(msg,
9958 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
9959
9960 if (wakeup->tcp_nomoretokens)
9961 nla_put_flag(msg,
9962 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
9963
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009964 if (wakeup->packet) {
9965 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
9966 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
9967
9968 if (!wakeup->packet_80211) {
9969 pkt_attr =
9970 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
9971 len_attr =
9972 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
9973 }
9974
9975 if (wakeup->packet_len &&
9976 nla_put_u32(msg, len_attr, wakeup->packet_len))
9977 goto free_msg;
9978
9979 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
9980 wakeup->packet))
9981 goto free_msg;
9982 }
9983
9984 nla_nest_end(msg, reasons);
9985 }
9986
9987 err = genlmsg_end(msg, hdr);
9988 if (err < 0)
9989 goto free_msg;
9990
9991 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9992 nl80211_mlme_mcgrp.id, gfp);
9993 return;
9994
9995 free_msg:
9996 nlmsg_free(msg);
9997}
9998EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
9999#endif
10000
Jouni Malinen3475b092012-11-16 22:49:57 +020010001void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10002 enum nl80211_tdls_operation oper,
10003 u16 reason_code, gfp_t gfp)
10004{
10005 struct wireless_dev *wdev = dev->ieee80211_ptr;
10006 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10007 struct sk_buff *msg;
10008 void *hdr;
10009 int err;
10010
10011 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10012 reason_code);
10013
10014 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10015 if (!msg)
10016 return;
10017
10018 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10019 if (!hdr) {
10020 nlmsg_free(msg);
10021 return;
10022 }
10023
10024 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10025 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10026 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10027 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10028 (reason_code > 0 &&
10029 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10030 goto nla_put_failure;
10031
10032 err = genlmsg_end(msg, hdr);
10033 if (err < 0) {
10034 nlmsg_free(msg);
10035 return;
10036 }
10037
10038 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10039 nl80211_mlme_mcgrp.id, gfp);
10040 return;
10041
10042 nla_put_failure:
10043 genlmsg_cancel(msg, hdr);
10044 nlmsg_free(msg);
10045}
10046EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10047
Jouni Malinen026331c2010-02-15 12:53:10 +020010048static int nl80211_netlink_notify(struct notifier_block * nb,
10049 unsigned long state,
10050 void *_notify)
10051{
10052 struct netlink_notify *notify = _notify;
10053 struct cfg80211_registered_device *rdev;
10054 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010055 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010056
10057 if (state != NETLINK_URELEASE)
10058 return NOTIFY_DONE;
10059
10060 rcu_read_lock();
10061
Johannes Berg5e7602302011-11-04 11:18:17 +010010062 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010063 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010064 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010065
10066 spin_lock_bh(&rdev->beacon_registrations_lock);
10067 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10068 list) {
10069 if (reg->nlportid == notify->portid) {
10070 list_del(&reg->list);
10071 kfree(reg);
10072 break;
10073 }
10074 }
10075 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010010076 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010077
10078 rcu_read_unlock();
10079
10080 return NOTIFY_DONE;
10081}
10082
10083static struct notifier_block nl80211_netlink_notifier = {
10084 .notifier_call = nl80211_netlink_notify,
10085};
10086
Johannes Berg55682962007-09-20 13:09:35 -040010087/* initialisation/exit functions */
10088
10089int nl80211_init(void)
10090{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010091 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010092
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010093 err = genl_register_family_with_ops(&nl80211_fam,
10094 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010095 if (err)
10096 return err;
10097
Johannes Berg55682962007-09-20 13:09:35 -040010098 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10099 if (err)
10100 goto err_out;
10101
Johannes Berg2a519312009-02-10 21:25:55 +010010102 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10103 if (err)
10104 goto err_out;
10105
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010106 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10107 if (err)
10108 goto err_out;
10109
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010110 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10111 if (err)
10112 goto err_out;
10113
Johannes Bergaff89a92009-07-01 21:26:51 +020010114#ifdef CONFIG_NL80211_TESTMODE
10115 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10116 if (err)
10117 goto err_out;
10118#endif
10119
Jouni Malinen026331c2010-02-15 12:53:10 +020010120 err = netlink_register_notifier(&nl80211_netlink_notifier);
10121 if (err)
10122 goto err_out;
10123
Johannes Berg55682962007-09-20 13:09:35 -040010124 return 0;
10125 err_out:
10126 genl_unregister_family(&nl80211_fam);
10127 return err;
10128}
10129
10130void nl80211_exit(void)
10131{
Jouni Malinen026331c2010-02-15 12:53:10 +020010132 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010133 genl_unregister_family(&nl80211_fam);
10134}