blob: 3b82f95b1a7cc10e8ed1e80a154339fb9944ee07 [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;
Johannes Berg1c33a052013-02-18 23:44:38 +0100557 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
558 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
559 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400560
David S. Miller9360ffd2012-03-29 04:41:26 -0400561 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
562 DBM_TO_MBM(chan->max_power)))
563 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400564
565 return 0;
566
567 nla_put_failure:
568 return -ENOBUFS;
569}
570
Johannes Berg55682962007-09-20 13:09:35 -0400571/* netlink command implementations */
572
Johannes Bergb9454e82009-07-08 13:29:08 +0200573struct key_parse {
574 struct key_params p;
575 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200576 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200577 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100578 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200579};
580
581static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
582{
583 struct nlattr *tb[NL80211_KEY_MAX + 1];
584 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
585 nl80211_key_policy);
586 if (err)
587 return err;
588
589 k->def = !!tb[NL80211_KEY_DEFAULT];
590 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
591
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100592 if (k->def) {
593 k->def_uni = true;
594 k->def_multi = true;
595 }
596 if (k->defmgmt)
597 k->def_multi = true;
598
Johannes Bergb9454e82009-07-08 13:29:08 +0200599 if (tb[NL80211_KEY_IDX])
600 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
601
602 if (tb[NL80211_KEY_DATA]) {
603 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
604 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
605 }
606
607 if (tb[NL80211_KEY_SEQ]) {
608 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
609 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
610 }
611
612 if (tb[NL80211_KEY_CIPHER])
613 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
614
Johannes Berge31b8212010-10-05 19:39:30 +0200615 if (tb[NL80211_KEY_TYPE]) {
616 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
617 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
618 return -EINVAL;
619 }
620
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100621 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
622 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100623 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
624 tb[NL80211_KEY_DEFAULT_TYPES],
625 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100626 if (err)
627 return err;
628
629 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
630 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
631 }
632
Johannes Bergb9454e82009-07-08 13:29:08 +0200633 return 0;
634}
635
636static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
637{
638 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
639 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
640 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
641 }
642
643 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
644 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
645 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
646 }
647
648 if (info->attrs[NL80211_ATTR_KEY_IDX])
649 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
650
651 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
652 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
653
654 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
655 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
656
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100657 if (k->def) {
658 k->def_uni = true;
659 k->def_multi = true;
660 }
661 if (k->defmgmt)
662 k->def_multi = true;
663
Johannes Berge31b8212010-10-05 19:39:30 +0200664 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
665 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
666 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
667 return -EINVAL;
668 }
669
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100670 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
671 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
672 int err = nla_parse_nested(
673 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
674 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
675 nl80211_key_default_policy);
676 if (err)
677 return err;
678
679 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
680 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
681 }
682
Johannes Bergb9454e82009-07-08 13:29:08 +0200683 return 0;
684}
685
686static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
687{
688 int err;
689
690 memset(k, 0, sizeof(*k));
691 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200692 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200693
694 if (info->attrs[NL80211_ATTR_KEY])
695 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
696 else
697 err = nl80211_parse_key_old(info, k);
698
699 if (err)
700 return err;
701
702 if (k->def && k->defmgmt)
703 return -EINVAL;
704
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100705 if (k->defmgmt) {
706 if (k->def_uni || !k->def_multi)
707 return -EINVAL;
708 }
709
Johannes Bergb9454e82009-07-08 13:29:08 +0200710 if (k->idx != -1) {
711 if (k->defmgmt) {
712 if (k->idx < 4 || k->idx > 5)
713 return -EINVAL;
714 } else if (k->def) {
715 if (k->idx < 0 || k->idx > 3)
716 return -EINVAL;
717 } else {
718 if (k->idx < 0 || k->idx > 5)
719 return -EINVAL;
720 }
721 }
722
723 return 0;
724}
725
Johannes Bergfffd0932009-07-08 14:22:54 +0200726static struct cfg80211_cached_keys *
727nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530728 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200729{
730 struct key_parse parse;
731 struct nlattr *key;
732 struct cfg80211_cached_keys *result;
733 int rem, err, def = 0;
734
735 result = kzalloc(sizeof(*result), GFP_KERNEL);
736 if (!result)
737 return ERR_PTR(-ENOMEM);
738
739 result->def = -1;
740 result->defmgmt = -1;
741
742 nla_for_each_nested(key, keys, rem) {
743 memset(&parse, 0, sizeof(parse));
744 parse.idx = -1;
745
746 err = nl80211_parse_key_new(key, &parse);
747 if (err)
748 goto error;
749 err = -EINVAL;
750 if (!parse.p.key)
751 goto error;
752 if (parse.idx < 0 || parse.idx > 4)
753 goto error;
754 if (parse.def) {
755 if (def)
756 goto error;
757 def = 1;
758 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100759 if (!parse.def_uni || !parse.def_multi)
760 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200761 } else if (parse.defmgmt)
762 goto error;
763 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200764 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200765 if (err)
766 goto error;
767 result->params[parse.idx].cipher = parse.p.cipher;
768 result->params[parse.idx].key_len = parse.p.key_len;
769 result->params[parse.idx].key = result->data[parse.idx];
770 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530771
772 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
773 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
774 if (no_ht)
775 *no_ht = true;
776 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200777 }
778
779 return result;
780 error:
781 kfree(result);
782 return ERR_PTR(err);
783}
784
785static int nl80211_key_allowed(struct wireless_dev *wdev)
786{
787 ASSERT_WDEV_LOCK(wdev);
788
Johannes Bergfffd0932009-07-08 14:22:54 +0200789 switch (wdev->iftype) {
790 case NL80211_IFTYPE_AP:
791 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200792 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700793 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200794 break;
795 case NL80211_IFTYPE_ADHOC:
796 if (!wdev->current_bss)
797 return -ENOLINK;
798 break;
799 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200800 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200801 if (wdev->sme_state != CFG80211_SME_CONNECTED)
802 return -ENOLINK;
803 break;
804 default:
805 return -EINVAL;
806 }
807
808 return 0;
809}
810
Johannes Berg7527a782011-05-13 10:58:57 +0200811static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
812{
813 struct nlattr *nl_modes = nla_nest_start(msg, attr);
814 int i;
815
816 if (!nl_modes)
817 goto nla_put_failure;
818
819 i = 0;
820 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400821 if ((ifmodes & 1) && nla_put_flag(msg, i))
822 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200823 ifmodes >>= 1;
824 i++;
825 }
826
827 nla_nest_end(msg, nl_modes);
828 return 0;
829
830nla_put_failure:
831 return -ENOBUFS;
832}
833
834static int nl80211_put_iface_combinations(struct wiphy *wiphy,
835 struct sk_buff *msg)
836{
837 struct nlattr *nl_combis;
838 int i, j;
839
840 nl_combis = nla_nest_start(msg,
841 NL80211_ATTR_INTERFACE_COMBINATIONS);
842 if (!nl_combis)
843 goto nla_put_failure;
844
845 for (i = 0; i < wiphy->n_iface_combinations; i++) {
846 const struct ieee80211_iface_combination *c;
847 struct nlattr *nl_combi, *nl_limits;
848
849 c = &wiphy->iface_combinations[i];
850
851 nl_combi = nla_nest_start(msg, i + 1);
852 if (!nl_combi)
853 goto nla_put_failure;
854
855 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
856 if (!nl_limits)
857 goto nla_put_failure;
858
859 for (j = 0; j < c->n_limits; j++) {
860 struct nlattr *nl_limit;
861
862 nl_limit = nla_nest_start(msg, j + 1);
863 if (!nl_limit)
864 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400865 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
866 c->limits[j].max))
867 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200868 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
869 c->limits[j].types))
870 goto nla_put_failure;
871 nla_nest_end(msg, nl_limit);
872 }
873
874 nla_nest_end(msg, nl_limits);
875
David S. Miller9360ffd2012-03-29 04:41:26 -0400876 if (c->beacon_int_infra_match &&
877 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
878 goto nla_put_failure;
879 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
880 c->num_different_channels) ||
881 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
882 c->max_interfaces))
883 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200884
885 nla_nest_end(msg, nl_combi);
886 }
887
888 nla_nest_end(msg, nl_combis);
889
890 return 0;
891nla_put_failure:
892 return -ENOBUFS;
893}
894
Eric W. Biederman15e47302012-09-07 20:12:54 +0000895static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400896 struct cfg80211_registered_device *dev)
897{
898 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100899 struct nlattr *nl_bands, *nl_band;
900 struct nlattr *nl_freqs, *nl_freq;
901 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100902 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100903 enum ieee80211_band band;
904 struct ieee80211_channel *chan;
905 struct ieee80211_rate *rate;
906 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200907 const struct ieee80211_txrx_stypes *mgmt_stypes =
908 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400909
Eric W. Biederman15e47302012-09-07 20:12:54 +0000910 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400911 if (!hdr)
912 return -1;
913
David S. Miller9360ffd2012-03-29 04:41:26 -0400914 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
915 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
916 nla_put_u32(msg, NL80211_ATTR_GENERATION,
917 cfg80211_rdev_list_generation) ||
918 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
919 dev->wiphy.retry_short) ||
920 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
921 dev->wiphy.retry_long) ||
922 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
923 dev->wiphy.frag_threshold) ||
924 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
925 dev->wiphy.rts_threshold) ||
926 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
927 dev->wiphy.coverage_class) ||
928 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
929 dev->wiphy.max_scan_ssids) ||
930 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
931 dev->wiphy.max_sched_scan_ssids) ||
932 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
933 dev->wiphy.max_scan_ie_len) ||
934 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
935 dev->wiphy.max_sched_scan_ie_len) ||
936 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
937 dev->wiphy.max_match_sets))
938 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200939
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
941 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
942 goto nla_put_failure;
943 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
944 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
945 goto nla_put_failure;
946 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
947 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
948 goto nla_put_failure;
949 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
950 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
951 goto nla_put_failure;
952 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
953 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
954 goto nla_put_failure;
955 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
956 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
957 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200958
David S. Miller9360ffd2012-03-29 04:41:26 -0400959 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
960 sizeof(u32) * dev->wiphy.n_cipher_suites,
961 dev->wiphy.cipher_suites))
962 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100963
David S. Miller9360ffd2012-03-29 04:41:26 -0400964 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
965 dev->wiphy.max_num_pmkids))
966 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530967
David S. Miller9360ffd2012-03-29 04:41:26 -0400968 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
969 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
970 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200971
David S. Miller9360ffd2012-03-29 04:41:26 -0400972 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
973 dev->wiphy.available_antennas_tx) ||
974 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
975 dev->wiphy.available_antennas_rx))
976 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100977
David S. Miller9360ffd2012-03-29 04:41:26 -0400978 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
979 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
980 dev->wiphy.probe_resp_offload))
981 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200982
Bruno Randolf7f531e02010-12-16 11:30:22 +0900983 if ((dev->wiphy.available_antennas_tx ||
984 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900985 u32 tx_ant = 0, rx_ant = 0;
986 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300987 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900988 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400989 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
990 tx_ant) ||
991 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
992 rx_ant))
993 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900994 }
995 }
996
Johannes Berg7527a782011-05-13 10:58:57 +0200997 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
998 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700999 goto nla_put_failure;
1000
Johannes Bergee688b002008-01-24 19:38:39 +01001001 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1002 if (!nl_bands)
1003 goto nla_put_failure;
1004
1005 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1006 if (!dev->wiphy.bands[band])
1007 continue;
1008
1009 nl_band = nla_nest_start(msg, band);
1010 if (!nl_band)
1011 goto nla_put_failure;
1012
Johannes Bergd51626d2008-10-09 12:20:13 +02001013 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -04001014 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
1015 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1016 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
1017 &dev->wiphy.bands[band]->ht_cap.mcs) ||
1018 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1019 dev->wiphy.bands[band]->ht_cap.cap) ||
1020 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1021 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
1022 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1023 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
1024 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001025
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001026 /* add VHT info */
1027 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1028 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1029 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1030 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1031 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1032 dev->wiphy.bands[band]->vht_cap.cap)))
1033 goto nla_put_failure;
1034
Johannes Bergee688b002008-01-24 19:38:39 +01001035 /* add frequencies */
1036 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1037 if (!nl_freqs)
1038 goto nla_put_failure;
1039
1040 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1041 nl_freq = nla_nest_start(msg, i);
1042 if (!nl_freq)
1043 goto nla_put_failure;
1044
1045 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001046
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001047 if (nl80211_msg_put_channel(msg, chan))
1048 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001049
Johannes Bergee688b002008-01-24 19:38:39 +01001050 nla_nest_end(msg, nl_freq);
1051 }
1052
1053 nla_nest_end(msg, nl_freqs);
1054
1055 /* add bitrates */
1056 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1057 if (!nl_rates)
1058 goto nla_put_failure;
1059
1060 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1061 nl_rate = nla_nest_start(msg, i);
1062 if (!nl_rate)
1063 goto nla_put_failure;
1064
1065 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001066 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1067 rate->bitrate))
1068 goto nla_put_failure;
1069 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1070 nla_put_flag(msg,
1071 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1072 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001073
1074 nla_nest_end(msg, nl_rate);
1075 }
1076
1077 nla_nest_end(msg, nl_rates);
1078
1079 nla_nest_end(msg, nl_band);
1080 }
1081 nla_nest_end(msg, nl_bands);
1082
Johannes Berg8fdc6212009-03-14 09:34:01 +01001083 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1084 if (!nl_cmds)
1085 goto nla_put_failure;
1086
1087 i = 0;
1088#define CMD(op, n) \
1089 do { \
1090 if (dev->ops->op) { \
1091 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001092 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1093 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001094 } \
1095 } while (0)
1096
1097 CMD(add_virtual_intf, NEW_INTERFACE);
1098 CMD(change_virtual_intf, SET_INTERFACE);
1099 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001100 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001101 CMD(add_station, NEW_STATION);
1102 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001103 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001104 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001105 CMD(auth, AUTHENTICATE);
1106 CMD(assoc, ASSOCIATE);
1107 CMD(deauth, DEAUTHENTICATE);
1108 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001109 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001110 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001111 CMD(set_pmksa, SET_PMKSA);
1112 CMD(del_pmksa, DEL_PMKSA);
1113 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001114 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1115 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001116 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001117 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001118 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001119 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001120 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001121 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1122 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001123 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001124 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001125 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001126 i++;
1127 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1128 goto nla_put_failure;
1129 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001130 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001131 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1132 CMD(tdls_mgmt, TDLS_MGMT);
1133 CMD(tdls_oper, TDLS_OPER);
1134 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001135 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1136 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001137 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001138 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e7602302011-11-04 11:18:17 +01001139 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1140 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001141 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1142 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01001143 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001144 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001145 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001146
Kalle Valo4745fc02011-11-17 19:06:10 +02001147#ifdef CONFIG_NL80211_TESTMODE
1148 CMD(testmode_cmd, TESTMODE);
1149#endif
1150
Johannes Berg8fdc6212009-03-14 09:34:01 +01001151#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001152
Johannes Berg6829c872009-07-02 09:13:27 +02001153 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001154 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001155 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1156 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001157 }
1158
Johannes Berg6829c872009-07-02 09:13:27 +02001159 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001160 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001161 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1162 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001163 }
1164
Johannes Berg8fdc6212009-03-14 09:34:01 +01001165 nla_nest_end(msg, nl_cmds);
1166
Johannes Berg7c4ef712011-11-18 15:33:48 +01001167 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001168 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1169 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1170 dev->wiphy.max_remain_on_channel_duration))
1171 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001172
David S. Miller9360ffd2012-03-29 04:41:26 -04001173 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1174 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1175 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001176
Johannes Berg2e161f72010-08-12 15:38:38 +02001177 if (mgmt_stypes) {
1178 u16 stypes;
1179 struct nlattr *nl_ftypes, *nl_ifs;
1180 enum nl80211_iftype ift;
1181
1182 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1183 if (!nl_ifs)
1184 goto nla_put_failure;
1185
1186 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1187 nl_ftypes = nla_nest_start(msg, ift);
1188 if (!nl_ftypes)
1189 goto nla_put_failure;
1190 i = 0;
1191 stypes = mgmt_stypes[ift].tx;
1192 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001193 if ((stypes & 1) &&
1194 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1195 (i << 4) | IEEE80211_FTYPE_MGMT))
1196 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001197 stypes >>= 1;
1198 i++;
1199 }
1200 nla_nest_end(msg, nl_ftypes);
1201 }
1202
Johannes Berg74b70a42010-08-24 12:15:53 +02001203 nla_nest_end(msg, nl_ifs);
1204
Johannes Berg2e161f72010-08-12 15:38:38 +02001205 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1206 if (!nl_ifs)
1207 goto nla_put_failure;
1208
1209 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1210 nl_ftypes = nla_nest_start(msg, ift);
1211 if (!nl_ftypes)
1212 goto nla_put_failure;
1213 i = 0;
1214 stypes = mgmt_stypes[ift].rx;
1215 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001216 if ((stypes & 1) &&
1217 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1218 (i << 4) | IEEE80211_FTYPE_MGMT))
1219 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001220 stypes >>= 1;
1221 i++;
1222 }
1223 nla_nest_end(msg, nl_ftypes);
1224 }
1225 nla_nest_end(msg, nl_ifs);
1226 }
1227
Johannes Bergdfb89c52012-06-27 09:23:48 +02001228#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001229 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1230 struct nlattr *nl_wowlan;
1231
1232 nl_wowlan = nla_nest_start(msg,
1233 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1234 if (!nl_wowlan)
1235 goto nla_put_failure;
1236
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1238 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1239 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1240 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1241 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1242 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1243 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1244 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1245 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1246 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1247 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1248 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1249 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1250 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1251 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1252 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1253 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001254 if (dev->wiphy.wowlan.n_patterns) {
1255 struct nl80211_wowlan_pattern_support pat = {
1256 .max_patterns = dev->wiphy.wowlan.n_patterns,
1257 .min_pattern_len =
1258 dev->wiphy.wowlan.pattern_min_len,
1259 .max_pattern_len =
1260 dev->wiphy.wowlan.pattern_max_len,
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08001261 .max_pkt_offset =
1262 dev->wiphy.wowlan.max_pkt_offset,
Johannes Bergff1b6e62011-05-04 15:37:28 +02001263 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001264 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1265 sizeof(pat), &pat))
1266 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001267 }
1268
1269 nla_nest_end(msg, nl_wowlan);
1270 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001271#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001272
Johannes Berg7527a782011-05-13 10:58:57 +02001273 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1274 dev->wiphy.software_iftypes))
1275 goto nla_put_failure;
1276
1277 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1278 goto nla_put_failure;
1279
David S. Miller9360ffd2012-03-29 04:41:26 -04001280 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1281 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1282 dev->wiphy.ap_sme_capa))
1283 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001284
David S. Miller9360ffd2012-03-29 04:41:26 -04001285 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1286 dev->wiphy.features))
1287 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001288
David S. Miller9360ffd2012-03-29 04:41:26 -04001289 if (dev->wiphy.ht_capa_mod_mask &&
1290 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1291 sizeof(*dev->wiphy.ht_capa_mod_mask),
1292 dev->wiphy.ht_capa_mod_mask))
1293 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001294
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05301295 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1296 dev->wiphy.max_acl_mac_addrs &&
1297 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1298 dev->wiphy.max_acl_mac_addrs))
1299 goto nla_put_failure;
1300
Johannes Berg55682962007-09-20 13:09:35 -04001301 return genlmsg_end(msg, hdr);
1302
1303 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001304 genlmsg_cancel(msg, hdr);
1305 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001306}
1307
1308static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1309{
Johannes Berg645e77d2013-03-01 14:03:49 +01001310 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001311 int start = cb->args[0];
1312 struct cfg80211_registered_device *dev;
1313
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001314 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001315 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001316 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1317 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001318 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001319 continue;
Johannes Berg645e77d2013-03-01 14:03:49 +01001320 ret = nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
1321 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1322 dev);
1323 if (ret < 0) {
1324 /*
1325 * If sending the wiphy data didn't fit (ENOBUFS or
1326 * EMSGSIZE returned), this SKB is still empty (so
1327 * it's not too big because another wiphy dataset is
1328 * already in the skb) and we've not tried to adjust
1329 * the dump allocation yet ... then adjust the alloc
1330 * size to be bigger, and return 1 but with the empty
1331 * skb. This results in an empty message being RX'ed
1332 * in userspace, but that is ignored.
1333 *
1334 * We can then retry with the larger buffer.
1335 */
1336 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1337 !skb->len &&
1338 cb->min_dump_alloc < 4096) {
1339 cb->min_dump_alloc = 4096;
1340 mutex_unlock(&cfg80211_mutex);
1341 return 1;
1342 }
Julius Volzb4637272008-07-08 14:02:19 +02001343 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001344 break;
Julius Volzb4637272008-07-08 14:02:19 +02001345 }
Johannes Berg55682962007-09-20 13:09:35 -04001346 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001347 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001348
1349 cb->args[0] = idx;
1350
1351 return skb->len;
1352}
1353
1354static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1355{
1356 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001357 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001358
Johannes Berg645e77d2013-03-01 14:03:49 +01001359 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001360 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001361 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001362
Eric W. Biederman15e47302012-09-07 20:12:54 +00001363 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001364 nlmsg_free(msg);
1365 return -ENOBUFS;
1366 }
Johannes Berg55682962007-09-20 13:09:35 -04001367
Johannes Berg134e6372009-07-10 09:51:34 +00001368 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001369}
1370
Jouni Malinen31888482008-10-30 16:59:24 +02001371static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1372 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1373 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1374 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1375 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1376 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1377};
1378
1379static int parse_txq_params(struct nlattr *tb[],
1380 struct ieee80211_txq_params *txq_params)
1381{
Johannes Berga3304b02012-03-28 11:04:24 +02001382 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001383 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1384 !tb[NL80211_TXQ_ATTR_AIFS])
1385 return -EINVAL;
1386
Johannes Berga3304b02012-03-28 11:04:24 +02001387 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001388 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1389 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1390 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1391 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1392
Johannes Berga3304b02012-03-28 11:04:24 +02001393 if (txq_params->ac >= NL80211_NUM_ACS)
1394 return -EINVAL;
1395
Jouni Malinen31888482008-10-30 16:59:24 +02001396 return 0;
1397}
1398
Johannes Bergf444de02010-05-05 15:25:02 +02001399static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1400{
1401 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001402 * You can only set the channel explicitly for WDS interfaces,
1403 * all others have their channel managed via their respective
1404 * "establish a connection" command (connect, join, ...)
1405 *
1406 * For AP/GO and mesh mode, the channel can be set with the
1407 * channel userspace API, but is only stored and passed to the
1408 * low-level driver when the AP starts or the mesh is joined.
1409 * This is for backward compatibility, userspace can also give
1410 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001411 *
1412 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001413 * whatever else is going on, so they have their own special
1414 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001415 */
1416 return !wdev ||
1417 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001418 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001419 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1420 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001421}
1422
Johannes Berg683b6d32012-11-08 21:25:48 +01001423static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1424 struct genl_info *info,
1425 struct cfg80211_chan_def *chandef)
1426{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301427 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001428
1429 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1430 return -EINVAL;
1431
1432 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1433
1434 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001435 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1436 chandef->center_freq1 = control_freq;
1437 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001438
1439 /* Primary channel not allowed */
1440 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1441 return -EINVAL;
1442
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001443 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1444 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001445
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001446 chantype = nla_get_u32(
1447 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1448
1449 switch (chantype) {
1450 case NL80211_CHAN_NO_HT:
1451 case NL80211_CHAN_HT20:
1452 case NL80211_CHAN_HT40PLUS:
1453 case NL80211_CHAN_HT40MINUS:
1454 cfg80211_chandef_create(chandef, chandef->chan,
1455 chantype);
1456 break;
1457 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001458 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001459 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001460 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1461 chandef->width =
1462 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1463 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1464 chandef->center_freq1 =
1465 nla_get_u32(
1466 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1467 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1468 chandef->center_freq2 =
1469 nla_get_u32(
1470 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1471 }
1472
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001473 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001474 return -EINVAL;
1475
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001476 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1477 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001478 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001479
Johannes Berg683b6d32012-11-08 21:25:48 +01001480 return 0;
1481}
1482
Johannes Bergf444de02010-05-05 15:25:02 +02001483static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1484 struct wireless_dev *wdev,
1485 struct genl_info *info)
1486{
Johannes Berg683b6d32012-11-08 21:25:48 +01001487 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001488 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001489 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1490
1491 if (wdev)
1492 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001493
Johannes Bergf444de02010-05-05 15:25:02 +02001494 if (!nl80211_can_set_dev_channel(wdev))
1495 return -EOPNOTSUPP;
1496
Johannes Berg683b6d32012-11-08 21:25:48 +01001497 result = nl80211_parse_chandef(rdev, info, &chandef);
1498 if (result)
1499 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001500
1501 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001502 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001503 case NL80211_IFTYPE_AP:
1504 case NL80211_IFTYPE_P2P_GO:
1505 if (wdev->beacon_interval) {
1506 result = -EBUSY;
1507 break;
1508 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001509 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001510 result = -EINVAL;
1511 break;
1512 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001513 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001514 result = 0;
1515 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001516 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001517 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001518 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001519 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001520 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001521 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001522 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001523 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001524 }
1525 mutex_unlock(&rdev->devlist_mtx);
1526
1527 return result;
1528}
1529
1530static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1531{
Johannes Berg4c476992010-10-04 21:36:35 +02001532 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1533 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001534
Johannes Berg4c476992010-10-04 21:36:35 +02001535 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001536}
1537
Bill Jordane8347eb2010-10-01 13:54:28 -04001538static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1539{
Johannes Berg43b19952010-10-07 13:10:30 +02001540 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1541 struct net_device *dev = info->user_ptr[1];
1542 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001543 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001544
1545 if (!info->attrs[NL80211_ATTR_MAC])
1546 return -EINVAL;
1547
Johannes Berg43b19952010-10-07 13:10:30 +02001548 if (netif_running(dev))
1549 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001550
Johannes Berg43b19952010-10-07 13:10:30 +02001551 if (!rdev->ops->set_wds_peer)
1552 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001553
Johannes Berg43b19952010-10-07 13:10:30 +02001554 if (wdev->iftype != NL80211_IFTYPE_WDS)
1555 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001556
1557 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001558 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001559}
1560
1561
Johannes Berg55682962007-09-20 13:09:35 -04001562static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1563{
1564 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001565 struct net_device *netdev = NULL;
1566 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001567 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001568 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001569 u32 changed;
1570 u8 retry_short = 0, retry_long = 0;
1571 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001572 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001573
Johannes Bergf444de02010-05-05 15:25:02 +02001574 /*
1575 * Try to find the wiphy and netdev. Normally this
1576 * function shouldn't need the netdev, but this is
1577 * done for backward compatibility -- previously
1578 * setting the channel was done per wiphy, but now
1579 * it is per netdev. Previous userland like hostapd
1580 * also passed a netdev to set_wiphy, so that it is
1581 * possible to let that go to the right netdev!
1582 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001583 mutex_lock(&cfg80211_mutex);
1584
Johannes Bergf444de02010-05-05 15:25:02 +02001585 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1586 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1587
1588 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1589 if (netdev && netdev->ieee80211_ptr) {
1590 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1591 mutex_lock(&rdev->mtx);
1592 } else
1593 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001594 }
1595
Johannes Bergf444de02010-05-05 15:25:02 +02001596 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001597 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1598 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001599 if (IS_ERR(rdev)) {
1600 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001601 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001602 }
1603 wdev = NULL;
1604 netdev = NULL;
1605 result = 0;
1606
1607 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001608 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001609 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001610
1611 /*
1612 * end workaround code, by now the rdev is available
1613 * and locked, and wdev may or may not be NULL.
1614 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001615
1616 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001617 result = cfg80211_dev_rename(
1618 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001619
1620 mutex_unlock(&cfg80211_mutex);
1621
1622 if (result)
1623 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001624
Jouni Malinen31888482008-10-30 16:59:24 +02001625 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1626 struct ieee80211_txq_params txq_params;
1627 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1628
1629 if (!rdev->ops->set_txq_params) {
1630 result = -EOPNOTSUPP;
1631 goto bad_res;
1632 }
1633
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001634 if (!netdev) {
1635 result = -EINVAL;
1636 goto bad_res;
1637 }
1638
Johannes Berg133a3ff2011-11-03 14:50:13 +01001639 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1640 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1641 result = -EINVAL;
1642 goto bad_res;
1643 }
1644
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001645 if (!netif_running(netdev)) {
1646 result = -ENETDOWN;
1647 goto bad_res;
1648 }
1649
Jouni Malinen31888482008-10-30 16:59:24 +02001650 nla_for_each_nested(nl_txq_params,
1651 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1652 rem_txq_params) {
1653 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1654 nla_data(nl_txq_params),
1655 nla_len(nl_txq_params),
1656 txq_params_policy);
1657 result = parse_txq_params(tb, &txq_params);
1658 if (result)
1659 goto bad_res;
1660
Hila Gonene35e4d22012-06-27 17:19:42 +03001661 result = rdev_set_txq_params(rdev, netdev,
1662 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001663 if (result)
1664 goto bad_res;
1665 }
1666 }
1667
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001668 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001669 result = __nl80211_set_channel(rdev,
1670 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1671 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001672 if (result)
1673 goto bad_res;
1674 }
1675
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001676 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001677 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001678 enum nl80211_tx_power_setting type;
1679 int idx, mbm = 0;
1680
Johannes Bergc8442112012-10-24 10:17:18 +02001681 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1682 txp_wdev = NULL;
1683
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001684 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001685 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001686 goto bad_res;
1687 }
1688
1689 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1690 type = nla_get_u32(info->attrs[idx]);
1691
1692 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1693 (type != NL80211_TX_POWER_AUTOMATIC)) {
1694 result = -EINVAL;
1695 goto bad_res;
1696 }
1697
1698 if (type != NL80211_TX_POWER_AUTOMATIC) {
1699 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1700 mbm = nla_get_u32(info->attrs[idx]);
1701 }
1702
Johannes Bergc8442112012-10-24 10:17:18 +02001703 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001704 if (result)
1705 goto bad_res;
1706 }
1707
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001708 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1709 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1710 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001711 if ((!rdev->wiphy.available_antennas_tx &&
1712 !rdev->wiphy.available_antennas_rx) ||
1713 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001714 result = -EOPNOTSUPP;
1715 goto bad_res;
1716 }
1717
1718 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1719 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1720
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001721 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001722 * available antenna masks, except for the "all" mask */
1723 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1724 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001725 result = -EINVAL;
1726 goto bad_res;
1727 }
1728
Bruno Randolf7f531e02010-12-16 11:30:22 +09001729 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1730 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001731
Hila Gonene35e4d22012-06-27 17:19:42 +03001732 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001733 if (result)
1734 goto bad_res;
1735 }
1736
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001737 changed = 0;
1738
1739 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1740 retry_short = nla_get_u8(
1741 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1742 if (retry_short == 0) {
1743 result = -EINVAL;
1744 goto bad_res;
1745 }
1746 changed |= WIPHY_PARAM_RETRY_SHORT;
1747 }
1748
1749 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1750 retry_long = nla_get_u8(
1751 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1752 if (retry_long == 0) {
1753 result = -EINVAL;
1754 goto bad_res;
1755 }
1756 changed |= WIPHY_PARAM_RETRY_LONG;
1757 }
1758
1759 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1760 frag_threshold = nla_get_u32(
1761 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1762 if (frag_threshold < 256) {
1763 result = -EINVAL;
1764 goto bad_res;
1765 }
1766 if (frag_threshold != (u32) -1) {
1767 /*
1768 * Fragments (apart from the last one) are required to
1769 * have even length. Make the fragmentation code
1770 * simpler by stripping LSB should someone try to use
1771 * odd threshold value.
1772 */
1773 frag_threshold &= ~0x1;
1774 }
1775 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1776 }
1777
1778 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1779 rts_threshold = nla_get_u32(
1780 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1781 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1782 }
1783
Lukáš Turek81077e82009-12-21 22:50:47 +01001784 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1785 coverage_class = nla_get_u8(
1786 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1787 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1788 }
1789
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001790 if (changed) {
1791 u8 old_retry_short, old_retry_long;
1792 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001793 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001794
1795 if (!rdev->ops->set_wiphy_params) {
1796 result = -EOPNOTSUPP;
1797 goto bad_res;
1798 }
1799
1800 old_retry_short = rdev->wiphy.retry_short;
1801 old_retry_long = rdev->wiphy.retry_long;
1802 old_frag_threshold = rdev->wiphy.frag_threshold;
1803 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001804 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001805
1806 if (changed & WIPHY_PARAM_RETRY_SHORT)
1807 rdev->wiphy.retry_short = retry_short;
1808 if (changed & WIPHY_PARAM_RETRY_LONG)
1809 rdev->wiphy.retry_long = retry_long;
1810 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1811 rdev->wiphy.frag_threshold = frag_threshold;
1812 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1813 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001814 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1815 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001816
Hila Gonene35e4d22012-06-27 17:19:42 +03001817 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001818 if (result) {
1819 rdev->wiphy.retry_short = old_retry_short;
1820 rdev->wiphy.retry_long = old_retry_long;
1821 rdev->wiphy.frag_threshold = old_frag_threshold;
1822 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001823 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001824 }
1825 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001826
Johannes Berg306d6112008-12-08 12:39:04 +01001827 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001828 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001829 if (netdev)
1830 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001831 return result;
1832}
1833
Johannes Berg71bbc992012-06-15 15:30:18 +02001834static inline u64 wdev_id(struct wireless_dev *wdev)
1835{
1836 return (u64)wdev->identifier |
1837 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1838}
Johannes Berg55682962007-09-20 13:09:35 -04001839
Johannes Berg683b6d32012-11-08 21:25:48 +01001840static int nl80211_send_chandef(struct sk_buff *msg,
1841 struct cfg80211_chan_def *chandef)
1842{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001843 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001844
Johannes Berg683b6d32012-11-08 21:25:48 +01001845 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1846 chandef->chan->center_freq))
1847 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001848 switch (chandef->width) {
1849 case NL80211_CHAN_WIDTH_20_NOHT:
1850 case NL80211_CHAN_WIDTH_20:
1851 case NL80211_CHAN_WIDTH_40:
1852 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1853 cfg80211_get_chandef_type(chandef)))
1854 return -ENOBUFS;
1855 break;
1856 default:
1857 break;
1858 }
1859 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1860 return -ENOBUFS;
1861 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1862 return -ENOBUFS;
1863 if (chandef->center_freq2 &&
1864 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001865 return -ENOBUFS;
1866 return 0;
1867}
1868
Eric W. Biederman15e47302012-09-07 20:12:54 +00001869static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001870 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001871 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001872{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001873 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001874 void *hdr;
1875
Eric W. Biederman15e47302012-09-07 20:12:54 +00001876 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001877 if (!hdr)
1878 return -1;
1879
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001880 if (dev &&
1881 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001882 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001883 goto nla_put_failure;
1884
1885 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1886 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001887 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001888 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001889 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1890 rdev->devlist_generation ^
1891 (cfg80211_rdev_list_generation << 2)))
1892 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001893
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001894 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001895 int ret;
1896 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001897
Johannes Berg683b6d32012-11-08 21:25:48 +01001898 ret = rdev_get_channel(rdev, wdev, &chandef);
1899 if (ret == 0) {
1900 if (nl80211_send_chandef(msg, &chandef))
1901 goto nla_put_failure;
1902 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001903 }
1904
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001905 if (wdev->ssid_len) {
1906 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1907 goto nla_put_failure;
1908 }
1909
Johannes Berg55682962007-09-20 13:09:35 -04001910 return genlmsg_end(msg, hdr);
1911
1912 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001913 genlmsg_cancel(msg, hdr);
1914 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001915}
1916
1917static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1918{
1919 int wp_idx = 0;
1920 int if_idx = 0;
1921 int wp_start = cb->args[0];
1922 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001923 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001924 struct wireless_dev *wdev;
1925
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001926 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001927 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1928 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001929 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001930 if (wp_idx < wp_start) {
1931 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001932 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001933 }
Johannes Berg55682962007-09-20 13:09:35 -04001934 if_idx = 0;
1935
Johannes Bergf5ea9122009-08-07 16:17:38 +02001936 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001937 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001938 if (if_idx < if_start) {
1939 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001940 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001941 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001942 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001943 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001944 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001945 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001946 goto out;
1947 }
1948 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001949 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001950 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001951
1952 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001953 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001954 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001955 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001956
1957 cb->args[0] = wp_idx;
1958 cb->args[1] = if_idx;
1959
1960 return skb->len;
1961}
1962
1963static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1964{
1965 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001966 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001967 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001968
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001969 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001970 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001971 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001972
Eric W. Biederman15e47302012-09-07 20:12:54 +00001973 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001974 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001975 nlmsg_free(msg);
1976 return -ENOBUFS;
1977 }
Johannes Berg55682962007-09-20 13:09:35 -04001978
Johannes Berg134e6372009-07-10 09:51:34 +00001979 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001980}
1981
Michael Wu66f7ac52008-01-31 19:48:22 +01001982static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1983 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1984 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1985 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1986 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1987 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1988};
1989
1990static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1991{
1992 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1993 int flag;
1994
1995 *mntrflags = 0;
1996
1997 if (!nla)
1998 return -EINVAL;
1999
2000 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2001 nla, mntr_flags_policy))
2002 return -EINVAL;
2003
2004 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2005 if (flags[flag])
2006 *mntrflags |= (1<<flag);
2007
2008 return 0;
2009}
2010
Johannes Berg9bc383d2009-11-19 11:55:19 +01002011static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002012 struct net_device *netdev, u8 use_4addr,
2013 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002014{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002015 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002016 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002017 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002018 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002019 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002020
2021 switch (iftype) {
2022 case NL80211_IFTYPE_AP_VLAN:
2023 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2024 return 0;
2025 break;
2026 case NL80211_IFTYPE_STATION:
2027 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2028 return 0;
2029 break;
2030 default:
2031 break;
2032 }
2033
2034 return -EOPNOTSUPP;
2035}
2036
Johannes Berg55682962007-09-20 13:09:35 -04002037static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2038{
Johannes Berg4c476992010-10-04 21:36:35 +02002039 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002040 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002041 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002042 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002043 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002044 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002045 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002046
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002047 memset(&params, 0, sizeof(params));
2048
Johannes Berg04a773a2009-04-19 21:24:32 +02002049 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002050
Johannes Berg723b0382008-09-16 20:22:09 +02002051 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002052 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002053 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002054 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002055 if (ntype > NL80211_IFTYPE_MAX)
2056 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002057 }
2058
Johannes Berg92ffe052008-09-16 20:39:36 +02002059 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002060 struct wireless_dev *wdev = dev->ieee80211_ptr;
2061
Johannes Berg4c476992010-10-04 21:36:35 +02002062 if (ntype != NL80211_IFTYPE_MESH_POINT)
2063 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002064 if (netif_running(dev))
2065 return -EBUSY;
2066
2067 wdev_lock(wdev);
2068 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2069 IEEE80211_MAX_MESH_ID_LEN);
2070 wdev->mesh_id_up_len =
2071 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2072 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2073 wdev->mesh_id_up_len);
2074 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002075 }
2076
Felix Fietkau8b787642009-11-10 18:53:10 +01002077 if (info->attrs[NL80211_ATTR_4ADDR]) {
2078 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2079 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002080 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002081 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002082 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002083 } else {
2084 params.use_4addr = -1;
2085 }
2086
Johannes Berg92ffe052008-09-16 20:39:36 +02002087 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002088 if (ntype != NL80211_IFTYPE_MONITOR)
2089 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002090 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2091 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002092 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002093 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002094
2095 flags = &_flags;
2096 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002097 }
Johannes Berg3b858752009-03-12 09:55:09 +01002098
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002099 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002100 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002101 else
2102 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002103
Johannes Berg9bc383d2009-11-19 11:55:19 +01002104 if (!err && params.use_4addr != -1)
2105 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2106
Johannes Berg55682962007-09-20 13:09:35 -04002107 return err;
2108}
2109
2110static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2111{
Johannes Berg4c476992010-10-04 21:36:35 +02002112 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002113 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002114 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002115 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002116 int err;
2117 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002118 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002119
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002120 memset(&params, 0, sizeof(params));
2121
Johannes Berg55682962007-09-20 13:09:35 -04002122 if (!info->attrs[NL80211_ATTR_IFNAME])
2123 return -EINVAL;
2124
2125 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2126 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2127 if (type > NL80211_IFTYPE_MAX)
2128 return -EINVAL;
2129 }
2130
Johannes Berg79c97e92009-07-07 03:56:12 +02002131 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002132 !(rdev->wiphy.interface_modes & (1 << type)))
2133 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002134
Arend van Spriel1c18f142013-01-08 10:17:27 +01002135 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2136 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2137 ETH_ALEN);
2138 if (!is_valid_ether_addr(params.macaddr))
2139 return -EADDRNOTAVAIL;
2140 }
2141
Johannes Berg9bc383d2009-11-19 11:55:19 +01002142 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002143 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002144 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002145 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002146 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002147 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002148
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002149 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2150 if (!msg)
2151 return -ENOMEM;
2152
Michael Wu66f7ac52008-01-31 19:48:22 +01002153 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2154 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2155 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002156 wdev = rdev_add_virtual_intf(rdev,
2157 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2158 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002159 if (IS_ERR(wdev)) {
2160 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002161 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002162 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002163
Johannes Berg98104fde2012-06-16 00:19:54 +02002164 switch (type) {
2165 case NL80211_IFTYPE_MESH_POINT:
2166 if (!info->attrs[NL80211_ATTR_MESH_ID])
2167 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002168 wdev_lock(wdev);
2169 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2170 IEEE80211_MAX_MESH_ID_LEN);
2171 wdev->mesh_id_up_len =
2172 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2173 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2174 wdev->mesh_id_up_len);
2175 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002176 break;
2177 case NL80211_IFTYPE_P2P_DEVICE:
2178 /*
2179 * P2P Device doesn't have a netdev, so doesn't go
2180 * through the netdev notifier and must be added here
2181 */
2182 mutex_init(&wdev->mtx);
2183 INIT_LIST_HEAD(&wdev->event_list);
2184 spin_lock_init(&wdev->event_lock);
2185 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2186 spin_lock_init(&wdev->mgmt_registrations_lock);
2187
2188 mutex_lock(&rdev->devlist_mtx);
2189 wdev->identifier = ++rdev->wdev_id;
2190 list_add_rcu(&wdev->list, &rdev->wdev_list);
2191 rdev->devlist_generation++;
2192 mutex_unlock(&rdev->devlist_mtx);
2193 break;
2194 default:
2195 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002196 }
2197
Eric W. Biederman15e47302012-09-07 20:12:54 +00002198 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002199 rdev, wdev) < 0) {
2200 nlmsg_free(msg);
2201 return -ENOBUFS;
2202 }
2203
2204 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002205}
2206
2207static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2208{
Johannes Berg4c476992010-10-04 21:36:35 +02002209 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002210 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002211
Johannes Berg4c476992010-10-04 21:36:35 +02002212 if (!rdev->ops->del_virtual_intf)
2213 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002214
Johannes Berg84efbb82012-06-16 00:00:26 +02002215 /*
2216 * If we remove a wireless device without a netdev then clear
2217 * user_ptr[1] so that nl80211_post_doit won't dereference it
2218 * to check if it needs to do dev_put(). Otherwise it crashes
2219 * since the wdev has been freed, unlike with a netdev where
2220 * we need the dev_put() for the netdev to really be freed.
2221 */
2222 if (!wdev->netdev)
2223 info->user_ptr[1] = NULL;
2224
Hila Gonene35e4d22012-06-27 17:19:42 +03002225 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002226}
2227
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002228static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2229{
2230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2231 struct net_device *dev = info->user_ptr[1];
2232 u16 noack_map;
2233
2234 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2235 return -EINVAL;
2236
2237 if (!rdev->ops->set_noack_map)
2238 return -EOPNOTSUPP;
2239
2240 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2241
Hila Gonene35e4d22012-06-27 17:19:42 +03002242 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002243}
2244
Johannes Berg41ade002007-12-19 02:03:29 +01002245struct get_key_cookie {
2246 struct sk_buff *msg;
2247 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002248 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002249};
2250
2251static void get_key_callback(void *c, struct key_params *params)
2252{
Johannes Bergb9454e82009-07-08 13:29:08 +02002253 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002254 struct get_key_cookie *cookie = c;
2255
David S. Miller9360ffd2012-03-29 04:41:26 -04002256 if ((params->key &&
2257 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2258 params->key_len, params->key)) ||
2259 (params->seq &&
2260 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2261 params->seq_len, params->seq)) ||
2262 (params->cipher &&
2263 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2264 params->cipher)))
2265 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002266
Johannes Bergb9454e82009-07-08 13:29:08 +02002267 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2268 if (!key)
2269 goto nla_put_failure;
2270
David S. Miller9360ffd2012-03-29 04:41:26 -04002271 if ((params->key &&
2272 nla_put(cookie->msg, NL80211_KEY_DATA,
2273 params->key_len, params->key)) ||
2274 (params->seq &&
2275 nla_put(cookie->msg, NL80211_KEY_SEQ,
2276 params->seq_len, params->seq)) ||
2277 (params->cipher &&
2278 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2279 params->cipher)))
2280 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002281
David S. Miller9360ffd2012-03-29 04:41:26 -04002282 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2283 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002284
2285 nla_nest_end(cookie->msg, key);
2286
Johannes Berg41ade002007-12-19 02:03:29 +01002287 return;
2288 nla_put_failure:
2289 cookie->error = 1;
2290}
2291
2292static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2293{
Johannes Berg4c476992010-10-04 21:36:35 +02002294 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002295 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002296 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002297 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002298 const u8 *mac_addr = NULL;
2299 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002300 struct get_key_cookie cookie = {
2301 .error = 0,
2302 };
2303 void *hdr;
2304 struct sk_buff *msg;
2305
2306 if (info->attrs[NL80211_ATTR_KEY_IDX])
2307 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2308
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002309 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002310 return -EINVAL;
2311
2312 if (info->attrs[NL80211_ATTR_MAC])
2313 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2314
Johannes Berge31b8212010-10-05 19:39:30 +02002315 pairwise = !!mac_addr;
2316 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2317 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2318 if (kt >= NUM_NL80211_KEYTYPES)
2319 return -EINVAL;
2320 if (kt != NL80211_KEYTYPE_GROUP &&
2321 kt != NL80211_KEYTYPE_PAIRWISE)
2322 return -EINVAL;
2323 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2324 }
2325
Johannes Berg4c476992010-10-04 21:36:35 +02002326 if (!rdev->ops->get_key)
2327 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002328
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002329 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002330 if (!msg)
2331 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002332
Eric W. Biederman15e47302012-09-07 20:12:54 +00002333 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002334 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002335 if (IS_ERR(hdr))
2336 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002337
2338 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002339 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002340
David S. Miller9360ffd2012-03-29 04:41:26 -04002341 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2342 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2343 goto nla_put_failure;
2344 if (mac_addr &&
2345 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2346 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002347
Johannes Berge31b8212010-10-05 19:39:30 +02002348 if (pairwise && mac_addr &&
2349 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2350 return -ENOENT;
2351
Hila Gonene35e4d22012-06-27 17:19:42 +03002352 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2353 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002354
2355 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002356 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002357
2358 if (cookie.error)
2359 goto nla_put_failure;
2360
2361 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002362 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002363
2364 nla_put_failure:
2365 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002366 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002367 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002368 return err;
2369}
2370
2371static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2372{
Johannes Berg4c476992010-10-04 21:36:35 +02002373 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002374 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002375 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002376 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002377
Johannes Bergb9454e82009-07-08 13:29:08 +02002378 err = nl80211_parse_key(info, &key);
2379 if (err)
2380 return err;
2381
2382 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002383 return -EINVAL;
2384
Johannes Bergb9454e82009-07-08 13:29:08 +02002385 /* only support setting default key */
2386 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002387 return -EINVAL;
2388
Johannes Bergfffd0932009-07-08 14:22:54 +02002389 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002390
2391 if (key.def) {
2392 if (!rdev->ops->set_default_key) {
2393 err = -EOPNOTSUPP;
2394 goto out;
2395 }
2396
2397 err = nl80211_key_allowed(dev->ieee80211_ptr);
2398 if (err)
2399 goto out;
2400
Hila Gonene35e4d22012-06-27 17:19:42 +03002401 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002402 key.def_uni, key.def_multi);
2403
2404 if (err)
2405 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002406
Johannes Berg3d23e342009-09-29 23:27:28 +02002407#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002408 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002409#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002410 } else {
2411 if (key.def_uni || !key.def_multi) {
2412 err = -EINVAL;
2413 goto out;
2414 }
2415
2416 if (!rdev->ops->set_default_mgmt_key) {
2417 err = -EOPNOTSUPP;
2418 goto out;
2419 }
2420
2421 err = nl80211_key_allowed(dev->ieee80211_ptr);
2422 if (err)
2423 goto out;
2424
Hila Gonene35e4d22012-06-27 17:19:42 +03002425 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002426 if (err)
2427 goto out;
2428
2429#ifdef CONFIG_CFG80211_WEXT
2430 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2431#endif
2432 }
2433
2434 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002435 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002436
Johannes Berg41ade002007-12-19 02:03:29 +01002437 return err;
2438}
2439
2440static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2441{
Johannes Berg4c476992010-10-04 21:36:35 +02002442 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002443 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002444 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002445 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002446 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002447
Johannes Bergb9454e82009-07-08 13:29:08 +02002448 err = nl80211_parse_key(info, &key);
2449 if (err)
2450 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002451
Johannes Bergb9454e82009-07-08 13:29:08 +02002452 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002453 return -EINVAL;
2454
Johannes Berg41ade002007-12-19 02:03:29 +01002455 if (info->attrs[NL80211_ATTR_MAC])
2456 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2457
Johannes Berge31b8212010-10-05 19:39:30 +02002458 if (key.type == -1) {
2459 if (mac_addr)
2460 key.type = NL80211_KEYTYPE_PAIRWISE;
2461 else
2462 key.type = NL80211_KEYTYPE_GROUP;
2463 }
2464
2465 /* for now */
2466 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2467 key.type != NL80211_KEYTYPE_GROUP)
2468 return -EINVAL;
2469
Johannes Berg4c476992010-10-04 21:36:35 +02002470 if (!rdev->ops->add_key)
2471 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002472
Johannes Berge31b8212010-10-05 19:39:30 +02002473 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2474 key.type == NL80211_KEYTYPE_PAIRWISE,
2475 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002476 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002477
2478 wdev_lock(dev->ieee80211_ptr);
2479 err = nl80211_key_allowed(dev->ieee80211_ptr);
2480 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002481 err = rdev_add_key(rdev, dev, key.idx,
2482 key.type == NL80211_KEYTYPE_PAIRWISE,
2483 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002484 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002485
Johannes Berg41ade002007-12-19 02:03:29 +01002486 return err;
2487}
2488
2489static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2490{
Johannes Berg4c476992010-10-04 21:36:35 +02002491 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002492 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002493 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002494 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002495 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002496
Johannes Bergb9454e82009-07-08 13:29:08 +02002497 err = nl80211_parse_key(info, &key);
2498 if (err)
2499 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002500
2501 if (info->attrs[NL80211_ATTR_MAC])
2502 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2503
Johannes Berge31b8212010-10-05 19:39:30 +02002504 if (key.type == -1) {
2505 if (mac_addr)
2506 key.type = NL80211_KEYTYPE_PAIRWISE;
2507 else
2508 key.type = NL80211_KEYTYPE_GROUP;
2509 }
2510
2511 /* for now */
2512 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2513 key.type != NL80211_KEYTYPE_GROUP)
2514 return -EINVAL;
2515
Johannes Berg4c476992010-10-04 21:36:35 +02002516 if (!rdev->ops->del_key)
2517 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002518
Johannes Bergfffd0932009-07-08 14:22:54 +02002519 wdev_lock(dev->ieee80211_ptr);
2520 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002521
2522 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2523 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2524 err = -ENOENT;
2525
Johannes Bergfffd0932009-07-08 14:22:54 +02002526 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002527 err = rdev_del_key(rdev, dev, key.idx,
2528 key.type == NL80211_KEYTYPE_PAIRWISE,
2529 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002530
Johannes Berg3d23e342009-09-29 23:27:28 +02002531#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002532 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002533 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002534 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002535 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002536 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2537 }
2538#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002539 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002540
Johannes Berg41ade002007-12-19 02:03:29 +01002541 return err;
2542}
2543
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302544/* This function returns an error or the number of nested attributes */
2545static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2546{
2547 struct nlattr *attr;
2548 int n_entries = 0, tmp;
2549
2550 nla_for_each_nested(attr, nl_attr, tmp) {
2551 if (nla_len(attr) != ETH_ALEN)
2552 return -EINVAL;
2553
2554 n_entries++;
2555 }
2556
2557 return n_entries;
2558}
2559
2560/*
2561 * This function parses ACL information and allocates memory for ACL data.
2562 * On successful return, the calling function is responsible to free the
2563 * ACL buffer returned by this function.
2564 */
2565static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2566 struct genl_info *info)
2567{
2568 enum nl80211_acl_policy acl_policy;
2569 struct nlattr *attr;
2570 struct cfg80211_acl_data *acl;
2571 int i = 0, n_entries, tmp;
2572
2573 if (!wiphy->max_acl_mac_addrs)
2574 return ERR_PTR(-EOPNOTSUPP);
2575
2576 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2577 return ERR_PTR(-EINVAL);
2578
2579 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2580 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2581 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2582 return ERR_PTR(-EINVAL);
2583
2584 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2585 return ERR_PTR(-EINVAL);
2586
2587 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2588 if (n_entries < 0)
2589 return ERR_PTR(n_entries);
2590
2591 if (n_entries > wiphy->max_acl_mac_addrs)
2592 return ERR_PTR(-ENOTSUPP);
2593
2594 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2595 GFP_KERNEL);
2596 if (!acl)
2597 return ERR_PTR(-ENOMEM);
2598
2599 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2600 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2601 i++;
2602 }
2603
2604 acl->n_acl_entries = n_entries;
2605 acl->acl_policy = acl_policy;
2606
2607 return acl;
2608}
2609
2610static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2611{
2612 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2613 struct net_device *dev = info->user_ptr[1];
2614 struct cfg80211_acl_data *acl;
2615 int err;
2616
2617 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2618 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2619 return -EOPNOTSUPP;
2620
2621 if (!dev->ieee80211_ptr->beacon_interval)
2622 return -EINVAL;
2623
2624 acl = parse_acl_data(&rdev->wiphy, info);
2625 if (IS_ERR(acl))
2626 return PTR_ERR(acl);
2627
2628 err = rdev_set_mac_acl(rdev, dev, acl);
2629
2630 kfree(acl);
2631
2632 return err;
2633}
2634
Johannes Berg88600202012-02-13 15:17:18 +01002635static int nl80211_parse_beacon(struct genl_info *info,
2636 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002637{
Johannes Berg88600202012-02-13 15:17:18 +01002638 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002639
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002640 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2641 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2642 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2643 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002644 return -EINVAL;
2645
Johannes Berg88600202012-02-13 15:17:18 +01002646 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002647
Johannes Berged1b6cc2007-12-19 02:03:32 +01002648 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002649 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2650 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2651 if (!bcn->head_len)
2652 return -EINVAL;
2653 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002654 }
2655
2656 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002657 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2658 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002659 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002660 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002661 }
2662
Johannes Berg4c476992010-10-04 21:36:35 +02002663 if (!haveinfo)
2664 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002665
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002666 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002667 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2668 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002669 }
2670
2671 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002672 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002673 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002674 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002675 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2676 }
2677
2678 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002679 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002680 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002681 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002682 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2683 }
2684
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002685 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002686 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002687 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002688 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002689 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2690 }
2691
Johannes Berg88600202012-02-13 15:17:18 +01002692 return 0;
2693}
2694
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002695static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2696 struct cfg80211_ap_settings *params)
2697{
2698 struct wireless_dev *wdev;
2699 bool ret = false;
2700
2701 mutex_lock(&rdev->devlist_mtx);
2702
Johannes Berg89a54e42012-06-15 14:33:17 +02002703 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002704 if (wdev->iftype != NL80211_IFTYPE_AP &&
2705 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2706 continue;
2707
Johannes Berg683b6d32012-11-08 21:25:48 +01002708 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002709 continue;
2710
Johannes Berg683b6d32012-11-08 21:25:48 +01002711 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002712 ret = true;
2713 break;
2714 }
2715
2716 mutex_unlock(&rdev->devlist_mtx);
2717
2718 return ret;
2719}
2720
Jouni Malinene39e5b52012-09-30 19:29:39 +03002721static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2722 enum nl80211_auth_type auth_type,
2723 enum nl80211_commands cmd)
2724{
2725 if (auth_type > NL80211_AUTHTYPE_MAX)
2726 return false;
2727
2728 switch (cmd) {
2729 case NL80211_CMD_AUTHENTICATE:
2730 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2731 auth_type == NL80211_AUTHTYPE_SAE)
2732 return false;
2733 return true;
2734 case NL80211_CMD_CONNECT:
2735 case NL80211_CMD_START_AP:
2736 /* SAE not supported yet */
2737 if (auth_type == NL80211_AUTHTYPE_SAE)
2738 return false;
2739 return true;
2740 default:
2741 return false;
2742 }
2743}
2744
Johannes Berg88600202012-02-13 15:17:18 +01002745static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2746{
2747 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2748 struct net_device *dev = info->user_ptr[1];
2749 struct wireless_dev *wdev = dev->ieee80211_ptr;
2750 struct cfg80211_ap_settings params;
2751 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01002752 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01002753
2754 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2755 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2756 return -EOPNOTSUPP;
2757
2758 if (!rdev->ops->start_ap)
2759 return -EOPNOTSUPP;
2760
2761 if (wdev->beacon_interval)
2762 return -EALREADY;
2763
2764 memset(&params, 0, sizeof(params));
2765
2766 /* these are required for START_AP */
2767 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2768 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2769 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2770 return -EINVAL;
2771
2772 err = nl80211_parse_beacon(info, &params.beacon);
2773 if (err)
2774 return err;
2775
2776 params.beacon_interval =
2777 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2778 params.dtim_period =
2779 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2780
2781 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2782 if (err)
2783 return err;
2784
2785 /*
2786 * In theory, some of these attributes should be required here
2787 * but since they were not used when the command was originally
2788 * added, keep them optional for old user space programs to let
2789 * them continue to work with drivers that do not need the
2790 * additional information -- drivers must check!
2791 */
2792 if (info->attrs[NL80211_ATTR_SSID]) {
2793 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2794 params.ssid_len =
2795 nla_len(info->attrs[NL80211_ATTR_SSID]);
2796 if (params.ssid_len == 0 ||
2797 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2798 return -EINVAL;
2799 }
2800
2801 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2802 params.hidden_ssid = nla_get_u32(
2803 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2804 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2805 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2806 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2807 return -EINVAL;
2808 }
2809
2810 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2811
2812 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2813 params.auth_type = nla_get_u32(
2814 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002815 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2816 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002817 return -EINVAL;
2818 } else
2819 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2820
2821 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2822 NL80211_MAX_NR_CIPHER_SUITES);
2823 if (err)
2824 return err;
2825
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302826 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2827 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2828 return -EOPNOTSUPP;
2829 params.inactivity_timeout = nla_get_u16(
2830 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2831 }
2832
Johannes Berg53cabad2012-11-14 15:17:28 +01002833 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2834 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2835 return -EINVAL;
2836 params.p2p_ctwindow =
2837 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2838 if (params.p2p_ctwindow > 127)
2839 return -EINVAL;
2840 if (params.p2p_ctwindow != 0 &&
2841 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2842 return -EINVAL;
2843 }
2844
2845 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2846 u8 tmp;
2847
2848 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2849 return -EINVAL;
2850 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2851 if (tmp > 1)
2852 return -EINVAL;
2853 params.p2p_opp_ps = tmp;
2854 if (params.p2p_opp_ps != 0 &&
2855 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2856 return -EINVAL;
2857 }
2858
Johannes Bergaa430da2012-05-16 23:50:18 +02002859 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002860 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2861 if (err)
2862 return err;
2863 } else if (wdev->preset_chandef.chan) {
2864 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002865 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002866 return -EINVAL;
2867
Johannes Berg683b6d32012-11-08 21:25:48 +01002868 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002869 return -EINVAL;
2870
Simon Wunderlich04f39042013-02-08 18:16:19 +01002871 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
2872 if (err < 0)
2873 return err;
2874 if (err) {
2875 radar_detect_width = BIT(params.chandef.width);
2876 params.radar_required = true;
2877 }
2878
Michal Kaziore4e32452012-06-29 12:47:08 +02002879 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01002880 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
2881 params.chandef.chan,
2882 CHAN_MODE_SHARED,
2883 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02002884 mutex_unlock(&rdev->devlist_mtx);
2885
2886 if (err)
2887 return err;
2888
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302889 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
2890 params.acl = parse_acl_data(&rdev->wiphy, info);
2891 if (IS_ERR(params.acl))
2892 return PTR_ERR(params.acl);
2893 }
2894
Hila Gonene35e4d22012-06-27 17:19:42 +03002895 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002896 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002897 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002898 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002899 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002900 wdev->ssid_len = params.ssid_len;
2901 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002902 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302903
2904 kfree(params.acl);
2905
Johannes Berg56d18932011-05-09 18:41:15 +02002906 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002907}
2908
Johannes Berg88600202012-02-13 15:17:18 +01002909static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2910{
2911 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2912 struct net_device *dev = info->user_ptr[1];
2913 struct wireless_dev *wdev = dev->ieee80211_ptr;
2914 struct cfg80211_beacon_data params;
2915 int err;
2916
2917 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2918 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2919 return -EOPNOTSUPP;
2920
2921 if (!rdev->ops->change_beacon)
2922 return -EOPNOTSUPP;
2923
2924 if (!wdev->beacon_interval)
2925 return -EINVAL;
2926
2927 err = nl80211_parse_beacon(info, &params);
2928 if (err)
2929 return err;
2930
Hila Gonene35e4d22012-06-27 17:19:42 +03002931 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002932}
2933
2934static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002935{
Johannes Berg4c476992010-10-04 21:36:35 +02002936 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2937 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002938
Michal Kazior60771782012-06-29 12:46:56 +02002939 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002940}
2941
Johannes Berg5727ef12007-12-19 02:03:34 +01002942static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2943 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2944 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2945 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002946 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002947 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002948 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002949};
2950
Johannes Bergeccb8e82009-05-11 21:57:56 +03002951static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002952 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002953 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002954{
2955 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002956 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002957 int flag;
2958
Johannes Bergeccb8e82009-05-11 21:57:56 +03002959 /*
2960 * Try parsing the new attribute first so userspace
2961 * can specify both for older kernels.
2962 */
2963 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2964 if (nla) {
2965 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002966
Johannes Bergeccb8e82009-05-11 21:57:56 +03002967 sta_flags = nla_data(nla);
2968 params->sta_flags_mask = sta_flags->mask;
2969 params->sta_flags_set = sta_flags->set;
2970 if ((params->sta_flags_mask |
2971 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2972 return -EINVAL;
2973 return 0;
2974 }
2975
2976 /* if present, parse the old attribute */
2977
2978 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002979 if (!nla)
2980 return 0;
2981
2982 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2983 nla, sta_flags_policy))
2984 return -EINVAL;
2985
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002986 /*
2987 * Only allow certain flags for interface types so that
2988 * other attributes are silently ignored. Remember that
2989 * this is backward compatibility code with old userspace
2990 * and shouldn't be hit in other cases anyway.
2991 */
2992 switch (iftype) {
2993 case NL80211_IFTYPE_AP:
2994 case NL80211_IFTYPE_AP_VLAN:
2995 case NL80211_IFTYPE_P2P_GO:
2996 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2997 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2998 BIT(NL80211_STA_FLAG_WME) |
2999 BIT(NL80211_STA_FLAG_MFP);
3000 break;
3001 case NL80211_IFTYPE_P2P_CLIENT:
3002 case NL80211_IFTYPE_STATION:
3003 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3004 BIT(NL80211_STA_FLAG_TDLS_PEER);
3005 break;
3006 case NL80211_IFTYPE_MESH_POINT:
3007 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3008 BIT(NL80211_STA_FLAG_MFP) |
3009 BIT(NL80211_STA_FLAG_AUTHORIZED);
3010 default:
3011 return -EINVAL;
3012 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003013
Johannes Berg3383b5a2012-05-10 20:14:43 +02003014 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3015 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003016 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003017
Johannes Berg3383b5a2012-05-10 20:14:43 +02003018 /* no longer support new API additions in old API */
3019 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3020 return -EINVAL;
3021 }
3022 }
3023
Johannes Berg5727ef12007-12-19 02:03:34 +01003024 return 0;
3025}
3026
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003027static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3028 int attr)
3029{
3030 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003031 u32 bitrate;
3032 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003033
3034 rate = nla_nest_start(msg, attr);
3035 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003036 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003037
3038 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3039 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003040 /* report 16-bit bitrate only if we can */
3041 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003042 if (bitrate > 0 &&
3043 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3044 return false;
3045 if (bitrate_compat > 0 &&
3046 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3047 return false;
3048
3049 if (info->flags & RATE_INFO_FLAGS_MCS) {
3050 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3051 return false;
3052 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3053 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3054 return false;
3055 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3056 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3057 return false;
3058 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3059 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3060 return false;
3061 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3062 return false;
3063 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3064 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3065 return false;
3066 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3067 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3068 return false;
3069 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3070 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3071 return false;
3072 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3073 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3074 return false;
3075 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3076 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3077 return false;
3078 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003079
3080 nla_nest_end(msg, rate);
3081 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003082}
3083
Eric W. Biederman15e47302012-09-07 20:12:54 +00003084static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003085 int flags,
3086 struct cfg80211_registered_device *rdev,
3087 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003088 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003089{
3090 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003091 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003092
Eric W. Biederman15e47302012-09-07 20:12:54 +00003093 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003094 if (!hdr)
3095 return -1;
3096
David S. Miller9360ffd2012-03-29 04:41:26 -04003097 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3098 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3099 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3100 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003101
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003102 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3103 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003104 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003105 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3106 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3107 sinfo->connected_time))
3108 goto nla_put_failure;
3109 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3110 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3111 sinfo->inactive_time))
3112 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003113 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3114 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003115 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003116 (u32)sinfo->rx_bytes))
3117 goto nla_put_failure;
3118 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3119 NL80211_STA_INFO_TX_BYTES64)) &&
3120 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3121 (u32)sinfo->tx_bytes))
3122 goto nla_put_failure;
3123 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3124 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003125 sinfo->rx_bytes))
3126 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003127 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3128 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003129 sinfo->tx_bytes))
3130 goto nla_put_failure;
3131 if ((sinfo->filled & STATION_INFO_LLID) &&
3132 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3133 goto nla_put_failure;
3134 if ((sinfo->filled & STATION_INFO_PLID) &&
3135 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3136 goto nla_put_failure;
3137 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3138 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3139 sinfo->plink_state))
3140 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003141 switch (rdev->wiphy.signal_type) {
3142 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003143 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3144 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3145 sinfo->signal))
3146 goto nla_put_failure;
3147 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3148 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3149 sinfo->signal_avg))
3150 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003151 break;
3152 default:
3153 break;
3154 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003155 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003156 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3157 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003158 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003159 }
3160 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3161 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3162 NL80211_STA_INFO_RX_BITRATE))
3163 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003164 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003165 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3166 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3167 sinfo->rx_packets))
3168 goto nla_put_failure;
3169 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3170 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3171 sinfo->tx_packets))
3172 goto nla_put_failure;
3173 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3174 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3175 sinfo->tx_retries))
3176 goto nla_put_failure;
3177 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3178 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3179 sinfo->tx_failed))
3180 goto nla_put_failure;
3181 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3182 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3183 sinfo->beacon_loss_count))
3184 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003185 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3186 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3187 sinfo->local_pm))
3188 goto nla_put_failure;
3189 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3190 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3191 sinfo->peer_pm))
3192 goto nla_put_failure;
3193 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3194 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3195 sinfo->nonpeer_pm))
3196 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003197 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3198 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3199 if (!bss_param)
3200 goto nla_put_failure;
3201
David S. Miller9360ffd2012-03-29 04:41:26 -04003202 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3203 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3204 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3205 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3206 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3207 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3208 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3209 sinfo->bss_param.dtim_period) ||
3210 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3211 sinfo->bss_param.beacon_interval))
3212 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003213
3214 nla_nest_end(msg, bss_param);
3215 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003216 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3217 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3218 sizeof(struct nl80211_sta_flag_update),
3219 &sinfo->sta_flags))
3220 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003221 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3222 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3223 sinfo->t_offset))
3224 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003225 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003226
David S. Miller9360ffd2012-03-29 04:41:26 -04003227 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3228 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3229 sinfo->assoc_req_ies))
3230 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003231
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003232 return genlmsg_end(msg, hdr);
3233
3234 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003235 genlmsg_cancel(msg, hdr);
3236 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003237}
3238
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003239static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003240 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003241{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003242 struct station_info sinfo;
3243 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003244 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003245 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003246 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003247 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003248
Johannes Berg67748892010-10-04 21:14:06 +02003249 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3250 if (err)
3251 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003252
3253 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003254 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003255 goto out_err;
3256 }
3257
Johannes Bergbba95fe2008-07-29 13:22:51 +02003258 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003259 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003260 err = rdev_dump_station(dev, netdev, sta_idx,
3261 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003262 if (err == -ENOENT)
3263 break;
3264 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003265 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003266
3267 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003268 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003269 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003270 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003271 &sinfo) < 0)
3272 goto out;
3273
3274 sta_idx++;
3275 }
3276
3277
3278 out:
3279 cb->args[1] = sta_idx;
3280 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003281 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003282 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003283
3284 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003285}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003286
Johannes Berg5727ef12007-12-19 02:03:34 +01003287static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3288{
Johannes Berg4c476992010-10-04 21:36:35 +02003289 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3290 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003291 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003292 struct sk_buff *msg;
3293 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003294 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003295
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003296 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003297
3298 if (!info->attrs[NL80211_ATTR_MAC])
3299 return -EINVAL;
3300
3301 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3302
Johannes Berg4c476992010-10-04 21:36:35 +02003303 if (!rdev->ops->get_station)
3304 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003305
Hila Gonene35e4d22012-06-27 17:19:42 +03003306 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003307 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003308 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003309
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003310 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003311 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003312 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003313
Eric W. Biederman15e47302012-09-07 20:12:54 +00003314 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003315 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003316 nlmsg_free(msg);
3317 return -ENOBUFS;
3318 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003319
Johannes Berg4c476992010-10-04 21:36:35 +02003320 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003321}
3322
3323/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003324 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003325 */
Johannes Berg80b99892011-11-18 16:23:01 +01003326static struct net_device *get_vlan(struct genl_info *info,
3327 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003328{
Johannes Berg463d0182009-07-14 00:33:35 +02003329 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003330 struct net_device *v;
3331 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003332
Johannes Berg80b99892011-11-18 16:23:01 +01003333 if (!vlanattr)
3334 return NULL;
3335
3336 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3337 if (!v)
3338 return ERR_PTR(-ENODEV);
3339
3340 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3341 ret = -EINVAL;
3342 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003343 }
Johannes Berg80b99892011-11-18 16:23:01 +01003344
3345 if (!netif_running(v)) {
3346 ret = -ENETDOWN;
3347 goto error;
3348 }
3349
3350 return v;
3351 error:
3352 dev_put(v);
3353 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003354}
3355
Jouni Malinendf881292013-02-14 21:10:54 +02003356static struct nla_policy
3357nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3358 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3359 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3360};
3361
3362static int nl80211_set_station_tdls(struct genl_info *info,
3363 struct station_parameters *params)
3364{
Jouni Malinendf881292013-02-14 21:10:54 +02003365 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3366 struct nlattr *nla;
3367 int err;
3368
Jouni Malinendf881292013-02-14 21:10:54 +02003369 /* Dummy STA entry gets updated once the peer capabilities are known */
3370 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3371 params->ht_capa =
3372 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3373 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3374 params->vht_capa =
3375 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3376
3377 /* parse WME attributes if present */
3378 if (!info->attrs[NL80211_ATTR_STA_WME])
3379 return 0;
3380
3381 nla = info->attrs[NL80211_ATTR_STA_WME];
3382 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3383 nl80211_sta_wme_policy);
3384 if (err)
3385 return err;
3386
3387 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3388 params->uapsd_queues = nla_get_u8(
3389 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3390 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3391 return -EINVAL;
3392
3393 if (tb[NL80211_STA_WME_MAX_SP])
3394 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3395
3396 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3397 return -EINVAL;
3398
3399 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3400
3401 return 0;
3402}
3403
Johannes Berg5727ef12007-12-19 02:03:34 +01003404static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3405{
Johannes Berg4c476992010-10-04 21:36:35 +02003406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003407 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003408 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003409 struct station_parameters params;
3410 u8 *mac_addr = NULL;
3411
3412 memset(&params, 0, sizeof(params));
3413
3414 params.listen_interval = -1;
3415
3416 if (info->attrs[NL80211_ATTR_STA_AID])
3417 return -EINVAL;
3418
3419 if (!info->attrs[NL80211_ATTR_MAC])
3420 return -EINVAL;
3421
3422 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3423
3424 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3425 params.supported_rates =
3426 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3427 params.supported_rates_len =
3428 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3429 }
3430
Jouni Malinen9d62a982013-02-14 21:10:13 +02003431 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3432 params.capability =
3433 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3434 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3435 }
3436
3437 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3438 params.ext_capab =
3439 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3440 params.ext_capab_len =
3441 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3442 }
3443
Jouni Malinendf881292013-02-14 21:10:54 +02003444 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003445 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03003446
Johannes Bergbdd90d52011-12-14 12:20:27 +01003447 if (!rdev->ops->change_station)
3448 return -EOPNOTSUPP;
3449
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003450 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003451 return -EINVAL;
3452
Johannes Bergf8bacc22013-02-14 23:27:01 +01003453 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003454 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003455 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3456 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3457 return -EINVAL;
3458 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003459
Johannes Bergf8bacc22013-02-14 23:27:01 +01003460 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003461 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003462 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3463 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3464 return -EINVAL;
3465 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3466 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003467
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003468 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3469 enum nl80211_mesh_power_mode pm = nla_get_u32(
3470 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3471
3472 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3473 pm > NL80211_MESH_POWER_MAX)
3474 return -EINVAL;
3475
3476 params.local_pm = pm;
3477 }
3478
Johannes Berga97f4422009-06-18 17:23:43 +02003479 switch (dev->ieee80211_ptr->iftype) {
3480 case NL80211_IFTYPE_AP:
3481 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003482 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003483 /* disallow mesh-specific things */
3484 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003485 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003486 if (params.local_pm)
3487 return -EINVAL;
Johannes Bergf8bacc22013-02-14 23:27:01 +01003488 if (params.sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3489 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003490
3491 /* TDLS can't be set, ... */
3492 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3493 return -EINVAL;
3494 /*
3495 * ... but don't bother the driver with it. This works around
3496 * a hostapd/wpa_supplicant issue -- it always includes the
3497 * TLDS_PEER flag in the mask even for AP mode.
3498 */
3499 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3500
3501 /* accept only the listed bits */
3502 if (params.sta_flags_mask &
3503 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003504 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3505 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003506 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3507 BIT(NL80211_STA_FLAG_WME) |
3508 BIT(NL80211_STA_FLAG_MFP)))
3509 return -EINVAL;
3510
Johannes Bergd582cff2012-10-26 17:53:44 +02003511 /* but authenticated/associated only if driver handles it */
3512 if (!(rdev->wiphy.features &
3513 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3514 params.sta_flags_mask &
3515 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3516 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3517 return -EINVAL;
3518
Johannes Bergba23d202012-12-27 17:32:09 +01003519 /* reject other things that can't change */
3520 if (params.supported_rates)
3521 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003522 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3523 return -EINVAL;
3524 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3525 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003526 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3527 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3528 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003529
Johannes Bergbdd90d52011-12-14 12:20:27 +01003530 /* must be last in here for error handling */
3531 params.vlan = get_vlan(info, rdev);
3532 if (IS_ERR(params.vlan))
3533 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003534 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003535 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003536 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003537 /*
3538 * Don't allow userspace to change the TDLS_PEER flag,
3539 * but silently ignore attempts to change it since we
3540 * don't have state here to verify that it doesn't try
3541 * to change the flag.
3542 */
3543 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Jouni Malinendf881292013-02-14 21:10:54 +02003544 /* Include parameters for TDLS peer (driver will check) */
3545 err = nl80211_set_station_tdls(info, &params);
3546 if (err)
3547 return err;
3548 /* disallow things sta doesn't support */
3549 if (params.plink_action)
3550 return -EINVAL;
3551 if (params.local_pm)
3552 return -EINVAL;
Johannes Bergf8bacc22013-02-14 23:27:01 +01003553 if (params.sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3554 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003555 /* reject any changes other than AUTHORIZED or WME (for TDLS) */
3556 if (params.sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3557 BIT(NL80211_STA_FLAG_WME)))
3558 return -EINVAL;
3559 break;
Antonio Quartulli267335d2012-01-31 20:25:47 +01003560 case NL80211_IFTYPE_ADHOC:
3561 /* disallow things sta doesn't support */
3562 if (params.plink_action)
3563 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003564 if (params.local_pm)
3565 return -EINVAL;
Johannes Bergf8bacc22013-02-14 23:27:01 +01003566 if (params.sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3567 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003568 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3569 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3570 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003571 /* reject any changes other than AUTHORIZED */
3572 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3573 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003574 break;
3575 case NL80211_IFTYPE_MESH_POINT:
3576 /* disallow things mesh doesn't support */
3577 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003578 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003579 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003580 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003581 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3582 return -EINVAL;
3583 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3584 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003585 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3586 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3587 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003588 /*
3589 * No special handling for TDLS here -- the userspace
3590 * mesh code doesn't have this bug.
3591 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003592 if (params.sta_flags_mask &
3593 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003594 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003595 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003596 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003597 break;
3598 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003599 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003600 }
3601
Johannes Bergbdd90d52011-12-14 12:20:27 +01003602 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003603
Hila Gonene35e4d22012-06-27 17:19:42 +03003604 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003605
Johannes Berg5727ef12007-12-19 02:03:34 +01003606 if (params.vlan)
3607 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003608
Johannes Berg5727ef12007-12-19 02:03:34 +01003609 return err;
3610}
3611
3612static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3613{
Johannes Berg4c476992010-10-04 21:36:35 +02003614 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003615 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003616 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003617 struct station_parameters params;
3618 u8 *mac_addr = NULL;
3619
3620 memset(&params, 0, sizeof(params));
3621
Johannes Berg984c3112013-02-14 23:43:25 +01003622 if (!rdev->ops->add_station)
3623 return -EOPNOTSUPP;
3624
Johannes Berg5727ef12007-12-19 02:03:34 +01003625 if (!info->attrs[NL80211_ATTR_MAC])
3626 return -EINVAL;
3627
Johannes Berg5727ef12007-12-19 02:03:34 +01003628 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3629 return -EINVAL;
3630
3631 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3632 return -EINVAL;
3633
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003634 if (!info->attrs[NL80211_ATTR_STA_AID])
3635 return -EINVAL;
3636
Johannes Berg5727ef12007-12-19 02:03:34 +01003637 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3638 params.supported_rates =
3639 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3640 params.supported_rates_len =
3641 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3642 params.listen_interval =
3643 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003644
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003645 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3646 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3647 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003648
Jouni Malinen9d62a982013-02-14 21:10:13 +02003649 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3650 params.capability =
3651 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3652 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3653 }
3654
3655 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3656 params.ext_capab =
3657 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3658 params.ext_capab_len =
3659 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3660 }
3661
Jouni Malinen36aedc902008-08-25 11:58:58 +03003662 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3663 params.ht_capa =
3664 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003665
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003666 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3667 params.vht_capa =
3668 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3669
Johannes Bergf8bacc22013-02-14 23:27:01 +01003670 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07003671 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003672 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3673 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3674 return -EINVAL;
3675 }
Javier Cardona96b78df2011-04-07 15:08:33 -07003676
Johannes Berg984c3112013-02-14 23:43:25 +01003677 if (info->attrs[NL80211_ATTR_STA_WME]) {
3678 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3679 struct nlattr *nla;
3680
3681 nla = info->attrs[NL80211_ATTR_STA_WME];
3682 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3683 nl80211_sta_wme_policy);
3684 if (err)
3685 return err;
3686
3687 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3688 params.uapsd_queues =
3689 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3690 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3691 return -EINVAL;
3692
3693 if (tb[NL80211_STA_WME_MAX_SP])
3694 params.max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3695
3696 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3697 return -EINVAL;
3698
3699 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3700 }
Johannes Bergbdd90d52011-12-14 12:20:27 +01003701
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003702 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003703 return -EINVAL;
3704
Johannes Bergbdd90d52011-12-14 12:20:27 +01003705 switch (dev->ieee80211_ptr->iftype) {
3706 case NL80211_IFTYPE_AP:
3707 case NL80211_IFTYPE_AP_VLAN:
3708 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01003709 /* ignore WME attributes if iface/sta is not capable */
3710 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
3711 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
3712 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003713
Johannes Bergbdd90d52011-12-14 12:20:27 +01003714 /* TDLS peers cannot be added */
3715 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003716 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003717 /* but don't bother the driver with it */
3718 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003719
Johannes Bergd582cff2012-10-26 17:53:44 +02003720 /* allow authenticated/associated only if driver handles it */
3721 if (!(rdev->wiphy.features &
3722 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3723 params.sta_flags_mask &
3724 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3725 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3726 return -EINVAL;
3727
Johannes Bergbdd90d52011-12-14 12:20:27 +01003728 /* must be last in here for error handling */
3729 params.vlan = get_vlan(info, rdev);
3730 if (IS_ERR(params.vlan))
3731 return PTR_ERR(params.vlan);
3732 break;
3733 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01003734 /* ignore uAPSD data */
3735 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
3736
Johannes Bergd582cff2012-10-26 17:53:44 +02003737 /* associated is disallowed */
3738 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3739 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003740 /* TDLS peers cannot be added */
3741 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003742 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003743 break;
3744 case NL80211_IFTYPE_STATION:
Johannes Berg984c3112013-02-14 23:43:25 +01003745 /* ignore uAPSD data */
3746 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
3747
Johannes Bergd582cff2012-10-26 17:53:44 +02003748 /* associated is disallowed */
3749 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3750 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003751 /* Only TDLS peers can be added */
3752 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3753 return -EINVAL;
3754 /* Can only add if TDLS ... */
3755 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3756 return -EOPNOTSUPP;
3757 /* ... with external setup is supported */
3758 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3759 return -EOPNOTSUPP;
3760 break;
3761 default:
3762 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003763 }
3764
Johannes Bergbdd90d52011-12-14 12:20:27 +01003765 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003766
Hila Gonene35e4d22012-06-27 17:19:42 +03003767 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003768
Johannes Berg5727ef12007-12-19 02:03:34 +01003769 if (params.vlan)
3770 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003771 return err;
3772}
3773
3774static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3775{
Johannes Berg4c476992010-10-04 21:36:35 +02003776 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3777 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003778 u8 *mac_addr = NULL;
3779
3780 if (info->attrs[NL80211_ATTR_MAC])
3781 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3782
Johannes Berge80cf852009-05-11 14:43:13 +02003783 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003784 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003785 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003786 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3787 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003788
Johannes Berg4c476992010-10-04 21:36:35 +02003789 if (!rdev->ops->del_station)
3790 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003791
Hila Gonene35e4d22012-06-27 17:19:42 +03003792 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003793}
3794
Eric W. Biederman15e47302012-09-07 20:12:54 +00003795static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003796 int flags, struct net_device *dev,
3797 u8 *dst, u8 *next_hop,
3798 struct mpath_info *pinfo)
3799{
3800 void *hdr;
3801 struct nlattr *pinfoattr;
3802
Eric W. Biederman15e47302012-09-07 20:12:54 +00003803 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003804 if (!hdr)
3805 return -1;
3806
David S. Miller9360ffd2012-03-29 04:41:26 -04003807 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3808 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3809 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3810 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3811 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003812
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003813 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3814 if (!pinfoattr)
3815 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003816 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3817 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3818 pinfo->frame_qlen))
3819 goto nla_put_failure;
3820 if (((pinfo->filled & MPATH_INFO_SN) &&
3821 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3822 ((pinfo->filled & MPATH_INFO_METRIC) &&
3823 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3824 pinfo->metric)) ||
3825 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3826 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3827 pinfo->exptime)) ||
3828 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3829 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3830 pinfo->flags)) ||
3831 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3832 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3833 pinfo->discovery_timeout)) ||
3834 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3835 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3836 pinfo->discovery_retries)))
3837 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003838
3839 nla_nest_end(msg, pinfoattr);
3840
3841 return genlmsg_end(msg, hdr);
3842
3843 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003844 genlmsg_cancel(msg, hdr);
3845 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003846}
3847
3848static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003849 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003850{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003851 struct mpath_info pinfo;
3852 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003853 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003854 u8 dst[ETH_ALEN];
3855 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003856 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003857 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003858
Johannes Berg67748892010-10-04 21:14:06 +02003859 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3860 if (err)
3861 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003862
3863 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003864 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003865 goto out_err;
3866 }
3867
Jouni Malineneec60b02009-03-20 21:21:19 +02003868 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3869 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003870 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003871 }
3872
Johannes Bergbba95fe2008-07-29 13:22:51 +02003873 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003874 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3875 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003876 if (err == -ENOENT)
3877 break;
3878 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003879 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003880
Eric W. Biederman15e47302012-09-07 20:12:54 +00003881 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003882 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3883 netdev, dst, next_hop,
3884 &pinfo) < 0)
3885 goto out;
3886
3887 path_idx++;
3888 }
3889
3890
3891 out:
3892 cb->args[1] = path_idx;
3893 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003894 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003895 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003896 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003897}
3898
3899static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3900{
Johannes Berg4c476992010-10-04 21:36:35 +02003901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003902 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003903 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003904 struct mpath_info pinfo;
3905 struct sk_buff *msg;
3906 u8 *dst = NULL;
3907 u8 next_hop[ETH_ALEN];
3908
3909 memset(&pinfo, 0, sizeof(pinfo));
3910
3911 if (!info->attrs[NL80211_ATTR_MAC])
3912 return -EINVAL;
3913
3914 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3915
Johannes Berg4c476992010-10-04 21:36:35 +02003916 if (!rdev->ops->get_mpath)
3917 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003918
Johannes Berg4c476992010-10-04 21:36:35 +02003919 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3920 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003921
Hila Gonene35e4d22012-06-27 17:19:42 +03003922 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003923 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003924 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003925
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003926 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003927 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003928 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003929
Eric W. Biederman15e47302012-09-07 20:12:54 +00003930 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003931 dev, dst, next_hop, &pinfo) < 0) {
3932 nlmsg_free(msg);
3933 return -ENOBUFS;
3934 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003935
Johannes Berg4c476992010-10-04 21:36:35 +02003936 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003937}
3938
3939static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3940{
Johannes Berg4c476992010-10-04 21:36:35 +02003941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3942 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003943 u8 *dst = NULL;
3944 u8 *next_hop = NULL;
3945
3946 if (!info->attrs[NL80211_ATTR_MAC])
3947 return -EINVAL;
3948
3949 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3950 return -EINVAL;
3951
3952 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3953 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3954
Johannes Berg4c476992010-10-04 21:36:35 +02003955 if (!rdev->ops->change_mpath)
3956 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003957
Johannes Berg4c476992010-10-04 21:36:35 +02003958 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3959 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003960
Hila Gonene35e4d22012-06-27 17:19:42 +03003961 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003962}
Johannes Berg4c476992010-10-04 21:36:35 +02003963
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003964static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3965{
Johannes Berg4c476992010-10-04 21:36:35 +02003966 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3967 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003968 u8 *dst = NULL;
3969 u8 *next_hop = NULL;
3970
3971 if (!info->attrs[NL80211_ATTR_MAC])
3972 return -EINVAL;
3973
3974 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3975 return -EINVAL;
3976
3977 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3978 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3979
Johannes Berg4c476992010-10-04 21:36:35 +02003980 if (!rdev->ops->add_mpath)
3981 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003982
Johannes Berg4c476992010-10-04 21:36:35 +02003983 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3984 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003985
Hila Gonene35e4d22012-06-27 17:19:42 +03003986 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003987}
3988
3989static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3990{
Johannes Berg4c476992010-10-04 21:36:35 +02003991 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3992 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003993 u8 *dst = NULL;
3994
3995 if (info->attrs[NL80211_ATTR_MAC])
3996 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3997
Johannes Berg4c476992010-10-04 21:36:35 +02003998 if (!rdev->ops->del_mpath)
3999 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004000
Hila Gonene35e4d22012-06-27 17:19:42 +03004001 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004002}
4003
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004004static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4005{
Johannes Berg4c476992010-10-04 21:36:35 +02004006 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4007 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004008 struct bss_parameters params;
4009
4010 memset(&params, 0, sizeof(params));
4011 /* default to not changing parameters */
4012 params.use_cts_prot = -1;
4013 params.use_short_preamble = -1;
4014 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004015 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004016 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004017 params.p2p_ctwindow = -1;
4018 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004019
4020 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4021 params.use_cts_prot =
4022 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4023 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4024 params.use_short_preamble =
4025 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4026 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4027 params.use_short_slot_time =
4028 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004029 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4030 params.basic_rates =
4031 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4032 params.basic_rates_len =
4033 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4034 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004035 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4036 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004037 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4038 params.ht_opmode =
4039 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004040
Johannes Berg53cabad2012-11-14 15:17:28 +01004041 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4042 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4043 return -EINVAL;
4044 params.p2p_ctwindow =
4045 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4046 if (params.p2p_ctwindow < 0)
4047 return -EINVAL;
4048 if (params.p2p_ctwindow != 0 &&
4049 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4050 return -EINVAL;
4051 }
4052
4053 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4054 u8 tmp;
4055
4056 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4057 return -EINVAL;
4058 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4059 if (tmp > 1)
4060 return -EINVAL;
4061 params.p2p_opp_ps = tmp;
4062 if (params.p2p_opp_ps &&
4063 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4064 return -EINVAL;
4065 }
4066
Johannes Berg4c476992010-10-04 21:36:35 +02004067 if (!rdev->ops->change_bss)
4068 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004069
Johannes Berg074ac8d2010-09-16 14:58:22 +02004070 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004071 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4072 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004073
Hila Gonene35e4d22012-06-27 17:19:42 +03004074 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004075}
4076
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004077static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004078 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4079 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4080 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4081 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4082 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4083 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4084};
4085
4086static int parse_reg_rule(struct nlattr *tb[],
4087 struct ieee80211_reg_rule *reg_rule)
4088{
4089 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4090 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4091
4092 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4093 return -EINVAL;
4094 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4095 return -EINVAL;
4096 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4097 return -EINVAL;
4098 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4099 return -EINVAL;
4100 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4101 return -EINVAL;
4102
4103 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4104
4105 freq_range->start_freq_khz =
4106 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4107 freq_range->end_freq_khz =
4108 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4109 freq_range->max_bandwidth_khz =
4110 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4111
4112 power_rule->max_eirp =
4113 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4114
4115 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4116 power_rule->max_antenna_gain =
4117 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4118
4119 return 0;
4120}
4121
4122static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4123{
4124 int r;
4125 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004126 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004127
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004128 /*
4129 * You should only get this when cfg80211 hasn't yet initialized
4130 * completely when built-in to the kernel right between the time
4131 * window between nl80211_init() and regulatory_init(), if that is
4132 * even possible.
4133 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004134 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004135 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004136
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004137 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4138 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004139
4140 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4141
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004142 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4143 user_reg_hint_type =
4144 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4145 else
4146 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4147
4148 switch (user_reg_hint_type) {
4149 case NL80211_USER_REG_HINT_USER:
4150 case NL80211_USER_REG_HINT_CELL_BASE:
4151 break;
4152 default:
4153 return -EINVAL;
4154 }
4155
4156 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004157
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004158 return r;
4159}
4160
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004161static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004162 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004163{
Johannes Berg4c476992010-10-04 21:36:35 +02004164 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004165 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004166 struct wireless_dev *wdev = dev->ieee80211_ptr;
4167 struct mesh_config cur_params;
4168 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004169 void *hdr;
4170 struct nlattr *pinfoattr;
4171 struct sk_buff *msg;
4172
Johannes Berg29cbe682010-12-03 09:20:44 +01004173 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4174 return -EOPNOTSUPP;
4175
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004176 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004177 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004178
Johannes Berg29cbe682010-12-03 09:20:44 +01004179 wdev_lock(wdev);
4180 /* If not connected, get default parameters */
4181 if (!wdev->mesh_id_len)
4182 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4183 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004184 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004185 wdev_unlock(wdev);
4186
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004187 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004188 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004189
4190 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004191 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004192 if (!msg)
4193 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004194 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004195 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004196 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004197 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004198 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004199 if (!pinfoattr)
4200 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004201 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4202 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4203 cur_params.dot11MeshRetryTimeout) ||
4204 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4205 cur_params.dot11MeshConfirmTimeout) ||
4206 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4207 cur_params.dot11MeshHoldingTimeout) ||
4208 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4209 cur_params.dot11MeshMaxPeerLinks) ||
4210 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4211 cur_params.dot11MeshMaxRetries) ||
4212 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4213 cur_params.dot11MeshTTL) ||
4214 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4215 cur_params.element_ttl) ||
4216 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4217 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004218 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4219 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004220 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4221 cur_params.dot11MeshHWMPmaxPREQretries) ||
4222 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4223 cur_params.path_refresh_time) ||
4224 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4225 cur_params.min_discovery_timeout) ||
4226 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4227 cur_params.dot11MeshHWMPactivePathTimeout) ||
4228 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4229 cur_params.dot11MeshHWMPpreqMinInterval) ||
4230 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4231 cur_params.dot11MeshHWMPperrMinInterval) ||
4232 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4233 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4234 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4235 cur_params.dot11MeshHWMPRootMode) ||
4236 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4237 cur_params.dot11MeshHWMPRannInterval) ||
4238 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4239 cur_params.dot11MeshGateAnnouncementProtocol) ||
4240 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4241 cur_params.dot11MeshForwarding) ||
4242 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004243 cur_params.rssi_threshold) ||
4244 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004245 cur_params.ht_opmode) ||
4246 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4247 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4248 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004249 cur_params.dot11MeshHWMProotInterval) ||
4250 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004251 cur_params.dot11MeshHWMPconfirmationInterval) ||
4252 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4253 cur_params.power_mode) ||
4254 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4255 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004256 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004257 nla_nest_end(msg, pinfoattr);
4258 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004259 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004260
Johannes Berg3b858752009-03-12 09:55:09 +01004261 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004262 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004263 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004264 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004265 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004266}
4267
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004268static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004269 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4270 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4271 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4272 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4273 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4274 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004275 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004276 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004277 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004278 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4279 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4280 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4281 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4282 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004283 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004284 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004285 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004286 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004287 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004288 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004289 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4290 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004291 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4292 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004293 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004294 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4295 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004296};
4297
Javier Cardonac80d5452010-12-16 17:37:49 -08004298static const struct nla_policy
4299 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004300 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004301 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4302 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004303 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004304 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004305 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004306 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004307};
4308
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004309static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004310 struct mesh_config *cfg,
4311 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004312{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004313 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004314 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004315
Marco Porschea54fba2013-01-07 16:04:48 +01004316#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4317do { \
4318 if (tb[attr]) { \
4319 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4320 return -EINVAL; \
4321 cfg->param = fn(tb[attr]); \
4322 mask |= (1 << (attr - 1)); \
4323 } \
4324} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004325
4326
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004327 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004328 return -EINVAL;
4329 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004330 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004331 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004332 return -EINVAL;
4333
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004334 /* This makes sure that there aren't more than 32 mesh config
4335 * parameters (otherwise our bitfield scheme would not work.) */
4336 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4337
4338 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004339 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004340 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4341 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004342 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004343 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4344 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004345 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004346 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4347 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004348 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004349 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4350 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004351 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004352 mask, NL80211_MESHCONF_MAX_RETRIES,
4353 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004354 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004355 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004356 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004357 mask, NL80211_MESHCONF_ELEMENT_TTL,
4358 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004359 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004360 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4361 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004362 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4363 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004364 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4365 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004366 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004367 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4368 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004369 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004370 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4371 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004372 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004373 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4374 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004375 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4376 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004377 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4378 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004379 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004380 1, 65535, mask,
4381 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004382 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004383 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004384 1, 65535, mask,
4385 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004386 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004387 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004388 dot11MeshHWMPnetDiameterTraversalTime,
4389 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004390 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4391 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004392 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4393 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4394 nla_get_u8);
4395 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4396 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004397 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004398 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004399 dot11MeshGateAnnouncementProtocol, 0, 1,
4400 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004401 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004402 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004403 mask, NL80211_MESHCONF_FORWARDING,
4404 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004405 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004406 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4407 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004408 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004409 mask, NL80211_MESHCONF_HT_OPMODE,
4410 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004411 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004412 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004413 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4414 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004415 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004416 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4417 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004418 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004419 dot11MeshHWMPconfirmationInterval,
4420 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004421 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4422 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004423 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4424 NL80211_MESH_POWER_ACTIVE,
4425 NL80211_MESH_POWER_MAX,
4426 mask, NL80211_MESHCONF_POWER_MODE,
4427 nla_get_u32);
4428 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4429 0, 65535, mask,
4430 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004431 if (mask_out)
4432 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004433
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004434 return 0;
4435
4436#undef FILL_IN_MESH_PARAM_IF_SET
4437}
4438
Javier Cardonac80d5452010-12-16 17:37:49 -08004439static int nl80211_parse_mesh_setup(struct genl_info *info,
4440 struct mesh_setup *setup)
4441{
4442 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4443
4444 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4445 return -EINVAL;
4446 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4447 info->attrs[NL80211_ATTR_MESH_SETUP],
4448 nl80211_mesh_setup_params_policy))
4449 return -EINVAL;
4450
Javier Cardonad299a1f2012-03-31 11:31:33 -07004451 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4452 setup->sync_method =
4453 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4454 IEEE80211_SYNC_METHOD_VENDOR :
4455 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4456
Javier Cardonac80d5452010-12-16 17:37:49 -08004457 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4458 setup->path_sel_proto =
4459 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4460 IEEE80211_PATH_PROTOCOL_VENDOR :
4461 IEEE80211_PATH_PROTOCOL_HWMP;
4462
4463 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4464 setup->path_metric =
4465 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4466 IEEE80211_PATH_METRIC_VENDOR :
4467 IEEE80211_PATH_METRIC_AIRTIME;
4468
Javier Cardona581a8b02011-04-07 15:08:27 -07004469
4470 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004471 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004472 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004473 if (!is_valid_ie_attr(ieattr))
4474 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004475 setup->ie = nla_data(ieattr);
4476 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004477 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004478 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4479 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004480
4481 return 0;
4482}
4483
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004484static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004485 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004486{
4487 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4488 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004489 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004490 struct mesh_config cfg;
4491 u32 mask;
4492 int err;
4493
Johannes Berg29cbe682010-12-03 09:20:44 +01004494 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4495 return -EOPNOTSUPP;
4496
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004497 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004498 return -EOPNOTSUPP;
4499
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004500 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004501 if (err)
4502 return err;
4503
Johannes Berg29cbe682010-12-03 09:20:44 +01004504 wdev_lock(wdev);
4505 if (!wdev->mesh_id_len)
4506 err = -ENOLINK;
4507
4508 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004509 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004510
4511 wdev_unlock(wdev);
4512
4513 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004514}
4515
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004516static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4517{
Johannes Berg458f4f92012-12-06 15:47:38 +01004518 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004519 struct sk_buff *msg;
4520 void *hdr = NULL;
4521 struct nlattr *nl_reg_rules;
4522 unsigned int i;
4523 int err = -EINVAL;
4524
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004525 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004526
4527 if (!cfg80211_regdomain)
4528 goto out;
4529
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004530 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004531 if (!msg) {
4532 err = -ENOBUFS;
4533 goto out;
4534 }
4535
Eric W. Biederman15e47302012-09-07 20:12:54 +00004536 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004537 NL80211_CMD_GET_REG);
4538 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004539 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004540
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004541 if (reg_last_request_cell_base() &&
4542 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4543 NL80211_USER_REG_HINT_CELL_BASE))
4544 goto nla_put_failure;
4545
Johannes Berg458f4f92012-12-06 15:47:38 +01004546 rcu_read_lock();
4547 regdom = rcu_dereference(cfg80211_regdomain);
4548
4549 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4550 (regdom->dfs_region &&
4551 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4552 goto nla_put_failure_rcu;
4553
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004554 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4555 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004556 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004557
Johannes Berg458f4f92012-12-06 15:47:38 +01004558 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004559 struct nlattr *nl_reg_rule;
4560 const struct ieee80211_reg_rule *reg_rule;
4561 const struct ieee80211_freq_range *freq_range;
4562 const struct ieee80211_power_rule *power_rule;
4563
Johannes Berg458f4f92012-12-06 15:47:38 +01004564 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004565 freq_range = &reg_rule->freq_range;
4566 power_rule = &reg_rule->power_rule;
4567
4568 nl_reg_rule = nla_nest_start(msg, i);
4569 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004570 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004571
David S. Miller9360ffd2012-03-29 04:41:26 -04004572 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4573 reg_rule->flags) ||
4574 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4575 freq_range->start_freq_khz) ||
4576 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4577 freq_range->end_freq_khz) ||
4578 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4579 freq_range->max_bandwidth_khz) ||
4580 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4581 power_rule->max_antenna_gain) ||
4582 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4583 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004584 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004585
4586 nla_nest_end(msg, nl_reg_rule);
4587 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004588 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004589
4590 nla_nest_end(msg, nl_reg_rules);
4591
4592 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004593 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004594 goto out;
4595
Johannes Berg458f4f92012-12-06 15:47:38 +01004596nla_put_failure_rcu:
4597 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004598nla_put_failure:
4599 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004600put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004601 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004602 err = -EMSGSIZE;
4603out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004604 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004605 return err;
4606}
4607
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004608static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4609{
4610 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4611 struct nlattr *nl_reg_rule;
4612 char *alpha2 = NULL;
4613 int rem_reg_rules = 0, r = 0;
4614 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004615 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004616 struct ieee80211_regdomain *rd = NULL;
4617
4618 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4619 return -EINVAL;
4620
4621 if (!info->attrs[NL80211_ATTR_REG_RULES])
4622 return -EINVAL;
4623
4624 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4625
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004626 if (info->attrs[NL80211_ATTR_DFS_REGION])
4627 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4628
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004629 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004630 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004631 num_rules++;
4632 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004633 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004634 }
4635
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004636 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004637 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004638
4639 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004640 if (!rd)
4641 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004642
4643 rd->n_reg_rules = num_rules;
4644 rd->alpha2[0] = alpha2[0];
4645 rd->alpha2[1] = alpha2[1];
4646
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004647 /*
4648 * Disable DFS master mode if the DFS region was
4649 * not supported or known on this kernel.
4650 */
4651 if (reg_supported_dfs_region(dfs_region))
4652 rd->dfs_region = dfs_region;
4653
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004654 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004655 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004656 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004657 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4658 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004659 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4660 if (r)
4661 goto bad_reg;
4662
4663 rule_idx++;
4664
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004665 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4666 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004667 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004668 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004669 }
4670
Johannes Berg6913b492012-12-04 00:48:59 +01004671 mutex_lock(&cfg80211_mutex);
4672
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004673 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004674 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004675 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004676 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004677
Johannes Bergd2372b32008-10-24 20:32:20 +02004678 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004679 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004680 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004681}
4682
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004683static int validate_scan_freqs(struct nlattr *freqs)
4684{
4685 struct nlattr *attr1, *attr2;
4686 int n_channels = 0, tmp1, tmp2;
4687
4688 nla_for_each_nested(attr1, freqs, tmp1) {
4689 n_channels++;
4690 /*
4691 * Some hardware has a limited channel list for
4692 * scanning, and it is pretty much nonsensical
4693 * to scan for a channel twice, so disallow that
4694 * and don't require drivers to check that the
4695 * channel list they get isn't longer than what
4696 * they can scan, as long as they can scan all
4697 * the channels they registered at once.
4698 */
4699 nla_for_each_nested(attr2, freqs, tmp2)
4700 if (attr1 != attr2 &&
4701 nla_get_u32(attr1) == nla_get_u32(attr2))
4702 return 0;
4703 }
4704
4705 return n_channels;
4706}
4707
Johannes Berg2a519312009-02-10 21:25:55 +01004708static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4709{
Johannes Berg4c476992010-10-04 21:36:35 +02004710 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004711 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004712 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004713 struct nlattr *attr;
4714 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004715 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004716 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004717
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004718 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4719 return -EINVAL;
4720
Johannes Berg79c97e92009-07-07 03:56:12 +02004721 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004722
Johannes Berg4c476992010-10-04 21:36:35 +02004723 if (!rdev->ops->scan)
4724 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004725
Johannes Berg4c476992010-10-04 21:36:35 +02004726 if (rdev->scan_req)
4727 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004728
4729 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004730 n_channels = validate_scan_freqs(
4731 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004732 if (!n_channels)
4733 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004734 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004735 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004736 n_channels = 0;
4737
Johannes Berg2a519312009-02-10 21:25:55 +01004738 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4739 if (wiphy->bands[band])
4740 n_channels += wiphy->bands[band]->n_channels;
4741 }
4742
4743 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4744 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4745 n_ssids++;
4746
Johannes Berg4c476992010-10-04 21:36:35 +02004747 if (n_ssids > wiphy->max_scan_ssids)
4748 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004749
Jouni Malinen70692ad2009-02-16 19:39:13 +02004750 if (info->attrs[NL80211_ATTR_IE])
4751 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4752 else
4753 ie_len = 0;
4754
Johannes Berg4c476992010-10-04 21:36:35 +02004755 if (ie_len > wiphy->max_scan_ie_len)
4756 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004757
Johannes Berg2a519312009-02-10 21:25:55 +01004758 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004759 + sizeof(*request->ssids) * n_ssids
4760 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004761 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004762 if (!request)
4763 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004764
Johannes Berg2a519312009-02-10 21:25:55 +01004765 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004766 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004767 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004768 if (ie_len) {
4769 if (request->ssids)
4770 request->ie = (void *)(request->ssids + n_ssids);
4771 else
4772 request->ie = (void *)(request->channels + n_channels);
4773 }
Johannes Berg2a519312009-02-10 21:25:55 +01004774
Johannes Berg584991d2009-11-02 13:32:03 +01004775 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004776 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4777 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004778 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004779 struct ieee80211_channel *chan;
4780
4781 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4782
4783 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004784 err = -EINVAL;
4785 goto out_free;
4786 }
Johannes Berg584991d2009-11-02 13:32:03 +01004787
4788 /* ignore disabled channels */
4789 if (chan->flags & IEEE80211_CHAN_DISABLED)
4790 continue;
4791
4792 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004793 i++;
4794 }
4795 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004796 enum ieee80211_band band;
4797
Johannes Berg2a519312009-02-10 21:25:55 +01004798 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004799 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4800 int j;
4801 if (!wiphy->bands[band])
4802 continue;
4803 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004804 struct ieee80211_channel *chan;
4805
4806 chan = &wiphy->bands[band]->channels[j];
4807
4808 if (chan->flags & IEEE80211_CHAN_DISABLED)
4809 continue;
4810
4811 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004812 i++;
4813 }
4814 }
4815 }
4816
Johannes Berg584991d2009-11-02 13:32:03 +01004817 if (!i) {
4818 err = -EINVAL;
4819 goto out_free;
4820 }
4821
4822 request->n_channels = i;
4823
Johannes Berg2a519312009-02-10 21:25:55 +01004824 i = 0;
4825 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4826 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004827 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004828 err = -EINVAL;
4829 goto out_free;
4830 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004831 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004832 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004833 i++;
4834 }
4835 }
4836
Jouni Malinen70692ad2009-02-16 19:39:13 +02004837 if (info->attrs[NL80211_ATTR_IE]) {
4838 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004839 memcpy((void *)request->ie,
4840 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004841 request->ie_len);
4842 }
4843
Johannes Berg34850ab2011-07-18 18:08:35 +02004844 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004845 if (wiphy->bands[i])
4846 request->rates[i] =
4847 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004848
4849 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4850 nla_for_each_nested(attr,
4851 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4852 tmp) {
4853 enum ieee80211_band band = nla_type(attr);
4854
Dan Carpenter84404622011-07-29 11:52:18 +03004855 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004856 err = -EINVAL;
4857 goto out_free;
4858 }
4859 err = ieee80211_get_ratemask(wiphy->bands[band],
4860 nla_data(attr),
4861 nla_len(attr),
4862 &request->rates[band]);
4863 if (err)
4864 goto out_free;
4865 }
4866 }
4867
Sam Leffler46856bb2012-10-11 21:03:32 -07004868 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004869 request->flags = nla_get_u32(
4870 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004871 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4872 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4873 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4874 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004875 err = -EOPNOTSUPP;
4876 goto out_free;
4877 }
4878 }
Sam Lefflered4737712012-10-11 21:03:31 -07004879
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304880 request->no_cck =
4881 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4882
Johannes Bergfd014282012-06-18 19:17:03 +02004883 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004884 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004885 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004886
Johannes Berg79c97e92009-07-07 03:56:12 +02004887 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004888 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004889
Johannes Berg463d0182009-07-14 00:33:35 +02004890 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004891 nl80211_send_scan_start(rdev, wdev);
4892 if (wdev->netdev)
4893 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004894 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004895 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004896 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004897 kfree(request);
4898 }
Johannes Berg3b858752009-03-12 09:55:09 +01004899
Johannes Berg2a519312009-02-10 21:25:55 +01004900 return err;
4901}
4902
Luciano Coelho807f8a82011-05-11 17:09:35 +03004903static int nl80211_start_sched_scan(struct sk_buff *skb,
4904 struct genl_info *info)
4905{
4906 struct cfg80211_sched_scan_request *request;
4907 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4908 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004909 struct nlattr *attr;
4910 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004911 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004912 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004913 enum ieee80211_band band;
4914 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004915 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004916
4917 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4918 !rdev->ops->sched_scan_start)
4919 return -EOPNOTSUPP;
4920
4921 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4922 return -EINVAL;
4923
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004924 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4925 return -EINVAL;
4926
4927 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4928 if (interval == 0)
4929 return -EINVAL;
4930
Luciano Coelho807f8a82011-05-11 17:09:35 +03004931 wiphy = &rdev->wiphy;
4932
4933 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4934 n_channels = validate_scan_freqs(
4935 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4936 if (!n_channels)
4937 return -EINVAL;
4938 } else {
4939 n_channels = 0;
4940
4941 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4942 if (wiphy->bands[band])
4943 n_channels += wiphy->bands[band]->n_channels;
4944 }
4945
4946 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4947 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4948 tmp)
4949 n_ssids++;
4950
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004951 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004952 return -EINVAL;
4953
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004954 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4955 nla_for_each_nested(attr,
4956 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4957 tmp)
4958 n_match_sets++;
4959
4960 if (n_match_sets > wiphy->max_match_sets)
4961 return -EINVAL;
4962
Luciano Coelho807f8a82011-05-11 17:09:35 +03004963 if (info->attrs[NL80211_ATTR_IE])
4964 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4965 else
4966 ie_len = 0;
4967
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004968 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004969 return -EINVAL;
4970
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004971 mutex_lock(&rdev->sched_scan_mtx);
4972
4973 if (rdev->sched_scan_req) {
4974 err = -EINPROGRESS;
4975 goto out;
4976 }
4977
Luciano Coelho807f8a82011-05-11 17:09:35 +03004978 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004979 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004980 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004981 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004982 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004983 if (!request) {
4984 err = -ENOMEM;
4985 goto out;
4986 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004987
4988 if (n_ssids)
4989 request->ssids = (void *)&request->channels[n_channels];
4990 request->n_ssids = n_ssids;
4991 if (ie_len) {
4992 if (request->ssids)
4993 request->ie = (void *)(request->ssids + n_ssids);
4994 else
4995 request->ie = (void *)(request->channels + n_channels);
4996 }
4997
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004998 if (n_match_sets) {
4999 if (request->ie)
5000 request->match_sets = (void *)(request->ie + ie_len);
5001 else if (request->ssids)
5002 request->match_sets =
5003 (void *)(request->ssids + n_ssids);
5004 else
5005 request->match_sets =
5006 (void *)(request->channels + n_channels);
5007 }
5008 request->n_match_sets = n_match_sets;
5009
Luciano Coelho807f8a82011-05-11 17:09:35 +03005010 i = 0;
5011 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5012 /* user specified, bail out if channel not found */
5013 nla_for_each_nested(attr,
5014 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5015 tmp) {
5016 struct ieee80211_channel *chan;
5017
5018 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5019
5020 if (!chan) {
5021 err = -EINVAL;
5022 goto out_free;
5023 }
5024
5025 /* ignore disabled channels */
5026 if (chan->flags & IEEE80211_CHAN_DISABLED)
5027 continue;
5028
5029 request->channels[i] = chan;
5030 i++;
5031 }
5032 } else {
5033 /* all channels */
5034 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5035 int j;
5036 if (!wiphy->bands[band])
5037 continue;
5038 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5039 struct ieee80211_channel *chan;
5040
5041 chan = &wiphy->bands[band]->channels[j];
5042
5043 if (chan->flags & IEEE80211_CHAN_DISABLED)
5044 continue;
5045
5046 request->channels[i] = chan;
5047 i++;
5048 }
5049 }
5050 }
5051
5052 if (!i) {
5053 err = -EINVAL;
5054 goto out_free;
5055 }
5056
5057 request->n_channels = i;
5058
5059 i = 0;
5060 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5061 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5062 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005063 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005064 err = -EINVAL;
5065 goto out_free;
5066 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005067 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005068 memcpy(request->ssids[i].ssid, nla_data(attr),
5069 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005070 i++;
5071 }
5072 }
5073
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005074 i = 0;
5075 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5076 nla_for_each_nested(attr,
5077 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5078 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005079 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005080
5081 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5082 nla_data(attr), nla_len(attr),
5083 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005084 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005085 if (ssid) {
5086 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5087 err = -EINVAL;
5088 goto out_free;
5089 }
5090 memcpy(request->match_sets[i].ssid.ssid,
5091 nla_data(ssid), nla_len(ssid));
5092 request->match_sets[i].ssid.ssid_len =
5093 nla_len(ssid);
5094 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005095 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5096 if (rssi)
5097 request->rssi_thold = nla_get_u32(rssi);
5098 else
5099 request->rssi_thold =
5100 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005101 i++;
5102 }
5103 }
5104
Luciano Coelho807f8a82011-05-11 17:09:35 +03005105 if (info->attrs[NL80211_ATTR_IE]) {
5106 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5107 memcpy((void *)request->ie,
5108 nla_data(info->attrs[NL80211_ATTR_IE]),
5109 request->ie_len);
5110 }
5111
Sam Leffler46856bb2012-10-11 21:03:32 -07005112 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005113 request->flags = nla_get_u32(
5114 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005115 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5116 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5117 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5118 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005119 err = -EOPNOTSUPP;
5120 goto out_free;
5121 }
5122 }
Sam Lefflered4737712012-10-11 21:03:31 -07005123
Luciano Coelho807f8a82011-05-11 17:09:35 +03005124 request->dev = dev;
5125 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005126 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005127 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005128
Hila Gonene35e4d22012-06-27 17:19:42 +03005129 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005130 if (!err) {
5131 rdev->sched_scan_req = request;
5132 nl80211_send_sched_scan(rdev, dev,
5133 NL80211_CMD_START_SCHED_SCAN);
5134 goto out;
5135 }
5136
5137out_free:
5138 kfree(request);
5139out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005140 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005141 return err;
5142}
5143
5144static int nl80211_stop_sched_scan(struct sk_buff *skb,
5145 struct genl_info *info)
5146{
5147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005148 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005149
5150 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5151 !rdev->ops->sched_scan_stop)
5152 return -EOPNOTSUPP;
5153
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005154 mutex_lock(&rdev->sched_scan_mtx);
5155 err = __cfg80211_stop_sched_scan(rdev, false);
5156 mutex_unlock(&rdev->sched_scan_mtx);
5157
5158 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005159}
5160
Simon Wunderlich04f39042013-02-08 18:16:19 +01005161static int nl80211_start_radar_detection(struct sk_buff *skb,
5162 struct genl_info *info)
5163{
5164 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5165 struct net_device *dev = info->user_ptr[1];
5166 struct wireless_dev *wdev = dev->ieee80211_ptr;
5167 struct cfg80211_chan_def chandef;
5168 int err;
5169
5170 err = nl80211_parse_chandef(rdev, info, &chandef);
5171 if (err)
5172 return err;
5173
5174 if (wdev->cac_started)
5175 return -EBUSY;
5176
5177 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5178 if (err < 0)
5179 return err;
5180
5181 if (err == 0)
5182 return -EINVAL;
5183
5184 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5185 return -EINVAL;
5186
5187 if (!rdev->ops->start_radar_detection)
5188 return -EOPNOTSUPP;
5189
5190 mutex_lock(&rdev->devlist_mtx);
5191 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5192 chandef.chan, CHAN_MODE_SHARED,
5193 BIT(chandef.width));
5194 if (err)
5195 goto err_locked;
5196
5197 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5198 if (!err) {
5199 wdev->channel = chandef.chan;
5200 wdev->cac_started = true;
5201 wdev->cac_start_time = jiffies;
5202 }
5203err_locked:
5204 mutex_unlock(&rdev->devlist_mtx);
5205
5206 return err;
5207}
5208
Johannes Berg9720bb32011-06-21 09:45:33 +02005209static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5210 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005211 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005212 struct wireless_dev *wdev,
5213 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005214{
Johannes Berg48ab9052009-07-10 18:42:31 +02005215 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005216 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005217 void *hdr;
5218 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005219 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005220
5221 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005222
Eric W. Biederman15e47302012-09-07 20:12:54 +00005223 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005224 NL80211_CMD_NEW_SCAN_RESULTS);
5225 if (!hdr)
5226 return -1;
5227
Johannes Berg9720bb32011-06-21 09:45:33 +02005228 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5229
David S. Miller9360ffd2012-03-29 04:41:26 -04005230 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5231 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5232 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005233
5234 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5235 if (!bss)
5236 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005237 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005238 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005239 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005240
5241 rcu_read_lock();
5242 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005243 if (ies) {
5244 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5245 goto fail_unlock_rcu;
5246 tsf = true;
5247 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5248 ies->len, ies->data))
5249 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005250 }
5251 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005252 if (ies) {
5253 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5254 goto fail_unlock_rcu;
5255 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5256 ies->len, ies->data))
5257 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005258 }
5259 rcu_read_unlock();
5260
David S. Miller9360ffd2012-03-29 04:41:26 -04005261 if (res->beacon_interval &&
5262 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5263 goto nla_put_failure;
5264 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5265 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5266 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5267 jiffies_to_msecs(jiffies - intbss->ts)))
5268 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005269
Johannes Berg77965c92009-02-18 18:45:06 +01005270 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005271 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005272 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5273 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005274 break;
5275 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005276 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5277 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005278 break;
5279 default:
5280 break;
5281 }
5282
Johannes Berg48ab9052009-07-10 18:42:31 +02005283 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005284 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005285 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005286 if (intbss == wdev->current_bss &&
5287 nla_put_u32(msg, NL80211_BSS_STATUS,
5288 NL80211_BSS_STATUS_ASSOCIATED))
5289 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005290 break;
5291 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005292 if (intbss == wdev->current_bss &&
5293 nla_put_u32(msg, NL80211_BSS_STATUS,
5294 NL80211_BSS_STATUS_IBSS_JOINED))
5295 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005296 break;
5297 default:
5298 break;
5299 }
5300
Johannes Berg2a519312009-02-10 21:25:55 +01005301 nla_nest_end(msg, bss);
5302
5303 return genlmsg_end(msg, hdr);
5304
Johannes Berg8cef2c92013-02-05 16:54:31 +01005305 fail_unlock_rcu:
5306 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005307 nla_put_failure:
5308 genlmsg_cancel(msg, hdr);
5309 return -EMSGSIZE;
5310}
5311
5312static int nl80211_dump_scan(struct sk_buff *skb,
5313 struct netlink_callback *cb)
5314{
Johannes Berg48ab9052009-07-10 18:42:31 +02005315 struct cfg80211_registered_device *rdev;
5316 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005317 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005318 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005319 int start = cb->args[1], idx = 0;
5320 int err;
5321
Johannes Berg67748892010-10-04 21:14:06 +02005322 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5323 if (err)
5324 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005325
Johannes Berg48ab9052009-07-10 18:42:31 +02005326 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005327
Johannes Berg48ab9052009-07-10 18:42:31 +02005328 wdev_lock(wdev);
5329 spin_lock_bh(&rdev->bss_lock);
5330 cfg80211_bss_expire(rdev);
5331
Johannes Berg9720bb32011-06-21 09:45:33 +02005332 cb->seq = rdev->bss_generation;
5333
Johannes Berg48ab9052009-07-10 18:42:31 +02005334 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005335 if (++idx <= start)
5336 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005337 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005338 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005339 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005340 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005341 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005342 }
5343 }
5344
Johannes Berg48ab9052009-07-10 18:42:31 +02005345 spin_unlock_bh(&rdev->bss_lock);
5346 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005347
5348 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005349 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005350
Johannes Berg67748892010-10-04 21:14:06 +02005351 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005352}
5353
Eric W. Biederman15e47302012-09-07 20:12:54 +00005354static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005355 int flags, struct net_device *dev,
5356 struct survey_info *survey)
5357{
5358 void *hdr;
5359 struct nlattr *infoattr;
5360
Eric W. Biederman15e47302012-09-07 20:12:54 +00005361 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005362 NL80211_CMD_NEW_SURVEY_RESULTS);
5363 if (!hdr)
5364 return -ENOMEM;
5365
David S. Miller9360ffd2012-03-29 04:41:26 -04005366 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5367 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005368
5369 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5370 if (!infoattr)
5371 goto nla_put_failure;
5372
David S. Miller9360ffd2012-03-29 04:41:26 -04005373 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5374 survey->channel->center_freq))
5375 goto nla_put_failure;
5376
5377 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5378 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5379 goto nla_put_failure;
5380 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5381 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5382 goto nla_put_failure;
5383 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5384 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5385 survey->channel_time))
5386 goto nla_put_failure;
5387 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5388 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5389 survey->channel_time_busy))
5390 goto nla_put_failure;
5391 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5392 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5393 survey->channel_time_ext_busy))
5394 goto nla_put_failure;
5395 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5396 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5397 survey->channel_time_rx))
5398 goto nla_put_failure;
5399 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5400 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5401 survey->channel_time_tx))
5402 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005403
5404 nla_nest_end(msg, infoattr);
5405
5406 return genlmsg_end(msg, hdr);
5407
5408 nla_put_failure:
5409 genlmsg_cancel(msg, hdr);
5410 return -EMSGSIZE;
5411}
5412
5413static int nl80211_dump_survey(struct sk_buff *skb,
5414 struct netlink_callback *cb)
5415{
5416 struct survey_info survey;
5417 struct cfg80211_registered_device *dev;
5418 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005419 int survey_idx = cb->args[1];
5420 int res;
5421
Johannes Berg67748892010-10-04 21:14:06 +02005422 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5423 if (res)
5424 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005425
5426 if (!dev->ops->dump_survey) {
5427 res = -EOPNOTSUPP;
5428 goto out_err;
5429 }
5430
5431 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005432 struct ieee80211_channel *chan;
5433
Hila Gonene35e4d22012-06-27 17:19:42 +03005434 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005435 if (res == -ENOENT)
5436 break;
5437 if (res)
5438 goto out_err;
5439
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005440 /* Survey without a channel doesn't make sense */
5441 if (!survey.channel) {
5442 res = -EINVAL;
5443 goto out;
5444 }
5445
5446 chan = ieee80211_get_channel(&dev->wiphy,
5447 survey.channel->center_freq);
5448 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5449 survey_idx++;
5450 continue;
5451 }
5452
Holger Schurig61fa7132009-11-11 12:25:40 +01005453 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005454 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005455 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5456 netdev,
5457 &survey) < 0)
5458 goto out;
5459 survey_idx++;
5460 }
5461
5462 out:
5463 cb->args[1] = survey_idx;
5464 res = skb->len;
5465 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005466 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005467 return res;
5468}
5469
Samuel Ortizb23aa672009-07-01 21:26:54 +02005470static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5471{
5472 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5473 NL80211_WPA_VERSION_2));
5474}
5475
Jouni Malinen636a5d32009-03-19 13:39:22 +02005476static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5477{
Johannes Berg4c476992010-10-04 21:36:35 +02005478 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5479 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005480 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005481 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5482 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005483 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005484 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005485 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005486
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005487 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5488 return -EINVAL;
5489
5490 if (!info->attrs[NL80211_ATTR_MAC])
5491 return -EINVAL;
5492
Jouni Malinen17780922009-03-27 20:52:47 +02005493 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5494 return -EINVAL;
5495
Johannes Berg19957bb2009-07-02 17:20:43 +02005496 if (!info->attrs[NL80211_ATTR_SSID])
5497 return -EINVAL;
5498
5499 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5500 return -EINVAL;
5501
Johannes Bergfffd0932009-07-08 14:22:54 +02005502 err = nl80211_parse_key(info, &key);
5503 if (err)
5504 return err;
5505
5506 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005507 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5508 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005509 if (!key.p.key || !key.p.key_len)
5510 return -EINVAL;
5511 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5512 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5513 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5514 key.p.key_len != WLAN_KEY_LEN_WEP104))
5515 return -EINVAL;
5516 if (key.idx > 4)
5517 return -EINVAL;
5518 } else {
5519 key.p.key_len = 0;
5520 key.p.key = NULL;
5521 }
5522
Johannes Bergafea0b72010-08-10 09:46:42 +02005523 if (key.idx >= 0) {
5524 int i;
5525 bool ok = false;
5526 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5527 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5528 ok = true;
5529 break;
5530 }
5531 }
Johannes Berg4c476992010-10-04 21:36:35 +02005532 if (!ok)
5533 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005534 }
5535
Johannes Berg4c476992010-10-04 21:36:35 +02005536 if (!rdev->ops->auth)
5537 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005538
Johannes Berg074ac8d2010-09-16 14:58:22 +02005539 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005540 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5541 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005542
Johannes Berg19957bb2009-07-02 17:20:43 +02005543 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005544 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005545 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005546 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5547 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005548
Johannes Berg19957bb2009-07-02 17:20:43 +02005549 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5550 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5551
5552 if (info->attrs[NL80211_ATTR_IE]) {
5553 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5554 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5555 }
5556
5557 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005558 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005559 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005560
Jouni Malinene39e5b52012-09-30 19:29:39 +03005561 if (auth_type == NL80211_AUTHTYPE_SAE &&
5562 !info->attrs[NL80211_ATTR_SAE_DATA])
5563 return -EINVAL;
5564
5565 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5566 if (auth_type != NL80211_AUTHTYPE_SAE)
5567 return -EINVAL;
5568 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5569 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5570 /* need to include at least Auth Transaction and Status Code */
5571 if (sae_data_len < 4)
5572 return -EINVAL;
5573 }
5574
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005575 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5576
Johannes Berg95de8172012-01-20 13:55:25 +01005577 /*
5578 * Since we no longer track auth state, ignore
5579 * requests to only change local state.
5580 */
5581 if (local_state_change)
5582 return 0;
5583
Johannes Berg4c476992010-10-04 21:36:35 +02005584 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5585 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005586 key.p.key, key.p.key_len, key.idx,
5587 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005588}
5589
Johannes Bergc0692b82010-08-27 14:26:53 +03005590static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5591 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005592 struct cfg80211_crypto_settings *settings,
5593 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005594{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005595 memset(settings, 0, sizeof(*settings));
5596
Samuel Ortizb23aa672009-07-01 21:26:54 +02005597 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5598
Johannes Bergc0692b82010-08-27 14:26:53 +03005599 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5600 u16 proto;
5601 proto = nla_get_u16(
5602 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5603 settings->control_port_ethertype = cpu_to_be16(proto);
5604 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5605 proto != ETH_P_PAE)
5606 return -EINVAL;
5607 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5608 settings->control_port_no_encrypt = true;
5609 } else
5610 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5611
Samuel Ortizb23aa672009-07-01 21:26:54 +02005612 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5613 void *data;
5614 int len, i;
5615
5616 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5617 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5618 settings->n_ciphers_pairwise = len / sizeof(u32);
5619
5620 if (len % sizeof(u32))
5621 return -EINVAL;
5622
Johannes Berg3dc27d22009-07-02 21:36:37 +02005623 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005624 return -EINVAL;
5625
5626 memcpy(settings->ciphers_pairwise, data, len);
5627
5628 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005629 if (!cfg80211_supported_cipher_suite(
5630 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005631 settings->ciphers_pairwise[i]))
5632 return -EINVAL;
5633 }
5634
5635 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5636 settings->cipher_group =
5637 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005638 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5639 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005640 return -EINVAL;
5641 }
5642
5643 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5644 settings->wpa_versions =
5645 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5646 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5647 return -EINVAL;
5648 }
5649
5650 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5651 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005652 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005653
5654 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5655 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5656 settings->n_akm_suites = len / sizeof(u32);
5657
5658 if (len % sizeof(u32))
5659 return -EINVAL;
5660
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005661 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5662 return -EINVAL;
5663
Samuel Ortizb23aa672009-07-01 21:26:54 +02005664 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005665 }
5666
5667 return 0;
5668}
5669
Jouni Malinen636a5d32009-03-19 13:39:22 +02005670static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5671{
Johannes Berg4c476992010-10-04 21:36:35 +02005672 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5673 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005674 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005675 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005676 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005677 int err, ssid_len, ie_len = 0;
5678 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005679 u32 flags = 0;
5680 struct ieee80211_ht_cap *ht_capa = NULL;
5681 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005682
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005683 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5684 return -EINVAL;
5685
5686 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005687 !info->attrs[NL80211_ATTR_SSID] ||
5688 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005689 return -EINVAL;
5690
Johannes Berg4c476992010-10-04 21:36:35 +02005691 if (!rdev->ops->assoc)
5692 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005693
Johannes Berg074ac8d2010-09-16 14:58:22 +02005694 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005695 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5696 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005697
Johannes Berg19957bb2009-07-02 17:20:43 +02005698 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005699
Johannes Berg19957bb2009-07-02 17:20:43 +02005700 chan = ieee80211_get_channel(&rdev->wiphy,
5701 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005702 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5703 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005704
Johannes Berg19957bb2009-07-02 17:20:43 +02005705 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5706 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005707
5708 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005709 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5710 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005711 }
5712
Jouni Malinendc6382c2009-05-06 22:09:37 +03005713 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005714 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005715 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005716 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005717 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005718 else if (mfp != NL80211_MFP_NO)
5719 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005720 }
5721
Johannes Berg3e5d7642009-07-07 14:37:26 +02005722 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5723 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5724
Ben Greear7e7c8922011-11-18 11:31:59 -08005725 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5726 flags |= ASSOC_REQ_DISABLE_HT;
5727
5728 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5729 ht_capa_mask =
5730 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5731
5732 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5733 if (!ht_capa_mask)
5734 return -EINVAL;
5735 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5736 }
5737
Johannes Bergc0692b82010-08-27 14:26:53 +03005738 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005739 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005740 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5741 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005742 &crypto, flags, ht_capa,
5743 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005744
Jouni Malinen636a5d32009-03-19 13:39:22 +02005745 return err;
5746}
5747
5748static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5749{
Johannes Berg4c476992010-10-04 21:36:35 +02005750 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5751 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005752 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005753 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005754 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005755 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005756
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005757 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5758 return -EINVAL;
5759
5760 if (!info->attrs[NL80211_ATTR_MAC])
5761 return -EINVAL;
5762
5763 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5764 return -EINVAL;
5765
Johannes Berg4c476992010-10-04 21:36:35 +02005766 if (!rdev->ops->deauth)
5767 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005768
Johannes Berg074ac8d2010-09-16 14:58:22 +02005769 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005770 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5771 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005772
Johannes Berg19957bb2009-07-02 17:20:43 +02005773 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005774
Johannes Berg19957bb2009-07-02 17:20:43 +02005775 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5776 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005777 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005778 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005779 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005780
5781 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005782 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5783 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005784 }
5785
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005786 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5787
Johannes Berg4c476992010-10-04 21:36:35 +02005788 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5789 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005790}
5791
5792static int nl80211_disassociate(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->disassoc)
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_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5833 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005834}
5835
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005836static bool
5837nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5838 int mcast_rate[IEEE80211_NUM_BANDS],
5839 int rateval)
5840{
5841 struct wiphy *wiphy = &rdev->wiphy;
5842 bool found = false;
5843 int band, i;
5844
5845 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5846 struct ieee80211_supported_band *sband;
5847
5848 sband = wiphy->bands[band];
5849 if (!sband)
5850 continue;
5851
5852 for (i = 0; i < sband->n_bitrates; i++) {
5853 if (sband->bitrates[i].bitrate == rateval) {
5854 mcast_rate[band] = i + 1;
5855 found = true;
5856 break;
5857 }
5858 }
5859 }
5860
5861 return found;
5862}
5863
Johannes Berg04a773a2009-04-19 21:24:32 +02005864static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5865{
Johannes Berg4c476992010-10-04 21:36:35 +02005866 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5867 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005868 struct cfg80211_ibss_params ibss;
5869 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005870 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005871 int err;
5872
Johannes Berg8e30bc52009-04-22 17:45:38 +02005873 memset(&ibss, 0, sizeof(ibss));
5874
Johannes Berg04a773a2009-04-19 21:24:32 +02005875 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5876 return -EINVAL;
5877
Johannes Berg683b6d32012-11-08 21:25:48 +01005878 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005879 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5880 return -EINVAL;
5881
Johannes Berg8e30bc52009-04-22 17:45:38 +02005882 ibss.beacon_interval = 100;
5883
5884 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5885 ibss.beacon_interval =
5886 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5887 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5888 return -EINVAL;
5889 }
5890
Johannes Berg4c476992010-10-04 21:36:35 +02005891 if (!rdev->ops->join_ibss)
5892 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005893
Johannes Berg4c476992010-10-04 21:36:35 +02005894 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5895 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005896
Johannes Berg79c97e92009-07-07 03:56:12 +02005897 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005898
Johannes Berg39193492011-09-16 13:45:25 +02005899 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005900 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005901
5902 if (!is_valid_ether_addr(ibss.bssid))
5903 return -EINVAL;
5904 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005905 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5906 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5907
5908 if (info->attrs[NL80211_ATTR_IE]) {
5909 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5910 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5911 }
5912
Johannes Berg683b6d32012-11-08 21:25:48 +01005913 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5914 if (err)
5915 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005916
Johannes Berg683b6d32012-11-08 21:25:48 +01005917 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005918 return -EINVAL;
5919
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005920 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5921 return -EINVAL;
5922 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5923 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005924 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005925
Johannes Berg04a773a2009-04-19 21:24:32 +02005926 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005927 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005928
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005929 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5930 u8 *rates =
5931 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5932 int n_rates =
5933 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5934 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005935 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005936
Johannes Berg34850ab2011-07-18 18:08:35 +02005937 err = ieee80211_get_ratemask(sband, rates, n_rates,
5938 &ibss.basic_rates);
5939 if (err)
5940 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005941 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005942
5943 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5944 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5945 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5946 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005947
Johannes Berg4c476992010-10-04 21:36:35 +02005948 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305949 bool no_ht = false;
5950
Johannes Berg4c476992010-10-04 21:36:35 +02005951 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305952 info->attrs[NL80211_ATTR_KEYS],
5953 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005954 if (IS_ERR(connkeys))
5955 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305956
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005957 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5958 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305959 kfree(connkeys);
5960 return -EINVAL;
5961 }
Johannes Berg4c476992010-10-04 21:36:35 +02005962 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005963
Antonio Quartulli267335d2012-01-31 20:25:47 +01005964 ibss.control_port =
5965 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5966
Johannes Berg4c476992010-10-04 21:36:35 +02005967 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005968 if (err)
5969 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005970 return err;
5971}
5972
5973static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5974{
Johannes Berg4c476992010-10-04 21:36:35 +02005975 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5976 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005977
Johannes Berg4c476992010-10-04 21:36:35 +02005978 if (!rdev->ops->leave_ibss)
5979 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005980
Johannes Berg4c476992010-10-04 21:36:35 +02005981 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5982 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005983
Johannes Berg4c476992010-10-04 21:36:35 +02005984 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005985}
5986
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005987static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5988{
5989 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5990 struct net_device *dev = info->user_ptr[1];
5991 int mcast_rate[IEEE80211_NUM_BANDS];
5992 u32 nla_rate;
5993 int err;
5994
5995 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5996 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5997 return -EOPNOTSUPP;
5998
5999 if (!rdev->ops->set_mcast_rate)
6000 return -EOPNOTSUPP;
6001
6002 memset(mcast_rate, 0, sizeof(mcast_rate));
6003
6004 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6005 return -EINVAL;
6006
6007 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6008 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6009 return -EINVAL;
6010
6011 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6012
6013 return err;
6014}
6015
6016
Johannes Bergaff89a92009-07-01 21:26:51 +02006017#ifdef CONFIG_NL80211_TESTMODE
6018static struct genl_multicast_group nl80211_testmode_mcgrp = {
6019 .name = "testmode",
6020};
6021
6022static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6023{
Johannes Berg4c476992010-10-04 21:36:35 +02006024 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006025 int err;
6026
6027 if (!info->attrs[NL80211_ATTR_TESTDATA])
6028 return -EINVAL;
6029
Johannes Bergaff89a92009-07-01 21:26:51 +02006030 err = -EOPNOTSUPP;
6031 if (rdev->ops->testmode_cmd) {
6032 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006033 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006034 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6035 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6036 rdev->testmode_info = NULL;
6037 }
6038
Johannes Bergaff89a92009-07-01 21:26:51 +02006039 return err;
6040}
6041
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006042static int nl80211_testmode_dump(struct sk_buff *skb,
6043 struct netlink_callback *cb)
6044{
Johannes Berg00918d32011-12-13 17:22:05 +01006045 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006046 int err;
6047 long phy_idx;
6048 void *data = NULL;
6049 int data_len = 0;
6050
6051 if (cb->args[0]) {
6052 /*
6053 * 0 is a valid index, but not valid for args[0],
6054 * so we need to offset by 1.
6055 */
6056 phy_idx = cb->args[0] - 1;
6057 } else {
6058 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6059 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6060 nl80211_policy);
6061 if (err)
6062 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006063
Johannes Berg2bd7e352012-06-15 14:23:16 +02006064 mutex_lock(&cfg80211_mutex);
6065 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6066 nl80211_fam.attrbuf);
6067 if (IS_ERR(rdev)) {
6068 mutex_unlock(&cfg80211_mutex);
6069 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006070 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006071 phy_idx = rdev->wiphy_idx;
6072 rdev = NULL;
6073 mutex_unlock(&cfg80211_mutex);
6074
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006075 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6076 cb->args[1] =
6077 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6078 }
6079
6080 if (cb->args[1]) {
6081 data = nla_data((void *)cb->args[1]);
6082 data_len = nla_len((void *)cb->args[1]);
6083 }
6084
6085 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006086 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6087 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006088 mutex_unlock(&cfg80211_mutex);
6089 return -ENOENT;
6090 }
Johannes Berg00918d32011-12-13 17:22:05 +01006091 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006092 mutex_unlock(&cfg80211_mutex);
6093
Johannes Berg00918d32011-12-13 17:22:05 +01006094 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006095 err = -EOPNOTSUPP;
6096 goto out_err;
6097 }
6098
6099 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006100 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006101 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6102 NL80211_CMD_TESTMODE);
6103 struct nlattr *tmdata;
6104
David S. Miller9360ffd2012-03-29 04:41:26 -04006105 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006106 genlmsg_cancel(skb, hdr);
6107 break;
6108 }
6109
6110 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6111 if (!tmdata) {
6112 genlmsg_cancel(skb, hdr);
6113 break;
6114 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006115 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006116 nla_nest_end(skb, tmdata);
6117
6118 if (err == -ENOBUFS || err == -ENOENT) {
6119 genlmsg_cancel(skb, hdr);
6120 break;
6121 } else if (err) {
6122 genlmsg_cancel(skb, hdr);
6123 goto out_err;
6124 }
6125
6126 genlmsg_end(skb, hdr);
6127 }
6128
6129 err = skb->len;
6130 /* see above */
6131 cb->args[0] = phy_idx + 1;
6132 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006133 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006134 return err;
6135}
6136
Johannes Bergaff89a92009-07-01 21:26:51 +02006137static struct sk_buff *
6138__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006139 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006140{
6141 struct sk_buff *skb;
6142 void *hdr;
6143 struct nlattr *data;
6144
6145 skb = nlmsg_new(approxlen + 100, gfp);
6146 if (!skb)
6147 return NULL;
6148
Eric W. Biederman15e47302012-09-07 20:12:54 +00006149 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006150 if (!hdr) {
6151 kfree_skb(skb);
6152 return NULL;
6153 }
6154
David S. Miller9360ffd2012-03-29 04:41:26 -04006155 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6156 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006157 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6158
6159 ((void **)skb->cb)[0] = rdev;
6160 ((void **)skb->cb)[1] = hdr;
6161 ((void **)skb->cb)[2] = data;
6162
6163 return skb;
6164
6165 nla_put_failure:
6166 kfree_skb(skb);
6167 return NULL;
6168}
6169
6170struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6171 int approxlen)
6172{
6173 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6174
6175 if (WARN_ON(!rdev->testmode_info))
6176 return NULL;
6177
6178 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006179 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006180 rdev->testmode_info->snd_seq,
6181 GFP_KERNEL);
6182}
6183EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6184
6185int cfg80211_testmode_reply(struct sk_buff *skb)
6186{
6187 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6188 void *hdr = ((void **)skb->cb)[1];
6189 struct nlattr *data = ((void **)skb->cb)[2];
6190
6191 if (WARN_ON(!rdev->testmode_info)) {
6192 kfree_skb(skb);
6193 return -EINVAL;
6194 }
6195
6196 nla_nest_end(skb, data);
6197 genlmsg_end(skb, hdr);
6198 return genlmsg_reply(skb, rdev->testmode_info);
6199}
6200EXPORT_SYMBOL(cfg80211_testmode_reply);
6201
6202struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6203 int approxlen, gfp_t gfp)
6204{
6205 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6206
6207 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6208}
6209EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6210
6211void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6212{
6213 void *hdr = ((void **)skb->cb)[1];
6214 struct nlattr *data = ((void **)skb->cb)[2];
6215
6216 nla_nest_end(skb, data);
6217 genlmsg_end(skb, hdr);
6218 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6219}
6220EXPORT_SYMBOL(cfg80211_testmode_event);
6221#endif
6222
Samuel Ortizb23aa672009-07-01 21:26:54 +02006223static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6224{
Johannes Berg4c476992010-10-04 21:36:35 +02006225 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6226 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006227 struct cfg80211_connect_params connect;
6228 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006229 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006230 int err;
6231
6232 memset(&connect, 0, sizeof(connect));
6233
6234 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6235 return -EINVAL;
6236
6237 if (!info->attrs[NL80211_ATTR_SSID] ||
6238 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6239 return -EINVAL;
6240
6241 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6242 connect.auth_type =
6243 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006244 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6245 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006246 return -EINVAL;
6247 } else
6248 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6249
6250 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6251
Johannes Bergc0692b82010-08-27 14:26:53 +03006252 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006253 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006254 if (err)
6255 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006256
Johannes Berg074ac8d2010-09-16 14:58:22 +02006257 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006258 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6259 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006260
Johannes Berg79c97e92009-07-07 03:56:12 +02006261 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006262
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306263 connect.bg_scan_period = -1;
6264 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6265 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6266 connect.bg_scan_period =
6267 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6268 }
6269
Samuel Ortizb23aa672009-07-01 21:26:54 +02006270 if (info->attrs[NL80211_ATTR_MAC])
6271 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6272 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6273 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6274
6275 if (info->attrs[NL80211_ATTR_IE]) {
6276 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6277 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6278 }
6279
Jouni Malinencee00a92013-01-15 17:15:57 +02006280 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6281 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6282 if (connect.mfp != NL80211_MFP_REQUIRED &&
6283 connect.mfp != NL80211_MFP_NO)
6284 return -EINVAL;
6285 } else {
6286 connect.mfp = NL80211_MFP_NO;
6287 }
6288
Samuel Ortizb23aa672009-07-01 21:26:54 +02006289 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6290 connect.channel =
6291 ieee80211_get_channel(wiphy,
6292 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6293 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006294 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6295 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006296 }
6297
Johannes Bergfffd0932009-07-08 14:22:54 +02006298 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6299 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306300 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006301 if (IS_ERR(connkeys))
6302 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006303 }
6304
Ben Greear7e7c8922011-11-18 11:31:59 -08006305 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6306 connect.flags |= ASSOC_REQ_DISABLE_HT;
6307
6308 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6309 memcpy(&connect.ht_capa_mask,
6310 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6311 sizeof(connect.ht_capa_mask));
6312
6313 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006314 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6315 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006316 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006317 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006318 memcpy(&connect.ht_capa,
6319 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6320 sizeof(connect.ht_capa));
6321 }
6322
Johannes Bergfffd0932009-07-08 14:22:54 +02006323 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006324 if (err)
6325 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006326 return err;
6327}
6328
6329static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6330{
Johannes Berg4c476992010-10-04 21:36:35 +02006331 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6332 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006333 u16 reason;
6334
6335 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6336 reason = WLAN_REASON_DEAUTH_LEAVING;
6337 else
6338 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6339
6340 if (reason == 0)
6341 return -EINVAL;
6342
Johannes Berg074ac8d2010-09-16 14:58:22 +02006343 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006344 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6345 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006346
Johannes Berg4c476992010-10-04 21:36:35 +02006347 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006348}
6349
Johannes Berg463d0182009-07-14 00:33:35 +02006350static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6351{
Johannes Berg4c476992010-10-04 21:36:35 +02006352 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006353 struct net *net;
6354 int err;
6355 u32 pid;
6356
6357 if (!info->attrs[NL80211_ATTR_PID])
6358 return -EINVAL;
6359
6360 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6361
Johannes Berg463d0182009-07-14 00:33:35 +02006362 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006363 if (IS_ERR(net))
6364 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006365
6366 err = 0;
6367
6368 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006369 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6370 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006371
Johannes Berg463d0182009-07-14 00:33:35 +02006372 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006373 return err;
6374}
6375
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006376static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6377{
Johannes Berg4c476992010-10-04 21:36:35 +02006378 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006379 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6380 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006381 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006382 struct cfg80211_pmksa pmksa;
6383
6384 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6385
6386 if (!info->attrs[NL80211_ATTR_MAC])
6387 return -EINVAL;
6388
6389 if (!info->attrs[NL80211_ATTR_PMKID])
6390 return -EINVAL;
6391
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006392 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6393 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6394
Johannes Berg074ac8d2010-09-16 14:58:22 +02006395 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006396 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6397 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006398
6399 switch (info->genlhdr->cmd) {
6400 case NL80211_CMD_SET_PMKSA:
6401 rdev_ops = rdev->ops->set_pmksa;
6402 break;
6403 case NL80211_CMD_DEL_PMKSA:
6404 rdev_ops = rdev->ops->del_pmksa;
6405 break;
6406 default:
6407 WARN_ON(1);
6408 break;
6409 }
6410
Johannes Berg4c476992010-10-04 21:36:35 +02006411 if (!rdev_ops)
6412 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006413
Johannes Berg4c476992010-10-04 21:36:35 +02006414 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006415}
6416
6417static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6418{
Johannes Berg4c476992010-10-04 21:36:35 +02006419 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6420 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006421
Johannes Berg074ac8d2010-09-16 14:58:22 +02006422 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006423 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6424 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006425
Johannes Berg4c476992010-10-04 21:36:35 +02006426 if (!rdev->ops->flush_pmksa)
6427 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006428
Hila Gonene35e4d22012-06-27 17:19:42 +03006429 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006430}
6431
Arik Nemtsov109086c2011-09-28 14:12:50 +03006432static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6433{
6434 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6435 struct net_device *dev = info->user_ptr[1];
6436 u8 action_code, dialog_token;
6437 u16 status_code;
6438 u8 *peer;
6439
6440 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6441 !rdev->ops->tdls_mgmt)
6442 return -EOPNOTSUPP;
6443
6444 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6445 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6446 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6447 !info->attrs[NL80211_ATTR_IE] ||
6448 !info->attrs[NL80211_ATTR_MAC])
6449 return -EINVAL;
6450
6451 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6452 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6453 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6454 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6455
Hila Gonene35e4d22012-06-27 17:19:42 +03006456 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6457 dialog_token, status_code,
6458 nla_data(info->attrs[NL80211_ATTR_IE]),
6459 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006460}
6461
6462static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6463{
6464 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6465 struct net_device *dev = info->user_ptr[1];
6466 enum nl80211_tdls_operation operation;
6467 u8 *peer;
6468
6469 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6470 !rdev->ops->tdls_oper)
6471 return -EOPNOTSUPP;
6472
6473 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6474 !info->attrs[NL80211_ATTR_MAC])
6475 return -EINVAL;
6476
6477 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6478 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6479
Hila Gonene35e4d22012-06-27 17:19:42 +03006480 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006481}
6482
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006483static int nl80211_remain_on_channel(struct sk_buff *skb,
6484 struct genl_info *info)
6485{
Johannes Berg4c476992010-10-04 21:36:35 +02006486 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006487 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006488 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006489 struct sk_buff *msg;
6490 void *hdr;
6491 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006492 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006493 int err;
6494
6495 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6496 !info->attrs[NL80211_ATTR_DURATION])
6497 return -EINVAL;
6498
6499 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6500
Johannes Berg7c4ef712011-11-18 15:33:48 +01006501 if (!rdev->ops->remain_on_channel ||
6502 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006503 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006504
Johannes Bergebf348f2012-06-01 12:50:54 +02006505 /*
6506 * We should be on that channel for at least a minimum amount of
6507 * time (10ms) but no longer than the driver supports.
6508 */
6509 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6510 duration > rdev->wiphy.max_remain_on_channel_duration)
6511 return -EINVAL;
6512
Johannes Berg683b6d32012-11-08 21:25:48 +01006513 err = nl80211_parse_chandef(rdev, info, &chandef);
6514 if (err)
6515 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006516
6517 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006518 if (!msg)
6519 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006520
Eric W. Biederman15e47302012-09-07 20:12:54 +00006521 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006522 NL80211_CMD_REMAIN_ON_CHANNEL);
6523
6524 if (IS_ERR(hdr)) {
6525 err = PTR_ERR(hdr);
6526 goto free_msg;
6527 }
6528
Johannes Berg683b6d32012-11-08 21:25:48 +01006529 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6530 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006531
6532 if (err)
6533 goto free_msg;
6534
David S. Miller9360ffd2012-03-29 04:41:26 -04006535 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6536 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006537
6538 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006539
6540 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006541
6542 nla_put_failure:
6543 err = -ENOBUFS;
6544 free_msg:
6545 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006546 return err;
6547}
6548
6549static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6550 struct genl_info *info)
6551{
Johannes Berg4c476992010-10-04 21:36:35 +02006552 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006553 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006554 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006555
6556 if (!info->attrs[NL80211_ATTR_COOKIE])
6557 return -EINVAL;
6558
Johannes Berg4c476992010-10-04 21:36:35 +02006559 if (!rdev->ops->cancel_remain_on_channel)
6560 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006561
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006562 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6563
Hila Gonene35e4d22012-06-27 17:19:42 +03006564 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006565}
6566
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006567static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6568 u8 *rates, u8 rates_len)
6569{
6570 u8 i;
6571 u32 mask = 0;
6572
6573 for (i = 0; i < rates_len; i++) {
6574 int rate = (rates[i] & 0x7f) * 5;
6575 int ridx;
6576 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6577 struct ieee80211_rate *srate =
6578 &sband->bitrates[ridx];
6579 if (rate == srate->bitrate) {
6580 mask |= 1 << ridx;
6581 break;
6582 }
6583 }
6584 if (ridx == sband->n_bitrates)
6585 return 0; /* rate not found */
6586 }
6587
6588 return mask;
6589}
6590
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006591static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6592 u8 *rates, u8 rates_len,
6593 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6594{
6595 u8 i;
6596
6597 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6598
6599 for (i = 0; i < rates_len; i++) {
6600 int ridx, rbit;
6601
6602 ridx = rates[i] / 8;
6603 rbit = BIT(rates[i] % 8);
6604
6605 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006606 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006607 return false;
6608
6609 /* check availability */
6610 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6611 mcs[ridx] |= rbit;
6612 else
6613 return false;
6614 }
6615
6616 return true;
6617}
6618
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006619static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006620 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6621 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006622 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6623 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006624};
6625
6626static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6627 struct genl_info *info)
6628{
6629 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006630 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006631 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006632 int rem, i;
6633 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006634 struct nlattr *tx_rates;
6635 struct ieee80211_supported_band *sband;
6636
6637 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6638 return -EINVAL;
6639
Johannes Berg4c476992010-10-04 21:36:35 +02006640 if (!rdev->ops->set_bitrate_mask)
6641 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006642
6643 memset(&mask, 0, sizeof(mask));
6644 /* Default to all rates enabled */
6645 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6646 sband = rdev->wiphy.bands[i];
6647 mask.control[i].legacy =
6648 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006649 if (sband)
6650 memcpy(mask.control[i].mcs,
6651 sband->ht_cap.mcs.rx_mask,
6652 sizeof(mask.control[i].mcs));
6653 else
6654 memset(mask.control[i].mcs, 0,
6655 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006656 }
6657
6658 /*
6659 * The nested attribute uses enum nl80211_band as the index. This maps
6660 * directly to the enum ieee80211_band values used in cfg80211.
6661 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006662 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006663 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6664 {
6665 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006666 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6667 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006668 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006669 if (sband == NULL)
6670 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006671 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6672 nla_len(tx_rates), nl80211_txattr_policy);
6673 if (tb[NL80211_TXRATE_LEGACY]) {
6674 mask.control[band].legacy = rateset_to_mask(
6675 sband,
6676 nla_data(tb[NL80211_TXRATE_LEGACY]),
6677 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306678 if ((mask.control[band].legacy == 0) &&
6679 nla_len(tb[NL80211_TXRATE_LEGACY]))
6680 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006681 }
6682 if (tb[NL80211_TXRATE_MCS]) {
6683 if (!ht_rateset_to_mask(
6684 sband,
6685 nla_data(tb[NL80211_TXRATE_MCS]),
6686 nla_len(tb[NL80211_TXRATE_MCS]),
6687 mask.control[band].mcs))
6688 return -EINVAL;
6689 }
6690
6691 if (mask.control[band].legacy == 0) {
6692 /* don't allow empty legacy rates if HT
6693 * is not even supported. */
6694 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6695 return -EINVAL;
6696
6697 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6698 if (mask.control[band].mcs[i])
6699 break;
6700
6701 /* legacy and mcs rates may not be both empty */
6702 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006703 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006704 }
6705 }
6706
Hila Gonene35e4d22012-06-27 17:19:42 +03006707 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006708}
6709
Johannes Berg2e161f72010-08-12 15:38:38 +02006710static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006711{
Johannes Berg4c476992010-10-04 21:36:35 +02006712 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006713 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006714 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006715
6716 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6717 return -EINVAL;
6718
Johannes Berg2e161f72010-08-12 15:38:38 +02006719 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6720 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006721
Johannes Berg71bbc992012-06-15 15:30:18 +02006722 switch (wdev->iftype) {
6723 case NL80211_IFTYPE_STATION:
6724 case NL80211_IFTYPE_ADHOC:
6725 case NL80211_IFTYPE_P2P_CLIENT:
6726 case NL80211_IFTYPE_AP:
6727 case NL80211_IFTYPE_AP_VLAN:
6728 case NL80211_IFTYPE_MESH_POINT:
6729 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006730 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006731 break;
6732 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006733 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006734 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006735
6736 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006737 if (!rdev->ops->mgmt_tx)
6738 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006739
Eric W. Biederman15e47302012-09-07 20:12:54 +00006740 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006741 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6742 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006743}
6744
Johannes Berg2e161f72010-08-12 15:38:38 +02006745static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006746{
Johannes Berg4c476992010-10-04 21:36:35 +02006747 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006748 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006749 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006750 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006751 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006752 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006753 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006754 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006755 bool offchan, no_cck, dont_wait_for_ack;
6756
6757 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006758
Johannes Berg683b6d32012-11-08 21:25:48 +01006759 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006760 return -EINVAL;
6761
Johannes Berg4c476992010-10-04 21:36:35 +02006762 if (!rdev->ops->mgmt_tx)
6763 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006764
Johannes Berg71bbc992012-06-15 15:30:18 +02006765 switch (wdev->iftype) {
6766 case NL80211_IFTYPE_STATION:
6767 case NL80211_IFTYPE_ADHOC:
6768 case NL80211_IFTYPE_P2P_CLIENT:
6769 case NL80211_IFTYPE_AP:
6770 case NL80211_IFTYPE_AP_VLAN:
6771 case NL80211_IFTYPE_MESH_POINT:
6772 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006773 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006774 break;
6775 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006776 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006777 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006778
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006779 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006780 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006781 return -EINVAL;
6782 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006783
6784 /*
6785 * We should wait on the channel for at least a minimum amount
6786 * of time (10ms) but no longer than the driver supports.
6787 */
6788 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6789 wait > rdev->wiphy.max_remain_on_channel_duration)
6790 return -EINVAL;
6791
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006792 }
6793
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006794 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6795
Johannes Berg7c4ef712011-11-18 15:33:48 +01006796 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6797 return -EINVAL;
6798
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306799 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6800
Johannes Berg683b6d32012-11-08 21:25:48 +01006801 err = nl80211_parse_chandef(rdev, info, &chandef);
6802 if (err)
6803 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006804
Johannes Berge247bd902011-11-04 11:18:21 +01006805 if (!dont_wait_for_ack) {
6806 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6807 if (!msg)
6808 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006809
Eric W. Biederman15e47302012-09-07 20:12:54 +00006810 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006811 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006812
Johannes Berge247bd902011-11-04 11:18:21 +01006813 if (IS_ERR(hdr)) {
6814 err = PTR_ERR(hdr);
6815 goto free_msg;
6816 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006817 }
Johannes Berge247bd902011-11-04 11:18:21 +01006818
Johannes Berg683b6d32012-11-08 21:25:48 +01006819 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006820 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6821 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006822 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006823 if (err)
6824 goto free_msg;
6825
Johannes Berge247bd902011-11-04 11:18:21 +01006826 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006827 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6828 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006829
Johannes Berge247bd902011-11-04 11:18:21 +01006830 genlmsg_end(msg, hdr);
6831 return genlmsg_reply(msg, info);
6832 }
6833
6834 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006835
6836 nla_put_failure:
6837 err = -ENOBUFS;
6838 free_msg:
6839 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006840 return err;
6841}
6842
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006843static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6844{
6845 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006846 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006847 u64 cookie;
6848
6849 if (!info->attrs[NL80211_ATTR_COOKIE])
6850 return -EINVAL;
6851
6852 if (!rdev->ops->mgmt_tx_cancel_wait)
6853 return -EOPNOTSUPP;
6854
Johannes Berg71bbc992012-06-15 15:30:18 +02006855 switch (wdev->iftype) {
6856 case NL80211_IFTYPE_STATION:
6857 case NL80211_IFTYPE_ADHOC:
6858 case NL80211_IFTYPE_P2P_CLIENT:
6859 case NL80211_IFTYPE_AP:
6860 case NL80211_IFTYPE_AP_VLAN:
6861 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006862 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006863 break;
6864 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006865 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006866 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006867
6868 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6869
Hila Gonene35e4d22012-06-27 17:19:42 +03006870 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006871}
6872
Kalle Valoffb9eb32010-02-17 17:58:10 +02006873static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6874{
Johannes Berg4c476992010-10-04 21:36:35 +02006875 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006876 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006877 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006878 u8 ps_state;
6879 bool state;
6880 int err;
6881
Johannes Berg4c476992010-10-04 21:36:35 +02006882 if (!info->attrs[NL80211_ATTR_PS_STATE])
6883 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006884
6885 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6886
Johannes Berg4c476992010-10-04 21:36:35 +02006887 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6888 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006889
6890 wdev = dev->ieee80211_ptr;
6891
Johannes Berg4c476992010-10-04 21:36:35 +02006892 if (!rdev->ops->set_power_mgmt)
6893 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006894
6895 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6896
6897 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006898 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006899
Hila Gonene35e4d22012-06-27 17:19:42 +03006900 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006901 if (!err)
6902 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006903 return err;
6904}
6905
6906static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6907{
Johannes Berg4c476992010-10-04 21:36:35 +02006908 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006909 enum nl80211_ps_state ps_state;
6910 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006911 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006912 struct sk_buff *msg;
6913 void *hdr;
6914 int err;
6915
Kalle Valoffb9eb32010-02-17 17:58:10 +02006916 wdev = dev->ieee80211_ptr;
6917
Johannes Berg4c476992010-10-04 21:36:35 +02006918 if (!rdev->ops->set_power_mgmt)
6919 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006920
6921 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006922 if (!msg)
6923 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006924
Eric W. Biederman15e47302012-09-07 20:12:54 +00006925 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006926 NL80211_CMD_GET_POWER_SAVE);
6927 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006928 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006929 goto free_msg;
6930 }
6931
6932 if (wdev->ps)
6933 ps_state = NL80211_PS_ENABLED;
6934 else
6935 ps_state = NL80211_PS_DISABLED;
6936
David S. Miller9360ffd2012-03-29 04:41:26 -04006937 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6938 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006939
6940 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006941 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006942
Johannes Berg4c476992010-10-04 21:36:35 +02006943 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006944 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006945 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006946 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006947 return err;
6948}
6949
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006950static struct nla_policy
6951nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6952 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6953 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6954 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006955 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6956 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6957 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006958};
6959
Thomas Pedersen84f10702012-07-12 16:17:33 -07006960static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006961 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006962{
6963 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6964 struct wireless_dev *wdev;
6965 struct net_device *dev = info->user_ptr[1];
6966
Johannes Bergd9d8b012012-11-26 12:51:52 +01006967 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006968 return -EINVAL;
6969
6970 wdev = dev->ieee80211_ptr;
6971
6972 if (!rdev->ops->set_cqm_txe_config)
6973 return -EOPNOTSUPP;
6974
6975 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6976 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6977 return -EOPNOTSUPP;
6978
Hila Gonene35e4d22012-06-27 17:19:42 +03006979 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006980}
6981
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006982static int nl80211_set_cqm_rssi(struct genl_info *info,
6983 s32 threshold, u32 hysteresis)
6984{
Johannes Berg4c476992010-10-04 21:36:35 +02006985 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006986 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006987 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006988
6989 if (threshold > 0)
6990 return -EINVAL;
6991
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006992 wdev = dev->ieee80211_ptr;
6993
Johannes Berg4c476992010-10-04 21:36:35 +02006994 if (!rdev->ops->set_cqm_rssi_config)
6995 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006996
Johannes Berg074ac8d2010-09-16 14:58:22 +02006997 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006998 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6999 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007000
Hila Gonene35e4d22012-06-27 17:19:42 +03007001 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007002}
7003
7004static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7005{
7006 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7007 struct nlattr *cqm;
7008 int err;
7009
7010 cqm = info->attrs[NL80211_ATTR_CQM];
7011 if (!cqm) {
7012 err = -EINVAL;
7013 goto out;
7014 }
7015
7016 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7017 nl80211_attr_cqm_policy);
7018 if (err)
7019 goto out;
7020
7021 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7022 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7023 s32 threshold;
7024 u32 hysteresis;
7025 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7026 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7027 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007028 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7029 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7030 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7031 u32 rate, pkts, intvl;
7032 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7033 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7034 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7035 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007036 } else
7037 err = -EINVAL;
7038
7039out:
7040 return err;
7041}
7042
Johannes Berg29cbe682010-12-03 09:20:44 +01007043static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7044{
7045 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7046 struct net_device *dev = info->user_ptr[1];
7047 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007048 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007049 int err;
7050
7051 /* start with default */
7052 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007053 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007054
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007055 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007056 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007057 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007058 if (err)
7059 return err;
7060 }
7061
7062 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7063 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7064 return -EINVAL;
7065
Javier Cardonac80d5452010-12-16 17:37:49 -08007066 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7067 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7068
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007069 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7070 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7071 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7072 return -EINVAL;
7073
Marco Porsch9bdbf042013-01-07 16:04:51 +01007074 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7075 setup.beacon_interval =
7076 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7077 if (setup.beacon_interval < 10 ||
7078 setup.beacon_interval > 10000)
7079 return -EINVAL;
7080 }
7081
7082 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7083 setup.dtim_period =
7084 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7085 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7086 return -EINVAL;
7087 }
7088
Javier Cardonac80d5452010-12-16 17:37:49 -08007089 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7090 /* parse additional setup parameters if given */
7091 err = nl80211_parse_mesh_setup(info, &setup);
7092 if (err)
7093 return err;
7094 }
7095
Johannes Bergcc1d2802012-05-16 23:50:20 +02007096 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007097 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7098 if (err)
7099 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007100 } else {
7101 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007102 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007103 }
7104
Javier Cardonac80d5452010-12-16 17:37:49 -08007105 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007106}
7107
7108static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7109{
7110 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7111 struct net_device *dev = info->user_ptr[1];
7112
7113 return cfg80211_leave_mesh(rdev, dev);
7114}
7115
Johannes Bergdfb89c52012-06-27 09:23:48 +02007116#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007117static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7118 struct cfg80211_registered_device *rdev)
7119{
7120 struct nlattr *nl_pats, *nl_pat;
7121 int i, pat_len;
7122
7123 if (!rdev->wowlan->n_patterns)
7124 return 0;
7125
7126 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7127 if (!nl_pats)
7128 return -ENOBUFS;
7129
7130 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7131 nl_pat = nla_nest_start(msg, i + 1);
7132 if (!nl_pat)
7133 return -ENOBUFS;
7134 pat_len = rdev->wowlan->patterns[i].pattern_len;
7135 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7136 DIV_ROUND_UP(pat_len, 8),
7137 rdev->wowlan->patterns[i].mask) ||
7138 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7139 pat_len, rdev->wowlan->patterns[i].pattern) ||
7140 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7141 rdev->wowlan->patterns[i].pkt_offset))
7142 return -ENOBUFS;
7143 nla_nest_end(msg, nl_pat);
7144 }
7145 nla_nest_end(msg, nl_pats);
7146
7147 return 0;
7148}
7149
Johannes Berg2a0e0472013-01-23 22:57:40 +01007150static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7151 struct cfg80211_wowlan_tcp *tcp)
7152{
7153 struct nlattr *nl_tcp;
7154
7155 if (!tcp)
7156 return 0;
7157
7158 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7159 if (!nl_tcp)
7160 return -ENOBUFS;
7161
7162 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7163 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7164 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7165 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7166 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7167 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7168 tcp->payload_len, tcp->payload) ||
7169 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7170 tcp->data_interval) ||
7171 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7172 tcp->wake_len, tcp->wake_data) ||
7173 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7174 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7175 return -ENOBUFS;
7176
7177 if (tcp->payload_seq.len &&
7178 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7179 sizeof(tcp->payload_seq), &tcp->payload_seq))
7180 return -ENOBUFS;
7181
7182 if (tcp->payload_tok.len &&
7183 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7184 sizeof(tcp->payload_tok) + tcp->tokens_size,
7185 &tcp->payload_tok))
7186 return -ENOBUFS;
7187
7188 return 0;
7189}
7190
Johannes Bergff1b6e62011-05-04 15:37:28 +02007191static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7192{
7193 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7194 struct sk_buff *msg;
7195 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007196 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007197
Johannes Berg2a0e0472013-01-23 22:57:40 +01007198 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7199 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007200 return -EOPNOTSUPP;
7201
Johannes Berg2a0e0472013-01-23 22:57:40 +01007202 if (rdev->wowlan && rdev->wowlan->tcp) {
7203 /* adjust size to have room for all the data */
7204 size += rdev->wowlan->tcp->tokens_size +
7205 rdev->wowlan->tcp->payload_len +
7206 rdev->wowlan->tcp->wake_len +
7207 rdev->wowlan->tcp->wake_len / 8;
7208 }
7209
7210 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007211 if (!msg)
7212 return -ENOMEM;
7213
Eric W. Biederman15e47302012-09-07 20:12:54 +00007214 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007215 NL80211_CMD_GET_WOWLAN);
7216 if (!hdr)
7217 goto nla_put_failure;
7218
7219 if (rdev->wowlan) {
7220 struct nlattr *nl_wowlan;
7221
7222 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7223 if (!nl_wowlan)
7224 goto nla_put_failure;
7225
David S. Miller9360ffd2012-03-29 04:41:26 -04007226 if ((rdev->wowlan->any &&
7227 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7228 (rdev->wowlan->disconnect &&
7229 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7230 (rdev->wowlan->magic_pkt &&
7231 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7232 (rdev->wowlan->gtk_rekey_failure &&
7233 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7234 (rdev->wowlan->eap_identity_req &&
7235 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7236 (rdev->wowlan->four_way_handshake &&
7237 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7238 (rdev->wowlan->rfkill_release &&
7239 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7240 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007241
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007242 if (nl80211_send_wowlan_patterns(msg, rdev))
7243 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007244
7245 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7246 goto nla_put_failure;
7247
Johannes Bergff1b6e62011-05-04 15:37:28 +02007248 nla_nest_end(msg, nl_wowlan);
7249 }
7250
7251 genlmsg_end(msg, hdr);
7252 return genlmsg_reply(msg, info);
7253
7254nla_put_failure:
7255 nlmsg_free(msg);
7256 return -ENOBUFS;
7257}
7258
Johannes Berg2a0e0472013-01-23 22:57:40 +01007259static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7260 struct nlattr *attr,
7261 struct cfg80211_wowlan *trig)
7262{
7263 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7264 struct cfg80211_wowlan_tcp *cfg;
7265 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7266 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7267 u32 size;
7268 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7269 int err, port;
7270
7271 if (!rdev->wiphy.wowlan.tcp)
7272 return -EINVAL;
7273
7274 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7275 nla_data(attr), nla_len(attr),
7276 nl80211_wowlan_tcp_policy);
7277 if (err)
7278 return err;
7279
7280 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7281 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7282 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7283 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7284 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7285 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7286 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7287 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7288 return -EINVAL;
7289
7290 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7291 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7292 return -EINVAL;
7293
7294 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7295 rdev->wiphy.wowlan.tcp->data_interval_max)
7296 return -EINVAL;
7297
7298 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7299 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7300 return -EINVAL;
7301
7302 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7303 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7304 return -EINVAL;
7305
7306 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7307 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7308
7309 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7310 tokens_size = tokln - sizeof(*tok);
7311
7312 if (!tok->len || tokens_size % tok->len)
7313 return -EINVAL;
7314 if (!rdev->wiphy.wowlan.tcp->tok)
7315 return -EINVAL;
7316 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7317 return -EINVAL;
7318 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7319 return -EINVAL;
7320 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7321 return -EINVAL;
7322 if (tok->offset + tok->len > data_size)
7323 return -EINVAL;
7324 }
7325
7326 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7327 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7328 if (!rdev->wiphy.wowlan.tcp->seq)
7329 return -EINVAL;
7330 if (seq->len == 0 || seq->len > 4)
7331 return -EINVAL;
7332 if (seq->len + seq->offset > data_size)
7333 return -EINVAL;
7334 }
7335
7336 size = sizeof(*cfg);
7337 size += data_size;
7338 size += wake_size + wake_mask_size;
7339 size += tokens_size;
7340
7341 cfg = kzalloc(size, GFP_KERNEL);
7342 if (!cfg)
7343 return -ENOMEM;
7344 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7345 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7346 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7347 ETH_ALEN);
7348 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7349 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7350 else
7351 port = 0;
7352#ifdef CONFIG_INET
7353 /* allocate a socket and port for it and use it */
7354 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7355 IPPROTO_TCP, &cfg->sock, 1);
7356 if (err) {
7357 kfree(cfg);
7358 return err;
7359 }
7360 if (inet_csk_get_port(cfg->sock->sk, port)) {
7361 sock_release(cfg->sock);
7362 kfree(cfg);
7363 return -EADDRINUSE;
7364 }
7365 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7366#else
7367 if (!port) {
7368 kfree(cfg);
7369 return -EINVAL;
7370 }
7371 cfg->src_port = port;
7372#endif
7373
7374 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7375 cfg->payload_len = data_size;
7376 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7377 memcpy((void *)cfg->payload,
7378 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7379 data_size);
7380 if (seq)
7381 cfg->payload_seq = *seq;
7382 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7383 cfg->wake_len = wake_size;
7384 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7385 memcpy((void *)cfg->wake_data,
7386 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7387 wake_size);
7388 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7389 data_size + wake_size;
7390 memcpy((void *)cfg->wake_mask,
7391 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7392 wake_mask_size);
7393 if (tok) {
7394 cfg->tokens_size = tokens_size;
7395 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7396 }
7397
7398 trig->tcp = cfg;
7399
7400 return 0;
7401}
7402
Johannes Bergff1b6e62011-05-04 15:37:28 +02007403static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7404{
7405 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7406 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007407 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007408 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007409 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7410 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007411 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007412
Johannes Berg2a0e0472013-01-23 22:57:40 +01007413 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7414 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007415 return -EOPNOTSUPP;
7416
Johannes Bergae33bd82012-07-12 16:25:02 +02007417 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7418 cfg80211_rdev_free_wowlan(rdev);
7419 rdev->wowlan = NULL;
7420 goto set_wakeup;
7421 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007422
7423 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7424 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7425 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7426 nl80211_wowlan_policy);
7427 if (err)
7428 return err;
7429
7430 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7431 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7432 return -EINVAL;
7433 new_triggers.any = true;
7434 }
7435
7436 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7437 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7438 return -EINVAL;
7439 new_triggers.disconnect = true;
7440 }
7441
7442 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7443 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7444 return -EINVAL;
7445 new_triggers.magic_pkt = true;
7446 }
7447
Johannes Berg77dbbb12011-07-13 10:48:55 +02007448 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7449 return -EINVAL;
7450
7451 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7452 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7453 return -EINVAL;
7454 new_triggers.gtk_rekey_failure = true;
7455 }
7456
7457 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7458 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7459 return -EINVAL;
7460 new_triggers.eap_identity_req = true;
7461 }
7462
7463 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7464 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7465 return -EINVAL;
7466 new_triggers.four_way_handshake = true;
7467 }
7468
7469 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7470 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7471 return -EINVAL;
7472 new_triggers.rfkill_release = true;
7473 }
7474
Johannes Bergff1b6e62011-05-04 15:37:28 +02007475 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7476 struct nlattr *pat;
7477 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007478 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007479 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7480
7481 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7482 rem)
7483 n_patterns++;
7484 if (n_patterns > wowlan->n_patterns)
7485 return -EINVAL;
7486
7487 new_triggers.patterns = kcalloc(n_patterns,
7488 sizeof(new_triggers.patterns[0]),
7489 GFP_KERNEL);
7490 if (!new_triggers.patterns)
7491 return -ENOMEM;
7492
7493 new_triggers.n_patterns = n_patterns;
7494 i = 0;
7495
7496 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7497 rem) {
7498 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7499 nla_data(pat), nla_len(pat), NULL);
7500 err = -EINVAL;
7501 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7502 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7503 goto error;
7504 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7505 mask_len = DIV_ROUND_UP(pat_len, 8);
7506 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7507 mask_len)
7508 goto error;
7509 if (pat_len > wowlan->pattern_max_len ||
7510 pat_len < wowlan->pattern_min_len)
7511 goto error;
7512
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007513 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7514 pkt_offset = 0;
7515 else
7516 pkt_offset = nla_get_u32(
7517 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7518 if (pkt_offset > wowlan->max_pkt_offset)
7519 goto error;
7520 new_triggers.patterns[i].pkt_offset = pkt_offset;
7521
Johannes Bergff1b6e62011-05-04 15:37:28 +02007522 new_triggers.patterns[i].mask =
7523 kmalloc(mask_len + pat_len, GFP_KERNEL);
7524 if (!new_triggers.patterns[i].mask) {
7525 err = -ENOMEM;
7526 goto error;
7527 }
7528 new_triggers.patterns[i].pattern =
7529 new_triggers.patterns[i].mask + mask_len;
7530 memcpy(new_triggers.patterns[i].mask,
7531 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7532 mask_len);
7533 new_triggers.patterns[i].pattern_len = pat_len;
7534 memcpy(new_triggers.patterns[i].pattern,
7535 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7536 pat_len);
7537 i++;
7538 }
7539 }
7540
Johannes Berg2a0e0472013-01-23 22:57:40 +01007541 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7542 err = nl80211_parse_wowlan_tcp(
7543 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7544 &new_triggers);
7545 if (err)
7546 goto error;
7547 }
7548
Johannes Bergae33bd82012-07-12 16:25:02 +02007549 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7550 if (!ntrig) {
7551 err = -ENOMEM;
7552 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007553 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007554 cfg80211_rdev_free_wowlan(rdev);
7555 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007556
Johannes Bergae33bd82012-07-12 16:25:02 +02007557 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007558 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007559 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007560
Johannes Bergff1b6e62011-05-04 15:37:28 +02007561 return 0;
7562 error:
7563 for (i = 0; i < new_triggers.n_patterns; i++)
7564 kfree(new_triggers.patterns[i].mask);
7565 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007566 if (new_triggers.tcp && new_triggers.tcp->sock)
7567 sock_release(new_triggers.tcp->sock);
7568 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007569 return err;
7570}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007571#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007572
Johannes Berge5497d72011-07-05 16:35:40 +02007573static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7574{
7575 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7576 struct net_device *dev = info->user_ptr[1];
7577 struct wireless_dev *wdev = dev->ieee80211_ptr;
7578 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7579 struct cfg80211_gtk_rekey_data rekey_data;
7580 int err;
7581
7582 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7583 return -EINVAL;
7584
7585 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7586 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7587 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7588 nl80211_rekey_policy);
7589 if (err)
7590 return err;
7591
7592 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7593 return -ERANGE;
7594 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7595 return -ERANGE;
7596 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7597 return -ERANGE;
7598
7599 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7600 NL80211_KEK_LEN);
7601 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7602 NL80211_KCK_LEN);
7603 memcpy(rekey_data.replay_ctr,
7604 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7605 NL80211_REPLAY_CTR_LEN);
7606
7607 wdev_lock(wdev);
7608 if (!wdev->current_bss) {
7609 err = -ENOTCONN;
7610 goto out;
7611 }
7612
7613 if (!rdev->ops->set_rekey_data) {
7614 err = -EOPNOTSUPP;
7615 goto out;
7616 }
7617
Hila Gonene35e4d22012-06-27 17:19:42 +03007618 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007619 out:
7620 wdev_unlock(wdev);
7621 return err;
7622}
7623
Johannes Berg28946da2011-11-04 11:18:12 +01007624static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7625 struct genl_info *info)
7626{
7627 struct net_device *dev = info->user_ptr[1];
7628 struct wireless_dev *wdev = dev->ieee80211_ptr;
7629
7630 if (wdev->iftype != NL80211_IFTYPE_AP &&
7631 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7632 return -EINVAL;
7633
Eric W. Biederman15e47302012-09-07 20:12:54 +00007634 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007635 return -EBUSY;
7636
Eric W. Biederman15e47302012-09-07 20:12:54 +00007637 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007638 return 0;
7639}
7640
Johannes Berg7f6cf312011-11-04 11:18:15 +01007641static int nl80211_probe_client(struct sk_buff *skb,
7642 struct genl_info *info)
7643{
7644 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7645 struct net_device *dev = info->user_ptr[1];
7646 struct wireless_dev *wdev = dev->ieee80211_ptr;
7647 struct sk_buff *msg;
7648 void *hdr;
7649 const u8 *addr;
7650 u64 cookie;
7651 int err;
7652
7653 if (wdev->iftype != NL80211_IFTYPE_AP &&
7654 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7655 return -EOPNOTSUPP;
7656
7657 if (!info->attrs[NL80211_ATTR_MAC])
7658 return -EINVAL;
7659
7660 if (!rdev->ops->probe_client)
7661 return -EOPNOTSUPP;
7662
7663 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7664 if (!msg)
7665 return -ENOMEM;
7666
Eric W. Biederman15e47302012-09-07 20:12:54 +00007667 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007668 NL80211_CMD_PROBE_CLIENT);
7669
7670 if (IS_ERR(hdr)) {
7671 err = PTR_ERR(hdr);
7672 goto free_msg;
7673 }
7674
7675 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7676
Hila Gonene35e4d22012-06-27 17:19:42 +03007677 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007678 if (err)
7679 goto free_msg;
7680
David S. Miller9360ffd2012-03-29 04:41:26 -04007681 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7682 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007683
7684 genlmsg_end(msg, hdr);
7685
7686 return genlmsg_reply(msg, info);
7687
7688 nla_put_failure:
7689 err = -ENOBUFS;
7690 free_msg:
7691 nlmsg_free(msg);
7692 return err;
7693}
7694
Johannes Berg5e7602302011-11-04 11:18:17 +01007695static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7696{
7697 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007698 struct cfg80211_beacon_registration *reg, *nreg;
7699 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01007700
7701 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7702 return -EOPNOTSUPP;
7703
Ben Greear37c73b52012-10-26 14:49:25 -07007704 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7705 if (!nreg)
7706 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01007707
Ben Greear37c73b52012-10-26 14:49:25 -07007708 /* First, check if already registered. */
7709 spin_lock_bh(&rdev->beacon_registrations_lock);
7710 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7711 if (reg->nlportid == info->snd_portid) {
7712 rv = -EALREADY;
7713 goto out_err;
7714 }
7715 }
7716 /* Add it to the list */
7717 nreg->nlportid = info->snd_portid;
7718 list_add(&nreg->list, &rdev->beacon_registrations);
7719
7720 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01007721
7722 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007723out_err:
7724 spin_unlock_bh(&rdev->beacon_registrations_lock);
7725 kfree(nreg);
7726 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01007727}
7728
Johannes Berg98104fde2012-06-16 00:19:54 +02007729static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7730{
7731 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7732 struct wireless_dev *wdev = info->user_ptr[1];
7733 int err;
7734
7735 if (!rdev->ops->start_p2p_device)
7736 return -EOPNOTSUPP;
7737
7738 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7739 return -EOPNOTSUPP;
7740
7741 if (wdev->p2p_started)
7742 return 0;
7743
7744 mutex_lock(&rdev->devlist_mtx);
7745 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7746 mutex_unlock(&rdev->devlist_mtx);
7747 if (err)
7748 return err;
7749
Johannes Bergeeb126e2012-10-23 15:16:50 +02007750 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007751 if (err)
7752 return err;
7753
7754 wdev->p2p_started = true;
7755 mutex_lock(&rdev->devlist_mtx);
7756 rdev->opencount++;
7757 mutex_unlock(&rdev->devlist_mtx);
7758
7759 return 0;
7760}
7761
7762static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7763{
7764 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7765 struct wireless_dev *wdev = info->user_ptr[1];
7766
7767 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7768 return -EOPNOTSUPP;
7769
7770 if (!rdev->ops->stop_p2p_device)
7771 return -EOPNOTSUPP;
7772
7773 if (!wdev->p2p_started)
7774 return 0;
7775
Johannes Bergeeb126e2012-10-23 15:16:50 +02007776 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007777 wdev->p2p_started = false;
7778
7779 mutex_lock(&rdev->devlist_mtx);
7780 rdev->opencount--;
7781 mutex_unlock(&rdev->devlist_mtx);
7782
7783 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7784 rdev->scan_req->aborted = true;
7785 ___cfg80211_scan_done(rdev, true);
7786 }
7787
7788 return 0;
7789}
7790
Johannes Berg4c476992010-10-04 21:36:35 +02007791#define NL80211_FLAG_NEED_WIPHY 0x01
7792#define NL80211_FLAG_NEED_NETDEV 0x02
7793#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007794#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7795#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7796 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007797#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007798/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007799#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7800 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007801
7802static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7803 struct genl_info *info)
7804{
7805 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007806 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007807 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007808 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7809
7810 if (rtnl)
7811 rtnl_lock();
7812
7813 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007814 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007815 if (IS_ERR(rdev)) {
7816 if (rtnl)
7817 rtnl_unlock();
7818 return PTR_ERR(rdev);
7819 }
7820 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007821 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7822 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007823 mutex_lock(&cfg80211_mutex);
7824 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7825 info->attrs);
7826 if (IS_ERR(wdev)) {
7827 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007828 if (rtnl)
7829 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007830 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007831 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007832
Johannes Berg89a54e42012-06-15 14:33:17 +02007833 dev = wdev->netdev;
7834 rdev = wiphy_to_dev(wdev->wiphy);
7835
Johannes Berg1bf614e2012-06-15 15:23:36 +02007836 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7837 if (!dev) {
7838 mutex_unlock(&cfg80211_mutex);
7839 if (rtnl)
7840 rtnl_unlock();
7841 return -EINVAL;
7842 }
7843
7844 info->user_ptr[1] = dev;
7845 } else {
7846 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007847 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007848
Johannes Berg1bf614e2012-06-15 15:23:36 +02007849 if (dev) {
7850 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7851 !netif_running(dev)) {
7852 mutex_unlock(&cfg80211_mutex);
7853 if (rtnl)
7854 rtnl_unlock();
7855 return -ENETDOWN;
7856 }
7857
7858 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007859 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7860 if (!wdev->p2p_started) {
7861 mutex_unlock(&cfg80211_mutex);
7862 if (rtnl)
7863 rtnl_unlock();
7864 return -ENETDOWN;
7865 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007866 }
7867
Johannes Berg89a54e42012-06-15 14:33:17 +02007868 cfg80211_lock_rdev(rdev);
7869
7870 mutex_unlock(&cfg80211_mutex);
7871
Johannes Berg4c476992010-10-04 21:36:35 +02007872 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007873 }
7874
7875 return 0;
7876}
7877
7878static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7879 struct genl_info *info)
7880{
7881 if (info->user_ptr[0])
7882 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007883 if (info->user_ptr[1]) {
7884 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7885 struct wireless_dev *wdev = info->user_ptr[1];
7886
7887 if (wdev->netdev)
7888 dev_put(wdev->netdev);
7889 } else {
7890 dev_put(info->user_ptr[1]);
7891 }
7892 }
Johannes Berg4c476992010-10-04 21:36:35 +02007893 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7894 rtnl_unlock();
7895}
7896
Johannes Berg55682962007-09-20 13:09:35 -04007897static struct genl_ops nl80211_ops[] = {
7898 {
7899 .cmd = NL80211_CMD_GET_WIPHY,
7900 .doit = nl80211_get_wiphy,
7901 .dumpit = nl80211_dump_wiphy,
7902 .policy = nl80211_policy,
7903 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007904 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007905 },
7906 {
7907 .cmd = NL80211_CMD_SET_WIPHY,
7908 .doit = nl80211_set_wiphy,
7909 .policy = nl80211_policy,
7910 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007911 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007912 },
7913 {
7914 .cmd = NL80211_CMD_GET_INTERFACE,
7915 .doit = nl80211_get_interface,
7916 .dumpit = nl80211_dump_interface,
7917 .policy = nl80211_policy,
7918 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007919 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007920 },
7921 {
7922 .cmd = NL80211_CMD_SET_INTERFACE,
7923 .doit = nl80211_set_interface,
7924 .policy = nl80211_policy,
7925 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007926 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7927 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007928 },
7929 {
7930 .cmd = NL80211_CMD_NEW_INTERFACE,
7931 .doit = nl80211_new_interface,
7932 .policy = nl80211_policy,
7933 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007934 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7935 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007936 },
7937 {
7938 .cmd = NL80211_CMD_DEL_INTERFACE,
7939 .doit = nl80211_del_interface,
7940 .policy = nl80211_policy,
7941 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007942 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007943 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007944 },
Johannes Berg41ade002007-12-19 02:03:29 +01007945 {
7946 .cmd = NL80211_CMD_GET_KEY,
7947 .doit = nl80211_get_key,
7948 .policy = nl80211_policy,
7949 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007950 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007951 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007952 },
7953 {
7954 .cmd = NL80211_CMD_SET_KEY,
7955 .doit = nl80211_set_key,
7956 .policy = nl80211_policy,
7957 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007958 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007959 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007960 },
7961 {
7962 .cmd = NL80211_CMD_NEW_KEY,
7963 .doit = nl80211_new_key,
7964 .policy = nl80211_policy,
7965 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007966 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007967 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007968 },
7969 {
7970 .cmd = NL80211_CMD_DEL_KEY,
7971 .doit = nl80211_del_key,
7972 .policy = nl80211_policy,
7973 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007974 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007975 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007976 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007977 {
7978 .cmd = NL80211_CMD_SET_BEACON,
7979 .policy = nl80211_policy,
7980 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007981 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007982 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007983 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007984 },
7985 {
Johannes Berg88600202012-02-13 15:17:18 +01007986 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007987 .policy = nl80211_policy,
7988 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007989 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007990 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007991 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007992 },
7993 {
Johannes Berg88600202012-02-13 15:17:18 +01007994 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007995 .policy = nl80211_policy,
7996 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007997 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007998 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007999 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008000 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008001 {
8002 .cmd = NL80211_CMD_GET_STATION,
8003 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008004 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008005 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008006 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8007 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008008 },
8009 {
8010 .cmd = NL80211_CMD_SET_STATION,
8011 .doit = nl80211_set_station,
8012 .policy = nl80211_policy,
8013 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008014 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008015 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008016 },
8017 {
8018 .cmd = NL80211_CMD_NEW_STATION,
8019 .doit = nl80211_new_station,
8020 .policy = nl80211_policy,
8021 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008022 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008023 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008024 },
8025 {
8026 .cmd = NL80211_CMD_DEL_STATION,
8027 .doit = nl80211_del_station,
8028 .policy = nl80211_policy,
8029 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008030 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008031 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008032 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008033 {
8034 .cmd = NL80211_CMD_GET_MPATH,
8035 .doit = nl80211_get_mpath,
8036 .dumpit = nl80211_dump_mpath,
8037 .policy = nl80211_policy,
8038 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008039 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008040 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008041 },
8042 {
8043 .cmd = NL80211_CMD_SET_MPATH,
8044 .doit = nl80211_set_mpath,
8045 .policy = nl80211_policy,
8046 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008047 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008048 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008049 },
8050 {
8051 .cmd = NL80211_CMD_NEW_MPATH,
8052 .doit = nl80211_new_mpath,
8053 .policy = nl80211_policy,
8054 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008055 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008056 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008057 },
8058 {
8059 .cmd = NL80211_CMD_DEL_MPATH,
8060 .doit = nl80211_del_mpath,
8061 .policy = nl80211_policy,
8062 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008063 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008064 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008065 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008066 {
8067 .cmd = NL80211_CMD_SET_BSS,
8068 .doit = nl80211_set_bss,
8069 .policy = nl80211_policy,
8070 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008071 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008072 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008073 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008074 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008075 .cmd = NL80211_CMD_GET_REG,
8076 .doit = nl80211_get_reg,
8077 .policy = nl80211_policy,
8078 /* can be retrieved by unprivileged users */
8079 },
8080 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008081 .cmd = NL80211_CMD_SET_REG,
8082 .doit = nl80211_set_reg,
8083 .policy = nl80211_policy,
8084 .flags = GENL_ADMIN_PERM,
8085 },
8086 {
8087 .cmd = NL80211_CMD_REQ_SET_REG,
8088 .doit = nl80211_req_set_reg,
8089 .policy = nl80211_policy,
8090 .flags = GENL_ADMIN_PERM,
8091 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008092 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008093 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8094 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008095 .policy = nl80211_policy,
8096 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008097 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008098 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008099 },
8100 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008101 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8102 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008103 .policy = nl80211_policy,
8104 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008105 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008106 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008107 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008108 {
Johannes Berg2a519312009-02-10 21:25:55 +01008109 .cmd = NL80211_CMD_TRIGGER_SCAN,
8110 .doit = nl80211_trigger_scan,
8111 .policy = nl80211_policy,
8112 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008113 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008114 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008115 },
8116 {
8117 .cmd = NL80211_CMD_GET_SCAN,
8118 .policy = nl80211_policy,
8119 .dumpit = nl80211_dump_scan,
8120 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008121 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008122 .cmd = NL80211_CMD_START_SCHED_SCAN,
8123 .doit = nl80211_start_sched_scan,
8124 .policy = nl80211_policy,
8125 .flags = GENL_ADMIN_PERM,
8126 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8127 NL80211_FLAG_NEED_RTNL,
8128 },
8129 {
8130 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8131 .doit = nl80211_stop_sched_scan,
8132 .policy = nl80211_policy,
8133 .flags = GENL_ADMIN_PERM,
8134 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8135 NL80211_FLAG_NEED_RTNL,
8136 },
8137 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008138 .cmd = NL80211_CMD_AUTHENTICATE,
8139 .doit = nl80211_authenticate,
8140 .policy = nl80211_policy,
8141 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008142 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008143 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008144 },
8145 {
8146 .cmd = NL80211_CMD_ASSOCIATE,
8147 .doit = nl80211_associate,
8148 .policy = nl80211_policy,
8149 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008150 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008151 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008152 },
8153 {
8154 .cmd = NL80211_CMD_DEAUTHENTICATE,
8155 .doit = nl80211_deauthenticate,
8156 .policy = nl80211_policy,
8157 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008158 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008159 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008160 },
8161 {
8162 .cmd = NL80211_CMD_DISASSOCIATE,
8163 .doit = nl80211_disassociate,
8164 .policy = nl80211_policy,
8165 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008166 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008167 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008168 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008169 {
8170 .cmd = NL80211_CMD_JOIN_IBSS,
8171 .doit = nl80211_join_ibss,
8172 .policy = nl80211_policy,
8173 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008174 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008175 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008176 },
8177 {
8178 .cmd = NL80211_CMD_LEAVE_IBSS,
8179 .doit = nl80211_leave_ibss,
8180 .policy = nl80211_policy,
8181 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008182 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008183 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008184 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008185#ifdef CONFIG_NL80211_TESTMODE
8186 {
8187 .cmd = NL80211_CMD_TESTMODE,
8188 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008189 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008190 .policy = nl80211_policy,
8191 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008192 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8193 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008194 },
8195#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008196 {
8197 .cmd = NL80211_CMD_CONNECT,
8198 .doit = nl80211_connect,
8199 .policy = nl80211_policy,
8200 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008201 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008202 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008203 },
8204 {
8205 .cmd = NL80211_CMD_DISCONNECT,
8206 .doit = nl80211_disconnect,
8207 .policy = nl80211_policy,
8208 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008209 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008210 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008211 },
Johannes Berg463d0182009-07-14 00:33:35 +02008212 {
8213 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8214 .doit = nl80211_wiphy_netns,
8215 .policy = nl80211_policy,
8216 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008217 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8218 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008219 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008220 {
8221 .cmd = NL80211_CMD_GET_SURVEY,
8222 .policy = nl80211_policy,
8223 .dumpit = nl80211_dump_survey,
8224 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008225 {
8226 .cmd = NL80211_CMD_SET_PMKSA,
8227 .doit = nl80211_setdel_pmksa,
8228 .policy = nl80211_policy,
8229 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008230 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008231 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008232 },
8233 {
8234 .cmd = NL80211_CMD_DEL_PMKSA,
8235 .doit = nl80211_setdel_pmksa,
8236 .policy = nl80211_policy,
8237 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008238 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008239 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008240 },
8241 {
8242 .cmd = NL80211_CMD_FLUSH_PMKSA,
8243 .doit = nl80211_flush_pmksa,
8244 .policy = nl80211_policy,
8245 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008246 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008247 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008248 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008249 {
8250 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8251 .doit = nl80211_remain_on_channel,
8252 .policy = nl80211_policy,
8253 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008254 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008255 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008256 },
8257 {
8258 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8259 .doit = nl80211_cancel_remain_on_channel,
8260 .policy = nl80211_policy,
8261 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008262 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008263 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008264 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008265 {
8266 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8267 .doit = nl80211_set_tx_bitrate_mask,
8268 .policy = nl80211_policy,
8269 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008270 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8271 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008272 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008273 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008274 .cmd = NL80211_CMD_REGISTER_FRAME,
8275 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008276 .policy = nl80211_policy,
8277 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008278 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008279 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008280 },
8281 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008282 .cmd = NL80211_CMD_FRAME,
8283 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008284 .policy = nl80211_policy,
8285 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008286 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008287 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008288 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008289 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008290 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8291 .doit = nl80211_tx_mgmt_cancel_wait,
8292 .policy = nl80211_policy,
8293 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008294 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008295 NL80211_FLAG_NEED_RTNL,
8296 },
8297 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008298 .cmd = NL80211_CMD_SET_POWER_SAVE,
8299 .doit = nl80211_set_power_save,
8300 .policy = nl80211_policy,
8301 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008302 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8303 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008304 },
8305 {
8306 .cmd = NL80211_CMD_GET_POWER_SAVE,
8307 .doit = nl80211_get_power_save,
8308 .policy = nl80211_policy,
8309 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008310 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8311 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008312 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008313 {
8314 .cmd = NL80211_CMD_SET_CQM,
8315 .doit = nl80211_set_cqm,
8316 .policy = nl80211_policy,
8317 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008318 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8319 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008320 },
Johannes Bergf444de02010-05-05 15:25:02 +02008321 {
8322 .cmd = NL80211_CMD_SET_CHANNEL,
8323 .doit = nl80211_set_channel,
8324 .policy = nl80211_policy,
8325 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008326 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8327 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008328 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008329 {
8330 .cmd = NL80211_CMD_SET_WDS_PEER,
8331 .doit = nl80211_set_wds_peer,
8332 .policy = nl80211_policy,
8333 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008334 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8335 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008336 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008337 {
8338 .cmd = NL80211_CMD_JOIN_MESH,
8339 .doit = nl80211_join_mesh,
8340 .policy = nl80211_policy,
8341 .flags = GENL_ADMIN_PERM,
8342 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8343 NL80211_FLAG_NEED_RTNL,
8344 },
8345 {
8346 .cmd = NL80211_CMD_LEAVE_MESH,
8347 .doit = nl80211_leave_mesh,
8348 .policy = nl80211_policy,
8349 .flags = GENL_ADMIN_PERM,
8350 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8351 NL80211_FLAG_NEED_RTNL,
8352 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008353#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008354 {
8355 .cmd = NL80211_CMD_GET_WOWLAN,
8356 .doit = nl80211_get_wowlan,
8357 .policy = nl80211_policy,
8358 /* can be retrieved by unprivileged users */
8359 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8360 NL80211_FLAG_NEED_RTNL,
8361 },
8362 {
8363 .cmd = NL80211_CMD_SET_WOWLAN,
8364 .doit = nl80211_set_wowlan,
8365 .policy = nl80211_policy,
8366 .flags = GENL_ADMIN_PERM,
8367 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8368 NL80211_FLAG_NEED_RTNL,
8369 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008370#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008371 {
8372 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8373 .doit = nl80211_set_rekey_data,
8374 .policy = nl80211_policy,
8375 .flags = GENL_ADMIN_PERM,
8376 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8377 NL80211_FLAG_NEED_RTNL,
8378 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008379 {
8380 .cmd = NL80211_CMD_TDLS_MGMT,
8381 .doit = nl80211_tdls_mgmt,
8382 .policy = nl80211_policy,
8383 .flags = GENL_ADMIN_PERM,
8384 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8385 NL80211_FLAG_NEED_RTNL,
8386 },
8387 {
8388 .cmd = NL80211_CMD_TDLS_OPER,
8389 .doit = nl80211_tdls_oper,
8390 .policy = nl80211_policy,
8391 .flags = GENL_ADMIN_PERM,
8392 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8393 NL80211_FLAG_NEED_RTNL,
8394 },
Johannes Berg28946da2011-11-04 11:18:12 +01008395 {
8396 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8397 .doit = nl80211_register_unexpected_frame,
8398 .policy = nl80211_policy,
8399 .flags = GENL_ADMIN_PERM,
8400 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8401 NL80211_FLAG_NEED_RTNL,
8402 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008403 {
8404 .cmd = NL80211_CMD_PROBE_CLIENT,
8405 .doit = nl80211_probe_client,
8406 .policy = nl80211_policy,
8407 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008408 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008409 NL80211_FLAG_NEED_RTNL,
8410 },
Johannes Berg5e7602302011-11-04 11:18:17 +01008411 {
8412 .cmd = NL80211_CMD_REGISTER_BEACONS,
8413 .doit = nl80211_register_beacons,
8414 .policy = nl80211_policy,
8415 .flags = GENL_ADMIN_PERM,
8416 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8417 NL80211_FLAG_NEED_RTNL,
8418 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008419 {
8420 .cmd = NL80211_CMD_SET_NOACK_MAP,
8421 .doit = nl80211_set_noack_map,
8422 .policy = nl80211_policy,
8423 .flags = GENL_ADMIN_PERM,
8424 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8425 NL80211_FLAG_NEED_RTNL,
8426 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008427 {
8428 .cmd = NL80211_CMD_START_P2P_DEVICE,
8429 .doit = nl80211_start_p2p_device,
8430 .policy = nl80211_policy,
8431 .flags = GENL_ADMIN_PERM,
8432 .internal_flags = NL80211_FLAG_NEED_WDEV |
8433 NL80211_FLAG_NEED_RTNL,
8434 },
8435 {
8436 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8437 .doit = nl80211_stop_p2p_device,
8438 .policy = nl80211_policy,
8439 .flags = GENL_ADMIN_PERM,
8440 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8441 NL80211_FLAG_NEED_RTNL,
8442 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008443 {
8444 .cmd = NL80211_CMD_SET_MCAST_RATE,
8445 .doit = nl80211_set_mcast_rate,
8446 .policy = nl80211_policy,
8447 .flags = GENL_ADMIN_PERM,
8448 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8449 NL80211_FLAG_NEED_RTNL,
8450 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308451 {
8452 .cmd = NL80211_CMD_SET_MAC_ACL,
8453 .doit = nl80211_set_mac_acl,
8454 .policy = nl80211_policy,
8455 .flags = GENL_ADMIN_PERM,
8456 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8457 NL80211_FLAG_NEED_RTNL,
8458 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008459 {
8460 .cmd = NL80211_CMD_RADAR_DETECT,
8461 .doit = nl80211_start_radar_detection,
8462 .policy = nl80211_policy,
8463 .flags = GENL_ADMIN_PERM,
8464 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8465 NL80211_FLAG_NEED_RTNL,
8466 },
Johannes Berg55682962007-09-20 13:09:35 -04008467};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008468
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008469static struct genl_multicast_group nl80211_mlme_mcgrp = {
8470 .name = "mlme",
8471};
Johannes Berg55682962007-09-20 13:09:35 -04008472
8473/* multicast groups */
8474static struct genl_multicast_group nl80211_config_mcgrp = {
8475 .name = "config",
8476};
Johannes Berg2a519312009-02-10 21:25:55 +01008477static struct genl_multicast_group nl80211_scan_mcgrp = {
8478 .name = "scan",
8479};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008480static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8481 .name = "regulatory",
8482};
Johannes Berg55682962007-09-20 13:09:35 -04008483
8484/* notification functions */
8485
8486void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8487{
8488 struct sk_buff *msg;
8489
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008490 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008491 if (!msg)
8492 return;
8493
8494 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
8495 nlmsg_free(msg);
8496 return;
8497 }
8498
Johannes Berg463d0182009-07-14 00:33:35 +02008499 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8500 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008501}
8502
Johannes Berg362a4152009-05-24 16:43:15 +02008503static int nl80211_add_scan_req(struct sk_buff *msg,
8504 struct cfg80211_registered_device *rdev)
8505{
8506 struct cfg80211_scan_request *req = rdev->scan_req;
8507 struct nlattr *nest;
8508 int i;
8509
Johannes Berg667503dd2009-07-07 03:56:11 +02008510 ASSERT_RDEV_LOCK(rdev);
8511
Johannes Berg362a4152009-05-24 16:43:15 +02008512 if (WARN_ON(!req))
8513 return 0;
8514
8515 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8516 if (!nest)
8517 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008518 for (i = 0; i < req->n_ssids; i++) {
8519 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8520 goto nla_put_failure;
8521 }
Johannes Berg362a4152009-05-24 16:43:15 +02008522 nla_nest_end(msg, nest);
8523
8524 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8525 if (!nest)
8526 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008527 for (i = 0; i < req->n_channels; i++) {
8528 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8529 goto nla_put_failure;
8530 }
Johannes Berg362a4152009-05-24 16:43:15 +02008531 nla_nest_end(msg, nest);
8532
David S. Miller9360ffd2012-03-29 04:41:26 -04008533 if (req->ie &&
8534 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8535 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008536
Sam Lefflered4737712012-10-11 21:03:31 -07008537 if (req->flags)
8538 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8539
Johannes Berg362a4152009-05-24 16:43:15 +02008540 return 0;
8541 nla_put_failure:
8542 return -ENOBUFS;
8543}
8544
Johannes Berga538e2d2009-06-16 19:56:42 +02008545static int nl80211_send_scan_msg(struct sk_buff *msg,
8546 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008547 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008548 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008549 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008550{
8551 void *hdr;
8552
Eric W. Biederman15e47302012-09-07 20:12:54 +00008553 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008554 if (!hdr)
8555 return -1;
8556
David S. Miller9360ffd2012-03-29 04:41:26 -04008557 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008558 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8559 wdev->netdev->ifindex)) ||
8560 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008561 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008562
Johannes Berg362a4152009-05-24 16:43:15 +02008563 /* ignore errors and send incomplete event anyway */
8564 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008565
8566 return genlmsg_end(msg, hdr);
8567
8568 nla_put_failure:
8569 genlmsg_cancel(msg, hdr);
8570 return -EMSGSIZE;
8571}
8572
Luciano Coelho807f8a82011-05-11 17:09:35 +03008573static int
8574nl80211_send_sched_scan_msg(struct sk_buff *msg,
8575 struct cfg80211_registered_device *rdev,
8576 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008577 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008578{
8579 void *hdr;
8580
Eric W. Biederman15e47302012-09-07 20:12:54 +00008581 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008582 if (!hdr)
8583 return -1;
8584
David S. Miller9360ffd2012-03-29 04:41:26 -04008585 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8586 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8587 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008588
8589 return genlmsg_end(msg, hdr);
8590
8591 nla_put_failure:
8592 genlmsg_cancel(msg, hdr);
8593 return -EMSGSIZE;
8594}
8595
Johannes Berga538e2d2009-06-16 19:56:42 +02008596void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008597 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008598{
8599 struct sk_buff *msg;
8600
Thomas Graf58050fc2012-06-28 03:57:45 +00008601 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008602 if (!msg)
8603 return;
8604
Johannes Bergfd014282012-06-18 19:17:03 +02008605 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008606 NL80211_CMD_TRIGGER_SCAN) < 0) {
8607 nlmsg_free(msg);
8608 return;
8609 }
8610
Johannes Berg463d0182009-07-14 00:33:35 +02008611 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8612 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008613}
8614
Johannes Berg2a519312009-02-10 21:25:55 +01008615void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008616 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008617{
8618 struct sk_buff *msg;
8619
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008620 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008621 if (!msg)
8622 return;
8623
Johannes Bergfd014282012-06-18 19:17:03 +02008624 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008625 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008626 nlmsg_free(msg);
8627 return;
8628 }
8629
Johannes Berg463d0182009-07-14 00:33:35 +02008630 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8631 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008632}
8633
8634void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008635 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008636{
8637 struct sk_buff *msg;
8638
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008639 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008640 if (!msg)
8641 return;
8642
Johannes Bergfd014282012-06-18 19:17:03 +02008643 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008644 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008645 nlmsg_free(msg);
8646 return;
8647 }
8648
Johannes Berg463d0182009-07-14 00:33:35 +02008649 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8650 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008651}
8652
Luciano Coelho807f8a82011-05-11 17:09:35 +03008653void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8654 struct net_device *netdev)
8655{
8656 struct sk_buff *msg;
8657
8658 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8659 if (!msg)
8660 return;
8661
8662 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8663 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8664 nlmsg_free(msg);
8665 return;
8666 }
8667
8668 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8669 nl80211_scan_mcgrp.id, GFP_KERNEL);
8670}
8671
8672void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8673 struct net_device *netdev, u32 cmd)
8674{
8675 struct sk_buff *msg;
8676
Thomas Graf58050fc2012-06-28 03:57:45 +00008677 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008678 if (!msg)
8679 return;
8680
8681 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8682 nlmsg_free(msg);
8683 return;
8684 }
8685
8686 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8687 nl80211_scan_mcgrp.id, GFP_KERNEL);
8688}
8689
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008690/*
8691 * This can happen on global regulatory changes or device specific settings
8692 * based on custom world regulatory domains.
8693 */
8694void nl80211_send_reg_change_event(struct regulatory_request *request)
8695{
8696 struct sk_buff *msg;
8697 void *hdr;
8698
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008699 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008700 if (!msg)
8701 return;
8702
8703 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8704 if (!hdr) {
8705 nlmsg_free(msg);
8706 return;
8707 }
8708
8709 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008710 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8711 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008712
David S. Miller9360ffd2012-03-29 04:41:26 -04008713 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8714 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8715 NL80211_REGDOM_TYPE_WORLD))
8716 goto nla_put_failure;
8717 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8718 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8719 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8720 goto nla_put_failure;
8721 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8722 request->intersect) {
8723 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8724 NL80211_REGDOM_TYPE_INTERSECTION))
8725 goto nla_put_failure;
8726 } else {
8727 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8728 NL80211_REGDOM_TYPE_COUNTRY) ||
8729 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8730 request->alpha2))
8731 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008732 }
8733
Johannes Bergf4173762012-12-03 18:23:37 +01008734 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008735 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8736 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008737
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008738 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008739
Johannes Bergbc43b282009-07-25 10:54:13 +02008740 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008741 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008742 GFP_ATOMIC);
8743 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008744
8745 return;
8746
8747nla_put_failure:
8748 genlmsg_cancel(msg, hdr);
8749 nlmsg_free(msg);
8750}
8751
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008752static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8753 struct net_device *netdev,
8754 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008755 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008756{
8757 struct sk_buff *msg;
8758 void *hdr;
8759
Johannes Berge6d6e342009-07-01 21:26:47 +02008760 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008761 if (!msg)
8762 return;
8763
8764 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8765 if (!hdr) {
8766 nlmsg_free(msg);
8767 return;
8768 }
8769
David S. Miller9360ffd2012-03-29 04:41:26 -04008770 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8771 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8772 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8773 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008774
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008775 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008776
Johannes Berg463d0182009-07-14 00:33:35 +02008777 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8778 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008779 return;
8780
8781 nla_put_failure:
8782 genlmsg_cancel(msg, hdr);
8783 nlmsg_free(msg);
8784}
8785
8786void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008787 struct net_device *netdev, const u8 *buf,
8788 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008789{
8790 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008791 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008792}
8793
8794void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8795 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008796 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008797{
Johannes Berge6d6e342009-07-01 21:26:47 +02008798 nl80211_send_mlme_event(rdev, netdev, buf, len,
8799 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008800}
8801
Jouni Malinen53b46b82009-03-27 20:53:56 +02008802void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008803 struct net_device *netdev, const u8 *buf,
8804 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008805{
8806 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008807 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008808}
8809
Jouni Malinen53b46b82009-03-27 20:53:56 +02008810void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8811 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008812 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008813{
8814 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008815 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008816}
8817
Jouni Malinencf4e5942010-12-16 00:52:40 +02008818void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8819 struct net_device *netdev, const u8 *buf,
8820 size_t len, gfp_t gfp)
8821{
8822 nl80211_send_mlme_event(rdev, netdev, buf, len,
8823 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8824}
8825
8826void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8827 struct net_device *netdev, const u8 *buf,
8828 size_t len, gfp_t gfp)
8829{
8830 nl80211_send_mlme_event(rdev, netdev, buf, len,
8831 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8832}
8833
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008834static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8835 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008836 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008837{
8838 struct sk_buff *msg;
8839 void *hdr;
8840
Johannes Berge6d6e342009-07-01 21:26:47 +02008841 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008842 if (!msg)
8843 return;
8844
8845 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8846 if (!hdr) {
8847 nlmsg_free(msg);
8848 return;
8849 }
8850
David S. Miller9360ffd2012-03-29 04:41:26 -04008851 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8852 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8853 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8854 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8855 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008856
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008857 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008858
Johannes Berg463d0182009-07-14 00:33:35 +02008859 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8860 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008861 return;
8862
8863 nla_put_failure:
8864 genlmsg_cancel(msg, hdr);
8865 nlmsg_free(msg);
8866}
8867
8868void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008869 struct net_device *netdev, const u8 *addr,
8870 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008871{
8872 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008873 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008874}
8875
8876void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008877 struct net_device *netdev, const u8 *addr,
8878 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008879{
Johannes Berge6d6e342009-07-01 21:26:47 +02008880 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8881 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008882}
8883
Samuel Ortizb23aa672009-07-01 21:26:54 +02008884void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8885 struct net_device *netdev, const u8 *bssid,
8886 const u8 *req_ie, size_t req_ie_len,
8887 const u8 *resp_ie, size_t resp_ie_len,
8888 u16 status, gfp_t gfp)
8889{
8890 struct sk_buff *msg;
8891 void *hdr;
8892
Thomas Graf58050fc2012-06-28 03:57:45 +00008893 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008894 if (!msg)
8895 return;
8896
8897 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8898 if (!hdr) {
8899 nlmsg_free(msg);
8900 return;
8901 }
8902
David S. Miller9360ffd2012-03-29 04:41:26 -04008903 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8904 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8905 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8906 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8907 (req_ie &&
8908 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8909 (resp_ie &&
8910 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8911 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008912
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008913 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008914
Johannes Berg463d0182009-07-14 00:33:35 +02008915 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8916 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008917 return;
8918
8919 nla_put_failure:
8920 genlmsg_cancel(msg, hdr);
8921 nlmsg_free(msg);
8922
8923}
8924
8925void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8926 struct net_device *netdev, const u8 *bssid,
8927 const u8 *req_ie, size_t req_ie_len,
8928 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8929{
8930 struct sk_buff *msg;
8931 void *hdr;
8932
Thomas Graf58050fc2012-06-28 03:57:45 +00008933 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008934 if (!msg)
8935 return;
8936
8937 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8938 if (!hdr) {
8939 nlmsg_free(msg);
8940 return;
8941 }
8942
David S. Miller9360ffd2012-03-29 04:41:26 -04008943 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8944 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8945 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8946 (req_ie &&
8947 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8948 (resp_ie &&
8949 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8950 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008951
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008952 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008953
Johannes Berg463d0182009-07-14 00:33:35 +02008954 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8955 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008956 return;
8957
8958 nla_put_failure:
8959 genlmsg_cancel(msg, hdr);
8960 nlmsg_free(msg);
8961
8962}
8963
8964void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8965 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008966 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008967{
8968 struct sk_buff *msg;
8969 void *hdr;
8970
Thomas Graf58050fc2012-06-28 03:57:45 +00008971 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008972 if (!msg)
8973 return;
8974
8975 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8976 if (!hdr) {
8977 nlmsg_free(msg);
8978 return;
8979 }
8980
David S. Miller9360ffd2012-03-29 04:41:26 -04008981 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8982 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8983 (from_ap && reason &&
8984 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8985 (from_ap &&
8986 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8987 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8988 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008989
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008990 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008991
Johannes Berg463d0182009-07-14 00:33:35 +02008992 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8993 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008994 return;
8995
8996 nla_put_failure:
8997 genlmsg_cancel(msg, hdr);
8998 nlmsg_free(msg);
8999
9000}
9001
Johannes Berg04a773a2009-04-19 21:24:32 +02009002void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9003 struct net_device *netdev, const u8 *bssid,
9004 gfp_t gfp)
9005{
9006 struct sk_buff *msg;
9007 void *hdr;
9008
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009009 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009010 if (!msg)
9011 return;
9012
9013 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9014 if (!hdr) {
9015 nlmsg_free(msg);
9016 return;
9017 }
9018
David S. Miller9360ffd2012-03-29 04:41:26 -04009019 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9020 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9021 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9022 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009023
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009024 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009025
Johannes Berg463d0182009-07-14 00:33:35 +02009026 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9027 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009028 return;
9029
9030 nla_put_failure:
9031 genlmsg_cancel(msg, hdr);
9032 nlmsg_free(msg);
9033}
9034
Javier Cardonac93b5e72011-04-07 15:08:34 -07009035void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
9036 struct net_device *netdev,
9037 const u8 *macaddr, const u8* ie, u8 ie_len,
9038 gfp_t gfp)
9039{
9040 struct sk_buff *msg;
9041 void *hdr;
9042
9043 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9044 if (!msg)
9045 return;
9046
9047 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9048 if (!hdr) {
9049 nlmsg_free(msg);
9050 return;
9051 }
9052
David S. Miller9360ffd2012-03-29 04:41:26 -04009053 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9054 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9055 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
9056 (ie_len && ie &&
9057 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9058 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009059
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009060 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009061
9062 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9063 nl80211_mlme_mcgrp.id, gfp);
9064 return;
9065
9066 nla_put_failure:
9067 genlmsg_cancel(msg, hdr);
9068 nlmsg_free(msg);
9069}
9070
Jouni Malinena3b8b052009-03-27 21:59:49 +02009071void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9072 struct net_device *netdev, const u8 *addr,
9073 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009074 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009075{
9076 struct sk_buff *msg;
9077 void *hdr;
9078
Johannes Berge6d6e342009-07-01 21:26:47 +02009079 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009080 if (!msg)
9081 return;
9082
9083 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9084 if (!hdr) {
9085 nlmsg_free(msg);
9086 return;
9087 }
9088
David S. Miller9360ffd2012-03-29 04:41:26 -04009089 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9090 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9091 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9092 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9093 (key_id != -1 &&
9094 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9095 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9096 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009097
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009098 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009099
Johannes Berg463d0182009-07-14 00:33:35 +02009100 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9101 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009102 return;
9103
9104 nla_put_failure:
9105 genlmsg_cancel(msg, hdr);
9106 nlmsg_free(msg);
9107}
9108
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009109void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9110 struct ieee80211_channel *channel_before,
9111 struct ieee80211_channel *channel_after)
9112{
9113 struct sk_buff *msg;
9114 void *hdr;
9115 struct nlattr *nl_freq;
9116
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009117 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009118 if (!msg)
9119 return;
9120
9121 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9122 if (!hdr) {
9123 nlmsg_free(msg);
9124 return;
9125 }
9126
9127 /*
9128 * Since we are applying the beacon hint to a wiphy we know its
9129 * wiphy_idx is valid
9130 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009131 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9132 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009133
9134 /* Before */
9135 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9136 if (!nl_freq)
9137 goto nla_put_failure;
9138 if (nl80211_msg_put_channel(msg, channel_before))
9139 goto nla_put_failure;
9140 nla_nest_end(msg, nl_freq);
9141
9142 /* After */
9143 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9144 if (!nl_freq)
9145 goto nla_put_failure;
9146 if (nl80211_msg_put_channel(msg, channel_after))
9147 goto nla_put_failure;
9148 nla_nest_end(msg, nl_freq);
9149
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009150 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009151
Johannes Berg463d0182009-07-14 00:33:35 +02009152 rcu_read_lock();
9153 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9154 GFP_ATOMIC);
9155 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009156
9157 return;
9158
9159nla_put_failure:
9160 genlmsg_cancel(msg, hdr);
9161 nlmsg_free(msg);
9162}
9163
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009164static void nl80211_send_remain_on_chan_event(
9165 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009166 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009167 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009168 unsigned int duration, gfp_t gfp)
9169{
9170 struct sk_buff *msg;
9171 void *hdr;
9172
9173 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9174 if (!msg)
9175 return;
9176
9177 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9178 if (!hdr) {
9179 nlmsg_free(msg);
9180 return;
9181 }
9182
David S. Miller9360ffd2012-03-29 04:41:26 -04009183 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009184 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9185 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009186 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009187 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009188 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9189 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009190 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9191 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009192
David S. Miller9360ffd2012-03-29 04:41:26 -04009193 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9194 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9195 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009196
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009197 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009198
9199 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9200 nl80211_mlme_mcgrp.id, gfp);
9201 return;
9202
9203 nla_put_failure:
9204 genlmsg_cancel(msg, hdr);
9205 nlmsg_free(msg);
9206}
9207
9208void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009209 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009210 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009211 unsigned int duration, gfp_t gfp)
9212{
9213 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009214 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009215 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009216}
9217
9218void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02009219 struct cfg80211_registered_device *rdev,
9220 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01009221 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009222{
9223 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009224 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009225}
9226
Johannes Berg98b62182009-12-23 13:15:44 +01009227void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
9228 struct net_device *dev, const u8 *mac_addr,
9229 struct station_info *sinfo, gfp_t gfp)
9230{
9231 struct sk_buff *msg;
9232
Thomas Graf58050fc2012-06-28 03:57:45 +00009233 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009234 if (!msg)
9235 return;
9236
John W. Linville66266b32012-03-15 13:25:41 -04009237 if (nl80211_send_station(msg, 0, 0, 0,
9238 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009239 nlmsg_free(msg);
9240 return;
9241 }
9242
9243 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9244 nl80211_mlme_mcgrp.id, gfp);
9245}
9246
Jouni Malinenec15e682011-03-23 15:29:52 +02009247void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
9248 struct net_device *dev, const u8 *mac_addr,
9249 gfp_t gfp)
9250{
9251 struct sk_buff *msg;
9252 void *hdr;
9253
Thomas Graf58050fc2012-06-28 03:57:45 +00009254 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009255 if (!msg)
9256 return;
9257
9258 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9259 if (!hdr) {
9260 nlmsg_free(msg);
9261 return;
9262 }
9263
David S. Miller9360ffd2012-03-29 04:41:26 -04009264 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9265 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9266 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009267
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009268 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009269
9270 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9271 nl80211_mlme_mcgrp.id, gfp);
9272 return;
9273
9274 nla_put_failure:
9275 genlmsg_cancel(msg, hdr);
9276 nlmsg_free(msg);
9277}
9278
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309279void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
9280 struct net_device *dev, const u8 *mac_addr,
9281 enum nl80211_connect_failed_reason reason,
9282 gfp_t gfp)
9283{
9284 struct sk_buff *msg;
9285 void *hdr;
9286
9287 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9288 if (!msg)
9289 return;
9290
9291 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9292 if (!hdr) {
9293 nlmsg_free(msg);
9294 return;
9295 }
9296
9297 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9298 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9299 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9300 goto nla_put_failure;
9301
9302 genlmsg_end(msg, hdr);
9303
9304 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9305 nl80211_mlme_mcgrp.id, gfp);
9306 return;
9307
9308 nla_put_failure:
9309 genlmsg_cancel(msg, hdr);
9310 nlmsg_free(msg);
9311}
9312
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009313static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9314 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009315{
9316 struct wireless_dev *wdev = dev->ieee80211_ptr;
9317 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9318 struct sk_buff *msg;
9319 void *hdr;
9320 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009321 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009322
Eric W. Biederman15e47302012-09-07 20:12:54 +00009323 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009324 return false;
9325
9326 msg = nlmsg_new(100, gfp);
9327 if (!msg)
9328 return true;
9329
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009330 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009331 if (!hdr) {
9332 nlmsg_free(msg);
9333 return true;
9334 }
9335
David S. Miller9360ffd2012-03-29 04:41:26 -04009336 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9337 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9338 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9339 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009340
9341 err = genlmsg_end(msg, hdr);
9342 if (err < 0) {
9343 nlmsg_free(msg);
9344 return true;
9345 }
9346
Eric W. Biederman15e47302012-09-07 20:12:54 +00009347 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009348 return true;
9349
9350 nla_put_failure:
9351 genlmsg_cancel(msg, hdr);
9352 nlmsg_free(msg);
9353 return true;
9354}
9355
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009356bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
9357{
9358 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9359 addr, gfp);
9360}
9361
9362bool nl80211_unexpected_4addr_frame(struct net_device *dev,
9363 const u8 *addr, gfp_t gfp)
9364{
9365 return __nl80211_unexpected_frame(dev,
9366 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9367 addr, gfp);
9368}
9369
Johannes Berg2e161f72010-08-12 15:38:38 +02009370int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009371 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009372 int freq, int sig_dbm,
9373 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009374{
Johannes Berg71bbc992012-06-15 15:30:18 +02009375 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009376 struct sk_buff *msg;
9377 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009378
9379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9380 if (!msg)
9381 return -ENOMEM;
9382
Johannes Berg2e161f72010-08-12 15:38:38 +02009383 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009384 if (!hdr) {
9385 nlmsg_free(msg);
9386 return -ENOMEM;
9387 }
9388
David S. Miller9360ffd2012-03-29 04:41:26 -04009389 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009390 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9391 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009392 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9393 (sig_dbm &&
9394 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9395 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9396 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009397
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009398 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009399
Eric W. Biederman15e47302012-09-07 20:12:54 +00009400 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009401
9402 nla_put_failure:
9403 genlmsg_cancel(msg, hdr);
9404 nlmsg_free(msg);
9405 return -ENOBUFS;
9406}
9407
Johannes Berg2e161f72010-08-12 15:38:38 +02009408void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009409 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02009410 const u8 *buf, size_t len, bool ack,
9411 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009412{
Johannes Berg71bbc992012-06-15 15:30:18 +02009413 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009414 struct sk_buff *msg;
9415 void *hdr;
9416
9417 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9418 if (!msg)
9419 return;
9420
Johannes Berg2e161f72010-08-12 15:38:38 +02009421 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009422 if (!hdr) {
9423 nlmsg_free(msg);
9424 return;
9425 }
9426
David S. Miller9360ffd2012-03-29 04:41:26 -04009427 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009428 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9429 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009430 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9431 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9432 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9433 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009434
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009435 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009436
9437 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9438 return;
9439
9440 nla_put_failure:
9441 genlmsg_cancel(msg, hdr);
9442 nlmsg_free(msg);
9443}
9444
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009445void
9446nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9447 struct net_device *netdev,
9448 enum nl80211_cqm_rssi_threshold_event rssi_event,
9449 gfp_t gfp)
9450{
9451 struct sk_buff *msg;
9452 struct nlattr *pinfoattr;
9453 void *hdr;
9454
Thomas Graf58050fc2012-06-28 03:57:45 +00009455 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009456 if (!msg)
9457 return;
9458
9459 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9460 if (!hdr) {
9461 nlmsg_free(msg);
9462 return;
9463 }
9464
David S. Miller9360ffd2012-03-29 04:41:26 -04009465 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9466 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9467 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009468
9469 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9470 if (!pinfoattr)
9471 goto nla_put_failure;
9472
David S. Miller9360ffd2012-03-29 04:41:26 -04009473 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9474 rssi_event))
9475 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009476
9477 nla_nest_end(msg, pinfoattr);
9478
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009479 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009480
9481 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9482 nl80211_mlme_mcgrp.id, gfp);
9483 return;
9484
9485 nla_put_failure:
9486 genlmsg_cancel(msg, hdr);
9487 nlmsg_free(msg);
9488}
9489
Johannes Berge5497d72011-07-05 16:35:40 +02009490void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9491 struct net_device *netdev, const u8 *bssid,
9492 const u8 *replay_ctr, gfp_t gfp)
9493{
9494 struct sk_buff *msg;
9495 struct nlattr *rekey_attr;
9496 void *hdr;
9497
Thomas Graf58050fc2012-06-28 03:57:45 +00009498 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009499 if (!msg)
9500 return;
9501
9502 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9503 if (!hdr) {
9504 nlmsg_free(msg);
9505 return;
9506 }
9507
David S. Miller9360ffd2012-03-29 04:41:26 -04009508 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9509 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9510 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9511 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009512
9513 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9514 if (!rekey_attr)
9515 goto nla_put_failure;
9516
David S. Miller9360ffd2012-03-29 04:41:26 -04009517 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9518 NL80211_REPLAY_CTR_LEN, replay_ctr))
9519 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009520
9521 nla_nest_end(msg, rekey_attr);
9522
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009523 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +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
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009534void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9535 struct net_device *netdev, int index,
9536 const u8 *bssid, bool preauth, gfp_t gfp)
9537{
9538 struct sk_buff *msg;
9539 struct nlattr *attr;
9540 void *hdr;
9541
Thomas Graf58050fc2012-06-28 03:57:45 +00009542 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009543 if (!msg)
9544 return;
9545
9546 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
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 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009555
9556 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9557 if (!attr)
9558 goto nla_put_failure;
9559
David S. Miller9360ffd2012-03-29 04:41:26 -04009560 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9561 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9562 (preauth &&
9563 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9564 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009565
9566 nla_nest_end(msg, attr);
9567
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009568 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009569
9570 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9571 nl80211_mlme_mcgrp.id, gfp);
9572 return;
9573
9574 nla_put_failure:
9575 genlmsg_cancel(msg, hdr);
9576 nlmsg_free(msg);
9577}
9578
Thomas Pedersen53145262012-04-06 13:35:47 -07009579void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009580 struct net_device *netdev,
9581 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009582{
9583 struct sk_buff *msg;
9584 void *hdr;
9585
Thomas Graf58050fc2012-06-28 03:57:45 +00009586 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009587 if (!msg)
9588 return;
9589
9590 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9591 if (!hdr) {
9592 nlmsg_free(msg);
9593 return;
9594 }
9595
Johannes Berg683b6d32012-11-08 21:25:48 +01009596 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9597 goto nla_put_failure;
9598
9599 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009600 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009601
9602 genlmsg_end(msg, hdr);
9603
9604 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9605 nl80211_mlme_mcgrp.id, gfp);
9606 return;
9607
9608 nla_put_failure:
9609 genlmsg_cancel(msg, hdr);
9610 nlmsg_free(msg);
9611}
9612
Johannes Bergc063dbf2010-11-24 08:10:05 +01009613void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009614nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9615 struct net_device *netdev, const u8 *peer,
9616 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9617{
9618 struct sk_buff *msg;
9619 struct nlattr *pinfoattr;
9620 void *hdr;
9621
9622 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9623 if (!msg)
9624 return;
9625
9626 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9627 if (!hdr) {
9628 nlmsg_free(msg);
9629 return;
9630 }
9631
9632 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9633 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9634 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9635 goto nla_put_failure;
9636
9637 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9638 if (!pinfoattr)
9639 goto nla_put_failure;
9640
9641 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9642 goto nla_put_failure;
9643
9644 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9645 goto nla_put_failure;
9646
9647 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9648 goto nla_put_failure;
9649
9650 nla_nest_end(msg, pinfoattr);
9651
9652 genlmsg_end(msg, hdr);
9653
9654 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9655 nl80211_mlme_mcgrp.id, gfp);
9656 return;
9657
9658 nla_put_failure:
9659 genlmsg_cancel(msg, hdr);
9660 nlmsg_free(msg);
9661}
9662
9663void
Simon Wunderlich04f39042013-02-08 18:16:19 +01009664nl80211_radar_notify(struct cfg80211_registered_device *rdev,
9665 struct cfg80211_chan_def *chandef,
9666 enum nl80211_radar_event event,
9667 struct net_device *netdev, gfp_t gfp)
9668{
9669 struct sk_buff *msg;
9670 void *hdr;
9671
9672 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9673 if (!msg)
9674 return;
9675
9676 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
9677 if (!hdr) {
9678 nlmsg_free(msg);
9679 return;
9680 }
9681
9682 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9683 goto nla_put_failure;
9684
9685 /* NOP and radar events don't need a netdev parameter */
9686 if (netdev) {
9687 struct wireless_dev *wdev = netdev->ieee80211_ptr;
9688
9689 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9690 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9691 goto nla_put_failure;
9692 }
9693
9694 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
9695 goto nla_put_failure;
9696
9697 if (nl80211_send_chandef(msg, chandef))
9698 goto nla_put_failure;
9699
9700 if (genlmsg_end(msg, hdr) < 0) {
9701 nlmsg_free(msg);
9702 return;
9703 }
9704
9705 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9706 nl80211_mlme_mcgrp.id, gfp);
9707 return;
9708
9709 nla_put_failure:
9710 genlmsg_cancel(msg, hdr);
9711 nlmsg_free(msg);
9712}
9713
9714void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009715nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9716 struct net_device *netdev, const u8 *peer,
9717 u32 num_packets, gfp_t gfp)
9718{
9719 struct sk_buff *msg;
9720 struct nlattr *pinfoattr;
9721 void *hdr;
9722
Thomas Graf58050fc2012-06-28 03:57:45 +00009723 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009724 if (!msg)
9725 return;
9726
9727 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9728 if (!hdr) {
9729 nlmsg_free(msg);
9730 return;
9731 }
9732
David S. Miller9360ffd2012-03-29 04:41:26 -04009733 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9734 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9735 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9736 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009737
9738 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9739 if (!pinfoattr)
9740 goto nla_put_failure;
9741
David S. Miller9360ffd2012-03-29 04:41:26 -04009742 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9743 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009744
9745 nla_nest_end(msg, pinfoattr);
9746
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009747 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009748
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
Johannes Berg7f6cf312011-11-04 11:18:15 +01009758void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9759 u64 cookie, bool acked, gfp_t gfp)
9760{
9761 struct wireless_dev *wdev = dev->ieee80211_ptr;
9762 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9763 struct sk_buff *msg;
9764 void *hdr;
9765 int err;
9766
Beni Lev4ee3e062012-08-27 12:49:39 +03009767 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9768
Thomas Graf58050fc2012-06-28 03:57:45 +00009769 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009770
Johannes Berg7f6cf312011-11-04 11:18:15 +01009771 if (!msg)
9772 return;
9773
9774 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9775 if (!hdr) {
9776 nlmsg_free(msg);
9777 return;
9778 }
9779
David S. Miller9360ffd2012-03-29 04:41:26 -04009780 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9781 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9782 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9783 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9784 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9785 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009786
9787 err = genlmsg_end(msg, hdr);
9788 if (err < 0) {
9789 nlmsg_free(msg);
9790 return;
9791 }
9792
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}
9801EXPORT_SYMBOL(cfg80211_probe_status);
9802
Johannes Berg5e7602302011-11-04 11:18:17 +01009803void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9804 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009805 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +01009806{
9807 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9808 struct sk_buff *msg;
9809 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009810 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +01009811
Beni Lev4ee3e062012-08-27 12:49:39 +03009812 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9813
Ben Greear37c73b52012-10-26 14:49:25 -07009814 spin_lock_bh(&rdev->beacon_registrations_lock);
9815 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9816 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9817 if (!msg) {
9818 spin_unlock_bh(&rdev->beacon_registrations_lock);
9819 return;
9820 }
Johannes Berg5e7602302011-11-04 11:18:17 +01009821
Ben Greear37c73b52012-10-26 14:49:25 -07009822 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9823 if (!hdr)
9824 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01009825
Ben Greear37c73b52012-10-26 14:49:25 -07009826 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9827 (freq &&
9828 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9829 (sig_dbm &&
9830 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9831 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9832 goto nla_put_failure;
9833
9834 genlmsg_end(msg, hdr);
9835
9836 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +01009837 }
Ben Greear37c73b52012-10-26 14:49:25 -07009838 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01009839 return;
9840
9841 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009842 spin_unlock_bh(&rdev->beacon_registrations_lock);
9843 if (hdr)
9844 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +01009845 nlmsg_free(msg);
9846}
9847EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9848
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009849#ifdef CONFIG_PM
9850void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
9851 struct cfg80211_wowlan_wakeup *wakeup,
9852 gfp_t gfp)
9853{
9854 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9855 struct sk_buff *msg;
9856 void *hdr;
9857 int err, size = 200;
9858
9859 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
9860
9861 if (wakeup)
9862 size += wakeup->packet_present_len;
9863
9864 msg = nlmsg_new(size, gfp);
9865 if (!msg)
9866 return;
9867
9868 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
9869 if (!hdr)
9870 goto free_msg;
9871
9872 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9873 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9874 goto free_msg;
9875
9876 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9877 wdev->netdev->ifindex))
9878 goto free_msg;
9879
9880 if (wakeup) {
9881 struct nlattr *reasons;
9882
9883 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9884
9885 if (wakeup->disconnect &&
9886 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
9887 goto free_msg;
9888 if (wakeup->magic_pkt &&
9889 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
9890 goto free_msg;
9891 if (wakeup->gtk_rekey_failure &&
9892 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
9893 goto free_msg;
9894 if (wakeup->eap_identity_req &&
9895 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
9896 goto free_msg;
9897 if (wakeup->four_way_handshake &&
9898 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
9899 goto free_msg;
9900 if (wakeup->rfkill_release &&
9901 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
9902 goto free_msg;
9903
9904 if (wakeup->pattern_idx >= 0 &&
9905 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
9906 wakeup->pattern_idx))
9907 goto free_msg;
9908
Johannes Berg2a0e0472013-01-23 22:57:40 +01009909 if (wakeup->tcp_match)
9910 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
9911
9912 if (wakeup->tcp_connlost)
9913 nla_put_flag(msg,
9914 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
9915
9916 if (wakeup->tcp_nomoretokens)
9917 nla_put_flag(msg,
9918 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
9919
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009920 if (wakeup->packet) {
9921 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
9922 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
9923
9924 if (!wakeup->packet_80211) {
9925 pkt_attr =
9926 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
9927 len_attr =
9928 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
9929 }
9930
9931 if (wakeup->packet_len &&
9932 nla_put_u32(msg, len_attr, wakeup->packet_len))
9933 goto free_msg;
9934
9935 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
9936 wakeup->packet))
9937 goto free_msg;
9938 }
9939
9940 nla_nest_end(msg, reasons);
9941 }
9942
9943 err = genlmsg_end(msg, hdr);
9944 if (err < 0)
9945 goto free_msg;
9946
9947 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9948 nl80211_mlme_mcgrp.id, gfp);
9949 return;
9950
9951 free_msg:
9952 nlmsg_free(msg);
9953}
9954EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
9955#endif
9956
Jouni Malinen3475b092012-11-16 22:49:57 +02009957void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9958 enum nl80211_tdls_operation oper,
9959 u16 reason_code, gfp_t gfp)
9960{
9961 struct wireless_dev *wdev = dev->ieee80211_ptr;
9962 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9963 struct sk_buff *msg;
9964 void *hdr;
9965 int err;
9966
9967 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9968 reason_code);
9969
9970 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9971 if (!msg)
9972 return;
9973
9974 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9975 if (!hdr) {
9976 nlmsg_free(msg);
9977 return;
9978 }
9979
9980 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9981 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9982 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9983 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9984 (reason_code > 0 &&
9985 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9986 goto nla_put_failure;
9987
9988 err = genlmsg_end(msg, hdr);
9989 if (err < 0) {
9990 nlmsg_free(msg);
9991 return;
9992 }
9993
9994 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9995 nl80211_mlme_mcgrp.id, gfp);
9996 return;
9997
9998 nla_put_failure:
9999 genlmsg_cancel(msg, hdr);
10000 nlmsg_free(msg);
10001}
10002EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10003
Jouni Malinen026331c2010-02-15 12:53:10 +020010004static int nl80211_netlink_notify(struct notifier_block * nb,
10005 unsigned long state,
10006 void *_notify)
10007{
10008 struct netlink_notify *notify = _notify;
10009 struct cfg80211_registered_device *rdev;
10010 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010011 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010012
10013 if (state != NETLINK_URELEASE)
10014 return NOTIFY_DONE;
10015
10016 rcu_read_lock();
10017
Johannes Berg5e7602302011-11-04 11:18:17 +010010018 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010019 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010020 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010021
10022 spin_lock_bh(&rdev->beacon_registrations_lock);
10023 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10024 list) {
10025 if (reg->nlportid == notify->portid) {
10026 list_del(&reg->list);
10027 kfree(reg);
10028 break;
10029 }
10030 }
10031 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010010032 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010033
10034 rcu_read_unlock();
10035
10036 return NOTIFY_DONE;
10037}
10038
10039static struct notifier_block nl80211_netlink_notifier = {
10040 .notifier_call = nl80211_netlink_notify,
10041};
10042
Johannes Berg55682962007-09-20 13:09:35 -040010043/* initialisation/exit functions */
10044
10045int nl80211_init(void)
10046{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010047 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010048
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010049 err = genl_register_family_with_ops(&nl80211_fam,
10050 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010051 if (err)
10052 return err;
10053
Johannes Berg55682962007-09-20 13:09:35 -040010054 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10055 if (err)
10056 goto err_out;
10057
Johannes Berg2a519312009-02-10 21:25:55 +010010058 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10059 if (err)
10060 goto err_out;
10061
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010062 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10063 if (err)
10064 goto err_out;
10065
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010066 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10067 if (err)
10068 goto err_out;
10069
Johannes Bergaff89a92009-07-01 21:26:51 +020010070#ifdef CONFIG_NL80211_TESTMODE
10071 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10072 if (err)
10073 goto err_out;
10074#endif
10075
Jouni Malinen026331c2010-02-15 12:53:10 +020010076 err = netlink_register_notifier(&nl80211_netlink_notifier);
10077 if (err)
10078 goto err_out;
10079
Johannes Berg55682962007-09-20 13:09:35 -040010080 return 0;
10081 err_out:
10082 genl_unregister_family(&nl80211_fam);
10083 return err;
10084}
10085
10086void nl80211_exit(void)
10087{
Jouni Malinen026331c2010-02-15 12:53:10 +020010088 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010089 genl_unregister_family(&nl80211_fam);
10090}